[llvm] r319797 - [WebAssembly] Implement WASM_STACK_POINTER.

Dan Gohman via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 5 09:23:43 PST 2017


Author: djg
Date: Tue Dec  5 09:23:43 2017
New Revision: 319797

URL: http://llvm.org/viewvc/llvm-project?rev=319797&view=rev
Log:
[WebAssembly] Implement WASM_STACK_POINTER.

Use the .stack_pointer directive to implement WASM_STACK_POINTER for
specifying a global variable to be the stack pointer.

Modified:
    llvm/trunk/lib/MC/WasmObjectWriter.cpp
    llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
    llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h
    llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
    llvm/trunk/test/CodeGen/WebAssembly/stack-alignment.ll

Modified: llvm/trunk/lib/MC/WasmObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/WasmObjectWriter.cpp?rev=319797&r1=319796&r2=319797&view=diff
==============================================================================
--- llvm/trunk/lib/MC/WasmObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/WasmObjectWriter.cpp Tue Dec  5 09:23:43 2017
@@ -287,7 +287,7 @@ private:
   void writeLinkingMetaDataSection(
       ArrayRef<WasmDataSegment> Segments, uint32_t DataSize,
       SmallVector<std::pair<StringRef, uint32_t>, 4> SymbolFlags,
-      bool HasStackPointer, uint32_t StackPointerGlobal);
+      Optional<uint32_t> StackPointerGlobal);
 
   uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry);
   void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations,
