clang  7.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  CmdArgs.push_back("-flavor");
45  CmdArgs.push_back("wasm");
46 
47  if (Args.hasArg(options::OPT_s))
48  CmdArgs.push_back("--strip-all");
49 
50  Args.AddAllArgs(CmdArgs, options::OPT_L);
51  Args.AddAllArgs(CmdArgs, options::OPT_u);
52  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
53 
54  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
55  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
56 
57  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
58 
59  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
60  if (ToolChain.ShouldLinkCXXStdlib(Args))
61  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
62 
63  if (Args.hasArg(options::OPT_pthread))
64  CmdArgs.push_back("-lpthread");
65 
66  CmdArgs.push_back("-lc");
67  AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
68  }
69 
70  CmdArgs.push_back("-o");
71  CmdArgs.push_back(Output.getFilename());
72 
73  C.addCommand(llvm::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs));
74 }
75 
76 WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
77  const llvm::opt::ArgList &Args)
78  : ToolChain(D, Triple, Args) {
79 
80  assert(Triple.isArch32Bit() != Triple.isArch64Bit());
81 
82  getProgramPaths().push_back(getDriver().getInstalledDir());
83 
84  getFilePaths().push_back(getDriver().SysRoot + "/lib");
85 }
86 
87 bool WebAssembly::IsMathErrnoDefault() const { return false; }
88 
89 bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
90 
91 bool WebAssembly::UseObjCMixedDispatch() const { return true; }
92 
93 bool WebAssembly::isPICDefault() const { return false; }
94 
95 bool WebAssembly::isPIEDefault() const { return false; }
96 
97 bool WebAssembly::isPICDefaultForced() const { return false; }
98 
99 bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
100 
101 bool WebAssembly::hasBlocksRuntime() const { return false; }
102 
103 // TODO: Support profiling.
104 bool WebAssembly::SupportsProfiling() const { return false; }
105 
106 bool WebAssembly::HasNativeLLVMSupport() const { return true; }
107 
108 void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
109  ArgStringList &CC1Args,
110  Action::OffloadKind) const {
111  if (DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
112  options::OPT_fno_use_init_array, true))
113  CC1Args.push_back("-fuse-init-array");
114 }
115 
116 ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
118 }
119 
120 ToolChain::CXXStdlibType WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
121  if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
122  StringRef Value = A->getValue();
123  if (Value != "libc++")
124  getDriver().Diag(diag::err_drv_invalid_stdlib_name)
125  << A->getAsString(Args);
126  }
127  return ToolChain::CST_Libcxx;
128 }
129 
130 void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
131  ArgStringList &CC1Args) const {
132  if (!DriverArgs.hasArg(options::OPT_nostdinc))
133  addSystemInclude(DriverArgs, CC1Args, getDriver().SysRoot + "/include");
134 }
135 
136 void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
137  ArgStringList &CC1Args) const {
138  if (!DriverArgs.hasArg(options::OPT_nostdlibinc) &&
139  !DriverArgs.hasArg(options::OPT_nostdincxx))
140  addSystemInclude(DriverArgs, CC1Args,
141  getDriver().SysRoot + "/include/c++/v1");
142 }
143 
144 void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
145  llvm::opt::ArgStringList &CmdArgs) const {
146 
147  switch (GetCXXStdlibType(Args)) {
149  CmdArgs.push_back("-lc++");
150  CmdArgs.push_back("-lc++abi");
151  break;
153  llvm_unreachable("invalid stdlib name");
154  }
155 }
156 
157 std::string WebAssembly::getThreadModel() const {
158  // The WebAssembly MVP does not yet support threads; for now, use the
159  // "single" threading model, which lowers atomics to non-atomic operations.
160  // When threading support is standardized and implemented in popular engines,
161  // this override should be removed.
162  return "single";
163 }
164 
165 Tool *WebAssembly::buildLinker() const {
166  return new tools::wasm::Linker(*this);
167 }
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:411
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:403
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:750
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:727
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:733
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:88