2010-06-15 10:49:03 +02:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Heptagon *)
|
|
|
|
(* *)
|
|
|
|
(* Author : Marc Pouzet *)
|
|
|
|
(* Organization : Demons, LRI, University of Paris-Sud, Orsay *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2010-06-18 10:30:23 +02:00
|
|
|
(* Translation from Minils to Obc. *)
|
2010-06-15 10:49:03 +02:00
|
|
|
open Misc
|
2010-06-18 10:30:23 +02:00
|
|
|
open Names
|
2010-07-23 19:45:19 +02:00
|
|
|
open Idents
|
2010-06-18 10:30:23 +02:00
|
|
|
open Signature
|
2010-06-15 10:49:03 +02:00
|
|
|
open Obc
|
2010-07-13 14:03:39 +02:00
|
|
|
open Types
|
2010-06-15 10:49:03 +02:00
|
|
|
open Control
|
|
|
|
open Static
|
2010-07-21 16:00:06 +02:00
|
|
|
open Obc_mapfold
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2010-06-30 17:30:24 +02:00
|
|
|
let gen_obj_name n =
|
|
|
|
(shortname n) ^ "_mem" ^ (gen_symbol ())
|
|
|
|
|
2010-06-18 10:30:23 +02:00
|
|
|
let op_from_string op = Modname { qual = "Pervasives"; id = op; }
|
2010-06-26 16:53:25 +02:00
|
|
|
|
|
|
|
let rec lhs_of_idx_list e = function
|
2010-07-13 14:03:39 +02:00
|
|
|
| [] -> e | idx :: l -> mk_lhs (Larray (lhs_of_idx_list e l, idx))
|
2010-06-25 13:42:10 +02:00
|
|
|
|
|
|
|
let array_elt_of_exp idx e =
|
2010-07-13 14:03:39 +02:00
|
|
|
match e.e_desc with
|
|
|
|
| Econst ({ se_desc = Sarray_power (c, _) }) ->
|
|
|
|
mk_exp (Econst c)
|
2010-06-26 16:53:25 +02:00
|
|
|
| _ ->
|
2010-07-13 14:03:39 +02:00
|
|
|
mk_lhs_exp (Larray(lhs_of_exp e, mk_exp (Elhs idx)))
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2010-06-16 11:32:13 +02:00
|
|
|
(** Creates the expression that checks that the indices
|
|
|
|
in idx_list are in the bounds. If idx_list=[e1;..;ep]
|
|
|
|
and bounds = [n1;..;np], it returns
|
|
|
|
e1 <= n1 && .. && ep <= np *)
|
|
|
|
let rec bound_check_expr idx_list bounds =
|
2010-06-18 10:30:23 +02:00
|
|
|
match (idx_list, bounds) with
|
2010-07-13 14:03:39 +02:00
|
|
|
| [idx], [n] ->
|
|
|
|
mk_exp (Eop (op_from_string "<",
|
|
|
|
[ idx; mk_exp (Econst n)]))
|
2010-06-29 11:18:50 +02:00
|
|
|
| (idx :: idx_list, n :: bounds) ->
|
2010-07-13 14:03:39 +02:00
|
|
|
let e = mk_exp (Eop (op_from_string "<",
|
|
|
|
[idx; mk_exp (Econst n)])) in
|
|
|
|
mk_exp (Eop (op_from_string "&",
|
|
|
|
[e; bound_check_expr idx_list bounds]))
|
2010-06-29 11:18:50 +02:00
|
|
|
| (_, _) -> assert false
|
|
|
|
|
2010-07-13 14:03:39 +02:00
|
|
|
let reinit o =
|
|
|
|
Acall ([], o, Mreset, [])
|
2010-06-29 11:18:50 +02:00
|
|
|
|
2010-06-27 17:24:31 +02:00
|
|
|
let rec translate_pat map = function
|
|
|
|
| Minils.Evarpat x -> [ var_from_name map x ]
|
|
|
|
| Minils.Etuplepat pat_list ->
|
2010-06-29 11:18:50 +02:00
|
|
|
List.fold_right (fun pat acc -> (translate_pat map pat) @ acc)
|
2010-06-27 17:24:31 +02:00
|
|
|
pat_list []
|
2010-06-29 11:18:50 +02:00
|
|
|
|
2010-07-22 09:36:22 +02:00
|
|
|
let translate_var_dec map l =
|
|
|
|
let one_var { Minils.v_ident = x; Minils.v_type = t; v_loc = loc } =
|
|
|
|
mk_var_dec ~loc:loc x t
|
|
|
|
in
|
|
|
|
List.map one_var l
|
|
|
|
|
2010-06-15 10:49:03 +02:00
|
|
|
(* [translate e = c] *)
|
2010-07-16 09:58:56 +02:00
|
|
|
let rec translate map (si, j, s) e =
|
2010-07-13 14:03:39 +02:00
|
|
|
let desc = match e.Minils.e_desc with
|
|
|
|
| Minils.Econst v -> Econst v
|
|
|
|
| Minils.Evar n -> Elhs (var_from_name map n)
|
|
|
|
| Minils.Eapp ({ Minils.a_op = Minils.Efun n },
|
|
|
|
e_list, _) when Mls_utils.is_op n ->
|
2010-07-16 09:58:56 +02:00
|
|
|
Eop (n, List.map (translate map (si, j, s)) e_list)
|
2010-07-13 14:03:39 +02:00
|
|
|
| Minils.Ewhen (e, _, _) ->
|
2010-07-16 09:58:56 +02:00
|
|
|
let e = translate map (si, j, s) e in
|
2010-07-13 14:03:39 +02:00
|
|
|
e.e_desc
|
2010-06-29 11:18:50 +02:00
|
|
|
| Minils.Estruct f_e_list ->
|
|
|
|
let type_name =
|
|
|
|
(match e.Minils.e_ty with
|
2010-07-13 14:03:39 +02:00
|
|
|
| Tid name -> name
|
2010-06-29 11:18:50 +02:00
|
|
|
| _ -> assert false) in
|
|
|
|
let f_e_list =
|
|
|
|
List.map
|
2010-07-16 09:58:56 +02:00
|
|
|
(fun (f, e) -> (f, (translate map (si, j, s) e)))
|
2010-06-29 11:18:50 +02:00
|
|
|
f_e_list
|
2010-07-13 14:03:39 +02:00
|
|
|
in Estruct (type_name, f_e_list)
|
|
|
|
| Minils.Eapp ({ Minils.a_op = Minils.Efield;
|
|
|
|
Minils.a_params = [{ se_desc = Sconstructor f }] },
|
|
|
|
[e], _) ->
|
2010-07-16 09:58:56 +02:00
|
|
|
let e = translate map (si, j, s) e in
|
2010-07-13 14:03:39 +02:00
|
|
|
Elhs (mk_lhs (Lfield (lhs_of_exp e, f)))
|
|
|
|
(*Array operators*)
|
|
|
|
| Minils.Eapp ({ Minils.a_op = Minils.Earray }, e_list, _) ->
|
2010-07-16 09:58:56 +02:00
|
|
|
Earray (List.map (translate map (si, j, s)) e_list)
|
2010-07-13 14:03:39 +02:00
|
|
|
| Minils.Eapp ({ Minils.a_op = Minils.Eselect;
|
|
|
|
Minils.a_params = idx }, [e], _) ->
|
2010-07-16 09:58:56 +02:00
|
|
|
let e = translate map (si, j, s) e in
|
2010-07-13 14:03:39 +02:00
|
|
|
let idx_list = List.map (fun idx -> mk_exp (Econst idx)) idx in
|
|
|
|
Elhs (lhs_of_idx_list (lhs_of_exp e) idx_list)
|
2010-06-29 11:18:50 +02:00
|
|
|
| _ -> (*Minils_printer.print_exp stdout e; flush stdout;*) assert false
|
2010-07-13 14:03:39 +02:00
|
|
|
in
|
|
|
|
mk_exp ~ty:e.Minils.e_ty desc
|
2010-06-15 10:49:03 +02:00
|
|
|
|
|
|
|
(* [translate pat act = si, j, d, s] *)
|
2010-07-16 09:58:56 +02:00
|
|
|
and translate_act map context pat
|
2010-06-29 11:18:50 +02:00
|
|
|
({ Minils.e_desc = desc } as act) =
|
2010-06-15 10:49:03 +02:00
|
|
|
match pat, desc with
|
2010-07-13 14:03:39 +02:00
|
|
|
| Minils.Etuplepat p_list,
|
|
|
|
Minils.Eapp ({ Minils.a_op = Minils.Etuple }, act_list, _) ->
|
|
|
|
List.flatten (List.map2 (translate_act map context) p_list act_list)
|
2010-07-15 13:12:11 +02:00
|
|
|
| Minils.Etuplepat p_list,
|
|
|
|
Minils.Econst { se_desc = Stuple se_list } ->
|
2010-07-15 17:58:32 +02:00
|
|
|
let const_list = Mls_utils.exp_list_of_static_exp_list se_list in
|
2010-07-15 13:12:11 +02:00
|
|
|
List.flatten (List.map2 (translate_act map context) p_list const_list)
|
2010-06-29 11:18:50 +02:00
|
|
|
| pat, Minils.Ewhen (e, _, _) ->
|
2010-07-13 14:03:39 +02:00
|
|
|
translate_act map context pat e
|
2010-06-29 11:18:50 +02:00
|
|
|
| pat, Minils.Emerge (x, c_act_list) ->
|
|
|
|
let lhs = var_from_name map x in
|
2010-07-13 14:03:39 +02:00
|
|
|
[Acase (mk_exp (Elhs lhs),
|
|
|
|
translate_c_act_list map context pat c_act_list)]
|
2010-06-29 11:18:50 +02:00
|
|
|
| Minils.Evarpat n, _ ->
|
2010-07-13 14:03:39 +02:00
|
|
|
[Aassgn (var_from_name map n, translate map context act)]
|
2010-06-29 11:18:50 +02:00
|
|
|
| _ -> (*Minils_printer.print_exp stdout act;*) assert false
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-07-13 14:03:39 +02:00
|
|
|
and translate_c_act_list map context pat c_act_list =
|
2010-06-15 10:49:03 +02:00
|
|
|
List.map
|
2010-07-22 09:36:22 +02:00
|
|
|
(fun (c, act) -> (c, mk_block (translate_act map context pat act)))
|
2010-06-15 10:49:03 +02:00
|
|
|
c_act_list
|
|
|
|
|
2010-07-21 16:00:06 +02:00
|
|
|
let mk_obj_call_from_context (o, _) n =
|
|
|
|
match o with
|
|
|
|
| Oobj _ -> Oobj n
|
|
|
|
| Oarray (_, lhs) -> Oarray(n, lhs)
|
|
|
|
|
|
|
|
let size_from_call_context (_, n) = n
|
|
|
|
|
|
|
|
let empty_call_context = Oobj "n", None
|
|
|
|
|
|
|
|
let rec translate_eq map call_context { Minils.eq_lhs = pat; Minils.eq_rhs = e }
|
2010-07-22 09:36:22 +02:00
|
|
|
(v, si, j, s) =
|
2010-07-15 10:06:16 +02:00
|
|
|
let { Minils.e_desc = desc; Minils.e_ty = ty;
|
|
|
|
Minils.e_ck = ck; Minils.e_loc = loc } = e in
|
2010-06-26 16:53:25 +02:00
|
|
|
match (pat, desc) with
|
2010-06-29 11:18:50 +02:00
|
|
|
| Minils.Evarpat n, Minils.Efby (opt_c, e) ->
|
|
|
|
let x = var_from_name map n in
|
|
|
|
let si = (match opt_c with
|
|
|
|
| None -> si
|
|
|
|
| Some c ->
|
2010-07-13 14:03:39 +02:00
|
|
|
(Aassgn (x,
|
|
|
|
mk_exp (Econst c))) :: si) in
|
|
|
|
let action = Aassgn (var_from_name map n,
|
2010-07-16 09:58:56 +02:00
|
|
|
translate map (si, j, s) e)
|
2010-06-29 11:18:50 +02:00
|
|
|
in
|
2010-07-22 09:36:22 +02:00
|
|
|
v, si, j, (control map ck action) :: s
|
2010-06-29 11:18:50 +02:00
|
|
|
|
2010-07-13 14:03:39 +02:00
|
|
|
| Minils.Etuplepat p_list,
|
2010-07-13 14:42:46 +02:00
|
|
|
Minils.Eapp({ Minils.a_op = Minils.Etuple }, act_list, _) ->
|
2010-06-29 11:18:50 +02:00
|
|
|
List.fold_right2
|
|
|
|
(fun pat e ->
|
2010-07-21 16:00:06 +02:00
|
|
|
translate_eq map call_context
|
2010-06-29 11:18:50 +02:00
|
|
|
(Minils.mk_equation pat e))
|
2010-07-22 09:36:22 +02:00
|
|
|
p_list act_list (v, si, j, s)
|
2010-06-29 11:18:50 +02:00
|
|
|
|
2010-07-13 14:42:46 +02:00
|
|
|
| pat, Minils.Eapp({ Minils.a_op = Minils.Eifthenelse }, [e1;e2;e3], _) ->
|
2010-07-16 09:58:56 +02:00
|
|
|
let cond = translate map (si, j, s) e1 in
|
2010-07-22 09:36:22 +02:00
|
|
|
let vt, si, j, true_act = translate_eq map call_context
|
|
|
|
(Minils.mk_equation pat e2) (v, si, j, s) in
|
|
|
|
let vf, si, j, false_act = translate_eq map call_context
|
|
|
|
(Minils.mk_equation pat e3) (v, si, j, s) in
|
|
|
|
let vf = translate_var_dec map vf in
|
|
|
|
let vt = translate_var_dec map vt in
|
|
|
|
let action =
|
|
|
|
Acase (cond, [Name "true", mk_block ~locals:vt true_act;
|
|
|
|
Name "false", mk_block ~locals:vf false_act]) in
|
|
|
|
v, si, j, (control map ck action) :: s
|
2010-07-13 14:42:46 +02:00
|
|
|
|
2010-07-13 14:03:39 +02:00
|
|
|
| Minils.Evarpat x,
|
2010-07-13 14:42:46 +02:00
|
|
|
Minils.Eapp ({ Minils.a_op = Minils.Efield_update;
|
2010-07-13 14:03:39 +02:00
|
|
|
Minils.a_params = [{ se_desc = Sconstructor f }] },
|
|
|
|
[e1; e2], _) ->
|
2010-06-29 11:18:50 +02:00
|
|
|
let x = var_from_name map x in
|
2010-07-16 09:58:56 +02:00
|
|
|
let copy = Aassgn (x, translate map (si, j, s) e1) in
|
2010-06-29 11:18:50 +02:00
|
|
|
let action =
|
2010-07-16 09:58:56 +02:00
|
|
|
Aassgn (mk_lhs (Lfield (x, f)), translate map (si, j, s) e2)
|
2010-06-29 11:18:50 +02:00
|
|
|
in
|
2010-07-22 09:36:22 +02:00
|
|
|
v, si, j, (control map ck copy) :: (control map ck action) :: s
|
2010-06-29 11:18:50 +02:00
|
|
|
|
|
|
|
| Minils.Evarpat x,
|
2010-07-13 14:03:39 +02:00
|
|
|
Minils.Eapp ({ Minils.a_op = Minils.Eselect_slice;
|
|
|
|
Minils.a_params = [idx1; idx2] }, [e], _) ->
|
2010-07-23 19:45:19 +02:00
|
|
|
let cpt = Idents.fresh "i" in
|
2010-07-16 09:58:56 +02:00
|
|
|
let e = translate map (si, j, s) e in
|
2010-07-13 14:03:39 +02:00
|
|
|
let idx = mk_exp (Eop (op_from_string "+",
|
|
|
|
[mk_evar cpt;
|
|
|
|
mk_exp (Econst idx1) ])) in
|
|
|
|
(* bound = (idx2 - idx1) + 1*)
|
|
|
|
let bound =
|
|
|
|
mk_static_exp (Sop(op_from_string "+",
|
|
|
|
[ mk_static_exp (Sint 1);
|
|
|
|
mk_static_exp (Sop (op_from_string "-",
|
|
|
|
[idx2;idx1])) ])) in
|
2010-06-29 11:18:50 +02:00
|
|
|
let action =
|
2010-07-13 14:03:39 +02:00
|
|
|
Afor (cpt, mk_static_exp (Sint 0), bound,
|
2010-07-22 09:36:22 +02:00
|
|
|
mk_block [Aassgn (mk_lhs (Larray (var_from_name map x,
|
|
|
|
mk_evar cpt)),
|
2010-07-13 14:03:39 +02:00
|
|
|
mk_lhs_exp (Larray (lhs_of_exp e, idx)))] )
|
2010-06-29 11:18:50 +02:00
|
|
|
in
|
2010-07-22 09:36:22 +02:00
|
|
|
v, si, j, (control map ck action) :: s
|
2010-06-29 11:18:50 +02:00
|
|
|
|
|
|
|
| Minils.Evarpat x,
|
2010-07-13 14:03:39 +02:00
|
|
|
Minils.Eapp ({ Minils.a_op = Minils.Eselect_dyn }, e1::e2::idx, _) ->
|
2010-06-29 11:18:50 +02:00
|
|
|
let x = var_from_name map x in
|
2010-06-30 13:46:46 +02:00
|
|
|
let bounds = Mls_utils.bounds_list e1.Minils.e_ty in
|
2010-07-16 09:58:56 +02:00
|
|
|
let e1 = translate map (si, j, s) e1 in
|
|
|
|
let idx = List.map (translate map (si, j, s)) idx in
|
2010-06-29 11:18:50 +02:00
|
|
|
let true_act =
|
2010-07-13 14:03:39 +02:00
|
|
|
Aassgn (x, mk_exp (Elhs (lhs_of_idx_list (lhs_of_exp e1) idx))) in
|
2010-07-16 09:58:56 +02:00
|
|
|
let false_act = Aassgn (x, translate map (si, j, s) e2) in
|
2010-06-29 11:18:50 +02:00
|
|
|
let cond = bound_check_expr idx bounds in
|
2010-07-22 09:36:22 +02:00
|
|
|
let action = Acase (cond, [ Name "true", mk_block [true_act];
|
|
|
|
Name "false", mk_block [false_act] ]) in
|
|
|
|
v, si, j, (control map ck action) :: s
|
2010-06-29 11:18:50 +02:00
|
|
|
|
|
|
|
| Minils.Evarpat x,
|
2010-07-13 14:03:39 +02:00
|
|
|
Minils.Eapp ({ Minils.a_op = Minils.Eupdate;
|
|
|
|
Minils.a_params = idx }, [e1; e2], _) ->
|
2010-06-29 11:18:50 +02:00
|
|
|
let x = var_from_name map x in
|
2010-07-16 09:58:56 +02:00
|
|
|
let copy = Aassgn (x, translate map (si, j, s) e1) in
|
2010-07-13 14:03:39 +02:00
|
|
|
let idx = List.map (fun idx -> mk_exp (Econst idx)) idx in
|
|
|
|
let action = Aassgn (lhs_of_idx_list x idx,
|
2010-07-16 09:58:56 +02:00
|
|
|
translate map (si, j, s) e2)
|
2010-06-29 11:18:50 +02:00
|
|
|
in
|
2010-07-22 09:36:22 +02:00
|
|
|
v, si, j, (control map ck copy) :: (control map ck action) :: s
|
2010-06-29 11:18:50 +02:00
|
|
|
|
|
|
|
| Minils.Evarpat x,
|
2010-07-13 14:03:39 +02:00
|
|
|
Minils.Eapp ({ Minils.a_op = Minils.Earray_fill;
|
|
|
|
Minils.a_params = [n] }, [e], _) ->
|
2010-07-23 19:45:19 +02:00
|
|
|
let cpt = Idents.fresh "i" in
|
2010-06-29 11:18:50 +02:00
|
|
|
let action =
|
2010-07-13 14:03:39 +02:00
|
|
|
Afor (cpt, mk_static_exp (Sint 0), n,
|
2010-07-22 09:36:22 +02:00
|
|
|
mk_block [Aassgn (mk_lhs (Larray (var_from_name map x,
|
|
|
|
mk_evar cpt)),
|
|
|
|
translate map (si, j, s) e) ])
|
2010-06-29 11:18:50 +02:00
|
|
|
in
|
2010-07-22 09:36:22 +02:00
|
|
|
v, si, j, (control map ck action) :: s
|
2010-06-29 11:18:50 +02:00
|
|
|
|
|
|
|
| Minils.Evarpat x,
|
2010-07-13 14:03:39 +02:00
|
|
|
Minils.Eapp ({ Minils.a_op = Minils.Econcat }, [e1; e2], _) ->
|
2010-07-23 19:45:19 +02:00
|
|
|
let cpt1 = Idents.fresh "i" in
|
|
|
|
let cpt2 = Idents.fresh "i" in
|
2010-06-29 11:18:50 +02:00
|
|
|
let x = var_from_name map x in
|
|
|
|
(match e1.Minils.e_ty, e2.Minils.e_ty with
|
2010-07-13 14:03:39 +02:00
|
|
|
| Tarray (_, n1), Tarray (_, n2) ->
|
2010-07-16 09:58:56 +02:00
|
|
|
let e1 = translate map (si, j, s) e1 in
|
|
|
|
let e2 = translate map (si, j, s) e2 in
|
2010-06-29 11:18:50 +02:00
|
|
|
let a1 =
|
2010-07-13 14:03:39 +02:00
|
|
|
Afor (cpt1, mk_static_exp (Sint 0), n1,
|
2010-07-22 09:36:22 +02:00
|
|
|
mk_block [Aassgn (mk_lhs (Larray (x, mk_evar cpt1)),
|
|
|
|
mk_lhs_exp (Larray (lhs_of_exp e1,
|
|
|
|
mk_evar cpt1)))] ) in
|
2010-07-13 14:03:39 +02:00
|
|
|
let idx = mk_exp (Eop (op_from_string "+",
|
|
|
|
[ mk_exp (Econst n1); mk_evar cpt2])) in
|
2010-06-29 11:18:50 +02:00
|
|
|
let a2 =
|
2010-07-13 14:03:39 +02:00
|
|
|
Afor (cpt2, static_exp_of_int 0, n2,
|
2010-07-22 09:36:22 +02:00
|
|
|
mk_block [Aassgn (mk_lhs (Larray (x, idx)),
|
|
|
|
mk_lhs_exp (Larray (lhs_of_exp e2,
|
|
|
|
mk_evar cpt2)))] )
|
2010-06-29 11:18:50 +02:00
|
|
|
in
|
2010-07-22 09:36:22 +02:00
|
|
|
v, si, j, (control map ck a1) :: (control map ck a2) :: s
|
2010-06-29 11:18:50 +02:00
|
|
|
| _ -> assert false )
|
|
|
|
|
2010-07-21 16:00:06 +02:00
|
|
|
| pat, Minils.Eapp ({ Minils.a_op = Minils.Efun _ | Minils.Enode _ } as app,
|
|
|
|
e_list, r) ->
|
|
|
|
let name_list = translate_pat map pat in
|
|
|
|
let c_list = List.map (translate map (si, j, s)) e_list in
|
2010-07-22 09:36:22 +02:00
|
|
|
let v', si', j', action = mk_node_call map call_context
|
2010-07-21 16:00:06 +02:00
|
|
|
app loc name_list c_list in
|
|
|
|
let action = List.map (control map ck) action in
|
|
|
|
let s = (match r, app.Minils.a_op with
|
|
|
|
| Some r, Minils.Enode _ ->
|
|
|
|
let ra = List.map (control map ck) si' in
|
|
|
|
ra @ action @ s
|
|
|
|
| _, _ -> action @ s) in
|
2010-07-22 09:36:22 +02:00
|
|
|
v' @ v, si'@si, j'@j, s
|
2010-07-21 16:00:06 +02:00
|
|
|
|
|
|
|
| pat, Minils.Eiterator (it, app, n, e_list, reset) ->
|
2010-06-29 11:18:50 +02:00
|
|
|
let name_list = translate_pat map pat in
|
|
|
|
let c_list =
|
2010-07-16 09:58:56 +02:00
|
|
|
List.map (translate map (si, j, s)) e_list in
|
2010-07-23 19:45:19 +02:00
|
|
|
let x = Idents.fresh "i" in
|
2010-07-21 16:00:06 +02:00
|
|
|
let call_context = Oarray ("n", mk_lhs (Lvar x)), Some n in
|
|
|
|
let si', j', action = translate_iterator map call_context it
|
|
|
|
name_list app loc n x c_list in
|
2010-07-13 14:03:39 +02:00
|
|
|
let action = List.map (control map ck) action in
|
2010-06-29 11:18:50 +02:00
|
|
|
let s =
|
2010-07-13 14:03:39 +02:00
|
|
|
(match reset, app.Minils.a_op with
|
|
|
|
| Some r, Minils.Enode _ ->
|
2010-07-21 16:00:06 +02:00
|
|
|
let ra = List.map (control map ck) si' in
|
|
|
|
ra @ action @ s
|
2010-07-13 14:03:39 +02:00
|
|
|
| _, _ -> action @ s)
|
2010-07-22 09:36:22 +02:00
|
|
|
in (v, si' @ si, j' @ j, s)
|
2010-06-29 11:18:50 +02:00
|
|
|
|
|
|
|
| (pat, _) ->
|
2010-07-16 09:58:56 +02:00
|
|
|
let action = translate_act map (si, j, s) pat e in
|
2010-07-13 14:03:39 +02:00
|
|
|
let action = List.map (control map ck) action in
|
2010-07-22 09:36:22 +02:00
|
|
|
v, si, j, action @ s
|
2010-07-13 14:03:39 +02:00
|
|
|
|
2010-07-21 16:00:06 +02:00
|
|
|
and translate_eq_list map call_context act_list =
|
2010-07-22 09:36:22 +02:00
|
|
|
List.fold_right (translate_eq map call_context) act_list ([], [], [], [])
|
2010-07-21 16:00:06 +02:00
|
|
|
|
|
|
|
and mk_node_call map call_context app loc name_list args =
|
|
|
|
match app.Minils.a_op with
|
|
|
|
| Minils.Enode f | Minils.Efun f ->
|
|
|
|
let o = mk_obj_call_from_context call_context (gen_obj_name f) in
|
2010-07-22 10:15:11 +02:00
|
|
|
let obj =
|
|
|
|
{ o_name = obj_call_name o; o_class = f;
|
|
|
|
o_params = app.Minils.a_params;
|
|
|
|
o_size = size_from_call_context call_context; o_loc = loc } in
|
2010-07-21 16:00:06 +02:00
|
|
|
let si =
|
|
|
|
(match app.Minils.a_op with
|
|
|
|
| Minils.Efun _ -> []
|
|
|
|
| Minils.Enode _ -> [reinit o]) in
|
2010-07-22 10:15:11 +02:00
|
|
|
[], si, [obj], [Acall (name_list, o, Mstep, args)]
|
2010-07-21 16:00:06 +02:00
|
|
|
|
|
|
|
| Minils.Elambda(inp, outp, locals, eq_list) ->
|
|
|
|
let add_input env vd =
|
|
|
|
Env.add vd.Minils.v_ident (mk_lhs (Lvar vd.Minils.v_ident)) env in
|
|
|
|
let build env vd a =
|
|
|
|
Env.add vd.Minils.v_ident a env in
|
2010-07-22 09:36:22 +02:00
|
|
|
let subst_act_list env act_list =
|
2010-07-21 16:00:06 +02:00
|
|
|
let exp funs env e = match e.e_desc with
|
|
|
|
| Elhs { l_desc = Lvar x } ->
|
|
|
|
let e =
|
|
|
|
(try Env.find x env
|
|
|
|
with Not_found -> e) in
|
|
|
|
e, env
|
|
|
|
| _ -> Obc_mapfold.exp funs env e
|
|
|
|
in
|
|
|
|
let funs = { Obc_mapfold.defaults with exp = exp } in
|
2010-07-22 09:36:22 +02:00
|
|
|
let act_list, _ = mapfold (Obc_mapfold.act_it funs) env act_list in
|
|
|
|
act_list
|
2010-07-21 16:00:06 +02:00
|
|
|
in
|
|
|
|
|
|
|
|
let map = List.fold_left add_input map inp in
|
|
|
|
let map = List.fold_left2 build map outp name_list in
|
|
|
|
let map = List.fold_left add_input map locals in
|
2010-07-22 09:36:22 +02:00
|
|
|
let v, si, j, s = translate_eq_list map call_context eq_list in
|
2010-07-21 16:00:06 +02:00
|
|
|
let env = List.fold_left2 build Env.empty inp args in
|
2010-07-22 09:36:22 +02:00
|
|
|
v @ locals, si, j, subst_act_list env s
|
2010-07-21 16:00:06 +02:00
|
|
|
|
|
|
|
| _ -> assert false
|
|
|
|
|
|
|
|
and translate_iterator map call_context it name_list app loc n x c_list =
|
2010-07-13 14:03:39 +02:00
|
|
|
let array_of_output name_list =
|
|
|
|
List.map (fun l -> mk_lhs (Larray (l, mk_evar x))) name_list in
|
|
|
|
let array_of_input c_list =
|
|
|
|
List.map (array_elt_of_exp (mk_lhs (Lvar x))) c_list in
|
2010-06-18 10:30:23 +02:00
|
|
|
|
2010-06-16 11:32:13 +02:00
|
|
|
match it with
|
2010-06-26 16:53:25 +02:00
|
|
|
| Minils.Imap ->
|
2010-07-13 14:03:39 +02:00
|
|
|
let c_list = array_of_input c_list in
|
|
|
|
let name_list = array_of_output name_list in
|
2010-07-22 09:36:22 +02:00
|
|
|
let v, si, j, action = mk_node_call map call_context
|
2010-07-21 16:00:06 +02:00
|
|
|
app loc name_list c_list in
|
2010-07-22 09:36:22 +02:00
|
|
|
let v = translate_var_dec map v in
|
|
|
|
let b = mk_block ~locals:v action in
|
|
|
|
si, j, [ Afor (x, static_exp_of_int 0, n, b) ]
|
2010-06-18 10:30:23 +02:00
|
|
|
|
2010-06-26 16:53:25 +02:00
|
|
|
| Minils.Imapfold ->
|
|
|
|
let (c_list, acc_in) = split_last c_list in
|
2010-07-13 14:03:39 +02:00
|
|
|
let c_list = array_of_input c_list in
|
2010-06-26 16:53:25 +02:00
|
|
|
let (name_list, acc_out) = split_last name_list in
|
2010-07-13 14:03:39 +02:00
|
|
|
let name_list = array_of_output name_list in
|
2010-07-22 09:36:22 +02:00
|
|
|
let v, si, j, action = mk_node_call map call_context
|
2010-07-21 16:00:06 +02:00
|
|
|
app loc (name_list @ [ acc_out ])
|
|
|
|
(c_list @ [ mk_exp (Elhs acc_out) ]) in
|
2010-07-22 09:36:22 +02:00
|
|
|
let v = translate_var_dec map v in
|
|
|
|
let b = mk_block ~locals:v action in
|
2010-07-21 16:00:06 +02:00
|
|
|
si, j, [Aassgn (acc_out, acc_in);
|
2010-07-22 09:36:22 +02:00
|
|
|
Afor (x, static_exp_of_int 0, n, b)]
|
2010-06-18 10:30:23 +02:00
|
|
|
|
2010-06-26 16:53:25 +02:00
|
|
|
| Minils.Ifold ->
|
|
|
|
let (c_list, acc_in) = split_last c_list in
|
2010-07-13 14:03:39 +02:00
|
|
|
let c_list = array_of_input c_list in
|
2010-06-26 16:53:25 +02:00
|
|
|
let acc_out = last_element name_list in
|
2010-07-22 09:36:22 +02:00
|
|
|
let v, si, j, action = mk_node_call map call_context
|
2010-07-21 16:00:06 +02:00
|
|
|
app loc name_list (c_list @ [ mk_exp (Elhs acc_out) ]) in
|
2010-07-22 09:36:22 +02:00
|
|
|
let v = translate_var_dec map v in
|
|
|
|
let b = mk_block ~locals:v action in
|
2010-07-21 16:00:06 +02:00
|
|
|
si, j, [ Aassgn (acc_out, acc_in);
|
2010-07-22 09:36:22 +02:00
|
|
|
Afor (x, static_exp_of_int 0, n, b) ]
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2010-07-26 09:33:22 +02:00
|
|
|
| Minils.Ifoldi ->
|
|
|
|
let (c_list, acc_in) = split_last c_list in
|
|
|
|
let c_list = array_of_input c_list in
|
|
|
|
let acc_out = last_element name_list in
|
|
|
|
let v, si, j, action = mk_node_call map call_context
|
|
|
|
app loc name_list (c_list @ [ mk_evar x; mk_exp (Elhs acc_out) ]) in
|
|
|
|
let v = translate_var_dec map v in
|
|
|
|
let b = mk_block ~locals:v action in
|
|
|
|
si, j, [ Aassgn (acc_out, acc_in);
|
|
|
|
Afor (x, static_exp_of_int 0, n, b) ]
|
|
|
|
|
2010-06-15 10:49:03 +02:00
|
|
|
let remove m d_list =
|
2010-06-27 17:24:31 +02:00
|
|
|
List.filter (fun { Minils.v_ident = n } -> not (List.mem_assoc n m)) d_list
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2010-07-16 09:58:56 +02:00
|
|
|
let translate_contract map mem_vars =
|
2010-06-18 10:30:23 +02:00
|
|
|
function
|
2010-07-16 09:58:56 +02:00
|
|
|
| None -> ([], [], [], [])
|
2010-06-26 16:53:25 +02:00
|
|
|
| Some
|
|
|
|
{
|
|
|
|
Minils.c_eq = eq_list;
|
|
|
|
Minils.c_local = d_list;
|
|
|
|
Minils.c_assume = e_a;
|
|
|
|
Minils.c_enforce = e_c
|
|
|
|
} ->
|
2010-07-22 09:36:22 +02:00
|
|
|
let (v, si, j, s_list) = translate_eq_list map
|
2010-07-21 16:00:06 +02:00
|
|
|
empty_call_context eq_list in
|
2010-07-22 09:36:22 +02:00
|
|
|
let d_list = translate_var_dec map (v @ d_list) in
|
2010-07-16 09:58:56 +02:00
|
|
|
let d_list = List.filter
|
|
|
|
(fun vd -> not (List.mem vd.v_ident mem_vars)) d_list in
|
|
|
|
(si, j, s_list, d_list)
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2010-06-15 10:49:03 +02:00
|
|
|
(** Returns a map, mapping variables names to the variables
|
2010-06-18 10:30:23 +02:00
|
|
|
where they will be stored. *)
|
|
|
|
let subst_map inputs outputs locals mems =
|
|
|
|
(* Create a map that simply maps each var to itself *)
|
|
|
|
let m =
|
2010-07-13 14:03:39 +02:00
|
|
|
List.fold_left
|
|
|
|
(fun m { Minils.v_ident = x } -> Env.add x (mk_lhs (Lvar x)) m)
|
2010-06-18 10:30:23 +02:00
|
|
|
Env.empty (inputs @ outputs @ locals)
|
2010-06-26 16:53:25 +02:00
|
|
|
in
|
2010-07-13 14:03:39 +02:00
|
|
|
List.fold_left (fun m x -> Env.add x (mk_lhs (Lmem x)) m) m mems
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2010-07-13 14:03:39 +02:00
|
|
|
let translate_node
|
2010-07-16 09:58:56 +02:00
|
|
|
({
|
2010-06-29 11:18:50 +02:00
|
|
|
Minils.n_name = f;
|
|
|
|
Minils.n_input = i_list;
|
|
|
|
Minils.n_output = o_list;
|
|
|
|
Minils.n_local = d_list;
|
|
|
|
Minils.n_equs = eq_list;
|
|
|
|
Minils.n_contract = contract;
|
2010-07-13 14:03:39 +02:00
|
|
|
Minils.n_params = params;
|
|
|
|
Minils.n_loc = loc;
|
2010-07-16 09:58:56 +02:00
|
|
|
} as n) =
|
|
|
|
let mem_vars = Mls_utils.node_memory_vars n in
|
2010-06-18 10:30:23 +02:00
|
|
|
let subst_map = subst_map i_list o_list d_list mem_vars in
|
2010-07-22 09:36:22 +02:00
|
|
|
let (v, si, j, s_list) = translate_eq_list subst_map
|
2010-07-21 16:00:06 +02:00
|
|
|
empty_call_context eq_list in
|
2010-07-16 09:58:56 +02:00
|
|
|
let (si', j', s_list', d_list') =
|
|
|
|
translate_contract subst_map mem_vars contract in
|
2010-07-13 14:03:39 +02:00
|
|
|
let i_list = translate_var_dec subst_map i_list in
|
|
|
|
let o_list = translate_var_dec subst_map o_list in
|
2010-07-22 09:36:22 +02:00
|
|
|
let d_list = translate_var_dec subst_map (v @ d_list) in
|
2010-07-16 09:58:56 +02:00
|
|
|
let m, d_list = List.partition
|
|
|
|
(fun vd -> List.mem vd.v_ident mem_vars) d_list in
|
2010-06-18 10:30:23 +02:00
|
|
|
let s = joinlist (s_list @ s_list') in
|
2010-07-22 10:15:11 +02:00
|
|
|
let j = j' @ j in
|
2010-06-18 10:30:23 +02:00
|
|
|
let si = joinlist (si @ si') in
|
2010-07-13 14:03:39 +02:00
|
|
|
let stepm = {
|
|
|
|
m_name = Mstep; m_inputs = i_list; m_outputs = o_list;
|
2010-07-22 09:36:22 +02:00
|
|
|
m_body = mk_block ~locals:(d_list' @ d_list) s } in
|
2010-07-13 14:03:39 +02:00
|
|
|
let resetm = {
|
|
|
|
m_name = Mreset; m_inputs = []; m_outputs = [];
|
2010-07-22 09:36:22 +02:00
|
|
|
m_body = mk_block si } in
|
2010-07-15 11:31:32 +02:00
|
|
|
{ cd_name = f; cd_mems = m; cd_params = params;
|
2010-07-13 14:03:39 +02:00
|
|
|
cd_objs = j; cd_methods = [stepm; resetm];
|
|
|
|
cd_loc = loc }
|
|
|
|
|
|
|
|
let translate_ty_def { Minils.t_name = name; Minils.t_desc = tdesc;
|
|
|
|
Minils.t_loc = loc } =
|
2010-06-18 10:30:23 +02:00
|
|
|
let tdesc =
|
|
|
|
match tdesc with
|
2010-06-26 16:53:25 +02:00
|
|
|
| Minils.Type_abs -> Type_abs
|
|
|
|
| Minils.Type_enum tag_name_list -> Type_enum tag_name_list
|
|
|
|
| Minils.Type_struct field_ty_list ->
|
2010-07-13 14:03:39 +02:00
|
|
|
Type_struct field_ty_list
|
|
|
|
in { t_name = name; t_desc = tdesc; t_loc = loc }
|
|
|
|
|
|
|
|
let translate_const_def { Minils.c_name = name; Minils.c_value = se;
|
|
|
|
Minils.c_type = ty; Minils.c_loc = loc } =
|
|
|
|
{ c_name = name;
|
|
|
|
c_value = se;
|
|
|
|
c_type = ty;
|
|
|
|
c_loc = loc }
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2010-06-18 10:30:23 +02:00
|
|
|
let program {
|
2010-07-13 14:03:39 +02:00
|
|
|
Minils.p_modname = p_modname;
|
2010-06-26 16:53:25 +02:00
|
|
|
Minils.p_opened = p_module_list;
|
|
|
|
Minils.p_types = p_type_list;
|
|
|
|
Minils.p_nodes = p_node_list;
|
|
|
|
Minils.p_consts = p_const_list
|
|
|
|
} =
|
2010-07-13 14:03:39 +02:00
|
|
|
{
|
|
|
|
p_modname = p_modname;
|
|
|
|
p_opened = p_module_list;
|
|
|
|
p_types = List.map translate_ty_def p_type_list;
|
|
|
|
p_consts = List.map translate_const_def p_const_list;
|
|
|
|
p_defs = List.map translate_node p_node_list;
|
|
|
|
}
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2010-06-18 10:30:23 +02:00
|
|
|
|