aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-16 23:12:24 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-16 23:12:24 +0100
commit60a9adc9154d59dfe904ebda4e3de9dc48cdf235 (patch)
treed121973eb74252ba8c374d4e4568b9bf30b42b0e /src/main/java
parent25ca0c973842be553326d3cd69ea72fd658b0ba6 (diff)
Preserve top level entities when exiting function
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java6
-rw-r--r--src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerContext.java15
2 files changed, 19 insertions, 2 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java
index 26319b43..4ee42fa8 100644
--- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java
+++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java
@@ -62,7 +62,9 @@ public class LlvmCompiler {
for (var bodyInstruction : defineFunction.body()) {
functionContext = compileInstruction(bodyInstruction, functionContext);
}
- // TODO: rejoin function context with context
+ var bodyResult = context.exitFunction(functionContext);
+ var body = bodyResult.value();
+ context = bodyResult.context();
yield context.addTopLevelEntity(new LlvmFunctionDefinition(
topLevelEntityName,
@@ -74,7 +76,7 @@ public class LlvmCompiler {
"shed__env__0"
)
),
- functionContext.basicBlocks()
+ body
));
}
case DiscardInstruction discardInstruction ->
diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerContext.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerContext.java
index 256079bc..d7d8c84d 100644
--- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerContext.java
+++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerContext.java
@@ -15,6 +15,7 @@ import org.shedlang.compiler.typechecker.TypeInfo;
import org.shedlang.compiler.util.POrderedMap;
import java.util.Collection;
+import java.util.List;
import java.util.Optional;
public record LlvmCompilerContext(
@@ -204,4 +205,18 @@ public record LlvmCompilerContext(
NameAllocator.empty()
);
}
+
+ public LlvmCompilerResult<List<LlvmBasicBlock>> exitFunction(
+ LlvmCompilerContext functionContext
+ ) {
+ return new LlvmCompilerContext(
+ typeInfo,
+ stack,
+ locals,
+ basicBlocks,
+ instructions,
+ topLevelEntitiesByName.plusAll(functionContext.topLevelEntitiesByName),
+ localVariableNames
+ ).toResult(functionContext.basicBlocks);
+ }
}