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