Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Driver/ToolChains/Myriad.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Myriad.cpp - Myriad ToolChain Implementations ----------*- 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 "Myriad.h"
10
#include "CommonArgs.h"
11
#include "clang/Driver/Compilation.h"
12
#include "clang/Driver/Driver.h"
13
#include "clang/Driver/DriverDiagnostic.h"
14
#include "clang/Driver/Options.h"
15
#include "llvm/Option/ArgList.h"
16
17
using namespace clang::driver;
18
using namespace clang::driver::toolchains;
19
using namespace clang;
20
using namespace llvm::opt;
21
22
using tools::addPathIfExists;
23
24
void tools::SHAVE::Compiler::ConstructJob(Compilation &C, const JobAction &JA,
25
                                          const InputInfo &Output,
26
                                          const InputInfoList &Inputs,
27
                                          const ArgList &Args,
28
7
                                          const char *LinkingOutput) const {
29
7
  ArgStringList CmdArgs;
30
7
  assert(Inputs.size() == 1);
31
7
  const InputInfo &II = Inputs[0];
32
7
  assert(II.getType() == types::TY_C || II.getType() == types::TY_CXX ||
33
7
         II.getType() == types::TY_PP_CXX);
34
7
35
7
  if (JA.getKind() == Action::PreprocessJobClass) {
36
1
    Args.ClaimAllArgs();
37
1
    CmdArgs.push_back("-E");
38
6
  } else {
39
6
    assert(Output.getType() == types::TY_PP_Asm); // Require preprocessed asm.
40
6
    CmdArgs.push_back("-S");
41
6
    CmdArgs.push_back("-fno-exceptions"); // Always do this even if unspecified.
42
6
  }
43
7
  CmdArgs.push_back("-DMYRIAD2");
44
7
45
7
  // Append all -I, -iquote, -isystem paths, defines/undefines, 'f'
46
7
  // flags, 'g' flags, 'M' flags, optimize flags, warning options,
47
7
  // mcpu flags, mllvm flags, and Xclang flags.
48
7
  // These are spelled the same way in clang and moviCompile.
49
7
  Args.AddAllArgsExcept(
50
7
      CmdArgs,
51
7
      {options::OPT_I_Group, options::OPT_clang_i_Group, options::OPT_std_EQ,
52
7
       options::OPT_D, options::OPT_U, options::OPT_f_Group,
53
7
       options::OPT_f_clang_Group, options::OPT_g_Group, options::OPT_M_Group,
54
7
       options::OPT_O_Group, options::OPT_W_Group, options::OPT_mcpu_EQ,
55
7
       options::OPT_mllvm, options::OPT_Xclang},
56
7
      {options::OPT_fno_split_dwarf_inlining});
57
7
  Args.hasArg(options::OPT_fno_split_dwarf_inlining); // Claim it if present.
58
7
59
7
  // If we're producing a dependency file, and assembly is the final action,
60
7
  // then the name of the target in the dependency file should be the '.o'
61
7
  // file, not the '.s' file produced by this step. For example, instead of
62
7
  //  /tmp/mumble.s: mumble.c .../someheader.h
63
7
  // the filename on the lefthand side should be "mumble.o"
64
7
  if (Args.getLastArg(options::OPT_MF) && 
!Args.getLastArg(options::OPT_MT)2
&&
65
7
      
C.getActions().size() == 12
&&
66
7
      
C.getActions()[0]->getKind() == Action::AssembleJobClass2
) {
67
2
    Arg *A = Args.getLastArg(options::OPT_o);
68
2
    if (A) {
69
1
      CmdArgs.push_back("-MT");
70
1
      CmdArgs.push_back(Args.MakeArgString(A->getValue()));
71
1
    }
72
2
  }
73
7
74
7
  CmdArgs.push_back(II.getFilename());
75
7
  CmdArgs.push_back("-o");
76
7
  CmdArgs.push_back(Output.getFilename());
77
7
78
7
  std::string Exec =
79
7
      Args.MakeArgString(getToolChain().GetProgramPath("moviCompile"));
80
7
  C.addCommand(llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Exec),
81
7
                                          CmdArgs, Inputs));
82
7
}
83
84
void tools::SHAVE::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
85
                                           const InputInfo &Output,
86
                                           const InputInfoList &Inputs,
87
                                           const ArgList &Args,
