1*6b755b0cSVlad Serebrennikov //===------ SemaMSP430.cpp ----- MSP430 target-specific routines ----------===//
2*6b755b0cSVlad Serebrennikov //
3*6b755b0cSVlad Serebrennikov // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*6b755b0cSVlad Serebrennikov // See https://llvm.org/LICENSE.txt for license information.
5*6b755b0cSVlad Serebrennikov // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*6b755b0cSVlad Serebrennikov //
7*6b755b0cSVlad Serebrennikov //===----------------------------------------------------------------------===//
8*6b755b0cSVlad Serebrennikov //
9*6b755b0cSVlad Serebrennikov // This file implements semantic analysis functions specific to NVPTX.
10*6b755b0cSVlad Serebrennikov //
11*6b755b0cSVlad Serebrennikov //===----------------------------------------------------------------------===//
12*6b755b0cSVlad Serebrennikov
13*6b755b0cSVlad Serebrennikov #include "clang/Sema/SemaMSP430.h"
14*6b755b0cSVlad Serebrennikov #include "clang/AST/ASTContext.h"
15*6b755b0cSVlad Serebrennikov #include "clang/AST/Attr.h"
16*6b755b0cSVlad Serebrennikov #include "clang/AST/DeclBase.h"
17*6b755b0cSVlad Serebrennikov #include "clang/Basic/DiagnosticSema.h"
18*6b755b0cSVlad Serebrennikov #include "clang/Sema/Attr.h"
19*6b755b0cSVlad Serebrennikov #include "clang/Sema/ParsedAttr.h"
20*6b755b0cSVlad Serebrennikov
21*6b755b0cSVlad Serebrennikov namespace clang {
22*6b755b0cSVlad Serebrennikov
SemaMSP430(Sema & S)23*6b755b0cSVlad Serebrennikov SemaMSP430::SemaMSP430(Sema &S) : SemaBase(S) {}
24*6b755b0cSVlad Serebrennikov
handleInterruptAttr(Decl * D,const ParsedAttr & AL)25*6b755b0cSVlad Serebrennikov void SemaMSP430::handleInterruptAttr(Decl *D, const ParsedAttr &AL) {
26*6b755b0cSVlad Serebrennikov // MSP430 'interrupt' attribute is applied to
27*6b755b0cSVlad Serebrennikov // a function with no parameters and void return type.
28*6b755b0cSVlad Serebrennikov if (!isFuncOrMethodForAttrSubject(D)) {
29*6b755b0cSVlad Serebrennikov Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
30*6b755b0cSVlad Serebrennikov << AL << AL.isRegularKeywordAttribute() << ExpectedFunctionOrMethod;
31*6b755b0cSVlad Serebrennikov return;
32*6b755b0cSVlad Serebrennikov }
33*6b755b0cSVlad Serebrennikov
34*6b755b0cSVlad Serebrennikov if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
35*6b755b0cSVlad Serebrennikov Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
36*6b755b0cSVlad Serebrennikov << /*MSP430*/ 1 << 0;
37*6b755b0cSVlad Serebrennikov return;
38*6b755b0cSVlad Serebrennikov }
39*6b755b0cSVlad Serebrennikov
40*6b755b0cSVlad Serebrennikov if (!getFunctionOrMethodResultType(D)->isVoidType()) {
41*6b755b0cSVlad Serebrennikov Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
42*6b755b0cSVlad Serebrennikov << /*MSP430*/ 1 << 1;
43*6b755b0cSVlad Serebrennikov return;
44*6b755b0cSVlad Serebrennikov }
45*6b755b0cSVlad Serebrennikov
46*6b755b0cSVlad Serebrennikov // The attribute takes one integer argument.
47*6b755b0cSVlad Serebrennikov if (!AL.checkExactlyNumArgs(SemaRef, 1))
48*6b755b0cSVlad Serebrennikov return;
49*6b755b0cSVlad Serebrennikov
50*6b755b0cSVlad Serebrennikov if (!AL.isArgExpr(0)) {
51*6b755b0cSVlad Serebrennikov Diag(AL.getLoc(), diag::err_attribute_argument_type)
52*6b755b0cSVlad Serebrennikov << AL << AANT_ArgumentIntegerConstant;
53*6b755b0cSVlad Serebrennikov return;
54*6b755b0cSVlad Serebrennikov }
55*6b755b0cSVlad Serebrennikov
56*6b755b0cSVlad Serebrennikov Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
57*6b755b0cSVlad Serebrennikov std::optional<llvm::APSInt> NumParams = llvm::APSInt(32);
58*6b755b0cSVlad Serebrennikov if (!(NumParams = NumParamsExpr->getIntegerConstantExpr(getASTContext()))) {
59*6b755b0cSVlad Serebrennikov Diag(AL.getLoc(), diag::err_attribute_argument_type)
60*6b755b0cSVlad Serebrennikov << AL << AANT_ArgumentIntegerConstant
61*6b755b0cSVlad Serebrennikov << NumParamsExpr->getSourceRange();
62*6b755b0cSVlad Serebrennikov return;
63*6b755b0cSVlad Serebrennikov }
64*6b755b0cSVlad Serebrennikov // The argument should be in range 0..63.
65*6b755b0cSVlad Serebrennikov unsigned Num = NumParams->getLimitedValue(255);
66*6b755b0cSVlad Serebrennikov if (Num > 63) {
67*6b755b0cSVlad Serebrennikov Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
68*6b755b0cSVlad Serebrennikov << AL << (int)NumParams->getSExtValue()
69*6b755b0cSVlad Serebrennikov << NumParamsExpr->getSourceRange();
70*6b755b0cSVlad Serebrennikov return;
71*6b755b0cSVlad Serebrennikov }
72*6b755b0cSVlad Serebrennikov
73*6b755b0cSVlad Serebrennikov D->addAttr(::new (getASTContext())
74*6b755b0cSVlad Serebrennikov MSP430InterruptAttr(getASTContext(), AL, Num));
75*6b755b0cSVlad Serebrennikov D->addAttr(UsedAttr::CreateImplicit(getASTContext()));
76*6b755b0cSVlad Serebrennikov }
77*6b755b0cSVlad Serebrennikov
78*6b755b0cSVlad Serebrennikov } // namespace clang
79