summaryrefslogtreecommitdiff
path: root/wasm/WriterUtils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'wasm/WriterUtils.cpp')
-rw-r--r--wasm/WriterUtils.cpp113
1 files changed, 55 insertions, 58 deletions
diff --git a/wasm/WriterUtils.cpp b/wasm/WriterUtils.cpp
index 5bdf0d2e3f65..201529edeaa6 100644
--- a/wasm/WriterUtils.cpp
+++ b/wasm/WriterUtils.cpp
@@ -8,12 +8,9 @@
//===----------------------------------------------------------------------===//
#include "WriterUtils.h"
-
#include "lld/Common/ErrorHandler.h"
-
#include "llvm/Support/Debug.h"
#include "llvm/Support/EndianStream.h"
-#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/LEB128.h"
#define DEBUG_TYPE "lld"
@@ -22,7 +19,7 @@ using namespace llvm;
using namespace llvm::wasm;
using namespace lld::wasm;
-static const char *valueTypeToString(int32_t Type) {
+static const char *valueTypeToString(uint8_t Type) {
switch (Type) {
case WASM_TYPE_I32:
return "i32";
@@ -39,61 +36,57 @@ static const char *valueTypeToString(int32_t Type) {
namespace lld {
-void wasm::debugWrite(uint64_t offset, Twine msg) {
- DEBUG(dbgs() << format(" | %08" PRIx64 ": ", offset) << msg << "\n");
+void wasm::debugWrite(uint64_t Offset, const Twine &Msg) {
+ LLVM_DEBUG(dbgs() << format(" | %08lld: ", Offset) << Msg << "\n");
}
-void wasm::writeUleb128(raw_ostream &OS, uint32_t Number, const char *msg) {
- if (msg)
- debugWrite(OS.tell(), msg + formatv(" [{0:x}]", Number));
+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, const char *msg) {
- if (msg)
- debugWrite(OS.tell(), msg + formatv(" [{0:x}]", Number));
+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,
- const char *msg) {
- if (msg)
- debugWrite(OS.tell(), msg + formatv(" [data[{0}]]", count));
- OS.write(bytes, count);
+void wasm::writeBytes(raw_ostream &OS, const char *Bytes, size_t Count,
+ const Twine &Msg) {
+ debugWrite(OS.tell(), Msg + " [data[" + Twine(Count) + "]]");
+ OS.write(Bytes, Count);
}
-void wasm::writeStr(raw_ostream &OS, const StringRef String, const char *msg) {
- if (msg)
- debugWrite(OS.tell(),
- msg + formatv(" [str[{0}]: {1}]", String.size(), String));
- writeUleb128(OS, String.size(), nullptr);
- writeBytes(OS, String.data(), String.size());
+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, const char *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, const char *msg) {
- debugWrite(OS.tell(), msg + formatv("[{0:x}]", Number));
- support::endian::Writer<support::little>(OS).write(Number);
+void wasm::writeU32(raw_ostream &OS, uint32_t Number, const Twine &Msg) {
+ debugWrite(OS.tell(), Msg + "[0x" + utohexstr(Number) + "]");
+ support::endian::write(OS, Number, support::little);
}
-void wasm::writeValueType(raw_ostream &OS, int32_t Type, const char *msg) {
- debugWrite(OS.tell(), msg + formatv("[type: {0}]", valueTypeToString(Type)));
- writeSleb128(OS, Type, nullptr);
+void wasm::writeValueType(raw_ostream &OS, uint8_t Type, const Twine &Msg) {
+ writeU8(OS, Type, Msg + "[type: " + valueTypeToString(Type) + "]");
}
void wasm::writeSig(raw_ostream &OS, const WasmSignature &Sig) {
- writeSleb128(OS, WASM_TYPE_FUNC, "signature type");
- writeUleb128(OS, Sig.ParamTypes.size(), "param count");
- for (int32_t ParamType : Sig.ParamTypes) {
+ writeU8(OS, WASM_TYPE_FUNC, "signature type");
+ writeUleb128(OS, Sig.ParamTypes.size(), "param Count");
+ for (uint8_t ParamType : Sig.ParamTypes) {
writeValueType(OS, ParamType, "param type");
}
if (Sig.ReturnType == WASM_TYPE_NORESULT) {
- writeUleb128(OS, 0, "result count");
+ writeUleb128(OS, 0, "result Count");
} else {
- writeUleb128(OS, 1, "result count");
+ writeUleb128(OS, 1, "result Count");
writeValueType(OS, Sig.ReturnType, "result type");
}
}
@@ -117,18 +110,27 @@ void wasm::writeInitExpr(raw_ostream &OS, const WasmInitExpr &InitExpr) {
}
void wasm::writeLimits(raw_ostream &OS, const WasmLimits &Limits) {
- writeUleb128(OS, Limits.Flags, "limits flags");
+ writeU8(OS, Limits.Flags, "limits flags");
writeUleb128(OS, Limits.Initial, "limits initial");
if (Limits.Flags & WASM_LIMITS_FLAG_HAS_MAX)
writeUleb128(OS, Limits.Maximum, "limits max");
}
+void wasm::writeGlobalType(raw_ostream &OS, const WasmGlobalType &Type) {
+ writeValueType(OS, Type.Type, "global type");
+ writeU8(OS, Type.Mutable, "global mutable");
+}
+
void wasm::writeGlobal(raw_ostream &OS, const WasmGlobal &Global) {
- writeValueType(OS, Global.Type, "global type");
- writeUleb128(OS, Global.Mutable, "global mutable");
+ writeGlobalType(OS, Global.Type);
writeInitExpr(OS, Global.InitExpr);
}
+void wasm::writeTableType(raw_ostream &OS, const llvm::wasm::WasmTable &Type) {
+ writeU8(OS, WASM_TYPE_ANYFUNC, "table type");
+ writeLimits(OS, Type.Limits);
+}
+
void wasm::writeImport(raw_ostream &OS, const WasmImport &Import) {
writeStr(OS, Import.Module, "import module name");
writeStr(OS, Import.Field, "import field name");
@@ -138,12 +140,14 @@ void wasm::writeImport(raw_ostream &OS, const WasmImport &Import) {
writeUleb128(OS, Import.SigIndex, "import sig index");
break;
case WASM_EXTERNAL_GLOBAL:
- writeValueType(OS, Import.Global.Type, "import global type");
- writeUleb128(OS, Import.Global.Mutable, "import global mutable");
+ writeGlobalType(OS, Import.Global);
break;
case WASM_EXTERNAL_MEMORY:
writeLimits(OS, Import.Memory);
break;
+ case WASM_EXTERNAL_TABLE:
+ writeTableType(OS, Import.Table);
+ break;
default:
fatal("unsupported import type: " + Twine(Import.Kind));
}
@@ -162,27 +166,13 @@ void wasm::writeExport(raw_ostream &OS, const WasmExport &Export) {
case WASM_EXTERNAL_MEMORY:
writeUleb128(OS, Export.Index, "memory index");
break;
- default:
- fatal("unsupported export type: " + Twine(Export.Kind));
- }
-}
-
-void wasm::writeReloc(raw_ostream &OS, const OutputRelocation &Reloc) {
- writeUleb128(OS, Reloc.Reloc.Type, "reloc type");
- writeUleb128(OS, Reloc.Reloc.Offset, "reloc offset");
- writeUleb128(OS, Reloc.NewIndex, "reloc index");
-
- switch (Reloc.Reloc.Type) {
- case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
- case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
- case R_WEBASSEMBLY_MEMORY_ADDR_I32:
- writeUleb128(OS, Reloc.Reloc.Addend, "reloc addend");
+ case WASM_EXTERNAL_TABLE:
+ writeUleb128(OS, Export.Index, "table index");
break;
default:
- break;
+ fatal("unsupported export type: " + Twine(Export.Kind));
}
}
-
} // namespace lld
std::string lld::toString(ValType Type) {
@@ -195,6 +185,8 @@ std::string lld::toString(ValType Type) {
return "F32";
case ValType::F64:
return "F64";
+ case ValType::EXCEPT_REF:
+ return "except_ref";
}
llvm_unreachable("Invalid wasm::ValType");
}
@@ -213,3 +205,8 @@ std::string lld::toString(const WasmSignature &Sig) {
S += toString(static_cast<ValType>(Sig.ReturnType));
return S.str();
}
+
+std::string lld::toString(const WasmGlobalType &Sig) {
+ return (Sig.Mutable ? "var " : "const ") +
+ toString(static_cast<ValType>(Sig.Type));
+}