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/TargetParser/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_mcpu_EQ)) {
122     StringRef CPUName = A->getValue();
123     if (CPUName == "native") {
124       std::string CPU = std::string(llvm::sys::getHostCPUName());
125       if (!CPU.empty() && CPU != "generic")
126         return CPU;
127       return "";
128     }
129     return std::string(CPUName);
130   }
131 
132   if (Triple.getArch() == llvm::Triple::sparc && Triple.isOSSolaris())
133     return "v9";
134   return "";
135 }
136 
137 void sparc::getSparcTargetFeatures(const Driver &D, const ArgList &Args,
138                                    std::vector<StringRef> &Features) {
139   sparc::FloatABI FloatABI = sparc::getSparcFloatABI(D, Args);
140   if (FloatABI == sparc::FloatABI::Soft)
141     Features.push_back("+soft-float");
142 
143   if (Arg *A = Args.getLastArg(options::OPT_mfsmuld, options::OPT_mno_fsmuld)) {
144     if (A->getOption().matches(options::OPT_mfsmuld))
145       Features.push_back("+fsmuld");
146     else
147       Features.push_back("-fsmuld");
148   }
149 
150   if (Arg *A = Args.getLastArg(options::OPT_mpopc, options::OPT_mno_popc)) {
151     if (A->getOption().matches(options::OPT_mpopc))
152       Features.push_back("+popc");
153     else
154       Features.push_back("-popc");
155   }
156 
157   if (Arg *A = Args.getLastArg(options::OPT_mvis, options::OPT_mno_vis)) {
158     if (A->getOption().matches(options::OPT_mvis))
159       Features.push_back("+vis");
160     else
161       Features.push_back("-vis");
162   }
163 
164   if (Arg *A = Args.getLastArg(options::OPT_mvis2, options::OPT_mno_vis2)) {
165     if (A->getOption().matches(options::OPT_mvis2))
166       Features.push_back("+vis2");
167     else
168       Features.push_back("-vis2");
169   }
170 
171   if (Arg *A = Args.getLastArg(options::OPT_mvis3, options::OPT_mno_vis3)) {
172     if (A->getOption().matches(options::OPT_mvis3))
173       Features.push_back("+vis3");
174     else
175       Features.push_back("-vis3");
176   }
177 
178   if (Arg *A = Args.getLastArg(options::OPT_mhard_quad_float,
179                                options::OPT_msoft_quad_float)) {
180     if (A->getOption().matches(options::OPT_mhard_quad_float))
181       Features.push_back("+hard-quad-float");
182     else
183       Features.push_back("-hard-quad-float");
184   }
185 }
186