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
21namespace clang {
22namespace targets {
23
24class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
25protected:
26 std::string ABI;
27 std::string CPU;
28 bool HasFeature32S;
29 bool HasFeatureD;
30 bool HasFeatureF;
31 bool HasFeatureLSX;
32 bool HasFeatureLASX;
33 bool HasFeatureFrecipe;
34 bool HasFeatureLAM_BH;
35 bool HasFeatureLAMCAS;
36 bool HasFeatureLD_SEQ_SA;
37 bool HasFeatureDiv32;
38 bool HasFeatureSCQ;
39
40public:
41 LoongArchTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
42 : TargetInfo(Triple) {
43 HasFeature32S = false;
44 HasFeatureD = false;
45 HasFeatureF = false;
46 HasFeatureLSX = false;
47 HasFeatureLASX = false;
48 HasFeatureFrecipe = false;
49 HasFeatureLAM_BH = false;
50 HasFeatureLAMCAS = false;
51 HasFeatureLD_SEQ_SA = false;
52 HasFeatureDiv32 = false;
53 HasFeatureSCQ = false;
54 BFloat16Width = 16;
55 BFloat16Align = 16;
56 BFloat16Format = &llvm::APFloat::BFloat();
57 LongDoubleWidth = 128;
58 LongDoubleAlign = 128;
59 LongDoubleFormat = &llvm::APFloat::IEEEquad();
60 MCountName = "_mcount";
61 HasFloat16 = true;
62 SuitableAlign = 128;
63 WCharType = SignedInt;
64 WIntType = UnsignedInt;
65 BitIntMaxAlign = 128;
66 }
67
68 bool setCPU(StringRef Name) override {
69 if (!isValidCPUName(Name))
70 return false;
71 CPU = Name;
72 return true;
73 }
74
75 StringRef getCPU() const { return CPU; }
76
77 StringRef getABI() const override { return ABI; }
78
79 void getTargetDefines(const LangOptions &Opts,
80 MacroBuilder &Builder) const override;
81
82 llvm::SmallVector<Builtin::InfosShard> getTargetBuiltins() const override;
83
84 BuiltinVaListKind getBuiltinVaListKind() const override {
85 return TargetInfo::VoidPtrBuiltinVaList;
86 }
87
88 std::string_view getClobbers() const override { return ""; }
89
90 ArrayRef<const char *> getGCCRegNames() const override;
91
92 int getEHDataRegisterNumber(unsigned RegNo) const override {
93 if (RegNo == 0)
94 return 4;
95 if (RegNo == 1)
96 return 5;
97 return -1;
98 }
99
100 ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override;
101
102 bool validateAsmConstraint(const char *&Name,
103 TargetInfo::ConstraintInfo &Info) const override;
104 std::string convertConstraint(const char *&Constraint) const override;
105
106 bool hasBitIntType() const override { return true; }
107
108 bool hasBFloat16Type() const override { return true; }
109
110 bool handleTargetFeatures(std::vector<std::string> &Features,
111 DiagnosticsEngine &Diags) override;
112
113 ParsedTargetAttr parseTargetAttr(StringRef Str) const override;
114 bool supportsTargetAttributeTune() const override { return true; }
115
116 bool
117 initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
118 StringRef CPU,
119 const std::vector<std::string> &FeaturesVec) const override;
120
121 bool hasFeature(StringRef Feature) const override;
122
123 bool isValidCPUName(StringRef Name) const override;
124 void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
125 bool isValidFeatureName(StringRef Name) const override;
126};
127
128class LLVM_LIBRARY_VISIBILITY LoongArch32TargetInfo
129 : public LoongArchTargetInfo {
130public:
131 LoongArch32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
132 : LoongArchTargetInfo(Triple, Opts) {
133 IntPtrType = SignedInt;
134 PtrDiffType = SignedInt;
135 SizeType = UnsignedInt;
136 // TODO: select appropriate ABI.
137 setABI("ilp32d");
138 resetDataLayout();
139 }
140
141 bool setABI(const std::string &Name) override {
142 if (Name == "ilp32d" || Name == "ilp32f" || Name == "ilp32s") {
143 ABI = Name;
144 return true;
145 }
146 return false;
147 }
148 void setMaxAtomicWidth() override {
149 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 32;
150 }
151};
152
153class LLVM_LIBRARY_VISIBILITY LoongArch64TargetInfo
154 : public LoongArchTargetInfo {
155public:
156 LoongArch64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
157 : LoongArchTargetInfo(Triple, Opts) {
158 LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
159 IntMaxType = Int64Type = SignedLong;
160 HasUnalignedAccess = true;
161 // TODO: select appropriate ABI.
162 setABI("lp64d");
163 resetDataLayout();
164 }
165
166 bool setABI(const std::string &Name) override {
167 if (Name == "lp64d" || Name == "lp64f" || Name == "lp64s") {
168 ABI = Name;
169 return true;
170 }
171 return false;
172 }
173 void setMaxAtomicWidth() override {
174 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
175 }
176};
177} // end namespace targets
178} // end namespace clang
179
180#endif // LLVM_CLANG_LIB_BASIC_TARGETS_LOONGARCH_H
181