Coverage Report

Created: 2020-09-22 08:39

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