Commit ae384a8f authored by Wilke Pierre's avatar Wilke Pierre
Browse files

sujet TP RTL + modifications mineures

parent 8b38e0b0
No preview for this file type
......@@ -26,18 +26,20 @@ type cfg_fun = {
type cprog = cfg_fun prog
(* [succs cfg n] gives the successors of a node [n] in a CFG [cfg]. *)
(* [succs cfg n] donne l'ensemble des successeurs d'un nœud [n] dans un CFG
[cfg]. *)
let succs cfg n =
match Hashtbl.find_option cfg n with
| None -> []
| None -> Set.empty
| Some (Cprint (_, s))
| Some (Cassign (_, _, s)) -> [s]
| Some (Creturn _) -> []
| Some (Ccmp (_, s1, s2)) -> [s1;s2]
| Some (Cnop s) -> [s]
| Some (Cassign (_, _, s)) -> Set.singleton s
| Some (Creturn _) -> Set.empty
| Some (Ccmp (_, s1, s2)) -> Set.of_list [s1;s2]
| Some (Cnop s) -> Set.singleton s
(* [preds n] gives the list of predecessors of a node [n]. *)
(* [preds cfg n] donne l'ensemble des prédécesseurs d'un nœud [n] dans un CFG [cfg]
*)
let preds cfgfunbody n =
Hashtbl.fold (fun m m' acc ->
match m' with
......@@ -73,21 +75,3 @@ let rec size_instr (i: cfg_node) : int =
let size_fun f =
Hashtbl.fold (fun k v acc -> acc + size_instr v) f 0
let fixpoint
(transfer: (int, cfg_node) Hashtbl.t -> (int, 'a) Hashtbl.t -> int -> 'a)
(init: (int, cfg_node) Hashtbl.t -> int -> cfg_node -> 'a)
(cfg: (int, cfg_node) Hashtbl.t) :
(int, 'a) Hashtbl.t =
let res = Hashtbl.map (fun k v -> init cfg k v) cfg in
let iter res =
Hashtbl.fold (fun n oldstate changed ->
let newstate = transfer cfg res n in
Hashtbl.replace res n newstate;
changed || not (Set.equal newstate oldstate)
) res false in
let rec fix () =
if iter res
then fix ()
else () in
fix ();
res
......@@ -85,8 +85,7 @@ let eval_eprog oc (ep: eprog) (memsize: int) (params: int list)
: int option res =
let st = init_state memsize in
find_function ep "main" >>= fun f ->
(* trim the parameter list to only take as many as required by the function.
*)
(* ne garde que le nombre nécessaire de paramètres pour la fonction "main". *)
let n = List.length f.funargs in
let params = take n params in
eval_efun oc st f "main" params >>= fun (v, st) ->
......
......@@ -161,7 +161,7 @@ let dump file dumpf p additional_command =
(Format.formatter_of_out_channel oc, fun () -> close_out oc)
in
dumpf oc p; close ();
additional_command file ()
if file <> "-" then additional_command file ()
end
......
......@@ -25,10 +25,9 @@ open Utils
*)
let find_var (next_reg, var2reg) v =
begin match List.assoc_opt v var2reg with
match List.assoc_opt v var2reg with
| Some r -> (r, next_reg, var2reg)
| None -> (next_reg, next_reg + 1, assoc_set var2reg v next_reg)
end
(* [rtl_instrs_of_cfg_expr (next_reg, var2reg) e] construit une liste
d'instructions RTL correspondant à l'évaluation d'une expression E.
......@@ -52,9 +51,12 @@ let is_cmp_op =
| Ecne -> Some Rcne
| _ -> None
let is_cmp (e: expr) =
let rtl_cmp_of_cfg_expr (e: expr) =
match e with
| Ebinop (b, e1, e2) -> (match is_cmp_op b with | None -> (Rcne, e, Eint 0) | Some rop -> (rop, e1, e2))
| Ebinop (b, e1, e2) ->
(match is_cmp_op b with
| None -> (Rcne, e, Eint 0)
| Some rop -> (rop, e1, e2))
| _ -> (Rcne, e, Eint 0)
......
Supports Markdown
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