16 #include "llvm/Option/ArgList.h"
17 #include "llvm/Support/Error.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/TargetParser/Host.h"
20 #include "llvm/TargetParser/RISCVISAInfo.h"
21 #include "llvm/TargetParser/RISCVTargetParser.h"
25 using namespace clang;
30 std::vector<StringRef> &Features,
31 const ArgList &Args) {
32 bool EnableExperimentalExtensions =
33 Args.hasArg(options::OPT_menable_experimental_extensions);
35 llvm::RISCVISAInfo::parseArchString(Arch, EnableExperimentalExtensions);
37 handleAllErrors(ISAInfo.takeError(), [&](llvm::StringError &ErrMsg) {
38 D.Diag(diag::err_drv_invalid_riscv_arch_name)
39 << Arch << ErrMsg.getMessage();
45 for (
const std::string &Str : (*ISAInfo)->toFeatures(
true,
47 Features.push_back(Args.MakeArgString(Str));
49 if (EnableExperimentalExtensions)
50 Features.push_back(Args.MakeArgString(
"+experimental"));
57 const llvm::Triple &Triple,
59 std::vector<StringRef> &Features) {
60 bool Is64Bit = Triple.isRISCV64();
61 if (!llvm::RISCV::parseCPU(Mcpu, Is64Bit)) {
63 if (llvm::RISCV::parseCPU(Mcpu, !Is64Bit))
64 D.Diag(clang::diag::err_drv_invalid_riscv_cpu_name_for_target)
67 D.Diag(clang::diag::err_drv_unsupported_option_argument)
68 << A->getSpelling() << Mcpu;
74 std::vector<StringRef> &Features) {
80 bool CPUFastScalarUnaligned =
false;
81 bool CPUFastVectorUnaligned =
false;
85 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
86 StringRef CPU = A->getValue();
88 CPU = llvm::sys::getHostCPUName();
92 if (llvm::RISCV::hasFastScalarUnalignedAccess(CPU))
93 CPUFastScalarUnaligned =
true;
94 if (llvm::RISCV::hasFastVectorUnalignedAccess(CPU))
95 CPUFastVectorUnaligned =
true;
99 if (Args.hasArg(options::OPT_ffixed_x1))
100 Features.push_back(
"+reserve-x1");
101 if (Args.hasArg(options::OPT_ffixed_x2))
102 Features.push_back(
"+reserve-x2");
103 if (Args.hasArg(options::OPT_ffixed_x3))
104 Features.push_back(
"+reserve-x3");
105 if (Args.hasArg(options::OPT_ffixed_x4))
106 Features.push_back(
"+reserve-x4");
107 if (Args.hasArg(options::OPT_ffixed_x5))
108 Features.push_back(
"+reserve-x5");
109 if (Args.hasArg(options::OPT_ffixed_x6))
110 Features.push_back(
"+reserve-x6");
111 if (Args.hasArg(options::OPT_ffixed_x7))
112 Features.push_back(
"+reserve-x7");
113 if (Args.hasArg(options::OPT_ffixed_x8))
114 Features.push_back(
"+reserve-x8");
115 if (Args.hasArg(options::OPT_ffixed_x9))
116 Features.push_back(
"+reserve-x9");
117 if (Args.hasArg(options::OPT_ffixed_x10))
118 Features.push_back(
"+reserve-x10");
119 if (Args.hasArg(options::OPT_ffixed_x11))
120 Features.push_back(
"+reserve-x11");
121 if (Args.hasArg(options::OPT_ffixed_x12))
122 Features.push_back(
"+reserve-x12");
123 if (Args.hasArg(options::OPT_ffixed_x13))
124 Features.push_back(
"+reserve-x13");
125 if (Args.hasArg(options::OPT_ffixed_x14))
126 Features.push_back(
"+reserve-x14");
127 if (Args.hasArg(options::OPT_ffixed_x15))
128 Features.push_back(
"+reserve-x15");
129 if (Args.hasArg(options::OPT_ffixed_x16))
130 Features.push_back(
"+reserve-x16");
131 if (Args.hasArg(options::OPT_ffixed_x17))
132 Features.push_back(
"+reserve-x17");
133 if (Args.hasArg(options::OPT_ffixed_x18))
134 Features.push_back(
"+reserve-x18");
135 if (Args.hasArg(options::OPT_ffixed_x19))
136 Features.push_back(
"+reserve-x19");
137 if (Args.hasArg(options::OPT_ffixed_x20))
138 Features.push_back(
"+reserve-x20");
139 if (Args.hasArg(options::OPT_ffixed_x21))
140 Features.push_back(
"+reserve-x21");
141 if (Args.hasArg(options::OPT_ffixed_x22))
142 Features.push_back(
"+reserve-x22");
143 if (Args.hasArg(options::OPT_ffixed_x23))
144 Features.push_back(
"+reserve-x23");
145 if (Args.hasArg(options::OPT_ffixed_x24))
146 Features.push_back(
"+reserve-x24");
147 if (Args.hasArg(options::OPT_ffixed_x25))
148 Features.push_back(
"+reserve-x25");
149 if (Args.hasArg(options::OPT_ffixed_x26))
150 Features.push_back(
"+reserve-x26");
151 if (Args.hasArg(options::OPT_ffixed_x27))
152 Features.push_back(
"+reserve-x27");
153 if (Args.hasArg(options::OPT_ffixed_x28))
154 Features.push_back(
"+reserve-x28");
155 if (Args.hasArg(options::OPT_ffixed_x29))
156 Features.push_back(
"+reserve-x29");
157 if (Args.hasArg(options::OPT_ffixed_x30))
158 Features.push_back(
"+reserve-x30");
159 if (Args.hasArg(options::OPT_ffixed_x31))
160 Features.push_back(
"+reserve-x31");
163 if (Args.hasFlag(options::OPT_mrelax, options::OPT_mno_relax,
true)) {
164 Features.push_back(
"+relax");
169 D.Diag(clang::diag::err_drv_riscv_unsupported_with_linker_relaxation)
170 << A->getAsString(Args);
172 Features.push_back(
"-relax");
179 if (
const Arg *A = Args.getLastArg(
180 options::OPT_mno_strict_align, options::OPT_mscalar_strict_align,
181 options::OPT_mstrict_align, options::OPT_mno_scalar_strict_align)) {
182 if (A->getOption().matches(options::OPT_mno_strict_align) ||
183 A->getOption().matches(options::OPT_mno_scalar_strict_align)) {
184 Features.push_back(
"+unaligned-scalar-mem");
186 Features.push_back(
"-unaligned-scalar-mem");
188 }
else if (CPUFastScalarUnaligned || Triple.isAndroid()) {
189 Features.push_back(
"+unaligned-scalar-mem");
196 if (
const Arg *A = Args.getLastArg(
197 options::OPT_mno_strict_align, options::OPT_mvector_strict_align,
198 options::OPT_mstrict_align, options::OPT_mno_vector_strict_align)) {
199 if (A->getOption().matches(options::OPT_mno_strict_align) ||
200 A->getOption().matches(options::OPT_mno_vector_strict_align)) {
201 Features.push_back(
"+unaligned-vector-mem");
203 Features.push_back(
"-unaligned-vector-mem");
205 }
else if (CPUFastVectorUnaligned || Triple.isAndroid()) {
206 Features.push_back(
"+unaligned-vector-mem");
212 options::OPT_m_riscv_Features_Group);
216 assert(Triple.isRISCV() &&
"Unexpected triple");
239 if (
const Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
240 return A->getValue();
252 auto ParseResult = llvm::RISCVISAInfo::parseArchString(
255 if (!llvm::errorToBool(ParseResult.takeError()))
256 return (*ParseResult)->computeDefaultABI();
263 if (Triple.isRISCV32()) {
264 if (Triple.getOS() == llvm::Triple::UnknownOS)
269 if (Triple.getOS() == llvm::Triple::UnknownOS)
277 const llvm::Triple &Triple) {
278 assert(Triple.isRISCV() &&
"Unexpected triple");
307 if (
const Arg *A = Args.getLastArg(options::OPT_march_EQ))
308 return A->getValue();
311 if (
const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
312 StringRef CPU = A->getValue();
313 if (CPU ==
"native") {
314 CPU = llvm::sys::getHostCPUName();
316 if (CPU.starts_with(
"generic")) {
317 auto FeatureMap = llvm::sys::getHostCPUFeatures();
319 if (!FeatureMap.empty()) {
320 std::vector<std::string> Features;
321 for (
auto &F : FeatureMap)
322 Features.push_back(((F.second ?
"+" :
"-") + F.first()).str());
323 auto ParseResult = llvm::RISCVISAInfo::parseFeatures(
324 Triple.isRISCV32() ? 32 : 64, Features);
326 return (*ParseResult)->toString();
331 StringRef MArch = llvm::RISCV::getMArchFromMcpu(CPU);
343 if (
const Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
344 StringRef MABI = A->getValue();
346 if (MABI.equals_insensitive(
"ilp32e"))
348 else if (MABI.equals_insensitive(
"lp64e"))
350 else if (MABI.starts_with_insensitive(
"ilp32"))
352 else if (MABI.starts_with_insensitive(
"lp64")) {
353 if (Triple.isAndroid())
354 return "rv64imafdcv_zba_zbb_zbs";
365 if (Triple.isRISCV32()) {
366 if (Triple.getOS() == llvm::Triple::UnknownOS)
371 if (Triple.getOS() == llvm::Triple::UnknownOS)
373 else if (Triple.isAndroid())
374 return "rv64imafdcv_zba_zbb_zbs";
381 const llvm::Triple &Triple) {
384 if (
const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
389 CPU = llvm::sys::getHostCPUName();
394 return Triple.isRISCV64() ?
"generic-rv64" :
"generic-rv32";
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
The JSON file list parser is used to communicate input to InstallAPI.