[Wasm] Move non-recursive function types out of rec group

This fixes linking with other wasm modules with non-rec types
This commit is contained in:
Svyatoslav Kuzmich
2022-12-21 17:16:32 +01:00
committed by teamcity
parent 88f1f74aec
commit d788adcbb5
9 changed files with 83 additions and 29 deletions
@@ -10,9 +10,7 @@ import org.jetbrains.kotlin.wasm.ir.source.location.SourceLocation
class WasmModule(
val functionTypes: List<WasmFunctionType> = emptyList(),
val gcTypes: List<WasmTypeDeclaration> = emptyList(),
val gcTypesInRecursiveGroup: Boolean,
val recGroupTypes: List<WasmTypeDeclaration> = emptyList(),
val importsInOrder: List<WasmNamedModuleField> = emptyList(),
val importedFunctions: List<WasmFunction.Imported> = emptyList(),
val importedMemories: List<WasmMemory> = emptyList(),
@@ -77,3 +77,15 @@ fun WasmType.getHeapType(): WasmHeapType =
is WasmExternRef -> WasmHeapType.Simple.Extern
else -> error("Unknown heap type for type $this")
}
fun WasmFunctionType.referencesTypeDeclarations(): Boolean =
parameterTypes.any { it.referencesTypeDeclaration() } or resultTypes.any { it.referencesTypeDeclaration() }
fun WasmType.referencesTypeDeclaration(): Boolean {
val heapType = when (this) {
is WasmRefNullType -> getHeapType()
is WasmRefType -> getHeapType()
else -> return false
}
return heapType is WasmHeapType.Type
}
@@ -16,7 +16,7 @@ fun WasmModule.calculateIds() {
}
functionTypes.calculateIds()
gcTypes.calculateIds(startIndex = functionTypes.size)
recGroupTypes.calculateIds(startIndex = functionTypes.size)
importedFunctions.calculateIds()
importedMemories.calculateIds()
importedTables.calculateIds()
@@ -329,8 +329,7 @@ class WasmBinaryToIR(val b: MyByteReader) {
return WasmModule(
functionTypes = functionTypes,
gcTypes = gcTypes,
gcTypesInRecursiveGroup = false,
recGroupTypes = gcTypes,
importsInOrder = importsInOrder,
importedFunctions = importedFunctions,
importedMemories = importedMemories,
@@ -35,17 +35,18 @@ class WasmIrToBinary(
with(module) {
// type section
appendSection(1u) {
if (module.gcTypesInRecursiveGroup) {
appendVectorSize(1)
b.writeByte(0x4f)
}
appendVectorSize(functionTypes.size + gcTypes.size)
val numRecGroups = if (recGroupTypes.isEmpty()) 0 else 1
appendVectorSize(functionTypes.size + numRecGroups)
functionTypes.forEach { appendFunctionTypeDeclaration(it) }
gcTypes.forEach {
when (it) {
is WasmStructDeclaration -> appendStructTypeDeclaration(it)
is WasmArrayDeclaration -> appendArrayTypeDeclaration(it)
is WasmFunctionType -> error("Function type in GC types")
if (!recGroupTypes.isEmpty()) {
b.writeByte(0x4f)
appendVectorSize(recGroupTypes.size)
recGroupTypes.forEach {
when (it) {
is WasmStructDeclaration -> appendStructTypeDeclaration(it)
is WasmArrayDeclaration -> appendArrayTypeDeclaration(it)
is WasmFunctionType -> appendFunctionTypeDeclaration(it)
}
}
}
}
@@ -172,8 +173,8 @@ class WasmIrToBinary(
// https://github.com/WebAssembly/extended-name-section/blob/main/document/core/appendix/custom.rst
appendSection(4u) {
appendVectorSize(module.gcTypes.size)
module.gcTypes.forEach {
appendVectorSize(module.recGroupTypes.size)
module.recGroupTypes.forEach {
appendModuleFieldReference(it)
b.writeString(it.name)
}
@@ -190,7 +191,7 @@ class WasmIrToBinary(
// Experimental fields name section
// https://github.com/WebAssembly/gc/issues/193
appendSection(10u) {
val structDeclarations = module.gcTypes.filterIsInstance<WasmStructDeclaration>()
val structDeclarations = module.recGroupTypes.filterIsInstance<WasmStructDeclaration>()
appendVectorSize(structDeclarations.size)
structDeclarations.forEach {
appendModuleFieldReference(it)
@@ -228,13 +228,14 @@ class WasmIrToText : SExpressionBuilder() {
with(module) {
newLineList("module") {
functionTypes.forEach { appendFunctionTypeDeclaration(it) }
gcTypes.forEach {
recGroupTypes.forEach {
when (it) {
is WasmStructDeclaration ->
appendStructTypeDeclaration(it)
is WasmArrayDeclaration ->
appendArrayTypeDeclaration(it)
else -> error("Unexpected GC type: $it")
is WasmFunctionType ->
appendFunctionTypeDeclaration(it)
}
}
importsInOrder.forEach {