xref: /freebsd/contrib/llvm-project/clang/lib/Basic/Targets/AVR.h (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
10b57cec5SDimitry Andric //===--- AVR.h - Declare AVR target feature support -------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file declares AVR TargetInfo objects.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #ifndef LLVM_CLANG_LIB_BASIC_TARGETS_AVR_H
140b57cec5SDimitry Andric #define LLVM_CLANG_LIB_BASIC_TARGETS_AVR_H
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "clang/Basic/TargetInfo.h"
170b57cec5SDimitry Andric #include "clang/Basic/TargetOptions.h"
180b57cec5SDimitry Andric #include "llvm/ADT/Triple.h"
190b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric namespace clang {
220b57cec5SDimitry Andric namespace targets {
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric // AVR Target
250b57cec5SDimitry Andric class LLVM_LIBRARY_VISIBILITY AVRTargetInfo : public TargetInfo {
260b57cec5SDimitry Andric public:
270b57cec5SDimitry Andric   AVRTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
280b57cec5SDimitry Andric       : TargetInfo(Triple) {
290b57cec5SDimitry Andric     TLSSupported = false;
300b57cec5SDimitry Andric     PointerWidth = 16;
310b57cec5SDimitry Andric     PointerAlign = 8;
320b57cec5SDimitry Andric     IntWidth = 16;
330b57cec5SDimitry Andric     IntAlign = 8;
340b57cec5SDimitry Andric     LongWidth = 32;
350b57cec5SDimitry Andric     LongAlign = 8;
360b57cec5SDimitry Andric     LongLongWidth = 64;
370b57cec5SDimitry Andric     LongLongAlign = 8;
380b57cec5SDimitry Andric     SuitableAlign = 8;
390b57cec5SDimitry Andric     DefaultAlignForAttributeAligned = 8;
400b57cec5SDimitry Andric     HalfWidth = 16;
410b57cec5SDimitry Andric     HalfAlign = 8;
420b57cec5SDimitry Andric     FloatWidth = 32;
430b57cec5SDimitry Andric     FloatAlign = 8;
440b57cec5SDimitry Andric     DoubleWidth = 32;
450b57cec5SDimitry Andric     DoubleAlign = 8;
460b57cec5SDimitry Andric     DoubleFormat = &llvm::APFloat::IEEEsingle();
470b57cec5SDimitry Andric     LongDoubleWidth = 32;
480b57cec5SDimitry Andric     LongDoubleAlign = 8;
490b57cec5SDimitry Andric     LongDoubleFormat = &llvm::APFloat::IEEEsingle();
500b57cec5SDimitry Andric     SizeType = UnsignedInt;
510b57cec5SDimitry Andric     PtrDiffType = SignedInt;
520b57cec5SDimitry Andric     IntPtrType = SignedInt;
530b57cec5SDimitry Andric     Char16Type = UnsignedInt;
540b57cec5SDimitry Andric     WIntType = SignedInt;
55fe6060f1SDimitry Andric     Int16Type = SignedInt;
560b57cec5SDimitry Andric     Char32Type = UnsignedLong;
570b57cec5SDimitry Andric     SigAtomicType = SignedChar;
58*04eeddc0SDimitry Andric     ProgramAddrSpace = 1;
590b57cec5SDimitry Andric     resetDataLayout("e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8");
600b57cec5SDimitry Andric   }
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric   void getTargetDefines(const LangOptions &Opts,
630b57cec5SDimitry Andric                         MacroBuilder &Builder) const override;
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   ArrayRef<Builtin::Info> getTargetBuiltins() const override { return None; }
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   BuiltinVaListKind getBuiltinVaListKind() const override {
680b57cec5SDimitry Andric     return TargetInfo::VoidPtrBuiltinVaList;
690b57cec5SDimitry Andric   }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric   const char *getClobbers() const override { return ""; }
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   ArrayRef<const char *> getGCCRegNames() const override {
740b57cec5SDimitry Andric     static const char *const GCCRegNames[] = {
750b57cec5SDimitry Andric         "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",  "r8",  "r9",
760b57cec5SDimitry Andric         "r10", "r11", "r12", "r13", "r14", "r15", "r16", "r17", "r18", "r19",
770b57cec5SDimitry Andric         "r20", "r21", "r22", "r23", "r24", "r25", "X",   "Y",   "Z",   "SP"
780b57cec5SDimitry Andric     };
790b57cec5SDimitry Andric     return llvm::makeArrayRef(GCCRegNames);
800b57cec5SDimitry Andric   }
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override {
830b57cec5SDimitry Andric     return None;
840b57cec5SDimitry Andric   }
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   ArrayRef<TargetInfo::AddlRegName> getGCCAddlRegNames() const override {
870b57cec5SDimitry Andric     static const TargetInfo::AddlRegName AddlRegNames[] = {
880b57cec5SDimitry Andric         {{"r26", "r27"}, 26},
890b57cec5SDimitry Andric         {{"r28", "r29"}, 27},
900b57cec5SDimitry Andric         {{"r30", "r31"}, 28},
910b57cec5SDimitry Andric         {{"SPL", "SPH"}, 29},
920b57cec5SDimitry Andric     };
930b57cec5SDimitry Andric     return llvm::makeArrayRef(AddlRegNames);
940b57cec5SDimitry Andric   }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   bool validateAsmConstraint(const char *&Name,
970b57cec5SDimitry Andric                              TargetInfo::ConstraintInfo &Info) const override {
980b57cec5SDimitry Andric     // There aren't any multi-character AVR specific constraints.
990b57cec5SDimitry Andric     if (StringRef(Name).size() > 1)
1000b57cec5SDimitry Andric       return false;
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric     switch (*Name) {
1030b57cec5SDimitry Andric     default:
1040b57cec5SDimitry Andric       return false;
1050b57cec5SDimitry Andric     case 'a': // Simple upper registers
1060b57cec5SDimitry Andric     case 'b': // Base pointer registers pairs
1070b57cec5SDimitry Andric     case 'd': // Upper register
1080b57cec5SDimitry Andric     case 'l': // Lower registers
1090b57cec5SDimitry Andric     case 'e': // Pointer register pairs
1100b57cec5SDimitry Andric     case 'q': // Stack pointer register
1110b57cec5SDimitry Andric     case 'r': // Any register
1120b57cec5SDimitry Andric     case 'w': // Special upper register pairs
1130b57cec5SDimitry Andric     case 't': // Temporary register
1140b57cec5SDimitry Andric     case 'x':
1150b57cec5SDimitry Andric     case 'X': // Pointer register pair X
1160b57cec5SDimitry Andric     case 'y':
1170b57cec5SDimitry Andric     case 'Y': // Pointer register pair Y
1180b57cec5SDimitry Andric     case 'z':
1190b57cec5SDimitry Andric     case 'Z': // Pointer register pair Z
1200b57cec5SDimitry Andric       Info.setAllowsRegister();
1210b57cec5SDimitry Andric       return true;
1220b57cec5SDimitry Andric     case 'I': // 6-bit positive integer constant
1230b57cec5SDimitry Andric       Info.setRequiresImmediate(0, 63);
1240b57cec5SDimitry Andric       return true;
1250b57cec5SDimitry Andric     case 'J': // 6-bit negative integer constant
1260b57cec5SDimitry Andric       Info.setRequiresImmediate(-63, 0);
1270b57cec5SDimitry Andric       return true;
1280b57cec5SDimitry Andric     case 'K': // Integer constant (Range: 2)
1290b57cec5SDimitry Andric       Info.setRequiresImmediate(2);
1300b57cec5SDimitry Andric       return true;
1310b57cec5SDimitry Andric     case 'L': // Integer constant (Range: 0)
1320b57cec5SDimitry Andric       Info.setRequiresImmediate(0);
1330b57cec5SDimitry Andric       return true;
1340b57cec5SDimitry Andric     case 'M': // 8-bit integer constant
1350b57cec5SDimitry Andric       Info.setRequiresImmediate(0, 0xff);
1360b57cec5SDimitry Andric       return true;
1370b57cec5SDimitry Andric     case 'N': // Integer constant (Range: -1)
1380b57cec5SDimitry Andric       Info.setRequiresImmediate(-1);
1390b57cec5SDimitry Andric       return true;
1400b57cec5SDimitry Andric     case 'O': // Integer constant (Range: 8, 16, 24)
1410b57cec5SDimitry Andric       Info.setRequiresImmediate({8, 16, 24});
1420b57cec5SDimitry Andric       return true;
1430b57cec5SDimitry Andric     case 'P': // Integer constant (Range: 1)
1440b57cec5SDimitry Andric       Info.setRequiresImmediate(1);
1450b57cec5SDimitry Andric       return true;
1460b57cec5SDimitry Andric     case 'R': // Integer constant (Range: -6 to 5)
1470b57cec5SDimitry Andric       Info.setRequiresImmediate(-6, 5);
1480b57cec5SDimitry Andric       return true;
1490b57cec5SDimitry Andric     case 'G': // Floating point constant
1500b57cec5SDimitry Andric     case 'Q': // A memory address based on Y or Z pointer with displacement.
1510b57cec5SDimitry Andric       return true;
1520b57cec5SDimitry Andric     }
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric     return false;
1550b57cec5SDimitry Andric   }
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric   IntType getIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {
1580b57cec5SDimitry Andric     // AVR prefers int for 16-bit integers.
1590b57cec5SDimitry Andric     return BitWidth == 16 ? (IsSigned ? SignedInt : UnsignedInt)
1600b57cec5SDimitry Andric                           : TargetInfo::getIntTypeByWidth(BitWidth, IsSigned);
1610b57cec5SDimitry Andric   }
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   IntType getLeastIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {
1640b57cec5SDimitry Andric     // AVR uses int for int_least16_t and int_fast16_t.
1650b57cec5SDimitry Andric     return BitWidth == 16
1660b57cec5SDimitry Andric                ? (IsSigned ? SignedInt : UnsignedInt)
1670b57cec5SDimitry Andric                : TargetInfo::getLeastIntTypeByWidth(BitWidth, IsSigned);
1680b57cec5SDimitry Andric   }
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric   bool isValidCPUName(StringRef Name) const override;
1710b57cec5SDimitry Andric   void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
1720b57cec5SDimitry Andric   bool setCPU(const std::string &Name) override {
1730b57cec5SDimitry Andric     bool isValid = isValidCPUName(Name);
1740b57cec5SDimitry Andric     if (isValid)
1750b57cec5SDimitry Andric       CPU = Name;
1760b57cec5SDimitry Andric     return isValid;
1770b57cec5SDimitry Andric   }
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric protected:
1800b57cec5SDimitry Andric   std::string CPU;
1810b57cec5SDimitry Andric };
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric } // namespace targets
1840b57cec5SDimitry Andric } // namespace clang
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric #endif // LLVM_CLANG_LIB_BASIC_TARGETS_AVR_H
187