Implement the env shuffling to enable blocks with labels and breaking to them (via 'hidden' depth constant added to each block env, from which the block depth is subtracted in the br vau

This commit is contained in:
Nathan Braswell
2021-07-25 02:14:20 -04:00
parent ece3844c88
commit 3ad51ce19d
2 changed files with 14 additions and 6 deletions

View File

@@ -8,12 +8,16 @@
(func $add (param $num i32) (result i32) (func $add (param $num i32) (result i32)
(local $tmp1 i32) (local $tmp1 i32)
(local $tmp2 i32) (local $tmp2 i32)
(block (block $test
(i32.const 1337) (i32.const 1337)
(i32.const 1338) (i32.const 1338)
drop drop
drop drop
(br 0) (block $inner_test
(br $inner_test)
(br $test)
)
(br $test)
drop drop
) )
(i32.const 11) (i32.const 11)

12
wasm.kp
View File

@@ -176,7 +176,7 @@
(= nil cur_typ) (recurse cur_list (idx (idx locals i) 2) 1 (+ 1 i)) (= nil cur_typ) (recurse cur_list (idx (idx locals i) 2) 1 (+ 1 i))
true (recurse (concat cur_list [[cur_num cur_typ]]) (idx (idx locals i) 2) 1 (+ 1 i))) true (recurse (concat cur_list [[cur_num cur_typ]]) (idx (idx locals i) 2) 1 (+ 1 i)))
) [] nil 0 0) ) [] nil 0 0)
inner_env (add-dict-to-env de inner_name_dict) inner_env (add-dict-to-env de (put inner_name_dict 'depth 0))
our_type [ (map (lambda (x) (idx x 2)) params) (slice result 1 -1) ] our_type [ (map (lambda (x) (idx x 2)) params) (slice result 1 -1) ]
_ (println "about to get our_code") _ (println "about to get our_code")
our_code (flat_map (lambda (x) (let (ins (eval x inner_env)) our_code (flat_map (lambda (x) (let (ins (eval x inner_env))
@@ -203,9 +203,13 @@
i32.const (lambda (const) [['i32.const const]]) i32.const (lambda (const) [['i32.const const]])
local.get (lambda (const) [['local.get const]]) local.get (lambda (const) [['local.get const]])
i32.add (lambda (& flatten) (concat (flat_map (lambda (x) x) flatten) [['i32.add]])) i32.add (lambda (& flatten) (concat (flat_map (lambda (x) x) flatten) [['i32.add]]))
block (lambda (& inner) [['block [] (flat_map (lambda (x) (cond (array? x) x block (vau de (name & inner) (let (
true (x))) inner)]]) new_depth (+ 1 (eval 'depth de))
br (lambda (const) [['br const]]) inner_env (add-dict-to-env de [[ name [new_depth] ] [ 'depth new_depth ]])
) [['block [] (flat_map (lambda (x) (let (ins (eval x inner_env)) (cond (array? ins) ins
true (ins)))) inner)]]))
br (vau de (b) (let (block (eval b de)) (if (int? block) [['br block]]
[['br (eval [- 'depth (idx block 0)] de)]])))
export (vau de (name t_v) (lambda (name_dict type function memory export code) export (vau de (name t_v) (lambda (name_dict type function memory export code)
[ name_dict type function memory (concat export [ [ name (idx t_v 0) (get-value name_dict (idx t_v 1)) ] ]) code ] [ name_dict type function memory (concat export [ [ name (idx t_v 0) (get-value name_dict (idx t_v 1)) ] ]) code ]
)) ))