88
5
                                           const char *LinkingOutput) const {
89
5
  ArgStringList CmdArgs;
90
5
91
5
  assert(Inputs.size() == 1);
92
5
  const InputInfo &II = Inputs[0];
93
5
  assert(II.getType() == types::TY_PP_Asm); // Require preprocessed asm input.
94
5
  assert(Output.getType() == types::TY_Object);
95
5
96
5
  CmdArgs.push_back("-no6thSlotCompression");
97
5
  const Arg *CPUArg = Args.getLastArg(options::OPT_mcpu_EQ);
98
5
  if (CPUArg)
99
1
    CmdArgs.push_back(
100
1
        Args.MakeArgString("-cv:" + StringRef(CPUArg->getValue())));
101
5
  CmdArgs.push_back("-noSPrefixing");
102
5
  CmdArgs.push_back("-a"); // Mystery option.
103
5
  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
104
5
  for (const Arg *A : Args.filtered(options::OPT_I, options::OPT_isystem)) {
105
4
    A->claim();
106
4
    CmdArgs.push_back(
107
4
        Args.MakeArgString(std::string("-i:") + A->getValue(0)));
108
4
  }
109
5
  CmdArgs.push_back(II.getFilename());
110
5
  CmdArgs.push_back(
111
5
      Args.MakeArgString(std::string("-o:") + Output.getFilename()));
112
5
113
5
  std::string Exec =
114
5
      Args.MakeArgString(getToolChain().GetProgramPath("moviAsm"));
115
5
  C.addCommand(llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Exec),
116
5
                                          CmdArgs, Inputs));
117
5
}
118
119
void tools::Myriad::Linker::ConstructJob(Compilation &C, const JobAction &JA,
120
                                         const InputInfo &Output,
121
                                         const InputInfoList &Inputs,
122
                                         const ArgList &Args,
123
5
                                         const char *LinkingOutput) const {
124
5
  const auto &TC =
125
5
      static_cast<const toolchains::MyriadToolChain &>(getToolChain());
126
5
  const llvm::Triple &T = TC.getTriple();
127
5
  ArgStringList CmdArgs;
128
5
  bool UseStartfiles =
129
5
      !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
130
5
  bool UseDefaultLibs =
131
5
      !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs);
132
5
  // Silence warning if the args contain both -nostdlib and -stdlib=.
133
5
  Args.getLastArg(options::OPT_stdlib_EQ);
134
5
135
5
  if (T.getArch() == llvm::Triple::sparc)
136
3
    CmdArgs.push_back("-EB");
137
2
  else // SHAVE assumes little-endian, and sparcel is expressly so.
138
2
    CmdArgs.push_back("-EL");
139
5
140
5
  // The remaining logic is mostly like gnutools::Linker::ConstructJob,
141
5
  // but we never pass through a --sysroot option and various other bits.
142
5
  // For example, there are no sanitizers (yet) nor gold linker.
143
5
144
5
  // Eat some arguments that may be present but have no effect.
145
5
  Args.ClaimAllArgs(options::OPT_g_Group);
146
5
  Args.ClaimAllArgs(options::OPT_w);
147
5
  Args.ClaimAllArgs(options::OPT_static_libgcc);
148
5
149
5
  if (Args.hasArg(options::OPT_s)) // Pass the 'strip' option.
150
0
    CmdArgs.push_back("-s");
151
5
152
5
  CmdArgs.push_back("-o");
153
5
  CmdArgs.push_back(Output.getFilename());
154
5
155
5
  if (UseStartfiles) {
156
4
    // If you want startfiles, it means you want the builtin crti and crtbegin,
157
4
    // but not crt0. Myriad link commands provide their own crt0.o as needed.
158
4
    CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crti.o")));
159
4
    CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtbegin.o")));
160
4
  }
161
5
162
5
  Args.AddAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
163
5
                            options::OPT_e, options::OPT_s, options::OPT_t,
164
5
                            options::OPT_Z_Flag, options::OPT_r});
165
5
166
5
  TC.AddFilePathLibArgs(Args, CmdArgs);
167
5
168
5
  bool NeedsSanitizerDeps = addSanitizerRuntimes(TC, Args, CmdArgs);
169
5
  AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
170
5
171
5
  if (UseDefaultLibs) {
172
4
    if (NeedsSanitizerDeps)
173
1
      linkSanitizerRuntimeDeps(TC, CmdArgs);
174
4
    if (C.getDriver().CCCIsCXX()) {
175
1
      if (TC.GetCXXStdlibType(Args) == ToolChain::CST_Libcxx) {
176
0
        CmdArgs.push_back("-lc++");
177
0
        CmdArgs.push_back("-lc++abi");
178
0
      } else
179
1
        CmdArgs.push_back("-lstdc++");
180
1
    }
181
4
    if (T.getOS() == llvm::Triple::RTEMS) {
182
2
      CmdArgs.push_back("--start-group");
183
2
      CmdArgs.push_back("-lc");
184
2
      CmdArgs.push_back("-lgcc"); // circularly dependent on rtems
185
2
      // You must provide your own "-L" option to enable finding these.
186
2
      CmdArgs.push_back("-lrtemscpu");
187
2
      CmdArgs.push_back("-lrtemsbsp");
188
2
      CmdArgs.push_back("--end-group");
189
2
    } else {
190
2
      CmdArgs.push_back("-lc");
191
2
      CmdArgs.push_back("-lgcc");
192
2
    }
193
4
  }
