diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-12-26 21:26:50 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-12-26 21:26:50 +0000 |
| commit | e89651789b217efbb060e07c022caf1573d567ba (patch) | |
| tree | 4326989273960aa2b2aed927f2b879bda4ff2eb9 /src/main/java | |
| parent | f0ee4f2251f02bdba1243a39f5c1ea639b86c4df (diff) | |
Support 64-bit memories
Diffstat (limited to 'src/main/java')
7 files changed, 74 insertions, 17 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java b/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java index e1204eb6..31c44cae 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java @@ -25,7 +25,8 @@ public class PatioToWasm { public static final String STACK_ALLOC_NAME = "malloc"; public static WasmModule generateCompilationUnit( - PatioCompilationUnit patioCompilationUnit + PatioCompilationUnit patioCompilationUnit, + WasmTarget target ) { var module = WasmModule.EMPTY.addImport(new WasmImport( "env", @@ -51,7 +52,7 @@ public class PatioToWasm { ) )); - var wasmMemoryBuilder = WasmMemoryBuilder.EMPTY; + var wasmMemoryBuilder = WasmMemoryBuilder.empty(target.indexType()); for (var patioCompilationUnitElement : patioCompilationUnit.elements()) { switch (patioCompilationUnitElement) { diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java index cf30d1c7..7d6f38bf 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java @@ -58,7 +58,10 @@ public class WasmBackend implements Backend { ShidToPatioContext.forTypeInfos(typeInfos) ); - var wasmModule = PatioToWasm.generateCompilationUnit(patioCompilationUnit); + var wasmModule = PatioToWasm.generateCompilationUnit( + patioCompilationUnit, + this.target + ); var objectPath = writeObjectFile( wasmModule, @@ -112,7 +115,10 @@ public class WasmBackend implements Backend { var patioCompilationUnit = context.toCompilationUnit(source); - var wasmModule = PatioToWasm.generateCompilationUnit(patioCompilationUnit); + var wasmModule = PatioToWasm.generateCompilationUnit( + patioCompilationUnit, + this.target + ); wasmModule = generateNativeRuntime(wasmModule); diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmMemoryBuilder.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmMemoryBuilder.java index 1f55af6c..821c203b 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmMemoryBuilder.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmMemoryBuilder.java @@ -5,20 +5,29 @@ import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpres import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI32Const; import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmMemoryAddress; import org.shedlang.compiler.backends.wasm.structure.ast.modules.*; +import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmIndexType; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmLimits; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmMemoryType; import java.util.List; public class WasmMemoryBuilder { - public static final WasmMemoryBuilder EMPTY = new WasmMemoryBuilder(OrderedPMap.empty(), 0); + public static WasmMemoryBuilder empty(WasmIndexType indexType) { + return new WasmMemoryBuilder(indexType, OrderedPMap.empty(), 0); + } private static final int PAGE_SIZE = 65536; + private final WasmIndexType indexType; private final OrderedPMap<String, Entity> entities; private final int size; - private WasmMemoryBuilder(OrderedPMap<String, Entity> entities, int size) { + private WasmMemoryBuilder( + WasmIndexType indexType, + OrderedPMap<String, Entity> entities, + int size + ) { + this.indexType = indexType; this.entities = entities; this.size = size; } @@ -32,11 +41,12 @@ public class WasmMemoryBuilder { public WasmModule toModule(WasmModule module) { var pageCount = (size + PAGE_SIZE - 1) / PAGE_SIZE; + var memoryType = new WasmMemoryType(WasmLimits.min(indexType, pageCount)); module = module .addImport(new WasmImport( "env", "__linear_memory", - new WasmImportDescriptor.Memory(new WasmMemoryType(WasmLimits.min(pageCount))) + new WasmImportDescriptor.Memory(memoryType) )); for (var entity : entities.values()) { @@ -73,6 +83,7 @@ public class WasmMemoryBuilder { } var builder = new WasmMemoryBuilder( + indexType, entities.plus(name, new Entity(name, offset, data, alignment)), offset + data.length ); diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmTarget.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmTarget.java index ac094c92..d1951297 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmTarget.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmTarget.java @@ -1,17 +1,28 @@ package org.shedlang.compiler.backends.wasm; +import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmIndexType; + import java.util.List; public enum WasmTarget { - WASM32("wasm32", List.of()), - WASM64("wasm64", List.of("-mwasm64")); + WASM32("wasm32", WasmIndexType.I32, List.of(), List.of()), + WASM64("wasm64", WasmIndexType.I64, List.of("-mwasm64"), List.of("--enable-memory64")); private final String targetName; + private final WasmIndexType indexType; private final List<String> linkerFlags; + private final List<String> wabtFlags; - WasmTarget(String name, List<String> linkerFlags) { + WasmTarget( + String name, + WasmIndexType indexType, + List<String> linkerFlags, + List<String> wabtFlags + ) { this.targetName = name; + this.indexType = indexType; this.linkerFlags = linkerFlags; + this.wabtFlags = wabtFlags; } public String targetName() { @@ -21,4 +32,12 @@ public enum WasmTarget { public List<String> linkerFlags() { return linkerFlags; } + + public WasmIndexType indexType() { + return indexType; + } + + public List<String> wabtFlags() { + return wabtFlags; + } } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmIndexType.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmIndexType.java new file mode 100644 index 00000000..9de7683e --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmIndexType.java @@ -0,0 +1,6 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.types; + +public enum WasmIndexType { + I32, + I64, +} diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmLimits.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmLimits.java index 722299b9..f0d74bfe 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmLimits.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmLimits.java @@ -2,12 +2,20 @@ package org.shedlang.compiler.backends.wasm.structure.ast.types; import java.util.OptionalInt; -public record WasmLimits(int min, OptionalInt max) { - public static WasmLimits min(int min) { - return new WasmLimits(min, OptionalInt.empty()); +public record WasmLimits(WasmIndexType indexType, int min, OptionalInt max) { + public static WasmLimits min(WasmIndexType indexType, int min) { + return new WasmLimits(indexType, min, OptionalInt.empty()); } - public static WasmLimits range(int min, int max) { - return new WasmLimits(min, OptionalInt.of(max)); + public static WasmLimits min32(int min) { + return min(WasmIndexType.I32, min); + } + + public static WasmLimits range(WasmIndexType indexType, int min, int max) { + return new WasmLimits(indexType, min, OptionalInt.of(max)); + } + + public static WasmLimits range32(int min, int max) { + return range(WasmIndexType.I32, min, max); } } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatTypes.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatTypes.java index 2ec737f5..cd21300a 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatTypes.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatTypes.java @@ -62,11 +62,17 @@ public class WasmBinaryFormatTypes { static void writeLimits(WasmLimits limits, WasmBinaryWriter writer) { if (limits.max().isPresent()) { - writer.writeByte(0x01); + writer.writeByte(switch (limits.indexType()) { + case I32 -> 0x01; + case I64 -> 0x05; + }); writer.writeUnsignedInt32(limits.min()); writer.writeUnsignedInt32(limits.max().getAsInt()); } else { - writer.writeByte(0x00); + writer.writeByte(switch (limits.indexType()) { + case I32 -> 0x00; + case I64 -> 0x04; + }); writer.writeUnsignedInt32(limits.min()); } } |
