From 3ad51ce19d1d9dac7623206d3d7d721851c7dd10 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Sun, 25 Jul 2021 02:14:20 -0400 Subject: [PATCH] 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 --- comp_wasm.kp | 8 ++++++-- wasm.kp | 12 ++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/comp_wasm.kp b/comp_wasm.kp index 98b2ae7..fb6f45f 100644 --- a/comp_wasm.kp +++ b/comp_wasm.kp @@ -8,12 +8,16 @@ (func $add (param $num i32) (result i32) (local $tmp1 i32) (local $tmp2 i32) - (block + (block $test (i32.const 1337) (i32.const 1338) drop drop - (br 0) + (block $inner_test + (br $inner_test) + (br $test) + ) + (br $test) drop ) (i32.const 11) diff --git a/wasm.kp b/wasm.kp index 252ecfa..6629f4d 100644 --- a/wasm.kp +++ b/wasm.kp @@ -176,7 +176,7 @@ (= 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))) ) [] 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) ] _ (println "about to get our_code") our_code (flat_map (lambda (x) (let (ins (eval x inner_env)) @@ -203,9 +203,13 @@ i32.const (lambda (const) [['i32.const const]]) local.get (lambda (const) [['local.get const]]) 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 - true (x))) inner)]]) - br (lambda (const) [['br const]]) + block (vau de (name & inner) (let ( + new_depth (+ 1 (eval 'depth de)) + 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) [ name_dict type function memory (concat export [ [ name (idx t_v 0) (get-value name_dict (idx t_v 1)) ] ]) code ] ))