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