]> granicus.if.org Git - llvm/commitdiff
[WebAssembly] Use __stack_pointer global when writing wasm binary
authorSam Clegg <sbc@chromium.org>
Fri, 16 Jun 2017 23:59:10 +0000 (23:59 +0000)
committerSam Clegg <sbc@chromium.org>
Fri, 16 Jun 2017 23:59:10 +0000 (23:59 +0000)
This ensures that symbolic relocations are generated for stack
pointer manipulations.

These relocations are of type R_WEBASSEMBLY_GLOBAL_INDEX_LEB.
This change also adds support for reading relocations of this
type in WasmObjectFile.cpp.

Since its a globally imported symbol this does mean that
the get_global/set_global instruction won't be valid until
the objects are linked that global used in no longer an
imported global.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305616 91177308-0d34-0410-b5e6-96231b3b80d8

17 files changed:
include/llvm/CodeGen/MachineModuleInfoImpls.h
lib/CodeGen/MachineModuleInfoImpls.cpp
lib/MC/WasmObjectWriter.cpp
lib/Object/WasmObjectFile.cpp
lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp
lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.cpp
lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h
lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp
lib/Target/WebAssembly/WebAssemblyRegStackify.cpp
test/CodeGen/WebAssembly/byval.ll
test/CodeGen/WebAssembly/reg-stackify.ll
test/CodeGen/WebAssembly/stack-alignment.ll
test/CodeGen/WebAssembly/userstack.ll
test/MC/WebAssembly/func-address.ll
test/MC/WebAssembly/stack-ptr.ll [new file with mode: 0644]

index 61cff3890b75fdbd4624846d5f5a97abcdb40b74..34b21ceddd4340ec26aab7fbad4faab2ffe23daa 100644 (file)
@@ -77,33 +77,6 @@ public:
   SymbolListTy GetGVStubList() { return getSortedStubs(GVStubs); }
 };
 
-/// MachineModuleInfoWasm - This is a MachineModuleInfoImpl implementation
-/// for Wasm targets.
-class MachineModuleInfoWasm : public MachineModuleInfoImpl {
-  /// WebAssembly global variables defined by CodeGen.
-  std::vector<wasm::Global> Globals;
-
-  /// The WebAssembly global variable which is the stack pointer.
-  unsigned StackPointerGlobal;
-
-  virtual void anchor(); // Out of line virtual method.
-public:
-  MachineModuleInfoWasm(const MachineModuleInfo &)
-    : StackPointerGlobal(-1U) {}
-
-  void addGlobal(const wasm::Global &G) { Globals.push_back(G); }
-  const std::vector<wasm::Global> &getGlobals() const { return Globals; }
-
-  bool hasStackPointerGlobal() const {
-    return StackPointerGlobal != -1U;
-  }
-  unsigned getStackPointerGlobal() const {
-    assert(hasStackPointerGlobal() && "Stack ptr global hasn't been set");
-    return StackPointerGlobal;
-  }
-  void setStackPointerGlobal(unsigned Global) { StackPointerGlobal = Global; }
-};
-
 } // end namespace llvm
 
 #endif
index 4c81fd91cb829e9cf6bc5b16fafa5af5bd9b0b1e..22d519e5d88fa38a95e99ac1780a0daa31971e79 100644 (file)
@@ -23,7 +23,6 @@ using namespace llvm;
 // Out of line virtual method.
 void MachineModuleInfoMachO::anchor() {}
 void MachineModuleInfoELF::anchor() {}
-void MachineModuleInfoWasm::anchor() {}
 
 static int SortSymbolPair(const void *LHS, const void *RHS) {
   typedef std::pair<MCSymbol*, MachineModuleInfoImpl::StubValueTy> PairTy;
index db304c027f991b51de8fee863c2bf26d55fd6acd..7ac4fb8767eedb10cc3814ab072965342602b649 100644 (file)
@@ -406,6 +406,8 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
   }
 
   assert(!IsPCRel);
+  assert(SymA);
+
   unsigned Type = getRelocType(Target, Fixup);
 
   WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
