[ARM] Add +feature names to TargetParser extensions table
[oota-llvm.git] / lib / Support / TargetParser.cpp
1 //===-- TargetParser - Parser for target features ---------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a target parser to recognise hardware features such as
11 // FPU/CPU/ARCH names as well as specific support such as HDIV, etc.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Support/ARMBuildAttributes.h"
16 #include "llvm/Support/TargetParser.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/ADT/Twine.h"
20 #include <cctype>
21
22 using namespace llvm;
23 using namespace ARM;
24
25 namespace {
26
27 // List of canonical FPU names (use getFPUSynonym) and which architectural
28 // features they correspond to (use getFPUFeatures).
29 // FIXME: TableGen this.
30 // The entries must appear in the order listed in ARM::FPUKind for correct indexing
31 static const struct {
32   const char *NameCStr;
33   size_t NameLength;
34   ARM::FPUKind ID;
35   ARM::FPUVersion FPUVersion;
36   ARM::NeonSupportLevel NeonSupport;
37   ARM::FPURestriction Restriction;
38
39   StringRef getName() const { return StringRef(NameCStr, NameLength); }
40 } FPUNames[] = {
41 #define ARM_FPU(NAME, KIND, VERSION, NEON_SUPPORT, RESTRICTION) \
42   { NAME, sizeof(NAME) - 1, KIND, VERSION, NEON_SUPPORT, RESTRICTION },
43 #include "llvm/Support/ARMTargetParser.def"
44 };
45
46 // List of canonical arch names (use getArchSynonym).
47 // This table also provides the build attribute fields for CPU arch
48 // and Arch ID, according to the Addenda to the ARM ABI, chapters
49 // 2.4 and 2.3.5.2 respectively.
50 // FIXME: SubArch values were simplified to fit into the expectations
51 // of the triples and are not conforming with their official names.
52 // Check to see if the expectation should be changed.
53 // FIXME: TableGen this.
54 static const struct {
55   const char *NameCStr;
56   size_t NameLength;
57   ARM::ArchKind ID;
58   const char *CPUAttrCStr;
59   size_t CPUAttrLength;
60   const char *SubArchCStr;
61   size_t SubArchLength;
62   ARMBuildAttrs::CPUArch ArchAttr; // Arch ID in build attributes.
63   unsigned DefaultFPU;
64   unsigned ArchBaseExtensions;
65
66   StringRef getName() const { return StringRef(NameCStr, NameLength); }
67
68   // CPU class in build attributes.
69   StringRef getCPUAttr() const { return StringRef(CPUAttrCStr, CPUAttrLength); }
70
71   // Sub-Arch name.
72   StringRef getSubArch() const { return StringRef(SubArchCStr, SubArchLength); }
73 } ARCHNames[] = {
74 #define ARM_ARCH(NAME, ID, CPU_ATTR, SUB_ARCH, ARCH_ATTR, ARCH_FPU, ARCH_BASE_EXT)       \
75   {NAME, sizeof(NAME) - 1, ID, CPU_ATTR, sizeof(CPU_ATTR) - 1, SUB_ARCH,       \
76    sizeof(SUB_ARCH) - 1, ARCH_ATTR, ARCH_FPU, ARCH_BASE_EXT},
77 #include "llvm/Support/ARMTargetParser.def"
78 };
79
80 // List of Arch Extension names.
81 // FIXME: TableGen this.
82 static const struct {
83   const char *NameCStr;
84   size_t NameLength;
85   unsigned ID;
86   const char *Feature;
87   const char *NegFeature;
88
89   StringRef getName() const { return StringRef(NameCStr, NameLength); }
90   StringRef getNegName() const { return (Twine("no") + getName()).str(); }
91 } ARCHExtNames[] = {
92 #define ARM_ARCH_EXT_NAME(NAME, ID, FEATURE, NEGFEATURE) \
93   { NAME, sizeof(NAME) - 1, ID, FEATURE, NEGFEATURE },
94 #include "llvm/Support/ARMTargetParser.def"
95 };
96
97 // List of HWDiv names (use getHWDivSynonym) and which architectural
98 // features they correspond to (use getHWDivFeatures).
99 // FIXME: TableGen this.
100 static const struct {
101   const char *NameCStr;
102   size_t NameLength;
103   unsigned ID;
104
105   StringRef getName() const { return StringRef(NameCStr, NameLength); }
106 } HWDivNames[] = {
107 #define ARM_HW_DIV_NAME(NAME, ID) { NAME, sizeof(NAME) - 1, ID },
108 #include "llvm/Support/ARMTargetParser.def"
109 };
110
111 // List of CPU names and their arches.
112 // The same CPU can have multiple arches and can be default on multiple arches.
113 // When finding the Arch for a CPU, first-found prevails. Sort them accordingly.
114 // When this becomes table-generated, we'd probably need two tables.
115 // FIXME: TableGen this.
116 static const struct {
117   const char *NameCStr;
118   size_t NameLength;
119   ARM::ArchKind ArchID;
120   bool Default; // is $Name the default CPU for $ArchID ?
121   unsigned DefaultExtensions;
122
123   StringRef getName() const { return StringRef(NameCStr, NameLength); }
124 } CPUNames[] = {
125 #define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
126   { NAME, sizeof(NAME) - 1, ID, IS_DEFAULT, DEFAULT_EXT },
127 #include "llvm/Support/ARMTargetParser.def"
128 };
129
130 } // namespace
131
132 // ======================================================= //
133 // Information by ID
134 // ======================================================= //
135
136 StringRef llvm::ARM::getFPUName(unsigned FPUKind) {
137   if (FPUKind >= ARM::FK_LAST)
138     return StringRef();
139   return FPUNames[FPUKind].getName();
140 }
141
142 unsigned llvm::ARM::getFPUVersion(unsigned FPUKind) {
143   if (FPUKind >= ARM::FK_LAST)
144     return 0;
145   return FPUNames[FPUKind].FPUVersion;
146 }
147
148 unsigned llvm::ARM::getFPUNeonSupportLevel(unsigned FPUKind) {
149   if (FPUKind >= ARM::FK_LAST)
150     return 0;
151   return FPUNames[FPUKind].NeonSupport;
152 }
153
154 unsigned llvm::ARM::getFPURestriction(unsigned FPUKind) {
155   if (FPUKind >= ARM::FK_LAST)
156     return 0;
157   return FPUNames[FPUKind].Restriction;
158 }
159
160 unsigned llvm::ARM::getDefaultFPU(StringRef CPU, unsigned ArchKind) {
161   if (CPU == "generic")
162     return ARCHNames[ArchKind].DefaultFPU;
163
164   return StringSwitch<unsigned>(CPU)
165 #define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
166     .Case(NAME, DEFAULT_FPU)
167 #include "llvm/Support/ARMTargetParser.def"
168     .Default(ARM::FK_INVALID);
169 }
170
171 unsigned llvm::ARM::getDefaultExtensions(StringRef CPU, unsigned ArchKind) {
172   if (CPU == "generic")
173     return ARCHNames[ArchKind].ArchBaseExtensions;
174
175   return StringSwitch<unsigned>(CPU)
176 #define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
177     .Case(NAME, ARCHNames[ID].ArchBaseExtensions | DEFAULT_EXT)
178 #include "llvm/Support/ARMTargetParser.def"
179     .Default(ARM::AEK_INVALID);
180 }
181
182 bool llvm::ARM::getHWDivFeatures(unsigned HWDivKind,
183                                  std::vector<const char *> &Features) {
184
185   if (HWDivKind == ARM::AEK_INVALID)
186     return false;
187
188   if (HWDivKind & ARM::AEK_HWDIVARM)
189     Features.push_back("+hwdiv-arm");
190   else
191     Features.push_back("-hwdiv-arm");
192
193   if (HWDivKind & ARM::AEK_HWDIV)
194     Features.push_back("+hwdiv");
195   else
196     Features.push_back("-hwdiv");
197
198   return true;
199 }
200
201 bool llvm::ARM::getExtensionFeatures(unsigned Extensions,
202                                      std::vector<const char *> &Features) {
203
204   if (Extensions == ARM::AEK_INVALID)
205     return false;
206
207   if (Extensions & ARM::AEK_CRC)
208     Features.push_back("+crc");
209   else
210     Features.push_back("-crc");
211
212   if (Extensions & ARM::AEK_DSP)
213     Features.push_back("+dsp");
214   else
215     Features.push_back("-dsp");
216
217   return getHWDivFeatures(Extensions, Features);
218 }
219
220 bool llvm::ARM::getFPUFeatures(unsigned FPUKind,
221                                std::vector<const char *> &Features) {
222
223   if (FPUKind >= ARM::FK_LAST || FPUKind == ARM::FK_INVALID)
224     return false;
225
226   // fp-only-sp and d16 subtarget features are independent of each other, so we
227   // must enable/disable both.
228   switch (FPUNames[FPUKind].Restriction) {
229   case ARM::FR_SP_D16:
230     Features.push_back("+fp-only-sp");
231     Features.push_back("+d16");
232     break;
233   case ARM::FR_D16:
234     Features.push_back("-fp-only-sp");
235     Features.push_back("+d16");
236     break;
237   case ARM::FR_None:
238     Features.push_back("-fp-only-sp");
239     Features.push_back("-d16");
240     break;
241   }
242
243   // FPU version subtarget features are inclusive of lower-numbered ones, so
244   // enable the one corresponding to this version and disable all that are
245   // higher. We also have to make sure to disable fp16 when vfp4 is disabled,
246   // as +vfp4 implies +fp16 but -vfp4 does not imply -fp16.
247   switch (FPUNames[FPUKind].FPUVersion) {
248   case ARM::FV_VFPV5:
249     Features.push_back("+fp-armv8");
250     break;
251   case ARM::FV_VFPV4:
252     Features.push_back("+vfp4");
253     Features.push_back("-fp-armv8");
254     break;
255   case ARM::FV_VFPV3_FP16:
256     Features.push_back("+vfp3");
257     Features.push_back("+fp16");
258     Features.push_back("-vfp4");
259     Features.push_back("-fp-armv8");
260     break;
261   case ARM::FV_VFPV3:
262     Features.push_back("+vfp3");
263     Features.push_back("-fp16");
264     Features.push_back("-vfp4");
265     Features.push_back("-fp-armv8");
266     break;
267   case ARM::FV_VFPV2:
268     Features.push_back("+vfp2");
269     Features.push_back("-vfp3");
270     Features.push_back("-fp16");
271     Features.push_back("-vfp4");
272     Features.push_back("-fp-armv8");
273     break;
274   case ARM::FV_NONE:
275     Features.push_back("-vfp2");
276     Features.push_back("-vfp3");
277     Features.push_back("-fp16");
278     Features.push_back("-vfp4");
279     Features.push_back("-fp-armv8");
280     break;
281   }
282
283   // crypto includes neon, so we handle this similarly to FPU version.
284   switch (FPUNames[FPUKind].NeonSupport) {
285   case ARM::NS_Crypto:
286     Features.push_back("+neon");
287     Features.push_back("+crypto");
288     break;
289   case ARM::NS_Neon:
290     Features.push_back("+neon");
291     Features.push_back("-crypto");
292     break;
293   case ARM::NS_None:
294     Features.push_back("-neon");
295     Features.push_back("-crypto");
296     break;
297   }
298
299   return true;
300 }
301
302 StringRef llvm::ARM::getArchName(unsigned ArchKind) {
303   if (ArchKind >= ARM::AK_LAST)
304     return StringRef();
305   return ARCHNames[ArchKind].getName();
306 }
307
308 StringRef llvm::ARM::getCPUAttr(unsigned ArchKind) {
309   if (ArchKind >= ARM::AK_LAST)
310     return StringRef();
311   return ARCHNames[ArchKind].getCPUAttr();
312 }
313
314 StringRef llvm::ARM::getSubArch(unsigned ArchKind) {
315   if (ArchKind >= ARM::AK_LAST)
316     return StringRef();
317   return ARCHNames[ArchKind].getSubArch();
318 }
319
320 unsigned llvm::ARM::getArchAttr(unsigned ArchKind) {
321   if (ArchKind >= ARM::AK_LAST)
322     return ARMBuildAttrs::CPUArch::Pre_v4;
323   return ARCHNames[ArchKind].ArchAttr;
324 }
325
326 StringRef llvm::ARM::getArchExtName(unsigned ArchExtKind) {
327   for (const auto AE : ARCHExtNames) {
328     if (ArchExtKind == AE.ID)
329       return AE.getName();
330   }
331   return StringRef();
332 }
333
334 const char *llvm::ARM::getArchExtFeature(StringRef ArchExt) {
335   for (const auto AE : ARCHExtNames) {
336     if (AE.Feature && ArchExt == AE.getName())
337       return AE.Feature;
338     else if (AE.NegFeature && ArchExt == AE.getNegName())
339       return AE.NegFeature;
340   }
341
342   return nullptr;
343 }
344
345 StringRef llvm::ARM::getHWDivName(unsigned HWDivKind) {
346   for (const auto D : HWDivNames) {
347     if (HWDivKind == D.ID)
348       return D.getName();
349   }
350   return StringRef();
351 }
352
353 StringRef llvm::ARM::getDefaultCPU(StringRef Arch) {
354   unsigned AK = parseArch(Arch);
355   if (AK == ARM::AK_INVALID)
356     return StringRef();
357
358   // Look for multiple AKs to find the default for pair AK+Name.
359   for (const auto CPU : CPUNames) {
360     if (CPU.ArchID == AK && CPU.Default)
361       return CPU.getName();
362   }
363
364   // If we can't find a default then target the architecture instead
365   return "generic";
366 }
367
368 // ======================================================= //
369 // Parsers
370 // ======================================================= //
371
372 static StringRef getHWDivSynonym(StringRef HWDiv) {
373   return StringSwitch<StringRef>(HWDiv)
374       .Case("thumb,arm", "arm,thumb")
375       .Default(HWDiv);
376 }
377
378 static StringRef getFPUSynonym(StringRef FPU) {
379   return StringSwitch<StringRef>(FPU)
380       .Cases("fpa", "fpe2", "fpe3", "maverick", "invalid") // Unsupported
381       .Case("vfp2", "vfpv2")
382       .Case("vfp3", "vfpv3")
383       .Case("vfp4", "vfpv4")
384       .Case("vfp3-d16", "vfpv3-d16")
385       .Case("vfp4-d16", "vfpv4-d16")
386       .Cases("fp4-sp-d16", "vfpv4-sp-d16", "fpv4-sp-d16")
387       .Cases("fp4-dp-d16", "fpv4-dp-d16", "vfpv4-d16")
388       .Case("fp5-sp-d16", "fpv5-sp-d16")
389       .Cases("fp5-dp-d16", "fpv5-dp-d16", "fpv5-d16")
390       // FIXME: Clang uses it, but it's bogus, since neon defaults to vfpv3.
391       .Case("neon-vfpv3", "neon")
392       .Default(FPU);
393 }
394
395 static StringRef getArchSynonym(StringRef Arch) {
396   return StringSwitch<StringRef>(Arch)
397       .Case("v5", "v5t")
398       .Case("v5e", "v5te")
399       .Case("v6hl", "v6k")
400       .Cases("v6m", "v6sm", "v6s-m", "v6-m")
401       .Cases("v6z", "v6zk", "v6kz")
402       .Cases("v7", "v7a", "v7hl", "v7l", "v7-a")
403       .Case("v7r", "v7-r")
404       .Case("v7m", "v7-m")
405       .Case("v7em", "v7e-m")
406       .Cases("v8", "v8a", "aarch64", "arm64", "v8-a")
407       .Case("v8.1a", "v8.1-a")
408       .Default(Arch);
409 }
410
411 // MArch is expected to be of the form (arm|thumb)?(eb)?(v.+)?(eb)?, but
412 // (iwmmxt|xscale)(eb)? is also permitted. If the former, return
413 // "v.+", if the latter, return unmodified string, minus 'eb'.
414 // If invalid, return empty string.
415 StringRef llvm::ARM::getCanonicalArchName(StringRef Arch) {
416   size_t offset = StringRef::npos;
417   StringRef A = Arch;
418   StringRef Error = "";
419
420   // Begins with "arm" / "thumb", move past it.
421   if (A.startswith("arm64"))
422     offset = 5;
423   else if (A.startswith("arm"))
424     offset = 3;
425   else if (A.startswith("thumb"))
426     offset = 5;
427   else if (A.startswith("aarch64")) {
428     offset = 7;
429     // AArch64 uses "_be", not "eb" suffix.
430     if (A.find("eb") != StringRef::npos)
431       return Error;
432     if (A.substr(offset, 3) == "_be")
433       offset += 3;
434   }
435
436   // Ex. "armebv7", move past the "eb".
437   if (offset != StringRef::npos && A.substr(offset, 2) == "eb")
438     offset += 2;
439   // Or, if it ends with eb ("armv7eb"), chop it off.
440   else if (A.endswith("eb"))
441     A = A.substr(0, A.size() - 2);
442   // Trim the head
443   if (offset != StringRef::npos)
444     A = A.substr(offset);
445
446   // Empty string means offset reached the end, which means it's valid.
447   if (A.empty())
448     return Arch;
449
450   // Only match non-marketing names
451   if (offset != StringRef::npos) {
452     // Must start with 'vN'.
453     if (A[0] != 'v' || !std::isdigit(A[1]))
454       return Error;
455     // Can't have an extra 'eb'.
456     if (A.find("eb") != StringRef::npos)
457       return Error;
458   }
459
460   // Arch will either be a 'v' name (v7a) or a marketing name (xscale).
461   return A;
462 }
463
464 unsigned llvm::ARM::parseHWDiv(StringRef HWDiv) {
465   StringRef Syn = getHWDivSynonym(HWDiv);
466   for (const auto D : HWDivNames) {
467     if (Syn == D.getName())
468       return D.ID;
469   }
470   return ARM::AEK_INVALID;
471 }
472
473 unsigned llvm::ARM::parseFPU(StringRef FPU) {
474   StringRef Syn = getFPUSynonym(FPU);
475   for (const auto F : FPUNames) {
476     if (Syn == F.getName())
477       return F.ID;
478   }
479   return ARM::FK_INVALID;
480 }
481
482 // Allows partial match, ex. "v7a" matches "armv7a".
483 unsigned llvm::ARM::parseArch(StringRef Arch) {
484   Arch = getCanonicalArchName(Arch);
485   StringRef Syn = getArchSynonym(Arch);
486   for (const auto A : ARCHNames) {
487     if (A.getName().endswith(Syn))
488       return A.ID;
489   }
490   return ARM::AK_INVALID;
491 }
492
493 unsigned llvm::ARM::parseArchExt(StringRef ArchExt) {
494   for (const auto A : ARCHExtNames) {
495     if (ArchExt == A.getName())
496       return A.ID;
497   }
498   return ARM::AEK_INVALID;
499 }
500
501 unsigned llvm::ARM::parseCPUArch(StringRef CPU) {
502   for (const auto C : CPUNames) {
503     if (CPU == C.getName())
504       return C.ArchID;
505   }
506   return ARM::AK_INVALID;
507 }
508
509 // ARM, Thumb, AArch64
510 unsigned llvm::ARM::parseArchISA(StringRef Arch) {
511   return StringSwitch<unsigned>(Arch)
512       .StartsWith("aarch64", ARM::IK_AARCH64)
513       .StartsWith("arm64", ARM::IK_AARCH64)
514       .StartsWith("thumb", ARM::IK_THUMB)
515       .StartsWith("arm", ARM::IK_ARM)
516       .Default(ARM::EK_INVALID);
517 }
518
519 // Little/Big endian
520 unsigned llvm::ARM::parseArchEndian(StringRef Arch) {
521   if (Arch.startswith("armeb") || Arch.startswith("thumbeb") ||
522       Arch.startswith("aarch64_be"))
523     return ARM::EK_BIG;
524
525   if (Arch.startswith("arm") || Arch.startswith("thumb")) {
526     if (Arch.endswith("eb"))
527       return ARM::EK_BIG;
528     else
529       return ARM::EK_LITTLE;
530   }
531
532   if (Arch.startswith("aarch64"))
533     return ARM::EK_LITTLE;
534
535   return ARM::EK_INVALID;
536 }
537
538 // Profile A/R/M
539 unsigned llvm::ARM::parseArchProfile(StringRef Arch) {
540   Arch = getCanonicalArchName(Arch);
541   switch (parseArch(Arch)) {
542   case ARM::AK_ARMV6M:
543   case ARM::AK_ARMV7M:
544   case ARM::AK_ARMV7EM:
545     return ARM::PK_M;
546   case ARM::AK_ARMV7R:
547     return ARM::PK_R;
548   case ARM::AK_ARMV7A:
549   case ARM::AK_ARMV7K:
550   case ARM::AK_ARMV8A:
551   case ARM::AK_ARMV8_1A:
552     return ARM::PK_A;
553   }
554   return ARM::PK_INVALID;
555 }
556
557 // Version number (ex. v7 = 7).
558 unsigned llvm::ARM::parseArchVersion(StringRef Arch) {
559   Arch = getCanonicalArchName(Arch);
560   switch (parseArch(Arch)) {
561   case ARM::AK_ARMV2:
562   case ARM::AK_ARMV2A:
563     return 2;
564   case ARM::AK_ARMV3:
565   case ARM::AK_ARMV3M:
566     return 3;
567   case ARM::AK_ARMV4:
568   case ARM::AK_ARMV4T:
569     return 4;
570   case ARM::AK_ARMV5T:
571   case ARM::AK_ARMV5TE:
572   case ARM::AK_IWMMXT:
573   case ARM::AK_IWMMXT2:
574   case ARM::AK_XSCALE:
575   case ARM::AK_ARMV5TEJ:
576     return 5;
577   case ARM::AK_ARMV6:
578   case ARM::AK_ARMV6J:
579   case ARM::AK_ARMV6K:
580   case ARM::AK_ARMV6T2:
581   case ARM::AK_ARMV6KZ:
582   case ARM::AK_ARMV6M:
583     return 6;
584   case ARM::AK_ARMV7A:
585   case ARM::AK_ARMV7R:
586   case ARM::AK_ARMV7M:
587   case ARM::AK_ARMV7S:
588   case ARM::AK_ARMV7EM:
589   case ARM::AK_ARMV7K:
590     return 7;
591   case ARM::AK_ARMV8A:
592   case ARM::AK_ARMV8_1A:
593     return 8;
594   }
595   return 0;
596 }