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/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 
17 using namespace clang::driver;
18 using namespace clang::driver::tools;
19 using namespace clang;
20 using namespace llvm::opt;
21 
22 const char *sparc::getSparcAsmModeForCPU(StringRef Name,
23                                          const llvm::Triple &Triple) {
24   if (Triple.getArch() == llvm::Triple::sparcv9) {
25     const char *DefV9CPU;
26 
27     if (Triple.isOSLinux() || Triple.isOSFreeBSD() || Triple.isOSOpenBSD())
28       DefV9CPU = "-Av9a";
29     else
30       DefV9CPU = "-Av9";
31 
32     return llvm::StringSwitch<const char *>(Name)
33         .Case("niagara", "-Av9b")
34         .Case("niagara2", "-Av9b")
35         .Case("niagara3", "-Av9d")
36         .Case("niagara4", "-Av9d")
37         .Default(DefV9CPU);
38   } else {
39     return llvm::StringSwitch<const char *>(Name)
40         .Case("v8", "-Av8")
41         .Case("supersparc", "-Av8")
42         .Case("sparclite", "-Asparclite")
43         .Case("f934", "-Asparclite")
44         .Case("hypersparc", "-Av8")
45         .Case("sparclite86x", "-Asparclite")
46         .Case("sparclet", "-Asparclet")
47         .Case("tsc701", "-Asparclet")
48         .Case("v9", "-Av8plus")
49         .Case("ultrasparc", "-Av8plus")
50         .Case("ultrasparc3", "-Av8plus")
51         .Case("niagara", "-Av8plusb")
52         .Case("niagara2", "-Av8plusb")
53         .Case("niagara3", "-Av8plusd")
54         .Case("niagara4", "-Av8plusd")
55         .Case("ma2100", "-Aleon")
56         .Case("ma2150", "-Aleon")
57         .Case("ma2155", "-Aleon")
58         .Case("ma2450", "-Aleon")
59         .Case("ma2455", "-Aleon")
60         .Case("ma2x5x", "-Aleon")
61         .Case("ma2080", "-Aleon")
62         .Case("ma2085", "-Aleon")
63         .Case("ma2480", "-Aleon")
64         .Case("ma2485", "-Aleon")
65         .Case("ma2x8x", "-Aleon")
66         .Case("myriad2", "-Aleon")
67         .Case("myriad2.1", "-Aleon")
68         .Case("myriad2.2", "-Aleon")
69         .Case("myriad2.3", "-Aleon")
70         .Case("leon2", "-Av8")
71         .Case("at697e", "-Av8")
72         .Case("at697f", "-Av8")
73         .Case("leon3", "-Aleon")
74         .Case("ut699", "-Av8")
75         .Case("gr712rc", "-Aleon")
76         .Case("leon4", "-Aleon")
77         .Case("gr740", "-Aleon")
78         .Default("-Av8");
79   }
80 }
81 
82 sparc::FloatABI sparc::getSparcFloatABI(const Driver &D,
83                                         const ArgList &Args) {
84   sparc::FloatABI ABI = sparc::FloatABI::Invalid;
85   if (Arg *A = Args.getLastArg(options::OPT_msoft_float, options::OPT_mno_fpu,
86                                options::OPT_mhard_float, options::OPT_mfpu,
87                                options::OPT_mfloat_abi_EQ)) {
88     if (A->getOption().matches(options::OPT_msoft_float) ||
89         A->getOption().matches(options::OPT_mno_fpu))
90       ABI = sparc::FloatABI::Soft;
91     else if (A->getOption().matches(options::OPT_mhard_float) ||
92              A->getOption().matches(options::OPT_mfpu))
93       ABI = sparc::FloatABI::Hard;
94     else {
95       ABI = llvm::StringSwitch<sparc::FloatABI>(A->getValue())
96                 .Case("soft", sparc::FloatABI::Soft)
97                 .Case("hard", sparc::FloatABI::Hard)
98                 .Default(sparc::FloatABI::Invalid);
99       if (ABI == sparc::FloatABI::Invalid &&
100           !StringRef(A->getValue()).empty()) {
101         D.Diag(clang::diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
102         ABI = sparc::FloatABI::Hard;
103       }
104     }
105   }
106 
107   // If unspecified, choose the default based on the platform.
108   // Only the hard-float ABI on Sparc is standardized, and it is the
109   // default. GCC also supports a nonstandard soft-float ABI mode, also
110   // implemented in LLVM. However as this is not standard we set the default
111   // to be hard-float.
112   if (ABI == sparc::FloatABI::Invalid) {
113     ABI = sparc::FloatABI::Hard;
114   }
115 
116   return ABI;
117 }
118 
119 std::string sparc::getSparcTargetCPU(const Driver &D, const ArgList &Args,
120                                      const llvm::Triple &Triple) {
121   if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ)) {
122     D.Diag(diag::err_drv_unsupported_opt_for_target)
123         << A->getSpelling() << Triple.getTriple();
124     return "";
125   }
126 
127   if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ)) {
128     StringRef CPUName = A->getValue();
129     if (CPUName == "native") {
130       std::string CPU = std::string(llvm::sys::getHostCPUName());
131       if (!CPU.empty() && CPU != "generic")
132         return CPU;
133       return "";
134     }
135     return std::string(CPUName);
136   }
137 
138   if (Triple.getArch() == llvm::Triple::sparc && Triple.isOSSolaris())
139     return "v9";
140   return "";
141 }
142 
143 void sparc::getSparcTargetFeatures(const Driver &D, 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("+soft-float");
148 
149   if (Arg *A = Args.getLastArg(options::OPT_mfsmuld, options::OPT_mno_fsmuld)) {
150     if (A->getOption().matches(options::OPT_mfsmuld))
151       Features.push_back("+fsmuld");
152     else
153       Features.push_back("-fsmuld");
154   }
155 
156   if (Arg *A = Args.getLastArg(options::OPT_mpopc, options::OPT_mno_popc)) {
157     if (A->getOption().matches(options::OPT_mpopc))
158       Features.push_back("+popc");
159     else
160       Features.push_back("-popc");
161   }
162 
163   if (Arg *A = Args.getLastArg(options::OPT_mvis, options::OPT_mno_vis)) {
164     if (A->getOption().matches(options::OPT_mvis))
165       Features.push_back("+vis");
166     else
167       Features.push_back("-vis");
168   }
169 
170   if (Arg *A = Args.getLastArg(options::OPT_mvis2, options::OPT_mno_vis2)) {
171     if (A->getOption().matches(options::OPT_mvis2))
172       Features.push_back("+vis2");
173     else
174       Features.push_back("-vis2");
175   }
176 
177   if (Arg *A = Args.getLastArg(options::OPT_mvis3, options::OPT_mno_vis3)) {
178     if (A->getOption().matches(options::OPT_mvis3))
179       Features.push_back("+vis3");
180     else
181       Features.push_back("-vis3");
182   }
183 
184   if (Arg *A = Args.getLastArg(options::OPT_mhard_quad_float,
185                                options::OPT_msoft_quad_float)) {
186     if (A->getOption().matches(options::OPT_mhard_quad_float))
187       Features.push_back("+hard-quad-float");
188     else
189       Features.push_back("-hard-quad-float");
190   }
191 }
192