MSP430.cpp 11 KB

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