Index: llvm/trunk/include/llvm/BinaryFormat/Wasm.h =================================================================== --- llvm/trunk/include/llvm/BinaryFormat/Wasm.h +++ llvm/trunk/include/llvm/BinaryFormat/Wasm.h @@ -33,24 +33,24 @@ }; struct WasmSignature { - std::vector ParamTypes; - int32_t ReturnType; + std::vector 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 WasmGlobalType { - int32_t Type; + uint8_t Type; bool Mutable; }; @@ -79,7 +79,7 @@ struct WasmImport { StringRef Module; StringRef Field; - uint32_t Kind; + uint8_t Kind; union { uint32_t SigIndex; WasmGlobalType Global; @@ -89,7 +89,7 @@ }; struct WasmLocalDecl { - int32_t Type; + uint8_t Type; uint32_t Count; }; @@ -128,7 +128,7 @@ }; 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 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 @@ }; // 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 @@ }; // 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 @@ #define WASM_RELOC(name, value) name = value, -enum : unsigned { +enum : uint8_t { #include "WasmRelocs.def" }; Index: llvm/trunk/include/llvm/ObjectYAML/WasmYAML.h =================================================================== --- llvm/trunk/include/llvm/ObjectYAML/WasmYAML.h +++ llvm/trunk/include/llvm/ObjectYAML/WasmYAML.h @@ -28,17 +28,17 @@ 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; Index: llvm/trunk/lib/MC/WasmObjectWriter.cpp =================================================================== --- llvm/trunk/lib/MC/WasmObjectWriter.cpp +++ llvm/trunk/lib/MC/WasmObjectWriter.cpp @@ -258,7 +258,7 @@ } void writeValueType(wasm::ValType Ty) { - encodeSLEB128(int32_t(Ty), getStream()); + write8(static_cast(Ty)); } void writeTypeSection(ArrayRef 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()); } Index: llvm/trunk/lib/Object/WasmObjectFile.cpp =================================================================== --- llvm/trunk/lib/Object/WasmObjectFile.cpp +++ llvm/trunk/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("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("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( "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); } Index: llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp =================================================================== --- llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp +++ llvm/trunk/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( Index: llvm/trunk/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp =================================================================== --- llvm/trunk/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp +++ llvm/trunk/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; } Index: llvm/trunk/tools/yaml2obj/yaml2wasm.cpp =================================================================== --- llvm/trunk/tools/yaml2obj/yaml2wasm.cpp +++ llvm/trunk/tools/yaml2obj/yaml2wasm.cpp @@ -78,7 +78,7 @@ } 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 @@ // 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 @@ #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 @@ // 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 @@ // 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 @@ // 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::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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ return 1; } ++ExpectedIndex; - encodeSLEB128(Global.Type, OS); + writeUint8(OS, Global.Type); writeUint8(OS, Global.Mutable); writeInitExpr(Global.InitExpr, OS); } @@ -394,7 +394,7 @@ 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 @@ } 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 @@ 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);