[PATCH] D87515: [WebAssembly] Add assembly syntax for mutable globals

Sam Clegg via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 11 07:37:28 PDT 2020


sbc100 created this revision.
Herald added subscribers: llvm-commits, ecnelises, sunfish, hiraditya, jgravelle-google, dschuff.
Herald added a project: LLVM.
sbc100 requested review of this revision.
Herald added a subscriber: aheejin.

This adds and optional ", immutable" to the end of a `.globaltype`
declaration.  I would have prefered to match the `.wat` syntax
where immutable is the default and `mut` is the signifier for
mutable globals.  Sadly changing the default would break backwards
compat with existing assembly in the wild so I think its best
to stick with this approach.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87515

Files:
  llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
  llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
  llvm/test/MC/WebAssembly/globals.s


Index: llvm/test/MC/WebAssembly/globals.s
===================================================================
--- llvm/test/MC/WebAssembly/globals.s
+++ llvm/test/MC/WebAssembly/globals.s
@@ -6,7 +6,7 @@
 .globl read_global
 .globl write_global
 .globaltype foo_global, i32
-.globaltype global2, i64
+.globaltype global2, i64, immutable
 .globaltype global3, f32
 .globaltype global4, f64
 
@@ -42,6 +42,12 @@
 # BIN-NEXT:       InitExpr:
 # BIN-NEXT:         Opcode:          I32_CONST
 # BIN-NEXT:         Value:           0
+# BIN-NEXT:     - Index:           1
+# BIN-NEXT:       Type:            I64
+# BIN-NEXT:       Mutable:         false
+# BIN-NEXT:       InitExpr:
+# BIN-NEXT:         Opcode:          I64_CONST
+# BIN-NEXT:         Value:           0
 
 #      BIN:  - Type:            CUSTOM
 # BIN-NEXT:    Name:            linking
Index: llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
===================================================================
--- llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
+++ llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
@@ -71,8 +71,10 @@
   assert(Sym->isGlobal());
   OS << "\t.globaltype\t" << Sym->getName() << ", "
      << WebAssembly::typeToString(
-            static_cast<wasm::ValType>(Sym->getGlobalType().Type))
-     << '\n';
+            static_cast<wasm::ValType>(Sym->getGlobalType().Type));
+  if (!Sym->getGlobalType().Mutable)
+    OS << ", immutable";
+  OS << '\n';
 }
 
 void WebAssemblyTargetAsmStreamer::emitEventType(const MCSymbolWasm *Sym) {
Index: llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
===================================================================
--- llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
+++ llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
@@ -689,11 +689,24 @@
       auto Type = parseType(TypeName);
       if (!Type)
         return error("Unknown type in .globaltype directive: ", TypeTok);
+      // Optional mutable modifier. Default to mutable for historical reasons.
+      // Ideally we would have gone with immutable as the default and used `mut`
+      // as the modifier to match the `.wat` format.
+      bool Mutable = true;
+      if (isNext(AsmToken::Comma)) {
+        TypeTok = Lexer.getTok();
+        auto Id = expectIdent();
+        if (Id == "immutable")
+          Mutable = false;
+        else
+          // Should we also allow `mutable` and `mut` here for clarity?
+          return error("Unknown type in .globaltype modifier: ", TypeTok);
+      }
       // Now set this symbol with the correct type.
       auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName));
       WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
       WasmSym->setGlobalType(
-          wasm::WasmGlobalType{uint8_t(Type.getValue()), true});
+          wasm::WasmGlobalType{uint8_t(Type.getValue()), Mutable});
       // And emit the directive again.
       TOut.emitGlobalType(WasmSym);
       return expect(AsmToken::EndOfStatement, "EOL");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87515.291216.patch
Type: text/x-patch
Size: 3083 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200911/ef374a9b/attachment.bin>


More information about the llvm-commits mailing list