Commit f035ab58 authored by Armillon Damien's avatar Armillon Damien
Browse files

rtl is generated

parent 0c2ae178
......@@ -84,8 +84,14 @@ let nop_elim_fun ({ cfgfunargs; cfgfunbody; cfgentry } as f: cfg_fun) =
replace_succs nop_transf node
) cfgfunbody in
let cfgfunbody = Hashtbl.filter_map (fun n node ->
if (Set.is_empty (preds cfgfunbody n)) then None else Some node
) cfgfunbody in
if (Set.is_empty (preds cfgfunbody n)) then
if n=cfgentry then
match node with
| Cnop(n) -> Some node
| _ -> Some node
else Some node
else Some node
) cfgfunbody in
(* La fonction renvoyée est composée du nouveau [cfgfunbody] que l'on vient de
calculer, et le point d'entrée est transformé en conséquence. *)
{f with cfgfunbody; cfgentry = replace_succ nop_transf cfgentry }
......
......@@ -42,7 +42,7 @@ let rec eval_eexpr st (e : expr) : int res =
| Eunop(op,expr) -> eval_eexpr st expr >>= fun value -> OK(eval_unop op value)
| Evar(str) ->
(match Hashtbl.find_option env str with
| None -> Error (Format.sprintf "variable not %s found" str)
| None -> Error (Format.sprintf "Unknown variable %s\n" str)
| Some(n) -> OK(n))
| Eint(n) -> OK(n)
......
......@@ -43,7 +43,21 @@ let find_var (next_reg, var2reg) v =
- [var2reg] est la nouvelle association nom de variable/registre.
*)
let rec rtl_instrs_of_cfg_expr (next_reg, var2reg) (e: expr) =
(next_reg, [], next_reg, var2reg)
match e with
| Eint(n) -> (next_reg, [Rconst(next_reg,n)], next_reg + 1, var2reg)
| Evar(str) ->
let (r',next_reg',var2reg') = find_var (next_reg, var2reg) str in
(r',[], next_reg', var2reg')
| Eunop(op,expr) ->
let (reg',operations', next_reg', var2reg') = rtl_instrs_of_cfg_expr (next_reg, var2reg) expr in
(next_reg', operations' @ [Runop(op,next_reg',reg')],next_reg'+ 1,var2reg')
| Ebinop(op,expr1,expr2) ->
let (reg, operations, next_reg, var2reg) = rtl_instrs_of_cfg_expr (next_reg, var2reg) expr1 in
let (reg',operations',next_reg',var2reg') = rtl_instrs_of_cfg_expr (next_reg, var2reg) expr2 in
(next_reg',operations @ operations' @ [Rbinop(op,next_reg', reg,reg')], next_reg'+ 1, var2reg')
let is_cmp_op =
function Eclt -> Some Rclt
......@@ -62,10 +76,24 @@ let rtl_cmp_of_cfg_expr (e: expr) =
| Some rop -> (rop, e1, e2))
| _ -> (Rcne, e, Eint 0)
let rtl_instrs_of_cfg_node ((next_reg:int), (var2reg: (string*int) list)) (c: cfg_node) =
(* TODO *)
([], next_reg, var2reg)
match c with
| Cassign(str,e,s) -> let (rres,l,next_reg',var2reg') = rtl_instrs_of_cfg_expr (next_reg, var2reg) e in
let (rvar, next_reg', var2reg') = find_var (next_reg',var2reg') str in (*create var register if needed*)
(l @ [Rmov(rvar,rres);Rjmp(s)],next_reg',var2reg')
| Ccmp(e,s1,s2) -> let (rop, e1, e2) = rtl_cmp_of_cfg_expr e in
let (rres1,l1,next_reg',var2reg') = rtl_instrs_of_cfg_expr (next_reg, var2reg) e1 in
let (rres2,l2,next_reg',var2reg') = rtl_instrs_of_cfg_expr (next_reg', var2reg') e2 in
(l1 @ l2 @ [Rbranch(rop,rres1,rres2,s1); Rjmp(s2)], next_reg', var2reg')
| Creturn(e) -> let (rres,l,next_reg',var2reg') = rtl_instrs_of_cfg_expr (next_reg, var2reg) e in
(l @ [Rret(rres)], next_reg',var2reg')
| Cprint(e,s) -> let (rres,l,next_reg',var2reg') = rtl_instrs_of_cfg_expr (next_reg, var2reg) e in
(l @ [Rprint(rres); Rjmp(s)], next_reg', var2reg')
| Cnop(s) -> ([Rjmp(s)], next_reg, var2reg)
let rtl_instrs_of_cfg_fun cfgfunname ({ cfgfunargs; cfgfunbody; cfgentry }: cfg_fun) =
let (rargs, next_reg, var2reg) =
......
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