[Wasm] Add comments to .wat files

This commit is contained in:
Svyatoslav Kuzmich
2022-12-16 13:57:59 +00:00
committed by Space Team
parent 41dbbd1fc8
commit 62ac77ca39
6 changed files with 78 additions and 1 deletions
@@ -95,6 +95,9 @@ sealed class WasmImmediate {
class HeapType(val value: WasmHeapType) : WasmImmediate() {
constructor(type: WasmType) : this(type.getHeapType())
}
// Pseudo-immediates
class ConstString(val value: String) : WasmImmediate()
}
@@ -403,11 +406,17 @@ enum class WasmOp(
// ============================================================
// Pseudo-instruction, just alias for a normal call. It's used to easily spot get_unit on the wasm level.
GET_UNIT("call", 0x10, FUNC_IDX)
GET_UNIT("call", 0x10, FUNC_IDX),
PSEUDO_COMMENT_PREVIOUS_INSTR("<comment-single>", WASM_OP_PSEUDO_OPCODE),
PSEUDO_COMMENT_GROUP_START("<comment-group-start>", WASM_OP_PSEUDO_OPCODE),
PSEUDO_COMMENT_GROUP_END("<comment-group-end>", WASM_OP_PSEUDO_OPCODE),
;
constructor(mnemonic: String, opcode: Int, vararg immediates: WasmImmediateKind) : this(mnemonic, opcode, immediates.toList())
}
const val WASM_OP_PSEUDO_OPCODE = 0xFFFF
val opcodesToOp: Map<Int, WasmOp> =
enumValues<WasmOp>().associateBy { it.opcode }
@@ -178,4 +178,16 @@ abstract class WasmExpressionBuilder {
fun buildDrop() {
buildInstr(WasmOp.DROP)
}
inline fun commentPreviousInstr(text: () -> String) {
buildInstr(WasmOp.PSEUDO_COMMENT_PREVIOUS_INSTR, WasmImmediate.ConstString(text()))
}
inline fun commentGroupStart(text: () -> String) {
buildInstr(WasmOp.PSEUDO_COMMENT_GROUP_START, WasmImmediate.ConstString(text()))
}
fun commentGroupEnd() {
buildInstr(WasmOp.PSEUDO_COMMENT_GROUP_END)
}
}
@@ -210,6 +210,10 @@ class WasmIrToBinary(
}
val opcode = instr.operator.opcode
if (opcode == WASM_OP_PSEUDO_OPCODE)
return
if (opcode > 0xFF) {
b.writeByte((opcode ushr 8).toByte())
b.writeByte((opcode and 0xFF).toByte())
@@ -259,6 +263,8 @@ class WasmIrToBinary(
is WasmImmediate.GcType -> appendModuleFieldReference(x.value.owner)
is WasmImmediate.StructFieldIdx -> b.writeVarUInt32(x.value.owner)
is WasmImmediate.HeapType -> appendHeapType(x.value)
is WasmImmediate.ConstString ->
error("Instructions with pseudo immediates should be skipped")
}
}
@@ -60,6 +60,34 @@ class WasmIrToText : SExpressionBuilder() {
private fun appendInstr(wasmInstr: WasmInstr) {
val op = wasmInstr.operator
if (op.opcode == WASM_OP_PSEUDO_OPCODE) {
fun commentText() =
(wasmInstr.immediates.single() as WasmImmediate.ConstString).value
when (op) {
WasmOp.PSEUDO_COMMENT_PREVIOUS_INSTR -> {
val text = commentText()
require(text.lineSequence().count() < 2) { "Comments for single instruction should be in one line" }
stringBuilder.append(" ;; ")
stringBuilder.append(text)
}
WasmOp.PSEUDO_COMMENT_GROUP_START -> {
newLine()
commentText().lines().forEach { line ->
newLine()
stringBuilder.append(";; ")
stringBuilder.append(line)
}
}
WasmOp.PSEUDO_COMMENT_GROUP_END -> {
newLine()
}
else -> error("Unknown pseudo op $op")
}
return
}
if (op == WasmOp.END || op == WasmOp.ELSE || op == WasmOp.CATCH)
indent--
@@ -113,6 +141,8 @@ class WasmIrToText : SExpressionBuilder() {
is WasmImmediate.HeapType -> {
appendHeapType(x.value)
}
is WasmImmediate.ConstString -> error("Pseudo immediate")
}
}