2012-06-27 18:09:30 +02:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Heptagon *)
|
|
|
|
(* *)
|
|
|
|
(* Gwenael Delaval, LIG/INRIA, UJF *)
|
|
|
|
(* Leonard Gerard, Parkas, ENS *)
|
|
|
|
(* Adrien Guatto, Parkas, ENS *)
|
|
|
|
(* Cedric Pasteur, Parkas, ENS *)
|
2012-06-29 01:43:15 +02:00
|
|
|
(* Marc Pouzet, Parkas, ENS *)
|
2012-06-27 18:09:30 +02:00
|
|
|
(* *)
|
|
|
|
(* Copyright 2012 ENS, INRIA, UJF *)
|
|
|
|
(* *)
|
|
|
|
(* This file is part of the Heptagon compiler. *)
|
|
|
|
(* *)
|
|
|
|
(* Heptagon is free software: you can redistribute it and/or modify it *)
|
|
|
|
(* under the terms of the GNU General Public License as published by *)
|
|
|
|
(* the Free Software Foundation, either version 3 of the License, or *)
|
|
|
|
(* (at your option) any later version. *)
|
|
|
|
(* *)
|
|
|
|
(* Heptagon is distributed in the hope that it will be useful, *)
|
|
|
|
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
|
|
|
|
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)
|
|
|
|
(* GNU General Public License for more details. *)
|
|
|
|
(* *)
|
|
|
|
(* You should have received a copy of the GNU General Public License *)
|
|
|
|
(* along with Heptagon. If not, see <http://www.gnu.org/licenses/> *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
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
|
2011-10-20 09:16:51 +02:00
|
|
|
open Mls_utils
|
2011-04-19 17:19:40 +02:00
|
|
|
|
|
|
|
(** 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-10-20 09:16:51 +02:00
|
|
|
|
2011-11-16 16:07:36 +01:00
|
|
|
We also need to add one copy if two (or more) registers are defined by each other, eg:
|
2011-10-20 09:16:51 +02:00
|
|
|
x = v fby y;
|
|
|
|
y = v fby x;
|
|
|
|
becomes
|
|
|
|
mem_x = v fby y;
|
|
|
|
x = mem_x;
|
|
|
|
y = v fby x
|
2011-04-19 17:19:40 +02:00
|
|
|
*)
|
|
|
|
|
2011-11-16 16:07:36 +01:00
|
|
|
let normalize_outputs = ref true
|
2011-10-20 09:16:51 +02:00
|
|
|
|
2011-11-16 16:07:36 +01:00
|
|
|
(** Builds the initial environment, that maps any register to the ident on the right hand side.
|
|
|
|
For outputs that are also registers, if normalize_outputs is true,
|
|
|
|
they are mapped to themselves to force the copy (made necessary by the calling convention).
|
|
|
|
Other variables are mapped to None. *)
|
|
|
|
let build_env nd =
|
|
|
|
let add_none env l = List.fold_left (fun env vd -> Env.add vd.v_ident None env) env l in
|
2011-11-29 14:08:12 +01:00
|
|
|
let rec add_eq env eq = match eq.eq_lhs, eq.eq_rhs.e_desc with
|
|
|
|
| _, Ewhen (e, _, _) -> add_eq env { eq with eq_rhs = e }
|
2011-11-16 16:07:36 +01:00
|
|
|
| Evarpat x, Efby (_, w) -> Env.add x (ident_of_extvalue w) env
|
|
|
|
| _, _ ->
|
|
|
|
List.fold_left (fun env id -> Env.add id None env) env (Vars.def [] eq)
|
|
|
|
in
|
|
|
|
let env = add_none Env.empty nd.n_input in
|
|
|
|
let env = List.fold_left add_eq env nd.n_equs in
|
|
|
|
let env =
|
|
|
|
if !normalize_outputs then
|
|
|
|
List.fold_left (fun env vd -> Env.add vd.v_ident (Some vd.v_ident) env) env nd.n_output
|
|
|
|
else
|
|
|
|
env
|
|
|
|
in
|
|
|
|
env
|
2011-10-20 09:16:51 +02:00
|
|
|
|
2011-11-29 14:08:12 +01:00
|
|
|
let rec replace_fby e exp_mem_x = match e.e_desc with
|
|
|
|
| Ewhen (e1, c, y) -> { e with e_desc = Ewhen (replace_fby e1 exp_mem_x, c, y) }
|
|
|
|
| Efby (_, _) -> exp_mem_x
|
|
|
|
| _ -> assert false
|
|
|
|
|
2011-11-16 16:07:36 +01:00
|
|
|
let rec depends_on x y env =
|
|
|
|
match Env.find y env with
|
|
|
|
| None -> false
|
|
|
|
| Some z ->
|
|
|
|
if ident_compare x z = 0 then true
|
|
|
|
else if ident_compare y z = 0 then false
|
|
|
|
else depends_on x z env
|
2011-10-20 09:16:51 +02:00
|
|
|
|
2011-11-29 14:08:12 +01:00
|
|
|
let eq funs (env, vds, v, eqs) eq = match eq.eq_lhs, eq.eq_rhs with
|
2011-11-30 08:29:50 +01:00
|
|
|
| Evarpat x, e when Vars.is_fby e && depends_on x x env ->
|
2011-11-16 16:07:36 +01:00
|
|
|
let vd = vd_find x vds in
|
2011-09-08 11:45:43 +02:00
|
|
|
let x_mem = Idents.gen_var "normalize_mem" ("mem_"^(Idents.name x)) in
|
|
|
|
let vd_mem = { vd with v_ident = x_mem } in
|
2011-11-29 14:08:12 +01:00
|
|
|
let ck = Misc.assert_1 (Clocks.unprod e.e_ct) in
|
|
|
|
let exp_mem_x = mk_extvalue_exp e.e_level_ck vd.v_type
|
|
|
|
~clock:ck ~linearity:vd.v_linearity (Wvar x_mem) in
|
2011-09-08 11:45:43 +02:00
|
|
|
(* mem_o = v fby e *)
|
|
|
|
let eq_copy = { eq with eq_lhs = Evarpat x_mem } in
|
|
|
|
(* o = mem_o *)
|
2011-11-29 14:08:12 +01:00
|
|
|
let eq = { eq with eq_rhs = replace_fby e exp_mem_x } in
|
2011-11-16 16:07:36 +01:00
|
|
|
(* remove the dependency in env *)
|
|
|
|
let env = Env.add x None env in
|
|
|
|
eq, (env, vds, vd_mem::v, eq::eq_copy::eqs)
|
2011-04-19 17:19:40 +02:00
|
|
|
| _, _ ->
|
2011-11-16 16:07:36 +01:00
|
|
|
eq, (env, vds, v, eq::eqs)
|
2011-04-19 17:19:40 +02:00
|
|
|
|
2011-07-27 16:13:45 +02:00
|
|
|
(* Leave contract unchanged (no output defined in it) *)
|
2011-11-16 16:07:36 +01:00
|
|
|
let contract _ acc c = c, acc
|
2011-07-27 16:13:45 +02:00
|
|
|
|
2011-04-19 17:19:40 +02:00
|
|
|
let node funs acc nd =
|
2011-11-16 16:07:36 +01:00
|
|
|
let env = build_env nd in
|
2011-10-20 09:16:51 +02:00
|
|
|
let nd, (_, _, v, eqs) =
|
2011-11-16 16:07:36 +01:00
|
|
|
Mls_mapfold.node_dec funs (env, nd.n_local @ nd.n_output, [], []) nd
|
2011-10-20 09:16:51 +02:00
|
|
|
in
|
2011-04-27 14:28:45 +02:00
|
|
|
(* return updated node *)
|
2011-11-16 16:07:36 +01:00
|
|
|
{ nd with n_local = v @ nd.n_local; n_equs = List.rev eqs }, acc
|
2011-04-19 17:19:40 +02:00
|
|
|
|
|
|
|
let program p =
|
2011-09-07 17:51:31 +02:00
|
|
|
let funs = { Mls_mapfold.defaults with
|
2011-09-08 11:45:43 +02:00
|
|
|
eq = eq; node_dec = node; contract = contract } in
|
2011-11-16 16:07:36 +01:00
|
|
|
let p, _ = Mls_mapfold.program_it funs (Env.empty, [], [], []) p in
|
2011-04-19 17:19:40 +02:00
|
|
|
p
|