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