Home | History | Annotate | Line # | Download | only in ToolChains
      1      1.1  joerg //===--- MSP430.cpp - MSP430 Helpers for Tools ------------------*- C++ -*-===//
      2      1.1  joerg //
      3      1.1  joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4      1.1  joerg // See https://llvm.org/LICENSE.txt for license information.
      5      1.1  joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6      1.1  joerg //
      7      1.1  joerg //===----------------------------------------------------------------------===//
      8      1.1  joerg 
      9      1.1  joerg #include "MSP430.h"
     10      1.1  joerg #include "CommonArgs.h"
     11      1.1  joerg #include "Gnu.h"
     12      1.1  joerg #include "InputInfo.h"
     13      1.1  joerg #include "clang/Driver/Compilation.h"
     14      1.1  joerg #include "clang/Driver/Multilib.h"
     15      1.1  joerg #include "clang/Driver/Options.h"
     16      1.1  joerg #include "llvm/Option/ArgList.h"
     17      1.1  joerg #include "llvm/Support/FileSystem.h"
     18      1.1  joerg #include "llvm/Support/Path.h"
     19      1.1  joerg 
     20      1.1  joerg using namespace clang::driver;
     21      1.1  joerg using namespace clang::driver::toolchains;
     22      1.1  joerg using namespace clang::driver::tools;
     23      1.1  joerg using namespace clang;
     24      1.1  joerg using namespace llvm::opt;
     25      1.1  joerg 
     26      1.1  joerg static bool isSupportedMCU(const StringRef MCU) {
     27      1.1  joerg   return llvm::StringSwitch<bool>(MCU)
     28      1.1  joerg #define MSP430_MCU(NAME) .Case(NAME, true)
     29      1.1  joerg #include "clang/Basic/MSP430Target.def"
     30      1.1  joerg       .Default(false);
     31      1.1  joerg }
     32      1.1  joerg 
     33      1.1  joerg static StringRef getSupportedHWMult(const Arg *MCU) {
     34      1.1  joerg   if (!MCU)
     35      1.1  joerg     return "none";
     36      1.1  joerg 
     37      1.1  joerg   return llvm::StringSwitch<StringRef>(MCU->getValue())
     38      1.1  joerg #define MSP430_MCU_FEAT(NAME, HWMULT) .Case(NAME, HWMULT)
     39      1.1  joerg #include "clang/Basic/MSP430Target.def"
     40      1.1  joerg       .Default("none");
     41      1.1  joerg }
     42      1.1  joerg 
     43      1.1  joerg static StringRef getHWMultLib(const ArgList &Args) {
     44      1.1  joerg   StringRef HWMult = Args.getLastArgValue(options::OPT_mhwmult_EQ, "auto");
     45      1.1  joerg   if (HWMult == "auto") {
     46      1.1  joerg     HWMult = getSupportedHWMult(Args.getLastArg(options::OPT_mmcu_EQ));
     47      1.1  joerg   }
     48      1.1  joerg 
     49      1.1  joerg   return llvm::StringSwitch<StringRef>(HWMult)
     50      1.1  joerg       .Case("16bit", "-lmul_16")
     51      1.1  joerg       .Case("32bit", "-lmul_32")
     52      1.1  joerg       .Case("f5series", "-lmul_f5")
     53      1.1  joerg       .Default("-lmul_none");
     54      1.1  joerg }
     55      1.1  joerg 
     56      1.1  joerg void msp430::getMSP430TargetFeatures(const Driver &D, const ArgList &Args,
     57      1.1  joerg                                      std::vector<StringRef> &Features) {
     58      1.1  joerg   const Arg *MCU = Args.getLastArg(options::OPT_mmcu_EQ);
     59      1.1  joerg   if (MCU && !isSupportedMCU(MCU->getValue())) {
     60      1.1  joerg     D.Diag(diag::err_drv_clang_unsupported) << MCU->getValue();
     61      1.1  joerg     return;
     62      1.1  joerg   }
     63      1.1  joerg 
     64      1.1  joerg   const Arg *HWMultArg = Args.getLastArg(options::OPT_mhwmult_EQ);
     65      1.1  joerg   if (!MCU && !HWMultArg)
     66      1.1  joerg     return;
     67      1.1  joerg 
     68      1.1  joerg   StringRef HWMult = HWMultArg ? HWMultArg->getValue() : "auto";
     69      1.1  joerg   StringRef SupportedHWMult = getSupportedHWMult(MCU);
     70      1.1  joerg 
     71      1.1  joerg   if (HWMult == "auto") {
     72      1.1  joerg     // 'auto' - deduce hw multiplier support based on mcu name provided.
     73      1.1  joerg     // If no mcu name is provided, assume no hw multiplier is supported.
     74      1.1  joerg     if (!MCU)
     75      1.1  joerg       D.Diag(clang::diag::warn_drv_msp430_hwmult_no_device);
     76      1.1  joerg     HWMult = SupportedHWMult;
     77      1.1  joerg   }
     78      1.1  joerg 
     79      1.1  joerg   if (HWMult == "none") {
     80      1.1  joerg     // 'none' - disable hw multiplier.
     81      1.1  joerg     Features.push_back("-hwmult16");
     82      1.1  joerg     Features.push_back("-hwmult32");
     83      1.1  joerg     Features.push_back("-hwmultf5");
     84      1.1  joerg     return;
     85      1.1  joerg   }
     86      1.1  joerg 
     87      1.1  joerg   if (MCU && SupportedHWMult == "none")
     88      1.1  joerg     D.Diag(clang::diag::warn_drv_msp430_hwmult_unsupported) << HWMult;
     89      1.1  joerg   if (MCU && HWMult != SupportedHWMult)
     90      1.1  joerg     D.Diag(clang::diag::warn_drv_msp430_hwmult_mismatch)
     91      1.1  joerg         << SupportedHWMult << HWMult;
     92      1.1  joerg 
     93      1.1  joerg   if (HWMult == "16bit") {
     94      1.1  joerg     // '16bit' - for 16-bit only hw multiplier.
     95      1.1  joerg     Features.push_back("+hwmult16");
     96      1.1  joerg   } else if (HWMult == "32bit") {
     97      1.1  joerg     // '32bit' - for 16/32-bit hw multiplier.
     98      1.1  joerg     Features.push_back("+hwmult32");
     99      1.1  joerg   } else if (HWMult == "f5series") {
    100      1.1  joerg     // 'f5series' - for 16/32-bit hw multiplier supported by F5 series mcus.
    101      1.1  joerg     Features.push_back("+hwmultf5");
    102      1.1  joerg   } else {
    103      1.1  joerg     D.Diag(clang::diag::err_drv_unsupported_option_argument)
    104      1.1  joerg         << HWMultArg->getAsString(Args) << HWMult;
    105      1.1  joerg   }
    106      1.1  joerg }
    107      1.1  joerg 
    108      1.1  joerg /// MSP430 Toolchain
    109      1.1  joerg MSP430ToolChain::MSP430ToolChain(const Driver &D, const llvm::Triple &Triple,
    110      1.1  joerg                                  const ArgList &Args)
    111      1.1  joerg     : Generic_ELF(D, Triple, Args) {
    112      1.1  joerg 
    113      1.1  joerg   StringRef MultilibSuf = "";
    114      1.1  joerg 
    115      1.1  joerg   GCCInstallation.init(Triple, Args);
    116      1.1  joerg   if (GCCInstallation.isValid()) {
    117      1.1  joerg     MultilibSuf = GCCInstallation.getMultilib().gccSuffix();
    118      1.1  joerg 
    119      1.1  joerg     SmallString<128> GCCBinPath;
    120      1.1  joerg     llvm::sys::path::append(GCCBinPath,
    121      1.1  joerg                             GCCInstallation.getParentLibPath(), "..", "bin");
    122      1.1  joerg     addPathIfExists(D, GCCBinPath, getProgramPaths());
    123      1.1  joerg 
    124      1.1  joerg     SmallString<128> GCCRtPath;
    125      1.1  joerg     llvm::sys::path::append(GCCRtPath,
    126      1.1  joerg                             GCCInstallation.getInstallPath(), MultilibSuf);
    127      1.1  joerg     addPathIfExists(D, GCCRtPath, getFilePaths());
    128      1.1  joerg   }
    129      1.1  joerg 
    130      1.1  joerg   SmallString<128> SysRootDir(computeSysRoot());
    131  1.1.1.2  joerg   llvm::sys::path::append(SysRootDir, "msp430-elf", "lib", MultilibSuf);
    132      1.1  joerg   addPathIfExists(D, SysRootDir, getFilePaths());
    133      1.1  joerg }
    134      1.1  joerg 
    135      1.1  joerg std::string MSP430ToolChain::computeSysRoot() const {
    136      1.1  joerg   if (!getDriver().SysRoot.empty())
    137      1.1  joerg     return getDriver().SysRoot;
    138      1.1  joerg 
    139      1.1  joerg   SmallString<128> Dir;
    140      1.1  joerg   if (GCCInstallation.isValid())
    141  1.1.1.2  joerg     llvm::sys::path::append(Dir, GCCInstallation.getParentLibPath(), "..");
    142      1.1  joerg   else
    143  1.1.1.2  joerg     llvm::sys::path::append(Dir, getDriver().Dir, "..");
    144      1.1  joerg 
    145  1.1.1.2  joerg   return std::string(Dir.str());
    146      1.1  joerg }
    147      1.1  joerg 
    148      1.1  joerg void MSP430ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
    149      1.1  joerg                                                 ArgStringList &CC1Args) const {
    150      1.1  joerg   if (DriverArgs.hasArg(options::OPT_nostdinc) ||
    151      1.1  joerg       DriverArgs.hasArg(options::OPT_nostdlibinc))
    152      1.1  joerg     return;
    153      1.1  joerg 
    154      1.1  joerg   SmallString<128> Dir(computeSysRoot());
    155  1.1.1.2  joerg   llvm::sys::path::append(Dir, "msp430-elf", "include");
    156      1.1  joerg   addSystemInclude(DriverArgs, CC1Args, Dir.str());
    157      1.1  joerg }
    158      1.1  joerg 
    159      1.1  joerg void MSP430ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
    160      1.1  joerg                                             ArgStringList &CC1Args,
    161      1.1  joerg                                             Action::OffloadKind) const {
    162      1.1  joerg   CC1Args.push_back("-nostdsysteminc");
    163      1.1  joerg 
    164      1.1  joerg   const auto *MCUArg = DriverArgs.getLastArg(options::OPT_mmcu_EQ);
    165      1.1  joerg   if (!MCUArg)
    166      1.1  joerg     return;
    167      1.1  joerg 
    168      1.1  joerg   const StringRef MCU = MCUArg->getValue();
    169      1.1  joerg   if (MCU.startswith("msp430i")) {
    170      1.1  joerg     // 'i' should be in lower case as it's defined in TI MSP430-GCC headers
    171      1.1  joerg     CC1Args.push_back(DriverArgs.MakeArgString(
    172      1.1  joerg         "-D__MSP430i" + MCU.drop_front(7).upper() + "__"));
    173      1.1  joerg   } else {
    174      1.1  joerg     CC1Args.push_back(DriverArgs.MakeArgString("-D__" + MCU.upper() + "__"));
    175      1.1  joerg   }
    176      1.1  joerg }
    177      1.1  joerg 
    178      1.1  joerg Tool *MSP430ToolChain::buildLinker() const {
    179      1.1  joerg   return new tools::msp430::Linker(*this);
    180      1.1  joerg }
    181      1.1  joerg 
    182  1.1.1.2  joerg void msp430::Linker::AddStartFiles(bool UseExceptions, const ArgList &Args,
    183  1.1.1.2  joerg                                    ArgStringList &CmdArgs) const {
    184  1.1.1.2  joerg   const ToolChain &ToolChain = getToolChain();
    185  1.1.1.2  joerg 
    186  1.1.1.2  joerg   CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
    187  1.1.1.2  joerg   const char *crtbegin = UseExceptions ? "crtbegin.o" : "crtbegin_no_eh.o";
    188  1.1.1.2  joerg   CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
    189  1.1.1.2  joerg }
    190  1.1.1.2  joerg 
    191  1.1.1.2  joerg void msp430::Linker::AddDefaultLibs(const llvm::opt::ArgList &Args,
    192  1.1.1.2  joerg                                     llvm::opt::ArgStringList &CmdArgs) const {
    193  1.1.1.2  joerg   const ToolChain &ToolChain = getToolChain();
    194  1.1.1.2  joerg   const Driver &D = ToolChain.getDriver();
    195  1.1.1.2  joerg 
    196  1.1.1.2  joerg   CmdArgs.push_back("--start-group");
    197  1.1.1.2  joerg   CmdArgs.push_back(Args.MakeArgString(getHWMultLib(Args)));
    198  1.1.1.2  joerg   CmdArgs.push_back("-lc");
    199  1.1.1.2  joerg   AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
    200  1.1.1.2  joerg   CmdArgs.push_back("-lcrt");
    201  1.1.1.2  joerg 
    202  1.1.1.2  joerg   if (Args.hasArg(options::OPT_msim)) {
    203  1.1.1.2  joerg     CmdArgs.push_back("-lsim");
    204  1.1.1.2  joerg 
    205  1.1.1.2  joerg     // msp430-sim.ld relies on __crt0_call_exit being implicitly .refsym-ed
    206  1.1.1.2  joerg     // in main() by msp430-gcc.
    207  1.1.1.2  joerg     // This workaround should work seamlessly unless the compilation unit that
    208  1.1.1.2  joerg     // contains main() is compiled by clang and then passed to
    209  1.1.1.2  joerg     // gcc compiler driver for linkage.
    210  1.1.1.2  joerg     CmdArgs.push_back("--undefined=__crt0_call_exit");
    211  1.1.1.2  joerg   } else
    212  1.1.1.2  joerg     CmdArgs.push_back("-lnosys");
    213  1.1.1.2  joerg 
    214  1.1.1.2  joerg   CmdArgs.push_back("--end-group");
    215  1.1.1.2  joerg   AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
    216  1.1.1.2  joerg }
    217  1.1.1.2  joerg 
    218  1.1.1.2  joerg void msp430::Linker::AddEndFiles(bool UseExceptions, const ArgList &Args,
    219  1.1.1.2  joerg                                  ArgStringList &CmdArgs) const {
    220  1.1.1.2  joerg   const ToolChain &ToolChain = getToolChain();
    221  1.1.1.2  joerg   const Driver &D = ToolChain.getDriver();
    222  1.1.1.2  joerg 
    223  1.1.1.2  joerg   const char *crtend = UseExceptions ? "crtend.o" : "crtend_no_eh.o";
    224  1.1.1.2  joerg   CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
    225  1.1.1.2  joerg   AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
    226  1.1.1.2  joerg }
    227  1.1.1.2  joerg 
    228  1.1.1.2  joerg static void AddSspArgs(const ArgList &Args, ArgStringList &CmdArgs) {
    229  1.1.1.2  joerg   Arg *SspFlag = Args.getLastArg(
    230  1.1.1.2  joerg       options::OPT_fno_stack_protector, options::OPT_fstack_protector,
    231  1.1.1.2  joerg       options::OPT_fstack_protector_all, options::OPT_fstack_protector_strong);
    232  1.1.1.2  joerg 
    233  1.1.1.2  joerg   if (SspFlag &&
    234  1.1.1.2  joerg       !SspFlag->getOption().matches(options::OPT_fno_stack_protector)) {
    235  1.1.1.2  joerg     CmdArgs.push_back("-lssp_nonshared");
    236  1.1.1.2  joerg     CmdArgs.push_back("-lssp");
    237  1.1.1.2  joerg   }
    238  1.1.1.2  joerg }
    239  1.1.1.2  joerg 
    240  1.1.1.2  joerg static void AddImplicitLinkerScript(const std::string SysRoot,
    241  1.1.1.2  joerg                                     const ArgList &Args,
    242  1.1.1.2  joerg                                     ArgStringList &CmdArgs) {
    243  1.1.1.2  joerg   if (Args.hasArg(options::OPT_T))
    244  1.1.1.2  joerg     return;
    245  1.1.1.2  joerg 
    246  1.1.1.2  joerg   if (Args.hasArg(options::OPT_msim)) {
    247  1.1.1.2  joerg     CmdArgs.push_back("-Tmsp430-sim.ld");
    248  1.1.1.2  joerg     return;
    249  1.1.1.2  joerg   }
    250  1.1.1.2  joerg 
    251  1.1.1.2  joerg   const Arg *MCUArg = Args.getLastArg(options::OPT_mmcu_EQ);
    252  1.1.1.2  joerg   if (!MCUArg)
    253  1.1.1.2  joerg     return;
    254  1.1.1.2  joerg 
    255  1.1.1.2  joerg   SmallString<128> MCULinkerScriptPath(SysRoot);
    256  1.1.1.2  joerg   llvm::sys::path::append(MCULinkerScriptPath, "include");
    257  1.1.1.2  joerg   // -L because <mcu>.ld INCLUDEs <mcu>_symbols.ld
    258  1.1.1.2  joerg   CmdArgs.push_back(Args.MakeArgString("-L" + MCULinkerScriptPath));
    259  1.1.1.2  joerg   CmdArgs.push_back(
    260  1.1.1.2  joerg       Args.MakeArgString("-T" + StringRef(MCUArg->getValue()) + ".ld"));
    261  1.1.1.2  joerg }
    262  1.1.1.2  joerg 
    263      1.1  joerg void msp430::Linker::ConstructJob(Compilation &C, const JobAction &JA,
    264      1.1  joerg                                   const InputInfo &Output,
    265      1.1  joerg                                   const InputInfoList &Inputs,
    266      1.1  joerg                                   const ArgList &Args,
    267      1.1  joerg                                   const char *LinkingOutput) const {
    268      1.1  joerg   const ToolChain &ToolChain = getToolChain();
    269      1.1  joerg   const Driver &D = ToolChain.getDriver();
    270      1.1  joerg   std::string Linker = ToolChain.GetProgramPath(getShortName());
    271      1.1  joerg   ArgStringList CmdArgs;
    272  1.1.1.2  joerg   bool UseExceptions = Args.hasFlag(options::OPT_fexceptions,
    273  1.1.1.2  joerg                                     options::OPT_fno_exceptions, false);
    274  1.1.1.2  joerg   bool UseStartAndEndFiles = !Args.hasArg(options::OPT_nostdlib, options::OPT_r,
    275  1.1.1.2  joerg                                           options::OPT_nostartfiles);
    276  1.1.1.2  joerg 
    277  1.1.1.2  joerg   if (Args.hasArg(options::OPT_mrelax))
    278  1.1.1.2  joerg     CmdArgs.push_back("--relax");
    279  1.1.1.2  joerg   if (!Args.hasArg(options::OPT_r, options::OPT_g_Group))
    280  1.1.1.2  joerg     CmdArgs.push_back("--gc-sections");
    281  1.1.1.2  joerg 
    282  1.1.1.2  joerg   Args.AddAllArgs(CmdArgs, {
    283  1.1.1.2  joerg                                options::OPT_e,
    284  1.1.1.2  joerg                                options::OPT_n,
    285  1.1.1.2  joerg                                options::OPT_s,
    286  1.1.1.2  joerg                                options::OPT_t,
    287  1.1.1.2  joerg                                options::OPT_u,
    288  1.1.1.2  joerg                            });
    289      1.1  joerg 
    290  1.1.1.2  joerg   if (UseStartAndEndFiles)
    291  1.1.1.2  joerg     AddStartFiles(UseExceptions, Args, CmdArgs);
    292      1.1  joerg 
    293      1.1  joerg   Args.AddAllArgs(CmdArgs, options::OPT_L);
    294      1.1  joerg   ToolChain.AddFilePathLibArgs(Args, CmdArgs);
    295      1.1  joerg   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
    296      1.1  joerg 
    297  1.1.1.2  joerg   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_r,
    298  1.1.1.2  joerg                    options::OPT_nodefaultlibs)) {
    299  1.1.1.2  joerg     AddSspArgs(Args, CmdArgs);
    300  1.1.1.2  joerg     AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
    301  1.1.1.2  joerg     if (!Args.hasArg(options::OPT_nolibc)) {
    302  1.1.1.2  joerg       AddDefaultLibs(Args, CmdArgs);
    303  1.1.1.2  joerg       AddImplicitLinkerScript(D.SysRoot, Args, CmdArgs);
    304  1.1.1.2  joerg     }
    305      1.1  joerg   }
    306      1.1  joerg 
    307  1.1.1.2  joerg   if (UseStartAndEndFiles)
    308  1.1.1.2  joerg     AddEndFiles(UseExceptions, Args, CmdArgs);
    309  1.1.1.2  joerg 
    310      1.1  joerg   CmdArgs.push_back("-o");
    311      1.1  joerg   CmdArgs.push_back(Output.getFilename());
    312  1.1.1.2  joerg 
    313  1.1.1.2  joerg   Args.AddAllArgs(CmdArgs, options::OPT_T);
    314  1.1.1.2  joerg 
    315  1.1.1.2  joerg   C.addCommand(std::make_unique<Command>(
    316  1.1.1.2  joerg       JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
    317  1.1.1.2  joerg       CmdArgs, Inputs, Output));
    318      1.1  joerg }
    319