1 //===--- PPC.cpp - PPC Helpers for Tools ------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 #include "ToolChains/CommonArgs.h"
11 #include "clang/Driver/Driver.h"
12 #include "clang/Driver/DriverDiagnostic.h"
13 #include "clang/Driver/Options.h"
14 #include "llvm/ADT/StringSwitch.h"
15 #include "llvm/Option/ArgList.h"
17 using namespace clang::driver;
18 using namespace clang::driver::tools;
19 using namespace clang;
20 using namespace llvm::opt;
22 /// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting.
23 std::string ppc::getPPCTargetCPU(const ArgList &Args) {
24 if (Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ)) {
25 StringRef CPUName = A->getValue();
27 if (CPUName == "native") {
28 std::string CPU = llvm::sys::getHostCPUName();
29 if (!CPU.empty() && CPU != "generic")
35 return llvm::StringSwitch<const char *>(CPUName)
36 .Case("common", "generic")
44 .Case("603ev", "603ev")
61 .Case("e500mc", "e500mc")
62 .Case("e5500", "e5500")
63 .Case("power3", "pwr3")
64 .Case("power4", "pwr4")
65 .Case("power5", "pwr5")
66 .Case("power5x", "pwr5x")
67 .Case("power6", "pwr6")
68 .Case("power6x", "pwr6x")
69 .Case("power7", "pwr7")
70 .Case("power8", "pwr8")
71 .Case("power9", "pwr9")
75 .Case("pwr5x", "pwr5x")
77 .Case("pwr6x", "pwr6x")
81 .Case("powerpc", "ppc")
82 .Case("powerpc64", "ppc64")
83 .Case("powerpc64le", "ppc64le")
90 const char *ppc::getPPCAsmModeForCPU(StringRef Name) {
91 return llvm::StringSwitch<const char *>(Name)
92 .Case("pwr7", "-mpower7")
93 .Case("power7", "-mpower7")
94 .Case("pwr8", "-mpower8")
95 .Case("power8", "-mpower8")
96 .Case("ppc64le", "-mpower8")
97 .Case("pwr9", "-mpower9")
98 .Case("power9", "-mpower9")
102 void ppc::getPPCTargetFeatures(const Driver &D, const llvm::Triple &Triple,
104 std::vector<StringRef> &Features) {
105 if (Triple.getSubArch() == llvm::Triple::PPCSubArch_spe)
106 Features.push_back("+spe");
108 handleTargetFeaturesGroup(Args, Features, options::OPT_m_ppc_Features_Group);
110 ppc::FloatABI FloatABI = ppc::getPPCFloatABI(D, Args);
111 if (FloatABI == ppc::FloatABI::Soft)
112 Features.push_back("-hard-float");
114 ppc::ReadGOTPtrMode ReadGOT = ppc::getPPCReadGOTPtrMode(D, Triple, Args);
115 if (ReadGOT == ppc::ReadGOTPtrMode::SecurePlt)
116 Features.push_back("+secure-plt");
119 ppc::ReadGOTPtrMode ppc::getPPCReadGOTPtrMode(const Driver &D, const llvm::Triple &Triple,
120 const ArgList &Args) {
121 if (Args.getLastArg(options::OPT_msecure_plt))
122 return ppc::ReadGOTPtrMode::SecurePlt;
123 if ((Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 13) ||
124 Triple.isOSNetBSD() || Triple.isOSOpenBSD() || Triple.isMusl())
125 return ppc::ReadGOTPtrMode::SecurePlt;
127 return ppc::ReadGOTPtrMode::Bss;
130 ppc::FloatABI ppc::getPPCFloatABI(const Driver &D, const ArgList &Args) {
131 ppc::FloatABI ABI = ppc::FloatABI::Invalid;
133 Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
134 options::OPT_mfloat_abi_EQ)) {
135 if (A->getOption().matches(options::OPT_msoft_float))
136 ABI = ppc::FloatABI::Soft;
137 else if (A->getOption().matches(options::OPT_mhard_float))
138 ABI = ppc::FloatABI::Hard;
140 ABI = llvm::StringSwitch<ppc::FloatABI>(A->getValue())
141 .Case("soft", ppc::FloatABI::Soft)
142 .Case("hard", ppc::FloatABI::Hard)
143 .Default(ppc::FloatABI::Invalid);
144 if (ABI == ppc::FloatABI::Invalid && !StringRef(A->getValue()).empty()) {
145 D.Diag(clang::diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
146 ABI = ppc::FloatABI::Hard;
151 // If unspecified, choose the default based on the platform.
152 if (ABI == ppc::FloatABI::Invalid) {
153 ABI = ppc::FloatABI::Hard;
159 bool ppc::hasPPCAbiArg(const ArgList &Args, const char *Value) {
160 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
161 return A && (A->getValue() == StringRef(Value));