Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
16
static bool isSupportedMCU(const StringRef MCU) {
27
16
  return llvm::StringSwitch<bool>(MCU)
28
3.47k
#define MSP430_MCU(NAME) .Case(NAME, true)
29
16
#include "clang/Basic/MSP430Target.def"
30
16
      .Default(false);
31
16
}
32
33
41
static StringRef getSupportedHWMult(const Arg *MCU) {
34
41
  if (!MCU)
35
19
    return "none";
36
22
37
22
  return llvm::StringSwitch<StringRef>(MCU->getValue())
38
1.27k
#define MSP430_MCU_FEAT(NAME, HWMULT) .Case(NAME, HWMULT)
39
22
#include "clang/Basic/MSP430Target.def"
40
22
      .Default("none");
41
22
}
42
43
33
static StringRef getHWMultLib(const ArgList &Args) {
44
33
  StringRef HWMult = Args.getLastArgValue(options::OPT_mhwmult_EQ, "auto");
45
33
  if (HWMult == "auto") {
46
15
    HWMult = getSupportedHWMult(Args.getLastArg(options::OPT_mmcu_EQ));
47
15
  }
48
33
49
33
  return llvm::StringSwitch<StringRef>(HWMult)
50
33
      .Case("16bit", "-lmul_16")
51
33
      .Case("32bit", "-lmul_32")
52
33
      .Case("f5series", "-lmul_f5")
53
33
      .Default("-lmul_none");
54
33
}
55
56
void msp430::getMSP430TargetFeatures(const Driver &D, const ArgList &Args,
57
35
                                     std::vector<StringRef> &Features) {
58
35
  const Arg *MCU = Args.getLastArg(options::OPT_mmcu_EQ);
59
35
  if (MCU && 
!isSupportedMCU(MCU->getValue())16
) {
60
1
    D.Diag(diag::err_drv_clang_unsupported) << MCU->getValue();
61
1
    return;
62
1
  }
63
34
64
34
  const Arg *HWMultArg = Args.getLastArg(options::OPT_mhwmult_EQ);
65
34
  if (!MCU && 
!HWMultArg19
)
66
8
    return;
67
26
68
26
  StringRef HWMult = HWMultArg ? 
HWMultArg->getValue()22
:
"auto"4
;
69
26
  StringRef SupportedHWMult = getSupportedHWMult(MCU);
70
26
71
26
  if (HWMult == "auto") {
72
8
    // 'auto' - deduce hw multiplier support based on mcu name provided.
73
8
    // If no mcu name is provided, assume no hw multiplier is supported.
74
8
    if (!MCU)
75
2
      D.Diag(clang::diag::warn_drv_msp430_hwmult_no_device);
76
8
    HWMult = SupportedHWMult;
77
8
  }
78
26
79
26
  if (HWMult == "none") {
80
8
    // 'none' - disable hw multiplier.
81
8
    Features.push_back("-hwmult16");
82
8
    Features.push_back("-hwmult32");
83
8
    Features.push_back("-hwmultf5");
84
8
    return;
85
8
  }
86
18
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
18
93
18
  if (HWMult == "16bit") {
94
7
    // '16bit' - for 16-bit only hw multiplier.
95
7
    Features.push_back("+hwmult16");
96
11
  } else if (HWMult == "32bit") {
97
6
    // '32bit' - for 16/32-bit hw multiplier.
98
6
    Features.push_back("+hwmult32");
99
6
  } else 
if (5
HWMult == "f5series"5
) {
100
4
    // 'f5series' - for 16/32-bit hw multiplier supported by F5 series mcus.
101
4
    Features.push_back("+hwmultf5");
102
4
  } 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
35
    : Generic_ELF(D, Triple, Args) {
112
35
113
35
  StringRef MultilibSuf = "";
114
35
115
35
  GCCInstallation.init(Triple, Args);
116
35
  if (GCCInstallation.isValid()) {
117
4
    MultilibSuf = GCCInstallation.getMultilib().gccSuffix();
118
4
119
4
    SmallString<128> GCCBinPath;
120
4
    llvm::sys::path::append(GCCBinPath,
121
4
                            GCCInstallation.getParentLibPath(), "..", "bin");
122
4
    addPathIfExists(D, GCCBinPath, getProgramPaths());
123
4
124
4
    SmallString<128> GCCRtPath;
125
4
    llvm::sys::path::append(GCCRtPath,
126
4
                            GCCInstallation.getInstallPath(), MultilibSuf);
127
4
    addPathIfExists(D, GCCRtPath, getFilePaths());
128
4
  }
129
35
130
35
  SmallString<128> SysRootDir(computeSysRoot());
131
35
  llvm::sys::path::append(SysRootDir, "lib", MultilibSuf);
132
35
  addPathIfExists(D, SysRootDir, getFilePaths());
133
35
}
134
135
70
std::string MSP430ToolChain::computeSysRoot() const {
136
70
  if (!getDriver().SysRoot.empty())
137
0
    return getDriver().SysRoot;
138
70
139
70
  SmallString<128> Dir;
140
70
  if (GCCInstallation.isValid())
141
8
    llvm::sys::path::append(Dir, GCCInstallation.getParentLibPath(), "..",
142
8
                            GCCInstallation.getTriple().str());
143
62
  else
144
62
    llvm::sys::path::append(Dir, getDriver().Dir, "..", getTriple().str());
145
70
146
70
  return Dir.str();
147
70
}
148
149
void MSP430ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
150
35
                                                ArgStringList &CC1Args) const {
151
35
  if (DriverArgs.hasArg(options::OPT_nostdinc) ||
152
35
      DriverArgs.hasArg(options::OPT_nostdlibinc))
153
0
    return;
154
35
155
35
  SmallString<128> Dir(computeSysRoot());
156
35
  llvm::sys::path::append(Dir, "include");
157
35
  addSystemInclude(DriverArgs, CC1Args, Dir.str());
158
35
}
159
160
void MSP430ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
161
                                            ArgStringList &CC1Args,
