diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-25 15:30:42 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-25 15:30:42 +0100 |
| commit | 9a60f58e4ba7889f87561945fa474adfcdbd46a5 (patch) | |
| tree | 7b81ce9421fa48d30c5d87f1f2d2d2d6358b5c2d /src/main/java | |
| parent | 5d987fd53baf2dd6632cc6db8d7acea7633f6780 (diff) | |
Support data symbols in WASM binary format
Diffstat (limited to 'src/main/java')
4 files changed, 45 insertions, 2 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmDataSegmentIndex.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmDataSegmentIndex.java new file mode 100644 index 00000000..80124ae9 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmDataSegmentIndex.java @@ -0,0 +1,4 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.linking; + +public record WasmDataSegmentIndex(int value) { +} diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmDataSymbol.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmDataSymbol.java new file mode 100644 index 00000000..864d46e9 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmDataSymbol.java @@ -0,0 +1,9 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.linking; + +public record WasmDataSymbol( + String name, + WasmDataSegmentIndex dataSegmentIndex, + int offset, + int size +) { +} diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmDataSegment.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmDataSegment.java index 7227f54b..911a6a01 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmDataSegment.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmDataSegment.java @@ -1,11 +1,15 @@ package org.shedlang.compiler.backends.wasm.structure.ast.modules; import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpression; +import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmDataSymbol; + +import java.util.List; public record WasmDataSegment( String name, byte[] init, WasmExpression offset, - WasmAlignment alignment + WasmAlignment alignment, + List<WasmDataSymbol> symbols ) { } 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 3cb75953..ecc06f71 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,6 +1,8 @@ package org.shedlang.compiler.backends.wasm.structure.binary; import org.shedlang.compiler.backends.wasm.structure.BinaryWriter; +import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmDataSegmentIndex; +import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmDataSymbol; import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmInitFunc; import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmSymbolIndex; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmAlignment; @@ -136,6 +138,14 @@ public class WasmBinaryFormatLinking { writeFuncIndex(function.funcIndex, writer); writeString(function.name(), writer); } + case SymbolInfo.Data data -> { + writer.writeByte(SymbolType.DATA.id); + writeFlags(data.flags, writer); + writeString(data.dataSymbol.name(), writer); + writeDataSegmentIndex(data.dataSymbol.dataSegmentIndex(), writer); + Leb128Encoding.encodeUnsignedInt32(data.dataSymbol.offset(), writer); + Leb128Encoding.encodeUnsignedInt32(data.dataSymbol.size(), writer); + } case SymbolInfo.Global global -> { writer.writeByte(SymbolType.GLOBAL.id); writeFlags(global.flags, writer); @@ -192,6 +202,12 @@ public class WasmBinaryFormatLinking { ); }); + var dataSymbols = module.dataSegments().stream() + .flatMap(dataSegment -> + dataSegment.symbols().stream() + .map(dataSymbol -> new SymbolInfo.Data(List.of(), dataSymbol)) + ); + var globalSymbols = module.indexedGlobals().stream() .map(global -> { var flags = switch (global) { @@ -208,7 +224,10 @@ public class WasmBinaryFormatLinking { ); }); - var symbolInfos = Stream.<SymbolInfo>concat(functionSymbols, globalSymbols).toList(); + var symbolInfos = Stream.<Stream<? extends SymbolInfo>>of(functionSymbols, dataSymbols, globalSymbols) + .<SymbolInfo>flatMap(x -> x) + .toList(); + return new SymbolTable(symbolInfos, functionSymbolIndexOffset); } @@ -218,6 +237,9 @@ public class WasmBinaryFormatLinking { record Global(List<SymbolFlag> flags, WasmGlobalIndex globalIndex, String name) implements SymbolInfo { } + + record Data(List<SymbolFlag> flags, WasmDataSymbol dataSymbol) implements SymbolInfo { + } } private enum SymbolFlag { @@ -250,6 +272,10 @@ public class WasmBinaryFormatLinking { Leb128Encoding.encodeUnsignedInt32(flagsToInt(flags), writer); } + private static void writeDataSegmentIndex(WasmDataSegmentIndex dataSegmentIndex, BinaryWriter writer) { + Leb128Encoding.encodeUnsignedInt32(dataSegmentIndex.value(), writer); + } + private static void writeSymbolIndex(WasmSymbolIndex symbolIndex, BinaryWriter writer) { Leb128Encoding.encodeUnsignedInt32(symbolIndex.value(), writer); } |
