[WebAssembly] Added a .globaltype directive to .s output.
authorWouter van Oortmerssen <aardappel@gmail.com>
Fri, 2 Nov 2018 00:45:00 +0000 (00:45 +0000)
committerWouter van Oortmerssen <aardappel@gmail.com>
Fri, 2 Nov 2018 00:45:00 +0000 (00:45 +0000)
Summary:
Assembly output can use globals like __stack_pointer implicitly,
but has no way of indicating the type of such a global, which makes
it hard for tools processing it (such as the MC Assembler) to
reconstruct this information.

The improved assembler directives parsing (in progress in
https://reviews.llvm.org/D53842) will make use of this information.

Also deleted code for the .import_global directive which was unused.

New test case in userstack.ll

Reviewers: dschuff, sbc100

Subscribers: jgravelle-google, aheejin, sunfish, llvm-commits

Differential Revision: https://reviews.llvm.org/D54012

llvm-svn: 345917

llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h
llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
llvm/test/CodeGen/WebAssembly/userstack.ll

index 2158ee3..4c4ca4e 100644 (file)
@@ -99,8 +99,11 @@ void WebAssemblyTargetAsmStreamer::emitIndirectFunctionType(
   OS << '\n';
 }
 
-void WebAssemblyTargetAsmStreamer::emitGlobalImport(StringRef name) {
-  OS << "\t.import_global\t" << name << '\n';
+void WebAssemblyTargetAsmStreamer::emitGlobalType(MCSymbolWasm *Sym) {
+  OS << "\t.globaltype\t" << Sym->getName() << ", " <<
+        WebAssembly::TypeToString(
+          static_cast<wasm::ValType>(Sym->getGlobalType().Type)) <<
+        '\n';
 }
 
 void WebAssemblyTargetAsmStreamer::emitImportModule(MCSymbolWasm *Sym,
@@ -152,8 +155,8 @@ void WebAssemblyTargetWasmStreamer::emitIndirectFunctionType(
   Symbol->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
 }
 
-void WebAssemblyTargetWasmStreamer::emitGlobalImport(StringRef name) {
-  llvm_unreachable(".global_import is not needed for direct wasm output");
+void WebAssemblyTargetWasmStreamer::emitGlobalType(MCSymbolWasm *Sym) {
+  // Not needed.
 }
 
 void WebAssemblyTargetWasmStreamer::emitImportModule(MCSymbolWasm *Sym,
index 43c422d..e60158b 100644 (file)
@@ -43,8 +43,8 @@ public:
   virtual void emitIndirectFunctionType(MCSymbolWasm *Symbol) = 0;
   /// .indidx
   virtual void emitIndIdx(const MCExpr *Value) = 0;
-  /// .import_global
-  virtual void emitGlobalImport(StringRef name) = 0;
+  /// .globaltype
+  virtual void emitGlobalType(MCSymbolWasm *Sym) = 0;
   /// .import_module
   virtual void emitImportModule(MCSymbolWasm *Sym, StringRef ModuleName) = 0;
 
@@ -65,7 +65,7 @@ public:
   void emitEndFunc() override;
   void emitIndirectFunctionType(MCSymbolWasm *Symbol) override;
   void emitIndIdx(const MCExpr *Value) override;
-  void emitGlobalImport(StringRef name) override;
+  void emitGlobalType(MCSymbolWasm *Sym) override;
   void emitImportModule(MCSymbolWasm *Sym, StringRef ModuleName) override;
 };
 
@@ -80,7 +80,7 @@ public:
   void emitEndFunc() override;
   void emitIndirectFunctionType(MCSymbolWasm *Symbol) override;
   void emitIndIdx(const MCExpr *Value) override;
-  void emitGlobalImport(StringRef name) override;
+  void emitGlobalType(MCSymbolWasm *Sym) override;
   void emitImportModule(MCSymbolWasm *Sym, StringRef ModuleName) override;
 };
 
index b8ac859..1e21ab9 100644 (file)
@@ -78,6 +78,14 @@ WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() {
 //===----------------------------------------------------------------------===//
 
 void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
+  for (auto &It : OutContext.getSymbols()) {
+    // Emit a .globaltype declaration.
+    auto Sym = cast<MCSymbolWasm>(It.getValue());
+    if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_GLOBAL) {
+      getTargetStreamer()->emitGlobalType(Sym);
+    }
+  }
+
   for (const auto &F : M) {
     // Emit function type info for all undefined functions
     if (F.isDeclarationForLinker() && !F.isIntrinsic()) {
@@ -105,6 +113,7 @@ void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
       }
     }
   }
+
   for (const auto &G : M.globals()) {
     if (!G.hasInitializer() && G.hasExternalLinkage()) {
       if (G.getValueType()->isSized()) {
index 4a6b48b..aa4acae 100644 (file)
@@ -330,4 +330,6 @@ define void @inline_asm() {
   ret void
 }
 
+; CHECK: .globaltype   __stack_pointer, i32{{$}}
+
 ; TODO: test over-aligned alloca