[Wasm] Don't generate unnecessary unreachable instructions

Helps with WAT readability
This commit is contained in:
Svyatoslav Kuzmich
2020-11-09 17:16:53 +03:00
parent b371b61cf5
commit ffe3487aa5
2 changed files with 9 additions and 0 deletions
@@ -9,6 +9,8 @@ abstract class WasmExpressionBuilder {
abstract fun buildInstr(op: WasmOp, vararg immediates: WasmImmediate)
abstract var numberOfNestedBlocks: Int
abstract val lastInstr: WasmOp?
fun buildConstI32(value: Int) {
buildInstr(WasmOp.I32_CONST, WasmImmediate.ConstI32(value))
}
@@ -30,6 +32,10 @@ abstract class WasmExpressionBuilder {
}
fun buildUnreachable() {
// Unreachable is not needed
if (lastInstr == WasmOp.UNREACHABLE || lastInstr == WasmOp.RETURN)
return
buildInstr(WasmOp.UNREACHABLE)
}
@@ -19,4 +19,7 @@ class WasmIrExpressionBuilder(
assert(value >= 0) { "end without matching block" }
field = value
}
override val lastInstr: WasmOp?
get() = expression.lastOrNull()?.operator
}