@@ -474,6 +476,7 @@ uint32_t WasmObjectWriter::getRelocationIndexValue(
     assert(IndirectSymbolIndices.count(RelEntry.Symbol));
     return IndirectSymbolIndices[RelEntry.Symbol];
   case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
+  case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
   case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB:
   case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB:
   case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32:
@@ -500,7 +503,8 @@ void WasmObjectWriter::applyRelocations(
     switch (RelEntry.Type) {
     case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
     case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
-    case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB: {
+    case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
+    case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: {
       uint32_t Index = getRelocationIndexValue(RelEntry);
       WritePatchableSLEB(Stream, Index, Offset);
       break;
@@ -526,7 +530,7 @@ void WasmObjectWriter::applyRelocations(
       break;
     }
     default:
-      llvm_unreachable("unsupported relocation type");
+      llvm_unreachable("invalid relocation type");
     }
   }
 }
index 2304098c1dc9f7eaa1b7e74121e109bf46818e4e..5e0affb9ef94515c1802404c5c74dbd440c0761e 100644 (file)
@@ -325,6 +325,7 @@ Error WasmObjectFile::parseRelocSection(StringRef Name, const uint8_t *Ptr,
     case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
     case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
     case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
+    case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
       break;
     case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB:
     case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB:
@@ -332,7 +333,8 @@ Error WasmObjectFile::parseRelocSection(StringRef Name, const uint8_t *Ptr,
       Reloc.Addend = readVarint32(Ptr);
       break;
     default:
-      return make_error<GenericBinaryError>("Bad relocation type",
+      return make_error<GenericBinaryError>("Bad relocation type: " +
+                                                Twine(Reloc.Type),
                                             object_error::parse_failed);
     }
     Section->Relocations.push_back(Reloc);
index c56c591def361943705f797a94b632d1b02b3a20..3e3b52fca5691387c50f3e471cde27b15677970a 100644 (file)
@@ -36,7 +36,6 @@ STATISTIC(MCNumFixups, "Number of MC fixups created.");
 namespace {
 class WebAssemblyMCCodeEmitter final : public MCCodeEmitter {
   const MCInstrInfo &MCII;
-  MCContext &Ctx;
 
   // Implementation generated by tablegen.
   uint64_t getBinaryCodeForInstr(const MCInst &MI,
@@ -48,14 +47,12 @@ class WebAssemblyMCCodeEmitter final : public MCCodeEmitter {
                          const MCSubtargetInfo &STI) const override;
 
 public:
-  WebAssemblyMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
-      : MCII(mcii), Ctx(ctx) {}
+  WebAssemblyMCCodeEmitter(const MCInstrInfo &mcii) : MCII(mcii) {}
 };
 } // end anonymous namespace
 
-MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII,
-                                                    MCContext &Ctx) {
-  return new WebAssemblyMCCodeEmitter(MCII, Ctx);
+MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) {
+  return new WebAssemblyMCCodeEmitter(MCII);
 }
 
 void WebAssemblyMCCodeEmitter::encodeInstruction(
@@ -89,11 +86,7 @@ void WebAssemblyMCCodeEmitter::encodeInstruction(
         } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) {
           encodeSLEB128(int64_t(MO.getImm()), OS);
         } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) {
-          Fixups.push_back(MCFixup::create(
-              OS.tell() - Start, MCConstantExpr::create(MO.getImm(), Ctx),
-              MCFixupKind(WebAssembly::fixup_code_global_index), MI.getLoc()));
-          ++MCNumFixups;
-          encodeULEB128(uint64_t(MO.getImm()), OS);
+          llvm_unreachable("wasm globals should only be accessed symbolicly");
         } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) {
           encodeSLEB128(int64_t(MO.getImm()), OS);
         } else {
@@ -135,6 +128,9 @@ void WebAssemblyMCCodeEmitter::encodeInstruction(
                  Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
         FixupKind = MCFixupKind(WebAssembly::fixup_code_uleb128_i32);
         PaddedSize = 5;
+      } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) {
+        FixupKind = MCFixupKind(WebAssembly::fixup_code_global_index);
+        PaddedSize = 5;
       } else {
         llvm_unreachable("unexpected symbolic operand kind");
       }
index 9fd3ec81c258f4209e972611c462eb8f191671f3..9580eeaa33d7347ec46d838cc092f67908b36af0 100644 (file)
@@ -74,7 +74,7 @@ static MCInstPrinter *createMCInstPrinter(const Triple & /*T*/,
 static MCCodeEmitter *createCodeEmitter(const MCInstrInfo &MCII,
                                         const MCRegisterInfo & /*MRI*/,
                                         MCContext &Ctx) {
-  return createWebAssemblyMCCodeEmitter(MCII, Ctx);
+  return createWebAssemblyMCCodeEmitter(MCII);
 }
 
 static MCAsmBackend *createAsmBackend(const Target & /*T*/,
index 0ba700a86b744fb2dea397e913125a105f05f6b5..4d676c32a09c511dce6b6449cbad36d6798e79b0 100644 (file)
@@ -35,8 +35,7 @@ class raw_pwrite_stream;
 Target &getTheWebAssemblyTarget32();
 Target &getTheWebAssemblyTarget64();
 
-MCCodeEmitter *createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII,
-                                              MCContext &Ctx);
+MCCodeEmitter *createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII);
 
 MCAsmBackend *createWebAssemblyAsmBackend(const Triple &TT);
 
index 19e14f3261aa7d1ca0c954d66a782d5c73f668a1..9cf77829f3bc26f94961b3f29a5c4d373a416a8b 100644 (file)
@@ -68,6 +68,8 @@ WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target,
   bool IsFunction = IsFunctionExpr(Fixup.getValue());
 
   switch (unsigned(Fixup.getKind())) {
+  case WebAssembly::fixup_code_global_index:
+    return wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB;
   case WebAssembly::fixup_code_sleb128_i32:
     if (IsFunction)
       return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB;
index b999091e2d2940476b2e0a28a86a0cce6ed61b3a..f51585a10ca122358967d9f8e960eda28c952c27 100644 (file)
@@ -96,13 +96,6 @@ void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
                                MCConstantExpr::create(Size, OutContext));
     }
   }
