aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-26 19:47:20 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-26 19:47:20 +0100
commitce407cc74ec827da9fb440d4601b21cad966bc95 (patch)
treebcc90e941d7da717fa698b5ab42f541ce197c997 /src/main/java
parenteff16c83d2e3b7c22d071eee55118910b6b6941d (diff)
Extract separate WasmMemoryAddress as non-instruction
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java2
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java2
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenStrings.java2
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmMemoryBuilder.java2
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java2
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmMemoryAddressConst.java (renamed from src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmMemoryAddress.java)7
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmMemoryAddress.java7
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java4
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatLinking.java6
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriter.java11
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmRelocationEntry.java2
11 files changed, 28 insertions, 19 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
index 8e8c2735..6ab555a7 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
@@ -42,7 +42,7 @@ public class WasmCodeGen {
var offset = defineStringResult.value();
context = defineStringResult.context();
- yield context.addInstruction(offset);
+ yield context.addInstruction(new WasmMemoryAddressConst(offset));
}
case PushUnitInstruction pushUnit ->
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java
index 46756088..7dfb13c2 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java
@@ -5,9 +5,9 @@ import org.pcollections.OrderedPMap;
import org.pcollections.PMap;
import org.pcollections.PVector;
import org.shedlang.compiler.backends.llvm.NameAllocator;
-import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmMemoryAddress;
import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpression;
import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmInstruction;
+import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmMemoryAddress;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.*;
import java.util.List;
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenStrings.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenStrings.java
index acf29b4d..3cd51098 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenStrings.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenStrings.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.backends.wasm;
-import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmMemoryAddress;
+import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmMemoryAddress;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmAlignment;
import java.nio.ByteBuffer;
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 d3dc728c..76a234d7 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmMemoryBuilder.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmMemoryBuilder.java
@@ -2,11 +2,11 @@ package org.shedlang.compiler.backends.wasm;
import org.pcollections.OrderedPMap;
import org.shedlang.compiler.backends.llvm.NameAllocator;
-import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmMemoryAddress;
import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpression;
import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI32Const;
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.WasmMemoryAddress;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmAlignment;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmDataSegment;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmMemory;
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java
index 4e1cc958..bc70f9c2 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java
@@ -1,4 +1,4 @@
package org.shedlang.compiler.backends.wasm.structure.ast.instructions;
-public sealed interface WasmInstruction permits WasmDrop, WasmI32Const, WasmI64Const, WasmLocalGet, WasmLocalSet, WasmMemoryAddress, WasmReturn {
+public sealed interface WasmInstruction permits WasmDrop, WasmI32Const, WasmI64Const, WasmLocalGet, WasmLocalSet, WasmMemoryAddressConst, WasmReturn {
}
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmMemoryAddress.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmMemoryAddressConst.java
index 704f7f16..74b8816d 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmMemoryAddress.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmMemoryAddressConst.java
@@ -1,14 +1,13 @@
package org.shedlang.compiler.backends.wasm.structure.ast.instructions;
-import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmDataSegmentIndex;
+import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmMemoryAddress;
/**
* This is a fake instruction that corresponds to an i32.const instruction for
* an offset into a data segment. Encapsulating this in its own instruction
* avoids having to separately track relocation.
*/
-public record WasmMemoryAddress(
- WasmDataSegmentIndex dataSegmentIndex,
- int dataSegmentOffset
+public record WasmMemoryAddressConst(
+ WasmMemoryAddress address
) implements WasmInstruction {
}
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmMemoryAddress.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmMemoryAddress.java
new file mode 100644
index 00000000..263d67b5
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmMemoryAddress.java
@@ -0,0 +1,7 @@
+package org.shedlang.compiler.backends.wasm.structure.ast.linking;
+
+public record WasmMemoryAddress(
+ WasmDataSegmentIndex dataSegmentIndex,
+ int dataSegmentOffset
+) {
+}
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java
index f58c396a..6eaf7093 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java
@@ -55,9 +55,9 @@ public class WasmBinaryFormatInstructions {
// == Relocatable Instructions ==
- case WasmMemoryAddress memoryAddress -> {
+ case WasmMemoryAddressConst memoryAddressConst -> {
writer.writeByte(0x41);
- writer.writeRelocatableUnsignedInt32(WasmRelocationType.MEMORY_ADDR_SLEB, memoryAddress);
+ writer.writeMemoryAddressUnsignedInt32(memoryAddressConst.address());
}
}
}
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 224508ec..0f8aa696 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,6 @@
package org.shedlang.compiler.backends.wasm.structure.binary;
-import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmMemoryAddress;
-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.linking.*;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmAlignment;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncIndex;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmGlobalIndex;
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 0857f4f7..92985baf 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
@@ -1,7 +1,7 @@
package org.shedlang.compiler.backends.wasm.structure.binary;
import org.shedlang.compiler.backends.wasm.structure.BinaryWriter;
-import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmMemoryAddress;
+import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmMemoryAddress;
import java.io.IOException;
import java.util.*;
@@ -28,7 +28,14 @@ public class WasmBinaryWriter {
Leb128Encoding.encodePaddedUnsignedInt32(value, writer);
}
- public void writeRelocatableUnsignedInt32(WasmRelocationType relocationType, WasmMemoryAddress index) {
+ public void writeMemoryAddressUnsignedInt32(WasmMemoryAddress index) {
+ writeRelocatableUnsignedInt32(
+ WasmRelocationType.MEMORY_ADDR_SLEB,
+ index
+ );
+ }
+
+ private void writeRelocatableUnsignedInt32(WasmRelocationType relocationType, WasmMemoryAddress index) {
// TODO: handle relocatable values in non-CODE sections
var sectionContentsOffset = writer.position() - sectionContentsStart;
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmRelocationEntry.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmRelocationEntry.java
index fa854748..287907a3 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmRelocationEntry.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmRelocationEntry.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.backends.wasm.structure.binary;
-import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmMemoryAddress;
+import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmMemoryAddress;
public record WasmRelocationEntry(
int sectionIndex,