diff options
Diffstat (limited to 'src')
3 files changed, 57 insertions, 43 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 0a3ad3ac..b88f8fce 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java @@ -76,6 +76,8 @@ public class LlvmCompiler { ); } case DefineFunctionInstruction defineFunction -> { + var closureTypes = LlvmCompilerClosures.types(defineFunction.freeVariables()); + var topLevelEntityName = context.generateTopLevelEntityName( LlvmCompilerTopLevelEntitySpace.FUNCTION, defineFunction.name() @@ -94,7 +96,7 @@ public class LlvmCompiler { LlvmCompilerTypes.VALUE_TYPE, List.of( new LlvmParam( - LlvmCompilerClosures.typeClosureEnvironmentPointer(Optional.of(defineFunction.freeVariables())), + closureTypes.closureEnvironmentPointer(), // TODO: allocate name properly "shed__env__0" ) @@ -109,7 +111,7 @@ public class LlvmCompiler { context = closurePointerResult.context(); var closureSizeResult = LlvmSizeOf.sizeOf( - LlvmCompilerClosures.typeClosureStruct(Optional.of(defineFunction.freeVariables())), + closureTypes.closureStruct(), context ); var closureSize = closureSizeResult.value(); @@ -117,7 +119,7 @@ public class LlvmCompiler { context = context.addInstruction(new LlvmCall( closurePointer, - LlvmCompilerClosures.typeClosurePointer(Optional.of(defineFunction.freeVariables())), + closureTypes.closurePointer(), new LlvmVariableGlobal("shed__runtime__alloc"), List.of( new LlvmTypedOperand(LlvmTypes.INT_64, closureSize) @@ -132,13 +134,13 @@ public class LlvmCompiler { context = context.addInstruction(new LlvmGetElementPtr( closureFunctionPointerPointer, - LlvmCompilerClosures.typeClosurePointer(Optional.of(defineFunction.freeVariables())), + closureTypes.closurePointer(), closurePointer, List.of(LlvmTypedOperand.arrayIndex(0), LlvmTypedOperand.structureIndex(0)) )); context = context.addInstruction(new LlvmStore( - LlvmCompilerClosures.typeClosureFunctionPointer(Optional.of(defineFunction.freeVariables())), + closureTypes.closureFunctionPointer(), new LlvmVariableGlobal(topLevelEntityName), closureFunctionPointerPointer )); @@ -154,7 +156,7 @@ public class LlvmCompiler { context = context.addInstruction(new LlvmGetElementPtr( closureEnvironmentPointer, - LlvmCompilerClosures.typeClosurePointer(Optional.of(defineFunction.freeVariables())), + closureTypes.closurePointer(), closurePointer, List.of( LlvmTypedOperand.arrayIndex(0), @@ -180,7 +182,7 @@ public class LlvmCompiler { context = context.addInstruction(new LlvmPtrToInt( closureValue, - LlvmCompilerClosures.typeClosurePointer(Optional.of(defineFunction.freeVariables())), + closureTypes.closurePointer(), closurePointer, LlvmCompilerTypes.VALUE_TYPE )); diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerClosures.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerClosures.java index daaa1ae1..518f4fce 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerClosures.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerClosures.java @@ -18,34 +18,46 @@ public class LlvmCompilerClosures { private LlvmCompilerClosures() { } - public static LlvmTypePointer typeClosurePointer(Optional<Set<String>> freeVariables) { - return LlvmTypes.pointer(typeClosureStruct(freeVariables)); - } + public record Types(Optional<Set<String>> freeVariables) { + public LlvmTypePointer closurePointer() { + return LlvmTypes.pointer(closureStruct()); + } - public static LlvmTypeStructure typeClosureStruct(Optional<Set<String>> freeVariables) { - return LlvmTypes.structure(List.of( - typeClosureFunctionPointer(freeVariables), - typeClosureEnvironment(freeVariables) - )); - } + public LlvmTypeStructure closureStruct() { + return LlvmTypes.structure(List.of( + closureFunctionPointer(), + closureEnvironment() + )); + } - public static LlvmTypePointer typeClosureFunctionPointer(Optional<Set<String>> freeVariables) { - return LlvmTypes.pointer(typeClosureFunction(freeVariables)); - } + public LlvmTypePointer closureFunctionPointer() { + return LlvmTypes.pointer(closureFunction()); + } - public static LlvmTypeFunction typeClosureFunction(Optional<Set<String>> freeVariables) { - return LlvmTypes.function( - LlvmCompilerTypes.VALUE_TYPE, - List.of(typeClosureEnvironmentPointer(freeVariables)) - ); + public LlvmTypeFunction closureFunction() { + return LlvmTypes.function( + LlvmCompilerTypes.VALUE_TYPE, + List.of(closureEnvironmentPointer()) + ); + } + + public LlvmTypePointer closureEnvironmentPointer() { + return LlvmTypes.pointer(closureEnvironment()); + } + + public LlvmTypeArray closureEnvironment() { + return LlvmTypes.array(freeVariables.map(x -> x.size()).orElse(0), LlvmCompilerTypes.VALUE_TYPE); + } } - public static LlvmTypePointer typeClosureEnvironmentPointer(Optional<Set<String>> freeVariables) { - return LlvmTypes.pointer(typeClosureEnvironment(freeVariables)); + public static final Types CALLABLE_TYPES = new Types(Optional.empty()); + + public static Types types(Set<String> freeVariables) { + return new Types(Optional.of(freeVariables)); } - public static LlvmTypeArray typeClosureEnvironment(Optional<Set<String>> freeVariables) { - return LlvmTypes.array(freeVariables.map(x -> x.size()).orElse(0), LlvmCompilerTypes.VALUE_TYPE); + public static Types types(Optional<Set<String>> freeVariables) { + return new Types(freeVariables); } public static LlvmCompilerContext call( @@ -72,7 +84,7 @@ public class LlvmCompilerClosures { context = callTargetResult.context(); var fullArgs = new ArrayList<LlvmTypedOperand>(); - fullArgs.add(new LlvmTypedOperand(typeClosureEnvironmentPointer(Optional.empty()), environmentPointer)); + fullArgs.add(new LlvmTypedOperand(CALLABLE_TYPES.closureEnvironmentPointer(), environmentPointer)); for (var arg : args) { fullArgs.add(new LlvmTypedOperand(LlvmCompilerTypes.VALUE_TYPE, arg)); } @@ -99,7 +111,7 @@ public class LlvmCompilerClosures { closurePointer, LlvmCompilerTypes.VALUE_TYPE, value, - typeClosurePointer(Optional.empty()) + CALLABLE_TYPES.closurePointer() )); return context.toResult(closurePointer); @@ -121,7 +133,7 @@ public class LlvmCompilerClosures { return context.addInstruction(new LlvmLoad( functionPointer, - typeClosureFunctionPointer(Optional.empty()), + CALLABLE_TYPES.closureFunctionPointer(), functionPointerPointer )).toResult(functionPointer); } @@ -138,7 +150,7 @@ public class LlvmCompilerClosures { context = context.addInstruction(new LlvmGetElementPtr( functionPointerPointer, - typeClosurePointer(Optional.empty()), + CALLABLE_TYPES.closurePointer(), closurePointer, List.of(LlvmTypedOperand.arrayIndex(0), LlvmTypedOperand.structureIndex(0)) )); @@ -158,7 +170,7 @@ public class LlvmCompilerClosures { context = context.addInstruction(new LlvmGetElementPtr( environmentPointer, - typeClosurePointer(Optional.empty()), + CALLABLE_TYPES.closurePointer(), closurePointer, List.of(LlvmTypedOperand.arrayIndex(0), LlvmTypedOperand.structureIndex(1)) )); diff --git a/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCompilerCallTests.java b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCompilerCallTests.java index 26804c2b..38ac3f35 100644 --- a/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCompilerCallTests.java +++ b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCompilerCallTests.java @@ -32,12 +32,12 @@ public class LlvmCompilerCallTests { has("target", x -> x.target(), isLlvmVariableLocal("closurePointer__0")), has("sourceType", x -> x.sourceType(), equalTo(LlvmCompilerTypes.VALUE_TYPE)), has("value", x -> x.value(), isLlvmVariableLocal("f")), - has("targetType", x -> x.targetType(), equalTo(LlvmCompilerClosures.typeClosurePointer(Optional.empty()))) + has("targetType", x -> x.targetType(), equalTo(LlvmCompilerClosures.CALLABLE_TYPES.closurePointer())) ), instanceOf( LlvmGetElementPtr.class, has("target", x -> x.target(), isLlvmVariableLocal("closureFunctionPointerPointer__0")), - has("pointerType", x -> x.pointerType(), equalTo(LlvmCompilerClosures.typeClosurePointer(Optional.empty()))), + has("pointerType", x -> x.pointerType(), equalTo(LlvmCompilerClosures.CALLABLE_TYPES.closurePointer())), has("pointer", x -> x.pointer(), isLlvmVariableLocal("closurePointer__0")), has("indexes", x -> x.indexes(), isSequence( isLlvmTypedOperand(LlvmTypes.INT_64, isLlvmOperandInt(0)), @@ -47,13 +47,13 @@ public class LlvmCompilerCallTests { instanceOf( LlvmLoad.class, has("target", x -> x.target(), isLlvmVariableLocal("closureFunctionPointer__0")), - has("type", x -> x.type(), equalTo(LlvmCompilerClosures.typeClosureFunctionPointer(Optional.empty()))), + has("type", x -> x.type(), equalTo(LlvmCompilerClosures.CALLABLE_TYPES.closureFunctionPointer())), has("pointer", x -> x.pointer(), isLlvmVariableLocal("closureFunctionPointerPointer__0")) ), instanceOf( LlvmGetElementPtr.class, has("target", x -> x.target(), isLlvmVariableLocal("closureEnvironmentPointer__0")), - has("pointerType", x -> x.pointerType(), equalTo(LlvmCompilerClosures.typeClosurePointer(Optional.empty()))), + has("pointerType", x -> x.pointerType(), equalTo(LlvmCompilerClosures.CALLABLE_TYPES.closurePointer())), has("pointer", x -> x.pointer(), isLlvmVariableLocal("closurePointer__0")), has("indexes", x -> x.indexes(), isSequence( isLlvmTypedOperand(LlvmTypes.INT_64, isLlvmOperandInt(0)), @@ -67,7 +67,7 @@ public class LlvmCompilerCallTests { has("functionPointer", x -> x.functionPointer(), isLlvmVariableLocal("closureFunctionPointer__0")), has("args", x -> x.args(), isSequence( isLlvmTypedOperand( - LlvmCompilerClosures.typeClosureEnvironmentPointer(Optional.empty()), + LlvmCompilerClosures.CALLABLE_TYPES.closureEnvironmentPointer(), isLlvmVariableLocal("closureEnvironmentPointer__0") ) )) @@ -98,12 +98,12 @@ public class LlvmCompilerCallTests { has("target", x -> x.target(), isLlvmVariableLocal("closurePointer__0")), has("sourceType", x -> x.sourceType(), equalTo(LlvmCompilerTypes.VALUE_TYPE)), has("value", x -> x.value(), isLlvmVariableLocal("f")), - has("targetType", x -> x.targetType(), equalTo(LlvmCompilerClosures.typeClosurePointer(Optional.empty()))) + has("targetType", x -> x.targetType(), equalTo(LlvmCompilerClosures.CALLABLE_TYPES.closurePointer())) ), instanceOf( LlvmGetElementPtr.class, has("target", x -> x.target(), isLlvmVariableLocal("closureFunctionPointerPointer__0")), - has("pointerType", x -> x.pointerType(), equalTo(LlvmCompilerClosures.typeClosurePointer(Optional.empty()))), + has("pointerType", x -> x.pointerType(), equalTo(LlvmCompilerClosures.CALLABLE_TYPES.closurePointer())), has("pointer", x -> x.pointer(), isLlvmVariableLocal("closurePointer__0")), has("indexes", x -> x.indexes(), isSequence( isLlvmTypedOperand(LlvmTypes.INT_64, isLlvmOperandInt(0)), @@ -113,13 +113,13 @@ public class LlvmCompilerCallTests { instanceOf( LlvmLoad.class, has("target", x -> x.target(), isLlvmVariableLocal("closureFunctionPointer__0")), - has("type", x -> x.type(), equalTo(LlvmCompilerClosures.typeClosureFunctionPointer(Optional.empty()))), + has("type", x -> x.type(), equalTo(LlvmCompilerClosures.CALLABLE_TYPES.closureFunctionPointer())), has("pointer", x -> x.pointer(), isLlvmVariableLocal("closureFunctionPointerPointer__0")) ), instanceOf( LlvmGetElementPtr.class, has("target", x -> x.target(), isLlvmVariableLocal("closureEnvironmentPointer__0")), - has("pointerType", x -> x.pointerType(), equalTo(LlvmCompilerClosures.typeClosurePointer(Optional.empty()))), + has("pointerType", x -> x.pointerType(), equalTo(LlvmCompilerClosures.CALLABLE_TYPES.closurePointer())), has("pointer", x -> x.pointer(), isLlvmVariableLocal("closurePointer__0")), has("indexes", x -> x.indexes(), isSequence( isLlvmTypedOperand(LlvmTypes.INT_64, isLlvmOperandInt(0)), @@ -133,7 +133,7 @@ public class LlvmCompilerCallTests { has("functionPointer", x -> x.functionPointer(), isLlvmVariableLocal("closureFunctionPointer__0")), has("args", x -> x.args(), isSequence( isLlvmTypedOperand( - LlvmCompilerClosures.typeClosureEnvironmentPointer(Optional.empty()), + LlvmCompilerClosures.CALLABLE_TYPES.closureEnvironmentPointer(), isLlvmVariableLocal("closureEnvironmentPointer__0") ), isLlvmTypedOperand(LlvmCompilerTypes.VALUE_TYPE, isLlvmVariableLocal("positional1")), |
