1 //===-- ARMTargetParser - Parser for ARM target features --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/ARMTargetParser.h"
15 #include "llvm/ADT/StringSwitch.h"
20 static StringRef
getHWDivSynonym(StringRef HWDiv
) {
21 return StringSwitch
<StringRef
>(HWDiv
)
22 .Case("thumb,arm", "arm,thumb")
26 // Allows partial match, ex. "v7a" matches "armv7a".
27 ARM::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
))
34 return ArchKind::INVALID
;
37 // Version number (ex. v7 = 7).
38 unsigned ARM::parseArchVersion(StringRef Arch
) {
39 Arch
= getCanonicalArchName(Arch
);
40 switch (parseArch(Arch
)) {
42 case ArchKind::ARMV2A
:
45 case ArchKind::ARMV3M
:
48 case ArchKind::ARMV4T
:
50 case ArchKind::ARMV5T
:
51 case ArchKind::ARMV5TE
:
52 case ArchKind::IWMMXT
:
53 case ArchKind::IWMMXT2
:
54 case ArchKind::XSCALE
:
55 case ArchKind::ARMV5TEJ
:
58 case ArchKind::ARMV6K
:
59 case ArchKind::ARMV6T2
:
60 case ArchKind::ARMV6KZ
:
61 case ArchKind::ARMV6M
:
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
:
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 case ArchKind::ARMV8_1MMainline
:
82 case ArchKind::INVALID
:
85 llvm_unreachable("Unhandled architecture");
89 ARM::ProfileKind
ARM::parseArchProfile(StringRef Arch
) {
90 Arch
= getCanonicalArchName(Arch
);
91 switch (parseArch(Arch
)) {
92 case ArchKind::ARMV6M
:
93 case ArchKind::ARMV7M
:
94 case ArchKind::ARMV7EM
:
95 case ArchKind::ARMV8MMainline
:
96 case ArchKind::ARMV8MBaseline
:
97 case ArchKind::ARMV8_1MMainline
:
98 return ProfileKind::M
;
99 case ArchKind::ARMV7R
:
100 case ArchKind::ARMV8R
:
101 return ProfileKind::R
;
102 case ArchKind::ARMV7A
:
103 case ArchKind::ARMV7VE
:
104 case ArchKind::ARMV7K
:
105 case ArchKind::ARMV8A
:
106 case ArchKind::ARMV8_1A
:
107 case ArchKind::ARMV8_2A
:
108 case ArchKind::ARMV8_3A
:
109 case ArchKind::ARMV8_4A
:
110 case ArchKind::ARMV8_5A
:
111 return ProfileKind::A
;
112 case ArchKind::ARMV2
:
113 case ArchKind::ARMV2A
:
114 case ArchKind::ARMV3
:
115 case ArchKind::ARMV3M
:
116 case ArchKind::ARMV4
:
117 case ArchKind::ARMV4T
:
118 case ArchKind::ARMV5T
:
119 case ArchKind::ARMV5TE
:
120 case ArchKind::ARMV5TEJ
:
121 case ArchKind::ARMV6
:
122 case ArchKind::ARMV6K
:
123 case ArchKind::ARMV6T2
:
124 case ArchKind::ARMV6KZ
:
125 case ArchKind::ARMV7S
:
126 case ArchKind::IWMMXT
:
127 case ArchKind::IWMMXT2
:
128 case ArchKind::XSCALE
:
129 case ArchKind::INVALID
:
130 return ProfileKind::INVALID
;
132 llvm_unreachable("Unhandled architecture");
135 StringRef
ARM::getArchSynonym(StringRef Arch
) {
136 return StringSwitch
<StringRef
>(Arch
)
141 .Cases("v6m", "v6sm", "v6s-m", "v6-m")
142 .Cases("v6z", "v6zk", "v6kz")
143 .Cases("v7", "v7a", "v7hl", "v7l", "v7-a")
146 .Case("v7em", "v7e-m")
147 .Cases("v8", "v8a", "v8l", "aarch64", "arm64", "v8-a")
148 .Case("v8.1a", "v8.1-a")
149 .Case("v8.2a", "v8.2-a")
150 .Case("v8.3a", "v8.3-a")
151 .Case("v8.4a", "v8.4-a")
152 .Case("v8.5a", "v8.5-a")
154 .Case("v8m.base", "v8-m.base")
155 .Case("v8m.main", "v8-m.main")
156 .Case("v8.1m.main", "v8.1-m.main")
160 bool ARM::getFPUFeatures(unsigned FPUKind
, std::vector
<StringRef
> &Features
) {
162 if (FPUKind
>= FK_LAST
|| FPUKind
== FK_INVALID
)
165 // FPU version subtarget features are inclusive of lower-numbered ones, so
166 // enable the one corresponding to this version and disable all that are
167 // higher. We also have to make sure to disable fp16 when vfp4 is disabled,
168 // as +vfp4 implies +fp16 but -vfp4 does not imply -fp16.
169 switch (FPUNames
[FPUKind
].FPUVer
) {
170 case FPUVersion::VFPV5_FULLFP16
:
171 Features
.push_back("+fp-armv8");
172 Features
.push_back("+fullfp16");
174 case FPUVersion::VFPV5
:
175 Features
.push_back("+fp-armv8");
177 case FPUVersion::VFPV4
:
178 Features
.push_back("+vfp4");
179 Features
.push_back("-fp-armv8");
181 case FPUVersion::VFPV3_FP16
:
182 Features
.push_back("+vfp3");
183 Features
.push_back("+fp16");
184 Features
.push_back("-vfp4");
185 Features
.push_back("-fp-armv8");
187 case FPUVersion::VFPV3
:
188 Features
.push_back("+vfp3");
189 Features
.push_back("-fp16");
190 Features
.push_back("-vfp4");
191 Features
.push_back("-fp-armv8");
193 case FPUVersion::VFPV2
:
194 Features
.push_back("+vfp2");
195 Features
.push_back("-vfp3");
196 Features
.push_back("-fp16");
197 Features
.push_back("-vfp4");
198 Features
.push_back("-fp-armv8");
200 case FPUVersion::NONE
:
201 Features
.push_back("-fpregs");
202 Features
.push_back("-vfp2");
203 Features
.push_back("-vfp3");
204 Features
.push_back("-fp16");
205 Features
.push_back("-vfp4");
206 Features
.push_back("-fp-armv8");
210 // fp64 and d32 subtarget features are independent of each other, so we
211 // must disable/enable both.
212 if (FPUKind
== FK_NONE
) {
213 Features
.push_back("-fp64");
214 Features
.push_back("-d32");
216 switch (FPUNames
[FPUKind
].Restriction
) {
217 case FPURestriction::SP_D16
:
218 Features
.push_back("-fp64");
219 Features
.push_back("-d32");
221 case FPURestriction::D16
:
222 Features
.push_back("+fp64");
223 Features
.push_back("-d32");
225 case FPURestriction::None
:
226 Features
.push_back("+fp64");
227 Features
.push_back("+d32");
232 // crypto includes neon, so we handle this similarly to FPU version.
233 switch (FPUNames
[FPUKind
].NeonSupport
) {
234 case NeonSupportLevel::Crypto
:
235 Features
.push_back("+neon");
236 Features
.push_back("+crypto");
238 case NeonSupportLevel::Neon
:
239 Features
.push_back("+neon");
240 Features
.push_back("-crypto");
242 case NeonSupportLevel::None
:
243 Features
.push_back("-neon");
244 Features
.push_back("-crypto");
252 ARM::EndianKind
ARM::parseArchEndian(StringRef Arch
) {
253 if (Arch
.startswith("armeb") || Arch
.startswith("thumbeb") ||
254 Arch
.startswith("aarch64_be"))
255 return EndianKind::BIG
;
257 if (Arch
.startswith("arm") || Arch
.startswith("thumb")) {
258 if (Arch
.endswith("eb"))
259 return EndianKind::BIG
;
261 return EndianKind::LITTLE
;
264 if (Arch
.startswith("aarch64") || Arch
.startswith("aarch64_32"))
265 return EndianKind::LITTLE
;
267 return EndianKind::INVALID
;
270 // ARM, Thumb, AArch64
271 ARM::ISAKind
ARM::parseArchISA(StringRef Arch
) {
272 return StringSwitch
<ISAKind
>(Arch
)
273 .StartsWith("aarch64", ISAKind::AARCH64
)
274 .StartsWith("arm64", ISAKind::AARCH64
)
275 .StartsWith("thumb", ISAKind::THUMB
)
276 .StartsWith("arm", ISAKind::ARM
)
277 .Default(ISAKind::INVALID
);
280 unsigned ARM::parseFPU(StringRef FPU
) {
281 StringRef Syn
= getFPUSynonym(FPU
);
282 for (const auto F
: FPUNames
) {
283 if (Syn
== F
.getName())
289 ARM::NeonSupportLevel
ARM::getFPUNeonSupportLevel(unsigned FPUKind
) {
290 if (FPUKind
>= FK_LAST
)
291 return NeonSupportLevel::None
;
292 return FPUNames
[FPUKind
].NeonSupport
;
295 // MArch is expected to be of the form (arm|thumb)?(eb)?(v.+)?(eb)?, but
296 // (iwmmxt|xscale)(eb)? is also permitted. If the former, return
297 // "v.+", if the latter, return unmodified string, minus 'eb'.
298 // If invalid, return empty string.
299 StringRef
ARM::getCanonicalArchName(StringRef Arch
) {
300 size_t offset
= StringRef::npos
;
302 StringRef Error
= "";
304 // Begins with "arm" / "thumb", move past it.
305 if (A
.startswith("arm64_32"))
307 else if (A
.startswith("arm64"))
309 else if (A
.startswith("aarch64_32"))
311 else if (A
.startswith("arm"))
313 else if (A
.startswith("thumb"))
315 else if (A
.startswith("aarch64")) {
317 // AArch64 uses "_be", not "eb" suffix.
318 if (A
.find("eb") != StringRef::npos
)
320 if (A
.substr(offset
, 3) == "_be")
324 // Ex. "armebv7", move past the "eb".
325 if (offset
!= StringRef::npos
&& A
.substr(offset
, 2) == "eb")
327 // Or, if it ends with eb ("armv7eb"), chop it off.
328 else if (A
.endswith("eb"))
329 A
= A
.substr(0, A
.size() - 2);
331 if (offset
!= StringRef::npos
)
332 A
= A
.substr(offset
);
334 // Empty string means offset reached the end, which means it's valid.
338 // Only match non-marketing names
339 if (offset
!= StringRef::npos
) {
340 // Must start with 'vN'.
341 if (A
.size() >= 2 && (A
[0] != 'v' || !std::isdigit(A
[1])))
343 // Can't have an extra 'eb'.
344 if (A
.find("eb") != StringRef::npos
)
348 // Arch will either be a 'v' name (v7a) or a marketing name (xscale).
352 StringRef
ARM::getFPUSynonym(StringRef FPU
) {
353 return StringSwitch
<StringRef
>(FPU
)
354 .Cases("fpa", "fpe2", "fpe3", "maverick", "invalid") // Unsupported
355 .Case("vfp2", "vfpv2")
356 .Case("vfp3", "vfpv3")
357 .Case("vfp4", "vfpv4")
358 .Case("vfp3-d16", "vfpv3-d16")
359 .Case("vfp4-d16", "vfpv4-d16")
360 .Cases("fp4-sp-d16", "vfpv4-sp-d16", "fpv4-sp-d16")
361 .Cases("fp4-dp-d16", "fpv4-dp-d16", "vfpv4-d16")
362 .Case("fp5-sp-d16", "fpv5-sp-d16")
363 .Cases("fp5-dp-d16", "fpv5-dp-d16", "fpv5-d16")
364 // FIXME: Clang uses it, but it's bogus, since neon defaults to vfpv3.
365 .Case("neon-vfpv3", "neon")
369 StringRef
ARM::getFPUName(unsigned FPUKind
) {
370 if (FPUKind
>= FK_LAST
)
372 return FPUNames
[FPUKind
].getName();
375 ARM::FPUVersion
ARM::getFPUVersion(unsigned FPUKind
) {
376 if (FPUKind
>= FK_LAST
)
377 return FPUVersion::NONE
;
378 return FPUNames
[FPUKind
].FPUVer
;
381 ARM::FPURestriction
ARM::getFPURestriction(unsigned FPUKind
) {
382 if (FPUKind
>= FK_LAST
)
383 return FPURestriction::None
;
384 return FPUNames
[FPUKind
].Restriction
;
387 unsigned ARM::getDefaultFPU(StringRef CPU
, ARM::ArchKind AK
) {
388 if (CPU
== "generic")
389 return ARM::ARCHNames
[static_cast<unsigned>(AK
)].DefaultFPU
;
391 return StringSwitch
<unsigned>(CPU
)
392 #define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
393 .Case(NAME, DEFAULT_FPU)
394 #include "llvm/Support/ARMTargetParser.def"
395 .Default(ARM::FK_INVALID
);
398 unsigned ARM::getDefaultExtensions(StringRef CPU
, ARM::ArchKind AK
) {
399 if (CPU
== "generic")
400 return ARM::ARCHNames
[static_cast<unsigned>(AK
)].ArchBaseExtensions
;
402 return StringSwitch
<unsigned>(CPU
)
403 #define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
405 ARCHNames[static_cast<unsigned>(ArchKind::ID)].ArchBaseExtensions | \
407 #include "llvm/Support/ARMTargetParser.def"
408 .Default(ARM::AEK_INVALID
);
411 bool ARM::getHWDivFeatures(unsigned HWDivKind
,
412 std::vector
<StringRef
> &Features
) {
414 if (HWDivKind
== AEK_INVALID
)
417 if (HWDivKind
& AEK_HWDIVARM
)
418 Features
.push_back("+hwdiv-arm");
420 Features
.push_back("-hwdiv-arm");
422 if (HWDivKind
& AEK_HWDIVTHUMB
)
423 Features
.push_back("+hwdiv");
425 Features
.push_back("-hwdiv");
430 bool ARM::getExtensionFeatures(unsigned Extensions
,
431 std::vector
<StringRef
> &Features
) {
433 if (Extensions
== AEK_INVALID
)
436 if (Extensions
& AEK_CRC
)
437 Features
.push_back("+crc");
439 Features
.push_back("-crc");
441 if (Extensions
& AEK_DSP
)
442 Features
.push_back("+dsp");
444 Features
.push_back("-dsp");
446 if (Extensions
& AEK_FP16FML
)
447 Features
.push_back("+fp16fml");
449 Features
.push_back("-fp16fml");
451 if (Extensions
& AEK_RAS
)
452 Features
.push_back("+ras");
454 Features
.push_back("-ras");
456 if (Extensions
& AEK_DOTPROD
)
457 Features
.push_back("+dotprod");
459 Features
.push_back("-dotprod");
461 return getHWDivFeatures(Extensions
, Features
);
464 StringRef
ARM::getArchName(ARM::ArchKind AK
) {
465 return ARCHNames
[static_cast<unsigned>(AK
)].getName();
468 StringRef
ARM::getCPUAttr(ARM::ArchKind AK
) {
469 return ARCHNames
[static_cast<unsigned>(AK
)].getCPUAttr();
472 StringRef
ARM::getSubArch(ARM::ArchKind AK
) {
473 return ARCHNames
[static_cast<unsigned>(AK
)].getSubArch();
476 unsigned ARM::getArchAttr(ARM::ArchKind AK
) {
477 return ARCHNames
[static_cast<unsigned>(AK
)].ArchAttr
;
480 StringRef
ARM::getArchExtName(unsigned ArchExtKind
) {
481 for (const auto AE
: ARCHExtNames
) {
482 if (ArchExtKind
== AE
.ID
)
488 StringRef
ARM::getArchExtFeature(StringRef ArchExt
) {
489 if (ArchExt
.startswith("no")) {
490 StringRef
ArchExtBase(ArchExt
.substr(2));
491 for (const auto AE
: ARCHExtNames
) {
492 if (AE
.NegFeature
&& ArchExtBase
== AE
.getName())
493 return StringRef(AE
.NegFeature
);
496 for (const auto AE
: ARCHExtNames
) {
497 if (AE
.Feature
&& ArchExt
== AE
.getName())
498 return StringRef(AE
.Feature
);
504 StringRef
ARM::getHWDivName(unsigned HWDivKind
) {
505 for (const auto D
: HWDivNames
) {
506 if (HWDivKind
== D
.ID
)
512 StringRef
ARM::getDefaultCPU(StringRef Arch
) {
513 ArchKind AK
= parseArch(Arch
);
514 if (AK
== ArchKind::INVALID
)
517 // Look for multiple AKs to find the default for pair AK+Name.
518 for (const auto CPU
: CPUNames
) {
519 if (CPU
.ArchID
== AK
&& CPU
.Default
)
520 return CPU
.getName();
523 // If we can't find a default then target the architecture instead
527 unsigned ARM::parseHWDiv(StringRef HWDiv
) {
528 StringRef Syn
= getHWDivSynonym(HWDiv
);
529 for (const auto D
: HWDivNames
) {
530 if (Syn
== D
.getName())
536 unsigned ARM::parseArchExt(StringRef ArchExt
) {
537 for (const auto A
: ARCHExtNames
) {
538 if (ArchExt
== A
.getName())
544 ARM::ArchKind
ARM::parseCPUArch(StringRef CPU
) {
545 for (const auto C
: CPUNames
) {
546 if (CPU
== C
.getName())
549 return ArchKind::INVALID
;
552 void ARM::fillValidCPUArchList(SmallVectorImpl
<StringRef
> &Values
) {
553 for (const CpuNames
<ArchKind
> &Arch
: CPUNames
) {
554 if (Arch
.ArchID
!= ArchKind::INVALID
)
555 Values
.push_back(Arch
.getName());
559 StringRef
ARM::computeDefaultTargetABI(const Triple
&TT
, StringRef CPU
) {
561 CPU
.empty() ? TT
.getArchName() : getArchName(parseCPUArch(CPU
));
563 if (TT
.isOSBinFormatMachO()) {
564 if (TT
.getEnvironment() == Triple::EABI
||
565 TT
.getOS() == Triple::UnknownOS
||
566 parseArchProfile(ArchName
) == ProfileKind::M
)
571 } else if (TT
.isOSWindows())
572 // FIXME: this is invalid for WindowsCE.
575 // Select the default based on the platform.
576 switch (TT
.getEnvironment()) {
577 case Triple::Android
:
578 case Triple::GNUEABI
:
579 case Triple::GNUEABIHF
:
580 case Triple::MuslEABI
:
581 case Triple::MuslEABIHF
:
582 return "aapcs-linux";
589 if (TT
.isOSOpenBSD())
590 return "aapcs-linux";