[WebAssembly] Use LEB encoding for value types
Previously we were using the encoded LEB hex values
for the value types. This change uses the decoded
negative value and the LEB encoder to write them out.
Differential Revision: https://reviews.llvm.org/D30847
Patch by Sam Clegg
llvm-svn: 297777
diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp
index e61b125..20ace61 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -109,6 +109,10 @@
void writeHeader(const MCAssembler &Asm);
+ void writeValueType(wasm::ValType Ty) {
+ encodeSLEB128(int32_t(Ty), getStream());
+ }
+
void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFixup &Fixup,
MCValue Target, bool &IsPCRel,
@@ -140,7 +144,7 @@
assert((Name != nullptr) == (SectionId == wasm::WASM_SEC_CUSTOM) &&
"Only custom sections can have names");
- write8(SectionId);
+ encodeULEB128(SectionId, getStream());
Section.SizeOffset = getStream().tell();
@@ -290,10 +294,10 @@
enum { Plain, Empty, Tombstone } State;
// The return types of the function.
- SmallVector<unsigned, 1> Returns;
+ SmallVector<wasm::ValType, 1> Returns;
// The parameter types of the function.
- SmallVector<unsigned, 4> Params;
+ SmallVector<wasm::ValType, 4> Params;
WasmFunctionType() : State(Plain) {}
@@ -317,10 +321,10 @@
}
static unsigned getHashValue(const WasmFunctionType &FuncTy) {
uintptr_t Value = FuncTy.State;
- for (unsigned Ret : FuncTy.Returns)
- Value += DenseMapInfo<unsigned>::getHashValue(Ret);
- for (unsigned Param : FuncTy.Params)
- Value += DenseMapInfo<unsigned>::getHashValue(Param);
+ for (wasm::ValType Ret : FuncTy.Returns)
+ Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Ret));
+ for (wasm::ValType Param : FuncTy.Params)
+ Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Param));
return Value;
}
static bool isEqual(const WasmFunctionType &LHS,
@@ -756,13 +760,13 @@
encodeULEB128(FunctionTypes.size(), getStream());
for (WasmFunctionType &FuncTy : FunctionTypes) {
- write8(wasm::WASM_TYPE_FUNC);
+ encodeSLEB128(wasm::WASM_TYPE_FUNC, getStream());
encodeULEB128(FuncTy.Params.size(), getStream());
- for (unsigned Ty : FuncTy.Params)
- write8(Ty);
+ for (wasm::ValType Ty : FuncTy.Params)
+ writeValueType(Ty);
encodeULEB128(FuncTy.Returns.size(), getStream());
- for (unsigned Ty : FuncTy.Returns)
- write8(Ty);
+ for (wasm::ValType Ty : FuncTy.Returns)
+ writeValueType(Ty);
}
endSection(Section);
@@ -782,15 +786,15 @@
encodeULEB128(FieldName.size(), getStream());
writeBytes(FieldName);
- write8(Import.Kind);
+ encodeULEB128(Import.Kind, getStream());
switch (Import.Kind) {
case wasm::WASM_EXTERNAL_FUNCTION:
encodeULEB128(Import.Type, getStream());
break;
case wasm::WASM_EXTERNAL_GLOBAL:
- write8(Import.Type);
- write8(0); // mutability
+ encodeSLEB128(Import.Type, getStream());
+ encodeULEB128(0, getStream()); // mutability
break;
default:
llvm_unreachable("unsupported import kind");
@@ -820,7 +824,7 @@
// The number of tables, fixed to 1 for now.
encodeULEB128(1, getStream());
- write8(wasm::WASM_TYPE_ANYFUNC);
+ encodeSLEB128(wasm::WASM_TYPE_ANYFUNC, getStream());
encodeULEB128(0, getStream()); // flags
encodeULEB128(TableElems.size(), getStream()); // initial
@@ -846,7 +850,7 @@
encodeULEB128(Globals.size(), getStream());
for (const WasmGlobal &Global : Globals) {
- write8(Global.Type);
+ encodeSLEB128(Global.Type, getStream());
write8(Global.IsMutable);
write8(wasm::WASM_OPCODE_I32_CONST);
@@ -866,7 +870,7 @@
encodeULEB128(Export.FieldName.size(), getStream());
writeBytes(Export.FieldName);
- write8(Export.Kind);
+ encodeSLEB128(Export.Kind, getStream());
encodeULEB128(Export.Index, getStream());
}
@@ -1007,7 +1011,7 @@
if (!CodeRelocations.empty()) {
startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.CODE");
- write8(wasm::WASM_SEC_CODE);
+ encodeULEB128(wasm::WASM_SEC_CODE, getStream());
encodeULEB128(CodeRelocations.size(), getStream());
@@ -1020,7 +1024,7 @@
if (!DataRelocations.empty()) {
startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.DATA");
- write8(wasm::WASM_SEC_DATA);
+ encodeULEB128(wasm::WASM_SEC_DATA, getStream());
encodeULEB128(DataRelocations.size(), getStream());