From 3dfd0c6534584206d2f0bb6a88b56f7201d18197 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 14 Jul 2023 22:10:03 +0100 Subject: Handle params when defining functions in WASM backend --- .../java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java | 2 ++ .../shedlang/compiler/backends/wasm/WasmCodeGenClosures.java | 11 +++++++++++ .../compiler/backends/wasm/WasmCodeGenLocalSpace.java | 1 + 3 files changed, 14 insertions(+) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java index e7ebff16..cd5f3b24 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java @@ -151,6 +151,8 @@ public class WasmCodeGen { case DefineFunctionInstruction defineFunction -> WasmCodeGenClosures.create( defineFunction.name(), + defineFunction.positionalParams(), + defineFunction.namedParams(), defineFunction.freeVariables(), functionContext -> { for (var bodyInstruction : defineFunction.body()) { diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenClosures.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenClosures.java index c63facd0..e6f60acf 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenClosures.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenClosures.java @@ -8,11 +8,13 @@ import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncName; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmLocalIndex; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmValueType; +import org.shedlang.compiler.stackir.instructions.DefineFunctionInstruction; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Function; +import java.util.stream.Stream; import static org.shedlang.compiler.util.Iterables.sortedBy; @@ -24,6 +26,8 @@ public class WasmCodeGenClosures { public static WasmCodeGenContext create( String name, + List positionalParams, + List namedParams, FreeVariables freeVariables, Function compileBody, WasmCodeGenContext context @@ -43,6 +47,13 @@ public class WasmCodeGenClosures { var closurePointer = closurePointerResult.value(); bodyContext = closurePointerResult.context(); + var shedParams = Stream.concat(positionalParams.stream(), namedParams.stream()).toList(); + + for (var shedParam : shedParams) { + var paramResult = bodyContext.addShedLocal(shedParam.name()); + bodyContext = paramResult.context(); + } + for (var freeVariable : freeVariables) { var freeVariableResult = bodyContext.addShedLocal(freeVariable.name()); var freeVariableIndex = freeVariableResult.value(); diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenLocalSpace.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenLocalSpace.java index 71c90174..534cc6fc 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenLocalSpace.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenLocalSpace.java @@ -9,6 +9,7 @@ public enum WasmCodeGenLocalSpace { LOCAL("local"), MAIN("main"), NAMESPACE("namespace"), + PARAM("param"), RECEIVER("receiver"); private final String name; -- cgit v1.2.3