194
5
  if (UseStartfiles) {
195
4
    CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtend.o")));
196
4
    CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtn.o")));
197
4
  }
198
5
199
5
  std::string Exec =
200
5
      Args.MakeArgString(TC.GetProgramPath("sparc-myriad-rtems-ld"));
201
5
  C.addCommand(llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Exec),
202
5
                                          CmdArgs, Inputs));
203
5
}
204
205
MyriadToolChain::MyriadToolChain(const Driver &D, const llvm::Triple &Triple,
206
                                 const ArgList &Args)
207
33
    : Generic_ELF(D, Triple, Args) {
208
33
  // If a target of 'sparc-myriad-elf' is specified to clang, it wants to use
209
33
  // 'sparc-myriad--elf' (note the unknown OS) as the canonical triple.
210
33
  // This won't work to find gcc. Instead we give the installation detector an
211
33
  // extra triple, which is preferable to further hacks of the logic that at
212
33
  // present is based solely on getArch(). In particular, it would be wrong to
213
33
  // choose the myriad installation when targeting a non-myriad sparc install.
214
33
  switch (Triple.getArch()) {
215
33
  default:
216
1
    D.Diag(clang::diag::err_target_unsupported_arch)
217
1
        << Triple.getArchName() << "myriad";
218
1
    LLVM_FALLTHROUGH;
219
8
  case llvm::Triple::shave:
220
8
    return;
221
25
  case llvm::Triple::sparc:
222
25
  case llvm::Triple::sparcel:
223
25
    GCCInstallation.init(Triple, Args, {"sparc-myriad-rtems"});
224
33
  }
225
33
226
33
  
if (25
GCCInstallation.isValid()25
) {
227
3
    // This directory contains crt{i,n,begin,end}.o as well as libgcc.
228
3
    // These files are tied to a particular version of gcc.
229
3
    SmallString<128> CompilerSupportDir(GCCInstallation.getInstallPath());
230
3
    addPathIfExists(D, CompilerSupportDir, getFilePaths());
231
3
  }
232
25
  // libstd++ and libc++ must both be found in this one place.
233
25
  addPathIfExists(D, D.Dir + "/../sparc-myriad-rtems/lib", getFilePaths());
234
25
}
235
236
33
MyriadToolChain::~MyriadToolChain() {}
237
238
void MyriadToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
239
26
                                                ArgStringList &CC1Args) const {
240
26
  if (!DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
241
25
    addSystemInclude(DriverArgs, CC1Args, getDriver().SysRoot + "/include");
242
26
}
243
244
void MyriadToolChain::addLibCxxIncludePaths(
245
    const llvm::opt::ArgList &DriverArgs,
246
1
    llvm::opt::ArgStringList &CC1Args) const {
247
1
  std::string Path(getDriver().getInstalledDir());
248
1
  addSystemInclude(DriverArgs, CC1Args, Path + "/../include/c++/v1");
249
1
}
250
251
void MyriadToolChain::addLibStdCxxIncludePaths(
252
    const llvm::opt::ArgList &DriverArgs,
253
2
    llvm::opt::ArgStringList &CC1Args) const {
254
2
  StringRef LibDir = GCCInstallation.getParentLibPath();
255
2
  const GCCVersion &Version = GCCInstallation.getVersion();
256
2
  StringRef TripleStr = GCCInstallation.getTriple().str();
257
2
  const Multilib &Multilib = GCCInstallation.getMultilib();
258
2
  addLibStdCXXIncludePaths(
259
2
      LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
260
2
      "", TripleStr, "", "", Multilib.includeSuffix(), DriverArgs, CC1Args);
261
2
}
262
263
// MyriadToolChain handles several triples:
264
//  {shave,sparc{,el}}-myriad-{rtems,unknown}-elf
265
51
Tool *MyriadToolChain::SelectTool(const JobAction &JA) const {
266
51
  // The inherited method works fine if not targeting the SHAVE.
267
51
  if (!isShaveCompilation(getTriple()))
268
39
    return ToolChain::SelectTool(JA);
269
12
  switch (JA.getKind()) {
270
12
  case Action::PreprocessJobClass:
271
7
  case Action::CompileJobClass:
272
7
    if (!Compiler)
273
7
      Compiler.reset(new tools::SHAVE::Compiler(*this));
274
7
    return Compiler.get();
275
7
  case Action::AssembleJobClass:
276
5
    if (!Assembler)
277
5
      Assembler.reset(new tools::SHAVE::Assembler(*this));
278
5
    return Assembler.get();
279
7
  default:
280
0
    return ToolChain::getTool(JA.getKind());
281
12
  }
282
12
}
283
284
5
Tool *MyriadToolChain::buildLinker() const {
285
5
  return new tools::Myriad::Linker(*this);
286
5
}
287
288
26
SanitizerMask MyriadToolChain::getSupportedSanitizers() const {
289
26
  return SanitizerKind::Address;
290
26
}