Sparc.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //===--- Sparc.cpp - Implement Sparc target feature support ---------------===//
  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. // This file implements Sparc TargetInfo objects.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "Sparc.h"
  13. #include "Targets.h"
  14. #include "clang/Basic/MacroBuilder.h"
  15. #include "llvm/ADT/StringSwitch.h"
  16. using namespace clang;
  17. using namespace clang::targets;
  18. const char *const SparcTargetInfo::GCCRegNames[] = {
  19. // Integer registers
  20. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
  21. "r11", "r12", "r13", "r14", "r15", "r16", "r17", "r18", "r19", "r20", "r21",
  22. "r22", "r23", "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  23. // Floating-point registers
  24. "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10",
  25. "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18", "f19", "f20", "f21",
  26. "f22", "f23", "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31", "f32",
  27. "f34", "f36", "f38", "f40", "f42", "f44", "f46", "f48", "f50", "f52", "f54",
  28. "f56", "f58", "f60", "f62",
  29. };
  30. ArrayRef<const char *> SparcTargetInfo::getGCCRegNames() const {
  31. return llvm::makeArrayRef(GCCRegNames);
  32. }
  33. const TargetInfo::GCCRegAlias SparcTargetInfo::GCCRegAliases[] = {
  34. {{"g0"}, "r0"}, {{"g1"}, "r1"}, {{"g2"}, "r2"}, {{"g3"}, "r3"},
  35. {{"g4"}, "r4"}, {{"g5"}, "r5"}, {{"g6"}, "r6"}, {{"g7"}, "r7"},
  36. {{"o0"}, "r8"}, {{"o1"}, "r9"}, {{"o2"}, "r10"}, {{"o3"}, "r11"},
  37. {{"o4"}, "r12"}, {{"o5"}, "r13"}, {{"o6", "sp"}, "r14"}, {{"o7"}, "r15"},
  38. {{"l0"}, "r16"}, {{"l1"}, "r17"}, {{"l2"}, "r18"}, {{"l3"}, "r19"},
  39. {{"l4"}, "r20"}, {{"l5"}, "r21"}, {{"l6"}, "r22"}, {{"l7"}, "r23"},
  40. {{"i0"}, "r24"}, {{"i1"}, "r25"}, {{"i2"}, "r26"}, {{"i3"}, "r27"},
  41. {{"i4"}, "r28"}, {{"i5"}, "r29"}, {{"i6", "fp"}, "r30"}, {{"i7"}, "r31"},
  42. };
  43. ArrayRef<TargetInfo::GCCRegAlias> SparcTargetInfo::getGCCRegAliases() const {
  44. return llvm::makeArrayRef(GCCRegAliases);
  45. }
  46. bool SparcTargetInfo::hasFeature(StringRef Feature) const {
  47. return llvm::StringSwitch<bool>(Feature)
  48. .Case("softfloat", SoftFloat)
  49. .Case("sparc", true)
  50. .Default(false);
  51. }
  52. struct SparcCPUInfo {
  53. llvm::StringLiteral Name;
  54. SparcTargetInfo::CPUKind Kind;
  55. SparcTargetInfo::CPUGeneration Generation;
  56. };
  57. static constexpr SparcCPUInfo CPUInfo[] = {
  58. {{"v8"}, SparcTargetInfo::CK_V8, SparcTargetInfo::CG_V8},
  59. {{"supersparc"}, SparcTargetInfo::CK_SUPERSPARC, SparcTargetInfo::CG_V8},
  60. {{"sparclite"}, SparcTargetInfo::CK_SPARCLITE, SparcTargetInfo::CG_V8},
  61. {{"f934"}, SparcTargetInfo::CK_F934, SparcTargetInfo::CG_V8},
  62. {{"hypersparc"}, SparcTargetInfo::CK_HYPERSPARC, SparcTargetInfo::CG_V8},
  63. {{"sparclite86x"},
  64. SparcTargetInfo::CK_SPARCLITE86X,
  65. SparcTargetInfo::CG_V8},
  66. {{"sparclet"}, SparcTargetInfo::CK_SPARCLET, SparcTargetInfo::CG_V8},
  67. {{"tsc701"}, SparcTargetInfo::CK_TSC701, SparcTargetInfo::CG_V8},
  68. {{"v9"}, SparcTargetInfo::CK_V9, SparcTargetInfo::CG_V9},
  69. {{"ultrasparc"}, SparcTargetInfo::CK_ULTRASPARC, SparcTargetInfo::CG_V9},
  70. {{"ultrasparc3"}, SparcTargetInfo::CK_ULTRASPARC3, SparcTargetInfo::CG_V9},
  71. {{"niagara"}, SparcTargetInfo::CK_NIAGARA, SparcTargetInfo::CG_V9},
  72. {{"niagara2"}, SparcTargetInfo::CK_NIAGARA2, SparcTargetInfo::CG_V9},
  73. {{"niagara3"}, SparcTargetInfo::CK_NIAGARA3, SparcTargetInfo::CG_V9},
  74. {{"niagara4"}, SparcTargetInfo::CK_NIAGARA4, SparcTargetInfo::CG_V9},
  75. {{"ma2100"}, SparcTargetInfo::CK_MYRIAD2100, SparcTargetInfo::CG_V8},
  76. {{"ma2150"}, SparcTargetInfo::CK_MYRIAD2150, SparcTargetInfo::CG_V8},
  77. {{"ma2155"}, SparcTargetInfo::CK_MYRIAD2155, SparcTargetInfo::CG_V8},
  78. {{"ma2450"}, SparcTargetInfo::CK_MYRIAD2450, SparcTargetInfo::CG_V8},
  79. {{"ma2455"}, SparcTargetInfo::CK_MYRIAD2455, SparcTargetInfo::CG_V8},
  80. {{"ma2x5x"}, SparcTargetInfo::CK_MYRIAD2x5x, SparcTargetInfo::CG_V8},
  81. {{"ma2080"}, SparcTargetInfo::CK_MYRIAD2080, SparcTargetInfo::CG_V8},
  82. {{"ma2085"}, SparcTargetInfo::CK_MYRIAD2085, SparcTargetInfo::CG_V8},
  83. {{"ma2480"}, SparcTargetInfo::CK_MYRIAD2480, SparcTargetInfo::CG_V8},
  84. {{"ma2485"}, SparcTargetInfo::CK_MYRIAD2485, SparcTargetInfo::CG_V8},
  85. {{"ma2x8x"}, SparcTargetInfo::CK_MYRIAD2x8x, SparcTargetInfo::CG_V8},
  86. // FIXME: the myriad2[.n] spellings are obsolete,
  87. // but a grace period is needed to allow updating dependent builds.
  88. {{"myriad2"}, SparcTargetInfo::CK_MYRIAD2x5x, SparcTargetInfo::CG_V8},
  89. {{"myriad2.1"}, SparcTargetInfo::CK_MYRIAD2100, SparcTargetInfo::CG_V8},
  90. {{"myriad2.2"}, SparcTargetInfo::CK_MYRIAD2x5x, SparcTargetInfo::CG_V8},
  91. {{"myriad2.3"}, SparcTargetInfo::CK_MYRIAD2x8x, SparcTargetInfo::CG_V8},
  92. {{"leon2"}, SparcTargetInfo::CK_LEON2, SparcTargetInfo::CG_V8},
  93. {{"at697e"}, SparcTargetInfo::CK_LEON2_AT697E, SparcTargetInfo::CG_V8},
  94. {{"at697f"}, SparcTargetInfo::CK_LEON2_AT697F, SparcTargetInfo::CG_V8},
  95. {{"leon3"}, SparcTargetInfo::CK_LEON3, SparcTargetInfo::CG_V8},
  96. {{"ut699"}, SparcTargetInfo::CK_LEON3_UT699, SparcTargetInfo::CG_V8},
  97. {{"gr712rc"}, SparcTargetInfo::CK_LEON3_GR712RC, SparcTargetInfo::CG_V8},
  98. {{"leon4"}, SparcTargetInfo::CK_LEON4, SparcTargetInfo::CG_V8},
  99. {{"gr740"}, SparcTargetInfo::CK_LEON4_GR740, SparcTargetInfo::CG_V8},
  100. };
  101. SparcTargetInfo::CPUGeneration
  102. SparcTargetInfo::getCPUGeneration(CPUKind Kind) const {
  103. if (Kind == CK_GENERIC)
  104. return CG_V8;
  105. const SparcCPUInfo *Item = llvm::find_if(
  106. CPUInfo, [Kind](const SparcCPUInfo &Info) { return Info.Kind == Kind; });
  107. if (Item == std::end(CPUInfo))
  108. llvm_unreachable("Unexpected CPU kind");
  109. return Item->Generation;
  110. }
  111. SparcTargetInfo::CPUKind SparcTargetInfo::getCPUKind(StringRef Name) const {
  112. const SparcCPUInfo *Item = llvm::find_if(
  113. CPUInfo, [Name](const SparcCPUInfo &Info) { return Info.Name == Name; });
  114. if (Item == std::end(CPUInfo))
  115. return CK_GENERIC;
  116. return Item->Kind;
  117. }
  118. void SparcTargetInfo::fillValidCPUList(
  119. SmallVectorImpl<StringRef> &Values) const {
  120. for (const SparcCPUInfo &Info : CPUInfo)
  121. Values.push_back(Info.Name);
  122. }
  123. void SparcTargetInfo::getTargetDefines(const LangOptions &Opts,
  124. MacroBuilder &Builder) const {
  125. DefineStd(Builder, "sparc", Opts);
  126. Builder.defineMacro("__REGISTER_PREFIX__", "");
  127. if (SoftFloat)
  128. Builder.defineMacro("SOFT_FLOAT", "1");
  129. }
  130. void SparcV8TargetInfo::getTargetDefines(const LangOptions &Opts,
  131. MacroBuilder &Builder) const {
  132. SparcTargetInfo::getTargetDefines(Opts, Builder);
  133. if (getTriple().getOS() == llvm::Triple::Solaris)
  134. Builder.defineMacro("__sparcv8");
  135. else {
  136. switch (getCPUGeneration(CPU)) {
  137. case CG_V8:
  138. Builder.defineMacro("__sparcv8");
  139. Builder.defineMacro("__sparcv8__");
  140. break;
  141. case CG_V9:
  142. Builder.defineMacro("__sparc_v9__");
  143. break;
  144. }
  145. }
  146. if (getTriple().getVendor() == llvm::Triple::Myriad) {
  147. std::string MyriadArchValue, Myriad2Value;
  148. Builder.defineMacro("__sparc_v8__");
  149. Builder.defineMacro("__leon__");
  150. switch (CPU) {
  151. case CK_MYRIAD2100:
  152. MyriadArchValue = "__ma2100";
  153. Myriad2Value = "1";
  154. break;
  155. case CK_MYRIAD2150:
  156. MyriadArchValue = "__ma2150";
  157. Myriad2Value = "2";
  158. break;
  159. case CK_MYRIAD2155:
  160. MyriadArchValue = "__ma2155";
  161. Myriad2Value = "2";
  162. break;
  163. case CK_MYRIAD2450:
  164. MyriadArchValue = "__ma2450";
  165. Myriad2Value = "2";
  166. break;
  167. case CK_MYRIAD2455:
  168. MyriadArchValue = "__ma2455";
  169. Myriad2Value = "2";
  170. break;
  171. case CK_MYRIAD2x5x:
  172. Myriad2Value = "2";
  173. break;
  174. case CK_MYRIAD2080:
  175. MyriadArchValue = "__ma2080";
  176. Myriad2Value = "3";
  177. break;
  178. case CK_MYRIAD2085:
  179. MyriadArchValue = "__ma2085";
  180. Myriad2Value = "3";
  181. break;
  182. case CK_MYRIAD2480:
  183. MyriadArchValue = "__ma2480";
  184. Myriad2Value = "3";
  185. break;
  186. case CK_MYRIAD2485:
  187. MyriadArchValue = "__ma2485";
  188. Myriad2Value = "3";
  189. break;
  190. case CK_MYRIAD2x8x:
  191. Myriad2Value = "3";
  192. break;
  193. default:
  194. MyriadArchValue = "__ma2100";
  195. Myriad2Value = "1";
  196. break;
  197. }
  198. if (!MyriadArchValue.empty()) {
  199. Builder.defineMacro(MyriadArchValue, "1");
  200. Builder.defineMacro(MyriadArchValue + "__", "1");
  201. }
  202. if (Myriad2Value == "2") {
  203. Builder.defineMacro("__ma2x5x", "1");
  204. Builder.defineMacro("__ma2x5x__", "1");
  205. } else if (Myriad2Value == "3") {
  206. Builder.defineMacro("__ma2x8x", "1");
  207. Builder.defineMacro("__ma2x8x__", "1");
  208. }
  209. Builder.defineMacro("__myriad2__", Myriad2Value);
  210. Builder.defineMacro("__myriad2", Myriad2Value);
  211. }
  212. if (getCPUGeneration(CPU) == CG_V9) {
  213. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
  214. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
  215. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
  216. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
  217. }
  218. }
  219. void SparcV9TargetInfo::getTargetDefines(const LangOptions &Opts,
  220. MacroBuilder &Builder) const {
  221. SparcTargetInfo::getTargetDefines(Opts, Builder);
  222. Builder.defineMacro("__sparcv9");
  223. Builder.defineMacro("__arch64__");
  224. // Solaris doesn't need these variants, but the BSDs do.
  225. if (getTriple().getOS() != llvm::Triple::Solaris) {
  226. Builder.defineMacro("__sparc64__");
  227. Builder.defineMacro("__sparc_v9__");
  228. Builder.defineMacro("__sparcv9__");
  229. }
  230. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
  231. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
  232. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
  233. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
  234. }
  235. void SparcV9TargetInfo::fillValidCPUList(
  236. SmallVectorImpl<StringRef> &Values) const {
  237. for (const SparcCPUInfo &Info : CPUInfo)
  238. if (Info.Generation == CG_V9)
  239. Values.push_back(Info.Name);
  240. }