blob: 73baac832ee307d4c7855bab0501159c183eb594 [file] [log] [blame]
David Spickette01718c2018-11-28 11:38:10 +00001//===-- ARMTargetParser - Parser for ARM target features --------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
David Spickette01718c2018-11-28 11:38:10 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a target parser to recognise ARM hardware features
10// such as FPU/CPU/ARCH/extensions and specific support such as HWDIV.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/ARMTargetParser.h"
15#include "llvm/ADT/StringSwitch.h"
Simon Pilgrimffd9cfa2020-05-07 17:14:14 +010016#include "llvm/ADT/Triple.h"
David Spickettcc3fa392018-11-28 15:12:33 +000017#include <cctype>
David Spickette01718c2018-11-28 11:38:10 +000018
19using namespace llvm;
20
21static StringRef getHWDivSynonym(StringRef HWDiv) {
22 return StringSwitch<StringRef>(HWDiv)
23 .Case("thumb,arm", "arm,thumb")
24 .Default(HWDiv);
25}
26
27// Allows partial match, ex. "v7a" matches "armv7a".
28ARM::ArchKind ARM::parseArch(StringRef Arch) {
29 Arch = getCanonicalArchName(Arch);
30 StringRef Syn = getArchSynonym(Arch);
Reid Kleckner7982db52020-02-04 15:48:22 -080031 for (const auto &A : ARCHNames) {
David Spickette01718c2018-11-28 11:38:10 +000032 if (A.getName().endswith(Syn))
33 return A.ID;
34 }
35 return ArchKind::INVALID;
36}
37
38// Version number (ex. v7 = 7).
39unsigned ARM::parseArchVersion(StringRef Arch) {
40 Arch = getCanonicalArchName(Arch);
41 switch (parseArch(Arch)) {
42 case ArchKind::ARMV2:
43 case ArchKind::ARMV2A:
44 return 2;
45 case ArchKind::ARMV3:
46 case ArchKind::ARMV3M:
47 return 3;
48 case ArchKind::ARMV4:
49 case ArchKind::ARMV4T:
50 return 4;
51 case ArchKind::ARMV5T:
52 case ArchKind::ARMV5TE:
53 case ArchKind::IWMMXT:
54 case ArchKind::IWMMXT2:
55 case ArchKind::XSCALE:
56 case ArchKind::ARMV5TEJ:
57 return 5;
58 case ArchKind::ARMV6:
59 case ArchKind::ARMV6K:
60 case ArchKind::ARMV6T2:
61 case ArchKind::ARMV6KZ:
62 case ArchKind::ARMV6M:
63 return 6;
64 case ArchKind::ARMV7A:
65 case ArchKind::ARMV7VE:
66 case ArchKind::ARMV7R:
67 case ArchKind::ARMV7M:
68 case ArchKind::ARMV7S:
69 case ArchKind::ARMV7EM:
70 case ArchKind::ARMV7K:
71 return 7;
72 case ArchKind::ARMV8A:
73 case ArchKind::ARMV8_1A:
74 case ArchKind::ARMV8_2A:
75 case ArchKind::ARMV8_3A:
76 case ArchKind::ARMV8_4A:
77 case ArchKind::ARMV8_5A:
Ties Stuij71ae2672020-03-26 08:17:29 +000078 case ArchKind::ARMV8_6A:
David Spickette01718c2018-11-28 11:38:10 +000079 case ArchKind::ARMV8R:
80 case ArchKind::ARMV8MBaseline:
81 case ArchKind::ARMV8MMainline:
Sjoerd Meijer930dee22019-05-30 12:57:04 +000082 case ArchKind::ARMV8_1MMainline:
David Spickette01718c2018-11-28 11:38:10 +000083 return 8;
84 case ArchKind::INVALID:
85 return 0;
86 }
87 llvm_unreachable("Unhandled architecture");
88}
89
90// Profile A/R/M
91ARM::ProfileKind ARM::parseArchProfile(StringRef Arch) {
92 Arch = getCanonicalArchName(Arch);
93 switch (parseArch(Arch)) {
94 case ArchKind::ARMV6M:
95 case ArchKind::ARMV7M:
96 case ArchKind::ARMV7EM:
97 case ArchKind::ARMV8MMainline:
98 case ArchKind::ARMV8MBaseline:
Sjoerd Meijer930dee22019-05-30 12:57:04 +000099 case ArchKind::ARMV8_1MMainline:
David Spickette01718c2018-11-28 11:38:10 +0000100 return ProfileKind::M;
101 case ArchKind::ARMV7R:
102 case ArchKind::ARMV8R:
103 return ProfileKind::R;
104 case ArchKind::ARMV7A:
105 case ArchKind::ARMV7VE:
106 case ArchKind::ARMV7K:
107 case ArchKind::ARMV8A:
108 case ArchKind::ARMV8_1A:
109 case ArchKind::ARMV8_2A:
110 case ArchKind::ARMV8_3A:
111 case ArchKind::ARMV8_4A:
112 case ArchKind::ARMV8_5A:
Ties Stuij71ae2672020-03-26 08:17:29 +0000113 case ArchKind::ARMV8_6A:
David Spickette01718c2018-11-28 11:38:10 +0000114 return ProfileKind::A;
115 case ArchKind::ARMV2:
116 case ArchKind::ARMV2A:
117 case ArchKind::ARMV3:
118 case ArchKind::ARMV3M:
119 case ArchKind::ARMV4:
120 case ArchKind::ARMV4T:
121 case ArchKind::ARMV5T:
122 case ArchKind::ARMV5TE:
123 case ArchKind::ARMV5TEJ:
124 case ArchKind::ARMV6:
125 case ArchKind::ARMV6K:
126 case ArchKind::ARMV6T2:
127 case ArchKind::ARMV6KZ:
128 case ArchKind::ARMV7S:
129 case ArchKind::IWMMXT:
130 case ArchKind::IWMMXT2:
131 case ArchKind::XSCALE:
132 case ArchKind::INVALID:
133 return ProfileKind::INVALID;
134 }
135 llvm_unreachable("Unhandled architecture");
136}
137
138StringRef ARM::getArchSynonym(StringRef Arch) {
139 return StringSwitch<StringRef>(Arch)
140 .Case("v5", "v5t")
141 .Case("v5e", "v5te")
142 .Case("v6j", "v6")
143 .Case("v6hl", "v6k")
144 .Cases("v6m", "v6sm", "v6s-m", "v6-m")
145 .Cases("v6z", "v6zk", "v6kz")
146 .Cases("v7", "v7a", "v7hl", "v7l", "v7-a")
147 .Case("v7r", "v7-r")
148 .Case("v7m", "v7-m")
149 .Case("v7em", "v7e-m")
150 .Cases("v8", "v8a", "v8l", "aarch64", "arm64", "v8-a")
151 .Case("v8.1a", "v8.1-a")
152 .Case("v8.2a", "v8.2-a")
153 .Case("v8.3a", "v8.3-a")
154 .Case("v8.4a", "v8.4-a")
155 .Case("v8.5a", "v8.5-a")
Ties Stuij71ae2672020-03-26 08:17:29 +0000156 .Case("v8.6a", "v8.6-a")
David Spickette01718c2018-11-28 11:38:10 +0000157 .Case("v8r", "v8-r")
158 .Case("v8m.base", "v8-m.base")
159 .Case("v8m.main", "v8-m.main")
Sjoerd Meijer930dee22019-05-30 12:57:04 +0000160 .Case("v8.1m.main", "v8.1-m.main")
David Spickette01718c2018-11-28 11:38:10 +0000161 .Default(Arch);
162}
163
164bool ARM::getFPUFeatures(unsigned FPUKind, std::vector<StringRef> &Features) {
165
166 if (FPUKind >= FK_LAST || FPUKind == FK_INVALID)
167 return false;
168
Simon Tatham5d66f2b2019-06-07 12:42:54 +0000169 static const struct FPUFeatureNameInfo {
170 const char *PlusName, *MinusName;
171 FPUVersion MinVersion;
172 FPURestriction MaxRestriction;
173 } FPUFeatureInfoList[] = {
174 // We have to specify the + and - versions of the name in full so
175 // that we can return them as static StringRefs.
176 //
177 // Also, the SubtargetFeatures ending in just "sp" are listed here
178 // under FPURestriction::None, which is the only FPURestriction in
179 // which they would be valid (since FPURestriction::SP doesn't
180 // exist).
Eli Friedmanddf5e862019-09-17 21:42:38 +0000181 {"+vfp2", "-vfp2", FPUVersion::VFPV2, FPURestriction::D16},
182 {"+vfp2sp", "-vfp2sp", FPUVersion::VFPV2, FPURestriction::SP_D16},
Simon Tatham5d66f2b2019-06-07 12:42:54 +0000183 {"+vfp3", "-vfp3", FPUVersion::VFPV3, FPURestriction::None},
184 {"+vfp3d16", "-vfp3d16", FPUVersion::VFPV3, FPURestriction::D16},
185 {"+vfp3d16sp", "-vfp3d16sp", FPUVersion::VFPV3, FPURestriction::SP_D16},
186 {"+vfp3sp", "-vfp3sp", FPUVersion::VFPV3, FPURestriction::None},
187 {"+fp16", "-fp16", FPUVersion::VFPV3_FP16, FPURestriction::SP_D16},
188 {"+vfp4", "-vfp4", FPUVersion::VFPV4, FPURestriction::None},
189 {"+vfp4d16", "-vfp4d16", FPUVersion::VFPV4, FPURestriction::D16},
190 {"+vfp4d16sp", "-vfp4d16sp", FPUVersion::VFPV4, FPURestriction::SP_D16},
191 {"+vfp4sp", "-vfp4sp", FPUVersion::VFPV4, FPURestriction::None},
192 {"+fp-armv8", "-fp-armv8", FPUVersion::VFPV5, FPURestriction::None},
193 {"+fp-armv8d16", "-fp-armv8d16", FPUVersion::VFPV5, FPURestriction::D16},
194 {"+fp-armv8d16sp", "-fp-armv8d16sp", FPUVersion::VFPV5, FPURestriction::SP_D16},
195 {"+fp-armv8sp", "-fp-armv8sp", FPUVersion::VFPV5, FPURestriction::None},
196 {"+fullfp16", "-fullfp16", FPUVersion::VFPV5_FULLFP16, FPURestriction::SP_D16},
197 {"+fp64", "-fp64", FPUVersion::VFPV2, FPURestriction::D16},
Eli Friedmanddf5e862019-09-17 21:42:38 +0000198 {"+d32", "-d32", FPUVersion::VFPV3, FPURestriction::None},
Simon Tatham5d66f2b2019-06-07 12:42:54 +0000199 };
200
201 for (const auto &Info: FPUFeatureInfoList) {
202 if (FPUNames[FPUKind].FPUVer >= Info.MinVersion &&
203 FPUNames[FPUKind].Restriction <= Info.MaxRestriction)
204 Features.push_back(Info.PlusName);
205 else
206 Features.push_back(Info.MinusName);
David Spickette01718c2018-11-28 11:38:10 +0000207 }
208
Simon Tatham5d66f2b2019-06-07 12:42:54 +0000209 static const struct NeonFeatureNameInfo {
210 const char *PlusName, *MinusName;
211 NeonSupportLevel MinSupportLevel;
212 } NeonFeatureInfoList[] = {
213 {"+neon", "-neon", NeonSupportLevel::Neon},
214 {"+crypto", "-crypto", NeonSupportLevel::Crypto},
215 };
Simon Tatham760df472019-05-28 16:13:20 +0000216
Simon Tatham5d66f2b2019-06-07 12:42:54 +0000217 for (const auto &Info: NeonFeatureInfoList) {
218 if (FPUNames[FPUKind].NeonSupport >= Info.MinSupportLevel)
219 Features.push_back(Info.PlusName);
220 else
221 Features.push_back(Info.MinusName);
David Spickette01718c2018-11-28 11:38:10 +0000222 }
223
224 return true;
225}
226
227// Little/Big endian
228ARM::EndianKind ARM::parseArchEndian(StringRef Arch) {
229 if (Arch.startswith("armeb") || Arch.startswith("thumbeb") ||
230 Arch.startswith("aarch64_be"))
231 return EndianKind::BIG;
232
233 if (Arch.startswith("arm") || Arch.startswith("thumb")) {
234 if (Arch.endswith("eb"))
235 return EndianKind::BIG;
236 else
237 return EndianKind::LITTLE;
238 }
239
Tim Northoverff6875a2019-05-14 11:25:44 +0000240 if (Arch.startswith("aarch64") || Arch.startswith("aarch64_32"))
David Spickette01718c2018-11-28 11:38:10 +0000241 return EndianKind::LITTLE;
242
243 return EndianKind::INVALID;
244}
245
246// ARM, Thumb, AArch64
247ARM::ISAKind ARM::parseArchISA(StringRef Arch) {
248 return StringSwitch<ISAKind>(Arch)
249 .StartsWith("aarch64", ISAKind::AARCH64)
250 .StartsWith("arm64", ISAKind::AARCH64)
251 .StartsWith("thumb", ISAKind::THUMB)
252 .StartsWith("arm", ISAKind::ARM)
253 .Default(ISAKind::INVALID);
254}
255
256unsigned ARM::parseFPU(StringRef FPU) {
257 StringRef Syn = getFPUSynonym(FPU);
Simon Pilgrimf16b2d82020-09-09 11:17:49 +0100258 for (const auto &F : FPUNames) {
David Spickette01718c2018-11-28 11:38:10 +0000259 if (Syn == F.getName())
260 return F.ID;
261 }
262 return FK_INVALID;
263}
264
265ARM::NeonSupportLevel ARM::getFPUNeonSupportLevel(unsigned FPUKind) {
266 if (FPUKind >= FK_LAST)
267 return NeonSupportLevel::None;
268 return FPUNames[FPUKind].NeonSupport;
269}
270
271// MArch is expected to be of the form (arm|thumb)?(eb)?(v.+)?(eb)?, but
272// (iwmmxt|xscale)(eb)? is also permitted. If the former, return
273// "v.+", if the latter, return unmodified string, minus 'eb'.
274// If invalid, return empty string.
275StringRef ARM::getCanonicalArchName(StringRef Arch) {
276 size_t offset = StringRef::npos;
277 StringRef A = Arch;
278 StringRef Error = "";
279
280 // Begins with "arm" / "thumb", move past it.
Tim Northoverff6875a2019-05-14 11:25:44 +0000281 if (A.startswith("arm64_32"))
282 offset = 8;
283 else if (A.startswith("arm64"))
David Spickette01718c2018-11-28 11:38:10 +0000284 offset = 5;
Tim Northoverff6875a2019-05-14 11:25:44 +0000285 else if (A.startswith("aarch64_32"))
286 offset = 10;
David Spickette01718c2018-11-28 11:38:10 +0000287 else if (A.startswith("arm"))
288 offset = 3;
289 else if (A.startswith("thumb"))
290 offset = 5;
291 else if (A.startswith("aarch64")) {
292 offset = 7;
293 // AArch64 uses "_be", not "eb" suffix.
294 if (A.find("eb") != StringRef::npos)
295 return Error;
296 if (A.substr(offset, 3) == "_be")
297 offset += 3;
298 }
299
300 // Ex. "armebv7", move past the "eb".
301 if (offset != StringRef::npos && A.substr(offset, 2) == "eb")
302 offset += 2;
303 // Or, if it ends with eb ("armv7eb"), chop it off.
304 else if (A.endswith("eb"))
305 A = A.substr(0, A.size() - 2);
306 // Trim the head
307 if (offset != StringRef::npos)
308 A = A.substr(offset);
309
310 // Empty string means offset reached the end, which means it's valid.
311 if (A.empty())
312 return Arch;
313
314 // Only match non-marketing names
315 if (offset != StringRef::npos) {
316 // Must start with 'vN'.
317 if (A.size() >= 2 && (A[0] != 'v' || !std::isdigit(A[1])))
318 return Error;
319 // Can't have an extra 'eb'.
320 if (A.find("eb") != StringRef::npos)
321 return Error;
322 }
323
324 // Arch will either be a 'v' name (v7a) or a marketing name (xscale).
325 return A;
326}
327
328StringRef ARM::getFPUSynonym(StringRef FPU) {
329 return StringSwitch<StringRef>(FPU)
330 .Cases("fpa", "fpe2", "fpe3", "maverick", "invalid") // Unsupported
331 .Case("vfp2", "vfpv2")
332 .Case("vfp3", "vfpv3")
333 .Case("vfp4", "vfpv4")
334 .Case("vfp3-d16", "vfpv3-d16")
335 .Case("vfp4-d16", "vfpv4-d16")
336 .Cases("fp4-sp-d16", "vfpv4-sp-d16", "fpv4-sp-d16")
337 .Cases("fp4-dp-d16", "fpv4-dp-d16", "vfpv4-d16")
338 .Case("fp5-sp-d16", "fpv5-sp-d16")
339 .Cases("fp5-dp-d16", "fpv5-dp-d16", "fpv5-d16")
340 // FIXME: Clang uses it, but it's bogus, since neon defaults to vfpv3.
341 .Case("neon-vfpv3", "neon")
342 .Default(FPU);
343}
344
345StringRef ARM::getFPUName(unsigned FPUKind) {
346 if (FPUKind >= FK_LAST)
347 return StringRef();
348 return FPUNames[FPUKind].getName();
349}
350
351ARM::FPUVersion ARM::getFPUVersion(unsigned FPUKind) {
352 if (FPUKind >= FK_LAST)
353 return FPUVersion::NONE;
354 return FPUNames[FPUKind].FPUVer;
355}
356
357ARM::FPURestriction ARM::getFPURestriction(unsigned FPUKind) {
358 if (FPUKind >= FK_LAST)
359 return FPURestriction::None;
360 return FPUNames[FPUKind].Restriction;
361}
362
363unsigned ARM::getDefaultFPU(StringRef CPU, ARM::ArchKind AK) {
364 if (CPU == "generic")
365 return ARM::ARCHNames[static_cast<unsigned>(AK)].DefaultFPU;
366
367 return StringSwitch<unsigned>(CPU)
368#define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
369 .Case(NAME, DEFAULT_FPU)
370#include "llvm/Support/ARMTargetParser.def"
371 .Default(ARM::FK_INVALID);
372}
373
Mikhail Maltsev7128aac2020-02-04 11:22:07 +0000374uint64_t ARM::getDefaultExtensions(StringRef CPU, ARM::ArchKind AK) {
David Spickette01718c2018-11-28 11:38:10 +0000375 if (CPU == "generic")
376 return ARM::ARCHNames[static_cast<unsigned>(AK)].ArchBaseExtensions;
377
Mikhail Maltsev7128aac2020-02-04 11:22:07 +0000378 return StringSwitch<uint64_t>(CPU)
David Spickette01718c2018-11-28 11:38:10 +0000379#define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
380 .Case(NAME, \
381 ARCHNames[static_cast<unsigned>(ArchKind::ID)].ArchBaseExtensions | \
382 DEFAULT_EXT)
383#include "llvm/Support/ARMTargetParser.def"
384 .Default(ARM::AEK_INVALID);
385}
386
Mikhail Maltsev7128aac2020-02-04 11:22:07 +0000387bool ARM::getHWDivFeatures(uint64_t HWDivKind,
David Spickette01718c2018-11-28 11:38:10 +0000388 std::vector<StringRef> &Features) {
389
390 if (HWDivKind == AEK_INVALID)
391 return false;
392
393 if (HWDivKind & AEK_HWDIVARM)
394 Features.push_back("+hwdiv-arm");
395 else
396 Features.push_back("-hwdiv-arm");
397
398 if (HWDivKind & AEK_HWDIVTHUMB)
399 Features.push_back("+hwdiv");
400 else
401 Features.push_back("-hwdiv");
402
403 return true;
404}
405
Mikhail Maltsev7128aac2020-02-04 11:22:07 +0000406bool ARM::getExtensionFeatures(uint64_t Extensions,
David Spickette01718c2018-11-28 11:38:10 +0000407 std::vector<StringRef> &Features) {
408
409 if (Extensions == AEK_INVALID)
410 return false;
411
Simon Pilgrimf16b2d82020-09-09 11:17:49 +0100412 for (const auto &AE : ARCHExtNames) {
Alexandros Lamprineas24cacf92019-07-14 18:32:42 +0000413 if ((Extensions & AE.ID) == AE.ID && AE.Feature)
414 Features.push_back(AE.Feature);
415 else if (AE.NegFeature)
416 Features.push_back(AE.NegFeature);
417 }
David Spickette01718c2018-11-28 11:38:10 +0000418
419 return getHWDivFeatures(Extensions, Features);
420}
421
422StringRef ARM::getArchName(ARM::ArchKind AK) {
423 return ARCHNames[static_cast<unsigned>(AK)].getName();
424}
425
426StringRef ARM::getCPUAttr(ARM::ArchKind AK) {
427 return ARCHNames[static_cast<unsigned>(AK)].getCPUAttr();
428}
429
430StringRef ARM::getSubArch(ARM::ArchKind AK) {
431 return ARCHNames[static_cast<unsigned>(AK)].getSubArch();
432}
433
434unsigned ARM::getArchAttr(ARM::ArchKind AK) {
435 return ARCHNames[static_cast<unsigned>(AK)].ArchAttr;
436}
437
Mikhail Maltsev7128aac2020-02-04 11:22:07 +0000438StringRef ARM::getArchExtName(uint64_t ArchExtKind) {
Simon Pilgrimf16b2d82020-09-09 11:17:49 +0100439 for (const auto &AE : ARCHExtNames) {
David Spickette01718c2018-11-28 11:38:10 +0000440 if (ArchExtKind == AE.ID)
441 return AE.getName();
442 }
443 return StringRef();
444}
445
Sjoerd Meijera1bb4fb2019-06-05 13:11:51 +0000446static bool stripNegationPrefix(StringRef &Name) {
447 if (Name.startswith("no")) {
448 Name = Name.substr(2);
449 return true;
David Spickette01718c2018-11-28 11:38:10 +0000450 }
Sjoerd Meijera1bb4fb2019-06-05 13:11:51 +0000451 return false;
452}
453
454StringRef ARM::getArchExtFeature(StringRef ArchExt) {
455 bool Negated = stripNegationPrefix(ArchExt);
Simon Pilgrimf16b2d82020-09-09 11:17:49 +0100456 for (const auto &AE : ARCHExtNames) {
David Spickette01718c2018-11-28 11:38:10 +0000457 if (AE.Feature && ArchExt == AE.getName())
Sjoerd Meijera1bb4fb2019-06-05 13:11:51 +0000458 return StringRef(Negated ? AE.NegFeature : AE.Feature);
David Spickette01718c2018-11-28 11:38:10 +0000459 }
460
461 return StringRef();
462}
463
Sjoerd Meijera1bb4fb2019-06-05 13:11:51 +0000464static unsigned findDoublePrecisionFPU(unsigned InputFPUKind) {
465 const ARM::FPUName &InputFPU = ARM::FPUNames[InputFPUKind];
466
467 // If the input FPU already supports double-precision, then there
468 // isn't any different FPU we can return here.
469 //
470 // The current available FPURestriction values are None (no
471 // restriction), D16 (only 16 d-regs) and SP_D16 (16 d-regs
472 // and single precision only); there's no value representing
473 // SP restriction without D16. So this test just means 'is it
474 // SP only?'.
475 if (InputFPU.Restriction != ARM::FPURestriction::SP_D16)
476 return ARM::FK_INVALID;
477
478 // Otherwise, look for an FPU entry with all the same fields, except
479 // that SP_D16 has been replaced with just D16, representing adding
480 // double precision and not changing anything else.
481 for (const ARM::FPUName &CandidateFPU : ARM::FPUNames) {
482 if (CandidateFPU.FPUVer == InputFPU.FPUVer &&
483 CandidateFPU.NeonSupport == InputFPU.NeonSupport &&
484 CandidateFPU.Restriction == ARM::FPURestriction::D16) {
485 return CandidateFPU.ID;
486 }
487 }
488
489 // nothing found
490 return ARM::FK_INVALID;
491}
492
Victor Camposd1a33962020-07-21 17:18:20 +0100493bool ARM::appendArchExtFeatures(StringRef CPU, ARM::ArchKind AK,
494 StringRef ArchExt,
495 std::vector<StringRef> &Features,
496 unsigned &ArgFPUID) {
Sjoerd Meijera1bb4fb2019-06-05 13:11:51 +0000497
Alexandros Lamprineas951bb682019-07-14 20:31:15 +0000498 size_t StartingNumFeatures = Features.size();
Sjoerd Meijera1bb4fb2019-06-05 13:11:51 +0000499 const bool Negated = stripNegationPrefix(ArchExt);
Mikhail Maltsev7128aac2020-02-04 11:22:07 +0000500 uint64_t ID = parseArchExt(ArchExt);
Alexandros Lamprineas951bb682019-07-14 20:31:15 +0000501
502 if (ID == AEK_INVALID)
503 return false;
504
Simon Pilgrimf16b2d82020-09-09 11:17:49 +0100505 for (const auto &AE : ARCHExtNames) {
Momchil Velikov3627c912020-02-05 15:19:03 +0000506 if (Negated) {
507 if ((AE.ID & ID) == ID && AE.NegFeature)
508 Features.push_back(AE.NegFeature);
509 } else {
510 if ((AE.ID & ID) == AE.ID && AE.Feature)
511 Features.push_back(AE.Feature);
512 }
Alexandros Lamprineas951bb682019-07-14 20:31:15 +0000513 }
Sjoerd Meijera1bb4fb2019-06-05 13:11:51 +0000514
515 if (CPU == "")
516 CPU = "generic";
517
518 if (ArchExt == "fp" || ArchExt == "fp.dp") {
519 unsigned FPUKind;
520 if (ArchExt == "fp.dp") {
521 if (Negated) {
522 Features.push_back("-fp64");
523 return true;
524 }
525 FPUKind = findDoublePrecisionFPU(getDefaultFPU(CPU, AK));
526 } else if (Negated) {
527 FPUKind = ARM::FK_NONE;
528 } else {
529 FPUKind = getDefaultFPU(CPU, AK);
530 }
Victor Camposd1a33962020-07-21 17:18:20 +0100531 ArgFPUID = FPUKind;
Sjoerd Meijera1bb4fb2019-06-05 13:11:51 +0000532 return ARM::getFPUFeatures(FPUKind, Features);
533 }
Alexandros Lamprineas951bb682019-07-14 20:31:15 +0000534 return StartingNumFeatures != Features.size();
Sjoerd Meijera1bb4fb2019-06-05 13:11:51 +0000535}
536
Mikhail Maltsev7128aac2020-02-04 11:22:07 +0000537StringRef ARM::getHWDivName(uint64_t HWDivKind) {
Simon Pilgrimf16b2d82020-09-09 11:17:49 +0100538 for (const auto &D : HWDivNames) {
David Spickette01718c2018-11-28 11:38:10 +0000539 if (HWDivKind == D.ID)
540 return D.getName();
541 }
542 return StringRef();
543}
544
545StringRef ARM::getDefaultCPU(StringRef Arch) {
546 ArchKind AK = parseArch(Arch);
547 if (AK == ArchKind::INVALID)
548 return StringRef();
549
550 // Look for multiple AKs to find the default for pair AK+Name.
Simon Pilgrimf16b2d82020-09-09 11:17:49 +0100551 for (const auto &CPU : CPUNames) {
David Spickette01718c2018-11-28 11:38:10 +0000552 if (CPU.ArchID == AK && CPU.Default)
553 return CPU.getName();
554 }
555
556 // If we can't find a default then target the architecture instead
557 return "generic";
558}
559
Mikhail Maltsev7128aac2020-02-04 11:22:07 +0000560uint64_t ARM::parseHWDiv(StringRef HWDiv) {
David Spickette01718c2018-11-28 11:38:10 +0000561 StringRef Syn = getHWDivSynonym(HWDiv);
Simon Pilgrimf16b2d82020-09-09 11:17:49 +0100562 for (const auto &D : HWDivNames) {
David Spickette01718c2018-11-28 11:38:10 +0000563 if (Syn == D.getName())
564 return D.ID;
565 }
566 return AEK_INVALID;
567}
568
Mikhail Maltsev7128aac2020-02-04 11:22:07 +0000569uint64_t ARM::parseArchExt(StringRef ArchExt) {
Simon Pilgrimf16b2d82020-09-09 11:17:49 +0100570 for (const auto &A : ARCHExtNames) {
David Spickette01718c2018-11-28 11:38:10 +0000571 if (ArchExt == A.getName())
572 return A.ID;
573 }
574 return AEK_INVALID;
575}
576
577ARM::ArchKind ARM::parseCPUArch(StringRef CPU) {
Simon Pilgrimf16b2d82020-09-09 11:17:49 +0100578 for (const auto &C : CPUNames) {
David Spickette01718c2018-11-28 11:38:10 +0000579 if (CPU == C.getName())
580 return C.ArchID;
581 }
582 return ArchKind::INVALID;
583}
584
585void ARM::fillValidCPUArchList(SmallVectorImpl<StringRef> &Values) {
586 for (const CpuNames<ArchKind> &Arch : CPUNames) {
587 if (Arch.ArchID != ArchKind::INVALID)
588 Values.push_back(Arch.getName());
589 }
590}
591
592StringRef ARM::computeDefaultTargetABI(const Triple &TT, StringRef CPU) {
593 StringRef ArchName =
594 CPU.empty() ? TT.getArchName() : getArchName(parseCPUArch(CPU));
595
596 if (TT.isOSBinFormatMachO()) {
597 if (TT.getEnvironment() == Triple::EABI ||
598 TT.getOS() == Triple::UnknownOS ||
599 parseArchProfile(ArchName) == ProfileKind::M)
600 return "aapcs";
601 if (TT.isWatchABI())
602 return "aapcs16";
603 return "apcs-gnu";
604 } else if (TT.isOSWindows())
605 // FIXME: this is invalid for WindowsCE.
606 return "aapcs";
607
608 // Select the default based on the platform.
609 switch (TT.getEnvironment()) {
610 case Triple::Android:
611 case Triple::GNUEABI:
612 case Triple::GNUEABIHF:
613 case Triple::MuslEABI:
614 case Triple::MuslEABIHF:
615 return "aapcs-linux";
616 case Triple::EABIHF:
617 case Triple::EABI:
618 return "aapcs";
619 default:
620 if (TT.isOSNetBSD())
621 return "apcs-gnu";
622 if (TT.isOSOpenBSD())
623 return "aapcs-linux";
624 return "aapcs";
625 }
626}