diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-14 22:10:03 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-14 22:10:14 +0100 |
| commit | 3dfd0c6534584206d2f0bb6a88b56f7201d18197 (patch) | |
| tree | e311694d0796f56ff88265f263eca9a6af405bea | |
| parent | 2e9b821350221fc2defa89e9c4ecefc1c273855b (diff) | |
Handle params when defining functions in WASM backend
5 files changed, 128 insertions, 0 deletions
diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.WasmCodeGenDefineFunctionTests]/[method:positionalAndNamedParamsAreDefined(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.WasmCodeGenDefineFunctionTests]/[method:positionalAndNamedParamsAreDefined(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected new file mode 100644 index 00000000..062f345e --- /dev/null +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.WasmCodeGenDefineFunctionTests]/[method:positionalAndNamedParamsAreDefined(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -0,0 +1,87 @@ + +module.wasm: file format wasm 0x1 + +Section Details: + +Type[2]: + - type[0] (i32) -> i32 + - type[1] () -> nil +Import[3]: + - table[0] type=funcref initial=0 <- env.__indirect_function_table + - func[0] sig=0 <shed__runtime__alloc> <- env.shed__runtime__alloc + - memory[0] pages: initial=0 <- env.__linear_memory +Function[2]: + - func[1] sig=0 <shed__function__f__0> + - func[2] sig=1 <f> +Code[2]: + - func[1] size=37 <shed__function__f__0> + - func[2] size=47 <f> +Custom: + - name: "linking" + - symbol table [count=3] + - 0: F <shed__runtime__alloc> func=0 [ undefined explicit_name binding=global vis=default ] + - 1: F <shed__function__f__0> func=1 [ binding=global vis=default ] + - 2: F <f> func=2 [ binding=global vis=default ] +Custom: + - name: "reloc.CODE" + - relocations for section: 3 (Code) [2] + - R_WASM_FUNCTION_INDEX_LEB offset=0x00003c(file=0x0000bd) symbol=0 <shed__runtime__alloc> + - R_WASM_TABLE_INDEX_SLEB offset=0x000046(file=0x0000c7) symbol=1 <shed__function__f__0> +Custom: + - name: "name" + - func[0] <shed__runtime__alloc> + - func[1] <shed__function__f__0> + - func[2] <f> + - func[1] local[0] <closurePointer__0> + - func[1] local[1] <local__param1__0> + - func[1] local[2] <local__param2__0> + - func[1] local[3] <local__paramA__0> + - func[1] local[4] <local__paramB__0> + - func[1] local[5] <local__a__0> + - func[1] local[6] <local__b__0> + - func[2] local[0] <local__b__0> + - func[2] local[1] <local__c__0> + - func[2] local[2] <local__a__0> + - func[2] local[3] <closurePointer__0> + +Code Disassembly: + +000087 func[1] <shed__function__f__0>: + 000088: 01 7f | local[0] type=i32 + 00008a: 01 7f | local[1] type=i32 + 00008c: 01 7f | local[2] type=i32 + 00008e: 01 7f | local[3] type=i32 + 000090: 01 7f | local[4] type=i32 + 000092: 01 7f | local[5] type=i32 + 000094: 20 00 | local.get 0 <closurePointer__0> + 000096: 28 02 04 | i32.load 2 4 + 000099: 21 05 | local.set 5 <local__a__0> + 00009b: 20 00 | local.get 0 <closurePointer__0> + 00009d: 28 02 08 | i32.load 2 8 + 0000a0: 21 06 | local.set 6 <local__b__0> + 0000a2: 20 05 | local.get 5 <local__a__0> + 0000a4: 20 01 | local.get 1 <local__param1__0> + 0000a6: 6a | i32.add + 0000a7: 20 03 | local.get 3 <local__paramA__0> + 0000a9: 6a | i32.add + 0000aa: 0f | return + 0000ab: 0b | end +0000b1 func[2] <f>: + 0000b2: 01 7f | local[0] type=i32 + 0000b4: 01 7f | local[1] type=i32 + 0000b6: 01 7f | local[2] type=i32 + 0000b8: 01 7f | local[3] type=i32 + 0000ba: 41 0c | i32.const 12 + 0000bc: 10 80 80 80 80 00 | call 0 <shed__runtime__alloc> + 0000c2: 21 03 | local.set 3 <closurePointer__0> + 0000c4: 20 03 | local.get 3 <closurePointer__0> + 0000c6: 41 80 80 80 80 00 | i32.const 0 + 0000cc: 36 02 00 | i32.store 2 0 + 0000cf: 20 03 | local.get 3 <closurePointer__0> + 0000d1: 20 02 | local.get 2 <local__a__0> + 0000d3: 36 02 04 | i32.store 2 4 + 0000d6: 20 03 | local.get 3 <closurePointer__0> + 0000d8: 20 00 | local.get 0 <local__b__0> + 0000da: 36 02 08 | i32.store 2 8 + 0000dd: 20 03 | local.get 3 <closurePointer__0> + 0000df: 0b | end
\ No newline at end of file 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; 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)); + } } |