-
-  if (!TM.getTargetTriple().isOSBinFormatELF()) {
-    MachineModuleInfoWasm &MMIW = MMI->getObjFileInfo<MachineModuleInfoWasm>();
-    getTargetStreamer()->emitGlobal(MMIW.getGlobals());
-    if (MMIW.hasStackPointerGlobal())
-      getTargetStreamer()->emitStackPointer(MMIW.getStackPointerGlobal());
-  }
 }
 
 void WebAssemblyAsmPrinter::EmitConstantPool() {
index 4209bc333f230640d83efbc83a48743312defce1..a37d6136e44ed87c5efdc228c790a8c7124d6ad2 100644 (file)
@@ -104,10 +104,10 @@ static void writeSPToMemory(unsigned SrcReg, MachineFunction &MF,
                             const DebugLoc &DL) {
   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
 
+  const char *ES = "__stack_pointer";
+  auto *SPSymbol = MF.createExternalSymbolName(ES);
   if (MF.getSubtarget<WebAssemblySubtarget>()
         .getTargetTriple().isOSBinFormatELF()) {
-    const char *ES = "__stack_pointer";
-    auto *SPSymbol = MF.createExternalSymbolName(ES);
     MachineRegisterInfo &MRI = MF.getRegInfo();
     const TargetRegisterClass *PtrRC =
         MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
@@ -125,10 +125,8 @@ static void writeSPToMemory(unsigned SrcReg, MachineFunction &MF,
         .addReg(SrcReg)
         .addMemOperand(MMO);
   } else {
-    MachineModuleInfoWasm &MMIW =
-        MF.getMMI().getObjFileInfo<MachineModuleInfoWasm>();
     BuildMI(MBB, InsertStore, DL, TII->get(WebAssembly::SET_GLOBAL_I32))
-        .addImm(MMIW.getStackPointerGlobal())
+        .addExternalSymbol(SPSymbol)
         .addReg(SrcReg);
   }
 }
@@ -171,10 +169,11 @@ void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
   unsigned SPReg = WebAssembly::SP32;
   if (StackSize)
     SPReg = MRI.createVirtualRegister(PtrRC);
+
+  const char *ES = "__stack_pointer";
+  auto *SPSymbol = MF.createExternalSymbolName(ES);
   if (MF.getSubtarget<WebAssemblySubtarget>()
         .getTargetTriple().isOSBinFormatELF()) {
-    const char *ES = "__stack_pointer";
-    auto *SPSymbol = MF.createExternalSymbolName(ES);
     unsigned Zero = MRI.createVirtualRegister(PtrRC);
 
     BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), Zero)
@@ -189,22 +188,8 @@ void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
         .addReg(Zero)    // addr
         .addMemOperand(LoadMMO);
   } else {
-    auto &MMIW = MF.getMMI().getObjFileInfo<MachineModuleInfoWasm>();
-    if (!MMIW.hasStackPointerGlobal()) {
-      MMIW.setStackPointerGlobal(MMIW.getGlobals().size());
-
-      // Create the stack-pointer global. For now, just use the
-      // Emscripten/Binaryen ABI names.
-      wasm::Global G;
-      G.Type = wasm::ValType::I32;
-      G.Mutable = true;
-      G.InitialValue = 0;
-      G.InitialModule = "env";
-      G.InitialName = "STACKTOP";
-      MMIW.addGlobal(G);
-    }
     BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::GET_GLOBAL_I32), SPReg)
-        .addImm(MMIW.getStackPointerGlobal());
+        .addExternalSymbol(SPSymbol);
   }
 
   bool HasBP = hasBP(MF);
