diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-19 23:45:53 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-19 23:45:53 +0100 |
| commit | 119f7541e02913903469d6b49da6b21649047be1 (patch) | |
| tree | 2324c9731b6bdc70c9a1ff96ac6686b75630139c /src/test/java | |
| parent | 24625ebf4de88cfe3b773b5b4cdb32e212fdd66b (diff) | |
Use locals instead of temporary stack in Patio
Diffstat (limited to 'src/test/java')
7 files changed, 86 insertions, 64 deletions
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenCallTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenCallTests.java index 3c39eb11..78c1e937 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenCallTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenCallTests.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.patio; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.patio.instructions.PatioLocal; import org.shedlang.compiler.stackir.instructions.Instructions; import org.shedlang.compiler.testing.snapshots.SnapshotResolver; import org.shedlang.compiler.testing.snapshots.Snapshotter; @@ -16,7 +17,9 @@ public class PatioCodeGenCallTests { @Test public void canCallFunctionWithNoArgs(Snapshotter snapshotter) throws IOException { var instruction = Instructions.call(0, List.of()); - var context = PatioCodeGenContext.EMPTY; + var context = PatioCodeGenContext.EMPTY + .temporaryPush(new PatioLocal("other")) + .temporaryPush(new PatioLocal("receiver")); var result = PatioCodeGen.generateInstruction(instruction, context); @@ -26,7 +29,13 @@ public class PatioCodeGenCallTests { @Test public void canCallFunctionWithArgs(Snapshotter snapshotter) throws IOException { var instruction = Instructions.call(2, List.of("b", "a")); - var context = PatioCodeGenContext.EMPTY; + var context = PatioCodeGenContext.EMPTY + .temporaryPush(new PatioLocal("other")) + .temporaryPush(new PatioLocal("receiver")) + .temporaryPush(new PatioLocal("pos1")) + .temporaryPush(new PatioLocal("pos2")) + .temporaryPush(new PatioLocal("namedB")) + .temporaryPush(new PatioLocal("namedA")); var result = PatioCodeGen.generateInstruction(instruction, context); diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDiscardTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDiscardTests.java index edee4d8d..7839f4d7 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDiscardTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDiscardTests.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.patio; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.patio.instructions.PatioLocal; import org.shedlang.compiler.stackir.instructions.Instructions; import org.shedlang.compiler.testing.snapshots.SnapshotResolver; import org.shedlang.compiler.testing.snapshots.Snapshotter; @@ -15,7 +16,9 @@ public class PatioCodeGenDiscardTests { @Test public void discardInstructionGeneratesDiscardInstruction(Snapshotter snapshotter) throws IOException { var stackIr = Instructions.discard(); - var context = PatioCodeGenContext.EMPTY; + var context = PatioCodeGenContext.EMPTY + .temporaryPush(new PatioLocal("a")) + .temporaryPush(new PatioLocal("b")); var result = PatioCodeGen.generateInstruction(stackIr, context); diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLocalStoreTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLocalStoreTests.java index 94ed40bc..3fb0456d 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLocalStoreTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLocalStoreTests.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.patio; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.patio.instructions.PatioLocal; import org.shedlang.compiler.stackir.instructions.Instructions; import org.shedlang.compiler.testing.snapshots.SnapshotResolver; import org.shedlang.compiler.testing.snapshots.Snapshotter; @@ -15,7 +16,9 @@ public class PatioCodeGenLocalStoreTests { @Test public void localStoreGeneratesLocalStore(Snapshotter snapshotter) throws IOException { var stackIr = Instructions.localStore("x"); - var context = PatioCodeGenContext.EMPTY; + var context = PatioCodeGenContext.EMPTY + .temporaryPush(new PatioLocal("a")) + .temporaryPush(new PatioLocal("b")); var result = PatioCodeGen.generateInstruction(stackIr, context); diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenReturnTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenReturnTests.java index 0723459f..fe06f556 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenReturnTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenReturnTests.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.patio; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.patio.instructions.PatioLocal; import org.shedlang.compiler.stackir.instructions.Instructions; import org.shedlang.compiler.testing.snapshots.SnapshotResolver; import org.shedlang.compiler.testing.snapshots.Snapshotter; @@ -15,7 +16,9 @@ public class PatioCodeGenReturnTests { @Test public void returnInstructionGeneratesReturnInstruction(Snapshotter snapshotter) throws IOException { var stackIr = Instructions.return_(); - var context = PatioCodeGenContext.EMPTY; + var context = PatioCodeGenContext.EMPTY + .temporaryPush(new PatioLocal("a")) + .temporaryPush(new PatioLocal("b")); var result = PatioCodeGen.generateInstruction(stackIr, context); diff --git a/src/test/java/org/shedlang/compiler/patio/PatioInstructionSerializerTests.java b/src/test/java/org/shedlang/compiler/patio/PatioInstructionSerializerTests.java index 2ce7d87c..1e1a9fa6 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioInstructionSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioInstructionSerializerTests.java @@ -12,91 +12,77 @@ import static org.zwobble.precisely.Matchers.equalTo; public class PatioInstructionSerializerTests { @Test public void canSerializeAlloc() { - var instruction = Patio.alloc(42); + var instruction = Patio.alloc("x", 42); var string = serialize(instruction); - assertThat(string, equalTo("(alloc 42)")); + assertThat(string, equalTo("(alloc \"x\" 42)")); } @Test public void canSerializeCallPointer() { - var instruction = Patio.callPointer("f", List.of("a", "b", "c")); + var instruction = Patio.callPointer( + "target", + "f", + List.of("a", "b", "c") + ); var string = serialize(instruction); - assertThat(string, equalTo("(call-pointer \"f\" (\"a\" \"b\" \"c\"))")); + assertThat(string, equalTo("(call-pointer \"target\" \"f\" (\"a\" \"b\" \"c\"))")); } @Test - public void canSerializeDiscard() { - var instruction = Patio.discard(); + public void canSerializeFunctionPointer() { + var instruction = Patio.functionPointer("target", "f"); var string = serialize(instruction); - assertThat(string, equalTo("(discard)")); + assertThat(string, equalTo("(function-pointer \"target\" \"f\")")); } @Test - public void canSerializeLocalLoad() { - var instruction = Patio.localLoad("x"); + public void canSerializeInt() { + var instruction = Patio.int_("target", 42); var string = serialize(instruction); - assertThat(string, equalTo("(local-load \"x\")")); + assertThat(string, equalTo("(int \"target\" 42)")); } @Test - public void canSerializeLocalStore() { - var instruction = Patio.localStore("x"); + public void canSerializeLocalCopy() { + var instruction = Patio.localCopy("target", "x"); var string = serialize(instruction); - assertThat(string, equalTo("(local-store \"x\")")); + assertThat(string, equalTo("(local-copy \"target\" \"x\")")); } @Test public void canSerializeMemoryLoad() { - var instruction = Patio.memoryLoad(4); + var instruction = Patio.memoryLoad("target", "pointer", 4); var string = serialize(instruction); - assertThat(string, equalTo("(memory-load offset=4)")); + assertThat(string, equalTo("(memory-load \"target\" pointer=\"pointer\" offset=4)")); } @Test public void canSerializeMemoryStore() { - var instruction = Patio.memoryStore(4); + var instruction = Patio.memoryStore("pointer", 4, "value"); var string = serialize(instruction); - assertThat(string, equalTo("(memory-store offset=4)")); - } - - @Test - public void canSerializePushFunctionPointer() { - var instruction = Patio.pushFunctionPointer("f"); - - var string = serialize(instruction); - - assertThat(string, equalTo("(push-function-pointer \"f\")")); - } - - @Test - public void canSerializePushInt() { - var instruction = Patio.pushInt(42); - - var string = serialize(instruction); - - assertThat(string, equalTo("(push-int 42)")); + assertThat(string, equalTo("(memory-store pointer=\"pointer\" offset=4 value=\"value\")")); } @Test public void canSerializeReturn() { - var instruction = Patio.return_(); + var instruction = Patio.return_("value"); var string = serialize(instruction); - assertThat(string, equalTo("(return)")); + assertThat(string, equalTo("(return \"value\")")); } } diff --git a/src/test/java/org/shedlang/compiler/patio/PatioTesting.java b/src/test/java/org/shedlang/compiler/patio/PatioTesting.java index 0a4387c4..5a0a4670 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioTesting.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioTesting.java @@ -7,10 +7,20 @@ class PatioTesting { static String serialize(PatioCodeGenContext context) { var builder = new StringBuilder(); + + builder.append("Instructions:\n\n"); PatioInstructionSerializer.serializeInstructions( context.functionContext().instructions(), builder ); + + builder.append("\n\nTemporary stack:\n"); + for (var value : context.functionContext().temporaryStack()) { + builder.append(" * "); + builder.append(value.name()); + builder.append("\n"); + } + return builder.toString(); } diff --git a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java index 2115fd06..05942dda 100644 --- a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java +++ b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java @@ -7,15 +7,17 @@ import java.util.List; public class Patio { private Patio() {} - public static PatioInstruction alloc(int bytes) { - return new PatioAlloc(bytes, Sources.NULL); + public static PatioInstruction alloc(String target, int bytes) { + return new PatioAlloc(new PatioLocal(target), bytes, Sources.NULL); } public static PatioInstruction callPointer( + String target, String functionPointer, List<String> args ) { return new PatioCallPointer( + new PatioLocal(target), new PatioLocal(functionPointer), args.stream() .map(arg -> new PatioLocal(arg)) @@ -24,35 +26,41 @@ public class Patio { ); } - public static PatioInstruction discard() { - return new PatioDiscard(Sources.NULL); + public static PatioInstruction functionPointer(String target, String name) { + return new PatioFunctionPointer(new PatioLocal(target), name, Sources.NULL); } - public static PatioInstruction localLoad(String name) { - return new PatioLocalLoad(new PatioLocal(name), Sources.NULL); + public static PatioInstruction int_(String target, int value) { + return new PatioInt(new PatioLocal(target), value, Sources.NULL); } - public static PatioInstruction localStore(String name) { - return new PatioLocalStore(new PatioLocal(name), Sources.NULL); - } - - public static PatioInstruction memoryLoad(int offset) { - return new PatioMemoryLoad(offset, Sources.NULL); - } - - public static PatioInstruction memoryStore(int offset) { - return new PatioMemoryStore(offset, Sources.NULL); + public static PatioInstruction localCopy(String target, String value) { + return new PatioLocalCopy( + new PatioLocal(target), + new PatioLocal(value), + Sources.NULL + ); } - public static PatioInstruction pushFunctionPointer(String name) { - return new PatioPushFunctionPointer(name, Sources.NULL); + public static PatioInstruction memoryLoad(String target, String pointer, int offset) { + return new PatioMemoryLoad( + new PatioLocal(target), + new PatioLocal(pointer), + offset, + Sources.NULL + ); } - public static PatioInstruction pushInt(int value) { - return new PatioPushInt(value, Sources.NULL); + public static PatioInstruction memoryStore(String pointer, int offset, String value) { + return new PatioMemoryStore( + new PatioLocal(pointer), + offset, + new PatioLocal(value), + Sources.NULL + ); } - public static PatioInstruction return_() { - return new PatioReturn(Sources.NULL); + public static PatioInstruction return_(String value) { + return new PatioReturn(new PatioLocal(value), Sources.NULL); } } |
