PPC.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //===--- PPC.cpp - PPC Helpers for Tools ------------------------*- 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. #include "PPC.h"
  9. #include "ToolChains/CommonArgs.h"
  10. #include "clang/Driver/Driver.h"
  11. #include "clang/Driver/DriverDiagnostic.h"
  12. #include "clang/Driver/Options.h"
  13. #include "llvm/ADT/StringSwitch.h"
  14. #include "llvm/Option/ArgList.h"
  15. #include "llvm/Support/Host.h"
  16. using namespace clang::driver;
  17. using namespace clang::driver::tools;
  18. using namespace clang;
  19. using namespace llvm::opt;
  20. /// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting.
  21. std::string ppc::getPPCTargetCPU(const ArgList &Args) {
  22. if (Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ)) {
  23. StringRef CPUName = A->getValue();
  24. if (CPUName == "native") {
  25. std::string CPU = std::string(llvm::sys::getHostCPUName());
  26. if (!CPU.empty() && CPU != "generic")
  27. return CPU;
  28. else
  29. return "";
  30. }
  31. return llvm::StringSwitch<const char *>(CPUName)
  32. .Case("common", "generic")
  33. .Case("440", "440")
  34. .Case("440fp", "440")
  35. .Case("450", "450")
  36. .Case("601", "601")
  37. .Case("602", "602")
  38. .Case("603", "603")
  39. .Case("603e", "603e")
  40. .Case("603ev", "603ev")
  41. .Case("604", "604")
  42. .Case("604e", "604e")
  43. .Case("620", "620")
  44. .Case("630", "pwr3")
  45. .Case("G3", "g3")
  46. .Case("7400", "7400")
  47. .Case("G4", "g4")
  48. .Case("7450", "7450")
  49. .Case("G4+", "g4+")
  50. .Case("750", "750")
  51. .Case("8548", "e500")
  52. .Case("970", "970")
  53. .Case("G5", "g5")
  54. .Case("a2", "a2")
  55. .Case("e500", "e500")
  56. .Case("e500mc", "e500mc")
  57. .Case("e5500", "e5500")
  58. .Case("power3", "pwr3")
  59. .Case("power4", "pwr4")
  60. .Case("power5", "pwr5")
  61. .Case("power5x", "pwr5x")
  62. .Case("power6", "pwr6")
  63. .Case("power6x", "pwr6x")
  64. .Case("power7", "pwr7")
  65. .Case("power8", "pwr8")
  66. .Case("power9", "pwr9")
  67. .Case("power10", "pwr10")
  68. .Case("future", "future")
  69. .Case("pwr3", "pwr3")
  70. .Case("pwr4", "pwr4")
  71. .Case("pwr5", "pwr5")
  72. .Case("pwr5x", "pwr5x")
  73. .Case("pwr6", "pwr6")
  74. .Case("pwr6x", "pwr6x")
  75. .Case("pwr7", "pwr7")
  76. .Case("pwr8", "pwr8")
  77. .Case("pwr9", "pwr9")
  78. .Case("pwr10", "pwr10")
  79. .Case("powerpc", "ppc")
  80. .Case("powerpc64", "ppc64")
  81. .Case("powerpc64le", "ppc64le")
  82. .Default("");
  83. }
  84. return "";
  85. }
  86. const char *ppc::getPPCAsmModeForCPU(StringRef Name) {
  87. return llvm::StringSwitch<const char *>(Name)
  88. .Case("pwr7", "-mpower7")
  89. .Case("power7", "-mpower7")
  90. .Case("pwr8", "-mpower8")
  91. .Case("power8", "-mpower8")
  92. .Case("ppc64le", "-mpower8")
  93. .Case("pwr9", "-mpower9")
  94. .Case("power9", "-mpower9")
  95. .Case("pwr10", "-mpower10")
  96. .Case("power10", "-mpower10")
  97. .Default("-many");
  98. }
  99. void ppc::getPPCTargetFeatures(const Driver &D, const llvm::Triple &Triple,
  100. const ArgList &Args,
  101. std::vector<StringRef> &Features) {
  102. if (Triple.getSubArch() == llvm::Triple::PPCSubArch_spe)
  103. Features.push_back("+spe");
  104. handleTargetFeaturesGroup(Args, Features, options::OPT_m_ppc_Features_Group);
  105. ppc::FloatABI FloatABI = ppc::getPPCFloatABI(D, Args);
  106. if (FloatABI == ppc::FloatABI::Soft)
  107. Features.push_back("-hard-float");
  108. ppc::ReadGOTPtrMode ReadGOT = ppc::getPPCReadGOTPtrMode(D, Triple, Args);
  109. if (ReadGOT == ppc::ReadGOTPtrMode::SecurePlt)
  110. Features.push_back("+secure-plt");
  111. }
  112. ppc::ReadGOTPtrMode ppc::getPPCReadGOTPtrMode(const Driver &D, const llvm::Triple &Triple,
  113. const ArgList &Args) {
  114. if (Args.getLastArg(options::OPT_msecure_plt))
  115. return ppc::ReadGOTPtrMode::SecurePlt;
  116. if ((Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 13) ||
  117. Triple.isOSNetBSD() || Triple.isOSOpenBSD() || Triple.isMusl())
  118. return ppc::ReadGOTPtrMode::SecurePlt;
  119. else
  120. return ppc::ReadGOTPtrMode::Bss;
  121. }
  122. ppc::FloatABI ppc::getPPCFloatABI(const Driver &D, const ArgList &Args) {
  123. ppc::FloatABI ABI = ppc::FloatABI::Invalid;
  124. if (Arg *A =
  125. Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
  126. options::OPT_mfloat_abi_EQ)) {
  127. if (A->getOption().matches(options::OPT_msoft_float))
  128. ABI = ppc::FloatABI::Soft;
  129. else if (A->getOption().matches(options::OPT_mhard_float))
  130. ABI = ppc::FloatABI::Hard;
  131. else {
  132. ABI = llvm::StringSwitch<ppc::FloatABI>(A->getValue())
  133. .Case("soft", ppc::FloatABI::Soft)
  134. .Case("hard", ppc::FloatABI::Hard)
  135. .Default(ppc::FloatABI::Invalid);
  136. if (ABI == ppc::FloatABI::Invalid && !StringRef(A->getValue()).empty()) {
  137. D.Diag(clang::diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
  138. ABI = ppc::FloatABI::Hard;
  139. }
  140. }
  141. }
  142. // If unspecified, choose the default based on the platform.
  143. if (ABI == ppc::FloatABI::Invalid) {
  144. ABI = ppc::FloatABI::Hard;
  145. }
  146. return ABI;
  147. }
  148. bool ppc::hasPPCAbiArg(const ArgList &Args, const char *Value) {
  149. Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
  150. return A && (A->getValue() == StringRef(Value));
  151. }