#include "ARM.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/DriverDiagnostic.h"
#include "clang/Driver/Options.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/ARMTargetParser.h"
#include "llvm/Support/TargetParser.h"
#include "llvm/Support/Host.h"
using namespace clang::driver;
using namespace clang::driver::tools;
using namespace clang;
using namespace llvm::opt;
int arm::getARMSubArchVersionNumber(const llvm::Triple &Triple) {
llvm::StringRef Arch = Triple.getArchName();
return llvm::ARM::parseArchVersion(Arch);
}
bool arm::isARMMProfile(const llvm::Triple &Triple) {
llvm::StringRef Arch = Triple.getArchName();
return llvm::ARM::parseArchProfile(Arch) == llvm::ARM::ProfileKind::M;
}
bool arm::isARMAProfile(const llvm::Triple &Triple) {
llvm::StringRef Arch = Triple.getArchName();
return llvm::ARM::parseArchProfile(Arch) == llvm::ARM::ProfileKind::A;
}
void arm::getARMArchCPUFromArgs(const ArgList &Args, llvm::StringRef &Arch,
llvm::StringRef &CPU, bool FromAs) {
if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ))
CPU = A->getValue();
if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
Arch = A->getValue();
if (!FromAs)
return;
for (const Arg *A :
Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
for (StringRef Value : A->getValues()) {
if (Value.startswith("-mcpu="))
CPU = Value.substr(6);
if (Value.startswith("-march="))
Arch = Value.substr(7);
}
}
}
static void getARMHWDivFeatures(const Driver &D, const Arg *A,
const ArgList &Args, StringRef HWDiv,
std::vector<StringRef> &Features) {
uint64_t HWDivID = llvm::ARM::parseHWDiv(HWDiv);
if (!llvm::ARM::getHWDivFeatures(HWDivID, Features))
D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
}
static unsigned getARMFPUFeatures(const Driver &D, const Arg *A,
const ArgList &Args, StringRef FPU,
std::vector<StringRef> &Features) {
unsigned FPUID = llvm::ARM::parseFPU(FPU);
if (!llvm::ARM::getFPUFeatures(FPUID, Features))
D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
return FPUID;
}
static bool DecodeARMFeatures(const Driver &D, StringRef text, StringRef CPU,
llvm::ARM::ArchKind ArchKind,
std::vector<StringRef> &Features,
unsigned &ArgFPUID) {
SmallVector<StringRef, 8> Split;
text.split(Split, StringRef("+"), -1, false);
for (StringRef Feature : Split) {
if (!appendArchExtFeatures(CPU, ArchKind, Feature, Features, ArgFPUID))
return false;
}
return true;
}
static void DecodeARMFeaturesFromCPU(const Driver &D, StringRef CPU,
std::vector<StringRef> &Features) {
CPU = CPU.split("+").first;
if (CPU != "generic") {
llvm::ARM::ArchKind ArchKind = llvm::ARM::parseCPUArch(CPU);
uint64_t Extension = llvm::ARM::getDefaultExtensions(CPU, ArchKind);
llvm::ARM::getExtensionFeatures(Extension, Features);
}
}
static void checkARMArchName(const Driver &D, const Arg *A, const ArgList &Args,
llvm::StringRef ArchName, llvm::StringRef CPUName,
std::vector<StringRef> &Features,
const llvm::Triple &Triple, unsigned &ArgFPUID) {
std::pair<StringRef, StringRef> Split = ArchName.split("+");
std::string MArch = arm::getARMArch(ArchName, Triple);
llvm::ARM::ArchKind ArchKind = llvm::ARM::parseArch(MArch);
if (ArchKind == llvm::ARM::ArchKind::INVALID ||
(Split.second.size() && !DecodeARMFeatures(D, Split.second, CPUName,
ArchKind, Features, ArgFPUID)))
D.Diag(clang::diag::err_drv_unsupported_option_argument)
<< A->getOption().getName() << A->getValue();
}
static void checkARMCPUName(const Driver &D, const Arg *A, const ArgList &Args,
llvm::StringRef CPUName, llvm::StringRef ArchName,
std::vector<StringRef> &Features,
const llvm::Triple &Triple, unsigned &ArgFPUID) {
std::pair<StringRef, StringRef> Split = CPUName.split("+");
std::string CPU = arm::getARMTargetCPU(CPUName, ArchName, Triple);
llvm::ARM::ArchKind ArchKind =
arm::getLLVMArchKindForARM(CPU, ArchName, Triple);
if (ArchKind == llvm::ARM::ArchKind::INVALID ||
(Split.second.size() &&
!DecodeARMFeatures(D, Split.second, CPU, ArchKind, Features, ArgFPUID)))
D.Diag(clang::diag::err_drv_unsupported_option_argument)
<< A->getOption().getName() << A->getValue();
}
bool arm::useAAPCSForMachO(const llvm::Triple &T) {
return T.getEnvironment() == llvm::Triple::EABI ||
T.getEnvironment() == llvm::Triple::EABIHF ||
T.getOS() == llvm::Triple::UnknownOS || isARMMProfile(T);
}
bool arm::isHardTPSupported(const llvm::Triple &Triple) {
int Ver = getARMSubArchVersionNumber(Triple);
llvm::ARM::ArchKind AK = llvm::ARM::parseArch(Triple.getArchName());
return Triple.isARM() || AK == llvm::ARM::ArchKind::ARMV6T2 ||
(Ver >= 7 && AK != llvm::ARM::ArchKind::ARMV8MBaseline);
}
arm::ReadTPMode arm::getReadTPMode(const Driver &D, const ArgList &Args,
const llvm::Triple &Triple, bool ForAS) {
if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) {
arm::ReadTPMode ThreadPointer =
llvm::StringSwitch<arm::ReadTPMode>(A->getValue())
.Case("cp15", ReadTPMode::Cp15)
.Case("soft", ReadTPMode::Soft)
.Default(ReadTPMode::Invalid);
if (ThreadPointer == ReadTPMode::Cp15 && !isHardTPSupported(Triple) &&
!ForAS) {
D.Diag(diag::err_target_unsupported_tp_hard) << Triple.getArchName();
return ReadTPMode::Invalid;
}
if (ThreadPointer != ReadTPMode::Invalid)
return ThreadPointer;
if (StringRef(A->getValue()).empty())
D.Diag(diag::err_drv_missing_arg_mtp) << A->getAsString(Args);
else
D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args);
return ReadTPMode::Invalid;
}
return ReadTPMode::Soft;
}
void arm::setArchNameInTriple(const Driver &D, const ArgList &Args,
types::ID InputType, llvm::Triple &Triple) {
StringRef MCPU, MArch;
if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
MCPU = A->getValue();
if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
MArch = A->getValue();
std::string CPU = Triple.isOSBinFormatMachO()
? tools::arm::getARMCPUForMArch(MArch, Triple).str()
: tools::arm::getARMTargetCPU(MCPU, MArch, Triple);
StringRef Suffix = tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple);
bool IsBigEndian = Triple.getArch() == llvm::Triple::armeb ||
Triple.getArch() == llvm::Triple::thumbeb;
if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
options::OPT_mbig_endian)) {
IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
}
std::string ArchName = IsBigEndian ? "armeb" : "arm";
bool IsMProfile =
llvm::ARM::parseArchProfile(Suffix) == llvm::ARM::ProfileKind::M;
bool ThumbDefault = IsMProfile ||
(llvm::ARM::parseArchVersion(Suffix) == 7 &&
Triple.isOSBinFormatMachO()) ||
Triple.isOSWindows();
bool ARMModeRequested =
!Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault);
if (IsMProfile && ARMModeRequested) {
if (MCPU.size())
D.Diag(diag::err_cpu_unsupported_isa) << CPU << "ARM";
else
D.Diag(diag::err_arch_unsupported_isa)
<< tools::arm::getARMArch(MArch, Triple) << "ARM";
}
bool IsThumb = false;
if (InputType != types::TY_PP_Asm)
IsThumb =
Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault);
else {
llvm::StringRef WaMArch, WaMCPU;
for (const auto *A :
Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
for (StringRef Value : A->getValues()) {
if (Value == "-mthumb")
IsThumb = true;
else if (Value.startswith("-march="))
WaMArch = Value.substr(7);
else if (Value.startswith("-mcpu="))
WaMCPU = Value.substr(6);
}
}
if (WaMCPU.size() || WaMArch.size()) {
Suffix = tools::arm::getLLVMArchSuffixForARM(WaMCPU, WaMArch, Triple);
}
}
if (IsThumb || IsMProfile || Triple.isOSWindows()) {
if (IsBigEndian)
ArchName = "thumbeb";
else
ArchName = "thumb";
}
Triple.setArchName(ArchName + Suffix.str());
}
void arm::setFloatABIInTriple(const Driver &D, const ArgList &Args,
llvm::Triple &Triple) {
bool isHardFloat =
(arm::getARMFloatABI(D, Triple, Args) == arm::FloatABI::Hard);
switch (Triple.getEnvironment()) {
case llvm::Triple::GNUEABI:
case llvm::Triple::GNUEABIHF:
Triple.setEnvironment(isHardFloat ? llvm::Triple::GNUEABIHF
: llvm::Triple::GNUEABI);
break;
case llvm::Triple::EABI:
case llvm::Triple::EABIHF:
Triple.setEnvironment(isHardFloat ? llvm::Triple::EABIHF
: llvm::Triple::EABI);
break;
case llvm::Triple::MuslEABI:
case llvm::Triple::MuslEABIHF:
Triple.setEnvironment(isHardFloat ? llvm::Triple::MuslEABIHF
: llvm::Triple::MuslEABI);
break;
default: {
arm::FloatABI DefaultABI = arm::getDefaultFloatABI(Triple);
if (DefaultABI != arm::FloatABI::Invalid &&
isHardFloat != (DefaultABI == arm::FloatABI::Hard)) {
Arg *ABIArg =
Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
options::OPT_mfloat_abi_EQ);
assert(ABIArg && "Non-default float abi expected to be from arg");
D.Diag(diag::err_drv_unsupported_opt_for_target)
<< ABIArg->getAsString(Args) << Triple.getTriple();
}
break;
}
}
}
arm::FloatABI arm::getARMFloatABI(const ToolChain &TC, const ArgList &Args) {
return arm::getARMFloatABI(TC.getDriver(), TC.getEffectiveTriple(), Args);
}
arm::FloatABI arm::getDefaultFloatABI(const llvm::Triple &Triple) {
auto SubArch = getARMSubArchVersionNumber(Triple);
switch (Triple.getOS()) {
case llvm::Triple::Darwin:
case llvm::Triple::MacOSX:
case llvm::Triple::IOS:
case llvm::Triple::TvOS:
case llvm::Triple::DriverKit:
if (Triple.isWatchABI())
return FloatABI::Hard;
else
return (SubArch == 6 || SubArch == 7) ? FloatABI::SoftFP : FloatABI::Soft;
case llvm::Triple::WatchOS:
return FloatABI::Hard;
case llvm::Triple::Win32:
if (Triple.isOSBinFormatMachO() && !useAAPCSForMachO(Triple))
return FloatABI::Soft;
return FloatABI::Hard;
case llvm::Triple::NetBSD:
switch (Triple.getEnvironment()) {
case llvm::Triple::EABIHF:
case llvm::Triple::GNUEABIHF:
return FloatABI::Hard;
default:
return FloatABI::Soft;
}
break;
case llvm::Triple::FreeBSD:
switch (Triple.getEnvironment()) {
case llvm::Triple::GNUEABIHF:
return FloatABI::Hard;
default:
return FloatABI::Soft;
}
break;
case llvm::Triple::OpenBSD:
return FloatABI::SoftFP;
default:
switch (Triple.getEnvironment()) {
case llvm::Triple::GNUEABIHF:
case llvm::Triple::MuslEABIHF:
case llvm::Triple::EABIHF:
return FloatABI::Hard;
case llvm::Triple::GNUEABI:
case llvm::Triple::MuslEABI:
case llvm::Triple::EABI:
return FloatABI::SoftFP;
case llvm::Triple::Android:
return (SubArch >= 7) ? FloatABI::SoftFP : FloatABI::Soft;
default:
return FloatABI::Invalid;
}
}
return FloatABI::Invalid;
}
arm::FloatABI arm::getARMFloatABI(const Driver &D, const llvm::Triple &Triple,
const ArgList &Args) {
arm::FloatABI ABI = FloatABI::Invalid;
if (Arg *A =
Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
options::OPT_mfloat_abi_EQ)) {
if (A->getOption().matches(options::OPT_msoft_float)) {
ABI = FloatABI::Soft;
} else if (A->getOption().matches(options::OPT_mhard_float)) {
ABI = FloatABI::Hard;
} else {
ABI = llvm::StringSwitch<arm::FloatABI>(A->getValue())
.Case("soft", FloatABI::Soft)
.Case("softfp", FloatABI::SoftFP)
.Case("hard", FloatABI::Hard)
.Default(FloatABI::Invalid);
if (ABI == FloatABI::Invalid && !StringRef(A->getValue()).empty()) {
D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
ABI = FloatABI::Soft;
}
}
}
if (ABI == FloatABI::Invalid)
ABI = arm::getDefaultFloatABI(Triple);
if (ABI == FloatABI::Invalid) {
if (Triple.isOSBinFormatMachO() &&
Triple.getSubArch() == llvm::Triple::ARMSubArch_v7em)
ABI = FloatABI::Hard;
else
ABI = FloatABI::Soft;
if (Triple.getOS() != llvm::Triple::UnknownOS ||
!Triple.isOSBinFormatMachO())
D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
}
assert(ABI != FloatABI::Invalid && "must select an ABI");
return ABI;
}
static bool hasIntegerMVE(const std::vector<StringRef> &F) {
auto MVE = llvm::find(llvm::reverse(F), "+mve");
auto NoMVE = llvm::find(llvm::reverse(F), "-mve");
return MVE != F.rend() &&
(NoMVE == F.rend() || std::distance(MVE, NoMVE) > 0);
}
void arm::getARMTargetFeatures(const Driver &D, const llvm::Triple &Triple,
const ArgList &Args,
std::vector<StringRef> &Features, bool ForAS) {
bool KernelOrKext =
Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext);
arm::FloatABI ABI = arm::getARMFloatABI(D, Triple, Args);
llvm::Optional<std::pair<const Arg *, StringRef>> WaCPU, WaFPU, WaHDiv,
WaArch;
std::vector<StringRef> ExtensionFeatures;
if (!ForAS) {
if (ABI == arm::FloatABI::Soft)
Features.push_back("+soft-float");
if (ABI != arm::FloatABI::Hard)
Features.push_back("+soft-float-abi");
} else {
for (const Arg *A :
Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
for (StringRef Value : A->getValues()) {
if (Value.startswith("-mfpu=")) {
WaFPU = std::make_pair(A, Value.substr(6));
} else if (Value.startswith("-mcpu=")) {
WaCPU = std::make_pair(A, Value.substr(6));
} else if (Value.startswith("-mhwdiv=")) {
WaHDiv = std::make_pair(A, Value.substr(8));
} else if (Value.startswith("-march=")) {
WaArch = std::make_pair(A, Value.substr(7));
}
}
}
}
if (getReadTPMode(D, Args, Triple, ForAS) == ReadTPMode::Cp15)
Features.push_back("+read-tp-hard");
const Arg *ArchArg = Args.getLastArg(options::OPT_march_EQ);
const Arg *CPUArg = Args.getLastArg(options::OPT_mcpu_EQ);
StringRef ArchName;
StringRef CPUName;
unsigned ArchArgFPUID = llvm::ARM::FK_INVALID;
unsigned CPUArgFPUID = llvm::ARM::FK_INVALID;
if (WaCPU) {
if (CPUArg)
D.Diag(clang::diag::warn_drv_unused_argument)
<< CPUArg->getAsString(Args);
CPUName = WaCPU->second;
CPUArg = WaCPU->first;
} else if (CPUArg)
CPUName = CPUArg->getValue();
if (WaArch) {
if (ArchArg)
D.Diag(clang::diag::warn_drv_unused_argument)
<< ArchArg->getAsString(Args);
ArchName = WaArch->second;
checkARMArchName(D, WaArch->first, Args, ArchName, CPUName,
ExtensionFeatures, Triple, ArchArgFPUID);
} else if (ArchArg) {
ArchName = ArchArg->getValue();
checkARMArchName(D, ArchArg, Args, ArchName, CPUName, ExtensionFeatures,
Triple, ArchArgFPUID);
}
if (CPUName == "native") {
llvm::StringMap<bool> HostFeatures;
if (llvm::sys::getHostCPUFeatures(HostFeatures))
for (auto &F : HostFeatures)
Features.push_back(
Args.MakeArgString((F.second ? "+" : "-") + F.first()));
} else if (!CPUName.empty()) {
DecodeARMFeaturesFromCPU(D, CPUName, Features);
}
if (CPUArg)
checkARMCPUName(D, CPUArg, Args, CPUName, ArchName, ExtensionFeatures,
Triple, CPUArgFPUID);
(void)Args.getLastArg(options::OPT_mtune_EQ);
unsigned FPUID = llvm::ARM::FK_INVALID;
const Arg *FPUArg = Args.getLastArg(options::OPT_mfpu_EQ);
if (WaFPU) {
if (FPUArg)
D.Diag(clang::diag::warn_drv_unused_argument)
<< FPUArg->getAsString(Args);
(void)getARMFPUFeatures(D, WaFPU->first, Args, WaFPU->second, Features);
} else if (FPUArg) {
FPUID = getARMFPUFeatures(D, FPUArg, Args, FPUArg->getValue(), Features);
} else if (Triple.isAndroid() && getARMSubArchVersionNumber(Triple) >= 7) {
const char *AndroidFPU = "neon";
FPUID = llvm::ARM::parseFPU(AndroidFPU);
if (!llvm::ARM::getFPUFeatures(FPUID, Features))
D.Diag(clang::diag::err_drv_clang_unsupported)
<< std::string("-mfpu=") + AndroidFPU;
} else {
if (!ForAS) {
std::string CPU = arm::getARMTargetCPU(CPUName, ArchName, Triple);
llvm::ARM::ArchKind ArchKind =
arm::getLLVMArchKindForARM(CPU, ArchName, Triple);
FPUID = llvm::ARM::getDefaultFPU(CPU, ArchKind);
(void)llvm::ARM::getFPUFeatures(FPUID, Features);
}
}
Features.insert(std::end(Features),
std::begin(ExtensionFeatures), std::end(ExtensionFeatures));
const Arg *HDivArg = Args.getLastArg(options::OPT_mhwdiv_EQ);
if (WaHDiv) {
if (HDivArg)
D.Diag(clang::diag::warn_drv_unused_argument)
<< HDivArg->getAsString(Args);
getARMHWDivFeatures(D, WaHDiv->first, Args, WaHDiv->second, Features);
} else if (HDivArg)
getARMHWDivFeatures(D, HDivArg, Args, HDivArg->getValue(), Features);
const auto ItRNoFullFP16 = std::find(Features.rbegin(), Features.rend(), "-fullfp16");
const auto ItRFP16FML = std::find(Features.rbegin(), Features.rend(), "+fp16fml");
if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v8_4a) {
const auto ItRFullFP16 = std::find(Features.rbegin(), Features.rend(), "+fullfp16");
if (ItRFullFP16 < ItRNoFullFP16 && ItRFullFP16 < ItRFP16FML) {
if (std::find(Features.rbegin(), ItRFullFP16, "-fp16fml") == ItRFullFP16)
Features.push_back("+fp16fml");
}
else
goto fp16_fml_fallthrough;
}
else {
fp16_fml_fallthrough:
if (ItRNoFullFP16 < ItRFP16FML)
Features.push_back("-fp16fml");
else if (ItRNoFullFP16 > ItRFP16FML)
Features.push_back("+fullfp16");
}
if (ABI == arm::FloatABI::Soft) {
llvm::ARM::getFPUFeatures(llvm::ARM::FK_NONE, Features);
Features.insert(Features.end(), {"-dotprod", "-fp16fml", "-bf16", "-mve",
"-mve.fp", "-fpregs"});
} else if (FPUID == llvm::ARM::FK_NONE ||
ArchArgFPUID == llvm::ARM::FK_NONE ||
CPUArgFPUID == llvm::ARM::FK_NONE) {
Features.insert(Features.end(),
{"-dotprod", "-fp16fml", "-bf16", "-mve.fp"});
if (!hasIntegerMVE(Features))
Features.emplace_back("-fpregs");
}
if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
if (A->getOption().matches(options::OPT_mcrc))
Features.push_back("+crc");
else
Features.push_back("-crc");
}
bool HasSHA2 = false;
bool HasAES = false;
const auto ItCrypto =
llvm::find_if(llvm::reverse(Features), [](const StringRef F) {
return F.contains("crypto");
});
const auto ItSHA2 =
llvm::find_if(llvm::reverse(Features), [](const StringRef F) {
return F.contains("crypto") || F.contains("sha2");
});
const auto ItAES =
llvm::find_if(llvm::reverse(Features), [](const StringRef F) {
return F.contains("crypto") || F.contains("aes");
});
const bool FoundSHA2 = ItSHA2 != Features.rend();
const bool FoundAES = ItAES != Features.rend();
if (FoundSHA2)
HasSHA2 = ItSHA2->take_front() == "+";
if (FoundAES)
HasAES = ItAES->take_front() == "+";
if (ItCrypto != Features.rend()) {
if (HasSHA2 && HasAES)
Features.push_back("+crypto");
else
Features.push_back("-crypto");
if (HasSHA2)
Features.push_back("+sha2");
else
Features.push_back("-sha2");
if (HasAES)
Features.push_back("+aes");
else
Features.push_back("-aes");
}
if (HasSHA2 || HasAES) {
StringRef ArchSuffix = arm::getLLVMArchSuffixForARM(
arm::getARMTargetCPU(CPUName, ArchName, Triple), ArchName, Triple);
llvm::ARM::ProfileKind ArchProfile =
llvm::ARM::parseArchProfile(ArchSuffix);
if (!((llvm::ARM::parseArchVersion(ArchSuffix) >= 8) &&
(ArchProfile == llvm::ARM::ProfileKind::A ||
ArchProfile == llvm::ARM::ProfileKind::R))) {
if (HasSHA2)
D.Diag(clang::diag::warn_target_unsupported_extension)
<< "sha2"
<< llvm::ARM::getArchName(llvm::ARM::parseArch(ArchSuffix));
if (HasAES)
D.Diag(clang::diag::warn_target_unsupported_extension)
<< "aes"
<< llvm::ARM::getArchName(llvm::ARM::parseArch(ArchSuffix));
if (!Args.hasArg(options::OPT_fno_integrated_as)) {
Features.push_back("-sha2");
Features.push_back("-aes");
}
}
}
if (Arg *A = Args.getLastArg(options::OPT_mframe_chain)) {
StringRef FrameChainOption = A->getValue();
if (FrameChainOption.startswith("aapcs"))
Features.push_back("+aapcs-frame-chain");
if (FrameChainOption == "aapcs+leaf")
Features.push_back("+aapcs-frame-chain-leaf");
}
if (Args.getLastArg(options::OPT_mcmse))
Features.push_back("+8msecext");
if (Arg *A = Args.getLastArg(options::OPT_mfix_cmse_cve_2021_35465,
options::OPT_mno_fix_cmse_cve_2021_35465)) {
if (!Args.getLastArg(options::OPT_mcmse))
D.Diag(diag::err_opt_not_valid_without_opt)
<< A->getOption().getName() << "-mcmse";
if (A->getOption().matches(options::OPT_mfix_cmse_cve_2021_35465))
Features.push_back("+fix-cmse-cve-2021-35465");
else
Features.push_back("-fix-cmse-cve-2021-35465");
}
if (Arg *A = Args.getLastArg(options::OPT_mfix_cortex_a57_aes_1742098,
options::OPT_mno_fix_cortex_a57_aes_1742098)) {
if (A->getOption().matches(options::OPT_mfix_cortex_a57_aes_1742098)) {
Features.push_back("+fix-cortex-a57-aes-1742098");
} else {
Features.push_back("-fix-cortex-a57-aes-1742098");
}
}
if (Arg *A = Args.getLastArg(options::OPT_mlong_calls,
options::OPT_mno_long_calls)) {
if (A->getOption().matches(options::OPT_mlong_calls))
Features.push_back("+long-calls");
} else if (KernelOrKext && (!Triple.isiOS() || Triple.isOSVersionLT(6)) &&
!Triple.isWatchOS()) {
Features.push_back("+long-calls");
}
if (!ForAS) {
if (Arg *A = Args.getLastArg(options::OPT_mexecute_only, options::OPT_mno_execute_only)) {
if (A->getOption().matches(options::OPT_mexecute_only)) {
if (getARMSubArchVersionNumber(Triple) < 7 &&
llvm::ARM::parseArch(Triple.getArchName()) != llvm::ARM::ArchKind::ARMV6T2)
D.Diag(diag::err_target_unsupported_execute_only) << Triple.getArchName();
else if (Arg *B = Args.getLastArg(options::OPT_mno_movt))
D.Diag(diag::err_opt_not_valid_with_opt) << A->getAsString(Args) << B->getAsString(Args);
else if (Arg *B = Args.getLastArg(options::OPT_mlong_calls, options::OPT_mno_long_calls)) {
if (B->getOption().matches(options::OPT_mlong_calls))
D.Diag(diag::err_opt_not_valid_with_opt) << A->getAsString(Args) << B->getAsString(Args);
}
Features.push_back("+execute-only");
}
}
}
if (KernelOrKext) {
Features.push_back("+strict-align");
} else if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
options::OPT_munaligned_access)) {
if (A->getOption().matches(options::OPT_munaligned_access)) {
if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m)
D.Diag(diag::err_target_unsupported_unaligned) << "v6m";
else if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v8m_baseline)
D.Diag(diag::err_target_unsupported_unaligned) << "v8m.base";
} else
Features.push_back("+strict-align");
} else {
int VersionNum = getARMSubArchVersionNumber(Triple);
if (Triple.isOSDarwin() || Triple.isOSNetBSD()) {
if (VersionNum < 6 ||
Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m)
Features.push_back("+strict-align");
} else if (Triple.isOSLinux() || Triple.isOSNaCl() ||
Triple.isOSWindows()) {
if (VersionNum < 7)
Features.push_back("+strict-align");
} else
Features.push_back("+strict-align");
}
if (Args.hasArg(options::OPT_ffixed_r9))
Features.push_back("+reserve-r9");
if (KernelOrKext || Args.hasArg(options::OPT_mno_movt))
Features.push_back("+no-movt");
if (Args.hasArg(options::OPT_mno_neg_immediates))
Features.push_back("+no-neg-immediates");
if (Arg *A = Args.getLastArg(options::OPT_mharden_sls_EQ)) {
StringRef Scope = A->getValue();
bool EnableRetBr = false;
bool EnableBlr = false;
bool DisableComdat = false;
if (Scope != "none") {
SmallVector<StringRef, 4> Opts;
Scope.split(Opts, ",");
for (auto Opt : Opts) {
Opt = Opt.trim();
if (Opt == "all") {
EnableBlr = true;
EnableRetBr = true;
continue;
}
if (Opt == "retbr") {
EnableRetBr = true;
continue;
}
if (Opt == "blr") {
EnableBlr = true;
continue;
}
if (Opt == "comdat") {
DisableComdat = false;
continue;
}
if (Opt == "nocomdat") {
DisableComdat = true;
continue;
}
D.Diag(diag::err_drv_unsupported_option_argument)
<< A->getOption().getName() << Scope;
break;
}
}
if (EnableRetBr || EnableBlr)
if (!(isARMAProfile(Triple) && getARMSubArchVersionNumber(Triple) >= 7))
D.Diag(diag::err_sls_hardening_arm_not_supported)
<< Scope << A->getAsString(Args);
if (EnableRetBr)
Features.push_back("+harden-sls-retbr");
if (EnableBlr)
Features.push_back("+harden-sls-blr");
if (DisableComdat) {
Features.push_back("+harden-sls-nocomdat");
}
}
if (Args.getLastArg(options::OPT_mno_bti_at_return_twice))
Features.push_back("+no-bti-at-return-twice");
}
std::string arm::getARMArch(StringRef Arch, const llvm::Triple &Triple) {
std::string MArch;
if (!Arch.empty())
MArch = std::string(Arch);
else
MArch = std::string(Triple.getArchName());
MArch = StringRef(MArch).split("+").first.lower();
if (MArch == "native") {
std::string CPU = std::string(llvm::sys::getHostCPUName());
if (CPU != "generic") {
StringRef Suffix = arm::getLLVMArchSuffixForARM(CPU, MArch, Triple);
if (Suffix.empty())
MArch = "";
else
MArch = std::string("arm") + Suffix.str();
}
}
return MArch;
}
StringRef arm::getARMCPUForMArch(StringRef Arch, const llvm::Triple &Triple) {
std::string MArch = getARMArch(Arch, Triple);
if (MArch.empty())
return StringRef();
return Triple.getARMCPUForArch(MArch);
}
std::string arm::getARMTargetCPU(StringRef CPU, StringRef Arch,
const llvm::Triple &Triple) {
if (!CPU.empty()) {
std::string MCPU = StringRef(CPU).split("+").first.lower();
if (MCPU == "native")
return std::string(llvm::sys::getHostCPUName());
else
return MCPU;
}
return std::string(getARMCPUForMArch(Arch, Triple));
}
llvm::ARM::ArchKind arm::getLLVMArchKindForARM(StringRef CPU, StringRef Arch,
const llvm::Triple &Triple) {
llvm::ARM::ArchKind ArchKind;
if (CPU == "generic" || CPU.empty()) {
std::string ARMArch = tools::arm::getARMArch(Arch, Triple);
ArchKind = llvm::ARM::parseArch(ARMArch);
if (ArchKind == llvm::ARM::ArchKind::INVALID)
ArchKind = llvm::ARM::parseCPUArch(Triple.getARMCPUForArch(ARMArch));
} else {
ArchKind = (Arch == "armv7k" || Arch == "thumbv7k")
? llvm::ARM::ArchKind::ARMV7K
: llvm::ARM::parseCPUArch(CPU);
}
return ArchKind;
}
StringRef arm::getLLVMArchSuffixForARM(StringRef CPU, StringRef Arch,
const llvm::Triple &Triple) {
llvm::ARM::ArchKind ArchKind = getLLVMArchKindForARM(CPU, Arch, Triple);
if (ArchKind == llvm::ARM::ArchKind::INVALID)
return "";
return llvm::ARM::getSubArch(ArchKind);
}
void arm::appendBE8LinkFlag(const ArgList &Args, ArgStringList &CmdArgs,
const llvm::Triple &Triple) {
if (Args.hasArg(options::OPT_r))
return;
if (arm::getARMSubArchVersionNumber(Triple) >= 7 || arm::isARMMProfile(Triple))
CmdArgs.push_back("--be8");
}