[Wasm][Stdlib] Add public APIs for linear memory access

Needed for interop with APIs that use linear memory.
This commit is contained in:
Svyatoslav Kuzmich
2022-12-29 19:25:23 +00:00
committed by Space Team
parent fbf06b5495
commit 9bc6b420a9
20 changed files with 702 additions and 56 deletions
@@ -69,9 +69,7 @@ sealed class WasmImmediate {
class ValTypeVector(val value: List<WasmType>) : WasmImmediate()
class MemoryIdx(val value: WasmSymbol<WasmMemory>) : WasmImmediate() {
constructor(value: WasmMemory) : this(WasmSymbol(value))
}
class MemoryIdx(val value: Int) : WasmImmediate()
class DataIdx(val value: WasmSymbol<Int>) : WasmImmediate() {
constructor(value: Int) : this(WasmSymbol(value))
@@ -425,7 +425,7 @@ class WasmBinaryToIR(val b: MyByteReader) {
WasmImmediateKind.LOCAL_IDX -> WasmImmediate.LocalIdx(locals[b.readVarUInt32AsInt()])
WasmImmediateKind.GLOBAL_IDX -> WasmImmediate.GlobalIdx(globalByIdx(b.readVarUInt32AsInt()))
WasmImmediateKind.TYPE_IDX -> WasmImmediate.TypeIdx(functionTypes[b.readVarUInt32AsInt()])
WasmImmediateKind.MEMORY_IDX -> WasmImmediate.MemoryIdx(memoryByIdx(b.readVarUInt32AsInt()))
WasmImmediateKind.MEMORY_IDX -> WasmImmediate.MemoryIdx(b.readVarUInt32AsInt())
WasmImmediateKind.DATA_IDX -> WasmImmediate.DataIdx(b.readVarUInt32AsInt())
WasmImmediateKind.TABLE_IDX -> WasmImmediate.TableIdx(b.readVarUInt32AsInt())
WasmImmediateKind.LABEL_IDX -> WasmImmediate.LabelIdx(b.readVarUInt32AsInt())
@@ -243,7 +243,7 @@ class WasmIrToBinary(
is WasmImmediate.LocalIdx -> appendLocalReference(x.value.owner)
is WasmImmediate.GlobalIdx -> appendModuleFieldReference(x.value.owner)
is WasmImmediate.TypeIdx -> appendModuleFieldReference(x.value.owner)
is WasmImmediate.MemoryIdx -> appendModuleFieldReference(x.value.owner)
is WasmImmediate.MemoryIdx -> b.writeVarUInt32(x.value)
is WasmImmediate.DataIdx -> b.writeVarUInt32(x.value.owner)
is WasmImmediate.TableIdx -> b.writeVarUInt32(x.value.owner)
is WasmImmediate.LabelIdx -> b.writeVarUInt32(x.value)
@@ -124,7 +124,7 @@ class WasmIrToText : SExpressionBuilder() {
is WasmImmediate.LocalIdx -> appendLocalReference(x.value.owner)
is WasmImmediate.GlobalIdx -> appendModuleFieldReference(x.value.owner)
is WasmImmediate.TypeIdx -> sameLineList("type") { appendModuleFieldReference(x.value.owner) }
is WasmImmediate.MemoryIdx -> appendModuleFieldIdIfNotNull(x.value.owner)
is WasmImmediate.MemoryIdx -> appendIdxIfNotZero(x.value)
is WasmImmediate.DataIdx -> appendElement(x.value.toString())
is WasmImmediate.TableIdx -> appendElement(x.value.toString())
is WasmImmediate.LabelIdx -> appendElement(x.value.toString())
@@ -523,9 +523,7 @@ class WasmIrToText : SExpressionBuilder() {
appendElement("$${local.id}_${sanitizeWatIdentifier(local.name)}")
}
fun appendModuleFieldIdIfNotNull(field: WasmNamedModuleField) {
val id = field.id
?: error("${field::class} ${field.name} ID is unlinked")
fun appendIdxIfNotZero(id: Int) {
if (id != 0) appendElement(id.toString())
}