ARMTargetParserCommon.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //===---------------- ARMTargetParserCommon ---------------------*- C++ -*-===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Code that is common to ARMTargetParser and AArch64TargetParser.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/TargetParser/ARMTargetParserCommon.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/ADT/StringSwitch.h"
  15. using namespace llvm;
  16. StringRef ARM::getArchSynonym(StringRef Arch) {
  17. return StringSwitch<StringRef>(Arch)
  18. .Case("v5", "v5t")
  19. .Case("v5e", "v5te")
  20. .Case("v6j", "v6")
  21. .Case("v6hl", "v6k")
  22. .Cases("v6m", "v6sm", "v6s-m", "v6-m")
  23. .Cases("v6z", "v6zk", "v6kz")
  24. .Cases("v7", "v7a", "v7hl", "v7l", "v7-a")
  25. .Case("v7r", "v7-r")
  26. .Case("v7m", "v7-m")
  27. .Case("v7em", "v7e-m")
  28. .Cases("v8", "v8a", "v8l", "aarch64", "arm64", "v8-a")
  29. .Case("v8.1a", "v8.1-a")
  30. .Case("v8.2a", "v8.2-a")
  31. .Case("v8.3a", "v8.3-a")
  32. .Case("v8.4a", "v8.4-a")
  33. .Case("v8.5a", "v8.5-a")
  34. .Case("v8.6a", "v8.6-a")
  35. .Case("v8.7a", "v8.7-a")
  36. .Case("v8.8a", "v8.8-a")
  37. .Case("v8.9a", "v8.9-a")
  38. .Case("v8r", "v8-r")
  39. .Cases("v9", "v9a", "v9-a")
  40. .Case("v9.1a", "v9.1-a")
  41. .Case("v9.2a", "v9.2-a")
  42. .Case("v9.3a", "v9.3-a")
  43. .Case("v9.4a", "v9.4-a")
  44. .Case("v8m.base", "v8-m.base")
  45. .Case("v8m.main", "v8-m.main")
  46. .Case("v8.1m.main", "v8.1-m.main")
  47. .Default(Arch);
  48. }
  49. StringRef ARM::getCanonicalArchName(StringRef Arch) {
  50. size_t offset = StringRef::npos;
  51. StringRef A = Arch;
  52. StringRef Error = "";
  53. // Begins with "arm" / "thumb", move past it.
  54. if (A.startswith("arm64_32"))
  55. offset = 8;
  56. else if (A.startswith("arm64e"))
  57. offset = 6;
  58. else if (A.startswith("arm64"))
  59. offset = 5;
  60. else if (A.startswith("aarch64_32"))
  61. offset = 10;
  62. else if (A.startswith("arm"))
  63. offset = 3;
  64. else if (A.startswith("thumb"))
  65. offset = 5;
  66. else if (A.startswith("aarch64")) {
  67. offset = 7;
  68. // AArch64 uses "_be", not "eb" suffix.
  69. if (A.contains("eb"))
  70. return Error;
  71. if (A.substr(offset, 3) == "_be")
  72. offset += 3;
  73. }
  74. // Ex. "armebv7", move past the "eb".
  75. if (offset != StringRef::npos && A.substr(offset, 2) == "eb")
  76. offset += 2;
  77. // Or, if it ends with eb ("armv7eb"), chop it off.
  78. else if (A.endswith("eb"))
  79. A = A.substr(0, A.size() - 2);
  80. // Trim the head
  81. if (offset != StringRef::npos)
  82. A = A.substr(offset);
  83. // Empty string means offset reached the end, which means it's valid.
  84. if (A.empty())
  85. return Arch;
  86. // Only match non-marketing names
  87. if (offset != StringRef::npos) {
  88. // Must start with 'vN'.
  89. if (A.size() >= 2 && (A[0] != 'v' || !std::isdigit(A[1])))
  90. return Error;
  91. // Can't have an extra 'eb'.
  92. if (A.contains("eb"))
  93. return Error;
  94. }
  95. // Arch will either be a 'v' name (v7a) or a marketing name (xscale).
  96. return A;
  97. }
  98. ARM::ISAKind ARM::parseArchISA(StringRef Arch) {
  99. return StringSwitch<ISAKind>(Arch)
  100. .StartsWith("aarch64", ISAKind::AARCH64)
  101. .StartsWith("arm64", ISAKind::AARCH64)
  102. .StartsWith("thumb", ISAKind::THUMB)
  103. .StartsWith("arm", ISAKind::ARM)
  104. .Default(ISAKind::INVALID);
  105. }
  106. ARM::EndianKind ARM::parseArchEndian(StringRef Arch) {
  107. if (Arch.startswith("armeb") || Arch.startswith("thumbeb") ||
  108. Arch.startswith("aarch64_be"))
  109. return EndianKind::BIG;
  110. if (Arch.startswith("arm") || Arch.startswith("thumb")) {
  111. if (Arch.endswith("eb"))
  112. return EndianKind::BIG;
  113. else
  114. return EndianKind::LITTLE;
  115. }
  116. if (Arch.startswith("aarch64") || Arch.startswith("aarch64_32"))
  117. return EndianKind::LITTLE;
  118. return EndianKind::INVALID;
  119. }
  120. // Parse a branch protection specification, which has the form
  121. // standard | none | [bti,pac-ret[+b-key,+leaf]*]
  122. // Returns true on success, with individual elements of the specification
  123. // returned in `PBP`. Returns false in error, with `Err` containing
  124. // an erroneous part of the spec.
  125. bool ARM::parseBranchProtection(StringRef Spec, ParsedBranchProtection &PBP,
  126. StringRef &Err) {
  127. PBP = {"none", "a_key", false};
  128. if (Spec == "none")
  129. return true; // defaults are ok
  130. if (Spec == "standard") {
  131. PBP.Scope = "non-leaf";
  132. PBP.BranchTargetEnforcement = true;
  133. return true;
  134. }
  135. SmallVector<StringRef, 4> Opts;
  136. Spec.split(Opts, "+");
  137. for (int I = 0, E = Opts.size(); I != E; ++I) {
  138. StringRef Opt = Opts[I].trim();
  139. if (Opt == "bti") {
  140. PBP.BranchTargetEnforcement = true;
  141. continue;
  142. }
  143. if (Opt == "pac-ret") {
  144. PBP.Scope = "non-leaf";
  145. for (; I + 1 != E; ++I) {
  146. StringRef PACOpt = Opts[I + 1].trim();
  147. if (PACOpt == "leaf")
  148. PBP.Scope = "all";
  149. else if (PACOpt == "b-key")
  150. PBP.Key = "b_key";
  151. else
  152. break;
  153. }
  154. continue;
  155. }
  156. if (Opt == "")
  157. Err = "<empty>";
  158. else
  159. Err = Opt;
  160. return false;
  161. }
  162. return true;
  163. }