xref: /llvm-project/clang/lib/Basic/Targets/RISCV.h (revision 5821a58d8e4c5510a4ab30fa758a9d22f41c346a)
1 //===--- RISCV.h - Declare RISCV target feature support ---------*- C++ -*-===//
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 // This file declares RISCV TargetInfo objects.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LIB_BASIC_TARGETS_RISCV_H
14 #define LLVM_CLANG_LIB_BASIC_TARGETS_RISCV_H
15 
16 #include "clang/Basic/TargetInfo.h"
17 #include "clang/Basic/TargetOptions.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Support/Compiler.h"
20 
21 namespace clang {
22 namespace targets {
23 
24 // RISC-V Target
25 class RISCVTargetInfo : public TargetInfo {
26 protected:
27   std::string ABI, CPU;
28   bool HasM = false;
29   bool HasA = false;
30   bool HasF = false;
31   bool HasD = false;
32   bool HasC = false;
33   bool HasB = false;
34   bool HasV = false;
35   bool HasZba = false;
36   bool HasZbb = false;
37   bool HasZbc = false;
38   bool HasZbe = false;
39   bool HasZbf = false;
40   bool HasZbm = false;
41   bool HasZbp = false;
42   bool HasZbproposedc = false;
43   bool HasZbr = false;
44   bool HasZbs = false;
45   bool HasZbt = false;
46   bool HasZfh = false;
47   bool HasZvamo = false;
48   bool HasZvlsseg = false;
49 
50   static const Builtin::Info BuiltinInfo[];
51 
52 public:
53   RISCVTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
54       : TargetInfo(Triple) {
55     LongDoubleWidth = 128;
56     LongDoubleAlign = 128;
57     LongDoubleFormat = &llvm::APFloat::IEEEquad();
58     SuitableAlign = 128;
59     WCharType = SignedInt;
60     WIntType = UnsignedInt;
61     HasRISCVVTypes = true;
62     MCountName = "_mcount";
63   }
64 
65   bool setCPU(const std::string &Name) override {
66     if (!isValidCPUName(Name))
67       return false;
68     CPU = Name;
69     return true;
70   }
71 
72   StringRef getABI() const override { return ABI; }
73   void getTargetDefines(const LangOptions &Opts,
74                         MacroBuilder &Builder) const override;
75 
76   ArrayRef<Builtin::Info> getTargetBuiltins() const override;
77 
78   BuiltinVaListKind getBuiltinVaListKind() const override {
79     return TargetInfo::VoidPtrBuiltinVaList;
80   }
81 
82   const char *getClobbers() const override { return ""; }
83 
84   ArrayRef<const char *> getGCCRegNames() const override;
85 
86   int getEHDataRegisterNumber(unsigned RegNo) const override {
87     if (RegNo == 0)
88       return 10;
89     else if (RegNo == 1)
90       return 11;
91     else
92       return -1;
93   }
94 
95   ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override;
96 
97   bool validateAsmConstraint(const char *&Name,
98                              TargetInfo::ConstraintInfo &Info) const override;
99 
100   std::string convertConstraint(const char *&Constraint) const override;
101 
102   bool hasFeature(StringRef Feature) const override;
103 
104   bool handleTargetFeatures(std::vector<std::string> &Features,
105                             DiagnosticsEngine &Diags) override;
106 
107   bool hasExtIntType() const override { return true; }
108 };
109 class LLVM_LIBRARY_VISIBILITY RISCV32TargetInfo : public RISCVTargetInfo {
110 public:
111   RISCV32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
112       : RISCVTargetInfo(Triple, Opts) {
113     IntPtrType = SignedInt;
114     PtrDiffType = SignedInt;
115     SizeType = UnsignedInt;
116     resetDataLayout("e-m:e-p:32:32-i64:64-n32-S128");
117   }
118 
119   bool setABI(const std::string &Name) override {
120     if (Name == "ilp32" || Name == "ilp32f" || Name == "ilp32d") {
121       ABI = Name;
122       return true;
123     }
124     return false;
125   }
126 
127   bool isValidCPUName(StringRef Name) const override;
128   void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
129   bool isValidTuneCPUName(StringRef Name) const override;
130   void fillValidTuneCPUList(SmallVectorImpl<StringRef> &Values) const override;
131 
132   void setMaxAtomicWidth() override {
133     MaxAtomicPromoteWidth = 128;
134 
135     if (HasA)
136       MaxAtomicInlineWidth = 32;
137   }
138 };
139 class LLVM_LIBRARY_VISIBILITY RISCV64TargetInfo : public RISCVTargetInfo {
140 public:
141   RISCV64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
142       : RISCVTargetInfo(Triple, Opts) {
143     LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
144     IntMaxType = Int64Type = SignedLong;
145     resetDataLayout("e-m:e-p:64:64-i64:64-i128:128-n64-S128");
146   }
147 
148   bool setABI(const std::string &Name) override {
149     if (Name == "lp64" || Name == "lp64f" || Name == "lp64d") {
150       ABI = Name;
151       return true;
152     }
153     return false;
154   }
155 
156   bool isValidCPUName(StringRef Name) const override;
157   void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
158   bool isValidTuneCPUName(StringRef Name) const override;
159   void fillValidTuneCPUList(SmallVectorImpl<StringRef> &Values) const override;
160 
161   void setMaxAtomicWidth() override {
162     MaxAtomicPromoteWidth = 128;
163 
164     if (HasA)
165       MaxAtomicInlineWidth = 64;
166   }
167 };
168 } // namespace targets
169 } // namespace clang
170 
171 #endif // LLVM_CLANG_LIB_BASIC_TARGETS_RISCV_H
172