From 60a9adc9154d59dfe904ebda4e3de9dc48cdf235 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 16 Jun 2023 23:12:24 +0100 Subject: Preserve top level entities when exiting function --- .../org/shedlang/compiler/backends/llvm/LlvmCompiler.java | 6 ++++-- .../compiler/backends/llvm/LlvmCompilerContext.java | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'src/main/java') 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> exitFunction( + LlvmCompilerContext functionContext + ) { + return new LlvmCompilerContext( + typeInfo, + stack, + locals, + basicBlocks, + instructions, + topLevelEntitiesByName.plusAll(functionContext.topLevelEntitiesByName), + localVariableNames + ).toResult(functionContext.basicBlocks); + } } -- cgit v1.2.3