2011-05-02 11:20:37 +02:00
|
|
|
open Idents
|
|
|
|
open Signature
|
2011-04-19 17:19:40 +02:00
|
|
|
open Minils
|
|
|
|
open Mls_mapfold
|
|
|
|
|
|
|
|
(** Adds an extra equation for outputs that are also memories.
|
|
|
|
For instance, if o is an output, then:
|
|
|
|
o = v fby e
|
|
|
|
becomes
|
|
|
|
mem_o = v fby e;
|
|
|
|
o = mem_o
|
|
|
|
*)
|
|
|
|
|
2011-05-02 11:20:37 +02:00
|
|
|
let fix_vd_ad env vd ad =
|
|
|
|
if Env.mem vd.v_ident env then
|
|
|
|
let x_copy = Env.find vd.v_ident env in
|
|
|
|
{ vd with v_ident = x_copy }, { ad with a_name = Some (Idents.name x_copy) }
|
|
|
|
else
|
|
|
|
vd, ad
|
2011-04-20 19:23:35 +02:00
|
|
|
|
2011-05-02 11:20:37 +02:00
|
|
|
let eq _ (outputs, eqs, env) eq = match eq.eq_lhs, eq.eq_rhs.e_desc with
|
2011-04-19 17:19:40 +02:00
|
|
|
| Evarpat x, Efby _ ->
|
2011-05-02 11:20:37 +02:00
|
|
|
if Mls_utils.vd_mem x outputs then
|
2011-04-19 17:19:40 +02:00
|
|
|
let ty = eq.eq_rhs.e_ty in
|
2011-05-09 19:32:12 +02:00
|
|
|
let ck = eq.eq_rhs.e_base_ck in
|
2011-04-20 19:23:35 +02:00
|
|
|
let x_copy = Idents.gen_var "normalize_mem" ("out_"^(Idents.name x)) in
|
2011-05-09 19:32:12 +02:00
|
|
|
let exp_x = mk_exp ck ty (Eextvalue (mk_extvalue ~ty:ty (Wvar x))) in
|
2011-04-20 19:23:35 +02:00
|
|
|
let eq_copy = { eq with eq_lhs = Evarpat x_copy; eq_rhs = exp_x } in
|
2011-05-02 11:20:37 +02:00
|
|
|
let env = Env.add x x_copy env in
|
|
|
|
eq, (outputs, eq::eq_copy::eqs, env)
|
|
|
|
else (* this memory is not an output *)
|
|
|
|
eq, (outputs, eq::eqs, env)
|
2011-04-19 17:19:40 +02:00
|
|
|
| _, _ ->
|
2011-05-02 11:20:37 +02:00
|
|
|
eq, (outputs, eq::eqs, env)
|
2011-04-19 17:19:40 +02:00
|
|
|
|
|
|
|
let node funs acc nd =
|
2011-05-02 11:20:37 +02:00
|
|
|
let nd, (_, eqs, env) = Mls_mapfold.node_dec funs (nd.n_output, [], Env.empty) nd in
|
|
|
|
let v = Env.fold
|
|
|
|
(fun x _ acc -> (Mls_utils.vd_find x nd.n_output)::acc) env nd.n_local in
|
2011-04-27 14:28:45 +02:00
|
|
|
(* update the signature of the node *)
|
|
|
|
let f = Modules.find_value nd.n_name in
|
2011-05-02 11:20:37 +02:00
|
|
|
let o, sig_o = List.split (List.map2 (fix_vd_ad env) nd.n_output f.node_outputs) in
|
|
|
|
let f = { f with node_outputs = sig_o } in
|
2011-04-27 14:28:45 +02:00
|
|
|
Modules.replace_value nd.n_name f;
|
|
|
|
(* return updated node *)
|
|
|
|
{ nd with n_local = v; n_equs = List.rev eqs; n_output = o }, acc
|
2011-04-19 17:19:40 +02:00
|
|
|
|
|
|
|
let program p =
|
|
|
|
let funs = { Mls_mapfold.defaults with eq = eq; node_dec = node } in
|
2011-05-02 11:20:37 +02:00
|
|
|
let p, _ = Mls_mapfold.program_it funs ([], [], Env.empty) p in
|
2011-04-19 17:19:40 +02:00
|
|
|
p
|