@@ -929,14 +929,14 @@ void WasmObjectWriter::writeDataRelocSec
 void WasmObjectWriter::writeLinkingMetaDataSection(
     ArrayRef<WasmDataSegment> Segments, uint32_t DataSize,
     SmallVector<std::pair<StringRef, uint32_t>, 4> SymbolFlags,
-    bool HasStackPointer, uint32_t StackPointerGlobal) {
+    Optional<uint32_t> StackPointerGlobal) {
   SectionBookkeeping Section;
   startSection(Section, wasm::WASM_SEC_CUSTOM, "linking");
   SectionBookkeeping SubSection;
 
-  if (HasStackPointer) {
+  if (StackPointerGlobal.hasValue()) {
     startSection(SubSection, wasm::WASM_STACK_POINTER);
-    encodeULEB128(StackPointerGlobal, getStream()); // id
+    encodeULEB128(StackPointerGlobal.getValue(), getStream()); // id
     endSection(SubSection);
   }
 
@@ -1010,9 +1010,9 @@ void WasmObjectWriter::writeObject(MCAss
   SmallPtrSet<const MCSymbolWasm *, 4> IsAddressTaken;
   unsigned NumFuncImports = 0;
   SmallVector<WasmDataSegment, 4> DataSegments;
-  uint32_t StackPointerGlobal = 0;
+  Optional<StringRef> StackPointerGlobalName;
+  Optional<uint32_t> StackPointerGlobal;
   uint32_t DataSize = 0;
-  bool HasStackPointer = false;
 
   // Populate the IsAddressTaken set.
   for (const WasmRelocationEntry &RelEntry : CodeRelocations) {
@@ -1143,10 +1143,7 @@ void WasmObjectWriter::writeObject(MCAss
     if (!DataFrag.getFixups().empty())
       report_fatal_error("fixups not supported in .stack_pointer");
     const SmallVectorImpl<char> &Contents = DataFrag.getContents();
-    if (Contents.size() != 4)
-      report_fatal_error("only one entry supported in .stack_pointer");
-    HasStackPointer = true;
-    StackPointerGlobal = NumGlobalImports + *(const int32_t *)Contents.data();
+    StackPointerGlobalName = StringRef(Contents.data(), Contents.size());
   }
 
   for (MCSection &Sec : Asm) {
@@ -1255,6 +1252,10 @@ void WasmObjectWriter::writeObject(MCAss
       SymbolIndices[&WS] = Index;
       DEBUG(dbgs() << "  -> global index: " << Index << "\n");
       Globals.push_back(Global);
+
+      if (StackPointerGlobalName.hasValue() &&
+          WS.getName() == StackPointerGlobalName.getValue())
+        StackPointerGlobal = Index;
     }
 
     // If the symbol is visible outside this translation unit, export it.
@@ -1331,7 +1332,7 @@ void WasmObjectWriter::writeObject(MCAss
   writeCodeRelocSection();
   writeDataRelocSection();
   writeLinkingMetaDataSection(DataSegments, DataSize, SymbolFlags,
-                              HasStackPointer, StackPointerGlobal);
+                              StackPointerGlobal);
 
   // TODO: Translate the .comment section to the output.
   // TODO: Translate debug sections to the output.

Modified: llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp?rev=319797&r1=319796&r2=319797&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp (original)
+++ llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp Tue Dec  5 09:23:43 2017
@@ -108,8 +108,8 @@ void WebAssemblyTargetAsmStreamer::emitG
   }
 }
 
-void WebAssemblyTargetAsmStreamer::emitStackPointer(uint32_t Index) {
-  OS << "\t.stack_pointer\t" << Index << '\n';
+void WebAssemblyTargetAsmStreamer::emitStackPointer(MCSymbol *Symbol) {
+  OS << "\t.stack_pointer\t" << Symbol->getName() << '\n';
 }
 
 void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; }
@@ -158,7 +158,7 @@ void WebAssemblyTargetELFStreamer::emitG
 }
 
 void WebAssemblyTargetELFStreamer::emitStackPointer(
-    uint32_t Index) {
+    MCSymbol *Symbol) {
   llvm_unreachable(".stack_pointer encoding not yet implemented");
 }
 
@@ -238,11 +238,11 @@ void WebAssemblyTargetWasmStreamer::emit
   Streamer.PopSection();
 }
 
-void WebAssemblyTargetWasmStreamer::emitStackPointer(uint32_t Index) {
+void WebAssemblyTargetWasmStreamer::emitStackPointer(MCSymbol *Symbol) {
   Streamer.PushSection();
   Streamer.SwitchSection(Streamer.getContext().getWasmSection(
       ".stack_pointer", SectionKind::getMetadata()));
-  Streamer.EmitIntValue(Index, 4);
+  Streamer.EmitBytes(Symbol->getName());
   Streamer.PopSection();
 }
 
@@ -277,4 +277,5 @@ void WebAssemblyTargetWasmStreamer::emit
 }
 
 void WebAssemblyTargetWasmStreamer::emitGlobalImport(StringRef name) {
+  llvm_unreachable(".global_import is not needed for direct wasm output");
 }

Modified: llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h?rev=319797&r1=319796&r2=319797&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h (original)
+++ llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h Tue Dec  5 09:23:43 2017
@@ -40,7 +40,7 @@ public:
   /// .globalvar
   virtual void emitGlobal(ArrayRef<wasm::Global> Globals) = 0;
   /// .stack_pointer
-  virtual void emitStackPointer(uint32_t Index) = 0;
+  virtual void emitStackPointer(MCSymbol *Symbol) = 0;
   /// .endfunc
   virtual void emitEndFunc() = 0;
   /// .functype
@@ -67,7 +67,7 @@ public:
   void emitResult(MCSymbol *Symbol, ArrayRef<MVT> Types) override;
   void emitLocal(ArrayRef<MVT> Types) override;
   void emitGlobal(ArrayRef<wasm::Global> Globals) override;
-  void emitStackPointer(uint32_t Index) override;
+  void emitStackPointer(MCSymbol *Symbol) override;
   void emitEndFunc() override;
   void emitIndirectFunctionType(MCSymbol *Symbol,
                                 SmallVectorImpl<MVT> &Params,
@@ -85,7 +85,7 @@ public:
   void emitResult(MCSymbol *Symbol, ArrayRef<MVT> Types) override;
   void emitLocal(ArrayRef<MVT> Types) override;
   void emitGlobal(ArrayRef<wasm::Global> Globals) override;
-  void emitStackPointer(uint32_t Index) override;
+  void emitStackPointer(MCSymbol *Symbol) override;
   void emitEndFunc() override;
   void emitIndirectFunctionType(MCSymbol *Symbol,
                                 SmallVectorImpl<MVT> &Params,
@@ -103,7 +103,7 @@ public:
   void emitResult(MCSymbol *Symbol, ArrayRef<MVT> Types) override;
   void emitLocal(ArrayRef<MVT> Types) override;
   void emitGlobal(ArrayRef<wasm::Global> Globals) override;
-  void emitStackPointer(uint32_t Index) override;
+  void emitStackPointer(MCSymbol *Symbol) override;
   void emitEndFunc() override;
   void emitIndirectFunctionType(MCSymbol *Symbol,
                                 SmallVectorImpl<MVT> &Params,

Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp?rev=319797&r1=319796&r2=319797&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp Tue Dec  5 09:23:43 2017
@@ -78,6 +78,10 @@ WebAssemblyTargetStreamer *WebAssemblyAs
 //===----------------------------------------------------------------------===//
 
 void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
+  // Declare the stack pointer.
+  getTargetStreamer()->emitStackPointer(
+      GetExternalSymbolSymbol("__stack_pointer"));
+
   for (const auto &F : M) {
     // Emit function type info for all undefined functions
     if (F.isDeclarationForLinker() && !F.isIntrinsic()) {

Modified: llvm/trunk/test/CodeGen/WebAssembly/stack-alignment.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/WebAssembly/stack-alignment.ll?rev=319797&r1=319796&r2=319797&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/WebAssembly/stack-alignment.ll (original)
+++ llvm/trunk/test/CodeGen/WebAssembly/stack-alignment.ll Tue Dec  5 09:23:43 2017
@@ -147,3 +147,5 @@ entry:
   call void @somefunc(i32* %static)
   ret void
 }
+
+; CHECK: .stack_pointer __stack_pointer




More information about the llvm-commits mailing list