[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
@@ -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)