From cccc61da54776d13fdb1655631e9ae3b81dd2ab3 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 8 Mar 2025 14:46:09 +0000 Subject: Support undefined data elements in Wasm binary format --- .../wasm/structure/ast/modules/WasmModule.java | 50 +++++++++++++++++----- .../structure/binary/WasmBinaryFormatLinking.java | 24 +++++++++++ 2 files changed, 63 insertions(+), 11 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java index 7fc9e9f9..67aef746 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java @@ -19,7 +19,8 @@ public record WasmModule( PVector dataSegments, PVector imports, PVector initFuncs, - PVector tags + PVector tags, + PVector undefinedDataElementNames ) { public static final WasmModule EMPTY = new WasmModule( P.orderedSet(), @@ -31,6 +32,7 @@ public record WasmModule( P.vector(), P.vector(), P.vector(), + P.vector(), P.vector() ); @@ -170,7 +172,8 @@ public record WasmModule( dataSegments, imports, initFuncs, - tags + tags, + this.undefinedDataElementNames ); } @@ -185,7 +188,8 @@ public record WasmModule( dataSegments, imports, initFuncs, - tags + tags, + this.undefinedDataElementNames ); } @@ -200,7 +204,8 @@ public record WasmModule( dataSegments, imports, initFuncs, - tags + tags, + this.undefinedDataElementNames ); } @@ -215,7 +220,8 @@ public record WasmModule( dataSegments, imports, initFuncs, - tags + tags, + this.undefinedDataElementNames ); } @@ -230,7 +236,8 @@ public record WasmModule( dataSegments, imports, initFuncs, - tags + tags, + this.undefinedDataElementNames ); } @@ -245,7 +252,8 @@ public record WasmModule( dataSegments, imports, initFuncs, - tags + tags, + this.undefinedDataElementNames ); } @@ -260,7 +268,8 @@ public record WasmModule( dataSegments.plus(dataSegment), imports, initFuncs, - tags + tags, + this.undefinedDataElementNames ); } @@ -282,7 +291,8 @@ public record WasmModule( dataSegments, newImports, initFuncs, - tags + tags, + this.undefinedDataElementNames ); } @@ -297,7 +307,8 @@ public record WasmModule( dataSegments, imports, initFuncs.plus(initFunc), - tags + tags, + this.undefinedDataElementNames ); } @@ -312,7 +323,24 @@ public record WasmModule( dataSegments, imports, initFuncs, - tags.plus(tag) + tags.plus(tag), + this.undefinedDataElementNames + ); + } + + public WasmModule addUndefinedDataElement(String name) { + return new WasmModule( + types, + functions, + tables, + memories, + globals, + elementSegments, + dataSegments, + imports, + initFuncs, + tags, + this.undefinedDataElementNames.plus(name) ); } } 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 6b8ff4ca..d874dda8 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 @@ -209,6 +209,11 @@ public class WasmBinaryFormatLinking { writer.writeUnsignedInt32(data.offset()); writer.writeUnsignedInt32(data.size()); } + case SymbolInfo.DataUndefined dataUndefined -> { + writer.writeByte(SymbolType.DATA.id); + writeFlags(dataUndefined.flags, writer); + writer.writeString(dataUndefined.name()); + } case SymbolInfo.Global global -> { writer.writeByte(SymbolType.GLOBAL.id); writeFlags(global.flags, writer); @@ -442,6 +447,19 @@ public class WasmBinaryFormatLinking { tableNameToSymbolIndex.put(table.name(), symbolIndex); } + for (var undefinedDataElementName : module.undefinedDataElementNames()) { + var symbolInfo = new SymbolInfo.DataUndefined( + List.of(SymbolFlag.UNDEFINED), + undefinedDataElementName + ); + var symbolIndex = new WasmSymbolIndex(symbolInfos.size()); + symbolInfos.add(symbolInfo); + memoryAddressToSymbolIndex.put( + new WasmMemoryAddress(undefinedDataElementName), + symbolIndex + ); + } + return new SymbolTable( symbolInfos, memoryAddressToSymbolIndex, @@ -466,6 +484,12 @@ public class WasmBinaryFormatLinking { ) implements SymbolInfo { } + record DataUndefined( + List flags, + String name + ) implements SymbolInfo { + } + record Global(List flags, WasmGlobalIndex globalIndex, String name) implements SymbolInfo { } -- cgit v1.2.3