[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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326454 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Sam Clegg 2018-03-01 18:06:21 +00:00
parent 92024e8c0f
commit 9f474dee9e
7 changed files with 75 additions and 87 deletions

View File

@ -33,24 +33,24 @@ struct WasmObjectHeader {
};
struct WasmSignature {
std::vector<int32_t> ParamTypes;
int32_t ReturnType;
std::vector<uint8_t> ParamTypes;
uint8_t ReturnType;
};
struct WasmExport {
StringRef Name;
uint32_t Kind;
uint8_t Kind;
uint32_t Index;
};
struct WasmLimits {
uint32_t Flags;
uint8_t Flags;
uint32_t Initial;
uint32_t Maximum;
};
struct WasmTable {
int32_t ElemType;
uint8_t ElemType;
WasmLimits Limits;
};
@ -66,7 +66,7 @@ struct WasmInitExpr {
};
struct WasmGlobalType {
int32_t Type;
uint8_t Type;
bool Mutable;
};
@ -79,7 +79,7 @@ struct WasmGlobal {
struct WasmImport {
StringRef Module;
StringRef Field;
uint32_t Kind;
uint8_t Kind;
union {
uint32_t SigIndex;
WasmGlobalType Global;
@ -89,7 +89,7 @@ struct WasmImport {
};
struct WasmLocalDecl {
int32_t Type;
uint8_t Type;
uint32_t Count;
};
@ -128,7 +128,7 @@ struct WasmDataReference {
};
struct WasmRelocation {
uint32_t Type; // The type of the relocation.
uint8_t Type; // The type of the relocation.
uint32_t Index; // Index into either symbol or type index space.
uint64_t Offset; // Offset from the start of the section.
int64_t Addend; // A value to add to the symbol.
@ -141,7 +141,7 @@ struct WasmInitFunc {
struct WasmSymbolInfo {
StringRef Name;
uint32_t Kind;
uint8_t Kind;
uint32_t Flags;
union {
// For function or global symbols, the index in function of global index
@ -178,18 +178,18 @@ enum : unsigned {
};
// Type immediate encodings used in various contexts.
enum {
WASM_TYPE_I32 = -0x01,
WASM_TYPE_I64 = -0x02,
WASM_TYPE_F32 = -0x03,
WASM_TYPE_F64 = -0x04,
WASM_TYPE_ANYFUNC = -0x10,
WASM_TYPE_FUNC = -0x20,
WASM_TYPE_NORESULT = -0x40, // for blocks with no result values
enum : uint8_t {
WASM_TYPE_I32 = 0x7F,
WASM_TYPE_I64 = 0x7E,
WASM_TYPE_F32 = 0x7D,
WASM_TYPE_F64 = 0x7C,
WASM_TYPE_ANYFUNC = 0x70,
WASM_TYPE_FUNC = 0x60,
WASM_TYPE_NORESULT = 0x40, // for blocks with no result values
};
// Kinds of externals (for imports and exports).
enum : unsigned {
enum : uint8_t {
WASM_EXTERNAL_FUNCTION = 0x0,
WASM_EXTERNAL_TABLE = 0x1,
WASM_EXTERNAL_MEMORY = 0x2,
@ -239,7 +239,7 @@ enum : unsigned {
};
// Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE
enum WasmSymbolType : unsigned {
enum WasmSymbolType : uint8_t {
WASM_SYMBOL_TYPE_FUNCTION = 0x0,
WASM_SYMBOL_TYPE_DATA = 0x1,
WASM_SYMBOL_TYPE_GLOBAL = 0x2,
@ -257,7 +257,7 @@ const unsigned WASM_SYMBOL_UNDEFINED = 0x10;
#define WASM_RELOC(name, value) name = value,
enum : unsigned {
enum : uint8_t {
#include "WasmRelocs.def"
};

View File

@ -28,17 +28,17 @@ namespace llvm {
namespace WasmYAML {
LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionType)
LLVM_YAML_STRONG_TYPEDEF(int32_t, ValueType)
LLVM_YAML_STRONG_TYPEDEF(int32_t, TableType)
LLVM_YAML_STRONG_TYPEDEF(int32_t, SignatureForm)
LLVM_YAML_STRONG_TYPEDEF(uint32_t, ExportKind)
LLVM_YAML_STRONG_TYPEDEF(uint8_t, ValueType)
LLVM_YAML_STRONG_TYPEDEF(uint8_t, TableType)
LLVM_YAML_STRONG_TYPEDEF(uint8_t, SignatureForm)
LLVM_YAML_STRONG_TYPEDEF(uint8_t, ExportKind)
LLVM_YAML_STRONG_TYPEDEF(uint32_t, Opcode)
LLVM_YAML_STRONG_TYPEDEF(uint32_t, RelocType)
LLVM_YAML_STRONG_TYPEDEF(uint8_t, RelocType)
LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolFlags)
LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolKind)
LLVM_YAML_STRONG_TYPEDEF(uint8_t, SymbolKind)
LLVM_YAML_STRONG_TYPEDEF(uint32_t, SegmentFlags)
LLVM_YAML_STRONG_TYPEDEF(uint32_t, LimitFlags)
LLVM_YAML_STRONG_TYPEDEF(uint32_t, ComdatKind)
LLVM_YAML_STRONG_TYPEDEF(uint8_t, LimitFlags)
LLVM_YAML_STRONG_TYPEDEF(uint8_t, ComdatKind)
struct FileHeader {
yaml::Hex32 Version;

View File

@ -258,7 +258,7 @@ private:
}
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 @@ void WasmObjectWriter::startSection(SectionBookkeeping &Section,
"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 @@ void WasmObjectWriter::writeRelocations(
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 @@ void WasmObjectWriter::writeTypeSection(
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 @@ void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports,
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 @@ void WasmObjectWriter::writeExportSection(ArrayRef<wasm::WasmExport> Exports) {
encodeULEB128(Exports.size(), getStream());
for (const wasm::WasmExport &Export : Exports) {
writeString(Export.Name);
encodeSLEB128(Export.Kind, getStream());
write8(Export.Kind);
encodeULEB128(Export.Index, getStream());
}

View File

@ -99,18 +99,6 @@ static uint8_t readVaruint1(const uint8_t *&Ptr) {
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::WasmLimits readLimits(const uint8_t *&Ptr) {
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 wasm::WasmTable readTable(const uint8_t *&Ptr) {
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 @@ Error WasmObjectFile::parseNameSection(const uint8_t *Ptr, const uint8_t *End) {
}
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 @@ Error WasmObjectFile::parseLinkingSection(const uint8_t *Ptr,
}
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 @@ WasmSection* WasmObjectFile::findSectionByType(uint32_t Type) {
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 @@ Error WasmObjectFile::parseTypeSection(const uint8_t *Ptr, const uint8_t *End) {
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 @@ Error WasmObjectFile::parseTypeSection(const uint8_t *Ptr, const uint8_t *End) {
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 @@ Error WasmObjectFile::parseTypeSection(const uint8_t *Ptr, const uint8_t *End) {
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 @@ Error WasmObjectFile::parseImportSection(const uint8_t *Ptr, const uint8_t *End)
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 @@ Error WasmObjectFile::parseGlobalSection(const uint8_t *Ptr, const uint8_t *End)
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 @@ Error WasmObjectFile::parseCodeSection(const uint8_t *Ptr, const uint8_t *End) {
while (NumLocalDecls--) {
wasm::WasmLocalDecl Decl;
Decl.Count = readVaruint32(Ptr);
Decl.Type = readVarint7(Ptr);
Decl.Type = readUint8(Ptr);
Function.Locals.push_back(Decl);
}

View File

@ -31,7 +31,7 @@ WebAssemblyTargetStreamer::WebAssemblyTargetStreamer(MCStreamer &S)
: MCTargetStreamer(S) {}
void WebAssemblyTargetStreamer::emitValueType(wasm::ValType Type) {
Streamer.EmitSLEB128IntValue(int32_t(Type));
Streamer.EmitIntValue(uint8_t(Type), 1);
}
WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer(

View File

@ -95,10 +95,10 @@ MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
// 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;
}

View File

@ -78,7 +78,7 @@ static int writeStringRef(const StringRef &Str, raw_ostream &OS) {
}
static int writeLimits(const WasmYAML::Limits &Lim, raw_ostream &OS) {
encodeULEB128(Lim.Flags, OS);
writeUint8(OS, Lim.Flags);
encodeULEB128(Lim.Initial, OS);
if (Lim.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
encodeULEB128(Lim.Maximum, OS);
@ -138,7 +138,7 @@ int WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &S
// SYMBOL_TABLE subsection
if (Section.SymbolTable.size()) {
encodeULEB128(wasm::WASM_SYMBOL_TABLE, OS);
writeUint8(OS, wasm::WASM_SYMBOL_TABLE);
encodeULEB128(Section.SymbolTable.size(), SubSection.GetStream());
#ifndef NDEBUG
@ -146,7 +146,7 @@ int WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &S
#endif
for (const WasmYAML::SymbolInfo &Info : Section.SymbolTable) {
assert(Info.Index == SymbolIndex++);
encodeULEB128(Info.Kind, SubSection.GetStream());
writeUint8(SubSection.GetStream(), Info.Kind);
encodeULEB128(Info.Flags, SubSection.GetStream());
switch (Info.Kind) {
case wasm::WASM_SYMBOL_TYPE_FUNCTION:
@ -173,7 +173,7 @@ int WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &S
// SEGMENT_NAMES subsection
if (Section.SegmentInfos.size()) {
encodeULEB128(wasm::WASM_SEGMENT_INFO, OS);
writeUint8(OS, wasm::WASM_SEGMENT_INFO);
encodeULEB128(Section.SegmentInfos.size(), SubSection.GetStream());
for (const WasmYAML::SegmentInfo &SegmentInfo : Section.SegmentInfos) {
writeStringRef(SegmentInfo.Name, SubSection.GetStream());
@ -185,7 +185,7 @@ int WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &S
// INIT_FUNCS subsection
if (Section.InitFunctions.size()) {
encodeULEB128(wasm::WASM_INIT_FUNCS, OS);
writeUint8(OS, wasm::WASM_INIT_FUNCS);
encodeULEB128(Section.InitFunctions.size(), SubSection.GetStream());
for (const WasmYAML::InitFunction &Func : Section.InitFunctions) {
encodeULEB128(Func.Priority, SubSection.GetStream());
@ -196,14 +196,14 @@ int WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &S
// COMDAT_INFO subsection
if (Section.Comdats.size()) {
encodeULEB128(wasm::WASM_COMDAT_INFO, OS);
writeUint8(OS, wasm::WASM_COMDAT_INFO);
encodeULEB128(Section.Comdats.size(), SubSection.GetStream());
for (const auto &C : Section.Comdats) {
writeStringRef(C.Name, SubSection.GetStream());
encodeULEB128(0, SubSection.GetStream()); // flags for future use
encodeULEB128(C.Entries.size(), SubSection.GetStream());
for (const WasmYAML::ComdatEntry &Entry : C.Entries) {
encodeULEB128(Entry.Kind, SubSection.GetStream());
writeUint8(SubSection.GetStream(), Entry.Kind);
encodeULEB128(Entry.Index, SubSection.GetStream());
}
}
@ -216,7 +216,7 @@ int WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &S
int WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section) {
writeStringRef(Section.Name, OS);
if (Section.FunctionNames.size()) {
encodeULEB128(wasm::WASM_NAMES_FUNCTION, OS);
writeUint8(OS, wasm::WASM_NAMES_FUNCTION);
SubSectionWriter SubSection(OS);
@ -255,15 +255,15 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
return 1;
}
++ExpectedIndex;
encodeSLEB128(Sig.Form, OS);
writeUint8(OS, Sig.Form);
encodeULEB128(Sig.ParamTypes.size(), OS);
for (auto ParamType : Sig.ParamTypes)
encodeSLEB128(ParamType, OS);
writeUint8(OS, ParamType);
if (Sig.ReturnType == wasm::WASM_TYPE_NORESULT) {
encodeSLEB128(0, OS);
encodeULEB128(0, OS);
} else {
encodeULEB128(1, OS);
encodeSLEB128(Sig.ReturnType, OS);
writeUint8(OS, Sig.ReturnType);
}
}
return 0;
@ -275,14 +275,14 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
for (const WasmYAML::Import &Import : Section.Imports) {
writeStringRef(Import.Module, OS);
writeStringRef(Import.Field, OS);
encodeULEB128(Import.Kind, OS);
writeUint8(OS, Import.Kind);
switch (Import.Kind) {
case wasm::WASM_EXTERNAL_FUNCTION:
encodeULEB128(Import.SigIndex, OS);
NumImportedFunctions++;
break;
case wasm::WASM_EXTERNAL_GLOBAL:
encodeSLEB128(Import.GlobalImport.Type, OS);
writeUint8(OS, Import.GlobalImport.Type);
writeUint8(OS, Import.GlobalImport.Mutable);
NumImportedGlobals++;
break;
@ -290,7 +290,7 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
writeLimits(Import.Memory, OS);
break;
case wasm::WASM_EXTERNAL_TABLE:
encodeSLEB128(Import.TableImport.ElemType, OS);
writeUint8(OS,Import.TableImport.ElemType);
writeLimits(Import.TableImport.TableLimits, OS);
break;
default:
@ -315,7 +315,7 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
encodeULEB128(Section.Exports.size(), OS);
for (const WasmYAML::Export &Export : Section.Exports) {
writeStringRef(Export.Name, OS);
encodeULEB128(Export.Kind, OS);
writeUint8(OS, Export.Kind);
encodeULEB128(Export.Index, OS);
}
return 0;
@ -331,7 +331,7 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
WasmYAML::TableSection &Section) {
encodeULEB128(Section.Tables.size(), OS);
for (auto &Table : Section.Tables) {
encodeSLEB128(Table.ElemType, OS);
writeUint8(OS, Table.ElemType);
writeLimits(Table.TableLimits, OS);
}
return 0;
@ -356,7 +356,7 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
return 1;
}
++ExpectedIndex;
encodeSLEB128(Global.Type, OS);
writeUint8(OS, Global.Type);
writeUint8(OS, Global.Mutable);
writeInitExpr(Global.InitExpr, OS);
}
@ -394,7 +394,7 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
encodeULEB128(Func.Locals.size(), StringStream);
for (auto &LocalDecl : Func.Locals) {
encodeULEB128(LocalDecl.Count, StringStream);
encodeSLEB128(LocalDecl.Type, StringStream);
writeUint8(StringStream, LocalDecl.Type);
}
Func.Body.writeAsBinary(StringStream);
@ -435,11 +435,11 @@ int WasmWriter::writeRelocSection(raw_ostream &OS,
}
writeStringRef(Name, OS);
encodeULEB128(Sec.Type, OS);
writeUint8(OS, Sec.Type);
encodeULEB128(Sec.Relocations.size(), OS);
for (auto Reloc: Sec.Relocations) {
encodeULEB128(Reloc.Type, OS);
writeUint8(OS, Reloc.Type);
encodeULEB128(Reloc.Offset, OS);
encodeULEB128(Reloc.Index, OS);
switch (Reloc.Type) {
@ -525,7 +525,7 @@ int WasmWriter::writeWasm(raw_ostream &OS) {
if (Sec->Relocations.empty())
continue;
encodeULEB128(wasm::WASM_SEC_CUSTOM, OS);
writeUint8(OS, wasm::WASM_SEC_CUSTOM);
std::string OutString;
raw_string_ostream StringStream(OutString);
writeRelocSection(StringStream, *Sec);