1//===--- Sparc.cpp - Tools Implementations ----------------------*- 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 "Sparc.h"
10#include "clang/Driver/Driver.h"
11#include "clang/Options/Options.h"
12#include "llvm/ADT/StringSwitch.h"
13#include "llvm/Option/ArgList.h"
14#include "llvm/TargetParser/Host.h"
15
16using namespace clang::driver;
17using namespace clang::driver::tools;
18using namespace clang;
19using namespace llvm::opt;
20
21const char *sparc::getSparcAsmModeForCPU(StringRef Name,
22 const llvm::Triple &Triple) {
23 if (Triple.getArch() == llvm::Triple::sparcv9) {
24 const char *DefV9CPU;
25
26 if (Triple.isOSSolaris())
27 DefV9CPU = "-Av9b";
28 else if (Triple.isOSLinux() || Triple.isOSFreeBSD() || Triple.isOSOpenBSD())
29 DefV9CPU = "-Av9a";
30 else
31 DefV9CPU = "-Av9";
32
33 return llvm::StringSwitch<const char *>(Name)
34 .Case(S: "niagara", Value: "-Av9b")
35 .Case(S: "niagara2", Value: "-Av9b")
36 .Case(S: "niagara3", Value: "-Av9d")
37 .Case(S: "niagara4", Value: "-Av9d")
38 .Default(Value: DefV9CPU);
39 } else {
40 const char *DefV8CPU;
41
42 if (Triple.isOSSolaris())
43 DefV8CPU = "-Av8plus";
44 else
45 DefV8CPU = "-Av8";
46
47 return llvm::StringSwitch<const char *>(Name)
48 .Case(S: "v8", Value: "-Av8")
49 .Case(S: "supersparc", Value: "-Av8")
50 .Case(S: "sparclite", Value: "-Asparclite")
51 .Case(S: "f934", Value: "-Asparclite")
52 .Case(S: "hypersparc", Value: "-Av8")
53 .Case(S: "sparclite86x", Value: "-Asparclite")
54 .Case(S: "sparclet", Value: "-Asparclet")
55 .Case(S: "tsc701", Value: "-Asparclet")
56 .Case(S: "v9", Value: "-Av8plus")
57 .Case(S: "ultrasparc", Value: "-Av8plus")
58 .Case(S: "ultrasparc3", Value: "-Av8plus")
59 .Case(S: "niagara", Value: "-Av8plusb")
60 .Case(S: "niagara2", Value: "-Av8plusb")
61 .Case(S: "niagara3", Value: "-Av8plusd")
62 .Case(S: "niagara4", Value: "-Av8plusd")
63 .Case(S: "ma2100", Value: "-Aleon")
64 .Case(S: "ma2150", Value: "-Aleon")
65 .Case(S: "ma2155", Value: "-Aleon")
66 .Case(S: "ma2450", Value: "-Aleon")
67 .Case(S: "ma2455", Value: "-Aleon")
68 .Case(S: "ma2x5x", Value: "-Aleon")
69 .Case(S: "ma2080", Value: "-Aleon")
70 .Case(S: "ma2085", Value: "-Aleon")
71 .Case(S: "ma2480", Value: "-Aleon")
72 .Case(S: "ma2485", Value: "-Aleon")
73 .Case(S: "ma2x8x", Value: "-Aleon")
74 .Case(S: "leon2", Value: "-Av8")
75 .Case(S: "at697e", Value: "-Av8")
76 .Case(S: "at697f", Value: "-Av8")
77 .Case(S: "leon3", Value: "-Aleon")
78 .Case(S: "ut699", Value: "-Av8")
79 .Case(S: "gr712rc", Value: "-Aleon")
80 .Case(S: "leon4", Value: "-Aleon")
81 .Case(S: "gr740", Value: "-Aleon")
82 .Default(Value: DefV8CPU);
83 }
84}
85
86sparc::FloatABI sparc::getSparcFloatABI(const Driver &D,
87 const ArgList &Args) {
88 sparc::FloatABI ABI = sparc::FloatABI::Invalid;
89 if (Arg *A = Args.getLastArg(Ids: options::OPT_msoft_float, Ids: options::OPT_mno_fpu,
90 Ids: options::OPT_mhard_float, Ids: options::OPT_mfpu,
91 Ids: options::OPT_mfloat_abi_EQ)) {
92 if (A->getOption().matches(ID: options::OPT_msoft_float) ||
93 A->getOption().matches(ID: options::OPT_mno_fpu))
94 ABI = sparc::FloatABI::Soft;
95 else if (A->getOption().matches(ID: options::OPT_mhard_float) ||
96 A->getOption().matches(ID: options::OPT_mfpu))
97 ABI = sparc::FloatABI::Hard;
98 else {
99 ABI = llvm::StringSwitch<sparc::FloatABI>(A->getValue())
100 .Case(S: "soft", Value: sparc::FloatABI::Soft)
101 .Case(S: "hard", Value: sparc::FloatABI::Hard)
102 .Default(Value: sparc::FloatABI::Invalid);
103 if (ABI == sparc::FloatABI::Invalid &&
104 !StringRef(A->getValue()).empty()) {
105 D.Diag(DiagID: clang::diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
106 ABI = sparc::FloatABI::Hard;
107 }
108 }
109 }
110
111 // If unspecified, choose the default based on the platform.
112 // Only the hard-float ABI on Sparc is standardized, and it is the
113 // default. GCC also supports a nonstandard soft-float ABI mode, also
114 // implemented in LLVM. However as this is not standard we set the default
115 // to be hard-float.
116 if (ABI == sparc::FloatABI::Invalid) {
117 ABI = sparc::FloatABI::Hard;
118 }
119
120 return ABI;
121}
122
123std::string sparc::getSparcTargetCPU(const Driver &D, const ArgList &Args,
124 const llvm::Triple &Triple) {
125 if (const Arg *A = Args.getLastArg(Ids: options::OPT_mcpu_EQ)) {
126 StringRef CPUName = A->getValue();
127 if (CPUName == "native") {
128 std::string CPU = std::string(llvm::sys::getHostCPUName());
129 if (!CPU.empty() && CPU != "generic")
130 return CPU;
131 return "";
132 }
133 return std::string(CPUName);
134 }
135
136 if (Triple.getArch() == llvm::Triple::sparc &&
137 (Triple.isOSSolaris() || Triple.isOSLinux()))
138 return "v9";
139 return "";
140}
141
142void sparc::getSparcTargetFeatures(const Driver &D, const llvm::Triple &Triple,
143 const ArgList &Args,
144 std::vector<StringRef> &Features) {
145 sparc::FloatABI FloatABI = sparc::getSparcFloatABI(D, Args);
146 if (FloatABI == sparc::FloatABI::Soft)
147 Features.push_back(x: "+soft-float");
148
149 if (Arg *A = Args.getLastArg(Ids: options::OPT_mfsmuld, Ids: options::OPT_mno_fsmuld)) {
150 if (A->getOption().matches(ID: options::OPT_mfsmuld))
151 Features.push_back(x: "+fsmuld");
152 else
153 Features.push_back(x: "-fsmuld");
154 }
155
156 if (Arg *A = Args.getLastArg(Ids: options::OPT_mpopc, Ids: options::OPT_mno_popc)) {
157 if (A->getOption().matches(ID: options::OPT_mpopc))
158 Features.push_back(x: "+popc");
159 else
160 Features.push_back(x: "-popc");
161 }
162
163 // Those OSes default to enabling VIS on 64-bit SPARC.
164 // See also the corresponding code for external assemblers in
165 // sparc::getSparcAsmModeForCPU().
166 bool IsSparcV9ATarget =
167 (Triple.getArch() == llvm::Triple::sparcv9) &&
168 (Triple.isOSLinux() || Triple.isOSFreeBSD() || Triple.isOSOpenBSD());
169 bool IsSparcV9BTarget = Triple.isOSSolaris();
170 bool IsSparcV8PlusTarget =
171 Triple.getArch() == llvm::Triple::sparc && Triple.isOSSolaris();
172 if (Arg *A = Args.getLastArg(Ids: options::OPT_mvis, Ids: options::OPT_mno_vis)) {
173 if (A->getOption().matches(ID: options::OPT_mvis))
174 Features.push_back(x: "+vis");
175 else
176 Features.push_back(x: "-vis");
177 } else if (IsSparcV9ATarget) {
178 Features.push_back(x: "+vis");
179 }
180
181 if (Arg *A = Args.getLastArg(Ids: options::OPT_mvis2, Ids: options::OPT_mno_vis2)) {
182 if (A->getOption().matches(ID: options::OPT_mvis2))
183 Features.push_back(x: "+vis2");
184 else
185 Features.push_back(x: "-vis2");
186 } else if (IsSparcV9BTarget) {
187 Features.push_back(x: "+vis2");
188 }
189
190 if (Arg *A = Args.getLastArg(Ids: options::OPT_mvis3, Ids: options::OPT_mno_vis3)) {
191 if (A->getOption().matches(ID: options::OPT_mvis3))
192 Features.push_back(x: "+vis3");
193 else
194 Features.push_back(x: "-vis3");
195 }
196
197 if (Arg *A = Args.getLastArg(Ids: options::OPT_mhard_quad_float,
198 Ids: options::OPT_msoft_quad_float)) {
199 if (A->getOption().matches(ID: options::OPT_mhard_quad_float))
200 Features.push_back(x: "+hard-quad-float");
201 else
202 Features.push_back(x: "-hard-quad-float");
203 }
204
205 if (Arg *A = Args.getLastArg(Ids: options::OPT_mv8plus, Ids: options::OPT_mno_v8plus)) {
206 if (A->getOption().matches(ID: options::OPT_mv8plus))
207 Features.push_back(x: "+v8plus");
208 } else if (IsSparcV8PlusTarget) {
209 Features.push_back(x: "+v8plus");
210 }
211
212 if (Args.hasArg(Ids: options::OPT_ffixed_g1))
213 Features.push_back(x: "+reserve-g1");
214
215 if (Args.hasArg(Ids: options::OPT_ffixed_g2))
216 Features.push_back(x: "+reserve-g2");
217
218 if (Args.hasArg(Ids: options::OPT_ffixed_g3))
219 Features.push_back(x: "+reserve-g3");
220
221 if (Args.hasArg(Ids: options::OPT_ffixed_g4))
222 Features.push_back(x: "+reserve-g4");
223
224 if (Args.hasArg(Ids: options::OPT_ffixed_g5))
225 Features.push_back(x: "+reserve-g5");
226
227 if (Args.hasArg(Ids: options::OPT_ffixed_g6))
228 Features.push_back(x: "+reserve-g6");
229
230 if (Args.hasArg(Ids: options::OPT_ffixed_g7))
231 Features.push_back(x: "+reserve-g7");
232
233 if (Args.hasArg(Ids: options::OPT_ffixed_o0))
234 Features.push_back(x: "+reserve-o0");
235
236 if (Args.hasArg(Ids: options::OPT_ffixed_o1))
237 Features.push_back(x: "+reserve-o1");
238
239 if (Args.hasArg(Ids: options::OPT_ffixed_o2))
240 Features.push_back(x: "+reserve-o2");
241
242 if (Args.hasArg(Ids: options::OPT_ffixed_o3))
243 Features.push_back(x: "+reserve-o3");
244
245 if (Args.hasArg(Ids: options::OPT_ffixed_o4))
246 Features.push_back(x: "+reserve-o4");
247
248 if (Args.hasArg(Ids: options::OPT_ffixed_o5))
249 Features.push_back(x: "+reserve-o5");
250
251 if (Args.hasArg(Ids: options::OPT_ffixed_l0))
252 Features.push_back(x: "+reserve-l0");
253
254 if (Args.hasArg(Ids: options::OPT_ffixed_l1))
255 Features.push_back(x: "+reserve-l1");
256
257 if (Args.hasArg(Ids: options::OPT_ffixed_l2))
258 Features.push_back(x: "+reserve-l2");
259
260 if (Args.hasArg(Ids: options::OPT_ffixed_l3))
261 Features.push_back(x: "+reserve-l3");
262
263 if (Args.hasArg(Ids: options::OPT_ffixed_l4))
264 Features.push_back(x: "+reserve-l4");
265
266 if (Args.hasArg(Ids: options::OPT_ffixed_l5))
267 Features.push_back(x: "+reserve-l5");
268
269 if (Args.hasArg(Ids: options::OPT_ffixed_l6))
270 Features.push_back(x: "+reserve-l6");
271
272 if (Args.hasArg(Ids: options::OPT_ffixed_l7))
273 Features.push_back(x: "+reserve-l7");
274
275 if (Args.hasArg(Ids: options::OPT_ffixed_i0))
276 Features.push_back(x: "+reserve-i0");
277
278 if (Args.hasArg(Ids: options::OPT_ffixed_i1))
279 Features.push_back(x: "+reserve-i1");
280
281 if (Args.hasArg(Ids: options::OPT_ffixed_i2))
282 Features.push_back(x: "+reserve-i2");
283
284 if (Args.hasArg(Ids: options::OPT_ffixed_i3))
285 Features.push_back(x: "+reserve-i3");
286
287 if (Args.hasArg(Ids: options::OPT_ffixed_i4))
288 Features.push_back(x: "+reserve-i4");
289
290 if (Args.hasArg(Ids: options::OPT_ffixed_i5))
291 Features.push_back(x: "+reserve-i5");
292
293 if (Args.hasArg(Ids: options::OPT_mfix_gr712rc)) {
294 Features.push_back(x: "+fix-tn0009");
295 Features.push_back(x: "+fix-tn0011");
296 Features.push_back(x: "+fix-tn0012");
297 Features.push_back(x: "+fix-tn0013");
298 }
299
300 if (Args.hasArg(Ids: options::OPT_mfix_ut700)) {
301 Features.push_back(x: "+fix-tn0009");
302 Features.push_back(x: "+fix-tn0010");
303 Features.push_back(x: "+fix-tn0013");
304 }
305}
306