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