From b48edd39f96c9f3f0917fb4000d10f0a78920b7d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 20 Jul 2023 14:17:58 +0100 Subject: Store function pointer in closure --- .../compiler/patio/PatioCodeGenClosures.java | 25 +++++++++++++++++++--- .../compiler/patio/PatioCodeGenLocalSpace.java | 1 + 2 files changed, 23 insertions(+), 3 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java index b3cace14..e8d991d2 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java @@ -1,10 +1,8 @@ package org.shedlang.compiler.patio; import org.shedlang.compiler.ast.typed.FreeVariables; -import org.shedlang.compiler.patio.ast.instructions.PatioAlloc; -import org.shedlang.compiler.patio.ast.instructions.PatioCallPointer; +import org.shedlang.compiler.patio.ast.instructions.*; import org.shedlang.compiler.patio.ast.PatioLocalRef; -import org.shedlang.compiler.patio.ast.instructions.PatioMemoryLoad; import org.shedlang.compiler.patio.ast.types.PatioDataLayout; import org.shedlang.compiler.patio.ast.types.PatioStruct; import org.shedlang.compiler.patio.ast.types.PatioType; @@ -71,6 +69,27 @@ class PatioCodeGenClosures { context = context.addInstruction(new PatioAlloc(closure, layout, source)); + var functionPointerResult = context.allocateLocal( + PatioCodeGenLocalSpace.CLOSURE_FUNCTION_POINTER, + PatioTypes.FUNCTION_POINTER + ); + var functionPointer = functionPointerResult.value(); + context = functionPointerResult.context(); + + context = context.addInstruction(new PatioFunctionPointerGet( + functionPointer, + fullName, + source + )); + + context = context.addInstruction(new PatioMemoryStore( + closure, + layout, + FUNCTION_POINTER_OFFSET, + functionPointer, + source + )); + return context; } diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java index 89fd69c6..d3827cb7 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java @@ -5,6 +5,7 @@ public enum PatioCodeGenLocalSpace { CALL_ARG_NAMED("callArgNamed"), CALL_FUNCTION_POINTER("callFunctionPointer"), CLOSURE("closure"), + CLOSURE_FUNCTION_POINTER("closureFunctionPointer"), LOCAL("local"), RECEIVER("receiver"), TEMPORARY("tmp"); -- cgit v1.2.3