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
:
81 case ArchKind::INVALID
:
84 llvm_unreachable("Unhandled architecture");
88 ARM::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
;
130 llvm_unreachable("Unhandled architecture");
133 StringRef
ARM::getArchSynonym(StringRef Arch
) {
134 return StringSwitch
<StringRef
>(Arch
)
139 .Cases("v6m", "v6sm", "v6s-m", "v6-m")
140 .Cases("v6z", "v6zk", "v6kz")
141 .Cases("v7", "v7a", "v7hl", "v7l", "v7-a")
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")
152 .Case("v8m.base", "v8-m.base")
153 .Case("v8m.main", "v8-m.main")
157 bool ARM::getFPUFeatures(unsigned FPUKind
, std::vector
<StringRef
> &Features
) {
159 if (FPUKind
>= FK_LAST
|| FPUKind
== FK_INVALID
)
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");
169 case FPURestriction::D16
:
170 Features
.push_back("-fp-only-sp");
171 Features
.push_back("+d16");
173 case FPURestriction::None
:
174 Features
.push_back("-fp-only-sp");
175 Features
.push_back("-d16");
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");
187 case FPUVersion::VFPV4
:
188 Features
.push_back("+vfp4");
189 Features
.push_back("-fp-armv8");
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");
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");
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");
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");
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");
225 case NeonSupportLevel::Neon
:
226 Features
.push_back("+neon");
227 Features
.push_back("-crypto");
229 case NeonSupportLevel::None
:
230 Features
.push_back("-neon");
231 Features
.push_back("-crypto");
239 ARM::EndianKind
ARM::parseArchEndian(StringRef Arch
) {
240 if (Arch
.startswith("armeb") || Arch
.startswith("thumbeb") ||
241 Arch
.startswith("aarch64_be"))
242 return EndianKind::BIG
;
244 if (Arch
.startswith("arm") || Arch
.startswith("thumb")) {
245 if (Arch
.endswith("eb"))
246 return EndianKind::BIG
;
248 return EndianKind::LITTLE
;
251 if (Arch
.startswith("aarch64"))
252 return EndianKind::LITTLE
;
254 return EndianKind::INVALID
;
257 // ARM, Thumb, AArch64
258 ARM::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
);
267 unsigned ARM::parseFPU(StringRef FPU
) {
268 StringRef Syn
= getFPUSynonym(FPU
);
269 for (const auto F
: FPUNames
) {
270 if (Syn
== F
.getName())
276 ARM::NeonSupportLevel
ARM::getFPUNeonSupportLevel(unsigned FPUKind
) {
277 if (FPUKind
>= FK_LAST
)
278 return NeonSupportLevel::None
;
279 return FPUNames
[FPUKind
].NeonSupport
;
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.
286 StringRef
ARM::getCanonicalArchName(StringRef Arch
) {
287 size_t offset
= StringRef::npos
;
289 StringRef Error
= "";
291 // Begins with "arm" / "thumb", move past it.
292 if (A
.startswith("arm64"))
294 else if (A
.startswith("arm"))
296 else if (A
.startswith("thumb"))
298 else if (A
.startswith("aarch64")) {
300 // AArch64 uses "_be", not "eb" suffix.
301 if (A
.find("eb") != StringRef::npos
)
303 if (A
.substr(offset
, 3) == "_be")
307 // Ex. "armebv7", move past the "eb".
308 if (offset
!= StringRef::npos
&& A
.substr(offset
, 2) == "eb")
310 // Or, if it ends with eb ("armv7eb"), chop it off.
311 else if (A
.endswith("eb"))
312 A
= A
.substr(0, A
.size() - 2);
314 if (offset
!= StringRef::npos
)
315 A
= A
.substr(offset
);
317 // Empty string means offset reached the end, which means it's valid.
321 // Only match non-marketing names
322 if (offset
!= StringRef::npos
) {
323 // Must start with 'vN'.
324 if (A
.size() >= 2 && (A
[0] != 'v' || !std::isdigit(A
[1])))
326 // Can't have an extra 'eb'.
327 if (A
.find("eb") != StringRef::npos
)
331 // Arch will either be a 'v' name (v7a) or a marketing name (xscale).
335 StringRef
ARM::getFPUSynonym(StringRef FPU
) {
336 return StringSwitch
<StringRef
>(FPU
)
337 .Cases("fpa", "fpe2", "fpe3", "maverick", "invalid") // Unsupported
338 .Case("vfp2", "vfpv2")
339 .Case("vfp3", "vfpv3")
340 .Case("vfp4", "vfpv4")
341 .Case("vfp3-d16", "vfpv3-d16")
342 .Case("vfp4-d16", "vfpv4-d16")
343 .Cases("fp4-sp-d16", "vfpv4-sp-d16", "fpv4-sp-d16")
344 .Cases("fp4-dp-d16", "fpv4-dp-d16", "vfpv4-d16")
345 .Case("fp5-sp-d16", "fpv5-sp-d16")
346 .Cases("fp5-dp-d16", "fpv5-dp-d16", "fpv5-d16")
347 // FIXME: Clang uses it, but it's bogus, since neon defaults to vfpv3.
348 .Case("neon-vfpv3", "neon")
352 StringRef
ARM::getFPUName(unsigned FPUKind
) {
353 if (FPUKind
>= FK_LAST
)
355 return FPUNames
[FPUKind
].getName();
358 ARM::FPUVersion
ARM::getFPUVersion(unsigned FPUKind
) {
359 if (FPUKind
>= FK_LAST
)
360 return FPUVersion::NONE
;
361 return FPUNames
[FPUKind
].FPUVer
;
364 ARM::FPURestriction
ARM::getFPURestriction(unsigned FPUKind
) {
365 if (FPUKind
>= FK_LAST
)
366 return FPURestriction::None
;
367 return FPUNames
[FPUKind
].Restriction
;
370 unsigned ARM::getDefaultFPU(StringRef CPU
, ARM::ArchKind AK
) {
371 if (CPU
== "generic")
372 return ARM::ARCHNames
[static_cast<unsigned>(AK
)].DefaultFPU
;
374 return StringSwitch
<unsigned>(CPU
)
375 #define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
376 .Case(NAME, DEFAULT_FPU)
377 #include "llvm/Support/ARMTargetParser.def"
378 .Default(ARM::FK_INVALID
);
381 unsigned ARM::getDefaultExtensions(StringRef CPU
, ARM::ArchKind AK
) {
382 if (CPU
== "generic")
383 return ARM::ARCHNames
[static_cast<unsigned>(AK
)].ArchBaseExtensions
;
385 return StringSwitch
<unsigned>(CPU
)
386 #define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
388 ARCHNames[static_cast<unsigned>(ArchKind::ID)].ArchBaseExtensions | \
390 #include "llvm/Support/ARMTargetParser.def"
391 .Default(ARM::AEK_INVALID
);
394 bool ARM::getHWDivFeatures(unsigned HWDivKind
,
395 std::vector
<StringRef
> &Features
) {
397 if (HWDivKind
== AEK_INVALID
)
400 if (HWDivKind
& AEK_HWDIVARM
)
401 Features
.push_back("+hwdiv-arm");
403 Features
.push_back("-hwdiv-arm");
405 if (HWDivKind
& AEK_HWDIVTHUMB
)
406 Features
.push_back("+hwdiv");
408 Features
.push_back("-hwdiv");
413 bool ARM::getExtensionFeatures(unsigned Extensions
,
414 std::vector
<StringRef
> &Features
) {
416 if (Extensions
== AEK_INVALID
)
419 if (Extensions
& AEK_CRC
)
420 Features
.push_back("+crc");
422 Features
.push_back("-crc");
424 if (Extensions
& AEK_DSP
)
425 Features
.push_back("+dsp");
427 Features
.push_back("-dsp");
429 if (Extensions
& AEK_FP16FML
)
430 Features
.push_back("+fp16fml");
432 Features
.push_back("-fp16fml");
434 if (Extensions
& AEK_RAS
)
435 Features
.push_back("+ras");
437 Features
.push_back("-ras");
439 if (Extensions
& AEK_DOTPROD
)
440 Features
.push_back("+dotprod");
442 Features
.push_back("-dotprod");
444 return getHWDivFeatures(Extensions
, Features
);
447 StringRef
ARM::getArchName(ARM::ArchKind AK
) {
448 return ARCHNames
[static_cast<unsigned>(AK
)].getName();
451 StringRef
ARM::getCPUAttr(ARM::ArchKind AK
) {
452 return ARCHNames
[static_cast<unsigned>(AK
)].getCPUAttr();
455 StringRef
ARM::getSubArch(ARM::ArchKind AK
) {
456 return ARCHNames
[static_cast<unsigned>(AK
)].getSubArch();
459 unsigned ARM::getArchAttr(ARM::ArchKind AK
) {
460 return ARCHNames
[static_cast<unsigned>(AK
)].ArchAttr
;
463 StringRef
ARM::getArchExtName(unsigned ArchExtKind
) {
464 for (const auto AE
: ARCHExtNames
) {
465 if (ArchExtKind
== AE
.ID
)
471 StringRef
ARM::getArchExtFeature(StringRef ArchExt
) {
472 if (ArchExt
.startswith("no")) {
473 StringRef
ArchExtBase(ArchExt
.substr(2));
474 for (const auto AE
: ARCHExtNames
) {
475 if (AE
.NegFeature
&& ArchExtBase
== AE
.getName())
476 return StringRef(AE
.NegFeature
);
479 for (const auto AE
: ARCHExtNames
) {
480 if (AE
.Feature
&& ArchExt
== AE
.getName())
481 return StringRef(AE
.Feature
);
487 StringRef
ARM::getHWDivName(unsigned HWDivKind
) {
488 for (const auto D
: HWDivNames
) {
489 if (HWDivKind
== D
.ID
)
495 StringRef
ARM::getDefaultCPU(StringRef Arch
) {
496 ArchKind AK
= parseArch(Arch
);
497 if (AK
== ArchKind::INVALID
)
500 // Look for multiple AKs to find the default for pair AK+Name.
501 for (const auto CPU
: CPUNames
) {
502 if (CPU
.ArchID
== AK
&& CPU
.Default
)
503 return CPU
.getName();
506 // If we can't find a default then target the architecture instead
510 unsigned ARM::parseHWDiv(StringRef HWDiv
) {
511 StringRef Syn
= getHWDivSynonym(HWDiv
);
512 for (const auto D
: HWDivNames
) {
513 if (Syn
== D
.getName())
519 unsigned ARM::parseArchExt(StringRef ArchExt
) {
520 for (const auto A
: ARCHExtNames
) {
521 if (ArchExt
== A
.getName())
527 ARM::ArchKind
ARM::parseCPUArch(StringRef CPU
) {
528 for (const auto C
: CPUNames
) {
529 if (CPU
== C
.getName())
532 return ArchKind::INVALID
;
535 void ARM::fillValidCPUArchList(SmallVectorImpl
<StringRef
> &Values
) {
536 for (const CpuNames
<ArchKind
> &Arch
: CPUNames
) {
537 if (Arch
.ArchID
!= ArchKind::INVALID
)
538 Values
.push_back(Arch
.getName());
542 StringRef
ARM::computeDefaultTargetABI(const Triple
&TT
, StringRef CPU
) {
544 CPU
.empty() ? TT
.getArchName() : getArchName(parseCPUArch(CPU
));
546 if (TT
.isOSBinFormatMachO()) {
547 if (TT
.getEnvironment() == Triple::EABI
||
548 TT
.getOS() == Triple::UnknownOS
||
549 parseArchProfile(ArchName
) == ProfileKind::M
)
554 } else if (TT
.isOSWindows())
555 // FIXME: this is invalid for WindowsCE.
558 // Select the default based on the platform.
559 switch (TT
.getEnvironment()) {
560 case Triple::Android
:
561 case Triple::GNUEABI
:
562 case Triple::GNUEABIHF
:
563 case Triple::MuslEABI
:
564 case Triple::MuslEABIHF
:
565 return "aapcs-linux";
572 if (TT
.isOSOpenBSD())
573 return "aapcs-linux";