blob: 02f0d95ff278416ea8bf94b2c80cee5c45c20b70 [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
David Spickette01718c2018-11-28 11:38:10 +0000162 // FPU version subtarget features are inclusive of lower-numbered ones, so
163 // enable the one corresponding to this version and disable all that are
164 // higher. We also have to make sure to disable fp16 when vfp4 is disabled,
165 // as +vfp4 implies +fp16 but -vfp4 does not imply -fp16.
166 switch (FPUNames[FPUKind].FPUVer) {
167 case FPUVersion::VFPV5:
168 Features.push_back("+fp-armv8");
169 break;
170 case FPUVersion::VFPV4:
171 Features.push_back("+vfp4");
172 Features.push_back("-fp-armv8");
173 break;
174 case FPUVersion::VFPV3_FP16:
175 Features.push_back("+vfp3");
176 Features.push_back("+fp16");
177 Features.push_back("-vfp4");
178 Features.push_back("-fp-armv8");
179 break;
180 case FPUVersion::VFPV3:
181 Features.push_back("+vfp3");
182 Features.push_back("-fp16");
183 Features.push_back("-vfp4");
184 Features.push_back("-fp-armv8");
185 break;
186 case FPUVersion::VFPV2:
187 Features.push_back("+vfp2");
188 Features.push_back("-vfp3");
189 Features.push_back("-fp16");
190 Features.push_back("-vfp4");
191 Features.push_back("-fp-armv8");
192 break;
193 case FPUVersion::NONE:
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");
199 break;
200 }
201
Simon Tatham760df472019-05-28 16:13:20 +0000202 // fp64 and d32 subtarget features are independent of each other, so we
203 // must disable/enable both.
204 if (FPUKind == FK_NONE) {
205 Features.push_back("-fp64");
206 Features.push_back("-d32");
207 } else {
208 switch (FPUNames[FPUKind].Restriction) {
209 case FPURestriction::SP_D16:
210 Features.push_back("-fp64");
211 Features.push_back("-d32");
212 break;
213 case FPURestriction::D16:
214 Features.push_back("+fp64");
215 Features.push_back("-d32");
216 break;
217 case FPURestriction::None:
218 Features.push_back("+fp64");
219 Features.push_back("+d32");
220 break;
221 }
222 }
223
David Spickette01718c2018-11-28 11:38:10 +0000224 // crypto includes neon, so we handle this similarly to FPU version.
225 switch (FPUNames[FPUKind].NeonSupport) {
226 case NeonSupportLevel::Crypto:
227 Features.push_back("+neon");
228 Features.push_back("+crypto");
229 break;
230 case NeonSupportLevel::Neon:
231 Features.push_back("+neon");
232 Features.push_back("-crypto");
233 break;
234 case NeonSupportLevel::None:
235 Features.push_back("-neon");
236 Features.push_back("-crypto");
237 break;
238 }
239
240 return true;
241}
242
243// Little/Big endian
244ARM::EndianKind ARM::parseArchEndian(StringRef Arch) {
245 if (Arch.startswith("armeb") || Arch.startswith("thumbeb") ||
246 Arch.startswith("aarch64_be"))
247 return EndianKind::BIG;
248
249 if (Arch.startswith("arm") || Arch.startswith("thumb")) {
250 if (Arch.endswith("eb"))
251 return EndianKind::BIG;
252 else
253 return EndianKind::LITTLE;
254 }
255
Tim Northoverff6875a2019-05-14 11:25:44 +0000256 if (Arch.startswith("aarch64") || Arch.startswith("aarch64_32"))
David Spickette01718c2018-11-28 11:38:10 +0000257 return EndianKind::LITTLE;
258
259 return EndianKind::INVALID;
260}
261
262// ARM, Thumb, AArch64
263ARM::ISAKind ARM::parseArchISA(StringRef Arch) {
264 return StringSwitch<ISAKind>(Arch)
265 .StartsWith("aarch64", ISAKind::AARCH64)
266 .StartsWith("arm64", ISAKind::AARCH64)
267 .StartsWith("thumb", ISAKind::THUMB)
268 .StartsWith("arm", ISAKind::ARM)
269 .Default(ISAKind::INVALID);
270}
271
272unsigned ARM::parseFPU(StringRef FPU) {
273 StringRef Syn = getFPUSynonym(FPU);
274 for (const auto F : FPUNames) {
275 if (Syn == F.getName())
276 return F.ID;
277 }
278 return FK_INVALID;
279}
280
281ARM::NeonSupportLevel ARM::getFPUNeonSupportLevel(unsigned FPUKind) {
282 if (FPUKind >= FK_LAST)
283 return NeonSupportLevel::None;
284 return FPUNames[FPUKind].NeonSupport;
285}
286
287// MArch is expected to be of the form (arm|thumb)?(eb)?(v.+)?(eb)?, but
288// (iwmmxt|xscale)(eb)? is also permitted. If the former, return
289// "v.+", if the latter, return unmodified string, minus 'eb'.
290// If invalid, return empty string.
291StringRef ARM::getCanonicalArchName(StringRef Arch) {
292 size_t offset = StringRef::npos;
293 StringRef A = Arch;
294 StringRef Error = "";
295
296 // Begins with "arm" / "thumb", move past it.
Tim Northoverff6875a2019-05-14 11:25:44 +0000297 if (A.startswith("arm64_32"))
298 offset = 8;
299 else if (A.startswith("arm64"))
David Spickette01718c2018-11-28 11:38:10 +0000300 offset = 5;
Tim Northoverff6875a2019-05-14 11:25:44 +0000301 else if (A.startswith("aarch64_32"))
302 offset = 10;
David Spickette01718c2018-11-28 11:38:10 +0000303 else if (A.startswith("arm"))
304 offset = 3;
305 else if (A.startswith("thumb"))
306 offset = 5;
307 else if (A.startswith("aarch64")) {
308 offset = 7;
309 // AArch64 uses "_be", not "eb" suffix.
310 if (A.find("eb") != StringRef::npos)
311 return Error;
312 if (A.substr(offset, 3) == "_be")
313 offset += 3;
314 }
315
316 // Ex. "armebv7", move past the "eb".
317 if (offset != StringRef::npos && A.substr(offset, 2) == "eb")
318 offset += 2;
319 // Or, if it ends with eb ("armv7eb"), chop it off.
320 else if (A.endswith("eb"))
321 A = A.substr(0, A.size() - 2);
322 // Trim the head
323 if (offset != StringRef::npos)
324 A = A.substr(offset);
325
326 // Empty string means offset reached the end, which means it's valid.
327 if (A.empty())
328 return Arch;
329
330 // Only match non-marketing names
331 if (offset != StringRef::npos) {
332 // Must start with 'vN'.
333 if (A.size() >= 2 && (A[0] != 'v' || !std::isdigit(A[1])))
334 return Error;
335 // Can't have an extra 'eb'.
336 if (A.find("eb") != StringRef::npos)
337 return Error;
338 }
339
340 // Arch will either be a 'v' name (v7a) or a marketing name (xscale).
341 return A;
342}
343
344StringRef ARM::getFPUSynonym(StringRef FPU) {
345 return StringSwitch<StringRef>(FPU)
346 .Cases("fpa", "fpe2", "fpe3", "maverick", "invalid") // Unsupported
347 .Case("vfp2", "vfpv2")
348 .Case("vfp3", "vfpv3")
349 .Case("vfp4", "vfpv4")
350 .Case("vfp3-d16", "vfpv3-d16")
351 .Case("vfp4-d16", "vfpv4-d16")
352 .Cases("fp4-sp-d16", "vfpv4-sp-d16", "fpv4-sp-d16")
353 .Cases("fp4-dp-d16", "fpv4-dp-d16", "vfpv4-d16")
354 .Case("fp5-sp-d16", "fpv5-sp-d16")
355 .Cases("fp5-dp-d16", "fpv5-dp-d16", "fpv5-d16")
356 // FIXME: Clang uses it, but it's bogus, since neon defaults to vfpv3.
357 .Case("neon-vfpv3", "neon")
358 .Default(FPU);
359}
360
361StringRef ARM::getFPUName(unsigned FPUKind) {
362 if (FPUKind >= FK_LAST)
363 return StringRef();
364 return FPUNames[FPUKind].getName();
365}
366
367ARM::FPUVersion ARM::getFPUVersion(unsigned FPUKind) {
368 if (FPUKind >= FK_LAST)
369 return FPUVersion::NONE;
370 return FPUNames[FPUKind].FPUVer;
371}
372
373ARM::FPURestriction ARM::getFPURestriction(unsigned FPUKind) {
374 if (FPUKind >= FK_LAST)
375 return FPURestriction::None;
376 return FPUNames[FPUKind].Restriction;
377}
378
379unsigned ARM::getDefaultFPU(StringRef CPU, ARM::ArchKind AK) {
380 if (CPU == "generic")
381 return ARM::ARCHNames[static_cast<unsigned>(AK)].DefaultFPU;
382
383 return StringSwitch<unsigned>(CPU)
384#define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
385 .Case(NAME, DEFAULT_FPU)
386#include "llvm/Support/ARMTargetParser.def"
387 .Default(ARM::FK_INVALID);
388}
389
390unsigned ARM::getDefaultExtensions(StringRef CPU, ARM::ArchKind AK) {
391 if (CPU == "generic")
392 return ARM::ARCHNames[static_cast<unsigned>(AK)].ArchBaseExtensions;
393
394 return StringSwitch<unsigned>(CPU)
395#define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
396 .Case(NAME, \
397 ARCHNames[static_cast<unsigned>(ArchKind::ID)].ArchBaseExtensions | \
398 DEFAULT_EXT)
399#include "llvm/Support/ARMTargetParser.def"
400 .Default(ARM::AEK_INVALID);
401}
402
403bool ARM::getHWDivFeatures(unsigned HWDivKind,
404 std::vector<StringRef> &Features) {
405
406 if (HWDivKind == AEK_INVALID)
407 return false;
408
409 if (HWDivKind & AEK_HWDIVARM)
410 Features.push_back("+hwdiv-arm");
411 else
412 Features.push_back("-hwdiv-arm");
413
414 if (HWDivKind & AEK_HWDIVTHUMB)
415 Features.push_back("+hwdiv");
416 else
417 Features.push_back("-hwdiv");
418
419 return true;
420}
421
422bool ARM::getExtensionFeatures(unsigned Extensions,
423 std::vector<StringRef> &Features) {
424
425 if (Extensions == AEK_INVALID)
426 return false;
427
428 if (Extensions & AEK_CRC)
429 Features.push_back("+crc");
430 else
431 Features.push_back("-crc");
432
433 if (Extensions & AEK_DSP)
434 Features.push_back("+dsp");
435 else
436 Features.push_back("-dsp");
437
438 if (Extensions & AEK_FP16FML)
439 Features.push_back("+fp16fml");
440 else
441 Features.push_back("-fp16fml");
442
443 if (Extensions & AEK_RAS)
444 Features.push_back("+ras");
445 else
446 Features.push_back("-ras");
447
448 if (Extensions & AEK_DOTPROD)
449 Features.push_back("+dotprod");
450 else
451 Features.push_back("-dotprod");
452
453 return getHWDivFeatures(Extensions, Features);
454}
455
456StringRef ARM::getArchName(ARM::ArchKind AK) {
457 return ARCHNames[static_cast<unsigned>(AK)].getName();
458}
459
460StringRef ARM::getCPUAttr(ARM::ArchKind AK) {
461 return ARCHNames[static_cast<unsigned>(AK)].getCPUAttr();
462}
463
464StringRef ARM::getSubArch(ARM::ArchKind AK) {
465 return ARCHNames[static_cast<unsigned>(AK)].getSubArch();
466}
467
468unsigned ARM::getArchAttr(ARM::ArchKind AK) {
469 return ARCHNames[static_cast<unsigned>(AK)].ArchAttr;
470}
471
472StringRef ARM::getArchExtName(unsigned ArchExtKind) {
473 for (const auto AE : ARCHExtNames) {
474 if (ArchExtKind == AE.ID)
475 return AE.getName();
476 }
477 return StringRef();
478}
479
480StringRef ARM::getArchExtFeature(StringRef ArchExt) {
481 if (ArchExt.startswith("no")) {
482 StringRef ArchExtBase(ArchExt.substr(2));
483 for (const auto AE : ARCHExtNames) {
484 if (AE.NegFeature && ArchExtBase == AE.getName())
485 return StringRef(AE.NegFeature);
486 }
487 }
488 for (const auto AE : ARCHExtNames) {
489 if (AE.Feature && ArchExt == AE.getName())
490 return StringRef(AE.Feature);
491 }
492
493 return StringRef();
494}
495
496StringRef ARM::getHWDivName(unsigned HWDivKind) {
497 for (const auto D : HWDivNames) {
498 if (HWDivKind == D.ID)
499 return D.getName();
500 }
501 return StringRef();
502}
503
504StringRef ARM::getDefaultCPU(StringRef Arch) {
505 ArchKind AK = parseArch(Arch);
506 if (AK == ArchKind::INVALID)
507 return StringRef();
508
509 // Look for multiple AKs to find the default for pair AK+Name.
510 for (const auto CPU : CPUNames) {
511 if (CPU.ArchID == AK && CPU.Default)
512 return CPU.getName();
513 }
514
515 // If we can't find a default then target the architecture instead
516 return "generic";
517}
518
519unsigned ARM::parseHWDiv(StringRef HWDiv) {
520 StringRef Syn = getHWDivSynonym(HWDiv);
521 for (const auto D : HWDivNames) {
522 if (Syn == D.getName())
523 return D.ID;
524 }
525 return AEK_INVALID;
526}
527
528unsigned ARM::parseArchExt(StringRef ArchExt) {
529 for (const auto A : ARCHExtNames) {
530 if (ArchExt == A.getName())
531 return A.ID;
532 }
533 return AEK_INVALID;
534}
535
536ARM::ArchKind ARM::parseCPUArch(StringRef CPU) {
537 for (const auto C : CPUNames) {
538 if (CPU == C.getName())
539 return C.ArchID;
540 }
541 return ArchKind::INVALID;
542}
543
544void ARM::fillValidCPUArchList(SmallVectorImpl<StringRef> &Values) {
545 for (const CpuNames<ArchKind> &Arch : CPUNames) {
546 if (Arch.ArchID != ArchKind::INVALID)
547 Values.push_back(Arch.getName());
548 }
549}
550
551StringRef ARM::computeDefaultTargetABI(const Triple &TT, StringRef CPU) {
552 StringRef ArchName =
553 CPU.empty() ? TT.getArchName() : getArchName(parseCPUArch(CPU));
554
555 if (TT.isOSBinFormatMachO()) {
556 if (TT.getEnvironment() == Triple::EABI ||
557 TT.getOS() == Triple::UnknownOS ||
558 parseArchProfile(ArchName) == ProfileKind::M)
559 return "aapcs";
560 if (TT.isWatchABI())
561 return "aapcs16";
562 return "apcs-gnu";
563 } else if (TT.isOSWindows())
564 // FIXME: this is invalid for WindowsCE.
565 return "aapcs";
566
567 // Select the default based on the platform.
568 switch (TT.getEnvironment()) {
569 case Triple::Android:
570 case Triple::GNUEABI:
571 case Triple::GNUEABIHF:
572 case Triple::MuslEABI:
573 case Triple::MuslEABIHF:
574 return "aapcs-linux";
575 case Triple::EABIHF:
576 case Triple::EABI:
577 return "aapcs";
578 default:
579 if (TT.isOSNetBSD())
580 return "apcs-gnu";
581 if (TT.isOSOpenBSD())
582 return "aapcs-linux";
583 return "aapcs";
584 }
585}