[Wasm] Replace deprecated cast instructions

This commit is contained in:
Igor Yakovlev
2023-05-12 18:35:24 +02:00
committed by Space Team
parent 45897cf045
commit 6437d0919c
8 changed files with 57 additions and 43 deletions
@@ -189,8 +189,8 @@ class WasmSymbols(
val refEq = getInternalFunction("wasm_ref_eq")
val refIsNull = getInternalFunction("wasm_ref_is_null")
val externRefIsNull = getInternalFunction("wasm_externref_is_null")
val refTest = getInternalFunction("wasm_ref_test_deprecated")
val refCastNull = getInternalFunction("wasm_ref_cast_deprecated")
val refTest = getInternalFunction("wasm_ref_test")
val refCastNull = getInternalFunction("wasm_ref_cast_null")
val wasmArrayCopy = getInternalFunction("wasm_array_copy")
val wasmArrayNewData0 = getInternalFunction("array_new_data0")
@@ -435,7 +435,7 @@ class BodyGenerator(
body.commentGroupStart { "virtual call: ${function.fqNameWhenAvailable}" }
//TODO: check why it could be needed
generateRefNullCast(receiver.type, klass.defaultType, location)
generateRefCast(receiver.type, klass.defaultType, location)
body.buildStructGet(context.referenceGcType(klass.symbol), WasmSymbol(0), location)
body.buildStructGet(context.referenceVTableGcType(klass.symbol), WasmSymbol(vfSlot), location)
@@ -450,7 +450,7 @@ class BodyGenerator(
body.buildStructGet(context.referenceGcType(irBuiltIns.anyClass), WasmSymbol(1), location)
val classITableReference = context.referenceClassITableGcType(symbol)
body.buildRefCastNullStatic(classITableReference, location)
body.buildRefCastStatic(classITableReference, location)
body.buildStructGet(classITableReference, context.referenceClassITableInterfaceSlot(symbol), location)
val vfSlot = context.getInterfaceMetadata(symbol).methods
@@ -486,6 +486,15 @@ class BodyGenerator(
}
}
private fun generateRefCast(fromType: IrType, toType: IrType, location: SourceLocation) {
if (!isDownCastAlwaysSuccessInRuntime(fromType, toType)) {
body.buildRefCastStatic(
toType = context.referenceGcType(toType.getRuntimeClass(irBuiltIns).symbol),
location
)
}
}
private fun generateRefTest(fromType: IrType, toType: IrType, location: SourceLocation) {
if (!isDownCastAlwaysSuccessInRuntime(fromType, toType)) {
body.buildRefTestStatic(
@@ -537,7 +546,14 @@ class BodyGenerator(
body.buildBlock("isInterface", WasmRefNullType(WasmHeapType.Simple.Data)) { innerLabel ->
body.buildGetLocal(parameterLocal, location)
body.buildStructGet(context.referenceGcType(irBuiltIns.anyClass), WasmSymbol(1), location)
body.buildBrInstr(WasmOp.BR_ON_CAST_FAIL_DEPRECATED, innerLabel, classITable, location)
body.buildBrInstr(
WasmOp.BR_ON_CAST_FAIL,
innerLabel,
classITable,
location,
)
body.buildStructGet(classITable, context.referenceClassITableInterfaceSlot(irInterface.symbol), location)
body.buildInstr(WasmOp.REF_IS_NULL, location)
body.buildInstr(WasmOp.I32_EQZ, location)
@@ -581,7 +597,7 @@ class BodyGenerator(
val klass: IrClass = backendContext.inlineClassesUtils.getInlinedClass(toType)!!
val field = getInlineClassBackingField(klass)
generateRefNullCast(fromType, toType, location)
generateRefCast(fromType, toType, location)
generateInstanceFieldAccess(field, location)
}
@@ -593,15 +609,17 @@ class BodyGenerator(
body.buildBlock("returnIfAny", WasmAnyRef) { innerLabel ->
body.buildGetLocal(functionContext.referenceLocal(0), location)
body.buildInstr(WasmOp.EXTERN_INTERNALIZE, location)
body.buildBrInstr(WasmOp.BR_ON_NON_DATA_DEPRECATED, innerLabel, location)
body.buildBrInstr(
WasmOp.BR_ON_CAST_FAIL_DEPRECATED,
WasmOp.BR_ON_CAST_FAIL,
innerLabel,
context.referenceGcType(backendContext.irBuiltIns.anyClass),
location
location,
)
body.buildInstr(WasmOp.RETURN, location)
}
body.buildDrop(location)
}
wasmSymbols.wasmArrayCopy -> {
@@ -770,7 +788,7 @@ class BodyGenerator(
// REF -> REF -> REF_CAST
if (!expectedIsPrimitive) {
if (expectedClassErased.isSubclassOf(actualClassErased)) {
generateRefNullCast(actualTypeErased, expectedTypeErased, location)
generateRefCast(actualTypeErased, expectedTypeErased, location)
} else {
body.buildUnreachableForVerifier()
}