From b6ade34f14bc5049e036a43f640b862f6c3390bc Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 16 Dec 2023 15:37:13 +0000 Subject: Extract PatioCodeGenVars.alloc() --- .../org/shedlang/compiler/patio/PatioCodeGen.java | 18 +++--------- .../shedlang/compiler/patio/PatioCodeGenVars.java | 33 +++++++++++++++++++--- 2 files changed, 33 insertions(+), 18 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index 1f676b6b..81fa758d 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -6,7 +6,6 @@ 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.PatioDataLayouts; import org.shedlang.compiler.patio.ast.types.PatioTypes; import org.shedlang.compiler.shid.ShidNamespace; import org.shedlang.compiler.shid.instructions.*; @@ -967,21 +966,12 @@ public class PatioCodeGen { ).context(); } case VAR -> { - var patioLocalResult = context.declareShidVal( + context = PatioCodeGenVars.alloc( boundVariable.name(), - PatioTypes.DATA_POINTER + // TODO: proper source (probably by storing variables on the function/namespace) + Sources.NULL, + context ); - var patioLocal = patioLocalResult.value(); - context = patioLocalResult.context(); - - // TODO: proper source (probably by storing variables on the function/namespace) - context = context.addInstruction(new PatioAllocStack( - patioLocal, - PatioDataLayouts.fromType(PatioCodeGenTypes.VALUE_TYPE), - Sources.NULL - )); - - context = context.initShidLocal(boundVariable.name()); } } } diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenVars.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenVars.java index ebf8b847..cd85e2cd 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenVars.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenVars.java @@ -1,10 +1,12 @@ package org.shedlang.compiler.patio; +import org.shedlang.compiler.patio.ast.instructions.PatioAllocStack; import org.shedlang.compiler.patio.ast.instructions.PatioMemoryLoad; import org.shedlang.compiler.patio.ast.instructions.PatioMemoryStore; import org.shedlang.compiler.patio.ast.operands.PatioLocalRef; import org.shedlang.compiler.patio.ast.operands.PatioOperand; import org.shedlang.compiler.patio.ast.types.PatioDataLayouts; +import org.shedlang.compiler.patio.ast.types.PatioTypes; import org.shedlang.compiler.sources.Source; import java.util.List; @@ -13,14 +15,37 @@ public class PatioCodeGenVars { private PatioCodeGenVars() { } + public static PatioCodeGenContext alloc( + String shidName, + Source source, + PatioCodeGenContext context + ) { + var patioLocalResult = context.declareShidVal( + shidName, + PatioTypes.DATA_POINTER + ); + var patioLocal = patioLocalResult.value(); + context = patioLocalResult.context(); + + context = context.addInstruction(new PatioAllocStack( + patioLocal, + PatioDataLayouts.fromType(PatioCodeGenTypes.VALUE_TYPE), + source + )); + + context = context.initShidLocal(shidName); + + return context; + } + public static PatioCodeGenContext store( - String name, + String shidName, PatioOperand value, Source source, PatioCodeGenContext context ) { return context.addInstruction(new PatioMemoryStore( - context.shidLocalNameToLocal(name), + context.shidLocalNameToLocal(shidName), PatioDataLayouts.fromType(PatioCodeGenTypes.VALUE_TYPE), List.of(), value, @@ -30,13 +55,13 @@ public class PatioCodeGenVars { public static PatioCodeGenContext load( PatioLocalRef target, - String name, + String shidName, Source source, PatioCodeGenContext context ) { return context.addInstruction(new PatioMemoryLoad( target, - context.shidLocalNameToLocal(name), + context.shidLocalNameToLocal(shidName), PatioDataLayouts.fromType(PatioCodeGenTypes.VALUE_TYPE), List.of(), source -- cgit v1.2.3