diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-27 19:19:19 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-27 19:19:19 +0100 |
| commit | fbe1a70809a9a6860b1b61f254855302924f7475 (patch) | |
| tree | c12280f6a5d1919f5d5a8155127ffcac91963079 /src/main/java | |
| parent | 20c96c0faeef62752b6b42f751a366d6f4b25bb0 (diff) | |
Reference init funcs by name
Diffstat (limited to 'src/main/java')
4 files changed, 12 insertions, 23 deletions
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<SymbolInfo> symbolInfos; - private final int functionSymbolIndexOffset; private final Map<WasmMemoryAddress, WasmSymbolIndex> memoryAddressToSymbolIndex; private final Map<WasmFuncType, WasmTypeIndex> funcTypeToTypeIndex; private final Map<String, WasmSymbolIndex> funcNameToSymbolIndex; public SymbolTable( List<SymbolInfo> symbolInfos, - int functionSymbolIndexOffset, Map<WasmMemoryAddress, WasmSymbolIndex> memoryAddressToSymbolIndex, Map<WasmFuncType, WasmTypeIndex> funcTypeToTypeIndex, Map<String, WasmSymbolIndex> 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<SymbolInfo>(); var memoryAddressToSymbolIndex = new HashMap<WasmMemoryAddress, WasmSymbolIndex>(); - var functionSymbolIndexOffset = 0; var funcNameToSymbolIndex = new HashMap<String, WasmSymbolIndex>(); 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) ); } |
