[Wasm] Refactoring: replace "struct types" with "GC types"

In preparation for adding array types
This commit is contained in:
Svyatoslav Kuzmich
2020-12-05 18:18:39 +03:00
parent 4bb163fd1f
commit d15af70a3e
14 changed files with 46 additions and 35 deletions
@@ -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)
@@ -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)
@@ -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()
@@ -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>
@@ -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>(),
@@ -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)
@@ -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> =