Mips.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //===--- Mips.cpp - Implement Mips 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 Mips TargetInfo objects.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "Mips.h"
  13. #include "Targets.h"
  14. #include "clang/Basic/Diagnostic.h"
  15. #include "clang/Basic/MacroBuilder.h"
  16. #include "clang/Basic/TargetBuiltins.h"
  17. #include "llvm/ADT/StringSwitch.h"
  18. using namespace clang;
  19. using namespace clang::targets;
  20. const Builtin::Info MipsTargetInfo::BuiltinInfo[] = {
  21. #define BUILTIN(ID, TYPE, ATTRS) \
  22. {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
  23. #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
  24. {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
  25. #include "clang/Basic/BuiltinsMips.def"
  26. };
  27. bool MipsTargetInfo::processorSupportsGPR64() const {
  28. return llvm::StringSwitch<bool>(CPU)
  29. .Case("mips3", true)
  30. .Case("mips4", true)
  31. .Case("mips5", true)
  32. .Case("mips64", true)
  33. .Case("mips64r2", true)
  34. .Case("mips64r3", true)
  35. .Case("mips64r5", true)
  36. .Case("mips64r6", true)
  37. .Case("octeon", true)
  38. .Case("octeon+", true)
  39. .Default(false);
  40. }
  41. static constexpr llvm::StringLiteral ValidCPUNames[] = {
  42. {"mips1"}, {"mips2"}, {"mips3"}, {"mips4"}, {"mips5"},
  43. {"mips32"}, {"mips32r2"}, {"mips32r3"}, {"mips32r5"}, {"mips32r6"},
  44. {"mips64"}, {"mips64r2"}, {"mips64r3"}, {"mips64r5"}, {"mips64r6"},
  45. {"octeon"}, {"octeon+"}, {"p5600"}};
  46. bool MipsTargetInfo::isValidCPUName(StringRef Name) const {
  47. return llvm::is_contained(ValidCPUNames, Name);
  48. }
  49. void MipsTargetInfo::fillValidCPUList(
  50. SmallVectorImpl<StringRef> &Values) const {
  51. Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
  52. }
  53. unsigned MipsTargetInfo::getISARev() const {
  54. return llvm::StringSwitch<unsigned>(getCPU())
  55. .Cases("mips32", "mips64", 1)
  56. .Cases("mips32r2", "mips64r2", "octeon", "octeon+", 2)
  57. .Cases("mips32r3", "mips64r3", 3)
  58. .Cases("mips32r5", "mips64r5", 5)
  59. .Cases("mips32r6", "mips64r6", 6)
  60. .Default(0);
  61. }
  62. void MipsTargetInfo::getTargetDefines(const LangOptions &Opts,
  63. MacroBuilder &Builder) const {
  64. if (BigEndian) {
  65. DefineStd(Builder, "MIPSEB", Opts);
  66. Builder.defineMacro("_MIPSEB");
  67. } else {
  68. DefineStd(Builder, "MIPSEL", Opts);
  69. Builder.defineMacro("_MIPSEL");
  70. }
  71. Builder.defineMacro("__mips__");
  72. Builder.defineMacro("_mips");
  73. if (Opts.GNUMode)
  74. Builder.defineMacro("mips");
  75. if (ABI == "o32") {
  76. Builder.defineMacro("__mips", "32");
  77. Builder.defineMacro("_MIPS_ISA", "_MIPS_ISA_MIPS32");
  78. } else {
  79. Builder.defineMacro("__mips", "64");
  80. Builder.defineMacro("__mips64");
  81. Builder.defineMacro("__mips64__");
  82. Builder.defineMacro("_MIPS_ISA", "_MIPS_ISA_MIPS64");
  83. }
  84. const std::string ISARev = std::to_string(getISARev());
  85. if (!ISARev.empty())
  86. Builder.defineMacro("__mips_isa_rev", ISARev);
  87. if (ABI == "o32") {
  88. Builder.defineMacro("__mips_o32");
  89. Builder.defineMacro("_ABIO32", "1");
  90. Builder.defineMacro("_MIPS_SIM", "_ABIO32");
  91. } else if (ABI == "n32") {
  92. Builder.defineMacro("__mips_n32");
  93. Builder.defineMacro("_ABIN32", "2");
  94. Builder.defineMacro("_MIPS_SIM", "_ABIN32");
  95. } else if (ABI == "n64") {
  96. Builder.defineMacro("__mips_n64");
  97. Builder.defineMacro("_ABI64", "3");
  98. Builder.defineMacro("_MIPS_SIM", "_ABI64");
  99. } else
  100. llvm_unreachable("Invalid ABI.");
  101. if (!IsNoABICalls) {
  102. Builder.defineMacro("__mips_abicalls");
  103. if (CanUseBSDABICalls)
  104. Builder.defineMacro("__ABICALLS__");
  105. }
  106. Builder.defineMacro("__REGISTER_PREFIX__", "");
  107. switch (FloatABI) {
  108. case HardFloat:
  109. Builder.defineMacro("__mips_hard_float", Twine(1));
  110. break;
  111. case SoftFloat:
  112. Builder.defineMacro("__mips_soft_float", Twine(1));
  113. break;
  114. }
  115. if (IsSingleFloat)
  116. Builder.defineMacro("__mips_single_float", Twine(1));
  117. switch (FPMode) {
  118. case FPXX:
  119. Builder.defineMacro("__mips_fpr", Twine(0));
  120. break;
  121. case FP32:
  122. Builder.defineMacro("__mips_fpr", Twine(32));
  123. break;
  124. case FP64:
  125. Builder.defineMacro("__mips_fpr", Twine(64));
  126. break;
  127. }
  128. if (FPMode == FP64 || IsSingleFloat)
  129. Builder.defineMacro("_MIPS_FPSET", Twine(32));
  130. else
  131. Builder.defineMacro("_MIPS_FPSET", Twine(16));
  132. if (IsMips16)
  133. Builder.defineMacro("__mips16", Twine(1));
  134. if (IsMicromips)
  135. Builder.defineMacro("__mips_micromips", Twine(1));
  136. if (IsNan2008)
  137. Builder.defineMacro("__mips_nan2008", Twine(1));
  138. if (IsAbs2008)
  139. Builder.defineMacro("__mips_abs2008", Twine(1));
  140. switch (DspRev) {
  141. default:
  142. break;
  143. case DSP1:
  144. Builder.defineMacro("__mips_dsp_rev", Twine(1));
  145. Builder.defineMacro("__mips_dsp", Twine(1));
  146. break;
  147. case DSP2:
  148. Builder.defineMacro("__mips_dsp_rev", Twine(2));
  149. Builder.defineMacro("__mips_dspr2", Twine(1));
  150. Builder.defineMacro("__mips_dsp", Twine(1));
  151. break;
  152. }
  153. if (HasMSA)
  154. Builder.defineMacro("__mips_msa", Twine(1));
  155. if (DisableMadd4)
  156. Builder.defineMacro("__mips_no_madd4", Twine(1));
  157. Builder.defineMacro("_MIPS_SZPTR", Twine(getPointerWidth(0)));
  158. Builder.defineMacro("_MIPS_SZINT", Twine(getIntWidth()));
  159. Builder.defineMacro("_MIPS_SZLONG", Twine(getLongWidth()));
  160. Builder.defineMacro("_MIPS_ARCH", "\"" + CPU + "\"");
  161. if (CPU == "octeon+")
  162. Builder.defineMacro("_MIPS_ARCH_OCTEONP");
  163. else
  164. Builder.defineMacro("_MIPS_ARCH_" + StringRef(CPU).upper());
  165. if (StringRef(CPU).startswith("octeon"))
  166. Builder.defineMacro("__OCTEON__");
  167. // These shouldn't be defined for MIPS-I but there's no need to check
  168. // for that since MIPS-I isn't supported.
  169. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
  170. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
  171. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
  172. // 32-bit MIPS processors don't have the necessary lld/scd instructions
  173. // found in 64-bit processors. In the case of O32 on a 64-bit processor,
  174. // the instructions exist but using them violates the ABI since they
  175. // require 64-bit GPRs and O32 only supports 32-bit GPRs.
  176. if (ABI == "n32" || ABI == "n64")
  177. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
  178. }
  179. bool MipsTargetInfo::hasFeature(StringRef Feature) const {
  180. return llvm::StringSwitch<bool>(Feature)
  181. .Case("mips", true)
  182. .Case("dsp", DspRev >= DSP1)
  183. .Case("dspr2", DspRev >= DSP2)
  184. .Case("fp64", FPMode == FP64)
  185. .Case("msa", HasMSA)
  186. .Default(false);
  187. }
  188. ArrayRef<Builtin::Info> MipsTargetInfo::getTargetBuiltins() const {
  189. return llvm::makeArrayRef(BuiltinInfo, clang::Mips::LastTSBuiltin -
  190. Builtin::FirstTSBuiltin);
  191. }
  192. unsigned MipsTargetInfo::getUnwindWordWidth() const {
  193. return llvm::StringSwitch<unsigned>(ABI)
  194. .Case("o32", 32)
  195. .Case("n32", 64)
  196. .Case("n64", 64)
  197. .Default(getPointerWidth(0));
  198. }
  199. bool MipsTargetInfo::validateTarget(DiagnosticsEngine &Diags) const {
  200. // microMIPS64R6 backend was removed.
  201. if (getTriple().isMIPS64() && IsMicromips && (ABI == "n32" || ABI == "n64")) {
  202. Diags.Report(diag::err_target_unsupported_cpu_for_micromips) << CPU;
  203. return false;
  204. }
  205. // FIXME: It's valid to use O32 on a 64-bit CPU but the backend can't handle
  206. // this yet. It's better to fail here than on the backend assertion.
  207. if (processorSupportsGPR64() && ABI == "o32") {
  208. Diags.Report(diag::err_target_unsupported_abi) << ABI << CPU;
  209. return false;
  210. }
  211. // 64-bit ABI's require 64-bit CPU's.
  212. if (!processorSupportsGPR64() && (ABI == "n32" || ABI == "n64")) {
  213. Diags.Report(diag::err_target_unsupported_abi) << ABI << CPU;
  214. return false;
  215. }
  216. // FIXME: It's valid to use O32 on a mips64/mips64el triple but the backend
  217. // can't handle this yet. It's better to fail here than on the
  218. // backend assertion.
  219. if (getTriple().isMIPS64() && ABI == "o32") {
  220. Diags.Report(diag::err_target_unsupported_abi_for_triple)
  221. << ABI << getTriple().str();
  222. return false;
  223. }
  224. // FIXME: It's valid to use N32/N64 on a mips/mipsel triple but the backend
  225. // can't handle this yet. It's better to fail here than on the
  226. // backend assertion.
  227. if (getTriple().isMIPS32() && (ABI == "n32" || ABI == "n64")) {
  228. Diags.Report(diag::err_target_unsupported_abi_for_triple)
  229. << ABI << getTriple().str();
  230. return false;
  231. }
  232. // -fpxx is valid only for the o32 ABI
  233. if (FPMode == FPXX && (ABI == "n32" || ABI == "n64")) {
  234. Diags.Report(diag::err_unsupported_abi_for_opt) << "-mfpxx" << "o32";
  235. return false;
  236. }
  237. // -mfp32 and n32/n64 ABIs are incompatible
  238. if (FPMode != FP64 && FPMode != FPXX && !IsSingleFloat &&
  239. (ABI == "n32" || ABI == "n64")) {
  240. Diags.Report(diag::err_opt_not_valid_with_opt) << "-mfpxx" << CPU;
  241. return false;
  242. }
  243. // Mips revision 6 and -mfp32 are incompatible
  244. if (FPMode != FP64 && FPMode != FPXX && (CPU == "mips32r6" ||
  245. CPU == "mips64r6")) {
  246. Diags.Report(diag::err_opt_not_valid_with_opt) << "-mfp32" << CPU;
  247. return false;
  248. }
  249. // Option -mfp64 permitted on Mips32 iff revision 2 or higher is present
  250. if (FPMode == FP64 && (CPU == "mips1" || CPU == "mips2" ||
  251. getISARev() < 2) && ABI == "o32") {
  252. Diags.Report(diag::err_mips_fp64_req) << "-mfp64";
  253. return false;
  254. }
  255. return true;
  256. }