diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-12-16 12:27:47 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-12-16 12:27:47 +0000 |
| commit | 32bdb166ae3d0656984d20d6d9fd7c29b425cea7 (patch) | |
| tree | 779f333b1ce0fd75a9f2c100008543e06c1858f8 /src/main/java | |
| parent | a0593daf8d29c741b1660b3a3ea5c615b56d1567 (diff) | |
Extract alloc code for closures
Diffstat (limited to 'src/main/java')
3 files changed, 39 insertions, 31 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java index a21aea3e..69434491 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java @@ -6,6 +6,7 @@ import org.shedlang.compiler.patio.ast.instructions.PatioCallPointer; import org.shedlang.compiler.patio.ast.instructions.PatioIntToDataPtr; import org.shedlang.compiler.patio.ast.instructions.PatioMemoryLoad; import org.shedlang.compiler.patio.ast.instructions.PatioMemoryStore; +import org.shedlang.compiler.patio.ast.operands.PatioFunctionPtr; import org.shedlang.compiler.patio.ast.operands.PatioLocalRef; import org.shedlang.compiler.patio.ast.operands.PatioOperand; import org.shedlang.compiler.patio.ast.types.*; @@ -78,6 +79,34 @@ class PatioCodeGenClosures { return bodyContext; } + static PatioCodeGenResult<PatioLocalRef> alloc( + String name, + String functionName, + Layout layout, + Source source, + PatioCodeGenContext context + ) { + var closurePointerResult = context.allocateVal( + PatioCodeGenLocalSpace.CLOSURE_POINTER, + name, + PatioTypes.DATA_POINTER + ); + var closurePointer = closurePointerResult.value(); + context = closurePointerResult.context(); + + context = PatioCodeGenAlloc.alloc(closurePointer, layout.dataLayout(), source, context); + + context = context.addInstruction(new PatioMemoryStore( + closurePointer, + layout.dataLayout(), + FUNCTION_POINTER_OFFSET, + new PatioFunctionPtr(functionName), + source + )); + + return context.toResult(closurePointer); + } + static PatioCodeGenContext storeFreeVariables( FreeVariables freeVariables, PatioLocalRef closurePointer, diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenEffects.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenEffects.java index acf1a770..c4a74f09 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenEffects.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenEffects.java @@ -6,7 +6,6 @@ import org.shedlang.compiler.patio.ast.PatioFunctionDeclaration; import org.shedlang.compiler.patio.ast.PatioLocal; import org.shedlang.compiler.patio.ast.PatioVisibility; import org.shedlang.compiler.patio.ast.instructions.*; -import org.shedlang.compiler.patio.ast.operands.PatioFunctionPtr; import org.shedlang.compiler.patio.ast.operands.PatioInt; import org.shedlang.compiler.patio.ast.operands.PatioLocalRef; import org.shedlang.compiler.patio.ast.operands.PatioOperand; @@ -35,7 +34,6 @@ public class PatioCodeGenEffects { private static final PatioType EFFECT_HANDLER_METHOD_POINTER_TYPE = PatioTypes.DATA_POINTER; private static final PatioType EFFECT_HANDLER_METHOD_TYPE = PatioCodeGenTypes.VALUE_TYPE; - private static final List<Integer> CLOSURE_FUNCTION_POINTER_OFFSET = List.of(0); private static final List<Integer> CLOSURE_EFFECT_HANDLER_OFFSET = List.of(1); private static PatioCodeGenClosures.Layout effectHandlerMethodDataLayout( @@ -428,24 +426,16 @@ public class PatioCodeGenEffects { return bodyContext; }); - var closurePointerResult = context.allocateVal( - PatioCodeGenLocalSpace.EFFECT_HANDLER_METHOD_POINTER, + var closurePointerResult = PatioCodeGenClosures.alloc( name, - PatioTypes.DATA_POINTER + fullName, + layout, + source, + context ); var closurePointer = closurePointerResult.value(); context = closurePointerResult.context(); - context = PatioCodeGenAlloc.alloc(closurePointer, layout.dataLayout(), source, context); - - context = context.addInstruction(new PatioMemoryStore( - closurePointer, - layout.dataLayout(), - CLOSURE_FUNCTION_POINTER_OFFSET, - new PatioFunctionPtr(fullName), - source - )); - var effectHandlerResult = context.allocateVal( PatioCodeGenLocalSpace.EFFECT_HANDLER_CURRENT, EFFECT_HANDLER_TYPE diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenFunctions.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenFunctions.java index c2737935..217ac701 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenFunctions.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenFunctions.java @@ -3,10 +3,7 @@ package org.shedlang.compiler.patio; import org.shedlang.compiler.ast.typed.FreeVariables; import org.shedlang.compiler.patio.ast.PatioVisibility; import org.shedlang.compiler.patio.ast.instructions.PatioDataPtrToInt; -import org.shedlang.compiler.patio.ast.instructions.PatioMemoryStore; -import org.shedlang.compiler.patio.ast.operands.PatioFunctionPtr; import org.shedlang.compiler.patio.ast.operands.PatioLocalRef; -import org.shedlang.compiler.patio.ast.types.PatioTypes; import org.shedlang.compiler.shid.instructions.ShidDefineFunction; import org.shedlang.compiler.sources.Source; @@ -70,24 +67,16 @@ public class PatioCodeGenFunctions { return bodyContext; }); - var closurePointerResult = context.allocateVal( - PatioCodeGenLocalSpace.CLOSURE_POINTER, + var closurePointerResult = PatioCodeGenClosures.alloc( name, - PatioTypes.DATA_POINTER + fullName, + layout, + source, + context ); var closurePointer = closurePointerResult.value(); context = closurePointerResult.context(); - context = PatioCodeGenAlloc.alloc(closurePointer, layout.dataLayout(), source, context); - - context = context.addInstruction(new PatioMemoryStore( - closurePointer, - layout.dataLayout(), - FUNCTION_POINTER_OFFSET, - new PatioFunctionPtr(fullName), - source - )); - context = context.addInstruction(new PatioDataPtrToInt( target, closurePointer, |
