1 //===--- ARM.cpp - ARM (not AArch64) Helpers for Tools ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/TargetParser.h"
18 using namespace clang::driver;
19 using namespace clang::driver::tools;
20 using namespace clang;
21 using namespace llvm::opt;
24 int arm::getARMSubArchVersionNumber(const llvm::Triple &Triple) {
25 llvm::StringRef Arch = Triple.getArchName();
26 return llvm::ARM::parseArchVersion(Arch);
30 bool arm::isARMMProfile(const llvm::Triple &Triple) {
31 llvm::StringRef Arch = Triple.getArchName();
32 unsigned Profile = llvm::ARM::parseArchProfile(Arch);
33 return Profile == llvm::ARM::PK_M;
36 // Get Arch/CPU from args.
37 void arm::getARMArchCPUFromArgs(const ArgList &Args, llvm::StringRef &Arch,
38 llvm::StringRef &CPU, bool FromAs) {
39 if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ))
41 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
47 Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
48 StringRef Value = A->getValue();
49 if (Value.startswith("-mcpu="))
50 CPU = Value.substr(6);
51 if (Value.startswith("-march="))
52 Arch = Value.substr(7);
57 // FIXME: Use ARMTargetParser.
58 static void getARMHWDivFeatures(const Driver &D, const Arg *A,
59 const ArgList &Args, StringRef HWDiv,
60 std::vector<StringRef> &Features) {
61 unsigned HWDivID = llvm::ARM::parseHWDiv(HWDiv);
62 if (!llvm::ARM::getHWDivFeatures(HWDivID, Features))
63 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
67 static void getARMFPUFeatures(const Driver &D, const Arg *A,
68 const ArgList &Args, StringRef FPU,
69 std::vector<StringRef> &Features) {
70 unsigned FPUID = llvm::ARM::parseFPU(FPU);
71 if (!llvm::ARM::getFPUFeatures(FPUID, Features))
72 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
75 // Decode ARM features from string like +[no]featureA+[no]featureB+...
76 static bool DecodeARMFeatures(const Driver &D, StringRef text,
77 std::vector<StringRef> &Features) {
78 SmallVector<StringRef, 8> Split;
79 text.split(Split, StringRef("+"), -1, false);
81 for (StringRef Feature : Split) {
82 StringRef FeatureName = llvm::ARM::getArchExtFeature(Feature);
83 if (!FeatureName.empty())
84 Features.push_back(FeatureName);
91 // Check if -march is valid by checking if it can be canonicalised and parsed.
92 // getARMArch is used here instead of just checking the -march value in order
93 // to handle -march=native correctly.
94 static void checkARMArchName(const Driver &D, const Arg *A, const ArgList &Args,
95 llvm::StringRef ArchName,
96 std::vector<StringRef> &Features,
97 const llvm::Triple &Triple) {
98 std::pair<StringRef, StringRef> Split = ArchName.split("+");
100 std::string MArch = arm::getARMArch(ArchName, Triple);
101 if (llvm::ARM::parseArch(MArch) == llvm::ARM::AK_INVALID ||
102 (Split.second.size() && !DecodeARMFeatures(D, Split.second, Features)))
103 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
106 // Check -mcpu=. Needs ArchName to handle -mcpu=generic.
107 static void checkARMCPUName(const Driver &D, const Arg *A, const ArgList &Args,
108 llvm::StringRef CPUName, llvm::StringRef ArchName,
109 std::vector<StringRef> &Features,
110 const llvm::Triple &Triple) {
111 std::pair<StringRef, StringRef> Split = CPUName.split("+");
113 std::string CPU = arm::getARMTargetCPU(CPUName, ArchName, Triple);
114 if (arm::getLLVMArchSuffixForARM(CPU, ArchName, Triple).empty() ||
115 (Split.second.size() && !DecodeARMFeatures(D, Split.second, Features)))
116 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
119 bool arm::useAAPCSForMachO(const llvm::Triple &T) {
120 // The backend is hardwired to assume AAPCS for M-class processors, ensure
121 // the frontend matches that.
122 return T.getEnvironment() == llvm::Triple::EABI ||
123 T.getOS() == llvm::Triple::UnknownOS || isARMMProfile(T);
126 // Select the float ABI as determined by -msoft-float, -mhard-float, and
128 arm::FloatABI arm::getARMFloatABI(const ToolChain &TC, const ArgList &Args) {
129 const Driver &D = TC.getDriver();
130 const llvm::Triple &Triple = TC.getEffectiveTriple();
131 auto SubArch = getARMSubArchVersionNumber(Triple);
132 arm::FloatABI ABI = FloatABI::Invalid;
134 Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
135 options::OPT_mfloat_abi_EQ)) {
136 if (A->getOption().matches(options::OPT_msoft_float)) {
137 ABI = FloatABI::Soft;
138 } else if (A->getOption().matches(options::OPT_mhard_float)) {
139 ABI = FloatABI::Hard;
141 ABI = llvm::StringSwitch<arm::FloatABI>(A->getValue())
142 .Case("soft", FloatABI::Soft)
143 .Case("softfp", FloatABI::SoftFP)
144 .Case("hard", FloatABI::Hard)
145 .Default(FloatABI::Invalid);
146 if (ABI == FloatABI::Invalid && !StringRef(A->getValue()).empty()) {
147 D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
148 ABI = FloatABI::Soft;
152 // It is incorrect to select hard float ABI on MachO platforms if the ABI is
154 if (Triple.isOSBinFormatMachO() && !useAAPCSForMachO(Triple) &&
155 ABI == FloatABI::Hard) {
156 D.Diag(diag::err_drv_unsupported_opt_for_target) << A->getAsString(Args)
157 << Triple.getArchName();
161 // If unspecified, choose the default based on the platform.
162 if (ABI == FloatABI::Invalid) {
163 switch (Triple.getOS()) {
164 case llvm::Triple::Darwin:
165 case llvm::Triple::MacOSX:
166 case llvm::Triple::IOS:
167 case llvm::Triple::TvOS: {
168 // Darwin defaults to "softfp" for v6 and v7.
169 ABI = (SubArch == 6 || SubArch == 7) ? FloatABI::SoftFP : FloatABI::Soft;
170 ABI = Triple.isWatchABI() ? FloatABI::Hard : ABI;
173 case llvm::Triple::WatchOS:
174 ABI = FloatABI::Hard;
177 // FIXME: this is invalid for WindowsCE
178 case llvm::Triple::Win32:
179 ABI = FloatABI::Hard;
182 case llvm::Triple::NetBSD:
183 switch (Triple.getEnvironment()) {
184 case llvm::Triple::EABIHF:
185 case llvm::Triple::GNUEABIHF:
186 ABI = FloatABI::Hard;
189 ABI = FloatABI::Soft;
194 case llvm::Triple::FreeBSD:
195 switch (Triple.getEnvironment()) {
196 case llvm::Triple::GNUEABIHF:
197 ABI = FloatABI::Hard;
200 // FreeBSD defaults to soft float
201 ABI = FloatABI::Soft;
206 case llvm::Triple::OpenBSD:
207 ABI = FloatABI::Soft;
211 switch (Triple.getEnvironment()) {
212 case llvm::Triple::GNUEABIHF:
213 case llvm::Triple::MuslEABIHF:
214 case llvm::Triple::EABIHF:
215 ABI = FloatABI::Hard;
217 case llvm::Triple::GNUEABI:
218 case llvm::Triple::MuslEABI:
219 case llvm::Triple::EABI:
220 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
221 ABI = FloatABI::SoftFP;
223 case llvm::Triple::Android:
224 ABI = (SubArch == 7) ? FloatABI::SoftFP : FloatABI::Soft;
227 // Assume "soft", but warn the user we are guessing.
228 if (Triple.isOSBinFormatMachO() &&
229 Triple.getSubArch() == llvm::Triple::ARMSubArch_v7em)
230 ABI = FloatABI::Hard;
232 ABI = FloatABI::Soft;
234 if (Triple.getOS() != llvm::Triple::UnknownOS ||
235 !Triple.isOSBinFormatMachO())
236 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
242 assert(ABI != FloatABI::Invalid && "must select an ABI");
246 void arm::getARMTargetFeatures(const ToolChain &TC,
247 const llvm::Triple &Triple,
249 ArgStringList &CmdArgs,
250 std::vector<StringRef> &Features,
252 const Driver &D = TC.getDriver();
255 Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext);
256 arm::FloatABI ABI = arm::getARMFloatABI(TC, Args);
257 const Arg *WaCPU = nullptr, *WaFPU = nullptr;
258 const Arg *WaHDiv = nullptr, *WaArch = nullptr;
261 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
262 // yet (it uses the -mfloat-abi and -msoft-float options), and it is
263 // stripped out by the ARM target. We should probably pass this a new
264 // -target-option, which is handled by the -cc1/-cc1as invocation.
266 // FIXME2: For consistency, it would be ideal if we set up the target
267 // machine state the same when using the frontend or the assembler. We don't
268 // currently do that for the assembler, we pass the options directly to the
269 // backend and never even instantiate the frontend TargetInfo. If we did,
270 // and used its handleTargetFeatures hook, then we could ensure the
271 // assembler and the frontend behave the same.
273 // Use software floating point operations?
274 if (ABI == arm::FloatABI::Soft)
275 Features.push_back("+soft-float");
277 // Use software floating point argument passing?
278 if (ABI != arm::FloatABI::Hard)
279 Features.push_back("+soft-float-abi");
281 // Here, we make sure that -Wa,-mfpu/cpu/arch/hwdiv will be passed down
282 // to the assembler correctly.
284 Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
285 StringRef Value = A->getValue();
286 if (Value.startswith("-mfpu=")) {
288 } else if (Value.startswith("-mcpu=")) {
290 } else if (Value.startswith("-mhwdiv=")) {
292 } else if (Value.startswith("-march=")) {
298 // Check -march. ClangAs gives preference to -Wa,-march=.
299 const Arg *ArchArg = Args.getLastArg(options::OPT_march_EQ);
303 D.Diag(clang::diag::warn_drv_unused_argument)
304 << ArchArg->getAsString(Args);
305 ArchName = StringRef(WaArch->getValue()).substr(7);
306 checkARMArchName(D, WaArch, Args, ArchName, Features, Triple);
308 D.Diag(clang::diag::warn_drv_unused_argument) << WaArch->getAsString(Args);
309 } else if (ArchArg) {
310 ArchName = ArchArg->getValue();
311 checkARMArchName(D, ArchArg, Args, ArchName, Features, Triple);
314 // Check -mcpu. ClangAs gives preference to -Wa,-mcpu=.
315 const Arg *CPUArg = Args.getLastArg(options::OPT_mcpu_EQ);
319 D.Diag(clang::diag::warn_drv_unused_argument)
320 << CPUArg->getAsString(Args);
321 CPUName = StringRef(WaCPU->getValue()).substr(6);
322 checkARMCPUName(D, WaCPU, Args, CPUName, ArchName, Features, Triple);
324 CPUName = CPUArg->getValue();
325 checkARMCPUName(D, CPUArg, Args, CPUName, ArchName, Features, Triple);
328 // Add CPU features for generic CPUs
329 if (CPUName == "native") {
330 llvm::StringMap<bool> HostFeatures;
331 if (llvm::sys::getHostCPUFeatures(HostFeatures))
332 for (auto &F : HostFeatures)
334 Args.MakeArgString((F.second ? "+" : "-") + F.first()));
337 // Honor -mfpu=. ClangAs gives preference to -Wa,-mfpu=.
338 const Arg *FPUArg = Args.getLastArg(options::OPT_mfpu_EQ);
341 D.Diag(clang::diag::warn_drv_unused_argument)
342 << FPUArg->getAsString(Args);
343 getARMFPUFeatures(D, WaFPU, Args, StringRef(WaFPU->getValue()).substr(6),
346 getARMFPUFeatures(D, FPUArg, Args, FPUArg->getValue(), Features);
349 // Honor -mhwdiv=. ClangAs gives preference to -Wa,-mhwdiv=.
350 const Arg *HDivArg = Args.getLastArg(options::OPT_mhwdiv_EQ);
353 D.Diag(clang::diag::warn_drv_unused_argument)
354 << HDivArg->getAsString(Args);
355 getARMHWDivFeatures(D, WaHDiv, Args,
356 StringRef(WaHDiv->getValue()).substr(8), Features);
358 getARMHWDivFeatures(D, HDivArg, Args, HDivArg->getValue(), Features);
360 // Setting -msoft-float effectively disables NEON because of the GCC
361 // implementation, although the same isn't true of VFP or VFP3.
362 if (ABI == arm::FloatABI::Soft) {
363 Features.push_back("-neon");
364 // Also need to explicitly disable features which imply NEON.
365 Features.push_back("-crypto");
368 // En/disable crc code generation.
369 if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
370 if (A->getOption().matches(options::OPT_mcrc))
371 Features.push_back("+crc");
373 Features.push_back("-crc");
376 // Look for the last occurrence of -mlong-calls or -mno-long-calls. If
377 // neither options are specified, see if we are compiling for kernel/kext and
378 // decide whether to pass "+long-calls" based on the OS and its version.
379 if (Arg *A = Args.getLastArg(options::OPT_mlong_calls,
380 options::OPT_mno_long_calls)) {
381 if (A->getOption().matches(options::OPT_mlong_calls))
382 Features.push_back("+long-calls");
383 } else if (KernelOrKext && (!Triple.isiOS() || Triple.isOSVersionLT(6)) &&
384 !Triple.isWatchOS()) {
385 Features.push_back("+long-calls");
388 // Generate execute-only output (no data access to code sections).
389 // This only makes sense for the compiler, not for the assembler.
391 // Supported only on ARMv6T2 and ARMv7 and above.
392 // Cannot be combined with -mno-movt or -mlong-calls
393 if (Arg *A = Args.getLastArg(options::OPT_mexecute_only, options::OPT_mno_execute_only)) {
394 if (A->getOption().matches(options::OPT_mexecute_only)) {
395 if (getARMSubArchVersionNumber(Triple) < 7 &&
396 llvm::ARM::parseArch(Triple.getArchName()) != llvm::ARM::AK_ARMV6T2)
397 D.Diag(diag::err_target_unsupported_execute_only) << Triple.getArchName();
398 else if (Arg *B = Args.getLastArg(options::OPT_mno_movt))
399 D.Diag(diag::err_opt_not_valid_with_opt) << A->getAsString(Args) << B->getAsString(Args);
400 // Long calls create constant pool entries and have not yet been fixed up
401 // to play nicely with execute-only. Hence, they cannot be used in
402 // execute-only code for now
403 else if (Arg *B = Args.getLastArg(options::OPT_mlong_calls, options::OPT_mno_long_calls)) {
404 if (B->getOption().matches(options::OPT_mlong_calls))
405 D.Diag(diag::err_opt_not_valid_with_opt) << A->getAsString(Args) << B->getAsString(Args);
407 Features.push_back("+execute-only");
412 // Kernel code has more strict alignment requirements.
414 Features.push_back("+strict-align");
415 else if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
416 options::OPT_munaligned_access)) {
417 if (A->getOption().matches(options::OPT_munaligned_access)) {
418 // No v6M core supports unaligned memory access (v6M ARM ARM A3.2).
419 if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m)
420 D.Diag(diag::err_target_unsupported_unaligned) << "v6m";
421 // v8M Baseline follows on from v6M, so doesn't support unaligned memory
423 else if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v8m_baseline)
424 D.Diag(diag::err_target_unsupported_unaligned) << "v8m.base";
426 Features.push_back("+strict-align");
428 // Assume pre-ARMv6 doesn't support unaligned accesses.
430 // ARMv6 may or may not support unaligned accesses depending on the
431 // SCTLR.U bit, which is architecture-specific. We assume ARMv6
432 // Darwin and NetBSD targets support unaligned accesses, and others don't.
434 // ARMv7 always has SCTLR.U set to 1, but it has a new SCTLR.A bit
435 // which raises an alignment fault on unaligned accesses. Linux
436 // defaults this bit to 0 and handles it as a system-wide (not
437 // per-process) setting. It is therefore safe to assume that ARMv7+
438 // Linux targets support unaligned accesses. The same goes for NaCl.
440 // The above behavior is consistent with GCC.
441 int VersionNum = getARMSubArchVersionNumber(Triple);
442 if (Triple.isOSDarwin() || Triple.isOSNetBSD()) {
443 if (VersionNum < 6 ||
444 Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m)
445 Features.push_back("+strict-align");
446 } else if (Triple.isOSLinux() || Triple.isOSNaCl()) {
448 Features.push_back("+strict-align");
450 Features.push_back("+strict-align");
453 // llvm does not support reserving registers in general. There is support
454 // for reserving r9 on ARM though (defined as a platform-specific register
456 if (Args.hasArg(options::OPT_ffixed_r9))
457 Features.push_back("+reserve-r9");
459 // The kext linker doesn't know how to deal with movw/movt.
460 if (KernelOrKext || Args.hasArg(options::OPT_mno_movt))
461 Features.push_back("+no-movt");
463 if (Args.hasArg(options::OPT_mno_neg_immediates))
464 Features.push_back("+no-neg-immediates");
467 const std::string arm::getARMArch(StringRef Arch, const llvm::Triple &Triple) {
472 MArch = Triple.getArchName();
473 MArch = StringRef(MArch).split("+").first.lower();
475 // Handle -march=native.
476 if (MArch == "native") {
477 std::string CPU = llvm::sys::getHostCPUName();
478 if (CPU != "generic") {
479 // Translate the native cpu into the architecture suffix for that CPU.
480 StringRef Suffix = arm::getLLVMArchSuffixForARM(CPU, MArch, Triple);
481 // If there is no valid architecture suffix for this CPU we don't know how
482 // to handle it, so return no architecture.
486 MArch = std::string("arm") + Suffix.str();
493 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
494 StringRef arm::getARMCPUForMArch(StringRef Arch, const llvm::Triple &Triple) {
495 std::string MArch = getARMArch(Arch, Triple);
496 // getARMCPUForArch defaults to the triple if MArch is empty, but empty MArch
497 // here means an -march=native that we can't handle, so instead return no CPU.
501 // We need to return an empty string here on invalid MArch values as the
502 // various places that call this function can't cope with a null result.
503 return Triple.getARMCPUForArch(MArch);
506 /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
507 std::string arm::getARMTargetCPU(StringRef CPU, StringRef Arch,
508 const llvm::Triple &Triple) {
509 // FIXME: Warn on inconsistent use of -mcpu and -march.
510 // If we have -mcpu=, use that.
512 std::string MCPU = StringRef(CPU).split("+").first.lower();
513 // Handle -mcpu=native.
514 if (MCPU == "native")
515 return llvm::sys::getHostCPUName();
520 return getARMCPUForMArch(Arch, Triple);
523 /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
524 /// CPU (or Arch, if CPU is generic).
525 // FIXME: This is redundant with -mcpu, why does LLVM use this.
526 StringRef arm::getLLVMArchSuffixForARM(StringRef CPU, StringRef Arch,
527 const llvm::Triple &Triple) {
529 if (CPU == "generic") {
530 std::string ARMArch = tools::arm::getARMArch(Arch, Triple);
531 ArchKind = llvm::ARM::parseArch(ARMArch);
532 if (ArchKind == llvm::ARM::AK_INVALID)
533 // In case of generic Arch, i.e. "arm",
534 // extract arch from default cpu of the Triple
535 ArchKind = llvm::ARM::parseCPUArch(Triple.getARMCPUForArch(ARMArch));
537 // FIXME: horrible hack to get around the fact that Cortex-A7 is only an
538 // armv7k triple if it's actually been specified via "-arch armv7k".
539 ArchKind = (Arch == "armv7k" || Arch == "thumbv7k")
540 ? (unsigned)llvm::ARM::AK_ARMV7K
541 : llvm::ARM::parseCPUArch(CPU);
543 if (ArchKind == llvm::ARM::AK_INVALID)
545 return llvm::ARM::getSubArch(ArchKind);
548 void arm::appendEBLinkFlags(const ArgList &Args, ArgStringList &CmdArgs,
549 const llvm::Triple &Triple) {
550 if (Args.hasArg(options::OPT_r))
553 // ARMv7 (and later) and ARMv6-M do not support BE-32, so instruct the linker
554 // to generate BE-8 executables.
555 if (arm::getARMSubArchVersionNumber(Triple) >= 7 || arm::isARMMProfile(Triple))
556 CmdArgs.push_back("--be8");