Verified Commit e296a836 authored by Camil Staps's avatar Camil Staps 🚀

WIP: use block parameters and let for temporary values in the WebAssembly interpreter

parent 4cda3b0b
Pipeline #48161 failed with stages
in 20 minutes and 18 seconds
......@@ -10,9 +10,10 @@ instance wasm_type TWord, TPtrOffset, TBool, TChar, TShort, TInt, TReal, TDReal,
:: TempVars
:: Target =
{ stmts :: ![Ex]
, instrs :: ![String]
, temp_vars :: !TempVars
{ stmts :: ![Ex]
, instrs :: ![String]
, temp_vars :: !TempVars
, var_counter :: !Int
}
append e t :== {t & stmts=[e:t.stmts]}
......
......@@ -53,9 +53,10 @@ where
start :: Target
start =
{ instrs = []
, stmts = []
, temp_vars = {tv_i32=0,tv_i64=0,tv_f64=0}
{ instrs = []
, stmts = []
, temp_vars = {tv_i32=0,tv_i64=0,tv_f64=0}
, var_counter = 0
}
new_temp_var :: !Type !Target -> (!Variable, !Target)
......@@ -72,7 +73,7 @@ new_temp_var tp t
I32 -> {t & temp_vars.tv_i32=t.temp_vars.tv_i32+1}
I64 -> {t & temp_vars.tv_i64=t.temp_vars.tv_i64+1}
F64 -> {t & temp_vars.tv_f64=t.temp_vars.tv_f64+1}
= (IF_GLOBAL_TEMP_VARS Global Local var,t)
= (/*IF_GLOBAL_TEMP_VARS Global*/ Local var,t)
i32_temp_vars =: {#{#'v','w',i} \\ i <- ['0'..'9']}
i64_temp_vars =: {#{#'v','q',i} \\ i <- ['0'..'9']}
......@@ -225,9 +226,10 @@ where
gc_block loop_label = IF_GLOBAL_RT_VARS
[ ") ;; block abc-gc"
, toString (Eset temp Hp_free)
, toString Hp_free
, toString (Ecall "clean_gc" [A])
, toString (Eif (Hp_free <=. Ivar temp))
, toString Hp_free
, "(if (i32.le_s)"
, "(then"
, toString (Ecall "clean_out_of_memory" [])
, toString Eunreachable
......@@ -249,8 +251,6 @@ where
, toString (Ebr loop_label)
, ") ;; loop abc-loop"
]
where
temp = IF_GLOBAL_TEMP_VARS Global Local "vw0"
type x :== Type TypeInferenceSettings x
type2 x y :== Type2 TypeInferenceSettings x y
......@@ -488,9 +488,26 @@ nop t = t
(:.) first then t = then (first t)
new_local :: !t !(Expr t) !((Expr t) Target -> Target) !Target -> Target | wasm_type t
new_local tp e f t
# (var,t) = new_temp_var (wasm_type tp) t
= f (Ivar var) (append (Eset var e) t)
new_local tp e f t=:{var_counter} = (
append e :.
append (Eblock_and_let upgraded_type name) :.
f (Ivar (Local name)) :.
append Eend :.
append Eend
) {t & var_counter=var_counter+1}
where
name = prefix+++toString var_counter
upgraded_type = case wasm_type tp of
I8 -> I64
I16 -> I64
t -> t
prefix = case upgraded_type of
I32 -> "vw"
I8 -> "vq"
I16 -> "vq"
I64 -> "vq"
F64 -> "vd"
t -> abort ("new_local: unexpected type "+++toString t+++"\n")
set :: !Ex !Ex !Target -> Target
set var e t = append expr t
......@@ -623,9 +640,7 @@ where
sft = type_width_shift ptr_type
new_double_real :: !(Expr (TPtr t)) !((Expr TDReal) Target -> Target) !Target -> Target
new_double_real ptr cont t
# (var,t) = new_temp_var (wasm_type TDReal) t
= cont (Ivar var) (append (Eset var expr) t)
new_double_real ptr cont t = new_local TDReal expr cont t
where
expr = Ereinterpret F64 I64
(Eor I64
......
......@@ -35,6 +35,7 @@ instance wasm_literal Int, Char
| Ereturn !Ex
| Eblock
| Eblock_and_let !Type !String
| Eloop
| Ebr !Label
| Ebr_local !Int
......
......@@ -58,6 +58,7 @@ subexpressions e = case e of
Ereturn e` -> [e:subexpressions e`]
Eblock -> [e]
Eblock_and_let _ _ -> [e]
Eloop -> [e]
Ebr _ -> [e]
Ebr_local _ -> [e]
......@@ -171,6 +172,8 @@ where
Ereturn e -> "(return "+++toString e+++")"
Eblock -> "(block"
Eblock_and_let type name ->
"(block (param "+++toString type+++") (let (local $"+++name+++" "+++toString type+++")"
Eloop -> "(loop"
Ebr l -> "(br $"+++l+++")"
Ebr_local i -> "(br "+++toString i+++")"
......@@ -254,6 +257,7 @@ optimize set e = case e of
Ereturn e -> Ereturn (optimize set e)
Eblock -> e
Eblock_and_let _ _ -> e
Eloop -> e
Ebr l -> case [new \\ (old,new) <- set.rename_labels | old==l] of
[new:_] -> Ebr new
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment