clang  8.0.0svn
WebAssembly.cpp
Go to the documentation of this file.
1 //===--- WebAssembly.cpp - WebAssembly ToolChain Implementation -*- 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 "WebAssembly.h"
11 #include "CommonArgs.h"
13 #include "clang/Driver/Driver.h"
15 #include "clang/Driver/Options.h"
16 #include "llvm/Option/ArgList.h"
17 
18 using namespace clang::driver;
19 using namespace clang::driver::tools;
20 using namespace clang::driver::toolchains;
21 using namespace clang;
22 using namespace llvm::opt;
23 
25  : GnuTool("wasm::Linker", "lld", TC) {}
26 
28  return true;
29 }
30 
32  return false;
33 }
34 
36  const InputInfo &Output,
37  const InputInfoList &Inputs,
38  const ArgList &Args,
39  const char *LinkingOutput) const {
40 
41  const ToolChain &ToolChain = getToolChain();
42  const char *Linker = Args.MakeArgString(ToolChain.GetLinkerPath());
43  ArgStringList CmdArgs;
44 
45  if (Args.hasArg(options::OPT_s))
46  CmdArgs.push_back("--strip-all");
47 
48  Args.AddAllArgs(CmdArgs, options::OPT_L);
49  Args.AddAllArgs(CmdArgs, options::OPT_u);
50  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
51 
52  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
53  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
54 
55  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
56 
57  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
58  if (ToolChain.ShouldLinkCXXStdlib(Args))
59  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
60 
61  if (Args.hasArg(options::OPT_pthread))
62  CmdArgs.push_back("-lpthread");
63 
64  CmdArgs.push_back("-lc");
65  AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
66  }
67 
68  CmdArgs.push_back("-o");
69  CmdArgs.push_back(Output.getFilename());
70 
71  C.addCommand(llvm::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs));
72 }
73 
74 WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
75  const llvm::opt::ArgList &Args)
76  : ToolChain(D, Triple, Args) {
77 
78  assert(Triple.isArch32Bit() != Triple.isArch64Bit());
79 
80  getProgramPaths().push_back(getDriver().getInstalledDir());
81 
82  getFilePaths().push_back(getDriver().SysRoot + "/lib");
83 }
84 
85 bool WebAssembly::IsMathErrnoDefault() const { return false; }
86 
87 bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
88 
89 bool WebAssembly::UseObjCMixedDispatch() const { return true; }
90 
91 bool WebAssembly::isPICDefault() const { return false; }
92 
93 bool WebAssembly::isPIEDefault() const { return false; }
94 
95 bool WebAssembly::isPICDefaultForced() const { return false; }
96 
97 bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
98 
99 bool WebAssembly::hasBlocksRuntime() const { return false; }
100 
101 // TODO: Support profiling.
102 bool WebAssembly::SupportsProfiling() const { return false; }
103 
104 bool WebAssembly::HasNativeLLVMSupport() const { return true; }
105 
106 void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
107  ArgStringList &CC1Args,
108  Action::OffloadKind) const {
109  if (DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
110  options::OPT_fno_use_init_array, true))
111  CC1Args.push_back("-fuse-init-array");
112 }
113 
114 ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
116 }
117 
118 ToolChain::CXXStdlibType WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
119  if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
120  StringRef Value = A->getValue();
121  if (Value != "libc++")
122  getDriver().Diag(diag::err_drv_invalid_stdlib_name)
123  << A->getAsString(Args);
124  }
125  return ToolChain::CST_Libcxx;
126 }
127 
128 void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
129  ArgStringList &CC1Args) const {
130  if (!DriverArgs.hasArg(options::OPT_nostdinc))
131  addSystemInclude(DriverArgs, CC1Args, getDriver().SysRoot + "/include");
132 }
133 
134 void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
135  ArgStringList &CC1Args) const {
136  if (!DriverArgs.hasArg(options::OPT_nostdlibinc) &&
137  !DriverArgs.hasArg(options::OPT_nostdincxx))
138  addSystemInclude(DriverArgs, CC1Args,
139  getDriver().SysRoot + "/include/c++/v1");
140 }
141 
142 void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
143  llvm::opt::ArgStringList &CmdArgs) const {
144 
145  switch (GetCXXStdlibType(Args)) {
147  CmdArgs.push_back("-lc++");
148  CmdArgs.push_back("-lc++abi");
149  break;
151  llvm_unreachable("invalid stdlib name");
152  }
153 }
154 
155 std::string WebAssembly::getThreadModel() const {
156  // The WebAssembly MVP does not yet support threads; for now, use the
157  // "single" threading model, which lowers atomics to non-atomic operations.
158  // When threading support is standardized and implemented in popular engines,
159  // this override should be removed.
160  return "single";
161 }
162 
163 Tool *WebAssembly::buildLinker() const {
164  return new tools::wasm::Linker(*this);
165 }
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:424
const char * getFilename() const
Definition: InputInfo.h:84
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:416
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:206
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:763
Base class for all GNU tools that provide the same behavior when it comes to response files support...
Definition: Gnu.h:41
const Driver & getDriver() const
Definition: ToolChain.h:181
bool isLinkJob() const override
Definition: WebAssembly.cpp:27
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:740
Dataflow Directional Tag Classes.
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
bool hasIntegratedCPP() const override
Definition: WebAssembly.cpp:31
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: WebAssembly.cpp:35
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:46
const ToolChain & getToolChain() const
Definition: Tool.h:84
virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: ToolChain.cpp:746
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:88