aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-14 23:01:44 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-14 23:01:44 +0100
commiteeef2f1636e2fc96c0018532d1e6945ee97c43c6 (patch)
treefaaca4f0a01417db5230435f356b50a7f36ee6d1
parent9b0f33fddc49fad7b8f341614bf083bd130783b6 (diff)
Fix WASM function type when function has params
-rw-r--r--snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.WasmCodeGenDefineFunctionTests]/[method:positionalAndNamedParamsAreDefined(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected89
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenClosures.java12
2 files changed, 52 insertions, 49 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
index 062f345e..52572aa0 100644
--- 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
@@ -3,18 +3,19 @@ module.wasm: file format wasm 0x1
Section Details:
-Type[2]:
+Type[3]:
- type[0] (i32) -> i32
- - type[1] () -> nil
+ - type[1] (i32, i32, i32, i32, i32) -> i32
+ - type[2] () -> 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>
+ - func[1] sig=1 <shed__function__f__0>
+ - func[2] sig=2 <f>
Code[2]:
- - func[1] size=37 <shed__function__f__0>
+ - func[1] size=29 <shed__function__f__0>
- func[2] size=47 <f>
Custom:
- name: "linking"
@@ -25,8 +26,8 @@ Custom:
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>
+ - R_WASM_FUNCTION_INDEX_LEB offset=0x000034(file=0x0000be) symbol=0 <shed__runtime__alloc>
+ - R_WASM_TABLE_INDEX_SLEB offset=0x00003e(file=0x0000c8) symbol=1 <shed__function__f__0>
Custom:
- name: "name"
- func[0] <shed__runtime__alloc>
@@ -46,42 +47,38 @@ Custom:
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
+000090 func[1] <shed__function__f__0>:
+ 000091: 01 7f | local[5] type=i32
+ 000093: 01 7f | local[6] type=i32
+ 000095: 20 00 | local.get 0 <closurePointer__0>
+ 000097: 28 02 04 | i32.load 2 4
+ 00009a: 21 05 | local.set 5 <local__a__0>
+ 00009c: 20 00 | local.get 0 <closurePointer__0>
+ 00009e: 28 02 08 | i32.load 2 8
+ 0000a1: 21 06 | local.set 6 <local__b__0>
+ 0000a3: 20 05 | local.get 5 <local__a__0>
+ 0000a5: 20 01 | local.get 1 <local__param1__0>
+ 0000a7: 6a | i32.add
+ 0000a8: 20 03 | local.get 3 <local__paramA__0>
+ 0000aa: 6a | i32.add
+ 0000ab: 0f | return
+ 0000ac: 0b | end
+0000b2 func[2] <f>:
+ 0000b3: 01 7f | local[0] type=i32
+ 0000b5: 01 7f | local[1] type=i32
+ 0000b7: 01 7f | local[2] type=i32
+ 0000b9: 01 7f | local[3] type=i32
+ 0000bb: 41 0c | i32.const 12
+ 0000bd: 10 80 80 80 80 00 | call 0 <shed__runtime__alloc>
+ 0000c3: 21 03 | local.set 3 <closurePointer__0>
+ 0000c5: 20 03 | local.get 3 <closurePointer__0>
+ 0000c7: 41 80 80 80 80 00 | i32.const 0
+ 0000cd: 36 02 00 | i32.store 2 0
+ 0000d0: 20 03 | local.get 3 <closurePointer__0>
+ 0000d2: 20 02 | local.get 2 <local__a__0>
+ 0000d4: 36 02 04 | i32.store 2 4
+ 0000d7: 20 03 | local.get 3 <closurePointer__0>
+ 0000d9: 20 00 | local.get 0 <local__b__0>
+ 0000db: 36 02 08 | i32.store 2 8
+ 0000de: 20 03 | local.get 3 <closurePointer__0>
+ 0000e0: 0b | end \ No newline at end of file
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 e6f60acf..28fe631c 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenClosures.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenClosures.java
@@ -37,8 +37,16 @@ public class WasmCodeGenClosures {
name
);
+ var shedParams = Stream.concat(positionalParams.stream(), namedParams.stream()).toList();
+
+ var wasmParamTypes = new ArrayList<WasmValueType>();
+ wasmParamTypes.add(CLOSURE_TYPE);
+ for (var shedParam : shedParams) {
+ wasmParamTypes.add(WasmCodeGenTypes.VALUE_TYPE);
+ }
+
var closureFuncType = new WasmFuncType(
- List.of(CLOSURE_TYPE),
+ wasmParamTypes,
List.of(WasmCodeGenTypes.VALUE_TYPE)
);
@@ -47,8 +55,6 @@ 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();