1//===- MSP430.cpp ---------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "ABIInfoImpl.h"
10#include "TargetInfo.h"
11
12using namespace clang;
13using namespace clang::CodeGen;
14
15//===----------------------------------------------------------------------===//
16// MSP430 ABI Implementation
17//===----------------------------------------------------------------------===//
18
19namespace {
20
21class MSP430ABIInfo : public DefaultABIInfo {
22 static ABIArgInfo complexArgInfo() {
23 ABIArgInfo Info = ABIArgInfo::getDirect();
24 Info.setCanBeFlattened(false);
25 return Info;
26 }
27
28public:
29 MSP430ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
30
31 ABIArgInfo classifyReturnType(QualType RetTy) const {
32 if (RetTy->isAnyComplexType())
33 return complexArgInfo();
34
35 return DefaultABIInfo::classifyReturnType(RetTy);
36 }
37
38 ABIArgInfo classifyArgumentType(QualType RetTy) const {
39 if (RetTy->isAnyComplexType())
40 return complexArgInfo();
41
42 return DefaultABIInfo::classifyArgumentType(RetTy);
43 }
44
45 // Just copy the original implementations because
46 // DefaultABIInfo::classify{Return,Argument}Type() are not virtual
47 void computeInfo(CGFunctionInfo &FI) const override {
48 if (!getCXXABI().classifyReturnType(FI))
49 FI.getReturnInfo() = classifyReturnType(RetTy: FI.getReturnType());
50 for (auto &I : FI.arguments())
51 I.info = classifyArgumentType(RetTy: I.type);
52 }
53
54 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
55 QualType Ty) const override {
56 return EmitVAArgInstr(CGF, VAListAddr, Ty, AI: classifyArgumentType(RetTy: Ty));
57 }
58};
59
60class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
61public:
62 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
63 : TargetCodeGenInfo(std::make_unique<MSP430ABIInfo>(args&: CGT)) {}
64 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
65 CodeGen::CodeGenModule &M) const override;
66};
67
68}
69
70void MSP430TargetCodeGenInfo::setTargetAttributes(
71 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
72 if (GV->isDeclaration())
73 return;
74 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Val: D)) {
75 const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>();
76 if (!InterruptAttr)
77 return;
78
79 // Handle 'interrupt' attribute:
80 llvm::Function *F = cast<llvm::Function>(Val: GV);
81
82 // Step 1: Set ISR calling convention.
83 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
84
85 // Step 2: Add attributes goodness.
86 F->addFnAttr(llvm::Attribute::NoInline);
87 F->addFnAttr("interrupt", llvm::utostr(X: InterruptAttr->getNumber()));
88 }
89}
90
91std::unique_ptr<TargetCodeGenInfo>
92CodeGen::createMSP430TargetCodeGenInfo(CodeGenModule &CGM) {
93 return std::make_unique<MSP430TargetCodeGenInfo>(args&: CGM.getTypes());
94}
95

source code of clang/lib/CodeGen/Targets/MSP430.cpp