Mips.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. //===--- Mips.h - Declare Mips target feature support -----------*- 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. // This file declares Mips TargetInfo objects.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_LIB_BASIC_TARGETS_MIPS_H
  13. #define LLVM_CLANG_LIB_BASIC_TARGETS_MIPS_H
  14. #include "clang/Basic/TargetInfo.h"
  15. #include "clang/Basic/TargetOptions.h"
  16. #include "llvm/ADT/Triple.h"
  17. #include "llvm/Support/Compiler.h"
  18. namespace clang {
  19. namespace targets {
  20. class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
  21. void setDataLayout() {
  22. StringRef Layout;
  23. if (ABI == "o32")
  24. Layout = "m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64";
  25. else if (ABI == "n32")
  26. Layout = "m:e-p:32:32-i8:8:32-i16:16:32-i64:64-n32:64-S128";
  27. else if (ABI == "n64")
  28. Layout = "m:e-i8:8:32-i16:16:32-i64:64-n32:64-S128";
  29. else
  30. llvm_unreachable("Invalid ABI");
  31. if (BigEndian)
  32. resetDataLayout(("E-" + Layout).str());
  33. else
  34. resetDataLayout(("e-" + Layout).str());
  35. }
  36. std::string CPU;
  37. bool IsMips16;
  38. bool IsMicromips;
  39. bool IsNan2008;
  40. bool IsAbs2008;
  41. bool IsSingleFloat;
  42. bool IsNoABICalls;
  43. bool CanUseBSDABICalls;
  44. enum MipsFloatABI { HardFloat, SoftFloat } FloatABI;
  45. enum DspRevEnum { NoDSP, DSP1, DSP2 } DspRev;
  46. bool HasMSA;
  47. bool DisableMadd4;
  48. bool UseIndirectJumpHazard;
  49. protected:
  50. enum FPModeEnum { FPXX, FP32, FP64 } FPMode;
  51. std::string ABI;
  52. public:
  53. MipsTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
  54. : TargetInfo(Triple), IsMips16(false), IsMicromips(false),
  55. IsNan2008(false), IsAbs2008(false), IsSingleFloat(false),
  56. IsNoABICalls(false), CanUseBSDABICalls(false), FloatABI(HardFloat),
  57. DspRev(NoDSP), HasMSA(false), DisableMadd4(false),
  58. UseIndirectJumpHazard(false), FPMode(FPXX) {
  59. TheCXXABI.set(TargetCXXABI::GenericMIPS);
  60. if (Triple.isMIPS32())
  61. setABI("o32");
  62. else if (Triple.getEnvironment() == llvm::Triple::GNUABIN32)
  63. setABI("n32");
  64. else
  65. setABI("n64");
  66. CPU = ABI == "o32" ? "mips32r2" : "mips64r2";
  67. CanUseBSDABICalls = Triple.isOSFreeBSD() ||
  68. Triple.isOSOpenBSD();
  69. }
  70. bool isIEEE754_2008Default() const {
  71. return CPU == "mips32r6" || CPU == "mips64r6";
  72. }
  73. bool isFP64Default() const {
  74. return CPU == "mips32r6" || ABI == "n32" || ABI == "n64" || ABI == "64";
  75. }
  76. bool isNan2008() const override { return IsNan2008; }
  77. bool processorSupportsGPR64() const;
  78. StringRef getABI() const override { return ABI; }
  79. bool setABI(const std::string &Name) override {
  80. if (Name == "o32") {
  81. setO32ABITypes();
  82. ABI = Name;
  83. return true;
  84. }
  85. if (Name == "n32") {
  86. setN32ABITypes();
  87. ABI = Name;
  88. return true;
  89. }
  90. if (Name == "n64") {
  91. setN64ABITypes();
  92. ABI = Name;
  93. return true;
  94. }
  95. return false;
  96. }
  97. void setO32ABITypes() {
  98. Int64Type = SignedLongLong;
  99. IntMaxType = Int64Type;
  100. LongDoubleFormat = &llvm::APFloat::IEEEdouble();
  101. LongDoubleWidth = LongDoubleAlign = 64;
  102. LongWidth = LongAlign = 32;
  103. MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 32;
  104. PointerWidth = PointerAlign = 32;
  105. PtrDiffType = SignedInt;
  106. SizeType = UnsignedInt;
  107. SuitableAlign = 64;
  108. }
  109. void setN32N64ABITypes() {
  110. LongDoubleWidth = LongDoubleAlign = 128;
  111. LongDoubleFormat = &llvm::APFloat::IEEEquad();
  112. if (getTriple().isOSFreeBSD()) {
  113. LongDoubleWidth = LongDoubleAlign = 64;
  114. LongDoubleFormat = &llvm::APFloat::IEEEdouble();
  115. }
  116. MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
  117. SuitableAlign = 128;
  118. }
  119. void setN64ABITypes() {
  120. setN32N64ABITypes();
  121. if (getTriple().isOSOpenBSD()) {
  122. Int64Type = SignedLongLong;
  123. } else {
  124. Int64Type = SignedLong;
  125. }
  126. IntMaxType = Int64Type;
  127. LongWidth = LongAlign = 64;
  128. PointerWidth = PointerAlign = 64;
  129. PtrDiffType = SignedLong;
  130. SizeType = UnsignedLong;
  131. }
  132. void setN32ABITypes() {
  133. setN32N64ABITypes();
  134. Int64Type = SignedLongLong;
  135. IntMaxType = Int64Type;
  136. LongWidth = LongAlign = 32;
  137. PointerWidth = PointerAlign = 32;
  138. PtrDiffType = SignedInt;
  139. SizeType = UnsignedInt;
  140. }
  141. bool isValidCPUName(StringRef Name) const override;
  142. void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
  143. bool setCPU(const std::string &Name) override {
  144. CPU = Name;
  145. return isValidCPUName(Name);
  146. }
  147. const std::string &getCPU() const { return CPU; }
  148. bool
  149. initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
  150. StringRef CPU,
  151. const std::vector<std::string> &FeaturesVec) const override {
  152. if (CPU.empty())
  153. CPU = getCPU();
  154. if (CPU == "octeon")
  155. Features["mips64r2"] = Features["cnmips"] = true;
  156. else if (CPU == "octeon+")
  157. Features["mips64r2"] = Features["cnmips"] = Features["cnmipsp"] = true;
  158. else
  159. Features[CPU] = true;
  160. return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
  161. }
  162. unsigned getISARev() const;
  163. void getTargetDefines(const LangOptions &Opts,
  164. MacroBuilder &Builder) const override;
  165. ArrayRef<Builtin::Info> getTargetBuiltins() const override;
  166. bool hasFeature(StringRef Feature) const override;
  167. BuiltinVaListKind getBuiltinVaListKind() const override {
  168. return TargetInfo::VoidPtrBuiltinVaList;
  169. }
  170. ArrayRef<const char *> getGCCRegNames() const override {
  171. static const char *const GCCRegNames[] = {
  172. // CPU register names
  173. // Must match second column of GCCRegAliases
  174. "$0", "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9", "$10",
  175. "$11", "$12", "$13", "$14", "$15", "$16", "$17", "$18", "$19", "$20",
  176. "$21", "$22", "$23", "$24", "$25", "$26", "$27", "$28", "$29", "$30",
  177. "$31",
  178. // Floating point register names
  179. "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7", "$f8", "$f9",
  180. "$f10", "$f11", "$f12", "$f13", "$f14", "$f15", "$f16", "$f17", "$f18",
  181. "$f19", "$f20", "$f21", "$f22", "$f23", "$f24", "$f25", "$f26", "$f27",
  182. "$f28", "$f29", "$f30", "$f31",
  183. // Hi/lo and condition register names
  184. "hi", "lo", "", "$fcc0", "$fcc1", "$fcc2", "$fcc3", "$fcc4", "$fcc5",
  185. "$fcc6", "$fcc7", "$ac1hi", "$ac1lo", "$ac2hi", "$ac2lo", "$ac3hi",
  186. "$ac3lo",
  187. // MSA register names
  188. "$w0", "$w1", "$w2", "$w3", "$w4", "$w5", "$w6", "$w7", "$w8", "$w9",
  189. "$w10", "$w11", "$w12", "$w13", "$w14", "$w15", "$w16", "$w17", "$w18",
  190. "$w19", "$w20", "$w21", "$w22", "$w23", "$w24", "$w25", "$w26", "$w27",
  191. "$w28", "$w29", "$w30", "$w31",
  192. // MSA control register names
  193. "$msair", "$msacsr", "$msaaccess", "$msasave", "$msamodify",
  194. "$msarequest", "$msamap", "$msaunmap"
  195. };
  196. return llvm::ArrayRef(GCCRegNames);
  197. }
  198. bool validateAsmConstraint(const char *&Name,
  199. TargetInfo::ConstraintInfo &Info) const override {
  200. switch (*Name) {
  201. default:
  202. return false;
  203. case 'r': // CPU registers.
  204. case 'd': // Equivalent to "r" unless generating MIPS16 code.
  205. case 'y': // Equivalent to "r", backward compatibility only.
  206. case 'f': // floating-point registers.
  207. case 'c': // $25 for indirect jumps
  208. case 'l': // lo register
  209. case 'x': // hilo register pair
  210. Info.setAllowsRegister();
  211. return true;
  212. case 'I': // Signed 16-bit constant
  213. case 'J': // Integer 0
  214. case 'K': // Unsigned 16-bit constant
  215. case 'L': // Signed 32-bit constant, lower 16-bit zeros (for lui)
  216. case 'M': // Constants not loadable via lui, addiu, or ori
  217. case 'N': // Constant -1 to -65535
  218. case 'O': // A signed 15-bit constant
  219. case 'P': // A constant between 1 go 65535
  220. return true;
  221. case 'R': // An address that can be used in a non-macro load or store
  222. Info.setAllowsMemory();
  223. return true;
  224. case 'Z':
  225. if (Name[1] == 'C') { // An address usable by ll, and sc.
  226. Info.setAllowsMemory();
  227. Name++; // Skip over 'Z'.
  228. return true;
  229. }
  230. return false;
  231. }
  232. }
  233. std::string convertConstraint(const char *&Constraint) const override {
  234. std::string R;
  235. switch (*Constraint) {
  236. case 'Z': // Two-character constraint; add "^" hint for later parsing.
  237. if (Constraint[1] == 'C') {
  238. R = std::string("^") + std::string(Constraint, 2);
  239. Constraint++;
  240. return R;
  241. }
  242. break;
  243. }
  244. return TargetInfo::convertConstraint(Constraint);
  245. }
  246. const char *getClobbers() const override {
  247. // In GCC, $1 is not widely used in generated code (it's used only in a few
  248. // specific situations), so there is no real need for users to add it to
  249. // the clobbers list if they want to use it in their inline assembly code.
  250. //
  251. // In LLVM, $1 is treated as a normal GPR and is always allocatable during
  252. // code generation, so using it in inline assembly without adding it to the
  253. // clobbers list can cause conflicts between the inline assembly code and
  254. // the surrounding generated code.
  255. //
  256. // Another problem is that LLVM is allowed to choose $1 for inline assembly
  257. // operands, which will conflict with the ".set at" assembler option (which
  258. // we use only for inline assembly, in order to maintain compatibility with
  259. // GCC) and will also conflict with the user's usage of $1.
  260. //
  261. // The easiest way to avoid these conflicts and keep $1 as an allocatable
  262. // register for generated code is to automatically clobber $1 for all inline
  263. // assembly code.
  264. //
  265. // FIXME: We should automatically clobber $1 only for inline assembly code
  266. // which actually uses it. This would allow LLVM to use $1 for inline
  267. // assembly operands if the user's assembly code doesn't use it.
  268. return "~{$1}";
  269. }
  270. bool handleTargetFeatures(std::vector<std::string> &Features,
  271. DiagnosticsEngine &Diags) override {
  272. IsMips16 = false;
  273. IsMicromips = false;
  274. IsNan2008 = isIEEE754_2008Default();
  275. IsAbs2008 = isIEEE754_2008Default();
  276. IsSingleFloat = false;
  277. FloatABI = HardFloat;
  278. DspRev = NoDSP;
  279. FPMode = isFP64Default() ? FP64 : FPXX;
  280. for (const auto &Feature : Features) {
  281. if (Feature == "+single-float")
  282. IsSingleFloat = true;
  283. else if (Feature == "+soft-float")
  284. FloatABI = SoftFloat;
  285. else if (Feature == "+mips16")
  286. IsMips16 = true;
  287. else if (Feature == "+micromips")
  288. IsMicromips = true;
  289. else if (Feature == "+dsp")
  290. DspRev = std::max(DspRev, DSP1);
  291. else if (Feature == "+dspr2")
  292. DspRev = std::max(DspRev, DSP2);
  293. else if (Feature == "+msa")
  294. HasMSA = true;
  295. else if (Feature == "+nomadd4")
  296. DisableMadd4 = true;
  297. else if (Feature == "+fp64")
  298. FPMode = FP64;
  299. else if (Feature == "-fp64")
  300. FPMode = FP32;
  301. else if (Feature == "+fpxx")
  302. FPMode = FPXX;
  303. else if (Feature == "+nan2008")
  304. IsNan2008 = true;
  305. else if (Feature == "-nan2008")
  306. IsNan2008 = false;
  307. else if (Feature == "+abs2008")
  308. IsAbs2008 = true;
  309. else if (Feature == "-abs2008")
  310. IsAbs2008 = false;
  311. else if (Feature == "+noabicalls")
  312. IsNoABICalls = true;
  313. else if (Feature == "+use-indirect-jump-hazard")
  314. UseIndirectJumpHazard = true;
  315. }
  316. setDataLayout();
  317. return true;
  318. }
  319. int getEHDataRegisterNumber(unsigned RegNo) const override {
  320. if (RegNo == 0)
  321. return 4;
  322. if (RegNo == 1)
  323. return 5;
  324. return -1;
  325. }
  326. bool isCLZForZeroUndef() const override { return false; }
  327. ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override {
  328. static const TargetInfo::GCCRegAlias O32RegAliases[] = {
  329. {{"at"}, "$1"}, {{"v0"}, "$2"}, {{"v1"}, "$3"},
  330. {{"a0"}, "$4"}, {{"a1"}, "$5"}, {{"a2"}, "$6"},
  331. {{"a3"}, "$7"}, {{"t0"}, "$8"}, {{"t1"}, "$9"},
  332. {{"t2"}, "$10"}, {{"t3"}, "$11"}, {{"t4"}, "$12"},
  333. {{"t5"}, "$13"}, {{"t6"}, "$14"}, {{"t7"}, "$15"},
  334. {{"s0"}, "$16"}, {{"s1"}, "$17"}, {{"s2"}, "$18"},
  335. {{"s3"}, "$19"}, {{"s4"}, "$20"}, {{"s5"}, "$21"},
  336. {{"s6"}, "$22"}, {{"s7"}, "$23"}, {{"t8"}, "$24"},
  337. {{"t9"}, "$25"}, {{"k0"}, "$26"}, {{"k1"}, "$27"},
  338. {{"gp"}, "$28"}, {{"sp", "$sp"}, "$29"}, {{"fp", "$fp"}, "$30"},
  339. {{"ra"}, "$31"}
  340. };
  341. static const TargetInfo::GCCRegAlias NewABIRegAliases[] = {
  342. {{"at"}, "$1"}, {{"v0"}, "$2"}, {{"v1"}, "$3"},
  343. {{"a0"}, "$4"}, {{"a1"}, "$5"}, {{"a2"}, "$6"},
  344. {{"a3"}, "$7"}, {{"a4"}, "$8"}, {{"a5"}, "$9"},
  345. {{"a6"}, "$10"}, {{"a7"}, "$11"}, {{"t0"}, "$12"},
  346. {{"t1"}, "$13"}, {{"t2"}, "$14"}, {{"t3"}, "$15"},
  347. {{"s0"}, "$16"}, {{"s1"}, "$17"}, {{"s2"}, "$18"},
  348. {{"s3"}, "$19"}, {{"s4"}, "$20"}, {{"s5"}, "$21"},
  349. {{"s6"}, "$22"}, {{"s7"}, "$23"}, {{"t8"}, "$24"},
  350. {{"t9"}, "$25"}, {{"k0"}, "$26"}, {{"k1"}, "$27"},
  351. {{"gp"}, "$28"}, {{"sp", "$sp"}, "$29"}, {{"fp", "$fp"}, "$30"},
  352. {{"ra"}, "$31"}
  353. };
  354. if (ABI == "o32")
  355. return llvm::ArrayRef(O32RegAliases);
  356. return llvm::ArrayRef(NewABIRegAliases);
  357. }
  358. bool hasInt128Type() const override {
  359. return (ABI == "n32" || ABI == "n64") || getTargetOpts().ForceEnableInt128;
  360. }
  361. unsigned getUnwindWordWidth() const override;
  362. bool validateTarget(DiagnosticsEngine &Diags) const override;
  363. bool hasBitIntType() const override { return true; }
  364. };
  365. } // namespace targets
  366. } // namespace clang
  367. #endif // LLVM_CLANG_LIB_BASIC_TARGETS_MIPS_H