aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-16 12:59:00 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-16 12:59:00 +0000
commit071fb90e7d12c6cc177fd9414584ee47679e2dc5 (patch)
tree1b5ff60ab55ad35854bd418b98c7394dc3488b97 /src/main/java
parent811a0571c1f616d3c27fe2fff929ffdc2bbc4f55 (diff)
Extract PatioCodeGenClosures.create()
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java60
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenEffects.java142
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenFunctions.java47
3 files changed, 123 insertions, 126 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java
index e96914fa..333f3d68 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java
@@ -3,6 +3,7 @@ package org.shedlang.compiler.patio;
import org.pcollections.PVector;
import org.shedlang.compiler.ast.typed.FreeVariable;
import org.shedlang.compiler.ast.typed.FreeVariables;
+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.PatioLocalRef;
@@ -79,6 +80,65 @@ class PatioCodeGenClosures {
return bodyContext;
}
+ static PatioCodeGenContext create(
+ PatioLocalRef target,
+ PatioCodeGenCompilationUnitElementSpace functionElementSpace,
+ String name,
+ List<ShidDefineFunction.Param> positionalParams,
+ List<ShidDefineFunction.Param> namedParams,
+ PatioDataLayout layout,
+ BiFunction<PatioLocalRef, PatioCodeGenContext, PatioCodeGenContext> storeAdditional,
+ FreeVariables freeVariables,
+ BiFunction<PatioLocalRef, PatioCodeGenContext, PatioCodeGenContext> generateBody,
+ Source source,
+ PatioCodeGenContext context
+ ) {
+ var fullNameResult = context.allocateCompilationUnitElementName(
+ functionElementSpace,
+ name
+ );
+ var fullName = fullNameResult.value();
+ context = fullNameResult.context();
+
+ var bodyContextParent = context;
+ context = context.createFunction(fullName, PatioVisibility.PRIVATE, PatioCodeGenTypes.VALUE_TYPE, source, bodyContext -> {
+ var closurePointerResult = bodyContext.allocateParam(
+ PatioCodeGenLocalSpace.CLOSURE_POINTER,
+ PatioCodeGenClosures.CLOSURE_POINTER_TYPE
+ );
+ var closurePointer = closurePointerResult.value();
+ bodyContext = closurePointerResult.context();
+
+ bodyContext = defineParams(positionalParams, namedParams, bodyContext);
+
+ bodyContext = loadFreeVariables(
+ freeVariables,
+ closurePointer,
+ layout,
+ source,
+ bodyContext,
+ bodyContextParent
+ );
+
+ bodyContext = generateBody.apply(closurePointer, bodyContext);
+
+ return bodyContext;
+ });
+
+ context = PatioCodeGenClosures.alloc(
+ target,
+ name,
+ fullName,
+ layout,
+ storeAdditional,
+ freeVariables,
+ source,
+ context
+ );
+
+ return context;
+ }
+
static PatioCodeGenContext alloc(
PatioLocalRef target,
String name,
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenEffects.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenEffects.java
index f8df4e1b..e88a2803 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenEffects.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenEffects.java
@@ -4,7 +4,6 @@ import org.shedlang.compiler.ast.typed.FreeVariables;
import org.shedlang.compiler.patio.ast.PatioExceptionTypeDefinition;
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.PatioInt;
import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
@@ -25,8 +24,6 @@ import java.util.Optional;
import java.util.function.Function;
import java.util.stream.IntStream;
-import static org.shedlang.compiler.patio.PatioCodeGenClosures.*;
-
public class PatioCodeGenEffects {
private static final PatioType EFFECT_TAG_TYPE = PatioTypes.INT;
private static final PatioType EFFECT_METHOD_INDEX_TYPE = PatioTypes.INT;
@@ -359,102 +356,77 @@ public class PatioCodeGenEffects {
.ensureCompilationUnitElement(GET_HANDLER_DECLARATION)
.ensureCompilationUnitElement(SET_HANDLER_DECLARATION);
- // TODO: remove duplication with PatioCodeGenClosures
var layout = effectHandlerMethodDataLayout(freeVariables, context);
- var fullNameResult = context.allocateCompilationUnitElementName(
- PatioCodeGenCompilationUnitElementSpace.EFFECT_HANDLER_METHOD,
- name
- );
- var fullName = fullNameResult.value();
- context = fullNameResult.context();
-
- var bodyContextParent = context;
- context = context.createFunction(fullName, PatioVisibility.PRIVATE, PatioCodeGenTypes.VALUE_TYPE, source, bodyContext -> {
- var closurePointerResult = bodyContext.allocateParam(
- PatioCodeGenLocalSpace.EFFECT_HANDLER_METHOD_POINTER,
- EFFECT_HANDLER_METHOD_POINTER_TYPE
- );
- var closurePointer = closurePointerResult.value();
- bodyContext = closurePointerResult.context();
-
- bodyContext = defineParams(positionalParams, namedParams, bodyContext);
-
- var previousEffectHandlerResult = bodyContext.allocateVal(
- PatioCodeGenLocalSpace.EFFECT_HANDLER,
- EFFECT_HANDLER_METHOD_POINTER_TYPE
- );
- var previousEffectHandler = previousEffectHandlerResult.value();
- bodyContext = previousEffectHandlerResult.context();
-
- bodyContext = getCurrentEffectHandler(previousEffectHandler, source, bodyContext);
-
- bodyContext = bodyContext.stashEffectHandler(previousEffectHandler);
-
- var effectHandlerResult = bodyContext.allocateVal(
- PatioCodeGenLocalSpace.EFFECT_HANDLER,
- EFFECT_HANDLER_METHOD_POINTER_TYPE
- );
- var effectHandler = effectHandlerResult.value();
- bodyContext = effectHandlerResult.context();
-
- bodyContext = bodyContext.addInstruction(new PatioMemoryLoad(
- effectHandler,
- closurePointer,
- layout,
- CLOSURE_EFFECT_HANDLER_OFFSET,
- source
- ));
-
- bodyContext = bodyContext.addInstruction(new PatioCall(
- Optional.empty(),
- SET_HANDLER_DECLARATION.name(),
- List.of(effectHandler),
- source
- ));
-
- bodyContext = loadFreeVariables(
- freeVariables,
- closurePointer,
- layout,
- source,
- bodyContext,
- bodyContextParent
- );
-
- bodyContext = generateBody.apply(bodyContext);
-
- return bodyContext;
- });
-
- var effectHandlerResult = context.allocateVal(
- PatioCodeGenLocalSpace.EFFECT_HANDLER_CURRENT,
- EFFECT_HANDLER_TYPE
- );
- var effectHandler = effectHandlerResult.value();
- context = effectHandlerResult.context();
-
- context = getCurrentEffectHandler(effectHandler, source, context);
-
- context = PatioCodeGenClosures.alloc(
+ return PatioCodeGenClosures.create(
target,
+ PatioCodeGenCompilationUnitElementSpace.EFFECT_HANDLER_METHOD,
name,
- fullName,
+ positionalParams,
+ namedParams,
layout,
- (closurePointer, allocContext) ->
- allocContext.addInstruction(new PatioMemoryStore(
+ (closurePointer, allocContext) -> {
+ var effectHandlerResult = allocContext.allocateVal(
+ PatioCodeGenLocalSpace.EFFECT_HANDLER_CURRENT,
+ EFFECT_HANDLER_TYPE
+ );
+ var effectHandler = effectHandlerResult.value();
+ allocContext = effectHandlerResult.context();
+
+ allocContext = getCurrentEffectHandler(effectHandler, source, allocContext);
+
+ allocContext = allocContext.addInstruction(new PatioMemoryStore(
closurePointer,
layout,
CLOSURE_EFFECT_HANDLER_OFFSET,
effectHandler,
source
- )),
+ ));
+
+ return allocContext;
+ },
freeVariables,
+ (closurePointer, bodyContext) -> {
+ var previousEffectHandlerResult = bodyContext.allocateVal(
+ PatioCodeGenLocalSpace.EFFECT_HANDLER,
+ EFFECT_HANDLER_METHOD_POINTER_TYPE
+ );
+ var previousEffectHandler = previousEffectHandlerResult.value();
+ bodyContext = previousEffectHandlerResult.context();
+
+ bodyContext = getCurrentEffectHandler(previousEffectHandler, source, bodyContext);
+
+ bodyContext = bodyContext.stashEffectHandler(previousEffectHandler);
+
+ var effectHandlerResult = bodyContext.allocateVal(
+ PatioCodeGenLocalSpace.EFFECT_HANDLER,
+ EFFECT_HANDLER_METHOD_POINTER_TYPE
+ );
+ var effectHandler = effectHandlerResult.value();
+ bodyContext = effectHandlerResult.context();
+
+ bodyContext = bodyContext.addInstruction(new PatioMemoryLoad(
+ effectHandler,
+ closurePointer,
+ layout,
+ CLOSURE_EFFECT_HANDLER_OFFSET,
+ source
+ ));
+
+ bodyContext = bodyContext.addInstruction(new PatioCall(
+ Optional.empty(),
+ SET_HANDLER_DECLARATION.name(),
+ List.of(effectHandler),
+ source
+ ));
+
+ bodyContext = generateBody.apply(bodyContext);
+
+ return bodyContext;
+ },
source,
context
);
-
- return context;
}
public static PatioCodeGenContext resume(
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenFunctions.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenFunctions.java
index bbcd4db5..eefdd4f5 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenFunctions.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenFunctions.java
@@ -1,7 +1,6 @@
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.operands.PatioLocalRef;
import org.shedlang.compiler.patio.ast.types.PatioDataLayout;
import org.shedlang.compiler.patio.ast.types.PatioDataLayouts;
@@ -11,9 +10,6 @@ import org.shedlang.compiler.sources.Source;
import java.util.List;
import java.util.function.Function;
-import static org.shedlang.compiler.patio.PatioCodeGenClosures.defineParams;
-import static org.shedlang.compiler.patio.PatioCodeGenClosures.loadFreeVariables;
-
public class PatioCodeGenFunctions {
private PatioCodeGenFunctions() {
}
@@ -41,49 +37,18 @@ public class PatioCodeGenFunctions {
) {
var layout = functionDataLayout(freeVariables, context);
- var fullNameResult = context.allocateCompilationUnitElementName(
- PatioCodeGenCompilationUnitElementSpace.FUNCTION,
- name
- );
- var fullName = fullNameResult.value();
- context = fullNameResult.context();
-
- var bodyContextParent = context;
- context = context.createFunction(fullName, PatioVisibility.PRIVATE, PatioCodeGenTypes.VALUE_TYPE, source, bodyContext -> {
- var closurePointerResult = bodyContext.allocateParam(
- PatioCodeGenLocalSpace.CLOSURE_POINTER,
- PatioCodeGenClosures.CLOSURE_POINTER_TYPE
- );
- var closurePointer = closurePointerResult.value();
- bodyContext = closurePointerResult.context();
-
- bodyContext = defineParams(positionalParams, namedParams, bodyContext);
-
- bodyContext = loadFreeVariables(
- freeVariables,
- closurePointer,
- layout,
- source,
- bodyContext,
- bodyContextParent
- );
-
- bodyContext = generateBody.apply(bodyContext);
-
- return bodyContext;
- });
-
- context = PatioCodeGenClosures.alloc(
+ return PatioCodeGenClosures.create(
target,
+ PatioCodeGenCompilationUnitElementSpace.FUNCTION,
name,
- fullName,
+ positionalParams,
+ namedParams,
layout,
- (closurePointer, allocContext) -> allocContext,
+ (closurePointer, storeContext) -> storeContext,
freeVariables,
+ (closurePointer, bodyContext) -> generateBody.apply(bodyContext),
source,
context
);
-
- return context;
}
}