[Wasm] Refactoring: replace "struct types" with "GC types"
In preparation for adding array types
This commit is contained in:
+4
-4
@@ -78,7 +78,7 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
|
||||
|
||||
private fun generateInstanceFieldAccess(field: IrField) {
|
||||
body.buildStructGet(
|
||||
context.referenceStructType(field.parentAsClass.symbol),
|
||||
context.referenceGcType(field.parentAsClass.symbol),
|
||||
context.getStructFieldRef(field)
|
||||
)
|
||||
}
|
||||
@@ -91,7 +91,7 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
|
||||
generateExpression(receiver)
|
||||
generateExpression(expression.value)
|
||||
body.buildStructSet(
|
||||
struct = context.referenceStructType(field.parentAsClass.symbol),
|
||||
struct = context.referenceGcType(field.parentAsClass.symbol),
|
||||
fieldId = context.getStructFieldRef(field),
|
||||
)
|
||||
} else {
|
||||
@@ -122,7 +122,7 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
|
||||
return
|
||||
}
|
||||
|
||||
val wasmStruct: WasmSymbol<WasmStructDeclaration> = context.referenceStructType(klass.symbol)
|
||||
val wasmStruct: WasmSymbol<WasmTypeDeclaration> = context.referenceGcType(klass.symbol)
|
||||
val wasmClassId = context.referenceClassId(klass.symbol)
|
||||
|
||||
val irFields: List<IrField> = klass.allFields(backendContext.irBuiltIns)
|
||||
@@ -157,7 +157,7 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
|
||||
if (call.symbol == wasmSymbols.boxIntrinsic) {
|
||||
val toType = call.getTypeArgument(0)!!
|
||||
val klass = toType.erasedUpperBound!!
|
||||
val structTypeName = context.referenceStructType(klass.symbol)
|
||||
val structTypeName = context.referenceGcType(klass.symbol)
|
||||
val klassId = context.referenceClassId(klass.symbol)
|
||||
|
||||
body.buildConstI32Symbol(klassId)
|
||||
|
||||
+1
-1
@@ -183,7 +183,7 @@ class DeclarationGenerator(val context: WasmModuleCodegenContext) : IrElementVis
|
||||
}
|
||||
)
|
||||
|
||||
context.defineStructType(symbol, structType)
|
||||
context.defineGcType(symbol, structType)
|
||||
|
||||
var depth = 2
|
||||
val metadata = context.getClassMetadata(symbol)
|
||||
|
||||
+1
-2
@@ -13,7 +13,6 @@ import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
|
||||
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.types.classOrNull
|
||||
import org.jetbrains.kotlin.ir.types.classifierOrNull
|
||||
import org.jetbrains.kotlin.ir.types.getClass
|
||||
import org.jetbrains.kotlin.ir.util.getInlineClassUnderlyingType
|
||||
@@ -47,7 +46,7 @@ class WasmTypeTransformer(
|
||||
}
|
||||
|
||||
fun IrType.toStructType(): WasmType =
|
||||
WasmRefNullType(WasmHeapType.Type(context.referenceStructType(erasedUpperBound?.symbol ?: builtIns.anyClass)))
|
||||
WasmRefNullType(WasmHeapType.Type(context.referenceGcType(erasedUpperBound?.symbol ?: builtIns.anyClass)))
|
||||
|
||||
fun IrType.toBoxedInlineClassType(): WasmType =
|
||||
toStructType()
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ interface WasmBaseCodegenContext {
|
||||
|
||||
fun referenceFunction(irFunction: IrFunctionSymbol): WasmSymbol<WasmFunction>
|
||||
fun referenceGlobal(irField: IrFieldSymbol): WasmSymbol<WasmGlobal>
|
||||
fun referenceStructType(irClass: IrClassSymbol): WasmSymbol<WasmStructDeclaration>
|
||||
fun referenceGcType(irClass: IrClassSymbol): WasmSymbol<WasmTypeDeclaration>
|
||||
fun referenceFunctionType(irFunction: IrFunctionSymbol): WasmSymbol<WasmFunctionType>
|
||||
|
||||
fun referenceClassId(irClass: IrClassSymbol): WasmSymbol<Int>
|
||||
|
||||
+4
-4
@@ -20,8 +20,8 @@ class WasmCompiledModuleFragment {
|
||||
ReferencableAndDefinable<IrFieldSymbol, WasmGlobal>()
|
||||
val functionTypes =
|
||||
ReferencableAndDefinable<IrFunctionSymbol, WasmFunctionType>()
|
||||
val structTypes =
|
||||
ReferencableAndDefinable<IrClassSymbol, WasmStructDeclaration>()
|
||||
val gcTypes =
|
||||
ReferencableAndDefinable<IrClassSymbol, WasmTypeDeclaration>()
|
||||
val classIds =
|
||||
ReferencableElements<IrClassSymbol, Int>()
|
||||
val interfaceId =
|
||||
@@ -88,7 +88,7 @@ class WasmCompiledModuleFragment {
|
||||
bind(functions.unbound, functions.defined)
|
||||
bind(globals.unbound, globals.defined)
|
||||
bind(functionTypes.unbound, functionTypes.defined)
|
||||
bind(structTypes.unbound, structTypes.defined)
|
||||
bind(gcTypes.unbound, gcTypes.defined)
|
||||
bind(runtimeTypes.unbound, runtimeTypes.defined)
|
||||
|
||||
val klassIds = mutableMapOf<IrClassSymbol, Int>()
|
||||
@@ -162,7 +162,7 @@ class WasmCompiledModuleFragment {
|
||||
|
||||
val module = WasmModule(
|
||||
functionTypes = functionTypes.elements,
|
||||
structs = structTypes.elements,
|
||||
gcTypes = gcTypes.elements,
|
||||
importsInOrder = importedFunctions,
|
||||
importedFunctions = importedFunctions,
|
||||
definedFunctions = functions.elements.filterIsInstance<WasmFunction.Defined>(),
|
||||
|
||||
+1
-1
@@ -19,7 +19,7 @@ import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
|
||||
interface WasmModuleCodegenContext : WasmBaseCodegenContext {
|
||||
fun defineFunction(irFunction: IrFunctionSymbol, wasmFunction: WasmFunction)
|
||||
fun defineGlobal(irField: IrFieldSymbol, wasmGlobal: WasmGlobal)
|
||||
fun defineStructType(irClass: IrClassSymbol, wasmStruct: WasmStructDeclaration)
|
||||
fun defineGcType(irClass: IrClassSymbol, wasmType: WasmTypeDeclaration)
|
||||
fun defineRTT(irClass: IrClassSymbol, wasmGlobal: WasmGlobal)
|
||||
fun defineFunctionType(irFunction: IrFunctionSymbol, wasmFunctionType: WasmFunctionType)
|
||||
fun addJsFun(importName: String, jsCode: String)
|
||||
|
||||
+4
-4
@@ -92,8 +92,8 @@ class WasmModuleCodegenContextImpl(
|
||||
wasmFragment.globals.define(irField, wasmGlobal)
|
||||
}
|
||||
|
||||
override fun defineStructType(irClass: IrClassSymbol, wasmStruct: WasmStructDeclaration) {
|
||||
wasmFragment.structTypes.define(irClass, wasmStruct)
|
||||
override fun defineGcType(irClass: IrClassSymbol, wasmStruct: WasmTypeDeclaration) {
|
||||
wasmFragment.gcTypes.define(irClass, wasmStruct)
|
||||
}
|
||||
|
||||
override fun defineRTT(irClass: IrClassSymbol, wasmGlobal: WasmGlobal) {
|
||||
@@ -122,12 +122,12 @@ class WasmModuleCodegenContextImpl(
|
||||
override fun referenceGlobal(irField: IrFieldSymbol): WasmSymbol<WasmGlobal> =
|
||||
wasmFragment.globals.reference(irField)
|
||||
|
||||
override fun referenceStructType(irClass: IrClassSymbol): WasmSymbol<WasmStructDeclaration> {
|
||||
override fun referenceGcType(irClass: IrClassSymbol): WasmSymbol<WasmTypeDeclaration> {
|
||||
val type = irClass.defaultType
|
||||
require(!type.isNothing()) {
|
||||
"Can't reference Nothing type"
|
||||
}
|
||||
return wasmFragment.structTypes.reference(irClass)
|
||||
return wasmFragment.gcTypes.reference(irClass)
|
||||
}
|
||||
|
||||
override fun referenceClassRTT(irClass: IrClassSymbol): WasmSymbol<WasmGlobal> =
|
||||
|
||||
@@ -8,7 +8,7 @@ package org.jetbrains.kotlin.wasm.ir
|
||||
|
||||
class WasmModule(
|
||||
val functionTypes: List<WasmFunctionType> = emptyList(),
|
||||
val structs: List<WasmStructDeclaration> = emptyList(),
|
||||
val gcTypes: List<WasmTypeDeclaration> = emptyList(),
|
||||
|
||||
val importsInOrder: List<WasmNamedModuleField> = emptyList(),
|
||||
val importedFunctions: List<WasmFunction.Imported> = emptyList(),
|
||||
|
||||
@@ -79,8 +79,8 @@ sealed class WasmImmediate {
|
||||
class LabelIdxVector(val value: List<Int>) : WasmImmediate()
|
||||
class ElemIdx(val value: WasmElement) : WasmImmediate()
|
||||
|
||||
class StructType(val value: WasmSymbol<WasmStructDeclaration>) : WasmImmediate() {
|
||||
constructor(value: WasmStructDeclaration) : this(WasmSymbol(value))
|
||||
class GcType(val value: WasmSymbol<WasmTypeDeclaration>) : WasmImmediate() {
|
||||
constructor(value: WasmTypeDeclaration) : this(WasmSymbol(value))
|
||||
}
|
||||
|
||||
class StructFieldIdx(val value: WasmSymbol<Int>) : WasmImmediate()
|
||||
|
||||
@@ -16,7 +16,7 @@ fun WasmModule.calculateIds() {
|
||||
}
|
||||
|
||||
functionTypes.calculateIds()
|
||||
structs.calculateIds(startIndex = functionTypes.size)
|
||||
gcTypes.calculateIds(startIndex = functionTypes.size)
|
||||
importedFunctions.calculateIds()
|
||||
importedMemories.calculateIds()
|
||||
importedTables.calculateIds()
|
||||
|
||||
@@ -106,22 +106,22 @@ abstract class WasmExpressionBuilder {
|
||||
buildInstr(WasmOp.GLOBAL_SET, WasmImmediate.GlobalIdx(global))
|
||||
}
|
||||
|
||||
fun buildStructGet(struct: WasmSymbol<WasmStructDeclaration>, fieldId: WasmSymbol<Int>) {
|
||||
fun buildStructGet(struct: WasmSymbol<WasmTypeDeclaration>, fieldId: WasmSymbol<Int>) {
|
||||
buildInstr(
|
||||
WasmOp.STRUCT_GET,
|
||||
WasmImmediate.StructType(struct),
|
||||
WasmImmediate.GcType(struct),
|
||||
WasmImmediate.StructFieldIdx(fieldId)
|
||||
)
|
||||
}
|
||||
|
||||
fun buildStructNew(struct: WasmSymbol<WasmStructDeclaration>) {
|
||||
buildInstr(WasmOp.STRUCT_NEW_WITH_RTT, WasmImmediate.StructType(struct))
|
||||
fun buildStructNew(struct: WasmSymbol<WasmTypeDeclaration>) {
|
||||
buildInstr(WasmOp.STRUCT_NEW_WITH_RTT, WasmImmediate.GcType(struct))
|
||||
}
|
||||
|
||||
fun buildStructSet(struct: WasmSymbol<WasmStructDeclaration>, fieldId: WasmSymbol<Int>) {
|
||||
fun buildStructSet(struct: WasmSymbol<WasmTypeDeclaration>, fieldId: WasmSymbol<Int>) {
|
||||
buildInstr(
|
||||
WasmOp.STRUCT_SET,
|
||||
WasmImmediate.StructType(struct),
|
||||
WasmImmediate.GcType(struct),
|
||||
WasmImmediate.StructFieldIdx(fieldId)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ class WasmBinaryToIR(val b: MyByteReader) {
|
||||
val validVersion = 1u
|
||||
|
||||
val functionTypes: MutableList<WasmFunctionType> = mutableListOf()
|
||||
val structs: MutableList<WasmStructDeclaration> = mutableListOf()
|
||||
val gcTypes: MutableList<WasmTypeDeclaration> = mutableListOf()
|
||||
|
||||
val importsInOrder: MutableList<WasmNamedModuleField> = mutableListOf()
|
||||
val importedFunctions: MutableList<WasmFunction.Imported> = mutableListOf()
|
||||
@@ -80,7 +80,7 @@ class WasmBinaryToIR(val b: MyByteReader) {
|
||||
is WasmFunctionType ->
|
||||
functionTypes += type
|
||||
is WasmStructDeclaration ->
|
||||
structs += type
|
||||
gcTypes += type
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -313,7 +313,7 @@ class WasmBinaryToIR(val b: MyByteReader) {
|
||||
|
||||
return WasmModule(
|
||||
functionTypes = functionTypes,
|
||||
structs = structs,
|
||||
gcTypes = gcTypes,
|
||||
importsInOrder = importsInOrder,
|
||||
importedFunctions = importedFunctions,
|
||||
importedMemories = importedMemories,
|
||||
|
||||
@@ -21,9 +21,14 @@ class WasmIrToBinary(outputStream: OutputStream, val module: WasmModule) {
|
||||
with(module) {
|
||||
// type section
|
||||
appendSection(1u) {
|
||||
appendVectorSize(functionTypes.size + structs.size)
|
||||
appendVectorSize(functionTypes.size + gcTypes.size)
|
||||
functionTypes.forEach { appendFunctionTypeDeclaration(it) }
|
||||
structs.forEach { appendStructTypeDeclaration(it) }
|
||||
gcTypes.forEach {
|
||||
when (it) {
|
||||
is WasmStructDeclaration -> appendStructTypeDeclaration(it)
|
||||
else -> TODO("Support arrays")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// import section
|
||||
@@ -148,7 +153,7 @@ class WasmIrToBinary(outputStream: OutputStream, val module: WasmModule) {
|
||||
appendType(type)
|
||||
}
|
||||
}
|
||||
is WasmImmediate.StructType -> appendModuleFieldReference(x.value.owner)
|
||||
is WasmImmediate.GcType -> appendModuleFieldReference(x.value.owner)
|
||||
is WasmImmediate.StructFieldIdx -> b.writeVarUInt32(x.value.owner)
|
||||
is WasmImmediate.HeapType -> appendHeapType(x.value)
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ class WasmIrToText : SExpressionBuilder() {
|
||||
|
||||
is WasmImmediate.ValTypeVector -> sameLineList("result") { x.value.forEach { appendType(it) } }
|
||||
|
||||
is WasmImmediate.StructType -> appendModuleFieldReference(x.value.owner)
|
||||
is WasmImmediate.GcType -> appendModuleFieldReference(x.value.owner)
|
||||
is WasmImmediate.StructFieldIdx -> appendElement(x.value.owner.toString())
|
||||
is WasmImmediate.HeapType -> {
|
||||
appendHeapType(x.value)
|
||||
@@ -197,7 +197,14 @@ class WasmIrToText : SExpressionBuilder() {
|
||||
with(module) {
|
||||
newLineList("module") {
|
||||
functionTypes.forEach { appendFunctionTypeDeclaration(it) }
|
||||
structs.forEach { appendStructTypeDeclaration(it) }
|
||||
gcTypes.forEach {
|
||||
when (it) {
|
||||
is WasmStructDeclaration ->
|
||||
appendStructTypeDeclaration(it)
|
||||
else ->
|
||||
TODO("Support arrays")
|
||||
}
|
||||
}
|
||||
importsInOrder.forEach {
|
||||
when (it) {
|
||||
is WasmFunction.Imported -> appendImportedFunction(it)
|
||||
|
||||
Reference in New Issue
Block a user