aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-14 22:10:03 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-14 22:10:14 +0100
commit3dfd0c6534584206d2f0bb6a88b56f7201d18197 (patch)
treee311694d0796f56ff88265f263eca9a6af405bea /src/main/java/org
parent2e9b821350221fc2defa89e9c4ecefc1c273855b (diff)
Handle params when defining functions in WASM backend
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java2
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenClosures.java11
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenLocalSpace.java1
3 files changed, 14 insertions, 0 deletions
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<DefineFunctionInstruction.Param> positionalParams,
+ List<DefineFunctionInstruction.Param> namedParams,
FreeVariables freeVariables,
Function<WasmCodeGenContext, WasmCodeGenContext> 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;