Index: wasm/Writer.cpp =================================================================== --- wasm/Writer.cpp +++ wasm/Writer.cpp @@ -261,7 +261,7 @@ raw_ostream &OS = Section->getStream(); writeUleb128(OS, 1, "table count"); - writeSleb128(OS, WASM_TYPE_ANYFUNC, "table type"); + writeU8(OS, WASM_TYPE_ANYFUNC, "table type"); writeUleb128(OS, WASM_LIMITS_FLAG_HAS_MAX, "table flags"); writeUleb128(OS, TableSize, "table initial size"); writeUleb128(OS, TableSize, "table max size"); @@ -427,7 +427,7 @@ WasmSymbolType Kind = Sym->getWasmType(); uint32_t Flags = getWasmFlags(Sym); - writeUleb128(Sub.OS, Kind, "sym kind"); + writeU8(Sub.OS, Kind, "sym kind"); writeUleb128(Sub.OS, Flags, "sym flags"); switch (Kind) { @@ -503,7 +503,7 @@ writeUleb128(Sub.OS, 0, "comdat flags"); // flags for future use writeUleb128(Sub.OS, C.second.size(), "num entries"); for (const ComdatEntry &Entry : C.second) { - writeUleb128(Sub.OS, Entry.Kind, "entry kind"); + writeU8(Sub.OS, Entry.Kind, "entry kind"); writeUleb128(Sub.OS, Entry.Index, "entry index"); } } Index: wasm/WriterUtils.h =================================================================== --- wasm/WriterUtils.h +++ wasm/WriterUtils.h @@ -44,19 +44,19 @@ void debugWrite(uint64_t Offset, const Twine &Msg); -void writeUleb128(raw_ostream &OS, uint32_t Number, StringRef Msg); +void writeUleb128(raw_ostream &OS, uint32_t Number, const Twine &Msg); -void writeSleb128(raw_ostream &OS, int32_t Number, StringRef Msg); +void writeSleb128(raw_ostream &OS, int32_t Number, const Twine &Msg); -void writeBytes(raw_ostream &OS, const char *Bytes, size_t count, StringRef Msg); +void writeBytes(raw_ostream &OS, const char *Bytes, size_t count, const Twine &Msg); -void writeStr(raw_ostream &OS, StringRef String, StringRef Msg); +void writeStr(raw_ostream &OS, StringRef String, const Twine &Msg); -void writeU8(raw_ostream &OS, uint8_t byte, StringRef Msg); +void writeU8(raw_ostream &OS, uint8_t byte, const Twine &Msg); -void writeU32(raw_ostream &OS, uint32_t Number, StringRef Msg); +void writeU32(raw_ostream &OS, uint32_t Number, const Twine &Msg); -void writeValueType(raw_ostream &OS, int32_t Type, StringRef Msg); +void writeValueType(raw_ostream &OS, uint8_t Type, const Twine &Msg); void writeSig(raw_ostream &OS, const llvm::wasm::WasmSignature &Sig); Index: wasm/WriterUtils.cpp =================================================================== --- wasm/WriterUtils.cpp +++ wasm/WriterUtils.cpp @@ -40,51 +40,54 @@ DEBUG(dbgs() << format(" | %08lld: ", Offset) << Msg << "\n"); } -void wasm::writeUleb128(raw_ostream &OS, uint32_t Number, StringRef Msg) { +void wasm::writeUleb128(raw_ostream &OS, uint32_t Number, const Twine &Msg) { debugWrite(OS.tell(), Msg + "[" + utohexstr(Number) + "]"); encodeULEB128(Number, OS); } -void wasm::writeSleb128(raw_ostream &OS, int32_t Number, StringRef Msg) { +void wasm::writeSleb128(raw_ostream &OS, int32_t Number, const Twine &Msg) { debugWrite(OS.tell(), Msg + "[" + utohexstr(Number) + "]"); encodeSLEB128(Number, OS); } void wasm::writeBytes(raw_ostream &OS, const char *Bytes, size_t Count, - StringRef Msg) { + const Twine &Msg) { debugWrite(OS.tell(), Msg + " [data[" + Twine(Count) + "]]"); OS.write(Bytes, Count); } -void wasm::writeStr(raw_ostream &OS, StringRef String, StringRef Msg) { +void wasm::writeStr(raw_ostream &OS, StringRef String, const Twine &Msg) { debugWrite(OS.tell(), Msg + " [str[" + Twine(String.size()) + "]: " + String + "]"); encodeULEB128(String.size(), OS); OS.write(String.data(), String.size()); } -void wasm::writeU8(raw_ostream &OS, uint8_t Byte, StringRef Msg) { OS << Byte; } +void wasm::writeU8(raw_ostream &OS, uint8_t Byte, const Twine &Msg) { + debugWrite(OS.tell(), Msg + " [0x" + utohexstr(Byte) + "]"); + OS << Byte; +} -void wasm::writeU32(raw_ostream &OS, uint32_t Number, StringRef Msg) { +void wasm::writeU32(raw_ostream &OS, uint32_t Number, const Twine &Msg) { debugWrite(OS.tell(), Msg + "[" + utohexstr(Number) + "]"); support::endian::Writer(OS).write(Number); } -void wasm::writeValueType(raw_ostream &OS, int32_t Type, StringRef Msg) { - debugWrite(OS.tell(), Msg + "[type: " + valueTypeToString(Type) + "]"); - encodeSLEB128(Type, OS); +void wasm::writeValueType(raw_ostream &OS, uint8_t Type, const Twine &Msg) { + writeU8(OS, Type, Twine(Msg) + "[type: " + valueTypeToString(Type) + "]"); } void wasm::writeSig(raw_ostream &OS, const WasmSignature &Sig) { - writeSleb128(OS, WASM_TYPE_FUNC, "signature type"); + writeU8(OS, WASM_TYPE_FUNC, "signature type"); writeUleb128(OS, Sig.ParamTypes.size(), "param Count"); - for (int32_t ParamType : Sig.ParamTypes) { + + for (uint8_t ParamType : Sig.ParamTypes) writeValueType(OS, ParamType, "param type"); - } + if (Sig.ReturnType == WASM_TYPE_NORESULT) { - writeUleb128(OS, 0, "result Count"); + writeU8(OS, 0, "result Count"); } else { - writeUleb128(OS, 1, "result Count"); + writeU8(OS, 1, "result Count"); writeValueType(OS, Sig.ReturnType, "result type"); } }