xref: /llvm-project/clang/lib/Basic/Targets/RISCV.h (revision 34e055d33e37cd87b9f6f4b0431a4c061628d036)
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     HasFloat16 = true;
64   }
65 
66   bool setCPU(const std::string &Name) override {
67     if (!isValidCPUName(Name))
68       return false;
69     CPU = Name;
70     return true;
71   }
72 
73   StringRef getABI() const override { return ABI; }
74   void getTargetDefines(const LangOptions &Opts,
75                         MacroBuilder &Builder) const override;
76 
77   ArrayRef<Builtin::Info> getTargetBuiltins() const override;
78 
79   BuiltinVaListKind getBuiltinVaListKind() const override {
80     return TargetInfo::VoidPtrBuiltinVaList;
81   }
82 
83   const char *getClobbers() const override { return ""; }
84 
85   StringRef getConstraintRegister(StringRef Constraint,
86                                   StringRef Expression) const override {
87     return Expression;
88   }
89 
90   ArrayRef<const char *> getGCCRegNames() const override;
91 
92   int getEHDataRegisterNumber(unsigned RegNo) const override {
93     if (RegNo == 0)
94       return 10;
95     else if (RegNo == 1)
96       return 11;
97     else
98       return -1;
99   }
100 
101   ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override;
102 
103   bool validateAsmConstraint(const char *&Name,
104                              TargetInfo::ConstraintInfo &Info) const override;
105 
106   std::string convertConstraint(const char *&Constraint) const override;
107 
108   bool
109   initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
110                  StringRef CPU,
111                  const std::vector<std::string> &FeaturesVec) const override;
112 
113   bool hasFeature(StringRef Feature) const override;
114 
115   bool handleTargetFeatures(std::vector<std::string> &Features,
116                             DiagnosticsEngine &Diags) override;
117 
118   bool hasExtIntType() const override { return true; }
119 };
120 class LLVM_LIBRARY_VISIBILITY RISCV32TargetInfo : public RISCVTargetInfo {
121 public:
122   RISCV32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
123       : RISCVTargetInfo(Triple, Opts) {
124     IntPtrType = SignedInt;
125     PtrDiffType = SignedInt;
126     SizeType = UnsignedInt;
127     resetDataLayout("e-m:e-p:32:32-i64:64-n32-S128");
128   }
129 
130   bool setABI(const std::string &Name) override {
131     if (Name == "ilp32" || Name == "ilp32f" || Name == "ilp32d") {
132       ABI = Name;
133       return true;
134     }
135     return false;
136   }
137 
138   bool isValidCPUName(StringRef Name) const override;
139   void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
140   bool isValidTuneCPUName(StringRef Name) const override;
141   void fillValidTuneCPUList(SmallVectorImpl<StringRef> &Values) const override;
142 
143   void setMaxAtomicWidth() override {
144     MaxAtomicPromoteWidth = 128;
145 
146     if (HasA)
147       MaxAtomicInlineWidth = 32;
148   }
149 };
150 class LLVM_LIBRARY_VISIBILITY RISCV64TargetInfo : public RISCVTargetInfo {
151 public:
152   RISCV64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
153       : RISCVTargetInfo(Triple, Opts) {
154     LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
155     IntMaxType = Int64Type = SignedLong;
156     resetDataLayout("e-m:e-p:64:64-i64:64-i128:128-n64-S128");
157   }
158 
159   bool setABI(const std::string &Name) override {
160     if (Name == "lp64" || Name == "lp64f" || Name == "lp64d") {
161       ABI = Name;
162       return true;
163     }
164     return false;
165   }
166 
167   bool isValidCPUName(StringRef Name) const override;
168   void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
169   bool isValidTuneCPUName(StringRef Name) const override;
170   void fillValidTuneCPUList(SmallVectorImpl<StringRef> &Values) const override;
171 
172   void setMaxAtomicWidth() override {
173     MaxAtomicPromoteWidth = 128;
174 
175     if (HasA)
176       MaxAtomicInlineWidth = 64;
177   }
178 };
179 } // namespace targets
180 } // namespace clang
181 
182 #endif // LLVM_CLANG_LIB_BASIC_TARGETS_RISCV_H
183