From ce407cc74ec827da9fb440d4601b21cad966bc95 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 26 Jun 2023 19:47:20 +0100 Subject: Extract separate WasmMemoryAddress as non-instruction --- .../org/shedlang/compiler/backends/wasm/WasmCodeGen.java | 2 +- .../compiler/backends/wasm/WasmCodeGenContext.java | 2 +- .../compiler/backends/wasm/WasmCodeGenStrings.java | 2 +- .../shedlang/compiler/backends/wasm/WasmMemoryBuilder.java | 2 +- .../wasm/structure/ast/instructions/WasmInstruction.java | 2 +- .../wasm/structure/ast/instructions/WasmMemoryAddress.java | 14 -------------- .../structure/ast/instructions/WasmMemoryAddressConst.java | 13 +++++++++++++ .../wasm/structure/ast/linking/WasmMemoryAddress.java | 7 +++++++ .../structure/binary/WasmBinaryFormatInstructions.java | 4 ++-- .../wasm/structure/binary/WasmBinaryFormatLinking.java | 6 +----- .../backends/wasm/structure/binary/WasmBinaryWriter.java | 11 +++++++++-- .../wasm/structure/binary/WasmRelocationEntry.java | 2 +- 12 files changed, 38 insertions(+), 29 deletions(-) delete mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmMemoryAddress.java create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmMemoryAddressConst.java create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/linking/WasmMemoryAddress.java (limited to 'src/main/java') 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/WasmMemoryAddress.java deleted file mode 100644 index 704f7f16..00000000 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmMemoryAddress.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.shedlang.compiler.backends.wasm.structure.ast.instructions; - -import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmDataSegmentIndex; - -/** - * 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 -) implements WasmInstruction { -} diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmMemoryAddressConst.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmMemoryAddressConst.java new file mode 100644 index 00000000..74b8816d --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmMemoryAddressConst.java @@ -0,0 +1,13 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.instructions; + +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 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, -- cgit v1.2.3