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