diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-12-02 20:40:31 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-12-02 20:41:26 +0000 |
| commit | 4201eba418a2a02fa1cb8190dd897cc246c3a45e (patch) | |
| tree | b8dbcef8df85f82c2e54baf82608dc740f9fa2f5 /src/main/java | |
| parent | 45a815c4c957b1f83c8e4d350d828c3dba7e284b (diff) | |
Extract functions to store and load free variables
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java | 77 |
1 files changed, 56 insertions, 21 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java index abc89e10..9e5b2b1d 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java @@ -96,24 +96,14 @@ class PatioCodeGenClosures { bodyContext = bodyContext.defineShidParam(shidParam.name()); } - for (var freeVariable : freeVariables) { - var freeVariableLocalResult = bodyContext.declareShidVal( - freeVariable.name(), - freeVariableType(freeVariable, bodyContextParent) - ); - var freeVariableLocal = freeVariableLocalResult.value(); - bodyContext = freeVariableLocalResult.context(); - - bodyContext = bodyContext.addInstruction(new PatioMemoryLoad( - freeVariableLocal, - closurePointer, - layout, - freeVariableOffset(freeVariable), - source - )); - - bodyContext = bodyContext.initShidLocal(freeVariable.name()); - } + bodyContext = loadFreeVariables( + freeVariables, + closurePointer, + layout, + source, + bodyContext, + bodyContextParent + ); bodyContext = generateBody.apply(bodyContext); @@ -144,23 +134,68 @@ class PatioCodeGenClosures { source )); + context = storeFreeVariables( + freeVariables, + closurePointer, + layout, + source, + context + ); + + return context; + } + + private static PatioCodeGenContext storeFreeVariables( + FreeVariables freeVariables, + PatioLocalRef closurePointer, + PatioDataLayout closureLayout, + Source source, + PatioCodeGenContext context + ) { for (var freeVariable : freeVariables) { context = context.onceShidLocalDefined( freeVariable.name(), c -> c.addInstruction(new PatioMemoryStore( closurePointer, - layout, + closureLayout, freeVariableOffset(freeVariable), c.shidLocalNameToLocal(freeVariable.name()), source )) ); - } - return context; } + private static PatioCodeGenContext loadFreeVariables( + FreeVariables freeVariables, + PatioLocalRef closurePointer, + PatioDataLayout closureLayout, + Source source, + PatioCodeGenContext bodyContext, + PatioCodeGenContext bodyContextParent + ) { + for (var freeVariable : freeVariables) { + var freeVariableLocalResult = bodyContext.declareShidVal( + freeVariable.name(), + freeVariableType(freeVariable, bodyContextParent) + ); + var freeVariableLocal = freeVariableLocalResult.value(); + bodyContext = freeVariableLocalResult.context(); + + bodyContext = bodyContext.addInstruction(new PatioMemoryLoad( + freeVariableLocal, + closurePointer, + closureLayout, + freeVariableOffset(freeVariable), + source + )); + + bodyContext = bodyContext.initShidLocal(freeVariable.name()); + } + return bodyContext; + } + static PatioCodeGenContext call( PatioLocalRef target, PatioLocalRef closure, |
