aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-03-08 14:46:09 +0000
committerMichael Williamson <mike@zwobble.org>2025-03-08 14:46:23 +0000
commitcccc61da54776d13fdb1655631e9ae3b81dd2ab3 (patch)
treec440648b21c87588c24240d03a7aee67fe5c277d /src/main/java
parent8cfdfc92fcf41f5db6d30015b20457ca64f0c3ce (diff)
Support undefined data elements in Wasm binary format
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java50
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatLinking.java24
2 files changed, 63 insertions, 11 deletions
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<WasmDataSegment> dataSegments,
PVector<WasmImport> imports,
PVector<WasmInitFunc> initFuncs,
- PVector<WasmTag> tags
+ PVector<WasmTag> tags,
+ PVector<String> 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<SymbolFlag> flags,
+ String name
+ ) implements SymbolInfo {
+ }
+
record Global(List<SymbolFlag> flags, WasmGlobalIndex globalIndex, String name) implements SymbolInfo {
}