index 6650191807dcbc911e30d1bd5f9f2bb099131fbe..ea9e3fa862ce2b84b07d76c21291644361358864 100644 (file)
@@ -170,28 +170,16 @@ static void Query(const MachineInstr &MI, AliasAnalysis &AA, bool &Read,
   if (MI.mayStore()) {
     Write = true;
 
-    const MachineFunction &MF = *MI.getParent()->getParent();
-    if (MF.getSubtarget<WebAssemblySubtarget>()
-          .getTargetTriple().isOSBinFormatELF()) {
-      // Check for stores to __stack_pointer.
-      for (auto MMO : MI.memoperands()) {
-        const MachinePointerInfo &MPI = MMO->getPointerInfo();
-        if (MPI.V.is<const PseudoSourceValue *>()) {
-          auto PSV = MPI.V.get<const PseudoSourceValue *>();
-          if (const ExternalSymbolPseudoSourceValue *EPSV =
-                  dyn_cast<ExternalSymbolPseudoSourceValue>(PSV))
-            if (StringRef(EPSV->getSymbol()) == "__stack_pointer")
-              StackPointer = true;
-        }
-      }
-    } else {
-      // Check for sets of the stack pointer.
-      const MachineModuleInfoWasm &MMIW =
-          MF.getMMI().getObjFileInfo<MachineModuleInfoWasm>();
-      if ((MI.getOpcode() == WebAssembly::SET_LOCAL_I32 ||
-           MI.getOpcode() == WebAssembly::SET_LOCAL_I64) &&
-          MI.getOperand(0).getImm() == MMIW.getStackPointerGlobal()) {
-        StackPointer = true;
+    // Check for stores to __stack_pointer.
+    for (auto MMO : MI.memoperands()) {
+      const MachinePointerInfo &MPI = MMO->getPointerInfo();
+      if (MPI.V.is<const PseudoSourceValue *>()) {
+        auto PSV = MPI.V.get<const PseudoSourceValue *>();
+        if (const ExternalSymbolPseudoSourceValue *EPSV =
+                dyn_cast<ExternalSymbolPseudoSourceValue>(PSV))
+          if (StringRef(EPSV->getSymbol()) == "__stack_pointer") {
+            StackPointer = true;
+          }
       }
     }
   } else if (MI.hasOrderedMemoryRef()) {
index 907320d7977c7a1560612920ba2eaa4e65560054..84c5ad02101ce63749c665f3a85b7d3b1dd40b0a 100644 (file)
@@ -24,12 +24,12 @@ declare void @ext_byval_func_empty(%EmptyStruct* byval)
 define void @byval_arg(%SmallStruct* %ptr) {
  ; CHECK: .param i32
  ; Subtract 16 from SP (SP is 16-byte aligned)
- ; CHECK-NEXT: get_global $push[[L2:.+]]=, 0
+ ; CHECK-NEXT: get_global $push[[L2:.+]]=, __stack_pointer
  ; CHECK-NEXT: i32.const $push[[L3:.+]]=, 16
  ; CHECK-NEXT: i32.sub $push[[L11:.+]]=, $pop[[L2]], $pop[[L3]]
  ; Ensure SP is stored back before the call
  ; CHECK-NEXT: tee_local $push[[L10:.+]]=, $[[SP:.+]]=, $pop[[L11]]{{$}}
- ; CHECK-NEXT: set_global 0, $pop[[L10]]{{$}}
+ ; CHECK-NEXT: set_global __stack_pointer, $pop[[L10]]{{$}}
  ; Copy the SmallStruct argument to the stack (SP+12, original SP-4)
  ; CHECK-NEXT: i32.load $push[[L0:.+]]=, 0($0)
  ; CHECK-NEXT: i32.store 12($[[SP]]), $pop[[L0]]
@@ -41,7 +41,7 @@ define void @byval_arg(%SmallStruct* %ptr) {
  ; Restore the stack
  ; CHECK-NEXT: i32.const $push[[L6:.+]]=, 16
  ; CHECK-NEXT: i32.add $push[[L8:.+]]=, $[[SP]], $pop[[L6]]
- ; CHECK-NEXT: set_global 0, $pop[[L8]]
+ ; CHECK-NEXT: set_global __stack_pointer, $pop[[L8]]
  ; CHECK-NEXT: return
  ret void
 }
@@ -53,7 +53,7 @@ define void @byval_arg_align8(%SmallStruct* %ptr) {
  ; CHECK: i32.const $push[[L1:.+]]=, 16
  ; CHECK-NEXT: i32.sub $push[[L11:.+]]=, {{.+}}, $pop[[L1]]
  ; CHECK-NEXT: tee_local $push[[L10:.+]]=, $[[SP:.+]]=, $pop[[L11]]{{$}}
- ; CHECK-NEXT: set_global 0, $pop[[L10]]{{$}}
+ ; CHECK-NEXT: set_global __stack_pointer, $pop[[L10]]{{$}}
  ; Copy the SmallStruct argument to the stack (SP+8, original SP-8)
  ; CHECK-NEXT: i32.load $push[[L0:.+]]=, 0($0){{$}}
  ; CHECK-NEXT: i32.store 8($[[SP]]), $pop[[L0]]{{$}}
@@ -72,7 +72,7 @@ define void @byval_arg_double(%AlignedStruct* %ptr) {
  ; CHECK: i32.const $push[[L1:.+]]=, 16
  ; CHECK-NEXT: i32.sub $push[[L14:.+]]=, {{.+}}, $pop[[L1]]
  ; CHECK-NEXT: tee_local $push[[L13:.+]]=, $[[SP:.+]]=, $pop[[L14]]
- ; CHECK-NEXT: set_global 0, $pop[[L13]]
+ ; CHECK-NEXT: set_global __stack_pointer, $pop[[L13]]
  ; Copy the AlignedStruct argument to the stack (SP+0, original SP-16)
  ; Just check the last load/store pair of the memcpy
  ; CHECK: i64.load $push[[L4:.+]]=, 0($0)
@@ -110,11 +110,11 @@ define void @byval_empty_callee(%EmptyStruct* byval %ptr) {
 
 ; Call memcpy for "big" byvals.
 ; CHECK-LABEL: big_byval:
-; CHECK:      get_global $push[[L2:.+]]=, 0{{$}}
+; CHECK:      get_global $push[[L2:.+]]=, __stack_pointer{{$}}
 ; CHECK-NEXT: i32.const $push[[L3:.+]]=, 131072
 ; CHECK-NEXT: i32.sub $push[[L11:.+]]=, $pop[[L2]], $pop[[L3]]
 ; CHECK-NEXT: tee_local $push[[L10:.+]]=, $[[SP:.+]]=, $pop[[L11]]{{$}}
-; CHECK-NEXT: set_global 0, $pop[[L10]]{{$}}
+; CHECK-NEXT: set_global __stack_pointer, $pop[[L10]]{{$}}
 ; CHECK-NEXT: i32.const $push[[L0:.+]]=, 131072
 ; CHECK-NEXT: i32.call       $push[[L11:.+]]=, memcpy@FUNCTION, $[[SP]], ${{.+}}, $pop{{.+}}
 ; CHECK-NEXT: tee_local      $push[[L9:.+]]=, $[[SP:.+]]=, $pop[[L11]]{{$}}
index d1423b5db395b69b14c92277e112621c5ed06c8c..ebda5373c600f13dee4c58c845a319b71f6e5f44 100644 (file)
@@ -448,7 +448,7 @@ bb10:                                             ; preds = %bb9, %bb
 
 ; CHECK-LABEL: stackpointer_dependency:
 ; CHECK:      call {{.+}}, stackpointer_callee@FUNCTION,
-; CHECK-NEXT: set_global 0,
+; CHECK-NEXT: set_global __stack_pointer,
 declare i32 @stackpointer_callee(i8* readnone, i8* readnone)
 declare i8* @llvm.frameaddress(i32)
 define i32 @stackpointer_dependency(i8* readnone) {
index 95aa1f9dbf07658b85d1ba90c82a04e5ea4d1a49..25e9d06db411bed5de67427299845b7831c7c350 100644 (file)
@@ -6,7 +6,7 @@ target triple = "wasm32-unknown-unknown-wasm"
 declare void @somefunc(i32*)
 
 ; CHECK-LABEL: underalign:
-; CHECK:      get_global $push[[L1:.+]]=, 0{{$}}
+; CHECK:      get_global $push[[L1:.+]]=, __stack_pointer{{$}}
 ; CHECK-NEXT: i32.const $push[[L2:.+]]=, 16
 ; CHECK-NEXT: i32.sub   $push[[L10:.+]]=, $pop[[L1]], $pop[[L2]]
 ; CHECK-NEXT: tee_local $push{{.+}}=, [[SP:.+]], $pop[[L10]]
@@ -17,7 +17,7 @@ declare void @somefunc(i32*)
 
 ; CHECK:      get_local $push[[M4:.+]]=, [[SP]]{{$}}
 ; CHECK:      i32.add   $push[[L5:.+]]=, $pop[[M4]], $pop{{.+}}
-; CHECK-NEXT: set_global 0, $pop[[L5]]
+; CHECK-NEXT: set_global __stack_pointer, $pop[[L5]]
 define void @underalign() {
 entry:
   %underaligned = alloca i32, align 8
@@ -26,7 +26,7 @@ entry:
 }
 
 ; CHECK-LABEL: overalign:
-; CHECK:      get_global $push[[L10:.+]]=, 0{{$}}
+; CHECK:      get_global $push[[L10:.+]]=, __stack_pointer{{$}}
 ; CHECK-NEXT: tee_local  $push[[L9:.+]]=, [[BP:.+]], $pop[[L10]]
 ; CHECK-NEXT: i32.const  $push[[L2:.+]]=, 32
 ; CHECK-NEXT: i32.sub    $push[[L8:.+]]=, $pop[[L9]], $pop[[L2]]
@@ -38,7 +38,7 @@ entry:
 ; CHECK:      call       somefunc@FUNCTION, $pop[[M5]]{{$}}
 
 ; CHECK:      get_local  $push[[M6:.+]]=, [[BP]]{{$}}
-; CHECK-NEXT: set_global 0, $pop[[M6]]
+; CHECK-NEXT: set_global __stack_pointer, $pop[[M6]]
 define void @overalign() {
 entry:
   %overaligned = alloca i32, align 32
@@ -47,7 +47,7 @@ entry:
 }
 
 ; CHECK-LABEL: over_and_normal_align:
-; CHECK:      get_global $push[[L14:.+]]=, 0{{$}}
+; CHECK:      get_global $push[[L14:.+]]=, __stack_pointer{{$}}
 ; CHECK-NEXT: tee_local  $push[[L13:.+]]=, [[BP:.+]], $pop[[L14]]
 ; CHECK:      i32.sub    $push[[L12:.+]]=, $pop[[L13]], $pop{{.+}}
 ; CHECK:      i32.and    $push[[L11:.+]]=, $pop[[L12]], $pop{{.+}}
@@ -61,7 +61,7 @@ entry:
 ; CHECK-NEXT: call       somefunc@FUNCTION, $pop[[L8]]
 
 ; CHECK:      get_local  $push[[L6:.+]]=, [[BP]]{{$}}
-; CHECK-NEXT: set_global 0, $pop[[L6]]
+; CHECK-NEXT: set_global __stack_pointer, $pop[[L6]]
 define void @over_and_normal_align() {
 entry:
   %over = alloca i32, align 32
@@ -72,7 +72,7 @@ entry:
 }
 
 ; CHECK-LABEL: dynamic_overalign:
-; CHECK:      get_global $push[[L18:.+]]=, 0{{$}}
+; CHECK:      get_global $push[[L18:.+]]=, __stack_pointer{{$}}
 ; CHECK-NEXT: tee_local  $push[[L17:.+]]=, [[SP:.+]], $pop[[L18]]
 ; CHECK-NEXT: set_local  [[BP:.+]], $pop[[L17]]
 ; CHECK:      tee_local  $push{{.+}}=, [[SP_2:.+]], $pop{{.+}}
@@ -81,7 +81,7 @@ entry:
 ; CHECK:      call       somefunc@FUNCTION, $pop[[M8]]
 
 ; CHECK:      get_local  $push[[M9:.+]]=, [[BP]]{{$}}
-; CHECK-NEXT: set_global 0, $pop[[M9]]
+; CHECK-NEXT: set_global __stack_pointer, $pop[[M9]]
 define void @dynamic_overalign(i32 %num) {
 entry:
   %dynamic = alloca i32, i32 %num, align 32
@@ -90,7 +90,7 @@ entry:
 }
 
 ; CHECK-LABEL: overalign_and_dynamic:
-; CHECK:      get_global $push[[L21:.+]]=, 0{{$}}
+; CHECK:      get_global $push[[L21:.+]]=, __stack_pointer{{$}}
 ; CHECK-NEXT: tee_local  $push[[L20:.+]]=, [[BP:.+]], $pop[[L21]]
 ; CHECK:      i32.sub    $push[[L19:.+]]=, $pop[[L20]], $pop{{.+}}
 ; CHECK:      i32.and    $push[[L18:.+]]=, $pop[[L19]], $pop{{.+}}
@@ -105,7 +105,7 @@ entry:
 ; CHECK-NEXT: call       somefunc@FUNCTION, $pop[[another]]
 
 ; CHECK:      get_local  $push[[M11:.+]]=, [[BP]]{{$}}
-; CHECK-NEXT: set_global 0, $pop[[M11]]
+; CHECK-NEXT: set_global __stack_pointer, $pop[[M11]]
 define void @overalign_and_dynamic(i32 %num) {
 entry:
   %over = alloca i32, align 32
@@ -116,7 +116,7 @@ entry:
 }
 
 ; CHECK-LABEL: overalign_static_and_dynamic:
-; CHECK:      get_global $push[[L26:.+]]=, 0{{$}}
+; CHECK:      get_global $push[[L26:.+]]=, __stack_pointer{{$}}
 ; CHECK-NEXT: tee_local  $push[[L25:.+]]=, [[BP:.+]], $pop[[L26]]
 ; CHECK:      i32.sub    $push[[L24:.+]]=, $pop[[L25]], $pop{{.+}}
 ; CHECK:      i32.and    $push[[L23:.+]]=, $pop[[L24]], $pop{{.+}}
@@ -136,7 +136,7 @@ entry:
 ; CHECK-NEXT: call       somefunc@FUNCTION, $pop[[static]]
 
 ; CHECK:      get_local  $push[[M14:.+]]=, [[BP]]{{$}}
-; CHECK-NEXT: set_global 0, $pop[[M14]]
+; CHECK-NEXT: set_global __stack_pointer, $pop[[M14]]
 define void @overalign_static_and_dynamic(i32 %num) {
 entry:
   %over = alloca i32, align 32
index 57ca75705e5ea73644ccf5f9455c4f809b99080f..c160b391f6e814e7b25e9e9ac29372fccd1cf5f0 100644 (file)
@@ -10,11 +10,11 @@ declare void @ext_func_i32(i32* %ptr)
 ; Check that there is an extra local for the stack pointer.
 ; CHECK: .local i32{{$}}
 define void @alloca32() noredzone {
- ; CHECK-NEXT: get_global $push[[L2:.+]]=, 0{{$}}
+ ; CHECK-NEXT: get_global $push[[L2:.+]]=, __stack_pointer{{$}}
  ; CHECK-NEXT: i32.const $push[[L3:.+]]=, 16
  ; CHECK-NEXT: i32.sub $push[[L9:.+]]=, $pop[[L2]], $pop[[L3]]
  ; CHECK-NEXT: tee_local $push[[L8:.+]]=, [[SP:.+]], $pop[[L9]]{{$}}
- ; CHECK-NEXT: set_global 0, $pop[[L8]]{{$}}
+ ; CHECK-NEXT: set_global __stack_pointer, $pop[[L8]]{{$}}
  %retval = alloca i32
  ; CHECK: get_local $push[[L4:.+]]=, [[SP]]{{$}}
  ; CHECK: i32.const $push[[L0:.+]]=, 0
@@ -23,14 +23,14 @@ define void @alloca32() noredzone {
  ; CHECK: get_local $push[[L6:.+]]=, [[SP]]{{$}}
  ; CHECK-NEXT: i32.const $push[[L5:.+]]=, 16
  ; CHECK-NEXT: i32.add $push[[L7:.+]]=, $pop[[L6]], $pop[[L5]]
- ; CHECK-NEXT: set_global 0, $pop[[L7]]
+ ; CHECK-NEXT: set_global __stack_pointer, $pop[[L7]]
  ret void
 }
 
 ; CHECK-LABEL: alloca3264:
 ; CHECK: .local i32{{$}}
 define void @alloca3264() {
- ; CHECK: get_global $push[[L3:.+]]=, 0{{$}}
+ ; CHECK: get_global $push[[L3:.+]]=, __stack_pointer{{$}}
  ; CHECK-NEXT: i32.const $push[[L4:.+]]=, 16
  ; CHECK-NEXT: i32.sub $push[[L6:.+]]=, $pop[[L3]], $pop[[L4]]
  ; CHECK-NEXT: tee_local $push[[L5:.+]]=, [[SP:.+]], $pop[[L6]]
@@ -50,11 +50,11 @@ define void @alloca3264() {
 ; CHECK-LABEL: allocarray:
 ; CHECK: .local i32{{$}}
 define void @allocarray() {
- ; CHECK-NEXT: get_global $push[[L4:.+]]=, 0{{$}}
+ ; CHECK-NEXT: get_global $push[[L4:.+]]=, __stack_pointer{{$}}
  ; CHECK-NEXT: i32.const $push[[L5:.+]]=, 144{{$}}
  ; CHECK-NEXT: i32.sub $push[[L12:.+]]=, $pop[[L4]], $pop[[L5]]
  ; CHECK-NEXT: tee_local $push[[L11:.+]]=, 0, $pop[[L12]]
- ; CHECK-NEXT: set_global 0, $pop[[L11]]
+ ; CHECK-NEXT: set_global __stack_pointer, $pop[[L11]]
  %r = alloca [33 x i32]
 
  ; CHECK:      i32.const $push{{.+}}=, 24
@@ -72,7 +72,7 @@ define void @allocarray() {
  ; CHECK-NEXT: get_local $push[[L2:.+]]=, [[SP]]{{$}}
  ; CHECK-NEXT: i32.const $push[[L7:.+]]=, 144
  ; CHECK-NEXT: i32.add $push[[L8:.+]]=, $pop[[L2]], $pop[[L7]]
- ; CHECK-NEXT: set_global 0, $pop[[L8]]
+ ; CHECK-NEXT: set_global __stack_pointer, $pop[[L8]]
  ret void
 }
 
@@ -81,7 +81,7 @@ define void @non_mem_use(i8** %addr) {
  ; CHECK: i32.const $push[[L2:.+]]=, 48
  ; CHECK-NEXT: i32.sub $push[[L12:.+]]=, {{.+}}, $pop[[L2]]
  ; CHECK-NEXT: tee_local $push[[L11:.+]]=, [[SP:.+]], $pop[[L12]]
- ; CHECK-NEXT: set_global 0, $pop[[L11]]
+ ; CHECK-NEXT: set_global __stack_pointer, $pop[[L11]]
  %buf = alloca [27 x i8], align 16
  %r = alloca i64
  %r2 = alloca i64
@@ -109,11 +109,11 @@ define void @non_mem_use(i8** %addr) {
 ; CHECK-LABEL: allocarray_inbounds:
 ; CHECK: .local i32{{$}}
 define void @allocarray_inbounds() {
- ; CHECK: get_global $push[[L3:.+]]=, 0{{$}}
+ ; CHECK: get_global $push[[L3:.+]]=, __stack_pointer{{$}}
  ; CHECK-NEXT: i32.const $push[[L4:.+]]=, 32{{$}}
  ; CHECK-NEXT: i32.sub $push[[L11:.+]]=, $pop[[L3]], $pop[[L4]]
  ; CHECK-NEXT: tee_local $push[[L10:.+]]=, [[SP:.+]], $pop[[L11]]
- ; CHECK-NEXT: set_global 0, $pop[[L10]]{{$}}
+ ; CHECK-NEXT: set_global __stack_pointer, $pop[[L10]]{{$}}
  %r = alloca [5 x i32]
  ; CHECK: i32.const $push[[L3:.+]]=, 1
  ; CHECK-DAG: i32.store 24(${{.+}}), $pop[[L3]]
@@ -127,29 +127,29 @@ define void @allocarray_inbounds() {
  ; CHECK: call ext_func
  ; CHECK: i32.const $push[[L5:.+]]=, 32{{$}}
  ; CHECK-NEXT: i32.add $push[[L7:.+]]=, ${{.+}}, $pop[[L5]]
- ; CHECK-NEXT: set_global 0, $pop[[L7]]
+ ; CHECK-NEXT: set_global __stack_pointer, $pop[[L7]]
  ret void
 }
 
 ; CHECK-LABEL: dynamic_alloca:
 define void @dynamic_alloca(i32 %alloc) {
- ; CHECK: get_global $push[[L13:.+]]=, 0{{$}}
+ ; CHECK: get_global $push[[L13:.+]]=, __stack_pointer{{$}}
  ; CHECK-NEXT: tee_local $push[[L12:.+]]=, [[SP:.+]], $pop[[L13]]{{$}}
  ; Target independent codegen bumps the stack pointer.
  ; CHECK: i32.sub
  ; Check that SP is written back to memory after decrement
- ; CHECK: set_global 0
+ ; CHECK: set_global __stack_pointer
  %r = alloca i32, i32 %alloc
  ; Target-independent codegen also calculates the store addr
  ; CHECK: call ext_func_i32@FUNCTION
  call void @ext_func_i32(i32* %r)
- ; CHECK: set_global 0, $pop{{.+}}
+ ; CHECK: set_global __stack_pointer, $pop{{.+}}
  ret void
 }
 
 ; CHECK-LABEL: dynamic_alloca_redzone:
 define void @dynamic_alloca_redzone(i32 %alloc) {
- ; CHECK: get_global $push[[L13:.+]]=, 0{{$}}
+ ; CHECK: get_global $push[[L13:.+]]=, __stack_pointer{{$}}
  ; CHECK-NEXT: tee_local $push[[L12:.+]]=, [[SP:.+]], $pop[[L13]]{{$}}
  ; Target independent codegen bumps the stack pointer
  ; CHECK: i32.sub
@@ -166,11 +166,11 @@ define void @dynamic_alloca_redzone(i32 %alloc) {
 ; CHECK-LABEL: dynamic_static_alloca:
 define void @dynamic_static_alloca(i32 %alloc) noredzone {
  ; Decrement SP in the prolog by the static amount and writeback to memory.
- ; CHECK: get_global $push[[L11:.+]]=, 0{{$}}
+ ; CHECK: get_global $push[[L11:.+]]=, __stack_pointer{{$}}
  ; CHECK-NEXT: i32.const $push[[L12:.+]]=, 16
  ; CHECK-NEXT: i32.sub $push[[L23:.+]]=, $pop[[L11]], $pop[[L12]]
  ; CHECK-NEXT: tee_local $push[[L22:.+]]=, [[SP:.+]], $pop[[L23]]
- ; CHECK-NEXT: set_global 0, $pop[[L22]]
+ ; CHECK-NEXT: set_global __stack_pointer, $pop[[L22]]
 
  ; Alloc and write to a static alloca
  ; CHECK: get_local $push[[L21:.+]]=, [[SP:.+]]
@@ -184,7 +184,7 @@ define void @dynamic_static_alloca(i32 %alloc) noredzone {
  ; CHECK: i32.sub
  ; CHECK: tee_local $push[[L16:.+]]=, [[dynamic_local:.+]], $pop{{.+}}
  ; CHECK: tee_local $push[[L15:.+]]=, [[other:.+]], $pop[[L16]]{{$}}
- ; CHECK: set_global 0, $pop[[L15]]{{$}}
+ ; CHECK: set_global __stack_pointer, $pop[[L15]]{{$}}
  %dynamic = alloca i32, i32 %alloc
 
  ; Ensure we don't modify the frame pointer after assigning it.
@@ -226,7 +226,7 @@ define void @dynamic_static_alloca(i32 %alloc) noredzone {
  ; CHECK: get_local $push[[L24:.+]]=, [[FP]]{{$}}
  ; CHECK: i32.const $push[[L18:.+]]=, 16
  ; CHECK-NEXT: i32.add $push[[L19:.+]]=, $pop[[L24]], $pop[[L18]]
- ; CHECK-NEXT: set_global 0, $pop[[L19]]
+ ; CHECK-NEXT: set_global __stack_pointer, $pop[[L19]]
  ret void
 }
 
@@ -235,7 +235,7 @@ declare void @llvm.stackrestore(i8*)
 
 ; CHECK-LABEL: llvm_stack_builtins:
 define void @llvm_stack_builtins(i32 %alloc) noredzone {
- ; CHECK: get_global $push[[L11:.+]]=, 0{{$}}
+ ; CHECK: get_global $push[[L11:.+]]=, __stack_pointer{{$}}
  ; CHECK-NEXT: tee_local $push[[L10:.+]]=, {{.+}}, $pop[[L11]]
  ; CHECK-NEXT: set_local [[STACK:.+]], $pop[[L10]]
  %stack = call i8* @llvm.stacksave()
@@ -245,7 +245,7 @@ define void @llvm_stack_builtins(i32 %alloc) noredzone {
  %dynamic = alloca i32, i32 %alloc
 
  ; CHECK: get_local $push[[L12:.+]]=, [[STACK]]
- ; CHECK-NEXT: set_global 0, $pop[[L12]]
+ ; CHECK-NEXT: set_global __stack_pointer, $pop[[L12]]
  call void @llvm.stackrestore(i8* %stack)
 
  ret void
@@ -256,7 +256,7 @@ define void @llvm_stack_builtins(i32 %alloc) noredzone {
 ; moved after the stack pointer was updated for the dynamic alloca.
 ; CHECK-LABEL: dynamic_alloca_nouse:
 define void @dynamic_alloca_nouse(i32 %alloc) noredzone {
- ; CHECK: get_global $push[[L11:.+]]=, 0{{$}}
+ ; CHECK: get_global $push[[L11:.+]]=, __stack_pointer{{$}}
  ; CHECK-NEXT: tee_local $push[[L10:.+]]=, {{.+}}, $pop[[L11]]
  ; CHECK-NEXT: set_local [[FP:.+]], $pop[[L10]]
  %dynamic = alloca i32, i32 %alloc
@@ -264,7 +264,7 @@ define void @dynamic_alloca_nouse(i32 %alloc) noredzone {
  ; CHECK-NOT: set_local [[FP]],
 
  ; CHECK: get_local $push[[L12:.+]]=, [[FP]]
- ; CHECK-NEXT: set_global 0, $pop[[L12]]
+ ; CHECK-NEXT: set_global __stack_pointer, $pop[[L12]]
  ret void
 }
 
@@ -295,11 +295,11 @@ declare i8* @llvm.frameaddress(i32)
 
 ; Test __builtin_frame_address(0).
 ; CHECK-LABEL: frameaddress_0:
-; CHECK: get_global $push[[L3:.+]]=, 0{{$}}
+; CHECK: get_global $push[[L3:.+]]=, __stack_pointer{{$}}
 ; CHECK-NEXT: tee_local $push[[L2:.+]]=, [[FP:.+]], $pop[[L3]]{{$}}
 ; CHECK-NEXT: call use_i8_star@FUNCTION, $pop[[L2]]
 ; CHECK-NEXT: get_local $push[[L5:.+]]=, [[FP]]
-; CHECK-NEXT: set_global 0, $pop[[L5]]
+; CHECK-NEXT: set_global __stack_pointer, $pop[[L5]]
 define void @frameaddress_0() {
   %t = call i8* @llvm.frameaddress(i32 0)
   call void @use_i8_star(i8* %t)
@@ -320,7 +320,7 @@ define void @frameaddress_1() {
 
 ; Test a stack address passed to an inline asm.
 ; CHECK-LABEL: inline_asm:
-; CHECK:       get_global {{.+}}, 0{{$}}
+; CHECK:       get_global {{.+}}, __stack_pointer{{$}}
 ; CHECK:       #APP
 ; CHECK-NEXT:  # %{{[0-9]+}}{{$}}
 ; CHECK-NEXT:  #NO_APP
index c0a9d9a801fff5a3d57147c9c06f208fd9777469..15c09e0ebd80444b15bb83cbf045d353bdb25a8a 100644 (file)
@@ -25,11 +25,10 @@ entry:
 ; CHECK:   Section {
 ; CHECK:     Type: ELEM (0x9)
 ; CHECK:     Size: 7
-; CHECK:     Offset: 165
 ; CHECK:   }
 
 ; CHECK: Relocations [
-; CHECK:   Section (9) CODE {
+; CHECK:   Section (8) CODE {
 ; CHECK:     Relocation {
 ; CHECK:       Type: R_WEBASSEMBLY_FUNCTION_INDEX_LEB (0)
 ; CHECK:       Offset: 0x4
@@ -42,7 +41,7 @@ entry:
 ; CHECK:     }
 ; CHECK:     Relocation {
 ; CHECK:       Type: R_WEBASSEMBLY_TABLE_INDEX_SLEB (1)
-; CHECK:       Offset: 0x1A
+; CHECK:       Offset: 0x1E
 ; CHECK:       Index: 0x0
 ; CHECK:     }
 ; CHECK:   }
diff --git a/test/MC/WebAssembly/stack-ptr.ll b/test/MC/WebAssembly/stack-ptr.ll
new file mode 100644 (file)
index 0000000..98d1311
--- /dev/null
@@ -0,0 +1,21 @@
+; RUN: llc -mtriple wasm32-unknown-unknown-wasm -filetype=obj %s -o - | obj2yaml | FileCheck %s
+
+; Function that uses explict stack, and should generate a reference to
+; __stack_pointer, along with the corresponding reloction entry.
+define hidden void @foo() #0 {
+entry:
+  alloca i32, align 4
+  ret void
+}
+
+; CHECK:  - Type:            IMPORT
+; CHECK:     Imports:
+; CHECK:       - Module:          env
+; CHECK:         Field:           __stack_pointer
+; CHECK:         Kind:            GLOBAL
+; CHECK:         GlobalType:      I32
+; CHECK:         GlobalMutable:   false
+; CHECK:   - Type:            CODE
+; CHECK:     Relocations:
+; CHECK:       - Type:            R_WEBASSEMBLY_GLOBAL_INDEX_LEB
+; CHECK:         Index:           0