xref: /llvm-project/clang/lib/Basic/Targets/LoongArch.h (revision 19834b4623fd1e7ae5185ed76031b407c3fa7a47)
1 //===-- LoongArch.h - Declare LoongArch 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 LoongArch TargetInfo objects.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LIB_BASIC_TARGETS_LOONGARCH_H
14 #define LLVM_CLANG_LIB_BASIC_TARGETS_LOONGARCH_H
15 
16 #include "clang/Basic/TargetInfo.h"
17 #include "clang/Basic/TargetOptions.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/TargetParser/Triple.h"
20 
21 namespace clang {
22 namespace targets {
23 
24 class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
25 protected:
26   std::string ABI;
27   std::string CPU;
28   bool HasFeatureD;
29   bool HasFeatureF;
30   bool HasFeatureLSX;
31   bool HasFeatureLASX;
32   bool HasFeatureFrecipe;
33   bool HasFeatureLAM_BH;
34   bool HasFeatureLAMCAS;
35   bool HasFeatureLD_SEQ_SA;
36   bool HasFeatureDiv32;
37   bool HasFeatureSCQ;
38 
39 public:
40   LoongArchTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
41       : TargetInfo(Triple) {
42     HasFeatureD = false;
43     HasFeatureF = false;
44     HasFeatureLSX = false;
45     HasFeatureLASX = false;
46     HasFeatureFrecipe = false;
47     HasFeatureLAM_BH = false;
48     HasFeatureLAMCAS = false;
49     HasFeatureLD_SEQ_SA = false;
50     HasFeatureDiv32 = false;
51     HasFeatureSCQ = false;
52     LongDoubleWidth = 128;
53     LongDoubleAlign = 128;
54     LongDoubleFormat = &llvm::APFloat::IEEEquad();
55     MCountName = "_mcount";
56     SuitableAlign = 128;
57     WCharType = SignedInt;
58     WIntType = UnsignedInt;
59   }
60 
61   bool setCPU(const std::string &Name) override {
62     if (!isValidCPUName(Name))
63       return false;
64     CPU = Name;
65     return true;
66   }
67 
68   StringRef getCPU() const { return CPU; }
69 
70   StringRef getABI() const override { return ABI; }
71 
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   std::string_view 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 4;
88     if (RegNo == 1)
89       return 5;
90     return -1;
91   }
92 
93   ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override;
94 
95   bool validateAsmConstraint(const char *&Name,
96                              TargetInfo::ConstraintInfo &Info) const override;
97   std::string convertConstraint(const char *&Constraint) const override;
98 
99   bool hasBitIntType() const override { return true; }
100 
101   bool handleTargetFeatures(std::vector<std::string> &Features,
102                             DiagnosticsEngine &Diags) override;
103 
104   bool
105   initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
106                  StringRef CPU,
107                  const std::vector<std::string> &FeaturesVec) const override;
108 
109   bool hasFeature(StringRef Feature) const override;
110 
111   bool isValidCPUName(StringRef Name) const override;
112   void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
113 };
114 
115 class LLVM_LIBRARY_VISIBILITY LoongArch32TargetInfo
116     : public LoongArchTargetInfo {
117 public:
118   LoongArch32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
119       : LoongArchTargetInfo(Triple, Opts) {
120     IntPtrType = SignedInt;
121     PtrDiffType = SignedInt;
122     SizeType = UnsignedInt;
123     resetDataLayout("e-m:e-p:32:32-i64:64-n32-S128");
124     // TODO: select appropriate ABI.
125     setABI("ilp32d");
126   }
127 
128   bool setABI(const std::string &Name) override {
129     if (Name == "ilp32d" || Name == "ilp32f" || Name == "ilp32s") {
130       ABI = Name;
131       return true;
132     }
133     return false;
134   }
135   void setMaxAtomicWidth() override {
136     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 32;
137   }
138 };
139 
140 class LLVM_LIBRARY_VISIBILITY LoongArch64TargetInfo
141     : public LoongArchTargetInfo {
142 public:
143   LoongArch64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
144       : LoongArchTargetInfo(Triple, Opts) {
145     LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
146     IntMaxType = Int64Type = SignedLong;
147     HasUnalignedAccess = true;
148     resetDataLayout("e-m:e-p:64:64-i64:64-i128:128-n32:64-S128");
149     // TODO: select appropriate ABI.
150     setABI("lp64d");
151   }
152 
153   bool setABI(const std::string &Name) override {
154     if (Name == "lp64d" || Name == "lp64f" || Name == "lp64s") {
155       ABI = Name;
156       return true;
157     }
158     return false;
159   }
160   void setMaxAtomicWidth() override {
161     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
162   }
163 };
164 } // end namespace targets
165 } // end namespace clang
166 
167 #endif // LLVM_CLANG_LIB_BASIC_TARGETS_LOONGARCH_H
168