Live Variables

This commit is contained in:
elvis
2024-12-21 02:16:04 +01:00
parent 1f0d48263a
commit f1b4c3a17d
8 changed files with 209 additions and 60 deletions

View File

@ -207,46 +207,8 @@ let compute_defined_variables (cfg: RISCCfg.t) : DVCfg.t =
let check_defined_variables (dvcfg: DVCfg.t) : bool =
let helper node (dvcfg: DVCfg.t) =
let code = match Cfg.NodeMap.find_opt node dvcfg.t.content with
None -> []
| Some c -> c
in
let internalvar = Cfg.NodeMap.find node dvcfg.internalvar in
let vua = variables_used_all code in
let outvar = (* is true if we are in the last node and the out variable is
not in vout, so its true if the out variable is not
defined *)
match (Option.equal (=) (Some node) dvcfg.t.terminal,
dvcfg.t.inputOutputVar,
internalvar.internalout) with
| (true, Some (_, outvar), vout) ->
not (List.mem outvar vout)
| (_, _, _) ->
false
in
if Utility.inclusion vua (internalvar.internalin) then
not outvar
else
(* the variable might be defined inside the block, so check all vin and
return true only if all variables are properly defined *)
let vuabetween = List.map variables_used code in
let check = List.fold_left
(fun acc (codevars, (vin, _vout)) ->
acc && (Utility.inclusion codevars vin))
true
(List.combine vuabetween internalvar.internalbetween)
in
check && (not outvar)
in
Cfg.NodeSet.fold (fun node acc -> acc && (helper node dvcfg)) dvcfg.t.nodes true
let undefined_variables (dvcfg: DVCfg.t) : Variable.t list =
let helper (node: Cfg.Node.t) (dvcfg: DVCfg.t) =
let check_undefined_variables (dvcfg: DVCfg.t) : Variable.t list option =
let helper (node: Cfg.Node.t) (dvcfg: DVCfg.t) : Variable.t list option =
let code = match Cfg.NodeMap.find_opt node dvcfg.t.content with
None -> []
| Some c -> c
@ -267,7 +229,8 @@ let undefined_variables (dvcfg: DVCfg.t) : Variable.t list =
in
if Utility.inclusion vua (internalvar.internalin) then
match outvar with None -> [] | Some outvar -> [outvar]
match outvar with None -> None
| Some outvar -> Some [outvar]
else
(* the variable might be defined inside the block, so check all vin and
return true only if all variables are properly defined *)
@ -278,9 +241,19 @@ let undefined_variables (dvcfg: DVCfg.t) : Variable.t list =
[]
(List.combine vuabetween internalvar.internalbetween)
in
match outvar with None -> undef_vars | Some outvar -> outvar :: undef_vars
match outvar, undef_vars with
None, [] -> None
| None, undef_vars -> Some undef_vars
| Some outvar, [] -> Some [outvar]
| Some outvar, undef_vars -> Some (outvar :: undef_vars)
in
Cfg.NodeSet.fold (fun node acc -> acc @ (helper node dvcfg)) dvcfg.t.nodes []
Cfg.NodeSet.fold (fun node acc ->
match acc, (helper node dvcfg) with
None, None -> None
| None, Some x -> Some x
| Some acc, None -> Some acc
| Some acc, Some x -> Some (acc @ x)
) dvcfg.t.nodes None
let compute_cfg (dvcfg: DVCfg.t) : RISCCfg.t =