change RET instruction to take an argument

This commit is contained in:
tali 2023-12-06 22:25:16 -05:00
parent 44406a233e
commit d7bf73a17c
2 changed files with 8 additions and 6 deletions

View File

@ -25,7 +25,7 @@ type ins =
| CON of reg * vtable | CON of reg * vtable
| CAL of reg * reg * reg * reg list | CAL of reg * reg * reg * reg list
(* control flow *) (* control flow *)
| RET | RET of reg
| JMP of block | JMP of block
| CBR of reg * block * block | CBR of reg * block * block
@ -58,7 +58,7 @@ let frame_size t =
in in
let meas (R i) fs = max fs (i + 1) in let meas (R i) fs = max fs (i + 1) in
let meas_ins fs = function let meas_ins fs = function
| RET -> fs | RET r
| LDI (r, _) | LDI (r, _)
| CON (r, _) | CON (r, _)
-> meas r fs -> meas r fs
@ -128,7 +128,7 @@ let pp_ins ~label ppf = function
Fmt.pf ppf "cal %a, %a[%a](" pp_reg a pp_reg b pp_reg c; Fmt.pf ppf "cal %a, %a[%a](" pp_reg a pp_reg b pp_reg c;
List.iteri (fun i d -> if i > 0 then Fmt.pf ppf ","; pp_reg ppf d) ds; List.iteri (fun i d -> if i > 0 then Fmt.pf ppf ","; pp_reg ppf d) ds;
Fmt.pf ppf ")" Fmt.pf ppf ")"
| RET -> Fmt.pf ppf "ret" | RET a -> Fmt.pf ppf "ret %a" pp_reg a
| JMP b -> Fmt.pf ppf "jmp %s" (label b) | JMP b -> Fmt.pf ppf "jmp %s" (label b)
| CBR (a, b1, b2) -> | CBR (a, b1, b2) ->
let l1 = label b1 in let l1 = label b1 in

View File

@ -60,6 +60,7 @@ end
type frame = { type frame = {
r : Value.t array; r : Value.t array;
mutable pc : Code.ins list; mutable pc : Code.ins list;
mutable rv : Value.t;
} }
let jmp fr b = fr.pc <- Code.instructions b let jmp fr b = fr.pc <- Code.instructions b
@ -85,7 +86,8 @@ let rec exec ({ r; _ } as fr) = function
| Code.JMP b -> jmp fr b | Code.JMP b -> jmp fr b
| Code.CBR (R a, b1, b2) -> | Code.CBR (R a, b1, b2) ->
jmp fr (if Value.truthy r.(a) then b1 else b2) jmp fr (if Value.truthy r.(a) then b1 else b2)
| Code.RET -> | Code.RET (R a) ->
fr.rv <- r.(a);
fr.pc <- [] fr.pc <- []
and call mthd self args = and call mthd self args =
@ -108,9 +110,9 @@ and step fr =
and run prog self (* args *) = and run prog self (* args *) =
let r = Array.make (Code.frame_size prog) Value.Nil in let r = Array.make (Code.frame_size prog) Value.Nil in
let fr = { r; pc = [] } in let fr = { r; pc = []; rv = Nil } in
r.(0) <- self; r.(0) <- self;
jmp fr prog.entry; jmp fr prog.entry;
step fr; step fr;
r.(0) fr.rv