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