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