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 --- .../compiler/backends/wasm/WasmCodeGen.java | 2 ++ .../backends/wasm/WasmCodeGenClosures.java | 11 +++++++++ .../backends/wasm/WasmCodeGenLocalSpace.java | 1 + .../wasm/WasmCodeGenDefineFunctionTests.java | 27 ++++++++++++++++++++++ 4 files changed, 41 insertions(+) (limited to 'src') 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; diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenDefineFunctionTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenDefineFunctionTests.java index 87a6dee6..d32f716a 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenDefineFunctionTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenDefineFunctionTests.java @@ -58,4 +58,31 @@ public class WasmCodeGenDefineFunctionTests { snapshotter.assertSnapshot(objdumpContext(result)); } + + @Test + public void positionalAndNamedParamsAreDefined(Snapshotter snapshotter) throws IOException, InterruptedException { + var instruction = new DefineFunctionInstruction( + "f", + List.of(Instructions.param("param1"), Instructions.param("param2")), + List.of(Instructions.param("paramA"), Instructions.param("paramB")), + FreeVariables.of("a", "b"), + List.of( + Instructions.localLoad("a"), + Instructions.localLoad("param1"), + Instructions.intAdd(), + Instructions.localLoad("paramA"), + Instructions.intAdd(), + Instructions.return_() + ), + Sources.NULL + ); + var context = WasmCodeGenContext.EMPTY + .addShedLocal("b").context() + .addShedLocal("c").context() + .addShedLocal("a").context(); + + var result = WasmCodeGen.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(objdumpContext(result)); + } } -- cgit v1.2.3