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