blob: c57da4cb2021898a4d1c87b3a86c046429db9c76 [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"
David Spickettcc3fa392018-11-28 15:12:33 +000016#include <cctype>
David Spickette01718c2018-11-28 11:38:10 +000017
18using namespace llvm;
19
20static StringRef getHWDivSynonym(StringRef HWDiv) {
21 return StringSwitch<StringRef>(HWDiv)
22 .Case("thumb,arm", "arm,thumb")
23 .Default(HWDiv);
24}
25
26// Allows partial match, ex. "v7a" matches "armv7a".
27ARM::ArchKind ARM::parseArch(StringRef Arch) {
28 Arch = getCanonicalArchName(Arch);
29 StringRef Syn = getArchSynonym(Arch);
30 for (const auto A : ARCHNames) {
31 if (A.getName().endswith(Syn))
32 return A.ID;
33 }
34 return ArchKind::INVALID;
35}
36
37// Version number (ex. v7 = 7).
38unsigned ARM::parseArchVersion(StringRef Arch) {
39 Arch = getCanonicalArchName(Arch);
40 switch (parseArch(Arch)) {
41 case ArchKind::ARMV2:
42 case ArchKind::ARMV2A:
43 return 2;
44 case ArchKind::ARMV3:
45 case ArchKind::ARMV3M:
46 return 3;
47 case ArchKind::ARMV4:
48 case ArchKind::ARMV4T:
49 return 4;
50 case ArchKind::ARMV5T:
51 case ArchKind::ARMV5TE:
52 case ArchKind::IWMMXT:
53 case ArchKind::IWMMXT2:
54 case ArchKind::XSCALE:
55 case ArchKind::ARMV5TEJ:
56 return 5;
57 case ArchKind::ARMV6:
58 case ArchKind::ARMV6K:
59 case ArchKind::ARMV6T2:
60 case ArchKind::ARMV6KZ:
61 case ArchKind::ARMV6M:
62 return 6;
63 case ArchKind::ARMV7A:
64 case ArchKind::ARMV7VE:
65 case ArchKind::ARMV7R:
66 case ArchKind::ARMV7M:
67 case ArchKind::ARMV7S:
68 case ArchKind::ARMV7EM:
69 case ArchKind::ARMV7K:
70 return 7;
71 case ArchKind::ARMV8A:
72 case ArchKind::ARMV8_1A:
73 case ArchKind::ARMV8_2A:
74 case ArchKind::ARMV8_3A:
75 case ArchKind::ARMV8_4A:
76 case ArchKind::ARMV8_5A:
77 case ArchKind::ARMV8R:
78 case ArchKind::ARMV8MBaseline:
79 case ArchKind::ARMV8MMainline:
80 return 8;
81 case ArchKind::INVALID:
82 return 0;
83 }
84 llvm_unreachable("Unhandled architecture");
85}
86
87// Profile A/R/M
88ARM::ProfileKind ARM::parseArchProfile(StringRef Arch) {
89 Arch = getCanonicalArchName(Arch);
90 switch (parseArch(Arch)) {
91 case ArchKind::ARMV6M:
92 case ArchKind::ARMV7M:
93 case ArchKind::ARMV7EM:
94 case ArchKind::ARMV8MMainline:
95 case ArchKind::ARMV8MBaseline:
96 return ProfileKind::M;
97 case ArchKind::ARMV7R:
98 case ArchKind::ARMV8R:
99 return ProfileKind::R;
100 case ArchKind::ARMV7A:
101 case ArchKind::ARMV7VE:
102 case ArchKind::ARMV7K:
103 case ArchKind::ARMV8A:
104 case ArchKind::ARMV8_1A:
105 case ArchKind::ARMV8_2A:
106 case ArchKind::ARMV8_3A:
107 case ArchKind::ARMV8_4A:
108 case ArchKind::ARMV8_5A:
109 return ProfileKind::A;
110 case ArchKind::ARMV2:
111 case ArchKind::ARMV2A:
112 case ArchKind::ARMV3:
113 case ArchKind::ARMV3M:
114 case ArchKind::ARMV4:
115 case ArchKind::ARMV4T:
116 case ArchKind::ARMV5T:
117 case ArchKind::ARMV5TE:
118 case ArchKind::ARMV5TEJ:
119 case ArchKind::ARMV6:
120 case ArchKind::ARMV6K:
121 case ArchKind::ARMV6T2:
122 case ArchKind::ARMV6KZ:
123 case ArchKind::ARMV7S:
124 case ArchKind::IWMMXT:
125 case ArchKind::IWMMXT2:
126 case ArchKind::XSCALE:
127 case ArchKind::INVALID:
128 return ProfileKind::INVALID;
129 }
130 llvm_unreachable("Unhandled architecture");
131}
132
133StringRef ARM::getArchSynonym(StringRef Arch) {
134 return StringSwitch<StringRef>(Arch)
135 .Case("v5", "v5t")
136 .Case("v5e", "v5te")
137 .Case("v6j", "v6")
138 .Case("v6hl", "v6k")
139 .Cases("v6m", "v6sm", "v6s-m", "v6-m")
140 .Cases("v6z", "v6zk", "v6kz")
141 .Cases("v7", "v7a", "v7hl", "v7l", "v7-a")
142 .Case("v7r", "v7-r")
143 .Case("v7m", "v7-m")
144 .Case("v7em", "v7e-m")
145 .Cases("v8", "v8a", "v8l", "aarch64", "arm64", "v8-a")
146 .Case("v8.1a", "v8.1-a")
147 .Case("v8.2a", "v8.2-a")
148 .Case("v8.3a", "v8.3-a")
149 .Case("v8.4a", "v8.4-a")
150 .Case("v8.5a", "v8.5-a")
151 .Case("v8r", "v8-r")
152 .Case("v8m.base", "v8-m.base")
153 .Case("v8m.main", "v8-m.main")
154 .Default(Arch);
155}
156
157bool ARM::getFPUFeatures(unsigned FPUKind, std::vector<StringRef> &Features) {
158
159 if (FPUKind >= FK_LAST || FPUKind == FK_INVALID)
160 return false;
161
162 // fp-only-sp and d16 subtarget features are independent of each other, so we
163 // must enable/disable both.
164 switch (FPUNames[FPUKind].Restriction) {
165 case FPURestriction::SP_D16:
166 Features.push_back("+fp-only-sp");
167 Features.push_back("+d16");
168 break;
169 case FPURestriction::D16:
170 Features.push_back("-fp-only-sp");
171 Features.push_back("+d16");
172 break;
173 case FPURestriction::None:
174 Features.push_back("-fp-only-sp");
175 Features.push_back("-d16");
176 break;
177 }
178
179 // FPU version subtarget features are inclusive of lower-numbered ones, so
180 // enable the one corresponding to this version and disable all that are
181 // higher. We also have to make sure to disable fp16 when vfp4 is disabled,
182 // as +vfp4 implies +fp16 but -vfp4 does not imply -fp16.
183 switch (FPUNames[FPUKind].FPUVer) {
184 case FPUVersion::VFPV5:
185 Features.push_back("+fp-armv8");
186 break;
187 case FPUVersion::VFPV4:
188 Features.push_back("+vfp4");
189 Features.push_back("-fp-armv8");
190 break;
191 case FPUVersion::VFPV3_FP16:
192 Features.push_back("+vfp3");
193 Features.push_back("+fp16");
194 Features.push_back("-vfp4");
195 Features.push_back("-fp-armv8");
196 break;
197 case FPUVersion::VFPV3:
198 Features.push_back("+vfp3");
199 Features.push_back("-fp16");
200 Features.push_back("-vfp4");
201 Features.push_back("-fp-armv8");
202 break;
203 case FPUVersion::VFPV2:
204 Features.push_back("+vfp2");
205 Features.push_back("-vfp3");
206 Features.push_back("-fp16");
207 Features.push_back("-vfp4");
208 Features.push_back("-fp-armv8");
209 break;
210 case FPUVersion::NONE:
211 Features.push_back("-vfp2");
212 Features.push_back("-vfp3");
213 Features.push_back("-fp16");
214 Features.push_back("-vfp4");
215 Features.push_back("-fp-armv8");
216 break;
217 }
218
219 // crypto includes neon, so we handle this similarly to FPU version.
220 switch (FPUNames[FPUKind].NeonSupport) {
221 case NeonSupportLevel::Crypto:
222 Features.push_back("+neon");
223 Features.push_back("+crypto");
224 break;
225 case NeonSupportLevel::Neon:
226 Features.push_back("+neon");
227 Features.push_back("-crypto");
228 break;
229 case NeonSupportLevel::None:
230 Features.push_back("-neon");
231 Features.push_back("-crypto");
232 break;
233 }
234
235 return true;
236}
237
238// Little/Big endian
239ARM::EndianKind ARM::parseArchEndian(StringRef Arch) {
240 if (Arch.startswith("armeb") || Arch.startswith("thumbeb") ||
241 Arch.startswith("aarch64_be"))
242 return EndianKind::BIG;
243
244 if (Arch.startswith("arm") || Arch.startswith("thumb")) {
245 if (Arch.endswith("eb"))
246 return EndianKind::BIG;
247 else
248 return EndianKind::LITTLE;
249 }
250
Tim Northoverff6875a2019-05-14 11:25:44 +0000251 if (Arch.startswith("aarch64") || Arch.startswith("aarch64_32"))
David Spickette01718c2018-11-28 11:38:10 +0000252 return EndianKind::LITTLE;
253
254 return EndianKind::INVALID;
255}
256
257// ARM, Thumb, AArch64
258ARM::ISAKind ARM::parseArchISA(StringRef Arch) {
259 return StringSwitch<ISAKind>(Arch)
260 .StartsWith("aarch64", ISAKind::AARCH64)
261 .StartsWith("arm64", ISAKind::AARCH64)
262 .StartsWith("thumb", ISAKind::THUMB)
263 .StartsWith("arm", ISAKind::ARM)
264 .Default(ISAKind::INVALID);
265}
266
267unsigned ARM::parseFPU(StringRef FPU) {
268 StringRef Syn = getFPUSynonym(FPU);
269 for (const auto F : FPUNames) {
270 if (Syn == F.getName())
271 return F.ID;
272 }
273 return FK_INVALID;
274}
275
276ARM::NeonSupportLevel ARM::getFPUNeonSupportLevel(unsigned FPUKind) {
277 if (FPUKind >= FK_LAST)
278 return NeonSupportLevel::None;
279 return FPUNames[FPUKind].NeonSupport;
280}
281
282// MArch is expected to be of the form (arm|thumb)?(eb)?(v.+)?(eb)?, but
283// (iwmmxt|xscale)(eb)? is also permitted. If the former, return
284// "v.+", if the latter, return unmodified string, minus 'eb'.
285// If invalid, return empty string.
286StringRef ARM::getCanonicalArchName(StringRef Arch) {
287 size_t offset = StringRef::npos;
288 StringRef A = Arch;
289 StringRef Error = "";
290
291 // Begins with "arm" / "thumb", move past it.
Tim Northoverff6875a2019-05-14 11:25:44 +0000292 if (A.startswith("arm64_32"))
293 offset = 8;
294 else if (A.startswith("arm64"))
David Spickette01718c2018-11-28 11:38:10 +0000295 offset = 5;
Tim Northoverff6875a2019-05-14 11:25:44 +0000296 else if (A.startswith("aarch64_32"))
297 offset = 10;
David Spickette01718c2018-11-28 11:38:10 +0000298 else if (A.startswith("arm"))
299 offset = 3;
300 else if (A.startswith("thumb"))
301 offset = 5;
302 else if (A.startswith("aarch64")) {
303 offset = 7;
304 // AArch64 uses "_be", not "eb" suffix.
305 if (A.find("eb") != StringRef::npos)
306 return Error;
307 if (A.substr(offset, 3) == "_be")
308 offset += 3;
309 }
310
311 // Ex. "armebv7", move past the "eb".
312 if (offset != StringRef::npos && A.substr(offset, 2) == "eb")
313 offset += 2;
314 // Or, if it ends with eb ("armv7eb"), chop it off.
315 else if (A.endswith("eb"))
316 A = A.substr(0, A.size() - 2);
317 // Trim the head
318 if (offset != StringRef::npos)
319 A = A.substr(offset);
320
321 // Empty string means offset reached the end, which means it's valid.
322 if (A.empty())
323 return Arch;
324
325 // Only match non-marketing names
326 if (offset != StringRef::npos) {
327 // Must start with 'vN'.
328 if (A.size() >= 2 && (A[0] != 'v' || !std::isdigit(A[1])))
329 return Error;
330 // Can't have an extra 'eb'.
331 if (A.find("eb") != StringRef::npos)
332 return Error;
333 }
334
335 // Arch will either be a 'v' name (v7a) or a marketing name (xscale).
336 return A;
337}
338
339StringRef ARM::getFPUSynonym(StringRef FPU) {
340 return StringSwitch<StringRef>(FPU)
341 .Cases("fpa", "fpe2", "fpe3", "maverick", "invalid") // Unsupported
342 .Case("vfp2", "vfpv2")
343 .Case("vfp3", "vfpv3")
344 .Case("vfp4", "vfpv4")
345 .Case("vfp3-d16", "vfpv3-d16")
346 .Case("vfp4-d16", "vfpv4-d16")
347 .Cases("fp4-sp-d16", "vfpv4-sp-d16", "fpv4-sp-d16")
348 .Cases("fp4-dp-d16", "fpv4-dp-d16", "vfpv4-d16")
349 .Case("fp5-sp-d16", "fpv5-sp-d16")
350 .Cases("fp5-dp-d16", "fpv5-dp-d16", "fpv5-d16")
351 // FIXME: Clang uses it, but it's bogus, since neon defaults to vfpv3.
352 .Case("neon-vfpv3", "neon")
353 .Default(FPU);
354}
355
356StringRef ARM::getFPUName(unsigned FPUKind) {
357 if (FPUKind >= FK_LAST)
358 return StringRef();
359 return FPUNames[FPUKind].getName();
360}
361
362ARM::FPUVersion ARM::getFPUVersion(unsigned FPUKind) {
363 if (FPUKind >= FK_LAST)
364 return FPUVersion::NONE;
365 return FPUNames[FPUKind].FPUVer;
366}
367
368ARM::FPURestriction ARM::getFPURestriction(unsigned FPUKind) {
369 if (FPUKind >= FK_LAST)
370 return FPURestriction::None;
371 return FPUNames[FPUKind].Restriction;
372}
373
374unsigned ARM::getDefaultFPU(StringRef CPU, ARM::ArchKind AK) {
375 if (CPU == "generic")
376 return ARM::ARCHNames[static_cast<unsigned>(AK)].DefaultFPU;
377
378 return StringSwitch<unsigned>(CPU)
379#define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
380 .Case(NAME, DEFAULT_FPU)
381#include "llvm/Support/ARMTargetParser.def"
382 .Default(ARM::FK_INVALID);
383}
384
385unsigned ARM::getDefaultExtensions(StringRef CPU, ARM::ArchKind AK) {
386 if (CPU == "generic")
387 return ARM::ARCHNames[static_cast<unsigned>(AK)].ArchBaseExtensions;
388
389 return StringSwitch<unsigned>(CPU)
390#define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
391 .Case(NAME, \
392 ARCHNames[static_cast<unsigned>(ArchKind::ID)].ArchBaseExtensions | \
393 DEFAULT_EXT)
394#include "llvm/Support/ARMTargetParser.def"
395 .Default(ARM::AEK_INVALID);
396}
397
398bool ARM::getHWDivFeatures(unsigned HWDivKind,
399 std::vector<StringRef> &Features) {
400
401 if (HWDivKind == AEK_INVALID)
402 return false;
403
404 if (HWDivKind & AEK_HWDIVARM)
405 Features.push_back("+hwdiv-arm");
406 else
407 Features.push_back("-hwdiv-arm");
408
409 if (HWDivKind & AEK_HWDIVTHUMB)
410 Features.push_back("+hwdiv");
411 else
412 Features.push_back("-hwdiv");
413
414 return true;
415}
416
417bool ARM::getExtensionFeatures(unsigned Extensions,
418 std::vector<StringRef> &Features) {
419
420 if (Extensions == AEK_INVALID)
421 return false;
422
423 if (Extensions & AEK_CRC)
424 Features.push_back("+crc");
425 else
426 Features.push_back("-crc");
427
428 if (Extensions & AEK_DSP)
429 Features.push_back("+dsp");
430 else
431 Features.push_back("-dsp");
432
433 if (Extensions & AEK_FP16FML)
434 Features.push_back("+fp16fml");
435 else
436 Features.push_back("-fp16fml");
437
438 if (Extensions & AEK_RAS)
439 Features.push_back("+ras");
440 else
441 Features.push_back("-ras");
442
443 if (Extensions & AEK_DOTPROD)
444 Features.push_back("+dotprod");
445 else
446 Features.push_back("-dotprod");
447
448 return getHWDivFeatures(Extensions, Features);
449}
450
451StringRef ARM::getArchName(ARM::ArchKind AK) {
452 return ARCHNames[static_cast<unsigned>(AK)].getName();
453}
454
455StringRef ARM::getCPUAttr(ARM::ArchKind AK) {
456 return ARCHNames[static_cast<unsigned>(AK)].getCPUAttr();
457}
458
459StringRef ARM::getSubArch(ARM::ArchKind AK) {
460 return ARCHNames[static_cast<unsigned>(AK)].getSubArch();
461}
462
463unsigned ARM::getArchAttr(ARM::ArchKind AK) {
464 return ARCHNames[static_cast<unsigned>(AK)].ArchAttr;
465}
466
467StringRef ARM::getArchExtName(unsigned ArchExtKind) {
468 for (const auto AE : ARCHExtNames) {
469 if (ArchExtKind == AE.ID)
470 return AE.getName();
471 }
472 return StringRef();
473}
474
475StringRef ARM::getArchExtFeature(StringRef ArchExt) {
476 if (ArchExt.startswith("no")) {
477 StringRef ArchExtBase(ArchExt.substr(2));
478 for (const auto AE : ARCHExtNames) {
479 if (AE.NegFeature && ArchExtBase == AE.getName())
480 return StringRef(AE.NegFeature);
481 }
482 }
483 for (const auto AE : ARCHExtNames) {
484 if (AE.Feature && ArchExt == AE.getName())
485 return StringRef(AE.Feature);
486 }
487
488 return StringRef();
489}
490
491StringRef ARM::getHWDivName(unsigned HWDivKind) {
492 for (const auto D : HWDivNames) {
493 if (HWDivKind == D.ID)
494 return D.getName();
495 }
496 return StringRef();
497}
498
499StringRef ARM::getDefaultCPU(StringRef Arch) {
500 ArchKind AK = parseArch(Arch);
501 if (AK == ArchKind::INVALID)
502 return StringRef();
503
504 // Look for multiple AKs to find the default for pair AK+Name.
505 for (const auto CPU : CPUNames) {
506 if (CPU.ArchID == AK && CPU.Default)
507 return CPU.getName();
508 }
509
510 // If we can't find a default then target the architecture instead
511 return "generic";
512}
513
514unsigned ARM::parseHWDiv(StringRef HWDiv) {
515 StringRef Syn = getHWDivSynonym(HWDiv);
516 for (const auto D : HWDivNames) {
517 if (Syn == D.getName())
518 return D.ID;
519 }
520 return AEK_INVALID;
521}
522
523unsigned ARM::parseArchExt(StringRef ArchExt) {
524 for (const auto A : ARCHExtNames) {
525 if (ArchExt == A.getName())
526 return A.ID;
527 }
528 return AEK_INVALID;
529}
530
531ARM::ArchKind ARM::parseCPUArch(StringRef CPU) {
532 for (const auto C : CPUNames) {
533 if (CPU == C.getName())
534 return C.ArchID;
535 }
536 return ArchKind::INVALID;
537}
538
539void ARM::fillValidCPUArchList(SmallVectorImpl<StringRef> &Values) {
540 for (const CpuNames<ArchKind> &Arch : CPUNames) {
541 if (Arch.ArchID != ArchKind::INVALID)
542 Values.push_back(Arch.getName());
543 }
544}
545
546StringRef ARM::computeDefaultTargetABI(const Triple &TT, StringRef CPU) {
547 StringRef ArchName =
548 CPU.empty() ? TT.getArchName() : getArchName(parseCPUArch(CPU));
549
550 if (TT.isOSBinFormatMachO()) {
551 if (TT.getEnvironment() == Triple::EABI ||
552 TT.getOS() == Triple::UnknownOS ||
553 parseArchProfile(ArchName) == ProfileKind::M)
554 return "aapcs";
555 if (TT.isWatchABI())
556 return "aapcs16";
557 return "apcs-gnu";
558 } else if (TT.isOSWindows())
559 // FIXME: this is invalid for WindowsCE.
560 return "aapcs";
561
562 // Select the default based on the platform.
563 switch (TT.getEnvironment()) {
564 case Triple::Android:
565 case Triple::GNUEABI:
566 case Triple::GNUEABIHF:
567 case Triple::MuslEABI:
568 case Triple::MuslEABIHF:
569 return "aapcs-linux";
570 case Triple::EABIHF:
571 case Triple::EABI:
572 return "aapcs";
573 default:
574 if (TT.isOSNetBSD())
575 return "apcs-gnu";
576 if (TT.isOSOpenBSD())
577 return "aapcs-linux";
578 return "aapcs";
579 }
580}