[llvm] r297991 - [WebAssembly] Fix some broken type encodings in wasm binary

Derek Schuff via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 16 13:49:49 PDT 2017


Author: dschuff
Date: Thu Mar 16 15:49:48 2017
New Revision: 297991

URL: http://llvm.org/viewvc/llvm-project?rev=297991&view=rev
Log:
[WebAssembly] Fix some broken type encodings in wasm binary

A recent change switch the in-memory wasm value types
to be signed integers, but I missing a few cases where
these were being writing to the binary.

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

Patch by Sam Clegg

Modified:
    llvm/trunk/lib/MC/WasmObjectWriter.cpp
    llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
    llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h

Modified: llvm/trunk/lib/MC/WasmObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/WasmObjectWriter.cpp?rev=297991&r1=297990&r2=297991&view=diff
==============================================================================
--- llvm/trunk/lib/MC/WasmObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/WasmObjectWriter.cpp Thu Mar 16 15:49:48 2017
@@ -343,7 +343,7 @@ struct WasmImport {
 
 // A wasm function to be written into the function section.
 struct WasmFunction {
-  unsigned Type;
+  int32_t Type;
   const MCSymbolWasm *Sym;
 };
 
@@ -356,7 +356,7 @@ struct WasmExport {
 
 // A wasm global to be written into the global section.
 struct WasmGlobal {
-  unsigned Type;
+  wasm::ValType Type;
   bool IsMutable;
   uint32_t InitialValue;
 };
@@ -510,10 +510,10 @@ static void WriteRelocations(
 void WasmObjectWriter::writeObject(MCAssembler &Asm,
                                    const MCAsmLayout &Layout) {
   MCContext &Ctx = Asm.getContext();
-  unsigned PtrType = is64Bit() ? wasm::WASM_TYPE_I64 : wasm::WASM_TYPE_I32;
+  wasm::ValType PtrType = is64Bit() ? wasm::ValType::I64 : wasm::ValType::I32;
 
   // Collect information from the available symbols.
-  DenseMap<WasmFunctionType, unsigned, WasmFunctionTypeDenseMapInfo>
+  DenseMap<WasmFunctionType, int32_t, WasmFunctionTypeDenseMapInfo>
       FunctionTypeIndices;
   SmallVector<WasmFunctionType, 4> FunctionTypes;
   SmallVector<WasmFunction, 4> Functions;
@@ -552,7 +552,7 @@ void WasmObjectWriter::writeObject(MCAss
   // Populate the Imports set.
   for (const MCSymbol &S : Asm.symbols()) {
     const auto &WS = static_cast<const MCSymbolWasm &>(S);
-    unsigned Type;
+    int32_t Type;
 
     if (WS.isFunction()) {
       // Prepare the function's type, if we haven't seen it yet.
@@ -566,7 +566,7 @@ void WasmObjectWriter::writeObject(MCAss
 
       Type = Pair.first->second;
     } else {
-      Type = PtrType;
+      Type = int32_t(PtrType);
     }
 
     // If the symbol is not defined in this translation unit, import it.
@@ -607,7 +607,7 @@ void WasmObjectWriter::writeObject(MCAss
     const SmallVectorImpl<char> &Contents = DataFrag.getContents();
     for (char p : Contents) {
       WasmGlobal G;
-      G.Type = uint8_t(p);
+      G.Type = wasm::ValType(p);
       G.IsMutable = true;
       G.InitialValue = 0;
       Globals.push_back(G);
@@ -632,7 +632,7 @@ void WasmObjectWriter::writeObject(MCAss
       if (Pair.second)
         FunctionTypes.push_back(F);
 
-      unsigned Type = Pair.first->second;
+      int32_t Type = Pair.first->second;
 
       if (WS.isDefined(/*SetUsed=*/false)) {
         // A definition. Take the next available index.
@@ -850,7 +850,7 @@ void WasmObjectWriter::writeObject(MCAss
 
     encodeULEB128(Globals.size(), getStream());
     for (const WasmGlobal &Global : Globals) {
-      encodeSLEB128(Global.Type, getStream());
+      writeValueType(Global.Type);
       write8(Global.IsMutable);
 
       write8(wasm::WASM_OPCODE_I32_CONST);

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=297991&r1=297990&r2=297991&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp (original)
+++ llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp Thu Mar 16 15:49:48 2017
@@ -31,6 +31,10 @@ using namespace llvm;
 WebAssemblyTargetStreamer::WebAssemblyTargetStreamer(MCStreamer &S)
     : MCTargetStreamer(S) {}
 
+void WebAssemblyTargetStreamer::emitValueType(wasm::ValType Type) {
+  Streamer.EmitSLEB128IntValue(int32_t(Type));
+}
+
 WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer(
     MCStreamer &S, formatted_raw_ostream &OS)
     : WebAssemblyTargetStreamer(S), OS(OS) {}
@@ -128,7 +132,7 @@ void WebAssemblyTargetELFStreamer::emitR
 void WebAssemblyTargetELFStreamer::emitLocal(ArrayRef<MVT> Types) {
   Streamer.EmitULEB128IntValue(Types.size());
   for (MVT Type : Types)
-    Streamer.EmitIntValue(int64_t(WebAssembly::toValType(Type)), 1);
+    emitValueType(WebAssembly::toValType(Type));
 }
 
 void WebAssemblyTargetELFStreamer::emitGlobal(ArrayRef<MVT> Types) {
@@ -182,7 +186,7 @@ void WebAssemblyTargetWasmStreamer::emit
   Streamer.EmitULEB128IntValue(Grouped.size());
   for (auto Pair : Grouped) {
     Streamer.EmitULEB128IntValue(Pair.second);
-    Streamer.EmitULEB128IntValue(uint64_t(WebAssembly::toValType(Pair.first)));
+    emitValueType(WebAssembly::toValType(Pair.first));
   }
 }
 
@@ -194,7 +198,7 @@ void WebAssemblyTargetWasmStreamer::emit
   Streamer.SwitchSection(Streamer.getContext()
                                  .getWasmSection(".global_variables", 0, 0));
   for (MVT Ty : Types)
-    Streamer.EmitIntValue(uint64_t(WebAssembly::toValType(Ty)), 1);
+    Streamer.EmitIntValue(int64_t(WebAssembly::toValType(Ty)), 1);
   Streamer.PopSection();
 }
 

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=297991&r1=297990&r2=297991&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h (original)
+++ llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h Thu Mar 16 15:49:48 2017
@@ -18,6 +18,7 @@
 
 #include "llvm/CodeGen/MachineValueType.h"
 #include "llvm/MC/MCStreamer.h"
+#include "llvm/Support/Wasm.h"
 
 namespace llvm {
 
@@ -50,6 +51,9 @@ public:
   virtual void emitIndIdx(const MCExpr *Value) = 0;
   /// .import_global
   virtual void emitGlobalImport(StringRef name) = 0;
+
+protected:
+  void emitValueType(wasm::ValType Type);
 };
 
 /// This part is for ascii assembly output




More information about the llvm-commits mailing list