From a2d7af1fdcc64f117d5db91761cb2610854e9e60 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 15 Dec 2023 14:41:14 +0000 Subject: Extract code gen module for Patio ints --- .../org/shedlang/compiler/patio/PatioCodeGen.java | 50 ++++++++---- .../shedlang/compiler/patio/PatioCodeGenInts.java | 90 ++++++++++++++++++++++ .../compiler/patio/PatioCodeGenLocalSpace.java | 1 + 3 files changed, 127 insertions(+), 14 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index 925b540a..3e3a2aa7 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -416,24 +416,26 @@ public class PatioCodeGen { case ShidIntAdd intAdd -> generateBinaryOperation( (target, left, right, operationContext) -> - operationContext.addInstruction(new PatioIntAdd( + PatioCodeGenInts.add( target, left, right, - intAdd.source() - )), + intAdd.source(), + operationContext + ), context ); case ShidIntEquals intEquals -> generateComparison( (target, left, right, operationContext) -> - operationContext.addInstruction(new PatioIntEquals( + PatioCodeGenInts.equals( target, left, right, - intEquals.source() - )), + intEquals.source(), + operationContext + ), intEquals.source(), context ); @@ -441,12 +443,13 @@ public class PatioCodeGen { case ShidIntNotEqual intNotEqual -> generateComparison( (target, left, right, operationContext) -> - operationContext.addInstruction(new PatioIntNotEqual( + PatioCodeGenInts.notEqual( target, left, right, - intNotEqual.source() - )), + intNotEqual.source(), + operationContext + ), intNotEqual.source(), context ); @@ -454,12 +457,13 @@ public class PatioCodeGen { case ShidIntSub intSub -> generateBinaryOperation( (target, left, right, operationContext) -> - operationContext.addInstruction(new PatioIntSub( + PatioCodeGenInts.sub( target, left, right, - intSub.source() - )), + intSub.source(), + operationContext + ), context ); @@ -542,8 +546,26 @@ public class PatioCodeGen { case ShidPushBool pushBool -> pushValue(pushBool.value() ? 1 : 0, pushBool.source(), context); - case ShidPushInt pushInt -> - pushValue(pushInt.value().intValueExact(), pushInt.source(), context); + case ShidPushInt pushInt -> { + var targetResult = context.allocateVal( + PatioCodeGenLocalSpace.INT, + PatioCodeGenInts.INT_TYPE + ); + var target = targetResult.value(); + context = targetResult.context(); + + context = PatioCodeGenInts.store( + target, + pushInt.value(), + pushInt.source(), + context + ); + + context = context.temporaryPush(target); + + yield context; + } + case ShidPushString pushString -> { var stringPtrOperandResult = PatioCodeGenStrings.define( diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java new file mode 100644 index 00000000..037bd64b --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java @@ -0,0 +1,90 @@ +package org.shedlang.compiler.patio; + +import org.shedlang.compiler.patio.ast.instructions.*; +import org.shedlang.compiler.patio.ast.operands.PatioInt; +import org.shedlang.compiler.patio.ast.operands.PatioLocalRef; +import org.shedlang.compiler.patio.ast.operands.PatioOperand; +import org.shedlang.compiler.patio.ast.types.PatioType; +import org.shedlang.compiler.sources.Source; + +import java.math.BigInteger; + +public class PatioCodeGenInts { + public static final PatioType INT_TYPE = PatioCodeGenTypes.VALUE_TYPE; + + private PatioCodeGenInts() { + } + + public static PatioCodeGenContext store( + PatioLocalRef target, + BigInteger value, + Source source, + PatioCodeGenContext context + ) { + return context.addInstruction(new PatioLocalStore( + target, + new PatioInt(value.intValueExact()), + source + )); + } + + public static PatioCodeGenContext add( + PatioLocalRef target, + PatioOperand left, + PatioOperand right, + Source source, + PatioCodeGenContext context + ) { + return context.addInstruction(new PatioIntAdd( + target, + left, + right, + source + )); + } + + public static PatioCodeGenContext sub( + PatioLocalRef target, + PatioOperand left, + PatioOperand right, + Source source, + PatioCodeGenContext context + ) { + return context.addInstruction(new PatioIntSub( + target, + left, + right, + source + )); + } + + public static PatioCodeGenContext equals( + PatioLocalRef target, + PatioOperand left, + PatioOperand right, + Source source, + PatioCodeGenContext context + ) { + return context.addInstruction(new PatioIntEquals( + target, + left, + right, + source + )); + } + + public static PatioCodeGenContext notEqual( + PatioLocalRef target, + PatioOperand left, + PatioOperand right, + Source source, + PatioCodeGenContext context + ) { + return context.addInstruction(new PatioIntNotEqual( + target, + left, + right, + source + )); + } +} diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java index 1fd6a5fd..89a47708 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java @@ -11,6 +11,7 @@ public enum PatioCodeGenLocalSpace { EFFECT_HANDLER_CURRENT("effectHandlerCurrent"), EFFECT_HANDLER_METHOD("effectHandlerMethod"), EFFECT_HANDLER_METHOD_POINTER("effectHandlerMethodPointer"), + INT("int"), MAIN("main"), NAMESPACE_IS_INITED_POINTER("namespaceIsInitedPointer"), NAMESPACE_POINTER("namespacePointer"), -- cgit v1.2.3