1 //===---------------- ARMTargetParserCommon ---------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Code that is common to ARMTargetParser and AArch64TargetParser.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/TargetParser/ARMTargetParserCommon.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringSwitch.h"
19 StringRef
ARM::getArchSynonym(StringRef Arch
) {
20 return StringSwitch
<StringRef
>(Arch
)
25 .Cases("v6m", "v6sm", "v6s-m", "v6-m")
26 .Cases("v6z", "v6zk", "v6kz")
27 .Cases("v7", "v7a", "v7hl", "v7l", "v7-a")
30 .Case("v7em", "v7e-m")
31 .Cases("v8", "v8a", "v8l", "aarch64", "arm64", "v8-a")
32 .Case("v8.1a", "v8.1-a")
33 .Case("v8.2a", "v8.2-a")
34 .Case("v8.3a", "v8.3-a")
35 .Case("v8.4a", "v8.4-a")
36 .Case("v8.5a", "v8.5-a")
37 .Case("v8.6a", "v8.6-a")
38 .Case("v8.7a", "v8.7-a")
39 .Case("v8.8a", "v8.8-a")
40 .Case("v8.9a", "v8.9-a")
42 .Cases("v9", "v9a", "v9-a")
43 .Case("v9.1a", "v9.1-a")
44 .Case("v9.2a", "v9.2-a")
45 .Case("v9.3a", "v9.3-a")
46 .Case("v9.4a", "v9.4-a")
47 .Case("v8m.base", "v8-m.base")
48 .Case("v8m.main", "v8-m.main")
49 .Case("v8.1m.main", "v8.1-m.main")
53 StringRef
ARM::getCanonicalArchName(StringRef Arch
) {
54 size_t offset
= StringRef::npos
;
58 // Begins with "arm" / "thumb", move past it.
59 if (A
.startswith("arm64_32"))
61 else if (A
.startswith("arm64e"))
63 else if (A
.startswith("arm64"))
65 else if (A
.startswith("aarch64_32"))
67 else if (A
.startswith("arm"))
69 else if (A
.startswith("thumb"))
71 else if (A
.startswith("aarch64")) {
73 // AArch64 uses "_be", not "eb" suffix.
76 if (A
.substr(offset
, 3) == "_be")
80 // Ex. "armebv7", move past the "eb".
81 if (offset
!= StringRef::npos
&& A
.substr(offset
, 2) == "eb")
83 // Or, if it ends with eb ("armv7eb"), chop it off.
84 else if (A
.endswith("eb"))
85 A
= A
.substr(0, A
.size() - 2);
87 if (offset
!= StringRef::npos
)
90 // Empty string means offset reached the end, which means it's valid.
94 // Only match non-marketing names
95 if (offset
!= StringRef::npos
) {
96 // Must start with 'vN'.
97 if (A
.size() >= 2 && (A
[0] != 'v' || !std::isdigit(A
[1])))
99 // Can't have an extra 'eb'.
100 if (A
.contains("eb"))
104 // Arch will either be a 'v' name (v7a) or a marketing name (xscale).
108 ARM::ISAKind
ARM::parseArchISA(StringRef Arch
) {
109 return StringSwitch
<ISAKind
>(Arch
)
110 .StartsWith("aarch64", ISAKind::AARCH64
)
111 .StartsWith("arm64", ISAKind::AARCH64
)
112 .StartsWith("thumb", ISAKind::THUMB
)
113 .StartsWith("arm", ISAKind::ARM
)
114 .Default(ISAKind::INVALID
);
117 ARM::EndianKind
ARM::parseArchEndian(StringRef Arch
) {
118 if (Arch
.startswith("armeb") || Arch
.startswith("thumbeb") ||
119 Arch
.startswith("aarch64_be"))
120 return EndianKind::BIG
;
122 if (Arch
.startswith("arm") || Arch
.startswith("thumb")) {
123 if (Arch
.endswith("eb"))
124 return EndianKind::BIG
;
126 return EndianKind::LITTLE
;
129 if (Arch
.startswith("aarch64") || Arch
.startswith("aarch64_32"))
130 return EndianKind::LITTLE
;
132 return EndianKind::INVALID
;
135 // Parse a branch protection specification, which has the form
136 // standard | none | [bti,pac-ret[+b-key,+leaf]*]
137 // Returns true on success, with individual elements of the specification
138 // returned in `PBP`. Returns false in error, with `Err` containing
139 // an erroneous part of the spec.
140 bool ARM::parseBranchProtection(StringRef Spec
, ParsedBranchProtection
&PBP
,
142 PBP
= {"none", "a_key", false};
144 return true; // defaults are ok
146 if (Spec
== "standard") {
147 PBP
.Scope
= "non-leaf";
148 PBP
.BranchTargetEnforcement
= true;
152 SmallVector
<StringRef
, 4> Opts
;
153 Spec
.split(Opts
, "+");
154 for (int I
= 0, E
= Opts
.size(); I
!= E
; ++I
) {
155 StringRef Opt
= Opts
[I
].trim();
157 PBP
.BranchTargetEnforcement
= true;
160 if (Opt
== "pac-ret") {
161 PBP
.Scope
= "non-leaf";
162 for (; I
+ 1 != E
; ++I
) {
163 StringRef PACOpt
= Opts
[I
+ 1].trim();
164 if (PACOpt
== "leaf")
166 else if (PACOpt
== "b-key")