diff options
author | JF Bastien <jfb@google.com> | 2015-10-21 02:23:09 +0000 |
---|---|---|
committer | JF Bastien <jfb@google.com> | 2015-10-21 02:23:09 +0000 |
commit | 1a59c6b2c967e76cd5e5d9884e47da133a430e52 (patch) | |
tree | e9ff0ff0d1a43fd0cfed4669f0b9d4308002e595 /llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp | |
parent | 8dd3fdbf43640021964546bb5b3bac52a3b7fcaa (diff) | |
download | llvm-1a59c6b2c967e76cd5e5d9884e47da133a430e52.zip llvm-1a59c6b2c967e76cd5e5d9884e47da133a430e52.tar.gz llvm-1a59c6b2c967e76cd5e5d9884e47da133a430e52.tar.bz2 |
WebAssembly: support imports
C/C++ code can declare an extern function, which will show up as an import in WebAssembly's output. It's expected that the linker will resolve these, and mark unresolved imports as call_import (I have a patch which does this in wasmate).
llvm-svn: 250875
Diffstat (limited to 'llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp')
-rw-r--r-- | llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp index e20ee69..f3e3170 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp @@ -77,8 +77,8 @@ private: void EmitJumpTableInfo() override; void EmitConstantPool() override; void EmitFunctionBodyStart() override; - void EmitInstruction(const MachineInstr *MI) override; + void EmitEndOfAsmFile(Module &M) override; std::string getRegTypeName(unsigned RegNo) const; static std::string toString(const APFloat &APF); @@ -330,6 +330,28 @@ void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) { } } +void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) { + SmallString<128> Str; + raw_svector_ostream OS(Str); + for (const Function &F : M) + if (F.isDeclarationForLinker()) { + assert(F.hasName() && "imported functions must have a name"); + if (F.getName().startswith("llvm.")) + continue; + if (Str.empty()) + OS << "\t.imports\n"; + Type *Rt = F.getReturnType(); + OS << "\t.import " << toSymbol(F.getName()) << " \"\" \"" << F.getName() + << "\""; + for (const Argument &A : F.args()) + OS << " (param " << toString(A.getType()) << ')'; + if (!Rt->isVoidTy()) + OS << " (result " << toString(Rt) << ')'; + OS << '\n'; + } + OutStreamer->EmitRawText(OS.str()); +} + // Force static initialization. extern "C" void LLVMInitializeWebAssemblyAsmPrinter() { RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32); |