162
35
                                            Action::OffloadKind) const {
163
35
  CC1Args.push_back("-nostdsysteminc");
164
35
165
35
  const auto *MCUArg = DriverArgs.getLastArg(options::OPT_mmcu_EQ);
166
35
  if (!MCUArg)
167
19
    return;
168
16
169
16
  const StringRef MCU = MCUArg->getValue();
170
16
  if (MCU.startswith("msp430i")) {
171
1
    // 'i' should be in lower case as it's defined in TI MSP430-GCC headers
172
1
    CC1Args.push_back(DriverArgs.MakeArgString(
173
1
        "-D__MSP430i" + MCU.drop_front(7).upper() + "__"));
174
15
  } else {
175
15
    CC1Args.push_back(DriverArgs.MakeArgString("-D__" + MCU.upper() + "__"));
176
15
  }
177
16
}
178
179
33
Tool *MSP430ToolChain::buildLinker() const {
180
33
  return new tools::msp430::Linker(*this);
181
33
}
182
183
void msp430::Linker::ConstructJob(Compilation &C, const JobAction &JA,
184
                                  const InputInfo &Output,
185
                                  const InputInfoList &Inputs,
186
                                  const ArgList &Args,
187
33
                                  const char *LinkingOutput) const {
188
33
  const ToolChain &ToolChain = getToolChain();
189
33
  const Driver &D = ToolChain.getDriver();
190
33
  std::string Linker = ToolChain.GetProgramPath(getShortName());
191
33
  ArgStringList CmdArgs;
192
33
193
33
  if (!D.SysRoot.empty())
194
0
    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
195
33
196
33
  Args.AddAllArgs(CmdArgs, options::OPT_L);
197
33
  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
198
33
199
33
  if (!Args.hasArg(options::OPT_T)) {
200
33
    if (const Arg *MCUArg = Args.getLastArg(options::OPT_mmcu_EQ))
201
16
      CmdArgs.push_back(
202
16
          Args.MakeArgString("-T" + StringRef(MCUArg->getValue()) + ".ld"));
203
33
  } else {
204
0
    Args.AddAllArgs(CmdArgs, options::OPT_T);
205
0
  }
206
33
207
33
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
208
31
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
209
31
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
210
31
  }
211
33
212
33
  AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
213
33
214
33
  CmdArgs.push_back("--start-group");
215
33
  CmdArgs.push_back(Args.MakeArgString(getHWMultLib(Args)));
216
33
  CmdArgs.push_back("-lgcc");
217
33
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
218
31
    CmdArgs.push_back("-lc");
219
31
    CmdArgs.push_back("-lcrt");
220
31
    CmdArgs.push_back("-lnosys");
221
31
  }
222
33
  CmdArgs.push_back("--end-group");
223
33
224
33
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
225
31
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
226
31
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
227
31
  }
228
33
  CmdArgs.push_back("-o");
229
33
  CmdArgs.push_back(Output.getFilename());
230
33
  C.addCommand(llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Linker),
231
33
                                          CmdArgs, Inputs));
232
33
}