clang  8.0.0
MSP430.cpp
Go to the documentation of this file.
1 //===--- MSP430.cpp - MSP430 Helpers for Tools ------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "MSP430.h"
11 #include "CommonArgs.h"
12 #include "Gnu.h"
13 #include "InputInfo.h"
15 #include "clang/Driver/Multilib.h"
16 #include "clang/Driver/Options.h"
17 #include "llvm/Option/ArgList.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/Path.h"
20 
21 using namespace clang::driver;
22 using namespace clang::driver::toolchains;
23 using namespace clang::driver::tools;
24 using namespace clang;
25 using namespace llvm::opt;
26 
27 static bool isSupportedMCU(const StringRef MCU) {
28  return llvm::StringSwitch<bool>(MCU)
29 #define MSP430_MCU(NAME) .Case(NAME, true)
30 #include "clang/Basic/MSP430Target.def"
31  .Default(false);
32 }
33 
34 static StringRef getSupportedHWMult(const Arg *MCU) {
35  if (!MCU)
36  return "none";
37 
38  return llvm::StringSwitch<StringRef>(MCU->getValue())
39 #define MSP430_MCU_FEAT(NAME, HWMULT) .Case(NAME, HWMULT)
40 #include "clang/Basic/MSP430Target.def"
41  .Default("none");
42 }
43 
44 static StringRef getHWMultLib(const ArgList &Args) {
45  StringRef HWMult = Args.getLastArgValue(options::OPT_mhwmult_EQ, "auto");
46  if (HWMult == "auto") {
47  HWMult = getSupportedHWMult(Args.getLastArg(options::OPT_mmcu_EQ));
48  }
49 
50  return llvm::StringSwitch<StringRef>(HWMult)
51  .Case("16bit", "-lmul_16")
52  .Case("32bit", "-lmul_32")
53  .Case("f5series", "-lmul_f5")
54  .Default("-lmul_none");
55 }
56 
57 void msp430::getMSP430TargetFeatures(const Driver &D, const ArgList &Args,
58  std::vector<StringRef> &Features) {
59  const Arg *MCU = Args.getLastArg(options::OPT_mmcu_EQ);
60  if (MCU && !isSupportedMCU(MCU->getValue())) {
61  D.Diag(diag::err_drv_clang_unsupported) << MCU->getValue();
62  return;
63  }
64 
65  const Arg *HWMultArg = Args.getLastArg(options::OPT_mhwmult_EQ);
66  if (!MCU && !HWMultArg)
67  return;
68 
69  StringRef HWMult = HWMultArg ? HWMultArg->getValue() : "auto";
70  StringRef SupportedHWMult = getSupportedHWMult(MCU);
71 
72  if (HWMult == "auto") {
73  // 'auto' - deduce hw multiplier support based on mcu name provided.
74  // If no mcu name is provided, assume no hw multiplier is supported.
75  if (!MCU)
76  D.Diag(clang::diag::warn_drv_msp430_hwmult_no_device);
77  HWMult = SupportedHWMult;
78  }
79 
80  if (HWMult == "none") {
81  // 'none' - disable hw multiplier.
82  Features.push_back("-hwmult16");
83  Features.push_back("-hwmult32");
84  Features.push_back("-hwmultf5");
85  return;
86  }
87 
88  if (MCU && SupportedHWMult == "none")
89  D.Diag(clang::diag::warn_drv_msp430_hwmult_unsupported) << HWMult;
90  if (MCU && HWMult != SupportedHWMult)
91  D.Diag(clang::diag::warn_drv_msp430_hwmult_mismatch)
92  << SupportedHWMult << HWMult;
93 
94  if (HWMult == "16bit") {
95  // '16bit' - for 16-bit only hw multiplier.
96  Features.push_back("+hwmult16");
97  } else if (HWMult == "32bit") {
98  // '32bit' - for 16/32-bit hw multiplier.
99  Features.push_back("+hwmult32");
100  } else if (HWMult == "f5series") {
101  // 'f5series' - for 16/32-bit hw multiplier supported by F5 series mcus.
102  Features.push_back("+hwmultf5");
103  } else {
104  D.Diag(clang::diag::err_drv_unsupported_option_argument)
105  << HWMultArg->getAsString(Args) << HWMult;
106  }
107 }
108 
109 /// MSP430 Toolchain
110 MSP430ToolChain::MSP430ToolChain(const Driver &D, const llvm::Triple &Triple,
111  const ArgList &Args)
112  : Generic_ELF(D, Triple, Args) {
113 
114  StringRef MultilibSuf = "";
115 
116  GCCInstallation.init(Triple, Args);
117  if (GCCInstallation.isValid()) {
118  MultilibSuf = GCCInstallation.getMultilib().gccSuffix();
119 
120  SmallString<128> GCCBinPath;
121  llvm::sys::path::append(GCCBinPath,
122  GCCInstallation.getParentLibPath(), "..", "bin");
123  addPathIfExists(D, GCCBinPath, getProgramPaths());
124 
125  SmallString<128> GCCRtPath;
126  llvm::sys::path::append(GCCRtPath,
127  GCCInstallation.getInstallPath(), MultilibSuf);
128  addPathIfExists(D, GCCRtPath, getFilePaths());
129  }
130 
131  SmallString<128> SysRootDir(computeSysRoot());
132  llvm::sys::path::append(SysRootDir, "lib", MultilibSuf);
133  addPathIfExists(D, SysRootDir, getFilePaths());
134 }
135 
136 std::string MSP430ToolChain::computeSysRoot() const {
137  if (!getDriver().SysRoot.empty())
138  return getDriver().SysRoot;
139 
140  SmallString<128> Dir;
141  if (GCCInstallation.isValid())
142  llvm::sys::path::append(Dir, GCCInstallation.getParentLibPath(), "..",
143  GCCInstallation.getTriple().str());
144  else
145  llvm::sys::path::append(Dir, getDriver().Dir, "..", getTriple().str());
146 
147  return Dir.str();
148 }
149 
150 void MSP430ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
151  ArgStringList &CC1Args) const {
152  if (DriverArgs.hasArg(options::OPT_nostdinc) ||
153  DriverArgs.hasArg(options::OPT_nostdlibinc))
154  return;
155 
156  SmallString<128> Dir(computeSysRoot());
157  llvm::sys::path::append(Dir, "include");
158  addSystemInclude(DriverArgs, CC1Args, Dir.str());
159 }
160 
161 void MSP430ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
162  ArgStringList &CC1Args,
163  Action::OffloadKind) const {
164  CC1Args.push_back("-nostdsysteminc");
165 
166  const auto *MCUArg = DriverArgs.getLastArg(options::OPT_mmcu_EQ);
167  if (!MCUArg)
168  return;
169 
170  const StringRef MCU = MCUArg->getValue();
171  if (MCU.startswith("msp430i")) {
172  // 'i' should be in lower case as it's defined in TI MSP430-GCC headers
173  CC1Args.push_back(DriverArgs.MakeArgString(
174  "-D__MSP430i" + MCU.drop_front(7).upper() + "__"));
175  } else {
176  CC1Args.push_back(DriverArgs.MakeArgString("-D__" + MCU.upper() + "__"));
177  }
178 }
179 
181  return new tools::msp430::Linker(*this);
182 }
183 
184 void msp430::Linker::ConstructJob(Compilation &C, const JobAction &JA,
185  const InputInfo &Output,
186  const InputInfoList &Inputs,
187  const ArgList &Args,
188  const char *LinkingOutput) const {
189  const ToolChain &ToolChain = getToolChain();
190  const Driver &D = ToolChain.getDriver();
191  std::string Linker = ToolChain.GetProgramPath(getShortName());
192  ArgStringList CmdArgs;
193 
194  if (!D.SysRoot.empty())
195  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
196 
197  Args.AddAllArgs(CmdArgs, options::OPT_L);
198  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
199 
200  if (!Args.hasArg(options::OPT_T)) {
201  if (const Arg *MCUArg = Args.getLastArg(options::OPT_mmcu_EQ))
202  CmdArgs.push_back(
203  Args.MakeArgString("-T" + StringRef(MCUArg->getValue()) + ".ld"));
204  } else {
205  Args.AddAllArgs(CmdArgs, options::OPT_T);
206  }
207 
208  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
209  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
210  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
211  }
212 
213  AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
214 
215  CmdArgs.push_back("--start-group");
216  CmdArgs.push_back(Args.MakeArgString(getHWMultLib(Args)));
217  CmdArgs.push_back("-lgcc");
218  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
219  CmdArgs.push_back("-lc");
220  CmdArgs.push_back("-lcrt");
221  CmdArgs.push_back("-lnosys");
222  }
223  CmdArgs.push_back("--end-group");
224 
225  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
226  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
227  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
228  }
229  CmdArgs.push_back("-o");
230  CmdArgs.push_back(Output.getFilename());
231  C.addCommand(llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Linker),
232  CmdArgs, Inputs));
233 }
Tool * buildLinker() const override
Definition: MSP430.cpp:180
const llvm::Triple & getTriple() const
Get the GCC triple for the detected install.
Definition: Gnu.h:220
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:109
static bool isSupportedMCU(const StringRef MCU)
Definition: MSP430.cpp:27
const char * getFilename() const
Definition: InputInfo.h:84
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:280
#define MSP430_MCU(NAME)
path_list & getProgramPaths()
Definition: ToolChain.h:227
const std::string & gccSuffix() const
Get the detected GCC installation path suffix for the multi-arch target variant.
Definition: Multilib.h:45
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:431
path_list & getFilePaths()
Definition: ToolChain.h:224
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:58
return Out str()
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:206
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind) const override
Add options that need to be passed to cc1 for this target.
Definition: MSP430.cpp:161
void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
Definition: ToolChain.cpp:778
#define MSP430_MCU_FEAT(NAME, HWMULT)
const Driver & getDriver() const
Definition: ToolChain.h:186
MSP430ToolChain(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
MSP430 Toolchain.
Definition: MSP430.cpp:110
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:702
StringRef getParentLibPath() const
Get the detected GCC parent lib path.
Definition: Gnu.h:226
Dataflow Directional Tag Classes.
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
std::string SysRoot
sysroot, if present
Definition: Driver.h:148
static StringRef getHWMultLib(const ArgList &Args)
Definition: MSP430.cpp:44
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
void getMSP430TargetFeatures(const Driver &D, const llvm::opt::ArgList &Args, std::vector< llvm::StringRef > &Features)
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: MSP430.cpp:150
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:46
const llvm::Triple & getTriple() const
Definition: ToolChain.h:188
std::string GetProgramPath(const char *Name) const
Definition: ToolChain.cpp:435
void addPathIfExists(const Driver &D, const Twine &Path, ToolChain::path_list &Paths)
Definition: CommonArgs.cpp:62
void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args, ArrayRef< std::string > ExtraTripleAliases=None)
Initialize a GCCInstallationDetector from the driver.
Definition: Gnu.cpp:1702
static StringRef getSupportedHWMult(const Arg *MCU)
Definition: MSP430.cpp:34
StringRef getInstallPath() const
Get the detected GCC installation path.
Definition: Gnu.h:223
const Multilib & getMultilib() const
Get the detected Multilib.
Definition: Gnu.h:229
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:89
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:217