-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
252 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
(module | ||
(type (;0;) (func (param i32))) | ||
(type (;1;) (cont 0)) | ||
(type (;2;) (func (result i32))) | ||
(type (;3;) (func (param i32 i32))) | ||
(type (;4;) (func)) | ||
(import "spectest" "print_i32" (func (;0;) (type 0))) | ||
(tag (;0;) (type 2) (result i32)) | ||
(export "_start" (func 4)) | ||
(start 4) | ||
(elem (;0;) declare func 3) | ||
(func (;1;) (type 0) (param i32) | ||
local.get 0 | ||
call 0 | ||
suspend 0 | ||
call 0 | ||
) | ||
(func (;2;) (type 3) (param i32 i32) | ||
(local i32) | ||
local.get 0 | ||
local.set 2 | ||
block ;; label = @1 | ||
loop ;; label = @2 | ||
local.get 2 | ||
local.get 1 | ||
i32.gt_u | ||
br_if 1 (;@1;) | ||
local.get 2 | ||
call 1 | ||
local.get 2 | ||
i32.const 1 | ||
i32.add | ||
local.set 2 | ||
br 0 (;@2;) | ||
end | ||
end | ||
) | ||
(func (;3;) (type 0) (param i32) | ||
local.get 0 | ||
i32.const 13 | ||
call 2 | ||
) | ||
(func (;4;) (type 4) | ||
(local (ref 1) i32) | ||
ref.func 3 | ||
cont.new 1 | ||
local.set 0 | ||
i32.const 10 | ||
local.set 1 | ||
block ;; label = @1 | ||
loop ;; label = @2 | ||
block (result (ref 1)) ;; label = @3 | ||
local.get 1 | ||
local.get 0 | ||
resume 1 (on 0 0 (;@3;)) | ||
i32.const -2 | ||
call 0 | ||
br 2 (;@1;) | ||
end | ||
local.set 0 | ||
local.get 1 | ||
i32.const 1 | ||
i32.add | ||
local.set 1 | ||
i32.const -1 | ||
call 0 | ||
br 0 (;@2;) | ||
end | ||
end | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
(module | ||
(import "spectest" "print_i32" (func $print_i32 (param i32))) | ||
(type $task (func (param i32))) | ||
(type $cont (cont $task)) | ||
|
||
(tag $yield (result i32)) | ||
;; DH: tag's input + continuation | ||
;; == block's output ((ref $cont)) | ||
;; == the values on the stack after suspend | ||
;; tag's output | ||
;; == continuation's input | ||
;; == the values on the stack when suspended computation is resumed | ||
|
||
(func $process (param $x i32) | ||
(call $print_i32 (local.get $x)) | ||
(suspend $yield) ;; DH: levaes a continuation on the stack, jump to the tag $yield | ||
;; when come back, the stack should contains a i32 value, since the return type of $yield is i32 | ||
(call $print_i32)) | ||
|
||
(func $range (param $from i32) (param $to i32) | ||
(local $i i32) | ||
(local.set $i (local.get $from)) | ||
(block $b | ||
(loop $l | ||
(br_if $b (i32.gt_u (local.get $i) (local.get $to))) | ||
(call $process (local.get $i)) | ||
(local.set $i (i32.add (local.get $i) (i32.const 1))) | ||
(br $l)))) | ||
|
||
(func $task1 (param $x i32) (call $range (local.get $x) (i32.const 13))) | ||
|
||
(func $main (export "_start") | ||
(local $k (ref $cont)) | ||
(local $i i32) | ||
(local.set $k (cont.new $cont (ref.func $task1))) | ||
(local.set $i (i32.const 10)) | ||
(block $h | ||
(loop $l | ||
(block $on_yield (result (ref $cont)) | ||
(resume $cont | ||
(on $yield $on_yield) | ||
(local.get $i) | ||
(local.get $k)) | ||
(call $print_i32 (i32.const -2)) ;; this code is executed only when no suspend is called in $k | ||
(br $h)) | ||
;; $on_yield lands here, with the continuation on the stack | ||
(local.set $k) ;; grab the continuation and save it | ||
(local.set $i (i32.add (local.get $i) (i32.const 1))) | ||
(call $print_i32 (i32.const -1)) | ||
(br $l)))) | ||
|
||
(elem declare func $task1) | ||
|
||
(start $main) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.