blob: 337532edbbc6f4af2d9a40cb4839ab862b350cd2 [file] [log] [blame]
Renato Golinf5f373f2015-05-08 21:04:27 +00001//===-- 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"
Bradley Smith7b0a7d82015-11-18 16:32:12 +000019#include "llvm/ADT/Twine.h"
Renato Golinebdd12c2015-05-22 20:43:30 +000020#include <cctype>
Renato Golinf5f373f2015-05-08 21:04:27 +000021
22using namespace llvm;
Chandler Carruth799e8802015-08-30 05:27:31 +000023using namespace ARM;
Renato Golinf5f373f2015-05-08 21:04:27 +000024
25namespace {
26
John Brawnd03d2292015-06-05 13:29:24 +000027// List of canonical FPU names (use getFPUSynonym) and which architectural
28// features they correspond to (use getFPUFeatures).
Renato Golinf5f373f2015-05-08 21:04:27 +000029// FIXME: TableGen this.
Javed Absard5526302015-06-29 09:32:29 +000030// The entries must appear in the order listed in ARM::FPUKind for correct indexing
Artyom Skrobovbc09f392015-11-16 12:08:05 +000031static const struct {
Chandler Carruth3309ef62015-08-30 07:51:04 +000032 const char *NameCStr;
33 size_t NameLength;
Renato Golinf5f373f2015-05-08 21:04:27 +000034 ARM::FPUKind ID;
Javed Absard5526302015-06-29 09:32:29 +000035 ARM::FPUVersion FPUVersion;
John Brawnd03d2292015-06-05 13:29:24 +000036 ARM::NeonSupportLevel NeonSupport;
37 ARM::FPURestriction Restriction;
Chandler Carruth3309ef62015-08-30 07:51:04 +000038
39 StringRef getName() const { return StringRef(NameCStr, NameLength); }
Renato Golinf5f373f2015-05-08 21:04:27 +000040} FPUNames[] = {
Chandler Carruth799e8802015-08-30 05:27:31 +000041#define ARM_FPU(NAME, KIND, VERSION, NEON_SUPPORT, RESTRICTION) \
Chandler Carruth3309ef62015-08-30 07:51:04 +000042 { NAME, sizeof(NAME) - 1, KIND, VERSION, NEON_SUPPORT, RESTRICTION },
Chandler Carruth799e8802015-08-30 05:27:31 +000043#include "llvm/Support/ARMTargetParser.def"
Renato Golinf5f373f2015-05-08 21:04:27 +000044};
John Brawnd03d2292015-06-05 13:29:24 +000045
Renato Golinf7c0d5f2015-05-27 18:15:37 +000046// List of canonical arch names (use getArchSynonym).
47// This table also provides the build attribute fields for CPU arch
48// and Arch ID, according to the Addenda to the ARM ABI, chapters
49// 2.4 and 2.3.5.2 respectively.
Renato Golin42dad642015-05-28 15:05:18 +000050// FIXME: SubArch values were simplified to fit into the expectations
51// of the triples and are not conforming with their official names.
52// Check to see if the expectation should be changed.
Renato Golinf5f373f2015-05-08 21:04:27 +000053// FIXME: TableGen this.
Artyom Skrobovbc09f392015-11-16 12:08:05 +000054static const struct {
Chandler Carruth3309ef62015-08-30 07:51:04 +000055 const char *NameCStr;
56 size_t NameLength;
Chandler Carruth3309ef62015-08-30 07:51:04 +000057 const char *CPUAttrCStr;
58 size_t CPUAttrLength;
59 const char *SubArchCStr;
60 size_t SubArchLength;
Bradley Smith4adcb732015-11-16 11:15:22 +000061 unsigned DefaultFPU;
Alexandros Lamprineasea33e5e2015-09-05 17:05:33 +000062 unsigned ArchBaseExtensions;
Ben Craig46642ff2015-12-14 21:57:05 +000063 ARM::ArchKind ID;
64 ARMBuildAttrs::CPUArch ArchAttr; // Arch ID in build attributes.
Chandler Carruth3309ef62015-08-30 07:51:04 +000065
66 StringRef getName() const { return StringRef(NameCStr, NameLength); }
67
68 // CPU class in build attributes.
69 StringRef getCPUAttr() const { return StringRef(CPUAttrCStr, CPUAttrLength); }
70
71 // Sub-Arch name.
72 StringRef getSubArch() const { return StringRef(SubArchCStr, SubArchLength); }
Renato Golinf5f373f2015-05-08 21:04:27 +000073} ARCHNames[] = {
Bradley Smith4adcb732015-11-16 11:15:22 +000074#define ARM_ARCH(NAME, ID, CPU_ATTR, SUB_ARCH, ARCH_ATTR, ARCH_FPU, ARCH_BASE_EXT) \
Ben Craig46642ff2015-12-14 21:57:05 +000075 {NAME, sizeof(NAME) - 1, CPU_ATTR, sizeof(CPU_ATTR) - 1, SUB_ARCH, \
76 sizeof(SUB_ARCH) - 1, ARCH_FPU, ARCH_BASE_EXT, ID, ARCH_ATTR},
Chandler Carruth799e8802015-08-30 05:27:31 +000077#include "llvm/Support/ARMTargetParser.def"
Renato Golinf5f373f2015-05-08 21:04:27 +000078};
Chandler Carruth3309ef62015-08-30 07:51:04 +000079
Renato Goline1326ca2015-05-28 08:59:03 +000080// List of Arch Extension names.
Renato Golinf5f373f2015-05-08 21:04:27 +000081// FIXME: TableGen this.
Artyom Skrobovbc09f392015-11-16 12:08:05 +000082static const struct {
Chandler Carruth3309ef62015-08-30 07:51:04 +000083 const char *NameCStr;
84 size_t NameLength;
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +000085 unsigned ID;
Bradley Smith7b0a7d82015-11-18 16:32:12 +000086 const char *Feature;
87 const char *NegFeature;
Chandler Carruth3309ef62015-08-30 07:51:04 +000088
89 StringRef getName() const { return StringRef(NameCStr, NameLength); }
Renato Golinf5f373f2015-05-08 21:04:27 +000090} ARCHExtNames[] = {
Bradley Smith7b0a7d82015-11-18 16:32:12 +000091#define ARM_ARCH_EXT_NAME(NAME, ID, FEATURE, NEGFEATURE) \
92 { NAME, sizeof(NAME) - 1, ID, FEATURE, NEGFEATURE },
Chandler Carruth799e8802015-08-30 05:27:31 +000093#include "llvm/Support/ARMTargetParser.def"
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +000094};
Chandler Carruth3309ef62015-08-30 07:51:04 +000095
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +000096// List of HWDiv names (use getHWDivSynonym) and which architectural
97// features they correspond to (use getHWDivFeatures).
98// FIXME: TableGen this.
Artyom Skrobovbc09f392015-11-16 12:08:05 +000099static const struct {
Chandler Carruth3309ef62015-08-30 07:51:04 +0000100 const char *NameCStr;
101 size_t NameLength;
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +0000102 unsigned ID;
Chandler Carruth3309ef62015-08-30 07:51:04 +0000103
104 StringRef getName() const { return StringRef(NameCStr, NameLength); }
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +0000105} HWDivNames[] = {
Chandler Carruth3309ef62015-08-30 07:51:04 +0000106#define ARM_HW_DIV_NAME(NAME, ID) { NAME, sizeof(NAME) - 1, ID },
Chandler Carruth799e8802015-08-30 05:27:31 +0000107#include "llvm/Support/ARMTargetParser.def"
Renato Golinf5f373f2015-05-08 21:04:27 +0000108};
Chandler Carruth3309ef62015-08-30 07:51:04 +0000109
Renato Goline8048f02015-05-20 15:05:07 +0000110// List of CPU names and their arches.
111// The same CPU can have multiple arches and can be default on multiple arches.
112// When finding the Arch for a CPU, first-found prevails. Sort them accordingly.
Renato Golin7374fcd2015-05-28 12:10:37 +0000113// When this becomes table-generated, we'd probably need two tables.
Renato Goline8048f02015-05-20 15:05:07 +0000114// FIXME: TableGen this.
Artyom Skrobovbc09f392015-11-16 12:08:05 +0000115static const struct {
Chandler Carruth3309ef62015-08-30 07:51:04 +0000116 const char *NameCStr;
117 size_t NameLength;
Renato Goline8048f02015-05-20 15:05:07 +0000118 ARM::ArchKind ArchID;
Alexandros Lamprineasfcd93d52015-07-15 10:46:21 +0000119 bool Default; // is $Name the default CPU for $ArchID ?
Alexandros Lamprineasea33e5e2015-09-05 17:05:33 +0000120 unsigned DefaultExtensions;
Chandler Carruth3309ef62015-08-30 07:51:04 +0000121
122 StringRef getName() const { return StringRef(NameCStr, NameLength); }
Renato Goline8048f02015-05-20 15:05:07 +0000123} CPUNames[] = {
Alexandros Lamprineasea33e5e2015-09-05 17:05:33 +0000124#define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
125 { NAME, sizeof(NAME) - 1, ID, IS_DEFAULT, DEFAULT_EXT },
Chandler Carruth799e8802015-08-30 05:27:31 +0000126#include "llvm/Support/ARMTargetParser.def"
Renato Goline8048f02015-05-20 15:05:07 +0000127};
Renato Golinf5f373f2015-05-08 21:04:27 +0000128
129} // namespace
130
Renato Golinf5f373f2015-05-08 21:04:27 +0000131// ======================================================= //
132// Information by ID
133// ======================================================= //
134
Chandler Carruth3309ef62015-08-30 07:51:04 +0000135StringRef llvm::ARM::getFPUName(unsigned FPUKind) {
Renato Goline8048f02015-05-20 15:05:07 +0000136 if (FPUKind >= ARM::FK_LAST)
Chandler Carruth3309ef62015-08-30 07:51:04 +0000137 return StringRef();
138 return FPUNames[FPUKind].getName();
Renato Golinf5f373f2015-05-08 21:04:27 +0000139}
140
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000141unsigned llvm::ARM::getFPUVersion(unsigned FPUKind) {
John Brawnd03d2292015-06-05 13:29:24 +0000142 if (FPUKind >= ARM::FK_LAST)
143 return 0;
144 return FPUNames[FPUKind].FPUVersion;
145}
146
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000147unsigned llvm::ARM::getFPUNeonSupportLevel(unsigned FPUKind) {
John Brawnd03d2292015-06-05 13:29:24 +0000148 if (FPUKind >= ARM::FK_LAST)
149 return 0;
150 return FPUNames[FPUKind].NeonSupport;
151}
152
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000153unsigned llvm::ARM::getFPURestriction(unsigned FPUKind) {
John Brawnd03d2292015-06-05 13:29:24 +0000154 if (FPUKind >= ARM::FK_LAST)
155 return 0;
156 return FPUNames[FPUKind].Restriction;
157}
158
Bradley Smith4adcb732015-11-16 11:15:22 +0000159unsigned llvm::ARM::getDefaultFPU(StringRef CPU, unsigned ArchKind) {
160 if (CPU == "generic")
161 return ARCHNames[ArchKind].DefaultFPU;
162
Chandler Carruth822d54a2015-08-30 09:01:38 +0000163 return StringSwitch<unsigned>(CPU)
Alexandros Lamprineasea33e5e2015-09-05 17:05:33 +0000164#define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
Chandler Carruth822d54a2015-08-30 09:01:38 +0000165 .Case(NAME, DEFAULT_FPU)
166#include "llvm/Support/ARMTargetParser.def"
167 .Default(ARM::FK_INVALID);
Alexandros Lamprineasfcd93d52015-07-15 10:46:21 +0000168}
169
Artyom Skrobovbc09f392015-11-16 12:08:05 +0000170unsigned llvm::ARM::getDefaultExtensions(StringRef CPU, unsigned ArchKind) {
171 if (CPU == "generic")
172 return ARCHNames[ArchKind].ArchBaseExtensions;
173
174 return StringSwitch<unsigned>(CPU)
175#define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
176 .Case(NAME, ARCHNames[ID].ArchBaseExtensions | DEFAULT_EXT)
177#include "llvm/Support/ARMTargetParser.def"
178 .Default(ARM::AEK_INVALID);
179}
180
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000181bool llvm::ARM::getHWDivFeatures(unsigned HWDivKind,
Chandler Carruth4fc3a982015-08-30 02:17:15 +0000182 std::vector<const char *> &Features) {
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +0000183
184 if (HWDivKind == ARM::AEK_INVALID)
185 return false;
186
Chandler Carruth4fc3a982015-08-30 02:17:15 +0000187 if (HWDivKind & ARM::AEK_HWDIVARM)
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +0000188 Features.push_back("+hwdiv-arm");
189 else
190 Features.push_back("-hwdiv-arm");
191
192 if (HWDivKind & ARM::AEK_HWDIV)
193 Features.push_back("+hwdiv");
194 else
195 Features.push_back("-hwdiv");
196
197 return true;
198}
199
Alexandros Lamprineasea33e5e2015-09-05 17:05:33 +0000200bool llvm::ARM::getExtensionFeatures(unsigned Extensions,
201 std::vector<const char *> &Features) {
202
203 if (Extensions == ARM::AEK_INVALID)
204 return false;
205
206 if (Extensions & ARM::AEK_CRC)
207 Features.push_back("+crc");
208 else
209 Features.push_back("-crc");
210
Artyom Skrobovcf296442015-09-24 17:31:16 +0000211 if (Extensions & ARM::AEK_DSP)
Artyom Skrobov5a6e3942015-10-23 17:19:19 +0000212 Features.push_back("+dsp");
Artyom Skrobovcf296442015-09-24 17:31:16 +0000213 else
Artyom Skrobov5a6e3942015-10-23 17:19:19 +0000214 Features.push_back("-dsp");
Artyom Skrobovcf296442015-09-24 17:31:16 +0000215
Alexandros Lamprineasea33e5e2015-09-05 17:05:33 +0000216 return getHWDivFeatures(Extensions, Features);
217}
218
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000219bool llvm::ARM::getFPUFeatures(unsigned FPUKind,
Chandler Carruth4fc3a982015-08-30 02:17:15 +0000220 std::vector<const char *> &Features) {
John Brawnd03d2292015-06-05 13:29:24 +0000221
222 if (FPUKind >= ARM::FK_LAST || FPUKind == ARM::FK_INVALID)
223 return false;
224
225 // fp-only-sp and d16 subtarget features are independent of each other, so we
226 // must enable/disable both.
227 switch (FPUNames[FPUKind].Restriction) {
228 case ARM::FR_SP_D16:
229 Features.push_back("+fp-only-sp");
230 Features.push_back("+d16");
231 break;
232 case ARM::FR_D16:
233 Features.push_back("-fp-only-sp");
234 Features.push_back("+d16");
235 break;
236 case ARM::FR_None:
237 Features.push_back("-fp-only-sp");
238 Features.push_back("-d16");
239 break;
240 }
241
242 // FPU version subtarget features are inclusive of lower-numbered ones, so
243 // enable the one corresponding to this version and disable all that are
John Brawnd9e39d52015-06-12 09:38:51 +0000244 // higher. We also have to make sure to disable fp16 when vfp4 is disabled,
245 // as +vfp4 implies +fp16 but -vfp4 does not imply -fp16.
John Brawnd03d2292015-06-05 13:29:24 +0000246 switch (FPUNames[FPUKind].FPUVersion) {
Javed Absard5526302015-06-29 09:32:29 +0000247 case ARM::FV_VFPV5:
John Brawnd03d2292015-06-05 13:29:24 +0000248 Features.push_back("+fp-armv8");
249 break;
Javed Absard5526302015-06-29 09:32:29 +0000250 case ARM::FV_VFPV4:
John Brawnd03d2292015-06-05 13:29:24 +0000251 Features.push_back("+vfp4");
252 Features.push_back("-fp-armv8");
253 break;
Javed Absard5526302015-06-29 09:32:29 +0000254 case ARM::FV_VFPV3_FP16:
255 Features.push_back("+vfp3");
256 Features.push_back("+fp16");
257 Features.push_back("-vfp4");
258 Features.push_back("-fp-armv8");
259 break;
260 case ARM::FV_VFPV3:
John Brawnd03d2292015-06-05 13:29:24 +0000261 Features.push_back("+vfp3");
John Brawnd9e39d52015-06-12 09:38:51 +0000262 Features.push_back("-fp16");
John Brawnd03d2292015-06-05 13:29:24 +0000263 Features.push_back("-vfp4");
264 Features.push_back("-fp-armv8");
265 break;
Javed Absard5526302015-06-29 09:32:29 +0000266 case ARM::FV_VFPV2:
John Brawnd03d2292015-06-05 13:29:24 +0000267 Features.push_back("+vfp2");
268 Features.push_back("-vfp3");
John Brawnd9e39d52015-06-12 09:38:51 +0000269 Features.push_back("-fp16");
John Brawnd03d2292015-06-05 13:29:24 +0000270 Features.push_back("-vfp4");
271 Features.push_back("-fp-armv8");
272 break;
Javed Absard5526302015-06-29 09:32:29 +0000273 case ARM::FV_NONE:
John Brawnd03d2292015-06-05 13:29:24 +0000274 Features.push_back("-vfp2");
275 Features.push_back("-vfp3");
John Brawnd9e39d52015-06-12 09:38:51 +0000276 Features.push_back("-fp16");
John Brawnd03d2292015-06-05 13:29:24 +0000277 Features.push_back("-vfp4");
278 Features.push_back("-fp-armv8");
279 break;
280 }
281
282 // crypto includes neon, so we handle this similarly to FPU version.
283 switch (FPUNames[FPUKind].NeonSupport) {
284 case ARM::NS_Crypto:
Alexandros Lamprineasea33e5e2015-09-05 17:05:33 +0000285 Features.push_back("+neon");
John Brawnd03d2292015-06-05 13:29:24 +0000286 Features.push_back("+crypto");
287 break;
288 case ARM::NS_Neon:
289 Features.push_back("+neon");
290 Features.push_back("-crypto");
291 break;
292 case ARM::NS_None:
293 Features.push_back("-neon");
294 Features.push_back("-crypto");
295 break;
296 }
297
298 return true;
299}
300
Chandler Carruth3309ef62015-08-30 07:51:04 +0000301StringRef llvm::ARM::getArchName(unsigned ArchKind) {
Renato Goline8048f02015-05-20 15:05:07 +0000302 if (ArchKind >= ARM::AK_LAST)
Chandler Carruth3309ef62015-08-30 07:51:04 +0000303 return StringRef();
304 return ARCHNames[ArchKind].getName();
Renato Golinf5f373f2015-05-08 21:04:27 +0000305}
306
Chandler Carruth3309ef62015-08-30 07:51:04 +0000307StringRef llvm::ARM::getCPUAttr(unsigned ArchKind) {
Renato Goline8048f02015-05-20 15:05:07 +0000308 if (ArchKind >= ARM::AK_LAST)
Chandler Carruth3309ef62015-08-30 07:51:04 +0000309 return StringRef();
310 return ARCHNames[ArchKind].getCPUAttr();
Renato Golinf5f373f2015-05-08 21:04:27 +0000311}
312
Chandler Carruth3309ef62015-08-30 07:51:04 +0000313StringRef llvm::ARM::getSubArch(unsigned ArchKind) {
Renato Golin42dad642015-05-28 15:05:18 +0000314 if (ArchKind >= ARM::AK_LAST)
Chandler Carruth3309ef62015-08-30 07:51:04 +0000315 return StringRef();
316 return ARCHNames[ArchKind].getSubArch();
Renato Golin42dad642015-05-28 15:05:18 +0000317}
318
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000319unsigned llvm::ARM::getArchAttr(unsigned ArchKind) {
Renato Goline8048f02015-05-20 15:05:07 +0000320 if (ArchKind >= ARM::AK_LAST)
321 return ARMBuildAttrs::CPUArch::Pre_v4;
Renato Golinf7c0d5f2015-05-27 18:15:37 +0000322 return ARCHNames[ArchKind].ArchAttr;
Renato Golinf5f373f2015-05-08 21:04:27 +0000323}
324
Chandler Carruth3309ef62015-08-30 07:51:04 +0000325StringRef llvm::ARM::getArchExtName(unsigned ArchExtKind) {
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +0000326 for (const auto AE : ARCHExtNames) {
327 if (ArchExtKind == AE.ID)
Chandler Carruth3309ef62015-08-30 07:51:04 +0000328 return AE.getName();
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +0000329 }
Chandler Carruth3309ef62015-08-30 07:51:04 +0000330 return StringRef();
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +0000331}
332
Bradley Smith7b0a7d82015-11-18 16:32:12 +0000333const char *llvm::ARM::getArchExtFeature(StringRef ArchExt) {
NAKAMURA Takumi768579c2015-11-19 15:42:52 +0000334 if (ArchExt.startswith("no")) {
NAKAMURA Takumib6b25452015-11-19 15:03:11 +0000335 StringRef ArchExtBase(ArchExt.substr(2));
336 for (const auto AE : ARCHExtNames) {
337 if (AE.NegFeature && ArchExtBase == AE.getName())
338 return AE.NegFeature;
339 }
340 }
Bradley Smith7b0a7d82015-11-18 16:32:12 +0000341 for (const auto AE : ARCHExtNames) {
342 if (AE.Feature && ArchExt == AE.getName())
343 return AE.Feature;
Bradley Smith7b0a7d82015-11-18 16:32:12 +0000344 }
345
346 return nullptr;
347}
348
Chandler Carruth3309ef62015-08-30 07:51:04 +0000349StringRef llvm::ARM::getHWDivName(unsigned HWDivKind) {
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +0000350 for (const auto D : HWDivNames) {
351 if (HWDivKind == D.ID)
Chandler Carruth3309ef62015-08-30 07:51:04 +0000352 return D.getName();
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +0000353 }
Chandler Carruth3309ef62015-08-30 07:51:04 +0000354 return StringRef();
Renato Goline8048f02015-05-20 15:05:07 +0000355}
356
Chandler Carruth3309ef62015-08-30 07:51:04 +0000357StringRef llvm::ARM::getDefaultCPU(StringRef Arch) {
Renato Goline8048f02015-05-20 15:05:07 +0000358 unsigned AK = parseArch(Arch);
359 if (AK == ARM::AK_INVALID)
Chandler Carruth3309ef62015-08-30 07:51:04 +0000360 return StringRef();
Renato Goline8048f02015-05-20 15:05:07 +0000361
362 // Look for multiple AKs to find the default for pair AK+Name.
363 for (const auto CPU : CPUNames) {
364 if (CPU.ArchID == AK && CPU.Default)
Chandler Carruth3309ef62015-08-30 07:51:04 +0000365 return CPU.getName();
Renato Goline8048f02015-05-20 15:05:07 +0000366 }
Bradley Smith4adcb732015-11-16 11:15:22 +0000367
368 // If we can't find a default then target the architecture instead
369 return "generic";
Renato Golinf5f373f2015-05-08 21:04:27 +0000370}
371
372// ======================================================= //
373// Parsers
374// ======================================================= //
375
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000376static StringRef getHWDivSynonym(StringRef HWDiv) {
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +0000377 return StringSwitch<StringRef>(HWDiv)
Chandler Carruth4fc3a982015-08-30 02:17:15 +0000378 .Case("thumb,arm", "arm,thumb")
379 .Default(HWDiv);
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +0000380}
381
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000382static StringRef getFPUSynonym(StringRef FPU) {
Renato Golinf5f373f2015-05-08 21:04:27 +0000383 return StringSwitch<StringRef>(FPU)
Chandler Carruth4fc3a982015-08-30 02:17:15 +0000384 .Cases("fpa", "fpe2", "fpe3", "maverick", "invalid") // Unsupported
385 .Case("vfp2", "vfpv2")
386 .Case("vfp3", "vfpv3")
387 .Case("vfp4", "vfpv4")
388 .Case("vfp3-d16", "vfpv3-d16")
389 .Case("vfp4-d16", "vfpv4-d16")
390 .Cases("fp4-sp-d16", "vfpv4-sp-d16", "fpv4-sp-d16")
391 .Cases("fp4-dp-d16", "fpv4-dp-d16", "vfpv4-d16")
392 .Case("fp5-sp-d16", "fpv5-sp-d16")
393 .Cases("fp5-dp-d16", "fpv5-dp-d16", "fpv5-d16")
394 // FIXME: Clang uses it, but it's bogus, since neon defaults to vfpv3.
395 .Case("neon-vfpv3", "neon")
396 .Default(FPU);
Renato Golinf5f373f2015-05-08 21:04:27 +0000397}
398
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000399static StringRef getArchSynonym(StringRef Arch) {
Renato Golinf5f373f2015-05-08 21:04:27 +0000400 return StringSwitch<StringRef>(Arch)
Artyom Skrobov2c2f3782015-11-12 15:51:41 +0000401 .Case("v5", "v5t")
402 .Case("v5e", "v5te")
Artyom Skrobov91f339a2015-11-20 16:46:09 +0000403 .Case("v6j", "v6")
Artyom Skrobov2c2f3782015-11-12 15:51:41 +0000404 .Case("v6hl", "v6k")
405 .Cases("v6m", "v6sm", "v6s-m", "v6-m")
Artyom Skrobovf187a652015-11-16 14:05:32 +0000406 .Cases("v6z", "v6zk", "v6kz")
Artyom Skrobov2c2f3782015-11-12 15:51:41 +0000407 .Cases("v7", "v7a", "v7hl", "v7l", "v7-a")
Chandler Carruth4fc3a982015-08-30 02:17:15 +0000408 .Case("v7r", "v7-r")
409 .Case("v7m", "v7-m")
410 .Case("v7em", "v7e-m")
411 .Cases("v8", "v8a", "aarch64", "arm64", "v8-a")
412 .Case("v8.1a", "v8.1-a")
Oliver Stannard46670712015-12-01 10:33:56 +0000413 .Case("v8.2a", "v8.2-a")
Chandler Carruth4fc3a982015-08-30 02:17:15 +0000414 .Default(Arch);
Renato Golinf5f373f2015-05-08 21:04:27 +0000415}
416
Renato Goline8048f02015-05-20 15:05:07 +0000417// MArch is expected to be of the form (arm|thumb)?(eb)?(v.+)?(eb)?, but
418// (iwmmxt|xscale)(eb)? is also permitted. If the former, return
Renato Golinebdd12c2015-05-22 20:43:30 +0000419// "v.+", if the latter, return unmodified string, minus 'eb'.
420// If invalid, return empty string.
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000421StringRef llvm::ARM::getCanonicalArchName(StringRef Arch) {
Renato Goline8048f02015-05-20 15:05:07 +0000422 size_t offset = StringRef::npos;
423 StringRef A = Arch;
Renato Golinb6b9e052015-05-21 13:52:20 +0000424 StringRef Error = "";
Renato Goline8048f02015-05-20 15:05:07 +0000425
426 // Begins with "arm" / "thumb", move past it.
Renato Golinebdd12c2015-05-22 20:43:30 +0000427 if (A.startswith("arm64"))
428 offset = 5;
429 else if (A.startswith("arm"))
Renato Goline8048f02015-05-20 15:05:07 +0000430 offset = 3;
431 else if (A.startswith("thumb"))
432 offset = 5;
Renato Golinb6b9e052015-05-21 13:52:20 +0000433 else if (A.startswith("aarch64")) {
434 offset = 7;
435 // AArch64 uses "_be", not "eb" suffix.
436 if (A.find("eb") != StringRef::npos)
437 return Error;
Chandler Carruth4fc3a982015-08-30 02:17:15 +0000438 if (A.substr(offset, 3) == "_be")
Renato Golinb6b9e052015-05-21 13:52:20 +0000439 offset += 3;
440 }
441
Renato Goline8048f02015-05-20 15:05:07 +0000442 // Ex. "armebv7", move past the "eb".
443 if (offset != StringRef::npos && A.substr(offset, 2) == "eb")
444 offset += 2;
445 // Or, if it ends with eb ("armv7eb"), chop it off.
446 else if (A.endswith("eb"))
447 A = A.substr(0, A.size() - 2);
Renato Golinebdd12c2015-05-22 20:43:30 +0000448 // Trim the head
449 if (offset != StringRef::npos)
Renato Goline8048f02015-05-20 15:05:07 +0000450 A = A.substr(offset);
451
Renato Golinebdd12c2015-05-22 20:43:30 +0000452 // Empty string means offset reached the end, which means it's valid.
Renato Goline8048f02015-05-20 15:05:07 +0000453 if (A.empty())
454 return Arch;
455
Renato Golinebdd12c2015-05-22 20:43:30 +0000456 // Only match non-marketing names
457 if (offset != StringRef::npos) {
Chandler Carruth4fc3a982015-08-30 02:17:15 +0000458 // Must start with 'vN'.
Renato Golinebdd12c2015-05-22 20:43:30 +0000459 if (A[0] != 'v' || !std::isdigit(A[1]))
460 return Error;
461 // Can't have an extra 'eb'.
462 if (A.find("eb") != StringRef::npos)
463 return Error;
464 }
Renato Goline8048f02015-05-20 15:05:07 +0000465
Renato Golinebdd12c2015-05-22 20:43:30 +0000466 // Arch will either be a 'v' name (v7a) or a marketing name (xscale).
Renato Goline8048f02015-05-20 15:05:07 +0000467 return A;
468}
469
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000470unsigned llvm::ARM::parseHWDiv(StringRef HWDiv) {
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +0000471 StringRef Syn = getHWDivSynonym(HWDiv);
472 for (const auto D : HWDivNames) {
Chandler Carruth3309ef62015-08-30 07:51:04 +0000473 if (Syn == D.getName())
Alexandros Lamprineas4ea70752015-07-27 22:26:59 +0000474 return D.ID;
475 }
476 return ARM::AEK_INVALID;
477}
478
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000479unsigned llvm::ARM::parseFPU(StringRef FPU) {
Renato Golinf5f373f2015-05-08 21:04:27 +0000480 StringRef Syn = getFPUSynonym(FPU);
481 for (const auto F : FPUNames) {
Chandler Carruth3309ef62015-08-30 07:51:04 +0000482 if (Syn == F.getName())
Renato Golinf5f373f2015-05-08 21:04:27 +0000483 return F.ID;
484 }
Renato Golin35de35d2015-05-12 10:33:58 +0000485 return ARM::FK_INVALID;
Renato Golinf5f373f2015-05-08 21:04:27 +0000486}
487
Renato Goline8048f02015-05-20 15:05:07 +0000488// Allows partial match, ex. "v7a" matches "armv7a".
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000489unsigned llvm::ARM::parseArch(StringRef Arch) {
Artyom Skrobov85aebc82015-06-04 21:26:58 +0000490 Arch = getCanonicalArchName(Arch);
Renato Golinf5f373f2015-05-08 21:04:27 +0000491 StringRef Syn = getArchSynonym(Arch);
492 for (const auto A : ARCHNames) {
Chandler Carruth3309ef62015-08-30 07:51:04 +0000493 if (A.getName().endswith(Syn))
Renato Golinf5f373f2015-05-08 21:04:27 +0000494 return A.ID;
495 }
Renato Golin35de35d2015-05-12 10:33:58 +0000496 return ARM::AK_INVALID;
Renato Golinf5f373f2015-05-08 21:04:27 +0000497}
498
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000499unsigned llvm::ARM::parseArchExt(StringRef ArchExt) {
Renato Golinf5f373f2015-05-08 21:04:27 +0000500 for (const auto A : ARCHExtNames) {
Chandler Carruth3309ef62015-08-30 07:51:04 +0000501 if (ArchExt == A.getName())
Renato Golinf5f373f2015-05-08 21:04:27 +0000502 return A.ID;
503 }
Renato Golin35de35d2015-05-12 10:33:58 +0000504 return ARM::AEK_INVALID;
Renato Golinf5f373f2015-05-08 21:04:27 +0000505}
506
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000507unsigned llvm::ARM::parseCPUArch(StringRef CPU) {
Renato Goline8048f02015-05-20 15:05:07 +0000508 for (const auto C : CPUNames) {
Chandler Carruth3309ef62015-08-30 07:51:04 +0000509 if (CPU == C.getName())
Renato Goline8048f02015-05-20 15:05:07 +0000510 return C.ArchID;
511 }
512 return ARM::AK_INVALID;
513}
514
Renato Golinb6b9e052015-05-21 13:52:20 +0000515// ARM, Thumb, AArch64
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000516unsigned llvm::ARM::parseArchISA(StringRef Arch) {
Renato Golinb6b9e052015-05-21 13:52:20 +0000517 return StringSwitch<unsigned>(Arch)
518 .StartsWith("aarch64", ARM::IK_AARCH64)
Chandler Carruth4fc3a982015-08-30 02:17:15 +0000519 .StartsWith("arm64", ARM::IK_AARCH64)
520 .StartsWith("thumb", ARM::IK_THUMB)
521 .StartsWith("arm", ARM::IK_ARM)
Renato Golinb6b9e052015-05-21 13:52:20 +0000522 .Default(ARM::EK_INVALID);
523}
524
525// Little/Big endian
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000526unsigned llvm::ARM::parseArchEndian(StringRef Arch) {
Chandler Carruth4fc3a982015-08-30 02:17:15 +0000527 if (Arch.startswith("armeb") || Arch.startswith("thumbeb") ||
Renato Golinb6b9e052015-05-21 13:52:20 +0000528 Arch.startswith("aarch64_be"))
529 return ARM::EK_BIG;
530
531 if (Arch.startswith("arm") || Arch.startswith("thumb")) {
532 if (Arch.endswith("eb"))
533 return ARM::EK_BIG;
534 else
535 return ARM::EK_LITTLE;
536 }
537
538 if (Arch.startswith("aarch64"))
539 return ARM::EK_LITTLE;
540
541 return ARM::EK_INVALID;
542}
543
Renato Golinfadc2102015-05-22 18:17:55 +0000544// Profile A/R/M
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000545unsigned llvm::ARM::parseArchProfile(StringRef Arch) {
Renato Golinfadc2102015-05-22 18:17:55 +0000546 Arch = getCanonicalArchName(Arch);
Chandler Carruth4fc3a982015-08-30 02:17:15 +0000547 switch (parseArch(Arch)) {
Renato Golinfadc2102015-05-22 18:17:55 +0000548 case ARM::AK_ARMV6M:
549 case ARM::AK_ARMV7M:
Renato Golinfadc2102015-05-22 18:17:55 +0000550 case ARM::AK_ARMV7EM:
551 return ARM::PK_M;
552 case ARM::AK_ARMV7R:
553 return ARM::PK_R;
Renato Golinfadc2102015-05-22 18:17:55 +0000554 case ARM::AK_ARMV7A:
Tim Northover2d4d1612015-10-28 22:36:05 +0000555 case ARM::AK_ARMV7K:
Renato Golinfadc2102015-05-22 18:17:55 +0000556 case ARM::AK_ARMV8A:
557 case ARM::AK_ARMV8_1A:
Oliver Stannard46670712015-12-01 10:33:56 +0000558 case ARM::AK_ARMV8_2A:
Renato Golinfadc2102015-05-22 18:17:55 +0000559 return ARM::PK_A;
560 }
561 return ARM::PK_INVALID;
562}
563
Renato Golinebdd12c2015-05-22 20:43:30 +0000564// Version number (ex. v7 = 7).
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000565unsigned llvm::ARM::parseArchVersion(StringRef Arch) {
Renato Golinfadc2102015-05-22 18:17:55 +0000566 Arch = getCanonicalArchName(Arch);
Chandler Carruth4fc3a982015-08-30 02:17:15 +0000567 switch (parseArch(Arch)) {
Renato Golinfadc2102015-05-22 18:17:55 +0000568 case ARM::AK_ARMV2:
569 case ARM::AK_ARMV2A:
570 return 2;
571 case ARM::AK_ARMV3:
572 case ARM::AK_ARMV3M:
573 return 3;
574 case ARM::AK_ARMV4:
575 case ARM::AK_ARMV4T:
576 return 4;
Renato Golinfadc2102015-05-22 18:17:55 +0000577 case ARM::AK_ARMV5T:
578 case ARM::AK_ARMV5TE:
579 case ARM::AK_IWMMXT:
580 case ARM::AK_IWMMXT2:
581 case ARM::AK_XSCALE:
Renato Golinfadc2102015-05-22 18:17:55 +0000582 case ARM::AK_ARMV5TEJ:
583 return 5;
584 case ARM::AK_ARMV6:
Renato Golinfadc2102015-05-22 18:17:55 +0000585 case ARM::AK_ARMV6K:
586 case ARM::AK_ARMV6T2:
Artyom Skrobovf187a652015-11-16 14:05:32 +0000587 case ARM::AK_ARMV6KZ:
Renato Golinfadc2102015-05-22 18:17:55 +0000588 case ARM::AK_ARMV6M:
Renato Golinfadc2102015-05-22 18:17:55 +0000589 return 6;
Renato Golinfadc2102015-05-22 18:17:55 +0000590 case ARM::AK_ARMV7A:
591 case ARM::AK_ARMV7R:
592 case ARM::AK_ARMV7M:
Renato Golinfadc2102015-05-22 18:17:55 +0000593 case ARM::AK_ARMV7S:
594 case ARM::AK_ARMV7EM:
Vedant Kumar366dd9fd2015-08-21 21:52:48 +0000595 case ARM::AK_ARMV7K:
Renato Golinfadc2102015-05-22 18:17:55 +0000596 return 7;
597 case ARM::AK_ARMV8A:
598 case ARM::AK_ARMV8_1A:
Oliver Stannard46670712015-12-01 10:33:56 +0000599 case ARM::AK_ARMV8_2A:
Renato Golinfadc2102015-05-22 18:17:55 +0000600 return 8;
601 }
602 return 0;
603}