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