[Wasm] Use Wasm GC arrays instead of JS arrays

JS arrays was a workaround for lack of arrays in Firefox Wasm GC prototype
Now with V8 as a test runner we can use proper arrays
This commit is contained in:
Svyatoslav Kuzmich
2020-12-07 15:42:55 +03:00
parent d15af70a3e
commit d4233f3f0e
15 changed files with 400 additions and 276 deletions
@@ -128,23 +128,7 @@ fun WasmCompiledModuleFragment.generateJs(): String {
Char_toString(char) {
return String.fromCharCode(char)
},
JsArray_new(size) {
return new Array(size);
},
JsArray_get(array, index) {
return array[index];
},
JsArray_set(array, index, value) {
array[index] = value;
},
JsArray_getSize(array) {
return array.length;
},
identity(x) {
return x;
},
@@ -122,7 +122,19 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
return
}
val wasmStruct: WasmSymbol<WasmTypeDeclaration> = context.referenceGcType(klass.symbol)
val wasmGcType: WasmSymbol<WasmTypeDeclaration> = context.referenceGcType(klass.symbol)
klass.getWasmArrayAnnotation()?.let { wasmArrayInfo ->
require(expression.valueArgumentsCount == 1) { "@WasmArrayOf constructs must have exactly one argument" }
generateExpression(expression.getValueArgument(0)!!)
body.buildRttCanon(context.transformType(klass.defaultType))
body.buildInstr(
WasmOp.ARRAY_NEW_DEFAULT_WITH_RTT,
WasmImmediate.GcType(wasmGcType)
)
return
}
val wasmClassId = context.referenceClassId(klass.symbol)
val irFields: List<IrField> = klass.allFields(backendContext.irBuiltIns)
@@ -135,7 +147,7 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
}
body.buildGetGlobal(context.referenceClassRTT(klass.symbol))
body.buildStructNew(wasmStruct)
body.buildStructNew(wasmGcType)
generateCall(expression)
}
@@ -472,12 +484,16 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
0 -> {
}
1 -> {
when (val imm = op.immediates[0]) {
WasmImmediateKind.MEM_ARG ->
immediates = arrayOf(WasmImmediate.MemArg(0u, 0u))
else ->
error("Immediate $imm is unsupported")
}
immediates = arrayOf(
when (val imm = op.immediates[0]) {
WasmImmediateKind.MEM_ARG ->
WasmImmediate.MemArg(0u, 0u)
WasmImmediateKind.STRUCT_TYPE_IDX ->
WasmImmediate.GcType(context.referenceGcType(function.dispatchReceiverParameter!!.type.classOrNull!!))
else ->
error("Immediate $imm is unsupported")
}
)
}
else ->
error("Op $opString is unsupported")
@@ -168,6 +168,23 @@ class DeclarationGenerator(val context: WasmModuleCodegenContext) : IrElementVis
if (declaration.isAnnotationClass) return
val symbol = declaration.symbol
// Handle arrays
declaration.getWasmArrayAnnotation()?.let { wasmArrayAnnotation ->
val nameStr = declaration.fqNameWhenAvailable.toString()
val wasmArrayDeclaration = WasmArrayDeclaration(
nameStr,
WasmStructFieldDeclaration(
name = "field",
type = context.transformType(wasmArrayAnnotation.type),
isMutable = true
)
)
context.defineGcType(symbol, wasmArrayDeclaration)
return
}
if (declaration.isInterface) {
context.registerInterface(symbol)
} else {
@@ -45,11 +45,11 @@ class WasmTypeTransformer(
toWasmValueType()
}
fun IrType.toStructType(): WasmType =
fun IrType.toWasmGcRefType(): WasmType =
WasmRefNullType(WasmHeapType.Type(context.referenceGcType(erasedUpperBound?.symbol ?: builtIns.anyClass)))
fun IrType.toBoxedInlineClassType(): WasmType =
toStructType()
toWasmGcRefType()
fun IrType.toWasmValueType(): WasmType =
when (this) {
@@ -88,7 +88,7 @@ class WasmTypeTransformer(
} else if (ic != null) {
getInlineClassUnderlyingType(ic).toWasmValueType()
} else {
this.toStructType()
this.toWasmGcRefType()
}
}
}
@@ -7,7 +7,11 @@ package org.jetbrains.kotlin.backend.wasm.utils
import org.jetbrains.kotlin.ir.backend.js.utils.getSingleConstStringArgument
import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.expressions.IrClassReference
import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.types.makeNullable
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.ir.util.getAnnotation
import org.jetbrains.kotlin.ir.util.hasAnnotation
import org.jetbrains.kotlin.name.FqName
@@ -36,5 +40,18 @@ fun IrAnnotationContainer.getWasmImportAnnotation(): WasmImportPair? =
)
}
class WasmArrayInfo(val klass: IrClass, val isNullable: Boolean) {
val type = klass.defaultType.let { if (isNullable) it.makeNullable() else it }
}
fun IrAnnotationContainer.getWasmArrayAnnotation(): WasmArrayInfo? =
getAnnotation(FqName("kotlin.wasm.internal.WasmArrayOf"))?.let {
WasmArrayInfo(
(it.getValueArgument(0) as IrClassReference).symbol.owner as IrClass,
(it.getValueArgument(1) as IrConst<*>).value as Boolean,
)
}
fun IrAnnotationContainer.getJsFunAnnotation(): String? =
getAnnotation(FqName("kotlin.JsFun"))?.getSingleConstStringArgument()