[WebAssembly] Use uint8_t for single byte values to match the spec
The original BinaryEncoding.md document used to specify that
these values were `varint7`, but the official spec lists them
explicitly as single byte values and not LEB.
A similar change for wabt is in flight:
https://github.com/WebAssembly/wabt/pull/782
Differential Revision: https://reviews.llvm.org/D43921
llvm-svn: 326454
diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp
index 93bab05..3c86ca4 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -258,7 +258,7 @@
}
void writeValueType(wasm::ValType Ty) {
- encodeSLEB128(int32_t(Ty), getStream());
+ write8(static_cast<uint8_t>(Ty));
}
void writeTypeSection(ArrayRef<WasmFunctionType> FunctionTypes);
@@ -300,7 +300,7 @@
"Only custom sections can have names");
DEBUG(dbgs() << "startSection " << SectionId << ": " << Name << "\n");
- encodeULEB128(SectionId, getStream());
+ write8(SectionId);
Section.SizeOffset = getStream().tell();
@@ -625,7 +625,7 @@
RelEntry.FixupSection->getSectionOffset();
uint32_t Index = getRelocationIndexValue(RelEntry);
- encodeULEB128(RelEntry.Type, Stream);
+ write8(RelEntry.Type);
encodeULEB128(Offset, Stream);
encodeULEB128(Index, Stream);
if (RelEntry.hasAddend())
@@ -644,7 +644,7 @@
encodeULEB128(FunctionTypes.size(), getStream());
for (const WasmFunctionType &FuncTy : FunctionTypes) {
- encodeSLEB128(wasm::WASM_TYPE_FUNC, getStream());
+ write8(wasm::WASM_TYPE_FUNC);
encodeULEB128(FuncTy.Params.size(), getStream());
for (wasm::ValType Ty : FuncTy.Params)
writeValueType(Ty);
@@ -671,22 +671,22 @@
for (const wasm::WasmImport &Import : Imports) {
writeString(Import.Module);
writeString(Import.Field);
- encodeULEB128(Import.Kind, getStream());
+ write8(Import.Kind);
switch (Import.Kind) {
case wasm::WASM_EXTERNAL_FUNCTION:
encodeULEB128(Import.SigIndex, getStream());
break;
case wasm::WASM_EXTERNAL_GLOBAL:
- encodeSLEB128(Import.Global.Type, getStream());
- encodeULEB128(uint32_t(Import.Global.Mutable), getStream());
+ write8(Import.Global.Type);
+ write8(Import.Global.Mutable ? 1 : 0);
break;
case wasm::WASM_EXTERNAL_MEMORY:
encodeULEB128(0, getStream()); // flags
encodeULEB128(NumPages, getStream()); // initial
break;
case wasm::WASM_EXTERNAL_TABLE:
- encodeSLEB128(Import.Table.ElemType, getStream());
+ write8(Import.Table.ElemType);
encodeULEB128(0, getStream()); // flags
encodeULEB128(NumElements, getStream()); // initial
break;
@@ -742,7 +742,7 @@
encodeULEB128(Exports.size(), getStream());
for (const wasm::WasmExport &Export : Exports) {
writeString(Export.Name);
- encodeSLEB128(Export.Kind, getStream());
+ write8(Export.Kind);
encodeULEB128(Export.Index, getStream());
}
diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp
index f32f57b..b47d6bb 100644
--- a/llvm/lib/Object/WasmObjectFile.cpp
+++ b/llvm/lib/Object/WasmObjectFile.cpp
@@ -99,18 +99,6 @@
return result;
}
-static int8_t readVarint7(const uint8_t *&Ptr) {
- int64_t result = readLEB128(Ptr);
- assert(result <= VARINT7_MAX && result >= VARINT7_MIN);
- return result;
-}
-
-static uint8_t readVaruint7(const uint8_t *&Ptr) {
- uint64_t result = readULEB128(Ptr);
- assert(result <= VARUINT7_MAX);
- return result;
-}
-
static int32_t readVarint32(const uint8_t *&Ptr) {
int64_t result = readLEB128(Ptr);
assert(result <= INT32_MAX && result >= INT32_MIN);
@@ -174,7 +162,7 @@
static wasm::WasmTable readTable(const uint8_t *&Ptr) {
wasm::WasmTable Table;
- Table.ElemType = readVarint7(Ptr);
+ Table.ElemType = readUint8(Ptr);
Table.Limits = readLimits(Ptr);
return Table;
}
@@ -182,7 +170,7 @@
static Error readSection(WasmSection &Section, const uint8_t *&Ptr,
const uint8_t *Start, const uint8_t *Eof) {
Section.Offset = Ptr - Start;
- Section.Type = readVaruint7(Ptr);
+ Section.Type = readUint8(Ptr);
uint32_t Size = readVaruint32(Ptr);
if (Size == 0)
return make_error<StringError>("Zero length section",
@@ -274,7 +262,7 @@
}
while (Ptr < End) {
- uint8_t Type = readVarint7(Ptr);
+ uint8_t Type = readUint8(Ptr);
uint32_t Size = readVaruint32(Ptr);
const uint8_t *SubSectionEnd = Ptr + Size;
switch (Type) {
@@ -324,7 +312,7 @@
}
while (Ptr < End) {
- uint8_t Type = readVarint7(Ptr);
+ uint8_t Type = readUint8(Ptr);
uint32_t Size = readVaruint32(Ptr);
const uint8_t *SubSectionEnd = Ptr + Size;
switch (Type) {
@@ -548,7 +536,7 @@
Error WasmObjectFile::parseRelocSection(StringRef Name, const uint8_t *Ptr,
const uint8_t *End) {
- uint8_t SectionCode = readVarint7(Ptr);
+ uint8_t SectionCode = readUint8(Ptr);
WasmSection* Section = nullptr;
if (SectionCode == wasm::WASM_SEC_CUSTOM) {
StringRef Name = readString(Ptr);
@@ -613,7 +601,7 @@
while (Count--) {
wasm::WasmSignature Sig;
Sig.ReturnType = wasm::WASM_TYPE_NORESULT;
- int8_t Form = readVarint7(Ptr);
+ uint8_t Form = readUint8(Ptr);
if (Form != wasm::WASM_TYPE_FUNC) {
return make_error<GenericBinaryError>("Invalid signature type",
object_error::parse_failed);
@@ -621,7 +609,7 @@
uint32_t ParamCount = readVaruint32(Ptr);
Sig.ParamTypes.reserve(ParamCount);
while (ParamCount--) {
- uint32_t ParamType = readVarint7(Ptr);
+ uint32_t ParamType = readUint8(Ptr);
Sig.ParamTypes.push_back(ParamType);
}
uint32_t ReturnCount = readVaruint32(Ptr);
@@ -630,7 +618,7 @@
return make_error<GenericBinaryError>(
"Multiple return types not supported", object_error::parse_failed);
}
- Sig.ReturnType = readVarint7(Ptr);
+ Sig.ReturnType = readUint8(Ptr);
}
Signatures.push_back(Sig);
}
@@ -655,7 +643,7 @@
break;
case wasm::WASM_EXTERNAL_GLOBAL:
NumImportedGlobals++;
- Im.Global.Type = readVarint7(Ptr);
+ Im.Global.Type = readUint8(Ptr);
Im.Global.Mutable = readVaruint1(Ptr);
break;
case wasm::WASM_EXTERNAL_MEMORY:
@@ -726,7 +714,7 @@
while (Count--) {
wasm::WasmGlobal Global;
Global.Index = NumImportedGlobals + Globals.size();
- Global.Type.Type = readVarint7(Ptr);
+ Global.Type.Type = readUint8(Ptr);
Global.Type.Mutable = readVaruint1(Ptr);
if (Error Err = readInitExpr(Global.InitExpr, Ptr))
return Err;
@@ -834,7 +822,7 @@
while (NumLocalDecls--) {
wasm::WasmLocalDecl Decl;
Decl.Count = readVaruint32(Ptr);
- Decl.Type = readVarint7(Ptr);
+ Decl.Type = readUint8(Ptr);
Function.Locals.push_back(Decl);
}
diff --git a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
index 9d4f308..cab14e9b 100644
--- a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
+++ b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
@@ -31,7 +31,7 @@
: MCTargetStreamer(S) {}
void WebAssemblyTargetStreamer::emitValueType(wasm::ValType Type) {
- Streamer.EmitSLEB128IntValue(int32_t(Type));
+ Streamer.EmitIntValue(uint8_t(Type), 1);
}
WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer(
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
index c8afa39..ac2258a 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
@@ -95,10 +95,10 @@
// here; this method is precisely there for fetching the signatures of known
// Clang-provided symbols.
if (strcmp(Name, "__stack_pointer") == 0) {
- wasm::ValType iPTR =
- Subtarget.hasAddr64() ? wasm::ValType::I64 : wasm::ValType::I32;
WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
- WasmSym->setGlobalType(wasm::WasmGlobalType{int32_t(iPTR), true});
+ WasmSym->setGlobalType(wasm::WasmGlobalType{
+ Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64 : wasm::WASM_TYPE_I32,
+ true});
return WasmSym;
}