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"
16 #include "llvm/Support/Host.h"
18 using namespace clang::driver;
19 using namespace clang::driver::tools;
20 using namespace clang;
21 using namespace llvm::opt;
23 /// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting.
24 std::string ppc::getPPCTargetCPU(const ArgList &Args) {
25 if (Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ)) {
26 StringRef CPUName = A->getValue();
28 if (CPUName == "native") {
29 std::string CPU = std::string(llvm::sys::getHostCPUName());
30 if (!CPU.empty() && CPU != "generic")
36 return llvm::StringSwitch<const char *>(CPUName)
37 .Case("common", "generic")
45 .Case("603ev", "603ev")
62 .Case("e500mc", "e500mc")
63 .Case("e5500", "e5500")
64 .Case("power3", "pwr3")
65 .Case("power4", "pwr4")
66 .Case("power5", "pwr5")
67 .Case("power5x", "pwr5x")
68 .Case("power6", "pwr6")
69 .Case("power6x", "pwr6x")
70 .Case("power7", "pwr7")
71 .Case("power8", "pwr8")
72 .Case("power9", "pwr9")
73 .Case("power10", "pwr10")
74 .Case("future", "future")
78 .Case("pwr5x", "pwr5x")
80 .Case("pwr6x", "pwr6x")
84 .Case("pwr10", "pwr10")
85 .Case("powerpc", "ppc")
86 .Case("powerpc64", "ppc64")
87 .Case("powerpc64le", "ppc64le")
94 const char *ppc::getPPCAsmModeForCPU(StringRef Name) {
95 return llvm::StringSwitch<const char *>(Name)
96 .Case("pwr7", "-mpower7")
97 .Case("power7", "-mpower7")
98 .Case("pwr8", "-mpower8")
99 .Case("power8", "-mpower8")
100 .Case("ppc64le", "-mpower8")
101 .Case("pwr9", "-mpower9")
102 .Case("power9", "-mpower9")
103 .Case("pwr10", "-mpower10")
104 .Case("power10", "-mpower10")
108 void ppc::getPPCTargetFeatures(const Driver &D, const llvm::Triple &Triple,
110 std::vector<StringRef> &Features) {
111 if (Triple.getSubArch() == llvm::Triple::PPCSubArch_spe)
112 Features.push_back("+spe");
114 handleTargetFeaturesGroup(Args, Features, options::OPT_m_ppc_Features_Group);
116 ppc::FloatABI FloatABI = ppc::getPPCFloatABI(D, Args);
117 if (FloatABI == ppc::FloatABI::Soft)
118 Features.push_back("-hard-float");
120 ppc::ReadGOTPtrMode ReadGOT = ppc::getPPCReadGOTPtrMode(D, Triple, Args);
121 if (ReadGOT == ppc::ReadGOTPtrMode::SecurePlt)
122 Features.push_back("+secure-plt");
125 ppc::ReadGOTPtrMode ppc::getPPCReadGOTPtrMode(const Driver &D, const llvm::Triple &Triple,
126 const ArgList &Args) {
127 if (Args.getLastArg(options::OPT_msecure_plt))
128 return ppc::ReadGOTPtrMode::SecurePlt;
129 if ((Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 13) ||
130 Triple.isOSNetBSD() || Triple.isOSOpenBSD() || Triple.isMusl())
131 return ppc::ReadGOTPtrMode::SecurePlt;
133 return ppc::ReadGOTPtrMode::Bss;
136 ppc::FloatABI ppc::getPPCFloatABI(const Driver &D, const ArgList &Args) {
137 ppc::FloatABI ABI = ppc::FloatABI::Invalid;
139 Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
140 options::OPT_mfloat_abi_EQ)) {
141 if (A->getOption().matches(options::OPT_msoft_float))
142 ABI = ppc::FloatABI::Soft;
143 else if (A->getOption().matches(options::OPT_mhard_float))
144 ABI = ppc::FloatABI::Hard;
146 ABI = llvm::StringSwitch<ppc::FloatABI>(A->getValue())
147 .Case("soft", ppc::FloatABI::Soft)
148 .Case("hard", ppc::FloatABI::Hard)
149 .Default(ppc::FloatABI::Invalid);
150 if (ABI == ppc::FloatABI::Invalid && !StringRef(A->getValue()).empty()) {
151 D.Diag(clang::diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
152 ABI = ppc::FloatABI::Hard;
157 // If unspecified, choose the default based on the platform.
158 if (ABI == ppc::FloatABI::Invalid) {
159 ABI = ppc::FloatABI::Hard;
165 bool ppc::hasPPCAbiArg(const ArgList &Args, const char *Value) {
166 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
167 return A && (A->getValue() == StringRef(Value));