|
| 1 | +//===- Z80.cpp - Z80 ABI Implementation -----------------------------------===// |
| 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 | +#include "clang/AST/DeclCXX.h" |
| 12 | + |
| 13 | +using namespace clang; |
| 14 | +using namespace clang::CodeGen; |
| 15 | + |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | +// Z80 ABI Implementation |
| 18 | +//===----------------------------------------------------------------------===// |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +class Z80ABIInfo : public DefaultABIInfo { |
| 23 | +public: |
| 24 | + Z80ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 25 | + |
| 26 | +private: |
| 27 | + void removeExtend(ABIArgInfo &AI) const { |
| 28 | + if (AI.isExtend()) { |
| 29 | + bool InReg = AI.getInReg(); |
| 30 | + AI = ABIArgInfo::getDirect(AI.getCoerceToType()); |
| 31 | + AI.setInReg(InReg); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + void computeInfo(CGFunctionInfo &FI) const override { |
| 36 | + if (!getCXXABI().classifyReturnType(FI)) |
| 37 | + FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 38 | + removeExtend(FI.getReturnInfo()); |
| 39 | + for (auto &Arg : FI.arguments()) |
| 40 | + removeExtend(Arg.info = classifyArgumentType(Arg.type)); |
| 41 | + } |
| 42 | + |
| 43 | + Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 44 | + QualType Ty) const override; |
| 45 | +}; |
| 46 | + |
| 47 | +class Z80TargetCodeGenInfo : public TargetCodeGenInfo { |
| 48 | +public: |
| 49 | + Z80TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 50 | + : TargetCodeGenInfo(std::make_unique<Z80ABIInfo>(CGT)) {} |
| 51 | + |
| 52 | + void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 53 | + CodeGen::CodeGenModule &CGM) const override; |
| 54 | +}; |
| 55 | + |
| 56 | +} // namespace |
| 57 | + |
| 58 | +Address Z80ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 59 | + QualType Ty) const { |
| 60 | + ABIArgInfo AI = classifyArgumentType(Ty); |
| 61 | + removeExtend(AI); |
| 62 | + return EmitVAArgInstr(CGF, VAListAddr, Ty, AI); |
| 63 | +} |
| 64 | + |
| 65 | +void Z80TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
| 66 | + llvm::GlobalValue *GV, |
| 67 | + CodeGen::CodeGenModule &CGM) const { |
| 68 | + const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 69 | + if (!FD) |
| 70 | + return; |
| 71 | + llvm::Function *Fn = cast<llvm::Function>(GV); |
| 72 | + |
| 73 | + if (Fn->isDeclaration()) { |
| 74 | + if (FD->getAttr<AnyZ80TIFlagsAttr>()) |
| 75 | + Fn->setCallingConv(llvm::CallingConv::Z80_TIFlags); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + const AnyZ80InterruptAttr *Attr = FD->getAttr<AnyZ80InterruptAttr>(); |
| 80 | + if (!Attr) |
| 81 | + return; |
| 82 | + |
| 83 | + const char *Kind; |
| 84 | + switch (Attr->getInterrupt()) { |
| 85 | + case AnyZ80InterruptAttr::Generic: |
| 86 | + Kind = "Generic"; |
| 87 | + break; |
| 88 | + case AnyZ80InterruptAttr::Nested: |
| 89 | + Kind = "Nested"; |
| 90 | + break; |
| 91 | + case AnyZ80InterruptAttr::NMI: |
| 92 | + Kind = "NMI"; |
| 93 | + break; |
| 94 | + } |
| 95 | + |
| 96 | + Fn->setCallingConv(llvm::CallingConv::PreserveAll); |
| 97 | + Fn->addFnAttr("interrupt", Kind); |
| 98 | +} |
| 99 | + |
| 100 | +std::unique_ptr<TargetCodeGenInfo> |
| 101 | +CodeGen::createZ80TargetCodeGenInfo(CodeGenModule &CGM) { |
| 102 | + return std::make_unique<Z80TargetCodeGenInfo>(CGM.getTypes()); |
| 103 | +} |
0 commit comments