Skip to content

Commit c2f9449

Browse files
authored
Single type annotation on switch (#81)
This patch redefines the `switch` instruction such that it only takes a single immediate type (in addition to the tag). Resolves #76.
1 parent bdcb1bc commit c2f9449

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

proposals/stack-switching/Explainer.md

+8-11
Original file line numberDiff line numberDiff line change
@@ -336,17 +336,15 @@ instruction, which also relies on tags, to transfer control from the
336336
original continuation directly to `$c`, thus avoiding the need for an
337337
intermediate stack switch to the parent.
338338

339-
Concretely, executing `switch $ct $ct $yield (local.get $c)` in our
339+
Concretely, executing `switch $ct $yield (local.get $c)` in our
340340
example behaves equivalently to `(suspend $yield)`, assuming that the
341341
active (ordinary) handler for `$yield` immediately resumes `$c` and
342342
additionally passes the continuation obtained from handling `$yield`
343343
along as an argument to `$c`. However, as mentioned above, using a
344344
`switch` instruction here has the advantage that a Wasm engine can
345345
implement it directly using only a single stack switch.
346346
Each `switch` instruction is annotated with the type of the
347-
continuation switched to and the type of the continuation that arises
348-
from the current context, respectively. Both types are identical in
349-
our example.
347+
continuation switched to.
350348

351349
The key idea is to inline scheduling logic in the tasks themselves in
352350
order to reduce (or avoid altogether) the need to switch stacks to the
@@ -406,7 +404,7 @@ code.
406404
;; Switch to $next_task.
407405
;; The switch instruction implicitly passes a reference to the currently
408406
;; executing continuation as an argument to $next_task.
409-
(switch $ct $ct $yield (local.get $next_task))
407+
(switch $ct $yield (local.get $next_task))
410408
;; If we get here, some other continuation switch-ed directly to us, or
411409
;; $entry resumed us.
412410
;; In the first case, we receive the continuation that switched to us here
@@ -599,16 +597,15 @@ The third way to invoke a continuation is to perform a symmetric
599597
switch.
600598

601599
```wast
602-
switch $ct1 $ct2 $e : [t1* (ref $ct1)] -> [t2*]
600+
switch $ct1 $e : [t1* (ref $ct1)] -> [t2*]
603601
where:
604602
- $e : [] -> [t*]
605603
- $ct1 = cont [t1* (ref $ct2)] -> [t*]
606604
- $ct2 = cont [t2*] -> [t*]
607605
```
608606

609607
The `switch` instruction is parameterised by the type of the
610-
continuation argument (`$ct1`), the type of the continuation arising
611-
from suspending the current context (`$ct2`), and a control tag
608+
continuation argument (`$ct1`) and a control tag
612609
(`$e`). It suspends the current continuation (of type `$ct2`), then
613610
performs a direct switch to the suspended peer continuation (of type
614611
`$ct1`), passing in the required parameters (including the just
@@ -812,10 +809,10 @@ This abbreviation will be formalised with an auxiliary function or other means i
812809
- iff `C.tags[$t] = tag $ft`
813810
- and `C.types[$ft] ~~ func [t1*] -> [t2*]`
814811

815-
- `switch <typeidx> <typeidx> <tagidx>`
812+
- `switch <typeidx> <tagidx>`
816813
- Switch to executing a given continuation directly, suspending the current execution.
817814
- The suspension and switch are performed from the perspective of a parent `(on $e switch)` handler, determined by the annotated control tag.
818-
- `switch $ct1 $ct2 $e : [t1* (ref null $ct1)] -> [t2*]`
815+
- `switch $ct1 $e : [t1* (ref null $ct1)] -> [t2*]`
819816
- iff `C.tags[$e] = tag $ft`
820817
- and `C.types[$ft] ~~ func [] -> [t*]`
821818
- and `C.types[$ct1] ~~ cont [t1* (ref null? $ct2)] -> [te1*]`
@@ -865,7 +862,7 @@ We use the use the opcode space `0xe0-0xe5` for the seven new instructions.
865862
| 0xe2 | `suspend $t` | `$t : u32` |
866863
| 0xe3 | `resume $ct hdl*` | `$ct : u32` (for hdl see below) |
867864
| 0xe4 | `resume_throw $ct $e hdl*` | `$ct : u32`, `$e : u32` (for hdl see below) |
868-
| 0xe5 | `switch $ct1 $ct2 $e` | `$ct1 : u32`, `$ct2 : u32`, `$e : u32` |
865+
| 0xe5 | `switch $ct1 $e` | `$ct1 : u32`, `$e : u32` |
869866

870867
In the case of `resume` and `resume_throw` we use a leading byte to
871868
indicate the shape of `hdl` as follows.

0 commit comments

Comments
 (0)