From 28bab9378c84646b274efabd054f388125514781 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 2 Feb 2025 19:40:59 +0000 Subject: Add Shid int-mul instruction --- .../org/shedlang/compiler/shid/ShidSerializer.java | 3 +++ .../shid/instructions/ShidInstruction.java | 2 +- .../shid/instructions/ShidIntMultiply.java | 14 +++++++++++ .../shedlang/compiler/shidtopatio/ShidToPatio.java | 13 +++++++++++ .../compiler/shidtopatio/ShidToPatioInts.java | 27 ++++++++++++++++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/shedlang/compiler/shid/instructions/ShidIntMultiply.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java index b5be44ce..fa38365f 100644 --- a/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java +++ b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java @@ -185,6 +185,9 @@ public class ShidSerializer { case ShidIntLessThanOrEqual intLessThanOrEqual -> treeList(treeIdentifier("int-lte")); + case ShidIntMultiply intMultiply -> + treeList(treeIdentifier("int-mul")); + case ShidIntNotEqual intNotEqual -> treeList(treeIdentifier("int-ne")); diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java index c04e3687..682a7e6a 100644 --- a/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java +++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java @@ -3,7 +3,7 @@ package org.shedlang.compiler.shid.instructions; import org.shedlang.compiler.sources.Source; public sealed interface ShidInstruction - permits ShidBlock, ShidCall, ShidDeclareNativeFunction, ShidDefineEffect, ShidDefineFunction, ShidDefineStruct, ShidDiscard, ShidDuplicate, ShidEffectHandlerBreak, ShidFieldLoad, ShidIf, ShidIntAdd, ShidIntEquals, ShidIntGreaterThan, ShidIntGreaterThanOrEqual, ShidIntLessThan, ShidIntLessThanOrEqual, ShidIntNotEqual, ShidIntSub, ShidLocalDrop, ShidLocalLoad, ShidLocalStore, ShidLoop, ShidLoopBreak, ShidLoopContinue, ShidMethodLoad, ShidMethodStore, ShidNamespaceFieldStore, ShidNamespaceImport, ShidPushBool, ShidPushInt, ShidPushString, ShidPushUnit, ShidResume, ShidReturn, ShidStringAdd, ShidSwitch, ShidTry, ShidTupleCreate, ShidTupleGet, ShidVarLoad, ShidVarStore { + permits ShidBlock, ShidCall, ShidDeclareNativeFunction, ShidDefineEffect, ShidDefineFunction, ShidDefineStruct, ShidDiscard, ShidDuplicate, ShidEffectHandlerBreak, ShidFieldLoad, ShidIf, ShidIntAdd, ShidIntEquals, ShidIntGreaterThan, ShidIntGreaterThanOrEqual, ShidIntLessThan, ShidIntLessThanOrEqual, ShidIntMultiply, ShidIntNotEqual, ShidIntSub, ShidLocalDrop, ShidLocalLoad, ShidLocalStore, ShidLoop, ShidLoopBreak, ShidLoopContinue, ShidMethodLoad, ShidMethodStore, ShidNamespaceFieldStore, ShidNamespaceImport, ShidPushBool, ShidPushInt, ShidPushString, ShidPushUnit, ShidResume, ShidReturn, ShidStringAdd, ShidSwitch, ShidTry, ShidTupleCreate, ShidTupleGet, ShidVarLoad, ShidVarStore { /** * If an instruction is a terminator, execution is guaranteed to leave the diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidIntMultiply.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidIntMultiply.java new file mode 100644 index 00000000..c8d333a5 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidIntMultiply.java @@ -0,0 +1,14 @@ +package org.shedlang.compiler.shid.instructions; + +import org.shedlang.compiler.sources.Source; + +/** + * Pop two values from the temporary value stack, assumed to be Ints, multiply + * them together, and push the result onto the temporary value stack. + */ +public record ShidIntMultiply(Source source) implements ShidInstruction { + @Override + public boolean isBlockTerminator() { + return false; + } +} diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java index 4ea228b2..9d585912 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java @@ -534,6 +534,19 @@ public class ShidToPatio { context ); + case ShidIntMultiply intMultiply -> + generateBinaryOperation( + (destination, left, right, operationContext) -> + ShidToPatioInts.multiply( + destination, + left, + right, + intMultiply.source(), + operationContext + ), + context + ); + case ShidIntNotEqual intNotEqual -> generateComparison( (destination, left, right, operationContext) -> diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioInts.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioInts.java index 3fe08470..7d9e97c8 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioInts.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioInts.java @@ -45,6 +45,16 @@ public class ShidToPatioInts { Sources.RUNTIME ); + public static final PatioFunctionDeclaration INT_MULTIPLY_DECLARATION = new PatioFunctionDeclaration( + "shed__runtime__int_multiply", + List.of( + PatioLocal.param("left", ShidToPatioTypes.VALUE_TYPE), + PatioLocal.param("right", ShidToPatioTypes.VALUE_TYPE) + ), + ShidToPatioTypes.VALUE_TYPE, + Sources.RUNTIME + ); + public static final PatioFunctionDeclaration INT_EQUALS_DECLARATION = new PatioFunctionDeclaration( "shed__runtime__int_equals", List.of( @@ -160,6 +170,23 @@ public class ShidToPatioInts { )); } + public static ShidToPatioContext multiply( + PatioLocalRef destination, + PatioOperand left, + PatioOperand right, + Source source, + ShidToPatioContext context + ) { + return context + .ensureCompilationUnitElement(INT_MULTIPLY_DECLARATION) + .addInstruction(new PatioCall( + Optional.of(destination), + INT_MULTIPLY_DECLARATION.name(), + List.of(left, right), + source + )); + } + public static ShidToPatioContext equals( PatioLocalRef destination, PatioOperand left, -- cgit v1.2.3