[llvm] r250875 - WebAssembly: support imports

JF Bastien via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 20 19:23:10 PDT 2015


Author: jfb
Date: Tue Oct 20 21:23:09 2015
New Revision: 250875

URL: http://llvm.org/viewvc/llvm-project?rev=250875&view=rev
Log:
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).

Added:
    llvm/trunk/test/CodeGen/WebAssembly/import.ll
Modified:
    llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp

Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp?rev=250875&r1=250874&r2=250875&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp Tue Oct 20 21:23:09 2015
@@ -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::EmitInstruct
   }
 }
 
+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);

Added: llvm/trunk/test/CodeGen/WebAssembly/import.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/WebAssembly/import.ll?rev=250875&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/WebAssembly/import.ll (added)
+++ llvm/trunk/test/CodeGen/WebAssembly/import.ll Tue Oct 20 21:23:09 2015
@@ -0,0 +1,21 @@
+; RUN: llc < %s -asm-verbose=false | FileCheck %s
+
+target datalayout = "e-p:32:32-i64:64-n32:64-S128"
+target triple = "wasm32-unknown-unknown"
+
+; CHECK-LABEL: .text
+; CHECK-LABEL: f:
+define void @f(i32 %a, float %b) {
+  tail call i32 @printi(i32 %a)
+  tail call float @printf(float %b)
+  tail call void @printv()
+  ret void
+}
+
+; CHECK-LABEL: .imports
+; CHECK-NEXT:  .import $printi "" "printi" (param i32) (result i32)
+; CHECK-NEXT:  .import $printf "" "printf" (param f32) (result f32)
+; CHECK-NEXT:  .import $printv "" "printv"
+declare i32 @printi(i32)
+declare float @printf(float)
+declare void @printv()




More information about the llvm-commits mailing list