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