1//===--- CSKY.cpp - Implement CSKY target feature support -----------------===//
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 implements CSKY TargetInfo objects.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CSKY.h"
14
15using namespace clang;
16using namespace clang::targets;
17
18bool CSKYTargetInfo::isValidCPUName(StringRef Name) const {
19 return llvm::CSKY::parseCPUArch(CPU: Name) != llvm::CSKY::ArchKind::INVALID;
20}
21
22bool CSKYTargetInfo::setCPU(const std::string &Name) {
23 llvm::CSKY::ArchKind archKind = llvm::CSKY::parseCPUArch(CPU: Name);
24 bool isValid = (archKind != llvm::CSKY::ArchKind::INVALID);
25
26 if (isValid) {
27 CPU = Name;
28 Arch = archKind;
29 }
30
31 return isValid;
32}
33
34void CSKYTargetInfo::getTargetDefines(const LangOptions &Opts,
35 MacroBuilder &Builder) const {
36 Builder.defineMacro(Name: "__csky__", Value: "2");
37 Builder.defineMacro(Name: "__CSKY__", Value: "2");
38 Builder.defineMacro(Name: "__ckcore__", Value: "2");
39 Builder.defineMacro(Name: "__CKCORE__", Value: "2");
40
41 Builder.defineMacro(Name: "__CSKYABI__", Value: ABI == "abiv2" ? "2" : "1");
42 Builder.defineMacro(Name: "__cskyabi__", Value: ABI == "abiv2" ? "2" : "1");
43
44 StringRef ArchName = "ck810";
45 StringRef CPUName = "ck810";
46
47 if (Arch != llvm::CSKY::ArchKind::INVALID) {
48 ArchName = llvm::CSKY::getArchName(AK: Arch);
49 CPUName = CPU;
50 }
51
52 Builder.defineMacro(Name: "__" + ArchName.upper() + "__");
53 Builder.defineMacro(Name: "__" + ArchName.lower() + "__");
54 if (ArchName != CPUName) {
55 Builder.defineMacro(Name: "__" + CPUName.upper() + "__");
56 Builder.defineMacro(Name: "__" + CPUName.lower() + "__");
57 }
58
59 // TODO: Add support for BE if BE was supported later
60 StringRef endian = "__cskyLE__";
61
62 Builder.defineMacro(Name: endian);
63 Builder.defineMacro(Name: endian.upper());
64 Builder.defineMacro(Name: endian.lower());
65
66 if (DSPV2) {
67 StringRef dspv2 = "__CSKY_DSPV2__";
68 Builder.defineMacro(Name: dspv2);
69 Builder.defineMacro(Name: dspv2.lower());
70 }
71
72 if (VDSPV2) {
73 StringRef vdspv2 = "__CSKY_VDSPV2__";
74 Builder.defineMacro(Name: vdspv2);
75 Builder.defineMacro(Name: vdspv2.lower());
76
77 if (HardFloat) {
78 StringRef vdspv2_f = "__CSKY_VDSPV2_F__";
79 Builder.defineMacro(Name: vdspv2_f);
80 Builder.defineMacro(Name: vdspv2_f.lower());
81 }
82 }
83 if (VDSPV1) {
84 StringRef vdspv1_64 = "__CSKY_VDSP64__";
85 StringRef vdspv1_128 = "__CSKY_VDSP128__";
86
87 Builder.defineMacro(Name: vdspv1_64);
88 Builder.defineMacro(Name: vdspv1_64.lower());
89 Builder.defineMacro(Name: vdspv1_128);
90 Builder.defineMacro(Name: vdspv1_128.lower());
91 }
92 if (is3E3R1) {
93 StringRef is3e3r1 = "__CSKY_3E3R1__";
94 Builder.defineMacro(Name: is3e3r1);
95 Builder.defineMacro(Name: is3e3r1.lower());
96 }
97}
98
99bool CSKYTargetInfo::hasFeature(StringRef Feature) const {
100 return llvm::StringSwitch<bool>(Feature)
101 .Case(S: "hard-float", Value: HardFloat)
102 .Case(S: "hard-float-abi", Value: HardFloatABI)
103 .Case(S: "fpuv2_sf", Value: FPUV2_SF)
104 .Case(S: "fpuv2_df", Value: FPUV2_DF)
105 .Case(S: "fpuv3_sf", Value: FPUV3_SF)
106 .Case(S: "fpuv3_df", Value: FPUV3_DF)
107 .Case(S: "vdspv2", Value: VDSPV2)
108 .Case(S: "dspv2", Value: DSPV2)
109 .Case(S: "vdspv1", Value: VDSPV1)
110 .Case(S: "3e3r1", Value: is3E3R1)
111 .Default(Value: false);
112}
113
114bool CSKYTargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
115 DiagnosticsEngine &Diags) {
116 for (const auto &Feature : Features) {
117 if (Feature == "+hard-float")
118 HardFloat = true;
119 if (Feature == "+hard-float-abi")
120 HardFloatABI = true;
121 if (Feature == "+fpuv2_sf")
122 FPUV2_SF = true;
123 if (Feature == "+fpuv2_df")
124 FPUV2_DF = true;
125 if (Feature == "+fpuv3_sf")
126 FPUV3_SF = true;
127 if (Feature == "+fpuv3_df")
128 FPUV3_DF = true;
129 if (Feature == "+vdspv2")
130 VDSPV2 = true;
131 if (Feature == "+dspv2")
132 DSPV2 = true;
133 if (Feature == "+vdspv1")
134 VDSPV1 = true;
135 if (Feature == "+3e3r1")
136 is3E3R1 = true;
137 }
138
139 return true;
140}
141
142ArrayRef<Builtin::Info> CSKYTargetInfo::getTargetBuiltins() const {
143 return ArrayRef<Builtin::Info>();
144}
145
146ArrayRef<const char *> CSKYTargetInfo::getGCCRegNames() const {
147 static const char *const GCCRegNames[] = {
148 // Integer registers
149 "r0",
150 "r1",
151 "r2",
152 "r3",
153 "r4",
154 "r5",
155 "r6",
156 "r7",
157 "r8",
158 "r9",
159 "r10",
160 "r11",
161 "r12",
162 "r13",
163 "r14",
164 "r15",
165 "r16",
166 "r17",
167 "r18",
168 "r19",
169 "r20",
170 "r21",
171 "r22",
172 "r23",
173 "r24",
174 "r25",
175 "r26",
176 "r27",
177 "r28",
178 "r29",
179 "r30",
180 "r31",
181
182 // Floating point registers
183 "fr0",
184 "fr1",
185 "fr2",
186 "fr3",
187 "fr4",
188 "fr5",
189 "fr6",
190 "fr7",
191 "fr8",
192 "fr9",
193 "fr10",
194 "fr11",
195 "fr12",
196 "fr13",
197 "fr14",
198 "fr15",
199 "fr16",
200 "fr17",
201 "fr18",
202 "fr19",
203 "fr20",
204 "fr21",
205 "fr22",
206 "fr23",
207 "fr24",
208 "fr25",
209 "fr26",
210 "fr27",
211 "fr28",
212 "fr29",
213 "fr30",
214 "fr31",
215
216 };
217 return llvm::ArrayRef(GCCRegNames);
218}
219
220ArrayRef<TargetInfo::GCCRegAlias> CSKYTargetInfo::getGCCRegAliases() const {
221 static const TargetInfo::GCCRegAlias GCCRegAliases[] = {
222 {.Aliases: {"a0"}, .Register: "r0"},
223 {.Aliases: {"a1"}, .Register: "r1"},
224 {.Aliases: {"a2"}, .Register: "r2"},
225 {.Aliases: {"a3"}, .Register: "r3"},
226 {.Aliases: {"l0"}, .Register: "r4"},
227 {.Aliases: {"l1"}, .Register: "r5"},
228 {.Aliases: {"l2"}, .Register: "r6"},
229 {.Aliases: {"l3"}, .Register: "r7"},
230 {.Aliases: {"l4"}, .Register: "r8"},
231 {.Aliases: {"l5"}, .Register: "r9"},
232 {.Aliases: {"l6"}, .Register: "r10"},
233 {.Aliases: {"l7"}, .Register: "r11"},
234 {.Aliases: {"t0"}, .Register: "r12"},
235 {.Aliases: {"t1"}, .Register: "r13"},
236 {.Aliases: {"sp"}, .Register: "r14"},
237 {.Aliases: {"lr"}, .Register: "r15"},
238 {.Aliases: {"l8"}, .Register: "r16"},
239 {.Aliases: {"l9"}, .Register: "r17"},
240 {.Aliases: {"t2"}, .Register: "r18"},
241 {.Aliases: {"t3"}, .Register: "r19"},
242 {.Aliases: {"t4"}, .Register: "r20"},
243 {.Aliases: {"t5"}, .Register: "r21"},
244 {.Aliases: {"t6"}, .Register: "r22"},
245 {.Aliases: {"t7", "fp"}, .Register: "r23"},
246 {.Aliases: {"t8", "top"}, .Register: "r24"},
247 {.Aliases: {"t9", "bsp"}, .Register: "r25"},
248 {.Aliases: {"r26"}, .Register: "r26"},
249 {.Aliases: {"r27"}, .Register: "r27"},
250 {.Aliases: {"gb", "rgb", "rdb"}, .Register: "r28"},
251 {.Aliases: {"tb", "rtb"}, .Register: "r29"},
252 {.Aliases: {"svbr"}, .Register: "r30"},
253 {.Aliases: {"tls"}, .Register: "r31"},
254
255 {.Aliases: {"vr0"}, .Register: "fr0"},
256 {.Aliases: {"vr1"}, .Register: "fr1"},
257 {.Aliases: {"vr2"}, .Register: "fr2"},
258 {.Aliases: {"vr3"}, .Register: "fr3"},
259 {.Aliases: {"vr4"}, .Register: "fr4"},
260 {.Aliases: {"vr5"}, .Register: "fr5"},
261 {.Aliases: {"vr6"}, .Register: "fr6"},
262 {.Aliases: {"vr7"}, .Register: "fr7"},
263 {.Aliases: {"vr8"}, .Register: "fr8"},
264 {.Aliases: {"vr9"}, .Register: "fr9"},
265 {.Aliases: {"vr10"}, .Register: "fr10"},
266 {.Aliases: {"vr11"}, .Register: "fr11"},
267 {.Aliases: {"vr12"}, .Register: "fr12"},
268 {.Aliases: {"vr13"}, .Register: "fr13"},
269 {.Aliases: {"vr14"}, .Register: "fr14"},
270 {.Aliases: {"vr15"}, .Register: "fr15"},
271 {.Aliases: {"vr16"}, .Register: "fr16"},
272 {.Aliases: {"vr17"}, .Register: "fr17"},
273 {.Aliases: {"vr18"}, .Register: "fr18"},
274 {.Aliases: {"vr19"}, .Register: "fr19"},
275 {.Aliases: {"vr20"}, .Register: "fr20"},
276 {.Aliases: {"vr21"}, .Register: "fr21"},
277 {.Aliases: {"vr22"}, .Register: "fr22"},
278 {.Aliases: {"vr23"}, .Register: "fr23"},
279 {.Aliases: {"vr24"}, .Register: "fr24"},
280 {.Aliases: {"vr25"}, .Register: "fr25"},
281 {.Aliases: {"vr26"}, .Register: "fr26"},
282 {.Aliases: {"vr27"}, .Register: "fr27"},
283 {.Aliases: {"vr28"}, .Register: "fr28"},
284 {.Aliases: {"vr29"}, .Register: "fr29"},
285 {.Aliases: {"vr30"}, .Register: "fr30"},
286 {.Aliases: {"vr31"}, .Register: "fr31"},
287
288 };
289 return llvm::ArrayRef(GCCRegAliases);
290}
291
292bool CSKYTargetInfo::validateAsmConstraint(
293 const char *&Name, TargetInfo::ConstraintInfo &Info) const {
294 switch (*Name) {
295 default:
296 return false;
297 case 'a':
298 case 'b':
299 case 'c':
300 case 'y':
301 case 'l':
302 case 'h':
303 case 'w':
304 case 'v': // A floating-point and vector register.
305 case 'z':
306 Info.setAllowsRegister();
307 return true;
308 }
309}
310
311unsigned CSKYTargetInfo::getMinGlobalAlign(uint64_t Size,
312 bool HasNonWeakDef) const {
313 if (Size >= 32)
314 return 32;
315 return 0;
316}
317