From fbe1a70809a9a6860b1b61f254855302924f7475 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 27 Jun 2023 19:19:19 +0100 Subject: Reference init funcs by name --- .../wasm/structure/ast/linking/WasmInitFunc.java | 4 ++-- .../ast/linking/WasmRelocatableIndex.java | 3 ++- .../structure/binary/WasmBinaryFormatLinking.java | 26 ++++++---------------- .../wasm/structure/binary/WasmBinaryWriter.java | 2 +- 4 files changed, 12 insertions(+), 23 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmInitFunc.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmInitFunc.java index 1613a96f..1c84e84f 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmInitFunc.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmInitFunc.java @@ -1,9 +1,9 @@ package org.shedlang.compiler.backends.wasm.structure.ast.linking; -import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncIndex; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncName; public record WasmInitFunc( int priority, - WasmFuncIndex funcIndex + WasmFuncName funcName ) { } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmRelocatableIndex.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmRelocatableIndex.java index 90989b14..b5035b34 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmRelocatableIndex.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmRelocatableIndex.java @@ -1,9 +1,10 @@ package org.shedlang.compiler.backends.wasm.structure.ast.linking; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncName; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; public sealed interface WasmRelocatableIndex { - record FuncIndex(String name) implements WasmRelocatableIndex {} + record FuncIndex(WasmFuncName funcName) implements WasmRelocatableIndex {} record MemoryAddress(WasmMemoryAddress address) implements WasmRelocatableIndex {} record TypeIndex(WasmFuncType funcType) implements WasmRelocatableIndex {} } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatLinking.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatLinking.java index 5e8a9425..68a9dfed 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatLinking.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatLinking.java @@ -1,10 +1,7 @@ package org.shedlang.compiler.backends.wasm.structure.binary; import org.shedlang.compiler.backends.wasm.structure.ast.linking.*; -import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncIndex; -import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmGlobalIndex; -import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; -import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmTypeIndex; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.*; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; import java.io.IOException; @@ -70,7 +67,7 @@ public class WasmBinaryFormatLinking { ) { var index = switch (relocationEntry.index()) { case WasmRelocatableIndex.FuncIndex funcIndex -> - symbolTable.funcNameToFuncIndex(funcIndex.name()).value(); + symbolTable.funcNameToSymbolIndex(funcIndex.funcName()).value(); case WasmRelocatableIndex.MemoryAddress memoryAddress -> symbolTable.memoryAddressToSymbolIndex(memoryAddress.address()).value(); @@ -169,7 +166,7 @@ public class WasmBinaryFormatLinking { WasmBinaryWriter writer ) { writer.writeUnsignedInt32(initFunc.priority()); - var symbolIndex = symbolTable.funcIndexToSymbolIndex(initFunc.funcIndex()); + var symbolIndex = symbolTable.funcNameToSymbolIndex(initFunc.funcName()); writeSymbolIndex(symbolIndex, writer); } @@ -229,29 +226,22 @@ public class WasmBinaryFormatLinking { private static class SymbolTable { private final List symbolInfos; - private final int functionSymbolIndexOffset; private final Map memoryAddressToSymbolIndex; private final Map funcTypeToTypeIndex; private final Map funcNameToSymbolIndex; public SymbolTable( List symbolInfos, - int functionSymbolIndexOffset, Map memoryAddressToSymbolIndex, Map funcTypeToTypeIndex, Map funcNameToSymbolIndex ) { this.symbolInfos = symbolInfos; - this.functionSymbolIndexOffset = functionSymbolIndexOffset; this.memoryAddressToSymbolIndex = memoryAddressToSymbolIndex; this.funcTypeToTypeIndex = funcTypeToTypeIndex; this.funcNameToSymbolIndex = funcNameToSymbolIndex; } - public WasmSymbolIndex funcIndexToSymbolIndex(WasmFuncIndex funcIndex) { - return new WasmSymbolIndex(funcIndex.value() + functionSymbolIndexOffset); - } - public WasmSymbolIndex memoryAddressToSymbolIndex(WasmMemoryAddress memoryAddress) { var symbolIndex = memoryAddressToSymbolIndex.get(memoryAddress); if (symbolIndex == null) { @@ -272,13 +262,13 @@ public class WasmBinaryFormatLinking { } } - public WasmSymbolIndex funcNameToFuncIndex(String name) { - var syjmbolIndex = funcNameToSymbolIndex.get(name); - if (syjmbolIndex == null) { + public WasmSymbolIndex funcNameToSymbolIndex(WasmFuncName name) { + var symbolIndex = funcNameToSymbolIndex.get(name.value()); + if (symbolIndex == null) { // TODO: better error throw new RuntimeException("could not find index for func: " + name); } else { - return syjmbolIndex; + return symbolIndex; } } } @@ -287,7 +277,6 @@ public class WasmBinaryFormatLinking { var symbolInfos = new ArrayList(); var memoryAddressToSymbolIndex = new HashMap(); - var functionSymbolIndexOffset = 0; var funcNameToSymbolIndex = new HashMap(); for (var function : module.indexedFunctions()) { var flags = switch (function) { @@ -345,7 +334,6 @@ public class WasmBinaryFormatLinking { return new SymbolTable( symbolInfos, - functionSymbolIndexOffset, memoryAddressToSymbolIndex, funcTypeToTypeIndex, funcNameToSymbolIndex diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriter.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriter.java index b3edd8f7..507600f9 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriter.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriter.java @@ -58,7 +58,7 @@ public class WasmBinaryWriter { writeRelocatableIndex( WasmRelocationType.FUNCTION_INDEX_LEB, () -> writePaddedUnsignedInt32(funcIndex.value()), - new WasmRelocatableIndex.FuncIndex(funcName.value()) + new WasmRelocatableIndex.FuncIndex(funcName) ); } -- cgit v1.2.3