10b57cec5SDimitry Andric //===--- MSP430.cpp - MSP430 Helpers for Tools ------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "MSP430.h" 100b57cec5SDimitry Andric #include "CommonArgs.h" 110b57cec5SDimitry Andric #include "Gnu.h" 120b57cec5SDimitry Andric #include "InputInfo.h" 130b57cec5SDimitry Andric #include "clang/Driver/Compilation.h" 140b57cec5SDimitry Andric #include "clang/Driver/Multilib.h" 150b57cec5SDimitry Andric #include "clang/Driver/Options.h" 160b57cec5SDimitry Andric #include "llvm/Option/ArgList.h" 170b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h" 180b57cec5SDimitry Andric #include "llvm/Support/Path.h" 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric using namespace clang::driver; 210b57cec5SDimitry Andric using namespace clang::driver::toolchains; 220b57cec5SDimitry Andric using namespace clang::driver::tools; 230b57cec5SDimitry Andric using namespace clang; 240b57cec5SDimitry Andric using namespace llvm::opt; 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric static bool isSupportedMCU(const StringRef MCU) { 270b57cec5SDimitry Andric return llvm::StringSwitch<bool>(MCU) 280b57cec5SDimitry Andric #define MSP430_MCU(NAME) .Case(NAME, true) 290b57cec5SDimitry Andric #include "clang/Basic/MSP430Target.def" 300b57cec5SDimitry Andric .Default(false); 310b57cec5SDimitry Andric } 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric static StringRef getSupportedHWMult(const Arg *MCU) { 340b57cec5SDimitry Andric if (!MCU) 350b57cec5SDimitry Andric return "none"; 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric return llvm::StringSwitch<StringRef>(MCU->getValue()) 380b57cec5SDimitry Andric #define MSP430_MCU_FEAT(NAME, HWMULT) .Case(NAME, HWMULT) 390b57cec5SDimitry Andric #include "clang/Basic/MSP430Target.def" 400b57cec5SDimitry Andric .Default("none"); 410b57cec5SDimitry Andric } 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric static StringRef getHWMultLib(const ArgList &Args) { 440b57cec5SDimitry Andric StringRef HWMult = Args.getLastArgValue(options::OPT_mhwmult_EQ, "auto"); 450b57cec5SDimitry Andric if (HWMult == "auto") { 460b57cec5SDimitry Andric HWMult = getSupportedHWMult(Args.getLastArg(options::OPT_mmcu_EQ)); 470b57cec5SDimitry Andric } 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric return llvm::StringSwitch<StringRef>(HWMult) 500b57cec5SDimitry Andric .Case("16bit", "-lmul_16") 510b57cec5SDimitry Andric .Case("32bit", "-lmul_32") 520b57cec5SDimitry Andric .Case("f5series", "-lmul_f5") 530b57cec5SDimitry Andric .Default("-lmul_none"); 540b57cec5SDimitry Andric } 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric void msp430::getMSP430TargetFeatures(const Driver &D, const ArgList &Args, 570b57cec5SDimitry Andric std::vector<StringRef> &Features) { 580b57cec5SDimitry Andric const Arg *MCU = Args.getLastArg(options::OPT_mmcu_EQ); 590b57cec5SDimitry Andric if (MCU && !isSupportedMCU(MCU->getValue())) { 600b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) << MCU->getValue(); 610b57cec5SDimitry Andric return; 620b57cec5SDimitry Andric } 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric const Arg *HWMultArg = Args.getLastArg(options::OPT_mhwmult_EQ); 650b57cec5SDimitry Andric if (!MCU && !HWMultArg) 660b57cec5SDimitry Andric return; 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric StringRef HWMult = HWMultArg ? HWMultArg->getValue() : "auto"; 690b57cec5SDimitry Andric StringRef SupportedHWMult = getSupportedHWMult(MCU); 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric if (HWMult == "auto") { 720b57cec5SDimitry Andric // 'auto' - deduce hw multiplier support based on mcu name provided. 730b57cec5SDimitry Andric // If no mcu name is provided, assume no hw multiplier is supported. 740b57cec5SDimitry Andric if (!MCU) 750b57cec5SDimitry Andric D.Diag(clang::diag::warn_drv_msp430_hwmult_no_device); 760b57cec5SDimitry Andric HWMult = SupportedHWMult; 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric if (HWMult == "none") { 800b57cec5SDimitry Andric // 'none' - disable hw multiplier. 810b57cec5SDimitry Andric Features.push_back("-hwmult16"); 820b57cec5SDimitry Andric Features.push_back("-hwmult32"); 830b57cec5SDimitry Andric Features.push_back("-hwmultf5"); 840b57cec5SDimitry Andric return; 850b57cec5SDimitry Andric } 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric if (MCU && SupportedHWMult == "none") 880b57cec5SDimitry Andric D.Diag(clang::diag::warn_drv_msp430_hwmult_unsupported) << HWMult; 890b57cec5SDimitry Andric if (MCU && HWMult != SupportedHWMult) 900b57cec5SDimitry Andric D.Diag(clang::diag::warn_drv_msp430_hwmult_mismatch) 910b57cec5SDimitry Andric << SupportedHWMult << HWMult; 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric if (HWMult == "16bit") { 940b57cec5SDimitry Andric // '16bit' - for 16-bit only hw multiplier. 950b57cec5SDimitry Andric Features.push_back("+hwmult16"); 960b57cec5SDimitry Andric } else if (HWMult == "32bit") { 970b57cec5SDimitry Andric // '32bit' - for 16/32-bit hw multiplier. 980b57cec5SDimitry Andric Features.push_back("+hwmult32"); 990b57cec5SDimitry Andric } else if (HWMult == "f5series") { 1000b57cec5SDimitry Andric // 'f5series' - for 16/32-bit hw multiplier supported by F5 series mcus. 1010b57cec5SDimitry Andric Features.push_back("+hwmultf5"); 1020b57cec5SDimitry Andric } else { 1030b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_unsupported_option_argument) 1040b57cec5SDimitry Andric << HWMultArg->getAsString(Args) << HWMult; 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric } 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric /// MSP430 Toolchain 1090b57cec5SDimitry Andric MSP430ToolChain::MSP430ToolChain(const Driver &D, const llvm::Triple &Triple, 1100b57cec5SDimitry Andric const ArgList &Args) 1110b57cec5SDimitry Andric : Generic_ELF(D, Triple, Args) { 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric StringRef MultilibSuf = ""; 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric GCCInstallation.init(Triple, Args); 1160b57cec5SDimitry Andric if (GCCInstallation.isValid()) { 1170b57cec5SDimitry Andric MultilibSuf = GCCInstallation.getMultilib().gccSuffix(); 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric SmallString<128> GCCBinPath; 1200b57cec5SDimitry Andric llvm::sys::path::append(GCCBinPath, 1210b57cec5SDimitry Andric GCCInstallation.getParentLibPath(), "..", "bin"); 1220b57cec5SDimitry Andric addPathIfExists(D, GCCBinPath, getProgramPaths()); 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric SmallString<128> GCCRtPath; 1250b57cec5SDimitry Andric llvm::sys::path::append(GCCRtPath, 1260b57cec5SDimitry Andric GCCInstallation.getInstallPath(), MultilibSuf); 1270b57cec5SDimitry Andric addPathIfExists(D, GCCRtPath, getFilePaths()); 1280b57cec5SDimitry Andric } 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric SmallString<128> SysRootDir(computeSysRoot()); 131*e8d8bef9SDimitry Andric llvm::sys::path::append(SysRootDir, "msp430-elf", "lib", MultilibSuf); 1320b57cec5SDimitry Andric addPathIfExists(D, SysRootDir, getFilePaths()); 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric std::string MSP430ToolChain::computeSysRoot() const { 1360b57cec5SDimitry Andric if (!getDriver().SysRoot.empty()) 1370b57cec5SDimitry Andric return getDriver().SysRoot; 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric SmallString<128> Dir; 1400b57cec5SDimitry Andric if (GCCInstallation.isValid()) 141*e8d8bef9SDimitry Andric llvm::sys::path::append(Dir, GCCInstallation.getParentLibPath(), ".."); 1420b57cec5SDimitry Andric else 143*e8d8bef9SDimitry Andric llvm::sys::path::append(Dir, getDriver().Dir, ".."); 1440b57cec5SDimitry Andric 1455ffd83dbSDimitry Andric return std::string(Dir.str()); 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric void MSP430ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 1490b57cec5SDimitry Andric ArgStringList &CC1Args) const { 1500b57cec5SDimitry Andric if (DriverArgs.hasArg(options::OPT_nostdinc) || 1510b57cec5SDimitry Andric DriverArgs.hasArg(options::OPT_nostdlibinc)) 1520b57cec5SDimitry Andric return; 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric SmallString<128> Dir(computeSysRoot()); 155*e8d8bef9SDimitry Andric llvm::sys::path::append(Dir, "msp430-elf", "include"); 1560b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, Dir.str()); 1570b57cec5SDimitry Andric } 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric void MSP430ToolChain::addClangTargetOptions(const ArgList &DriverArgs, 1600b57cec5SDimitry Andric ArgStringList &CC1Args, 1610b57cec5SDimitry Andric Action::OffloadKind) const { 1620b57cec5SDimitry Andric CC1Args.push_back("-nostdsysteminc"); 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric const auto *MCUArg = DriverArgs.getLastArg(options::OPT_mmcu_EQ); 1650b57cec5SDimitry Andric if (!MCUArg) 1660b57cec5SDimitry Andric return; 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric const StringRef MCU = MCUArg->getValue(); 1690b57cec5SDimitry Andric if (MCU.startswith("msp430i")) { 1700b57cec5SDimitry Andric // 'i' should be in lower case as it's defined in TI MSP430-GCC headers 1710b57cec5SDimitry Andric CC1Args.push_back(DriverArgs.MakeArgString( 1720b57cec5SDimitry Andric "-D__MSP430i" + MCU.drop_front(7).upper() + "__")); 1730b57cec5SDimitry Andric } else { 1740b57cec5SDimitry Andric CC1Args.push_back(DriverArgs.MakeArgString("-D__" + MCU.upper() + "__")); 1750b57cec5SDimitry Andric } 1760b57cec5SDimitry Andric } 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric Tool *MSP430ToolChain::buildLinker() const { 1790b57cec5SDimitry Andric return new tools::msp430::Linker(*this); 1800b57cec5SDimitry Andric } 1810b57cec5SDimitry Andric 182*e8d8bef9SDimitry Andric void msp430::Linker::AddStartFiles(bool UseExceptions, const ArgList &Args, 183*e8d8bef9SDimitry Andric ArgStringList &CmdArgs) const { 184*e8d8bef9SDimitry Andric const ToolChain &ToolChain = getToolChain(); 185*e8d8bef9SDimitry Andric 186*e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o"))); 187*e8d8bef9SDimitry Andric const char *crtbegin = UseExceptions ? "crtbegin.o" : "crtbegin_no_eh.o"; 188*e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin))); 189*e8d8bef9SDimitry Andric } 190*e8d8bef9SDimitry Andric 191*e8d8bef9SDimitry Andric void msp430::Linker::AddDefaultLibs(const llvm::opt::ArgList &Args, 192*e8d8bef9SDimitry Andric llvm::opt::ArgStringList &CmdArgs) const { 193*e8d8bef9SDimitry Andric const ToolChain &ToolChain = getToolChain(); 194*e8d8bef9SDimitry Andric const Driver &D = ToolChain.getDriver(); 195*e8d8bef9SDimitry Andric 196*e8d8bef9SDimitry Andric CmdArgs.push_back("--start-group"); 197*e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString(getHWMultLib(Args))); 198*e8d8bef9SDimitry Andric CmdArgs.push_back("-lc"); 199*e8d8bef9SDimitry Andric AddRunTimeLibs(ToolChain, D, CmdArgs, Args); 200*e8d8bef9SDimitry Andric CmdArgs.push_back("-lcrt"); 201*e8d8bef9SDimitry Andric 202*e8d8bef9SDimitry Andric if (Args.hasArg(options::OPT_msim)) { 203*e8d8bef9SDimitry Andric CmdArgs.push_back("-lsim"); 204*e8d8bef9SDimitry Andric 205*e8d8bef9SDimitry Andric // msp430-sim.ld relies on __crt0_call_exit being implicitly .refsym-ed 206*e8d8bef9SDimitry Andric // in main() by msp430-gcc. 207*e8d8bef9SDimitry Andric // This workaround should work seamlessly unless the compilation unit that 208*e8d8bef9SDimitry Andric // contains main() is compiled by clang and then passed to 209*e8d8bef9SDimitry Andric // gcc compiler driver for linkage. 210*e8d8bef9SDimitry Andric CmdArgs.push_back("--undefined=__crt0_call_exit"); 211*e8d8bef9SDimitry Andric } else 212*e8d8bef9SDimitry Andric CmdArgs.push_back("-lnosys"); 213*e8d8bef9SDimitry Andric 214*e8d8bef9SDimitry Andric CmdArgs.push_back("--end-group"); 215*e8d8bef9SDimitry Andric AddRunTimeLibs(ToolChain, D, CmdArgs, Args); 216*e8d8bef9SDimitry Andric } 217*e8d8bef9SDimitry Andric 218*e8d8bef9SDimitry Andric void msp430::Linker::AddEndFiles(bool UseExceptions, const ArgList &Args, 219*e8d8bef9SDimitry Andric ArgStringList &CmdArgs) const { 220*e8d8bef9SDimitry Andric const ToolChain &ToolChain = getToolChain(); 221*e8d8bef9SDimitry Andric const Driver &D = ToolChain.getDriver(); 222*e8d8bef9SDimitry Andric 223*e8d8bef9SDimitry Andric const char *crtend = UseExceptions ? "crtend.o" : "crtend_no_eh.o"; 224*e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend))); 225*e8d8bef9SDimitry Andric AddRunTimeLibs(ToolChain, D, CmdArgs, Args); 226*e8d8bef9SDimitry Andric } 227*e8d8bef9SDimitry Andric 228*e8d8bef9SDimitry Andric static void AddSspArgs(const ArgList &Args, ArgStringList &CmdArgs) { 229*e8d8bef9SDimitry Andric Arg *SspFlag = Args.getLastArg( 230*e8d8bef9SDimitry Andric options::OPT_fno_stack_protector, options::OPT_fstack_protector, 231*e8d8bef9SDimitry Andric options::OPT_fstack_protector_all, options::OPT_fstack_protector_strong); 232*e8d8bef9SDimitry Andric 233*e8d8bef9SDimitry Andric if (SspFlag && 234*e8d8bef9SDimitry Andric !SspFlag->getOption().matches(options::OPT_fno_stack_protector)) { 235*e8d8bef9SDimitry Andric CmdArgs.push_back("-lssp_nonshared"); 236*e8d8bef9SDimitry Andric CmdArgs.push_back("-lssp"); 237*e8d8bef9SDimitry Andric } 238*e8d8bef9SDimitry Andric } 239*e8d8bef9SDimitry Andric 240*e8d8bef9SDimitry Andric static void AddImplicitLinkerScript(const std::string SysRoot, 241*e8d8bef9SDimitry Andric const ArgList &Args, 242*e8d8bef9SDimitry Andric ArgStringList &CmdArgs) { 243*e8d8bef9SDimitry Andric if (Args.hasArg(options::OPT_T)) 244*e8d8bef9SDimitry Andric return; 245*e8d8bef9SDimitry Andric 246*e8d8bef9SDimitry Andric if (Args.hasArg(options::OPT_msim)) { 247*e8d8bef9SDimitry Andric CmdArgs.push_back("-Tmsp430-sim.ld"); 248*e8d8bef9SDimitry Andric return; 249*e8d8bef9SDimitry Andric } 250*e8d8bef9SDimitry Andric 251*e8d8bef9SDimitry Andric const Arg *MCUArg = Args.getLastArg(options::OPT_mmcu_EQ); 252*e8d8bef9SDimitry Andric if (!MCUArg) 253*e8d8bef9SDimitry Andric return; 254*e8d8bef9SDimitry Andric 255*e8d8bef9SDimitry Andric SmallString<128> MCULinkerScriptPath(SysRoot); 256*e8d8bef9SDimitry Andric llvm::sys::path::append(MCULinkerScriptPath, "include"); 257*e8d8bef9SDimitry Andric // -L because <mcu>.ld INCLUDEs <mcu>_symbols.ld 258*e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-L" + MCULinkerScriptPath)); 259*e8d8bef9SDimitry Andric CmdArgs.push_back( 260*e8d8bef9SDimitry Andric Args.MakeArgString("-T" + StringRef(MCUArg->getValue()) + ".ld")); 261*e8d8bef9SDimitry Andric } 262*e8d8bef9SDimitry Andric 2630b57cec5SDimitry Andric void msp430::Linker::ConstructJob(Compilation &C, const JobAction &JA, 2640b57cec5SDimitry Andric const InputInfo &Output, 2650b57cec5SDimitry Andric const InputInfoList &Inputs, 2660b57cec5SDimitry Andric const ArgList &Args, 2670b57cec5SDimitry Andric const char *LinkingOutput) const { 2680b57cec5SDimitry Andric const ToolChain &ToolChain = getToolChain(); 2690b57cec5SDimitry Andric const Driver &D = ToolChain.getDriver(); 2700b57cec5SDimitry Andric std::string Linker = ToolChain.GetProgramPath(getShortName()); 2710b57cec5SDimitry Andric ArgStringList CmdArgs; 272*e8d8bef9SDimitry Andric bool UseExceptions = Args.hasFlag(options::OPT_fexceptions, 273*e8d8bef9SDimitry Andric options::OPT_fno_exceptions, false); 274*e8d8bef9SDimitry Andric bool UseStartAndEndFiles = !Args.hasArg(options::OPT_nostdlib, options::OPT_r, 275*e8d8bef9SDimitry Andric options::OPT_nostartfiles); 2760b57cec5SDimitry Andric 277*e8d8bef9SDimitry Andric if (Args.hasArg(options::OPT_mrelax)) 278*e8d8bef9SDimitry Andric CmdArgs.push_back("--relax"); 279*e8d8bef9SDimitry Andric if (!Args.hasArg(options::OPT_r, options::OPT_g_Group)) 280*e8d8bef9SDimitry Andric CmdArgs.push_back("--gc-sections"); 281*e8d8bef9SDimitry Andric 282*e8d8bef9SDimitry Andric Args.AddAllArgs(CmdArgs, { 283*e8d8bef9SDimitry Andric options::OPT_e, 284*e8d8bef9SDimitry Andric options::OPT_n, 285*e8d8bef9SDimitry Andric options::OPT_s, 286*e8d8bef9SDimitry Andric options::OPT_t, 287*e8d8bef9SDimitry Andric options::OPT_u, 288*e8d8bef9SDimitry Andric }); 289*e8d8bef9SDimitry Andric 290*e8d8bef9SDimitry Andric if (UseStartAndEndFiles) 291*e8d8bef9SDimitry Andric AddStartFiles(UseExceptions, Args, CmdArgs); 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_L); 2940b57cec5SDimitry Andric ToolChain.AddFilePathLibArgs(Args, CmdArgs); 2950b57cec5SDimitry Andric AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA); 2960b57cec5SDimitry Andric 297*e8d8bef9SDimitry Andric if (!Args.hasArg(options::OPT_nostdlib, options::OPT_r, 298*e8d8bef9SDimitry Andric options::OPT_nodefaultlibs)) { 299*e8d8bef9SDimitry Andric AddSspArgs(Args, CmdArgs); 300*e8d8bef9SDimitry Andric AddRunTimeLibs(ToolChain, D, CmdArgs, Args); 301*e8d8bef9SDimitry Andric if (!Args.hasArg(options::OPT_nolibc)) { 302*e8d8bef9SDimitry Andric AddDefaultLibs(Args, CmdArgs); 303*e8d8bef9SDimitry Andric AddImplicitLinkerScript(D.SysRoot, Args, CmdArgs); 3040b57cec5SDimitry Andric } 305*e8d8bef9SDimitry Andric } 3060b57cec5SDimitry Andric 307*e8d8bef9SDimitry Andric if (UseStartAndEndFiles) 308*e8d8bef9SDimitry Andric AddEndFiles(UseExceptions, Args, CmdArgs); 309*e8d8bef9SDimitry Andric 3100b57cec5SDimitry Andric CmdArgs.push_back("-o"); 3110b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 312*e8d8bef9SDimitry Andric 313*e8d8bef9SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_T); 314*e8d8bef9SDimitry Andric 315*e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>( 316*e8d8bef9SDimitry Andric JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker), 317*e8d8bef9SDimitry Andric CmdArgs, Inputs, Output)); 3180b57cec5SDimitry Andric } 319