1f5f373fcSRenato Golin //===-- TargetParser - Parser for target features ---------------*- C++ -*-===//
2f5f373fcSRenato Golin //
3f5f373fcSRenato Golin //                     The LLVM Compiler Infrastructure
4f5f373fcSRenato Golin //
5f5f373fcSRenato Golin // This file is distributed under the University of Illinois Open Source
6f5f373fcSRenato Golin // License. See LICENSE.TXT for details.
7f5f373fcSRenato Golin //
8f5f373fcSRenato Golin //===----------------------------------------------------------------------===//
9f5f373fcSRenato Golin //
10f5f373fcSRenato Golin // This file implements a target parser to recognise hardware features such as
11f5f373fcSRenato Golin // FPU/CPU/ARCH names as well as specific support such as HDIV, etc.
12f5f373fcSRenato Golin //
13f5f373fcSRenato Golin //===----------------------------------------------------------------------===//
14f5f373fcSRenato Golin 
15f5f373fcSRenato Golin #include "llvm/Support/ARMBuildAttributes.h"
16f5f373fcSRenato Golin #include "llvm/Support/TargetParser.h"
17f5f373fcSRenato Golin #include "llvm/ADT/StringExtras.h"
18f5f373fcSRenato Golin #include "llvm/ADT/StringSwitch.h"
19ebdd12cbSRenato Golin #include <cctype>
20f5f373fcSRenato Golin 
21f5f373fcSRenato Golin using namespace llvm;
22f5f373fcSRenato Golin 
23f5f373fcSRenato Golin namespace {
24f5f373fcSRenato Golin 
25*d03d2292SJohn Brawn // List of canonical FPU names (use getFPUSynonym) and which architectural
26*d03d2292SJohn Brawn // features they correspond to (use getFPUFeatures).
27f5f373fcSRenato Golin // FIXME: TableGen this.
28f5f373fcSRenato Golin struct {
29f5f373fcSRenato Golin   const char * Name;
30f5f373fcSRenato Golin   ARM::FPUKind ID;
31*d03d2292SJohn Brawn   unsigned FPUVersion; //< Corresponds directly to the FP arch version number.
32*d03d2292SJohn Brawn   ARM::NeonSupportLevel NeonSupport;
33*d03d2292SJohn Brawn   ARM::FPURestriction Restriction;
34f5f373fcSRenato Golin } FPUNames[] = {
35*d03d2292SJohn Brawn   { "invalid",       ARM::FK_INVALID,       0, ARM::NS_None,   ARM::FR_None},
36*d03d2292SJohn Brawn   { "vfp",           ARM::FK_VFP,           2, ARM::NS_None,   ARM::FR_None},
37*d03d2292SJohn Brawn   { "vfpv2",         ARM::FK_VFPV2,         2, ARM::NS_None,   ARM::FR_None},
38*d03d2292SJohn Brawn   { "vfpv3",         ARM::FK_VFPV3,         3, ARM::NS_None,   ARM::FR_None},
39*d03d2292SJohn Brawn   { "vfpv3-d16",     ARM::FK_VFPV3_D16,     3, ARM::NS_None,   ARM::FR_D16},
40*d03d2292SJohn Brawn   { "vfpv4",         ARM::FK_VFPV4,         4, ARM::NS_None,   ARM::FR_None},
41*d03d2292SJohn Brawn   { "vfpv4-d16",     ARM::FK_VFPV4_D16,     4, ARM::NS_None,   ARM::FR_D16},
42*d03d2292SJohn Brawn   { "fpv5-d16",      ARM::FK_FPV5_D16,      5, ARM::NS_None,   ARM::FR_D16},
43*d03d2292SJohn Brawn   { "fp-armv8",      ARM::FK_FP_ARMV8,      5, ARM::NS_None,   ARM::FR_None},
44*d03d2292SJohn Brawn   { "neon",          ARM::FK_NEON,          3, ARM::NS_Neon,   ARM::FR_None},
45*d03d2292SJohn Brawn   { "neon-vfpv4",    ARM::FK_NEON_VFPV4,    4, ARM::NS_Neon,   ARM::FR_None},
46*d03d2292SJohn Brawn   { "neon-fp-armv8", ARM::FK_NEON_FP_ARMV8, 5, ARM::NS_Neon,   ARM::FR_None},
47*d03d2292SJohn Brawn   { "crypto-neon-fp-armv8",
48*d03d2292SJohn Brawn               ARM::FK_CRYPTO_NEON_FP_ARMV8, 5, ARM::NS_Crypto, ARM::FR_None},
49*d03d2292SJohn Brawn   { "softvfp",       ARM::FK_SOFTVFP,       0, ARM::NS_None,   ARM::FR_None},
50f5f373fcSRenato Golin };
51*d03d2292SJohn Brawn 
52f7c0d5f2SRenato Golin // List of canonical arch names (use getArchSynonym).
53f7c0d5f2SRenato Golin // This table also provides the build attribute fields for CPU arch
54f7c0d5f2SRenato Golin // and Arch ID, according to the Addenda to the ARM ABI, chapters
55f7c0d5f2SRenato Golin // 2.4 and 2.3.5.2 respectively.
5642dad647SRenato Golin // FIXME: SubArch values were simplified to fit into the expectations
5742dad647SRenato Golin // of the triples and are not conforming with their official names.
5842dad647SRenato Golin // Check to see if the expectation should be changed.
59f5f373fcSRenato Golin // FIXME: TableGen this.
60f5f373fcSRenato Golin struct {
61f5f373fcSRenato Golin   const char *Name;
62f5f373fcSRenato Golin   ARM::ArchKind ID;
63f7c0d5f2SRenato Golin   const char *CPUAttr; // CPU class in build attributes.
6442dad647SRenato Golin   const char *SubArch; // Sub-Arch name.
65f7c0d5f2SRenato Golin   ARMBuildAttrs::CPUArch ArchAttr; // Arch ID in build attributes.
66f5f373fcSRenato Golin } ARCHNames[] = {
6742dad647SRenato Golin   { "invalid",   ARM::AK_INVALID,  nullptr,   nullptr, ARMBuildAttrs::CPUArch::Pre_v4 },
6842dad647SRenato Golin   { "armv2",     ARM::AK_ARMV2,    "2",       "v2",    ARMBuildAttrs::CPUArch::Pre_v4 },
6942dad647SRenato Golin   { "armv2a",    ARM::AK_ARMV2A,   "2A",      "v2a",   ARMBuildAttrs::CPUArch::Pre_v4 },
7042dad647SRenato Golin   { "armv3",     ARM::AK_ARMV3,    "3",       "v3",    ARMBuildAttrs::CPUArch::Pre_v4 },
7142dad647SRenato Golin   { "armv3m",    ARM::AK_ARMV3M,   "3M",      "v3m",   ARMBuildAttrs::CPUArch::Pre_v4 },
7242dad647SRenato Golin   { "armv4",     ARM::AK_ARMV4,    "4",       "v4",    ARMBuildAttrs::CPUArch::v4 },
7342dad647SRenato Golin   { "armv4t",    ARM::AK_ARMV4T,   "4T",      "v4t",   ARMBuildAttrs::CPUArch::v4T },
7442dad647SRenato Golin   { "armv5t",    ARM::AK_ARMV5T,   "5T",      "v5",    ARMBuildAttrs::CPUArch::v5T },
7542dad647SRenato Golin   { "armv5te",   ARM::AK_ARMV5TE,  "5TE",     "v5e",   ARMBuildAttrs::CPUArch::v5TE },
7642dad647SRenato Golin   { "armv5tej",  ARM::AK_ARMV5TEJ, "5TEJ",    "v5e",   ARMBuildAttrs::CPUArch::v5TEJ },
7742dad647SRenato Golin   { "armv6",     ARM::AK_ARMV6,    "6",       "v6",    ARMBuildAttrs::CPUArch::v6 },
7842dad647SRenato Golin   { "armv6k",    ARM::AK_ARMV6K,   "6K",      "v6k",   ARMBuildAttrs::CPUArch::v6K },
7942dad647SRenato Golin   { "armv6t2",   ARM::AK_ARMV6T2,  "6T2",     "v6t2",  ARMBuildAttrs::CPUArch::v6T2 },
8042dad647SRenato Golin   { "armv6z",    ARM::AK_ARMV6Z,   "6Z",      "v6z",   ARMBuildAttrs::CPUArch::v6KZ },
8142dad647SRenato Golin   { "armv6zk",   ARM::AK_ARMV6ZK,  "6ZK",     "v6zk",  ARMBuildAttrs::CPUArch::v6KZ },
8242dad647SRenato Golin   { "armv6-m",   ARM::AK_ARMV6M,   "6-M",     "v6m",   ARMBuildAttrs::CPUArch::v6_M },
8342dad647SRenato Golin   { "armv6s-m",  ARM::AK_ARMV6SM,  "6S-M",    "v6sm",  ARMBuildAttrs::CPUArch::v6S_M },
8442dad647SRenato Golin   { "armv7-a",   ARM::AK_ARMV7A,   "7-A",     "v7",    ARMBuildAttrs::CPUArch::v7 },
8542dad647SRenato Golin   { "armv7-r",   ARM::AK_ARMV7R,   "7-R",     "v7r",   ARMBuildAttrs::CPUArch::v7 },
8642dad647SRenato Golin   { "armv7-m",   ARM::AK_ARMV7M,   "7-M",     "v7m",   ARMBuildAttrs::CPUArch::v7 },
8742dad647SRenato Golin   { "armv7e-m",  ARM::AK_ARMV7EM,  "7E-M",    "v7em",  ARMBuildAttrs::CPUArch::v7E_M },
8842dad647SRenato Golin   { "armv8-a",   ARM::AK_ARMV8A,   "8-A",     "v8",    ARMBuildAttrs::CPUArch::v8 },
8942dad647SRenato Golin   { "armv8.1-a", ARM::AK_ARMV8_1A, "8.1-A",   "v8.1a", ARMBuildAttrs::CPUArch::v8 },
90e8048f0dSRenato Golin   // Non-standard Arch names.
9142dad647SRenato Golin   { "iwmmxt",    ARM::AK_IWMMXT,   "iwmmxt",  "",      ARMBuildAttrs::CPUArch::v5TE },
9242dad647SRenato Golin   { "iwmmxt2",   ARM::AK_IWMMXT2,  "iwmmxt2", "",      ARMBuildAttrs::CPUArch::v5TE },
9342dad647SRenato Golin   { "xscale",    ARM::AK_XSCALE,   "xscale",  "",      ARMBuildAttrs::CPUArch::v5TE },
9442dad647SRenato Golin   { "armv5",     ARM::AK_ARMV5,    "5T",      "v5",    ARMBuildAttrs::CPUArch::v5T },
9542dad647SRenato Golin   { "armv5e",    ARM::AK_ARMV5E,   "5TE",     "v5e",   ARMBuildAttrs::CPUArch::v5TE },
9642dad647SRenato Golin   { "armv6j",    ARM::AK_ARMV6J,   "6J",      "v6",    ARMBuildAttrs::CPUArch::v6 },
9742dad647SRenato Golin   { "armv6hl",   ARM::AK_ARMV6HL,  "6-M",     "v6hl",  ARMBuildAttrs::CPUArch::v6_M },
9842dad647SRenato Golin   { "armv7",     ARM::AK_ARMV7,    "7",       "v7",    ARMBuildAttrs::CPUArch::v7 },
9942dad647SRenato Golin   { "armv7l",    ARM::AK_ARMV7L,   "7-L",     "v7l",   ARMBuildAttrs::CPUArch::v7 },
10042dad647SRenato Golin   { "armv7hl",   ARM::AK_ARMV7HL,  "7-L",     "v7hl",  ARMBuildAttrs::CPUArch::v7 },
10142dad647SRenato Golin   { "armv7s",    ARM::AK_ARMV7S,   "7-S",     "v7s",   ARMBuildAttrs::CPUArch::v7 }
102f5f373fcSRenato Golin };
103e1326cadSRenato Golin // List of Arch Extension names.
104f5f373fcSRenato Golin // FIXME: TableGen this.
105f5f373fcSRenato Golin struct {
106f5f373fcSRenato Golin   const char *Name;
107f5f373fcSRenato Golin   ARM::ArchExtKind ID;
108f5f373fcSRenato Golin } ARCHExtNames[] = {
10935de35d0SRenato Golin   { "invalid",  ARM::AEK_INVALID },
11035de35d0SRenato Golin   { "crc",      ARM::AEK_CRC },
11135de35d0SRenato Golin   { "crypto",   ARM::AEK_CRYPTO },
11235de35d0SRenato Golin   { "fp",       ARM::AEK_FP },
11335de35d0SRenato Golin   { "idiv",     ARM::AEK_HWDIV },
11435de35d0SRenato Golin   { "mp",       ARM::AEK_MP },
115230d2983SRenato Golin   { "simd",     ARM::AEK_SIMD },
11635de35d0SRenato Golin   { "sec",      ARM::AEK_SEC },
117230d2983SRenato Golin   { "virt",     ARM::AEK_VIRT },
118230d2983SRenato Golin   { "os",       ARM::AEK_OS },
119230d2983SRenato Golin   { "iwmmxt",   ARM::AEK_IWMMXT },
120230d2983SRenato Golin   { "iwmmxt2",  ARM::AEK_IWMMXT2 },
121230d2983SRenato Golin   { "maverick", ARM::AEK_MAVERICK },
122230d2983SRenato Golin   { "xscale",   ARM::AEK_XSCALE }
123f5f373fcSRenato Golin };
124e8048f0dSRenato Golin // List of CPU names and their arches.
125e8048f0dSRenato Golin // The same CPU can have multiple arches and can be default on multiple arches.
126e8048f0dSRenato Golin // When finding the Arch for a CPU, first-found prevails. Sort them accordingly.
1277374fcdbSRenato Golin // When this becomes table-generated, we'd probably need two tables.
128e8048f0dSRenato Golin // FIXME: TableGen this.
129e8048f0dSRenato Golin struct {
130e8048f0dSRenato Golin   const char *Name;
131e8048f0dSRenato Golin   ARM::ArchKind ArchID;
132e8048f0dSRenato Golin   bool Default;
133e8048f0dSRenato Golin } CPUNames[] = {
134e8048f0dSRenato Golin   { "arm2",          ARM::AK_ARMV2,    true },
1357374fcdbSRenato Golin   { "arm3",          ARM::AK_ARMV2A,   true },
136e8048f0dSRenato Golin   { "arm6",          ARM::AK_ARMV3,    true },
137e8048f0dSRenato Golin   { "arm7m",         ARM::AK_ARMV3M,   true },
1387374fcdbSRenato Golin   { "arm8",          ARM::AK_ARMV4,    false },
1397374fcdbSRenato Golin   { "arm810",        ARM::AK_ARMV4,    false },
140e8048f0dSRenato Golin   { "strongarm",     ARM::AK_ARMV4,    true },
1417374fcdbSRenato Golin   { "strongarm110",  ARM::AK_ARMV4,    false },
1427374fcdbSRenato Golin   { "strongarm1100", ARM::AK_ARMV4,    false },
1437374fcdbSRenato Golin   { "strongarm1110", ARM::AK_ARMV4,    false },
144e8048f0dSRenato Golin   { "arm7tdmi",      ARM::AK_ARMV4T,   true },
145e8048f0dSRenato Golin   { "arm7tdmi-s",    ARM::AK_ARMV4T,   false },
146e8048f0dSRenato Golin   { "arm710t",       ARM::AK_ARMV4T,   false },
147e8048f0dSRenato Golin   { "arm720t",       ARM::AK_ARMV4T,   false },
148e8048f0dSRenato Golin   { "arm9",          ARM::AK_ARMV4T,   false },
149e8048f0dSRenato Golin   { "arm9tdmi",      ARM::AK_ARMV4T,   false },
150e8048f0dSRenato Golin   { "arm920",        ARM::AK_ARMV4T,   false },
151e8048f0dSRenato Golin   { "arm920t",       ARM::AK_ARMV4T,   false },
152e8048f0dSRenato Golin   { "arm922t",       ARM::AK_ARMV4T,   false },
153e8048f0dSRenato Golin   { "arm9312",       ARM::AK_ARMV4T,   false },
154e8048f0dSRenato Golin   { "arm940t",       ARM::AK_ARMV4T,   false },
155e8048f0dSRenato Golin   { "ep9312",        ARM::AK_ARMV4T,   false },
156e8048f0dSRenato Golin   { "arm10tdmi",     ARM::AK_ARMV5T,   true },
157e8048f0dSRenato Golin   { "arm1020t",      ARM::AK_ARMV5T,   false },
158e8048f0dSRenato Golin   { "arm9e",         ARM::AK_ARMV5TE,  false },
1597374fcdbSRenato Golin   { "arm946e-s",     ARM::AK_ARMV5TE,  false },
160e8048f0dSRenato Golin   { "arm966e-s",     ARM::AK_ARMV5TE,  false },
161e8048f0dSRenato Golin   { "arm968e-s",     ARM::AK_ARMV5TE,  false },
1627374fcdbSRenato Golin   { "arm10e",        ARM::AK_ARMV5TE,  false },
163e8048f0dSRenato Golin   { "arm1020e",      ARM::AK_ARMV5TE,  false },
164e8048f0dSRenato Golin   { "arm1022e",      ARM::AK_ARMV5TE,  true },
165e8048f0dSRenato Golin   { "iwmmxt",        ARM::AK_ARMV5TE,  false },
1667374fcdbSRenato Golin   { "xscale",        ARM::AK_ARMV5TE,  false },
1677374fcdbSRenato Golin   { "arm926ej-s",    ARM::AK_ARMV5TEJ, true },
168e8048f0dSRenato Golin   { "arm1136jf-s",   ARM::AK_ARMV6,    true },
169e8048f0dSRenato Golin   { "arm1176j-s",    ARM::AK_ARMV6K,   false },
1707374fcdbSRenato Golin   { "arm1176jz-s",   ARM::AK_ARMV6K,   false },
171e8048f0dSRenato Golin   { "mpcore",        ARM::AK_ARMV6K,   false },
172e8048f0dSRenato Golin   { "mpcorenovfp",   ARM::AK_ARMV6K,   false },
173e8048f0dSRenato Golin   { "arm1176jzf-s",  ARM::AK_ARMV6K,   true },
174e8048f0dSRenato Golin   { "arm1176jzf-s",  ARM::AK_ARMV6Z,   true },
175e8048f0dSRenato Golin   { "arm1176jzf-s",  ARM::AK_ARMV6ZK,  true },
176e8048f0dSRenato Golin   { "arm1156t2-s",   ARM::AK_ARMV6T2,  true },
177e8048f0dSRenato Golin   { "arm1156t2f-s",  ARM::AK_ARMV6T2,  false },
178e8048f0dSRenato Golin   { "cortex-m0",     ARM::AK_ARMV6M,   true },
179e8048f0dSRenato Golin   { "cortex-m0plus", ARM::AK_ARMV6M,   false },
180e8048f0dSRenato Golin   { "cortex-m1",     ARM::AK_ARMV6M,   false },
181e8048f0dSRenato Golin   { "sc000",         ARM::AK_ARMV6M,   false },
182e8048f0dSRenato Golin   { "cortex-a5",     ARM::AK_ARMV7A,   false },
183e8048f0dSRenato Golin   { "cortex-a7",     ARM::AK_ARMV7A,   false },
184e8048f0dSRenato Golin   { "cortex-a8",     ARM::AK_ARMV7A,   true },
185e8048f0dSRenato Golin   { "cortex-a9",     ARM::AK_ARMV7A,   false },
186e8048f0dSRenato Golin   { "cortex-a12",    ARM::AK_ARMV7A,   false },
187e8048f0dSRenato Golin   { "cortex-a15",    ARM::AK_ARMV7A,   false },
188e8048f0dSRenato Golin   { "cortex-a17",    ARM::AK_ARMV7A,   false },
189e8048f0dSRenato Golin   { "krait",         ARM::AK_ARMV7A,   false },
190e8048f0dSRenato Golin   { "cortex-r4",     ARM::AK_ARMV7R,   true },
191e8048f0dSRenato Golin   { "cortex-r4f",    ARM::AK_ARMV7R,   false },
192e8048f0dSRenato Golin   { "cortex-r5",     ARM::AK_ARMV7R,   false },
193e8048f0dSRenato Golin   { "cortex-r7",     ARM::AK_ARMV7R,   false },
194e8048f0dSRenato Golin   { "sc300",         ARM::AK_ARMV7M,   false },
195e8048f0dSRenato Golin   { "cortex-m3",     ARM::AK_ARMV7M,   true },
1967374fcdbSRenato Golin   { "cortex-m4",     ARM::AK_ARMV7EM,  true },
1977374fcdbSRenato Golin   { "cortex-m7",     ARM::AK_ARMV7EM,  false },
198e8048f0dSRenato Golin   { "cortex-a53",    ARM::AK_ARMV8A,   true },
199e8048f0dSRenato Golin   { "cortex-a57",    ARM::AK_ARMV8A,   false },
200e8048f0dSRenato Golin   { "cortex-a72",    ARM::AK_ARMV8A,   false },
201e8048f0dSRenato Golin   { "cyclone",       ARM::AK_ARMV8A,   false },
202e8048f0dSRenato Golin   { "generic",       ARM::AK_ARMV8_1A, true },
203e8048f0dSRenato Golin   // Non-standard Arch names.
2047374fcdbSRenato Golin   { "iwmmxt",        ARM::AK_IWMMXT,   true },
2057374fcdbSRenato Golin   { "xscale",        ARM::AK_XSCALE,   true },
2067374fcdbSRenato Golin   { "arm10tdmi",     ARM::AK_ARMV5,    true },
207e8048f0dSRenato Golin   { "arm1022e",      ARM::AK_ARMV5E,   true },
2087374fcdbSRenato Golin   { "arm1136j-s",    ARM::AK_ARMV6J,   true },
2097374fcdbSRenato Golin   { "arm1136jz-s",   ARM::AK_ARMV6J,   false },
210e8048f0dSRenato Golin   { "cortex-m0",     ARM::AK_ARMV6SM,  true },
211b6b9e056SRenato Golin   { "arm1176jzf-s",  ARM::AK_ARMV6HL,  true },
2127374fcdbSRenato Golin   { "cortex-a8",     ARM::AK_ARMV7,    true },
213e8048f0dSRenato Golin   { "cortex-a8",     ARM::AK_ARMV7L,   true },
214b6b9e056SRenato Golin   { "cortex-a8",     ARM::AK_ARMV7HL,  true },
215e8048f0dSRenato Golin   { "cortex-m4",     ARM::AK_ARMV7EM,  true },
216e8048f0dSRenato Golin   { "swift",         ARM::AK_ARMV7S,   true },
217e8048f0dSRenato Golin   // Invalid CPU
218e8048f0dSRenato Golin   { "invalid",       ARM::AK_INVALID,  true }
219e8048f0dSRenato Golin };
220f5f373fcSRenato Golin 
221f5f373fcSRenato Golin } // namespace
222f5f373fcSRenato Golin 
223f5f373fcSRenato Golin namespace llvm {
224f5f373fcSRenato Golin 
225f5f373fcSRenato Golin // ======================================================= //
226f5f373fcSRenato Golin // Information by ID
227f5f373fcSRenato Golin // ======================================================= //
228f5f373fcSRenato Golin 
229e8048f0dSRenato Golin const char *ARMTargetParser::getFPUName(unsigned FPUKind) {
230e8048f0dSRenato Golin   if (FPUKind >= ARM::FK_LAST)
231f5f373fcSRenato Golin     return nullptr;
232e8048f0dSRenato Golin   return FPUNames[FPUKind].Name;
233f5f373fcSRenato Golin }
234f5f373fcSRenato Golin 
235*d03d2292SJohn Brawn unsigned ARMTargetParser::getFPUVersion(unsigned FPUKind) {
236*d03d2292SJohn Brawn   if (FPUKind >= ARM::FK_LAST)
237*d03d2292SJohn Brawn     return 0;
238*d03d2292SJohn Brawn   return FPUNames[FPUKind].FPUVersion;
239*d03d2292SJohn Brawn }
240*d03d2292SJohn Brawn 
241*d03d2292SJohn Brawn unsigned getFPUNeonSupportLevel(unsigned FPUKind) {
242*d03d2292SJohn Brawn   if (FPUKind >= ARM::FK_LAST)
243*d03d2292SJohn Brawn     return 0;
244*d03d2292SJohn Brawn   return FPUNames[FPUKind].NeonSupport;
245*d03d2292SJohn Brawn }
246*d03d2292SJohn Brawn 
247*d03d2292SJohn Brawn unsigned getFPURestriction(unsigned FPUKind) {
248*d03d2292SJohn Brawn   if (FPUKind >= ARM::FK_LAST)
249*d03d2292SJohn Brawn     return 0;
250*d03d2292SJohn Brawn   return FPUNames[FPUKind].Restriction;
251*d03d2292SJohn Brawn }
252*d03d2292SJohn Brawn 
253*d03d2292SJohn Brawn bool ARMTargetParser::getFPUFeatures(unsigned FPUKind,
254*d03d2292SJohn Brawn                                      std::vector<const char *> &Features) {
255*d03d2292SJohn Brawn 
256*d03d2292SJohn Brawn   if (FPUKind >= ARM::FK_LAST || FPUKind == ARM::FK_INVALID)
257*d03d2292SJohn Brawn     return false;
258*d03d2292SJohn Brawn 
259*d03d2292SJohn Brawn   // fp-only-sp and d16 subtarget features are independent of each other, so we
260*d03d2292SJohn Brawn   // must enable/disable both.
261*d03d2292SJohn Brawn   switch (FPUNames[FPUKind].Restriction) {
262*d03d2292SJohn Brawn   case ARM::FR_SP_D16:
263*d03d2292SJohn Brawn     Features.push_back("+fp-only-sp");
264*d03d2292SJohn Brawn     Features.push_back("+d16");
265*d03d2292SJohn Brawn     break;
266*d03d2292SJohn Brawn   case ARM::FR_D16:
267*d03d2292SJohn Brawn     Features.push_back("-fp-only-sp");
268*d03d2292SJohn Brawn     Features.push_back("+d16");
269*d03d2292SJohn Brawn     break;
270*d03d2292SJohn Brawn   case ARM::FR_None:
271*d03d2292SJohn Brawn     Features.push_back("-fp-only-sp");
272*d03d2292SJohn Brawn     Features.push_back("-d16");
273*d03d2292SJohn Brawn     break;
274*d03d2292SJohn Brawn   }
275*d03d2292SJohn Brawn 
276*d03d2292SJohn Brawn   // FPU version subtarget features are inclusive of lower-numbered ones, so
277*d03d2292SJohn Brawn   // enable the one corresponding to this version and disable all that are
278*d03d2292SJohn Brawn   // higher.
279*d03d2292SJohn Brawn   switch (FPUNames[FPUKind].FPUVersion) {
280*d03d2292SJohn Brawn   case 5:
281*d03d2292SJohn Brawn     Features.push_back("+fp-armv8");
282*d03d2292SJohn Brawn     break;
283*d03d2292SJohn Brawn   case 4:
284*d03d2292SJohn Brawn     Features.push_back("+vfp4");
285*d03d2292SJohn Brawn     Features.push_back("-fp-armv8");
286*d03d2292SJohn Brawn     break;
287*d03d2292SJohn Brawn   case 3:
288*d03d2292SJohn Brawn     Features.push_back("+vfp3");
289*d03d2292SJohn Brawn     Features.push_back("-vfp4");
290*d03d2292SJohn Brawn     Features.push_back("-fp-armv8");
291*d03d2292SJohn Brawn     break;
292*d03d2292SJohn Brawn   case 2:
293*d03d2292SJohn Brawn     Features.push_back("+vfp2");
294*d03d2292SJohn Brawn     Features.push_back("-vfp3");
295*d03d2292SJohn Brawn     Features.push_back("-vfp4");
296*d03d2292SJohn Brawn     Features.push_back("-fp-armv8");
297*d03d2292SJohn Brawn     break;
298*d03d2292SJohn Brawn   case 0:
299*d03d2292SJohn Brawn     Features.push_back("-vfp2");
300*d03d2292SJohn Brawn     Features.push_back("-vfp3");
301*d03d2292SJohn Brawn     Features.push_back("-vfp4");
302*d03d2292SJohn Brawn     Features.push_back("-fp-armv8");
303*d03d2292SJohn Brawn     break;
304*d03d2292SJohn Brawn   }
305*d03d2292SJohn Brawn 
306*d03d2292SJohn Brawn   // crypto includes neon, so we handle this similarly to FPU version.
307*d03d2292SJohn Brawn   switch (FPUNames[FPUKind].NeonSupport) {
308*d03d2292SJohn Brawn   case ARM::NS_Crypto:
309*d03d2292SJohn Brawn     Features.push_back("+crypto");
310*d03d2292SJohn Brawn     break;
311*d03d2292SJohn Brawn   case ARM::NS_Neon:
312*d03d2292SJohn Brawn     Features.push_back("+neon");
313*d03d2292SJohn Brawn     Features.push_back("-crypto");
314*d03d2292SJohn Brawn     break;
315*d03d2292SJohn Brawn   case ARM::NS_None:
316*d03d2292SJohn Brawn     Features.push_back("-neon");
317*d03d2292SJohn Brawn     Features.push_back("-crypto");
318*d03d2292SJohn Brawn     break;
319*d03d2292SJohn Brawn   }
320*d03d2292SJohn Brawn 
321*d03d2292SJohn Brawn   return true;
322*d03d2292SJohn Brawn }
323*d03d2292SJohn Brawn 
324e8048f0dSRenato Golin const char *ARMTargetParser::getArchName(unsigned ArchKind) {
325e8048f0dSRenato Golin   if (ArchKind >= ARM::AK_LAST)
326f5f373fcSRenato Golin     return nullptr;
327e8048f0dSRenato Golin   return ARCHNames[ArchKind].Name;
328f5f373fcSRenato Golin }
329f5f373fcSRenato Golin 
330f7c0d5f2SRenato Golin const char *ARMTargetParser::getCPUAttr(unsigned ArchKind) {
331e8048f0dSRenato Golin   if (ArchKind >= ARM::AK_LAST)
332f5f373fcSRenato Golin     return nullptr;
333f7c0d5f2SRenato Golin   return ARCHNames[ArchKind].CPUAttr;
334f5f373fcSRenato Golin }
335f5f373fcSRenato Golin 
33642dad647SRenato Golin const char *ARMTargetParser::getSubArch(unsigned ArchKind) {
33742dad647SRenato Golin   if (ArchKind >= ARM::AK_LAST)
33842dad647SRenato Golin     return nullptr;
33942dad647SRenato Golin   return ARCHNames[ArchKind].SubArch;
34042dad647SRenato Golin }
34142dad647SRenato Golin 
342f7c0d5f2SRenato Golin unsigned ARMTargetParser::getArchAttr(unsigned ArchKind) {
343e8048f0dSRenato Golin   if (ArchKind >= ARM::AK_LAST)
344e8048f0dSRenato Golin     return ARMBuildAttrs::CPUArch::Pre_v4;
345f7c0d5f2SRenato Golin   return ARCHNames[ArchKind].ArchAttr;
346f5f373fcSRenato Golin }
347f5f373fcSRenato Golin 
348e8048f0dSRenato Golin const char *ARMTargetParser::getArchExtName(unsigned ArchExtKind) {
349e8048f0dSRenato Golin   if (ArchExtKind >= ARM::AEK_LAST)
350f5f373fcSRenato Golin     return nullptr;
351e8048f0dSRenato Golin   return ARCHExtNames[ArchExtKind].Name;
352e8048f0dSRenato Golin }
353e8048f0dSRenato Golin 
354e8048f0dSRenato Golin const char *ARMTargetParser::getDefaultCPU(StringRef Arch) {
355e8048f0dSRenato Golin   unsigned AK = parseArch(Arch);
356e8048f0dSRenato Golin   if (AK == ARM::AK_INVALID)
357e8048f0dSRenato Golin     return nullptr;
358e8048f0dSRenato Golin 
359e8048f0dSRenato Golin   // Look for multiple AKs to find the default for pair AK+Name.
360e8048f0dSRenato Golin   for (const auto CPU : CPUNames) {
361e8048f0dSRenato Golin     if (CPU.ArchID == AK && CPU.Default)
362e8048f0dSRenato Golin       return CPU.Name;
363e8048f0dSRenato Golin   }
364e8048f0dSRenato Golin   return nullptr;
365f5f373fcSRenato Golin }
366f5f373fcSRenato Golin 
367f5f373fcSRenato Golin // ======================================================= //
368f5f373fcSRenato Golin // Parsers
369f5f373fcSRenato Golin // ======================================================= //
370f5f373fcSRenato Golin 
371f5f373fcSRenato Golin StringRef ARMTargetParser::getFPUSynonym(StringRef FPU) {
372f5f373fcSRenato Golin   return StringSwitch<StringRef>(FPU)
373f5f373fcSRenato Golin     .Cases("fpa", "fpe2", "fpe3", "maverick", "invalid") // Unsupported
374f5f373fcSRenato Golin     .Case("vfp2", "vfpv2")
375f5f373fcSRenato Golin     .Case("vfp3", "vfpv3")
376f5f373fcSRenato Golin     .Case("vfp4", "vfpv4")
377f5f373fcSRenato Golin     .Case("vfp3-d16", "vfpv3-d16")
378f5f373fcSRenato Golin     .Case("vfp4-d16", "vfpv4-d16")
379f5f373fcSRenato Golin     // FIXME: sp-16 is NOT the same as d16
380f5f373fcSRenato Golin     .Cases("fp4-sp-d16", "fpv4-sp-d16", "vfpv4-d16")
381f5f373fcSRenato Golin     .Cases("fp4-dp-d16", "fpv4-dp-d16", "vfpv4-d16")
382f5f373fcSRenato Golin     .Cases("fp5-sp-d16", "fpv5-sp-d16", "fpv5-d16")
383f5f373fcSRenato Golin     .Cases("fp5-dp-d16", "fpv5-dp-d16", "fpv5-d16")
384f5f373fcSRenato Golin     // FIXME: Clang uses it, but it's bogus, since neon defaults to vfpv3.
385f5f373fcSRenato Golin     .Case("neon-vfpv3", "neon")
386f5f373fcSRenato Golin     .Default(FPU);
387f5f373fcSRenato Golin }
388f5f373fcSRenato Golin 
389f5f373fcSRenato Golin StringRef ARMTargetParser::getArchSynonym(StringRef Arch) {
390f5f373fcSRenato Golin   return StringSwitch<StringRef>(Arch)
39185aebc8cSArtyom Skrobov     .Case("v6sm", "v6s-m")
39285aebc8cSArtyom Skrobov     .Case("v6m", "v6-m")
39385aebc8cSArtyom Skrobov     .Case("v7a", "v7-a")
39485aebc8cSArtyom Skrobov     .Case("v7r", "v7-r")
39585aebc8cSArtyom Skrobov     .Case("v7m", "v7-m")
39685aebc8cSArtyom Skrobov     .Case("v7em", "v7e-m")
397acd1cd66SArtyom Skrobov     .Cases("v8", "v8a", "aarch64", "arm64", "v8-a")
39885aebc8cSArtyom Skrobov     .Case("v8.1a", "v8.1-a")
399f5f373fcSRenato Golin     .Default(Arch);
400f5f373fcSRenato Golin }
401f5f373fcSRenato Golin 
402e8048f0dSRenato Golin // MArch is expected to be of the form (arm|thumb)?(eb)?(v.+)?(eb)?, but
403e8048f0dSRenato Golin // (iwmmxt|xscale)(eb)? is also permitted. If the former, return
404ebdd12cbSRenato Golin // "v.+", if the latter, return unmodified string, minus 'eb'.
405ebdd12cbSRenato Golin // If invalid, return empty string.
406e8048f0dSRenato Golin StringRef ARMTargetParser::getCanonicalArchName(StringRef Arch) {
407e8048f0dSRenato Golin   size_t offset = StringRef::npos;
408e8048f0dSRenato Golin   StringRef A = Arch;
409b6b9e056SRenato Golin   StringRef Error = "";
410e8048f0dSRenato Golin 
411e8048f0dSRenato Golin   // Begins with "arm" / "thumb", move past it.
412ebdd12cbSRenato Golin   if (A.startswith("arm64"))
413ebdd12cbSRenato Golin     offset = 5;
414ebdd12cbSRenato Golin   else if (A.startswith("arm"))
415e8048f0dSRenato Golin     offset = 3;
416e8048f0dSRenato Golin   else if (A.startswith("thumb"))
417e8048f0dSRenato Golin     offset = 5;
418b6b9e056SRenato Golin   else if (A.startswith("aarch64")) {
419b6b9e056SRenato Golin     offset = 7;
420b6b9e056SRenato Golin     // AArch64 uses "_be", not "eb" suffix.
421b6b9e056SRenato Golin     if (A.find("eb") != StringRef::npos)
422b6b9e056SRenato Golin       return Error;
423b6b9e056SRenato Golin     if (A.substr(offset,3) == "_be")
424b6b9e056SRenato Golin       offset += 3;
425b6b9e056SRenato Golin   }
426b6b9e056SRenato Golin 
427e8048f0dSRenato Golin   // Ex. "armebv7", move past the "eb".
428e8048f0dSRenato Golin   if (offset != StringRef::npos && A.substr(offset, 2) == "eb")
429e8048f0dSRenato Golin     offset += 2;
430e8048f0dSRenato Golin   // Or, if it ends with eb ("armv7eb"), chop it off.
431e8048f0dSRenato Golin   else if (A.endswith("eb"))
432e8048f0dSRenato Golin     A = A.substr(0, A.size() - 2);
433ebdd12cbSRenato Golin   // Trim the head
434ebdd12cbSRenato Golin   if (offset != StringRef::npos)
435e8048f0dSRenato Golin     A = A.substr(offset);
436e8048f0dSRenato Golin 
437ebdd12cbSRenato Golin   // Empty string means offset reached the end, which means it's valid.
438e8048f0dSRenato Golin   if (A.empty())
439e8048f0dSRenato Golin     return Arch;
440e8048f0dSRenato Golin 
441ebdd12cbSRenato Golin   // Only match non-marketing names
442ebdd12cbSRenato Golin   if (offset != StringRef::npos) {
443ebdd12cbSRenato Golin   // Must start with 'vN'.
444ebdd12cbSRenato Golin     if (A[0] != 'v' || !std::isdigit(A[1]))
445b6b9e056SRenato Golin       return Error;
446ebdd12cbSRenato Golin     // Can't have an extra 'eb'.
447ebdd12cbSRenato Golin     if (A.find("eb") != StringRef::npos)
448ebdd12cbSRenato Golin       return Error;
449ebdd12cbSRenato Golin   }
450e8048f0dSRenato Golin 
451ebdd12cbSRenato Golin   // Arch will either be a 'v' name (v7a) or a marketing name (xscale).
452e8048f0dSRenato Golin   return A;
453e8048f0dSRenato Golin }
454e8048f0dSRenato Golin 
455f5f373fcSRenato Golin unsigned ARMTargetParser::parseFPU(StringRef FPU) {
456f5f373fcSRenato Golin   StringRef Syn = getFPUSynonym(FPU);
457f5f373fcSRenato Golin   for (const auto F : FPUNames) {
458f5f373fcSRenato Golin     if (Syn == F.Name)
459f5f373fcSRenato Golin       return F.ID;
460f5f373fcSRenato Golin   }
46135de35d0SRenato Golin   return ARM::FK_INVALID;
462f5f373fcSRenato Golin }
463f5f373fcSRenato Golin 
464e8048f0dSRenato Golin // Allows partial match, ex. "v7a" matches "armv7a".
465f5f373fcSRenato Golin unsigned ARMTargetParser::parseArch(StringRef Arch) {
46685aebc8cSArtyom Skrobov   Arch = getCanonicalArchName(Arch);
467f5f373fcSRenato Golin   StringRef Syn = getArchSynonym(Arch);
468f5f373fcSRenato Golin   for (const auto A : ARCHNames) {
469e8048f0dSRenato Golin     if (StringRef(A.Name).endswith(Syn))
470f5f373fcSRenato Golin       return A.ID;
471f5f373fcSRenato Golin   }
47235de35d0SRenato Golin   return ARM::AK_INVALID;
473f5f373fcSRenato Golin }
474f5f373fcSRenato Golin 
475f5f373fcSRenato Golin unsigned ARMTargetParser::parseArchExt(StringRef ArchExt) {
476f5f373fcSRenato Golin   for (const auto A : ARCHExtNames) {
477f5f373fcSRenato Golin     if (ArchExt == A.Name)
478f5f373fcSRenato Golin       return A.ID;
479f5f373fcSRenato Golin   }
48035de35d0SRenato Golin   return ARM::AEK_INVALID;
481f5f373fcSRenato Golin }
482f5f373fcSRenato Golin 
483e8048f0dSRenato Golin unsigned ARMTargetParser::parseCPUArch(StringRef CPU) {
484e8048f0dSRenato Golin   for (const auto C : CPUNames) {
485e8048f0dSRenato Golin     if (CPU == C.Name)
486e8048f0dSRenato Golin       return C.ArchID;
487e8048f0dSRenato Golin   }
488e8048f0dSRenato Golin   return ARM::AK_INVALID;
489e8048f0dSRenato Golin }
490e8048f0dSRenato Golin 
491b6b9e056SRenato Golin // ARM, Thumb, AArch64
492b6b9e056SRenato Golin unsigned ARMTargetParser::parseArchISA(StringRef Arch) {
493b6b9e056SRenato Golin   return StringSwitch<unsigned>(Arch)
494b6b9e056SRenato Golin       .StartsWith("aarch64", ARM::IK_AARCH64)
495b6b9e056SRenato Golin       .StartsWith("arm64",   ARM::IK_AARCH64)
496b6b9e056SRenato Golin       .StartsWith("thumb",   ARM::IK_THUMB)
497b6b9e056SRenato Golin       .StartsWith("arm",     ARM::IK_ARM)
498b6b9e056SRenato Golin       .Default(ARM::EK_INVALID);
499b6b9e056SRenato Golin }
500b6b9e056SRenato Golin 
501b6b9e056SRenato Golin // Little/Big endian
502b6b9e056SRenato Golin unsigned ARMTargetParser::parseArchEndian(StringRef Arch) {
503b6b9e056SRenato Golin   if (Arch.startswith("armeb") ||
504b6b9e056SRenato Golin       Arch.startswith("thumbeb") ||
505b6b9e056SRenato Golin       Arch.startswith("aarch64_be"))
506b6b9e056SRenato Golin     return ARM::EK_BIG;
507b6b9e056SRenato Golin 
508b6b9e056SRenato Golin   if (Arch.startswith("arm") || Arch.startswith("thumb")) {
509b6b9e056SRenato Golin     if (Arch.endswith("eb"))
510b6b9e056SRenato Golin       return ARM::EK_BIG;
511b6b9e056SRenato Golin     else
512b6b9e056SRenato Golin       return ARM::EK_LITTLE;
513b6b9e056SRenato Golin   }
514b6b9e056SRenato Golin 
515b6b9e056SRenato Golin   if (Arch.startswith("aarch64"))
516b6b9e056SRenato Golin     return ARM::EK_LITTLE;
517b6b9e056SRenato Golin 
518b6b9e056SRenato Golin   return ARM::EK_INVALID;
519b6b9e056SRenato Golin }
520b6b9e056SRenato Golin 
521fadc2108SRenato Golin // Profile A/R/M
522fadc2108SRenato Golin unsigned ARMTargetParser::parseArchProfile(StringRef Arch) {
523fadc2108SRenato Golin   Arch = getCanonicalArchName(Arch);
524fadc2108SRenato Golin   switch(parseArch(Arch)) {
525fadc2108SRenato Golin   case ARM::AK_ARMV6M:
526fadc2108SRenato Golin   case ARM::AK_ARMV7M:
527fadc2108SRenato Golin   case ARM::AK_ARMV6SM:
528fadc2108SRenato Golin   case ARM::AK_ARMV7EM:
529fadc2108SRenato Golin     return ARM::PK_M;
530fadc2108SRenato Golin   case ARM::AK_ARMV7R:
531fadc2108SRenato Golin     return ARM::PK_R;
532fadc2108SRenato Golin   case ARM::AK_ARMV7:
533fadc2108SRenato Golin   case ARM::AK_ARMV7A:
534fadc2108SRenato Golin   case ARM::AK_ARMV8A:
535fadc2108SRenato Golin   case ARM::AK_ARMV8_1A:
536fadc2108SRenato Golin     return ARM::PK_A;
537fadc2108SRenato Golin   }
538fadc2108SRenato Golin   return ARM::PK_INVALID;
539fadc2108SRenato Golin }
540fadc2108SRenato Golin 
541ebdd12cbSRenato Golin // Version number (ex. v7 = 7).
542fadc2108SRenato Golin unsigned ARMTargetParser::parseArchVersion(StringRef Arch) {
543fadc2108SRenato Golin   Arch = getCanonicalArchName(Arch);
544fadc2108SRenato Golin   switch(parseArch(Arch)) {
545fadc2108SRenato Golin   case ARM::AK_ARMV2:
546fadc2108SRenato Golin   case ARM::AK_ARMV2A:
547fadc2108SRenato Golin     return 2;
548fadc2108SRenato Golin   case ARM::AK_ARMV3:
549fadc2108SRenato Golin   case ARM::AK_ARMV3M:
550fadc2108SRenato Golin     return 3;
551fadc2108SRenato Golin   case ARM::AK_ARMV4:
552fadc2108SRenato Golin   case ARM::AK_ARMV4T:
553fadc2108SRenato Golin     return 4;
554fadc2108SRenato Golin   case ARM::AK_ARMV5:
555fadc2108SRenato Golin   case ARM::AK_ARMV5T:
556fadc2108SRenato Golin   case ARM::AK_ARMV5TE:
557fadc2108SRenato Golin   case ARM::AK_IWMMXT:
558fadc2108SRenato Golin   case ARM::AK_IWMMXT2:
559fadc2108SRenato Golin   case ARM::AK_XSCALE:
560fadc2108SRenato Golin   case ARM::AK_ARMV5E:
561fadc2108SRenato Golin   case ARM::AK_ARMV5TEJ:
562fadc2108SRenato Golin     return 5;
563fadc2108SRenato Golin   case ARM::AK_ARMV6:
564fadc2108SRenato Golin   case ARM::AK_ARMV6J:
565fadc2108SRenato Golin   case ARM::AK_ARMV6K:
566fadc2108SRenato Golin   case ARM::AK_ARMV6T2:
567fadc2108SRenato Golin   case ARM::AK_ARMV6Z:
568fadc2108SRenato Golin   case ARM::AK_ARMV6ZK:
569fadc2108SRenato Golin   case ARM::AK_ARMV6M:
570fadc2108SRenato Golin   case ARM::AK_ARMV6SM:
571fadc2108SRenato Golin   case ARM::AK_ARMV6HL:
572fadc2108SRenato Golin     return 6;
573fadc2108SRenato Golin   case ARM::AK_ARMV7:
574fadc2108SRenato Golin   case ARM::AK_ARMV7A:
575fadc2108SRenato Golin   case ARM::AK_ARMV7R:
576fadc2108SRenato Golin   case ARM::AK_ARMV7M:
577fadc2108SRenato Golin   case ARM::AK_ARMV7L:
578fadc2108SRenato Golin   case ARM::AK_ARMV7HL:
579fadc2108SRenato Golin   case ARM::AK_ARMV7S:
580fadc2108SRenato Golin   case ARM::AK_ARMV7EM:
581fadc2108SRenato Golin     return 7;
582fadc2108SRenato Golin   case ARM::AK_ARMV8A:
583fadc2108SRenato Golin   case ARM::AK_ARMV8_1A:
584fadc2108SRenato Golin     return 8;
585fadc2108SRenato Golin   }
586fadc2108SRenato Golin   return 0;
587fadc2108SRenato Golin }
588fadc2108SRenato Golin 
589f5f373fcSRenato Golin } // namespace llvm
590