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
|
2011-02-14 15:21:57 +01:00
|
|
|
open Obc_utils
|
|
|
|
open Obc_mapfold
|
2010-07-13 14:03:39 +02:00
|
|
|
open Types
|
2011-05-09 19:32:12 +02:00
|
|
|
open Clocks
|
2010-06-15 10:49:03 +02:00
|
|
|
open Static
|
2010-08-03 22:38:42 +02:00
|
|
|
open Initial
|
|
|
|
|
2011-04-18 15:38:42 +02:00
|
|
|
|
|
|
|
let build_anon, find_anon =
|
|
|
|
let anon_nodes = ref QualEnv.empty in
|
|
|
|
let build_anon nodes =
|
2011-04-18 19:20:03 +02:00
|
|
|
let build env nd = match nd with
|
2011-04-19 18:45:56 +02:00
|
|
|
| Minils.Pnode nd ->
|
2011-04-18 19:20:03 +02:00
|
|
|
if Itfusion.is_anon_node nd.Minils.n_name
|
|
|
|
then QualEnv.add nd.Minils.n_name nd env
|
|
|
|
else env
|
2011-04-19 18:45:56 +02:00
|
|
|
| _ -> env
|
2011-04-18 15:38:42 +02:00
|
|
|
in
|
2011-04-18 19:20:03 +02:00
|
|
|
anon_nodes := List.fold_left build QualEnv.empty nodes
|
2011-04-18 15:38:42 +02:00
|
|
|
in
|
2011-04-18 19:20:03 +02:00
|
|
|
let find_anon qn = QualEnv.find qn !anon_nodes in
|
|
|
|
build_anon, find_anon
|
2011-04-18 15:38:42 +02:00
|
|
|
|
2011-04-14 11:53:39 +02:00
|
|
|
let var_from_name map x =
|
|
|
|
begin try
|
|
|
|
Env.find x map
|
|
|
|
with
|
2011-07-06 09:50:51 +02:00
|
|
|
_ ->
|
|
|
|
Format.eprintf
|
|
|
|
"Internal compiler error: unknown identifier %a@."
|
|
|
|
Global_printer.print_ident x;
|
|
|
|
assert false
|
2011-04-14 11:53:39 +02:00
|
|
|
end
|
2010-12-14 18:29:55 +01:00
|
|
|
|
2011-05-30 10:06:16 +02:00
|
|
|
let ext_value_exp_from_name map x =
|
|
|
|
let w = ext_value_of_pattern (var_from_name map x) in
|
|
|
|
mk_exp w.w_ty (Eextvalue w)
|
|
|
|
|
|
|
|
(* let lvar_from_name map ty x = mk_pattern ty (Lvar (var_from_name map x)) *)
|
|
|
|
|
2011-01-24 16:07:26 +01:00
|
|
|
let fresh_it () =
|
|
|
|
let id = Idents.gen_var "mls2obc" "i" in
|
|
|
|
id, mk_var_dec id Initial.tint
|
2010-06-30 17:30:24 +02:00
|
|
|
|
2011-01-20 23:05:18 +01:00
|
|
|
let gen_obj_ident n = Idents.gen_var "mls2obc" ((shortname n) ^ "_inst")
|
2011-04-14 15:14:41 +02:00
|
|
|
let fresh_for = fresh_for "mls2obc"
|
2011-04-14 18:06:54 +02:00
|
|
|
(*let copy_array = copy_array "mls2obc"*)
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2011-02-07 14:24:17 +01:00
|
|
|
let op_from_string op = { qual = Pervasives; name = op; }
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2011-01-24 16:07:26 +01:00
|
|
|
let rec pattern_of_idx_list p l =
|
2011-11-21 01:37:15 +01:00
|
|
|
let rec aux p l = match Modules.unalias_type p.pat_ty, l with
|
2011-01-24 16:07:26 +01:00
|
|
|
| _, [] -> p
|
2011-03-23 16:49:32 +01:00
|
|
|
| Tarray (ty',_), idx :: l -> aux (mk_pattern ty' (Larray (p, idx))) l
|
2011-05-23 09:24:57 +02:00
|
|
|
| _ -> internal_error "mls2obc"
|
2011-01-24 16:07:26 +01:00
|
|
|
in
|
2011-03-23 16:49:32 +01:00
|
|
|
aux p l
|
2011-03-22 22:12:59 +01:00
|
|
|
|
2011-07-21 09:13:49 +02:00
|
|
|
let rec exp_of_idx_list e l = match Modules.unalias_type e.w_ty, l with
|
2011-06-27 16:08:56 +02:00
|
|
|
| _, [] -> e
|
|
|
|
| Tarray (ty',_), idx :: l ->
|
|
|
|
exp_of_idx_list (mk_ext_value ty' (Warray (e, idx))) l
|
2011-07-21 09:13:49 +02:00
|
|
|
| _ -> internal_error "mls2obc exp_of_idx_list"
|
2011-06-27 16:08:56 +02:00
|
|
|
|
2011-07-21 09:13:49 +02:00
|
|
|
let rec extvalue_of_idx_list w l = match Modules.unalias_type w.w_ty, l with
|
2011-05-30 10:06:16 +02:00
|
|
|
| _, [] -> w
|
|
|
|
| Tarray (ty',_), idx :: l ->
|
|
|
|
extvalue_of_idx_list (mk_ext_value ty' (Warray (w, idx))) l
|
2011-07-21 09:13:49 +02:00
|
|
|
| _ -> internal_error "mls2obc extvalue_of_idx_list"
|
2011-05-30 10:06:16 +02:00
|
|
|
|
|
|
|
let rec ext_value_of_trunc_idx_list p l =
|
2011-03-22 22:12:59 +01:00
|
|
|
let mk_between idx se =
|
2011-05-30 10:06:16 +02:00
|
|
|
mk_exp_int (Eop (mk_pervasives "between", [idx; mk_ext_value_exp se.se_ty (Wconst se)]))
|
2011-03-22 22:12:59 +01:00
|
|
|
in
|
2011-05-30 10:06:16 +02:00
|
|
|
let rec aux p l = match p.w_ty, l with
|
2011-03-22 22:12:59 +01:00
|
|
|
| _, [] -> p
|
2011-05-30 10:06:16 +02:00
|
|
|
| Tarray (ty', se), idx :: l -> aux (mk_ext_value ty' (Warray (p, mk_between idx se))) l
|
2011-07-21 09:13:49 +02:00
|
|
|
| _ -> internal_error "mls2obc ext_value_of_trunc_idx_list"
|
2011-03-22 22:12:59 +01:00
|
|
|
in
|
2011-03-23 16:49:32 +01:00
|
|
|
aux p l
|
2010-06-25 13:42:10 +02:00
|
|
|
|
2011-07-21 11:19:07 +02:00
|
|
|
let rec ty_of_idx_list ty idx_list = match ty, idx_list with
|
|
|
|
| _, [] -> ty
|
|
|
|
| Tarray(ty, _), idx::idx_list -> ty_of_idx_list ty idx_list
|
|
|
|
| _, _ -> internal_error "mls2obc ty_of_idx_list"
|
|
|
|
|
|
|
|
let mk_static_array_power ty c params = match params with
|
|
|
|
| [] -> mk_ext_value_exp ty (Wconst c)
|
|
|
|
| _ ->
|
|
|
|
let se = mk_static_exp ty (Sarray_power (c, params)) in
|
|
|
|
mk_ext_value_exp ty (Wconst se)
|
|
|
|
|
2010-06-25 13:42:10 +02:00
|
|
|
let array_elt_of_exp idx e =
|
2011-01-24 16:07:26 +01:00
|
|
|
match e.e_desc, Modules.unalias_type e.e_ty with
|
2011-07-21 11:19:07 +02:00
|
|
|
| Eextvalue { w_desc = Wconst { se_desc = Sarray_power (c, _::new_params) }; }, Tarray (ty,_) ->
|
|
|
|
mk_static_array_power ty c new_params
|
2011-06-27 16:08:56 +02:00
|
|
|
| _, Tarray (ty,_) ->
|
|
|
|
mk_ext_value_exp ty (Warray(ext_value_of_exp e, idx))
|
2011-07-21 09:13:49 +02:00
|
|
|
| _ -> internal_error "mls2obc array_elt_of_exp"
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2011-06-27 19:20:47 +02:00
|
|
|
let rec array_elt_of_exp_list idx_list e =
|
2011-06-27 10:58:14 +02:00
|
|
|
match e.e_desc, Modules.unalias_type e.e_ty with
|
2011-07-21 11:19:07 +02:00
|
|
|
| Eextvalue { w_desc = Wconst { se_desc = Sarray_power (c, params) } }, Tarray (ty,n) ->
|
|
|
|
let new_params, _ = Misc.split_at (List.length params - List.length idx_list) params in
|
|
|
|
let ty = ty_of_idx_list (Tarray(ty,n)) idx_list in
|
|
|
|
mk_static_array_power ty c new_params
|
2011-06-27 16:08:56 +02:00
|
|
|
| _ , t ->
|
2011-07-21 09:13:49 +02:00
|
|
|
let rec ty id_l t = match id_l, Modules.unalias_type t with
|
2011-06-27 16:08:56 +02:00
|
|
|
| [] , t -> t
|
|
|
|
| _::id_l , Tarray (t,_) -> ty id_l t
|
2011-07-21 09:13:49 +02:00
|
|
|
| _, _ -> internal_error "mls2obc ty"
|
2011-06-27 16:08:56 +02:00
|
|
|
in
|
|
|
|
mk_exp (ty idx_list t) (Eextvalue (extvalue_of_idx_list (ext_value_of_exp e) idx_list))
|
|
|
|
|
2011-06-27 10:58:14 +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
|
2011-04-14 11:17:12 +02:00
|
|
|
0<= e1 < n1 && .. && 0 <= ep < np *)
|
2010-06-16 11:32:13 +02:00
|
|
|
let rec bound_check_expr idx_list bounds =
|
2011-04-14 11:17:12 +02:00
|
|
|
let mk_comp idx n =
|
|
|
|
let e1 = mk_exp_bool (Eop (op_from_string "<",
|
2011-05-30 10:06:16 +02:00
|
|
|
[idx; mk_ext_value_exp_int (Wconst n)])) in
|
2011-04-14 11:17:12 +02:00
|
|
|
let e2 = mk_exp_bool (Eop (op_from_string "<=",
|
2011-05-30 10:06:16 +02:00
|
|
|
[mk_ext_value_exp_int (Wconst (mk_static_int 0)); idx])) in
|
2011-04-14 11:17:12 +02:00
|
|
|
mk_exp_bool (Eop (op_from_string "&", [e1;e2]))
|
|
|
|
in
|
2010-06-18 10:30:23 +02:00
|
|
|
match (idx_list, bounds) with
|
2011-05-02 11:24:12 +02:00
|
|
|
| [idx], n::_ -> mk_comp idx n
|
2010-06-29 11:18:50 +02:00
|
|
|
| (idx :: idx_list, n :: bounds) ->
|
2011-04-14 11:17:12 +02:00
|
|
|
let e = mk_comp idx n in
|
|
|
|
mk_exp_bool (Eop (op_from_string "&",
|
|
|
|
[e; bound_check_expr idx_list bounds]))
|
2011-05-23 09:24:57 +02:00
|
|
|
| (_, _) -> internal_error "mls2obc"
|
2010-06-29 11:18:50 +02:00
|
|
|
|
2011-04-18 15:38:42 +02:00
|
|
|
let mk_plus_one e = match e.e_desc with
|
2011-06-28 14:45:15 +02:00
|
|
|
| Eextvalue ({ w_desc = Wconst idx } as w) ->
|
2011-04-18 15:38:42 +02:00
|
|
|
let idx_plus_one = mk_static_int_op (mk_pervasives "+") [idx; mk_static_int 1] in
|
2011-05-30 10:06:16 +02:00
|
|
|
{ e with e_desc = Eextvalue { w with w_desc = Wconst idx_plus_one; }; }
|
2011-04-18 15:38:42 +02:00
|
|
|
| _ ->
|
|
|
|
let idx_plus_one = Eop (mk_pervasives "+", [e; mk_exp_const_int 1]) in
|
|
|
|
{ e with e_desc = idx_plus_one }
|
|
|
|
|
2011-04-14 15:14:41 +02:00
|
|
|
(** Creates the action list that copies [src] to [dest],
|
|
|
|
updating the value at index [idx_list] with the value [v]. *)
|
2011-11-21 01:37:15 +01:00
|
|
|
let rec ssa_update_array dest src idx_list v = match Modules.unalias_type dest.pat_ty, idx_list with
|
2011-04-14 15:14:41 +02:00
|
|
|
| Tarray (t, n), idx::idx_list ->
|
|
|
|
(*Body of the copy loops*)
|
|
|
|
let copy i =
|
2011-04-19 12:58:41 +02:00
|
|
|
let src_i = array_elt_of_exp i src in
|
2011-04-14 15:14:41 +02:00
|
|
|
let dest_i = mk_pattern t (Larray (dest, i)) in
|
2011-04-14 18:06:54 +02:00
|
|
|
[Aassgn(dest_i, src_i)]
|
2011-04-14 15:14:41 +02:00
|
|
|
in
|
|
|
|
(*Copy values < idx*)
|
2011-04-18 15:38:42 +02:00
|
|
|
let a_lower = fresh_for (mk_exp_const_int 0) idx copy in
|
2011-04-14 15:14:41 +02:00
|
|
|
(* Update the correct element*)
|
2011-04-19 12:58:41 +02:00
|
|
|
let src_idx = array_elt_of_exp idx src in
|
2011-04-18 15:38:42 +02:00
|
|
|
let dest_idx = mk_pattern t (Larray (dest, idx)) in
|
2011-11-21 01:37:15 +01:00
|
|
|
let a_update = ssa_update_array dest_idx src_idx idx_list v in
|
2011-04-14 15:14:41 +02:00
|
|
|
(*Copy values > idx*)
|
2011-04-18 15:38:42 +02:00
|
|
|
let idx_plus_one = mk_plus_one idx in
|
|
|
|
let a_upper = fresh_for idx_plus_one (mk_exp_static_int n) copy in
|
2011-04-14 18:06:54 +02:00
|
|
|
[a_lower] @ a_update @ [a_upper]
|
2011-04-14 15:14:41 +02:00
|
|
|
| _, _ ->
|
|
|
|
[Aassgn(dest, v)]
|
|
|
|
|
|
|
|
(** Creates the action list that copies [src] to [dest],
|
|
|
|
updating the value of field [f] with the value [v]. *)
|
2011-11-21 01:37:15 +01:00
|
|
|
let ssa_update_record dest src f v =
|
2011-04-14 15:14:41 +02:00
|
|
|
let assgn_act { f_name = l; f_type = ty } =
|
|
|
|
let dest_l = mk_pattern ty (Lfield(dest, l)) in
|
2011-05-30 10:06:16 +02:00
|
|
|
let src_l = mk_ext_value_exp ty (Wfield(src, l)) in
|
2011-04-14 15:14:41 +02:00
|
|
|
if f = l then
|
|
|
|
Aassgn(dest_l, v)
|
|
|
|
else
|
|
|
|
Aassgn(dest_l, src_l)
|
|
|
|
in
|
2011-04-14 18:06:54 +02:00
|
|
|
let fields = match dest.pat_ty with
|
|
|
|
| Tid n -> Modules.find_struct n
|
2011-05-23 09:24:57 +02:00
|
|
|
| _ -> Misc.internal_error "mls2obc field of nonstruct"
|
2011-04-14 18:06:54 +02:00
|
|
|
in
|
|
|
|
List.map assgn_act fields
|
2011-04-14 15:14:41 +02:00
|
|
|
|
2011-05-30 10:06:16 +02:00
|
|
|
let rec control map ck s = match ck with
|
|
|
|
| Cbase | Cvar { contents = Cindex _ } -> s
|
|
|
|
| Cvar { contents = Clink ck } -> control map ck s
|
|
|
|
| Con(ck, c, n) ->
|
|
|
|
let x = ext_value_exp_from_name map n in
|
|
|
|
control map ck (Acase(x, [(c, mk_block [s])]))
|
2011-04-14 11:53:39 +02:00
|
|
|
|
2010-07-13 14:03:39 +02:00
|
|
|
let reinit o =
|
|
|
|
Acall ([], o, Mreset, [])
|
2010-06-29 11:18:50 +02:00
|
|
|
|
2011-05-30 10:06:16 +02:00
|
|
|
let rec translate_pat map ty pat = match pat, ty with
|
|
|
|
| Minils.Evarpat x, _ -> [ var_from_name map x ]
|
|
|
|
| Minils.Etuplepat pat_list, Tprod ty_l ->
|
|
|
|
List.fold_right2 (fun ty pat acc -> (translate_pat map ty pat) @ acc)
|
|
|
|
ty_l pat_list []
|
2011-06-28 14:45:15 +02:00
|
|
|
| Minils.Etuplepat _, _ -> Misc.internal_error "Ill-typed pattern"
|
2010-06-29 11:18:50 +02:00
|
|
|
|
2010-11-23 17:13:33 +01:00
|
|
|
let translate_var_dec l =
|
2011-04-26 18:02:18 +02:00
|
|
|
let one_var { Minils.v_ident = x; Minils.v_type = t; Minils.v_linearity = lin; v_loc = loc } =
|
|
|
|
mk_var_dec ~loc:loc ~linearity:lin x t
|
2010-07-22 09:36:22 +02:00
|
|
|
in
|
|
|
|
List.map one_var l
|
|
|
|
|
2011-05-30 10:06:16 +02:00
|
|
|
let rec translate_extvalue map w = match w.Minils.w_desc with
|
|
|
|
| Minils.Wvar x -> ext_value_of_pattern (var_from_name map x)
|
|
|
|
| _ ->
|
|
|
|
let desc = match w.Minils.w_desc with
|
|
|
|
| Minils.Wconst v -> Wconst v
|
|
|
|
| Minils.Wvar x -> assert false
|
|
|
|
| Minils.Wfield (w1, f) -> Wfield (translate_extvalue map w1, f)
|
2011-10-17 15:28:04 +02:00
|
|
|
| Minils.Wwhen (w1, _, _) | Minils.Wreinit(_, w1) -> (translate_extvalue map w1).w_desc
|
2011-05-30 10:06:16 +02:00
|
|
|
in
|
|
|
|
mk_ext_value w.Minils.w_ty desc
|
|
|
|
|
|
|
|
and translate_extvalue_to_exp map w =
|
|
|
|
mk_exp ~loc:w.Minils.w_loc w.Minils.w_ty (Eextvalue (translate_extvalue map w))
|
2011-04-14 11:17:12 +02:00
|
|
|
|
2010-06-15 10:49:03 +02:00
|
|
|
(* [translate e = c] *)
|
2010-11-05 16:03:39 +01:00
|
|
|
let rec translate map e =
|
2010-07-13 14:03:39 +02:00
|
|
|
let desc = match e.Minils.e_desc with
|
2011-04-14 11:17:12 +02:00
|
|
|
| Minils.Eextvalue w ->
|
2011-05-30 10:06:16 +02:00
|
|
|
let w = translate_extvalue map w in Eextvalue w
|
|
|
|
| Minils.Eapp ({ Minils.a_op = Minils.Eequal }, w_list, _) ->
|
|
|
|
Eop (op_from_string "=", List.map (translate_extvalue_to_exp map) w_list)
|
2011-04-14 11:17:12 +02:00
|
|
|
| Minils.Eapp ({ Minils.a_op = Minils.Efun n }, e_list, _)
|
|
|
|
when Mls_utils.is_op n ->
|
2011-05-30 10:06:16 +02:00
|
|
|
Eop (n, List.map (translate_extvalue_to_exp map ) e_list)
|
2010-06-29 11:18:50 +02:00
|
|
|
| Minils.Estruct f_e_list ->
|
2011-01-05 15:47:53 +01:00
|
|
|
let type_name = (match e.Minils.e_ty with
|
|
|
|
| Tid name -> name
|
|
|
|
| _ -> assert false) in
|
2011-04-14 11:17:12 +02:00
|
|
|
let f_e_list = List.map
|
2011-05-30 10:06:16 +02:00
|
|
|
(fun (f, e) -> (f, (translate_extvalue_to_exp map e))) f_e_list in
|
2011-01-05 15:47:53 +01:00
|
|
|
Estruct (type_name, f_e_list)
|
|
|
|
(*Remaining array operators*)
|
2010-07-13 14:03:39 +02:00
|
|
|
| Minils.Eapp ({ Minils.a_op = Minils.Earray }, e_list, _) ->
|
2011-05-30 10:06:16 +02:00
|
|
|
Earray (List.map (translate_extvalue_to_exp map ) e_list)
|
2010-07-13 14:03:39 +02:00
|
|
|
| Minils.Eapp ({ Minils.a_op = Minils.Eselect;
|
2011-05-30 10:06:16 +02:00
|
|
|
Minils.a_params = idx_list }, e_list, _) ->
|
2011-04-14 11:17:12 +02:00
|
|
|
let e = translate_extvalue map (assert_1 e_list) in
|
2011-05-30 10:06:16 +02:00
|
|
|
let idx_list = List.map mk_exp_static_int idx_list in
|
|
|
|
Eextvalue (extvalue_of_idx_list e idx_list)
|
2011-05-18 09:59:21 +02:00
|
|
|
| Minils.Ewhen(e,_,_) ->
|
|
|
|
let e = translate map e in
|
|
|
|
e.e_desc
|
2011-01-05 15:51:55 +01:00
|
|
|
(* Already treated cases when translating the [eq] *)
|
|
|
|
| Minils.Eiterator _ | Minils.Emerge _ | Minils.Efby _
|
2011-03-22 22:12:59 +01:00
|
|
|
| Minils.Eapp ({Minils.a_op=(Minils.Enode _|Minils.Efun _|Minils.Econcat
|
2011-04-14 18:06:54 +02:00
|
|
|
|Minils.Eupdate|Minils.Eselect_dyn
|
|
|
|
|Minils.Eselect_trunc|Minils.Eselect_slice
|
|
|
|
|Minils.Earray_fill|Minils.Efield_update
|
2011-04-18 16:09:07 +02:00
|
|
|
|Minils.Eifthenelse)}, _, _) ->
|
2011-05-23 09:24:57 +02:00
|
|
|
internal_error "mls2obc"
|
2010-07-13 14:03:39 +02:00
|
|
|
in
|
2011-04-14 11:17:12 +02:00
|
|
|
mk_exp e.Minils.e_ty desc
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2011-04-18 15:38:42 +02:00
|
|
|
and translate_act_extvalue map pat w =
|
|
|
|
match pat with
|
|
|
|
| Minils.Evarpat n ->
|
2011-05-30 10:06:16 +02:00
|
|
|
[Aassgn (var_from_name map n, translate_extvalue_to_exp map w)]
|
2011-04-18 15:38:42 +02:00
|
|
|
| _ -> assert false
|
|
|
|
|
2010-08-04 15:36:20 +02:00
|
|
|
(* [translate pat act = si, d] *)
|
2010-11-05 16:03:39 +01:00
|
|
|
and translate_act map pat
|
2010-06-29 11:18:50 +02:00
|
|
|
({ Minils.e_desc = desc } as act) =
|
2010-08-04 15:36:20 +02:00
|
|
|
match pat, desc with
|
2011-01-20 23:05:18 +01:00
|
|
|
(* When Merge *)
|
2011-05-18 09:59:21 +02:00
|
|
|
| pat, Minils.Ewhen (e,_,_) -> translate_act map pat e
|
2011-04-18 15:38:42 +02:00
|
|
|
| Minils.Evarpat x, Minils.Emerge (y, c_act_list) ->
|
|
|
|
let x = var_from_name map x in
|
|
|
|
let translate_c_extvalue (c, w) =
|
2011-05-30 10:06:16 +02:00
|
|
|
c, mk_block [Aassgn (x, translate_extvalue_to_exp map w)]
|
2011-04-18 15:38:42 +02:00
|
|
|
in
|
2011-05-30 10:06:16 +02:00
|
|
|
|
|
|
|
[Acase (ext_value_exp_from_name map y,
|
|
|
|
List.map translate_c_extvalue c_act_list)]
|
2011-01-20 23:05:18 +01:00
|
|
|
(* Array ops *)
|
2011-04-14 11:17:12 +02:00
|
|
|
| Minils.Evarpat x,
|
|
|
|
Minils.Eapp ({ Minils.a_op = Minils.Econcat }, [e1; e2], _) ->
|
2011-01-24 16:07:26 +01:00
|
|
|
let cpt1, cpt1d = fresh_it () in
|
|
|
|
let cpt2, cpt2d = fresh_it () in
|
2011-04-14 11:53:39 +02:00
|
|
|
let x = var_from_name map x in
|
2011-01-24 16:07:26 +01:00
|
|
|
let t = x.pat_ty in
|
2011-04-18 15:38:42 +02:00
|
|
|
(match e1.Minils.w_ty, e2.Minils.w_ty with
|
2011-01-24 16:07:26 +01:00
|
|
|
| Tarray (t1, n1), Tarray (t2, n2) ->
|
2011-05-30 10:06:16 +02:00
|
|
|
let e1 = translate_extvalue_to_exp map e1 in
|
|
|
|
let e2 = translate_extvalue_to_exp map e2 in
|
2010-08-04 15:36:20 +02:00
|
|
|
let a1 =
|
2011-04-18 15:38:42 +02:00
|
|
|
Afor (cpt1d, mk_exp_const_int 0, mk_exp_static_int n1,
|
2011-04-14 11:17:12 +02:00
|
|
|
mk_block [Aassgn (mk_pattern t1 (Larray (x, mk_evar_int cpt1)),
|
2011-04-19 12:58:41 +02:00
|
|
|
array_elt_of_exp (mk_evar_int cpt1) e1)] ) in
|
2011-04-14 11:17:12 +02:00
|
|
|
let idx = mk_exp_int (Eop (op_from_string "+",
|
2011-05-30 10:06:16 +02:00
|
|
|
[ mk_exp_static_int n1; mk_evar_int cpt2])) in
|
2011-04-19 12:58:41 +02:00
|
|
|
let p2 = array_elt_of_exp (mk_evar_int cpt2) e2 in
|
2011-04-18 15:38:42 +02:00
|
|
|
let a2 = Afor (cpt2d, mk_exp_const_int 0, mk_exp_static_int n2,
|
2011-04-14 11:17:12 +02:00
|
|
|
mk_block [Aassgn (mk_pattern t2 (Larray (x, idx)), p2)] )
|
2010-08-04 15:36:20 +02:00
|
|
|
in
|
|
|
|
[a1; a2]
|
2011-04-14 11:17:12 +02:00
|
|
|
| _ -> assert false)
|
|
|
|
|
|
|
|
| Minils.Evarpat x,
|
2011-05-26 18:39:33 +02:00
|
|
|
Minils.Eapp ({ Minils.a_op = Minils.Earray_fill; Minils.a_params = n_list }, [e], _) ->
|
2011-05-30 10:06:16 +02:00
|
|
|
let e = translate_extvalue_to_exp map e in
|
2011-04-14 11:53:39 +02:00
|
|
|
let x = var_from_name map x in
|
2011-03-21 14:30:19 +01:00
|
|
|
let t = match x.pat_ty with
|
|
|
|
| Tarray (t,_) -> t
|
2011-05-23 09:24:57 +02:00
|
|
|
| _ -> Misc.internal_error "mls2obc select slice type"
|
2011-03-21 14:30:19 +01:00
|
|
|
in
|
2011-05-30 10:06:16 +02:00
|
|
|
|
2011-05-26 18:39:33 +02:00
|
|
|
let rec make_loop power_list replace = match power_list with
|
|
|
|
| [] -> x, replace
|
|
|
|
| p :: power_list ->
|
|
|
|
let cpt, cptd = fresh_it () in
|
2011-05-30 10:06:16 +02:00
|
|
|
let e, replace =
|
|
|
|
make_loop power_list
|
|
|
|
(fun y -> [Afor (cptd, mk_exp_const_int 0,
|
2011-05-26 18:39:33 +02:00
|
|
|
mk_exp_static_int p, mk_block (replace y))]) in
|
|
|
|
let e = Larray (e, mk_evar_int cpt) in
|
|
|
|
(mk_pattern t e, replace)
|
|
|
|
in
|
|
|
|
let e, b = make_loop n_list (fun y -> [Aassgn (y, e)]) in
|
|
|
|
b e
|
2011-04-14 11:17:12 +02:00
|
|
|
|
|
|
|
| Minils.Evarpat x,
|
|
|
|
Minils.Eapp ({ Minils.a_op = Minils.Eselect_slice;
|
|
|
|
Minils.a_params = [idx1; idx2] }, [e], _) ->
|
2011-01-24 16:07:26 +01:00
|
|
|
let cpt, cptd = fresh_it () in
|
2011-05-30 10:06:16 +02:00
|
|
|
let e = translate_extvalue_to_exp map e in
|
2011-04-14 11:53:39 +02:00
|
|
|
let x = var_from_name map x in
|
2011-03-21 14:30:19 +01:00
|
|
|
let t = match x.pat_ty with
|
|
|
|
| Tarray (t,_) -> t
|
2011-05-23 09:24:57 +02:00
|
|
|
| _ -> Misc.internal_error "mls2obc select slice type"
|
2011-03-21 14:30:19 +01:00
|
|
|
in
|
2011-04-14 11:17:12 +02:00
|
|
|
let idx = mk_exp_int (Eop (op_from_string "+",
|
2011-05-30 10:06:16 +02:00
|
|
|
[mk_evar_int cpt; mk_exp_static_int idx1 ])) in
|
2010-09-13 11:23:39 +02:00
|
|
|
(* bound = (idx2 - idx1) + 1*)
|
|
|
|
let bound = mk_static_int_op (op_from_string "+")
|
2011-04-14 11:17:12 +02:00
|
|
|
[ mk_static_int 1; mk_static_int_op (op_from_string "-") [idx2;idx1] ] in
|
2011-04-18 15:38:42 +02:00
|
|
|
[ Afor (cptd, mk_exp_const_int 0, mk_exp_static_int bound,
|
2011-03-21 14:30:19 +01:00
|
|
|
mk_block [Aassgn (mk_pattern t (Larray (x, mk_evar_int cpt)),
|
2011-04-19 12:58:41 +02:00
|
|
|
array_elt_of_exp idx e)] ) ]
|
2011-04-14 11:17:12 +02:00
|
|
|
|
2011-01-20 23:05:18 +01:00
|
|
|
| Minils.Evarpat x, Minils.Eapp ({ Minils.a_op = Minils.Eselect_dyn }, e1::e2::idx, _) ->
|
2011-04-14 11:53:39 +02:00
|
|
|
let x = var_from_name map x in
|
2011-04-18 15:38:42 +02:00
|
|
|
let bounds = Mls_utils.bounds_list e1.Minils.w_ty in
|
2011-04-14 11:17:12 +02:00
|
|
|
let e1 = translate_extvalue map e1 in
|
2011-05-30 10:06:16 +02:00
|
|
|
let idx = List.map (translate_extvalue_to_exp map) idx in
|
|
|
|
let w = extvalue_of_idx_list e1 idx in
|
|
|
|
let true_act = Aassgn (x, mk_exp w.w_ty (Eextvalue w)) in
|
|
|
|
let false_act = Aassgn (x, translate_extvalue_to_exp map e2) in
|
2010-09-13 11:23:39 +02:00
|
|
|
let cond = bound_check_expr idx bounds in
|
2011-04-18 15:38:42 +02:00
|
|
|
[ mk_ifthenelse cond [true_act] [false_act] ]
|
2011-03-22 22:12:59 +01:00
|
|
|
|
|
|
|
| Minils.Evarpat x, Minils.Eapp ({ Minils.a_op = Minils.Eselect_trunc }, e1::idx, _) ->
|
2011-04-14 11:53:39 +02:00
|
|
|
let x = var_from_name map x in
|
2011-04-18 15:38:42 +02:00
|
|
|
let bounds = Mls_utils.bounds_list e1.Minils.w_ty in
|
2011-04-14 11:17:12 +02:00
|
|
|
let e1 = translate_extvalue map e1 in
|
2011-05-30 10:06:16 +02:00
|
|
|
let idx = List.map (translate_extvalue_to_exp map) idx in
|
|
|
|
let w = ext_value_of_trunc_idx_list e1 idx in
|
|
|
|
[Aassgn (x, mk_exp w.w_ty (Eextvalue w))]
|
2011-03-22 22:12:59 +01:00
|
|
|
|
Decade alpha1
On the road to beta is the new Minils AST, for now :
* Heptagon and Obc AST changes,
* Java code generation,
* Recursives Qualnames,
* Various bug fixes,
* Added partial application for iterators,
For instance:
... = map<<n>> (f<<se>>)((t1, t1'))(t2, t3)
is translated to:
for(int i =...)
... = f(t1, t1', t2[i], t3[i])
2011-01-05 15:51:55 +01:00
|
|
|
| Minils.Evarpat x, Minils.Eapp ({ Minils.a_op = Minils.Eupdate }, e1::e2::idx, _) ->
|
2011-04-14 11:53:39 +02:00
|
|
|
let x = var_from_name map x in
|
2011-04-18 15:38:42 +02:00
|
|
|
let bounds = Mls_utils.bounds_list e1.Minils.w_ty in
|
2011-05-30 10:06:16 +02:00
|
|
|
let idx = List.map (translate_extvalue_to_exp map) idx in
|
|
|
|
let e1 = translate_extvalue_to_exp map e1 in
|
|
|
|
let e2 = translate_extvalue_to_exp map e2 in
|
2010-09-13 11:23:39 +02:00
|
|
|
let cond = bound_check_expr idx bounds in
|
2011-11-21 01:37:15 +01:00
|
|
|
let copy = Aassgn (x, e1) in
|
|
|
|
if !Compiler_options.strict_ssa
|
|
|
|
then (
|
|
|
|
let ssa_up = ssa_update_array x e1 idx e2 in
|
|
|
|
[ mk_ifthenelse cond ssa_up [copy] ]
|
|
|
|
) else (
|
|
|
|
let assgn = Aassgn (pattern_of_idx_list x idx, e2) in
|
|
|
|
[copy; mk_if cond [assgn]]
|
|
|
|
)
|
2010-09-13 11:23:39 +02:00
|
|
|
|
|
|
|
| Minils.Evarpat x,
|
2011-04-14 11:17:12 +02:00
|
|
|
Minils.Eapp ({ Minils.a_op = Minils.Efield_update;
|
|
|
|
Minils.a_params = [{ se_desc = Sfield f }] }, [e1; e2], _) ->
|
2011-04-14 11:53:39 +02:00
|
|
|
let x = var_from_name map x in
|
2011-11-21 01:37:15 +01:00
|
|
|
let e1' = translate_extvalue map e1 in
|
2011-05-30 10:06:16 +02:00
|
|
|
let e2 = translate_extvalue_to_exp map e2 in
|
2011-11-21 01:37:15 +01:00
|
|
|
if !Compiler_options.strict_ssa
|
|
|
|
then ssa_update_record x e1' f e2
|
|
|
|
else (
|
|
|
|
let copy = Aassgn (x, translate_extvalue_to_exp map e1) in
|
|
|
|
let action = Aassgn (mk_pattern (Types.Tid (Modules.find_field f)) (Lfield (x, f)), e2) in
|
|
|
|
[copy; action]
|
|
|
|
)
|
2010-06-29 11:18:50 +02:00
|
|
|
| Minils.Evarpat n, _ ->
|
2011-04-14 11:53:39 +02:00
|
|
|
[Aassgn (var_from_name map n, translate map act)]
|
2010-09-13 10:20:24 +02:00
|
|
|
| _ ->
|
2011-01-05 15:47:53 +01:00
|
|
|
Format.eprintf "%a The pattern %a should be a simple var to be translated to obc.@."
|
|
|
|
Location.print_location act.Minils.e_loc Mls_printer.print_pat pat;
|
|
|
|
assert false
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2011-01-20 23:05:18 +01:00
|
|
|
(** In an iteration, objects used are element of object arrays *)
|
2011-06-27 10:58:14 +02:00
|
|
|
type obj_array = { oa_index : Obc.pattern list; oa_size : static_exp list }
|
2011-01-20 23:05:18 +01:00
|
|
|
|
|
|
|
(** A [None] context is normal, otherwise, we are in an iteration *)
|
|
|
|
type call_context = obj_array option
|
|
|
|
|
|
|
|
let mk_obj_call_from_context c n = match c with
|
|
|
|
| None -> Oobj n
|
|
|
|
| Some oa -> Oarray (n, oa.oa_index)
|
2010-07-21 16:00:06 +02:00
|
|
|
|
2011-01-20 23:05:18 +01:00
|
|
|
let size_from_call_context c = match c with
|
|
|
|
| None -> None
|
|
|
|
| Some oa -> Some (oa.oa_size)
|
2010-07-21 16:00:06 +02:00
|
|
|
|
2011-01-20 23:05:18 +01:00
|
|
|
let empty_call_context = None
|
2010-07-21 16:00:06 +02:00
|
|
|
|
2011-02-14 15:21:57 +01:00
|
|
|
(** [si] the initialization actions used in the reset method,
|
2010-11-05 16:03:39 +01:00
|
|
|
[j] obj decs
|
2011-02-14 15:21:57 +01:00
|
|
|
[s] the actions used in the step method.
|
2010-11-05 16:03:39 +01:00
|
|
|
[v] var decs *)
|
2011-11-28 00:45:45 +01:00
|
|
|
let rec translate_eq map call_context ({ Minils.eq_lhs = pat; Minils.eq_rhs = e } as eq)
|
2010-07-22 09:36:22 +02:00
|
|
|
(v, si, j, s) =
|
2011-05-09 19:32:12 +02:00
|
|
|
let { Minils.e_desc = desc; Minils.e_base_ck = ck; Minils.e_loc = loc } = e in
|
2010-06-26 16:53:25 +02:00
|
|
|
match (pat, desc) with
|
2011-11-28 00:45:45 +01:00
|
|
|
| pat, Minils.Ewhen (e,_,_) ->
|
|
|
|
translate_eq map call_context {eq with Minils.eq_rhs = e} (v, si, j, s)
|
|
|
|
(* TODO Efby and Eifthenelse should be dealt with in translate_act, no ? *)
|
2010-06-29 11:18:50 +02:00
|
|
|
| Minils.Evarpat n, Minils.Efby (opt_c, e) ->
|
2011-04-14 11:53:39 +02:00
|
|
|
let x = var_from_name map n in
|
2010-06-29 11:18:50 +02:00
|
|
|
let si = (match opt_c with
|
|
|
|
| None -> si
|
2011-05-30 10:06:16 +02:00
|
|
|
| Some c -> (Aassgn (x, mk_ext_value_static x.pat_ty c)) :: si) in
|
|
|
|
let action = Aassgn (var_from_name map n, translate_extvalue_to_exp map e) in
|
2011-04-14 13:56:24 +02:00
|
|
|
v, si, j, (control map ck action) :: s
|
2011-04-14 11:17:12 +02:00
|
|
|
(* should be unnecessary
|
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)
|
2011-04-14 11:17:12 +02:00
|
|
|
*)
|
2010-07-13 14:42:46 +02:00
|
|
|
| pat, Minils.Eapp({ Minils.a_op = Minils.Eifthenelse }, [e1;e2;e3], _) ->
|
2011-05-30 10:06:16 +02:00
|
|
|
let cond = translate_extvalue_to_exp map e1 in
|
2011-04-18 15:38:42 +02:00
|
|
|
let true_act = translate_act_extvalue map pat e2 in
|
|
|
|
let false_act = translate_act_extvalue map pat e3 in
|
|
|
|
let action = mk_ifthenelse cond true_act false_act in
|
2011-05-30 10:06:16 +02:00
|
|
|
v, si, j, (control map ck action) :: s
|
2010-07-13 14:42:46 +02:00
|
|
|
|
2011-01-24 16:07:26 +01:00
|
|
|
| pat, Minils.Eapp ({ Minils.a_op = Minils.Efun _ | Minils.Enode _ } as app, e_list, r) ->
|
2011-05-30 10:06:16 +02:00
|
|
|
let name_list = translate_pat map e.Minils.e_ty pat in
|
|
|
|
let c_list = List.map (translate_extvalue_to_exp map) e_list in
|
2011-04-14 11:17:12 +02:00
|
|
|
let v', si', j', action = mk_node_call map call_context
|
|
|
|
app loc name_list c_list e.Minils.e_ty in
|
2011-04-14 13:56:24 +02:00
|
|
|
let action = List.map (control map ck) action in
|
2010-07-21 16:00:06 +02:00
|
|
|
let s = (match r, app.Minils.a_op with
|
|
|
|
| Some r, Minils.Enode _ ->
|
2010-07-27 17:55:45 +02:00
|
|
|
let ck = Clocks.Con (ck, Initial.ptrue, r) in
|
2011-04-14 13:56:24 +02:00
|
|
|
let ra = List.map (control map ck) si' in
|
2010-07-21 16:00:06 +02:00
|
|
|
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
|
|
|
|
2011-06-27 16:08:56 +02:00
|
|
|
| pat, Minils.Eiterator (it, app, n_list, pe_list, e_list, reset) ->
|
2011-05-30 10:06:16 +02:00
|
|
|
let name_list = translate_pat map e.Minils.e_ty pat in
|
|
|
|
let p_list = List.map (translate_extvalue_to_exp map) pe_list in
|
|
|
|
let c_list = List.map (translate_extvalue_to_exp map) e_list in
|
2011-06-27 10:58:14 +02:00
|
|
|
let xl, xdl = List.split (List.map (fun _ -> fresh_it ()) n_list) in
|
2011-06-27 19:20:47 +02:00
|
|
|
let call_context =
|
2011-06-27 10:58:14 +02:00
|
|
|
Some { oa_index = List.map (fun x -> mk_pattern_int (Lvar x)) xl;
|
|
|
|
oa_size = n_list} in
|
|
|
|
let n_list = List.map mk_exp_static_int n_list in
|
2010-07-21 16:00:06 +02:00
|
|
|
let si', j', action = translate_iterator map call_context it
|
2011-06-27 10:58:14 +02:00
|
|
|
name_list app loc n_list xl xdl p_list c_list e.Minils.e_ty in
|
2011-04-14 13:56:24 +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-27 17:55:45 +02:00
|
|
|
let ck = Clocks.Con (ck, Initial.ptrue, r) in
|
2011-04-14 13:56:24 +02:00
|
|
|
let ra = List.map (control map ck) si' in
|
2010-07-21 16:00:06 +02:00
|
|
|
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-11-05 16:03:39 +01:00
|
|
|
let action = translate_act map pat e in
|
2011-04-14 13:56:24 +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
|
|
|
|
2011-05-30 10:06:16 +02:00
|
|
|
and mk_node_call map call_context app loc (name_list : Obc.pattern list) args ty =
|
2010-07-21 16:00:06 +02:00
|
|
|
match app.Minils.a_op with
|
2010-07-27 17:48:21 +02:00
|
|
|
| Minils.Efun f when Mls_utils.is_op f ->
|
2011-04-28 15:20:21 +02:00
|
|
|
let act = match name_list with
|
|
|
|
| [] -> Aop (f, args)
|
|
|
|
| [name] ->
|
|
|
|
let e = mk_exp ty (Eop(f, args)) in
|
|
|
|
Aassgn (name, e)
|
|
|
|
| _ ->
|
2011-06-09 14:12:32 +02:00
|
|
|
Misc.unsupported "mls2obc: external function with multiple return values" in
|
2011-04-28 15:20:21 +02:00
|
|
|
[], [], [], [act]
|
2010-07-27 17:48:21 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
| Minils.Enode f when Itfusion.is_anon_node f ->
|
2011-05-30 10:06:16 +02:00
|
|
|
let add_input env vd =
|
|
|
|
Env.add vd.Minils.v_ident
|
|
|
|
(mk_pattern vd.Minils.v_type (Lvar vd.Minils.v_ident)) env in
|
2011-01-20 23:05:18 +01:00
|
|
|
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
|
2011-05-30 10:06:16 +02:00
|
|
|
| Eextvalue { w_desc = Wvar x } ->
|
2010-07-21 16:00:06 +02:00
|
|
|
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
|
|
|
|
|
2011-04-18 15:38:42 +02:00
|
|
|
let nd = find_anon f in
|
2010-09-06 14:03:47 +02:00
|
|
|
let map = List.fold_left add_input map nd.Minils.n_input in
|
|
|
|
let map = List.fold_left2 build map nd.Minils.n_output name_list in
|
|
|
|
let map = List.fold_left add_input map nd.Minils.n_local in
|
|
|
|
let v, si, j, s = translate_eq_list map call_context nd.Minils.n_equs in
|
|
|
|
let env = List.fold_left2 build Env.empty nd.Minils.n_input args in
|
|
|
|
v @ nd.Minils.n_local, si, j, subst_act_list env s
|
|
|
|
|
|
|
|
| Minils.Enode f | Minils.Efun f ->
|
2011-11-21 01:37:15 +01:00
|
|
|
let id = match app.Minils.a_id with
|
|
|
|
| None -> gen_obj_ident f
|
|
|
|
| Some id -> id
|
|
|
|
in
|
2011-03-16 23:34:35 +01:00
|
|
|
let o = mk_obj_call_from_context call_context id in
|
2010-09-06 14:03:47 +02:00
|
|
|
let obj =
|
2011-01-20 23:05:18 +01:00
|
|
|
{ o_ident = obj_ref_name o; o_class = f;
|
2010-09-06 14:03:47 +02:00
|
|
|
o_params = app.Minils.a_params;
|
|
|
|
o_size = size_from_call_context call_context; o_loc = loc } in
|
2011-11-21 01:37:15 +01:00
|
|
|
let si = match app.Minils.a_op with
|
|
|
|
| Minils.Efun _ -> []
|
|
|
|
| Minils.Enode _ -> [reinit o]
|
|
|
|
| _ -> assert false
|
|
|
|
in
|
2011-03-09 00:02:30 +01:00
|
|
|
let s = [Acall (name_list, o, Mstep, args)] in
|
2011-01-05 15:51:55 +01:00
|
|
|
[], si, [obj], s
|
2010-07-21 16:00:06 +02:00
|
|
|
| _ -> assert false
|
|
|
|
|
2011-03-21 17:22:03 +01:00
|
|
|
and translate_iterator map call_context it name_list
|
2011-06-27 10:58:14 +02:00
|
|
|
app loc n_list xl xdl p_list c_list ty =
|
|
|
|
let rec unarray n ty = match ty, n with
|
|
|
|
| Tarray (t,_), 1 -> t
|
|
|
|
| Tarray (t,_), n -> unarray (n-1) t
|
2011-03-21 17:22:03 +01:00
|
|
|
| _ ->
|
|
|
|
Format.eprintf "%a" Global_printer.print_type ty;
|
2011-05-23 09:24:57 +02:00
|
|
|
internal_error "mls2obc"
|
2011-01-24 16:07:26 +01:00
|
|
|
in
|
2011-06-27 10:58:14 +02:00
|
|
|
let unarray = unarray (List.length n_list) in
|
2011-01-24 16:07:26 +01:00
|
|
|
let array_of_output name_list ty_list =
|
2011-06-27 10:58:14 +02:00
|
|
|
let rec aux l ty xl = match ty, xl with
|
|
|
|
| _, [] -> l
|
2011-07-08 17:22:58 +02:00
|
|
|
| Tarray(tyn, _), x :: xl -> aux (mk_pattern ~loc:loc tyn (Larray(l, mk_evar_int x))) tyn xl
|
2011-06-27 10:58:14 +02:00
|
|
|
| _, _ -> assert false
|
|
|
|
in
|
|
|
|
List.map2 (fun l ty -> aux l ty xl) name_list ty_list
|
2011-01-24 16:07:26 +01:00
|
|
|
in
|
2010-07-13 14:03:39 +02:00
|
|
|
let array_of_input c_list =
|
2011-06-27 19:20:47 +02:00
|
|
|
List.map (array_elt_of_exp_list (List.map mk_evar_int xl)) c_list
|
2011-06-27 10:58:14 +02:00
|
|
|
in
|
|
|
|
let mk_loop b xdl nl =
|
|
|
|
let rec mk_loop b xdl nl = match xdl, nl with
|
|
|
|
| xd::[], n::[] -> Afor (xd, mk_exp_const_int 0, n, b)
|
|
|
|
| xd::xdl, n::nl -> mk_loop (mk_block [Afor (xd, mk_exp_const_int 0, n, b)]) xdl nl
|
|
|
|
| _, _ -> assert false
|
|
|
|
in
|
2011-06-27 19:20:47 +02:00
|
|
|
mk_loop b (List.rev xdl) nl
|
2011-06-27 10:58:14 +02:00
|
|
|
in
|
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
|
2011-02-07 14:24:17 +01:00
|
|
|
let ty_list = List.map unarray (Types.unprod ty) in
|
2011-06-27 10:58:14 +02:00
|
|
|
let name_list = array_of_output name_list (Types.unprod ty) in
|
2011-02-07 14:24:17 +01:00
|
|
|
let node_out_ty = Types.prod ty_list in
|
2010-07-22 09:36:22 +02:00
|
|
|
let v, si, j, action = mk_node_call map call_context
|
2011-03-21 17:22:03 +01:00
|
|
|
app loc name_list (p_list@c_list) node_out_ty in
|
2010-11-05 16:03:39 +01:00
|
|
|
let v = translate_var_dec v in
|
2010-07-22 09:36:22 +02:00
|
|
|
let b = mk_block ~locals:v action in
|
2011-02-07 14:24:17 +01:00
|
|
|
let bi = mk_block si in
|
2011-06-27 10:58:14 +02:00
|
|
|
[mk_loop bi xdl n_list], j, [mk_loop b xdl n_list]
|
2010-06-18 10:30:23 +02:00
|
|
|
|
2011-03-22 09:28:41 +01:00
|
|
|
| Minils.Imapi ->
|
|
|
|
let c_list = array_of_input c_list in
|
|
|
|
let ty_list = List.map unarray (Types.unprod ty) in
|
2011-06-27 10:58:14 +02:00
|
|
|
let name_list = array_of_output name_list (Types.unprod ty) in
|
2011-03-22 09:28:41 +01:00
|
|
|
let node_out_ty = Types.prod ty_list in
|
|
|
|
let v, si, j, action = mk_node_call map call_context
|
2011-06-27 10:58:14 +02:00
|
|
|
app loc name_list (p_list@c_list@(List.map mk_evar_int xl)) node_out_ty in
|
2011-03-22 09:28:41 +01:00
|
|
|
let v = translate_var_dec v in
|
|
|
|
let b = mk_block ~locals:v action in
|
|
|
|
let bi = mk_block si in
|
2011-06-27 10:58:14 +02:00
|
|
|
[mk_loop bi xdl n_list], j, [mk_loop b xdl n_list]
|
2011-03-22 09:28:41 +01: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
|
2011-07-08 17:22:58 +02:00
|
|
|
let ty_list = Types.unprod ty in
|
|
|
|
let ty_name_list, _ = Misc.split_last ty_list in
|
2011-02-07 14:24:17 +01:00
|
|
|
let (name_list, acc_out) = Misc.split_last name_list in
|
|
|
|
let name_list = array_of_output name_list ty_name_list in
|
2011-07-08 17:22:58 +02:00
|
|
|
let node_out_ty = Types.prod (Misc.map_butlast unarray ty_list) in
|
2011-03-21 17:22:03 +01:00
|
|
|
let v, si, j, action = mk_node_call map call_context app loc
|
|
|
|
(name_list @ [ acc_out ])
|
2011-05-30 10:06:16 +02:00
|
|
|
(p_list @ c_list @ [ exp_of_pattern acc_out ])
|
2011-03-21 17:22:03 +01:00
|
|
|
node_out_ty
|
2011-01-24 16:07:26 +01:00
|
|
|
in
|
2010-11-05 16:03:39 +01:00
|
|
|
let v = translate_var_dec v in
|
2010-07-22 09:36:22 +02:00
|
|
|
let b = mk_block ~locals:v action in
|
2011-02-07 14:24:17 +01:00
|
|
|
let bi = mk_block si in
|
2011-06-27 10:58:14 +02:00
|
|
|
[mk_loop bi xdl n_list], j,
|
|
|
|
[Aassgn (acc_out, acc_in); mk_loop b xdl n_list]
|
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
|
2011-01-24 16:07:26 +01:00
|
|
|
let v, si, j, action =
|
2011-03-21 17:22:03 +01:00
|
|
|
mk_node_call map call_context app loc name_list
|
2011-05-30 10:06:16 +02:00
|
|
|
(p_list @ c_list @ [ exp_of_pattern acc_out ]) ty
|
2011-01-24 16:07:26 +01:00
|
|
|
in
|
2010-11-05 16:03:39 +01:00
|
|
|
let v = translate_var_dec v in
|
2010-07-22 09:36:22 +02:00
|
|
|
let b = mk_block ~locals:v action in
|
2011-02-07 14:24:17 +01:00
|
|
|
let bi = mk_block si in
|
2011-06-27 10:58:14 +02:00
|
|
|
[mk_loop bi xdl n_list], j,
|
|
|
|
[ Aassgn (acc_out, acc_in); mk_loop b xdl n_list]
|
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
|
2011-01-24 16:07:26 +01:00
|
|
|
let v, si, j, action = mk_node_call map call_context app loc name_list
|
2011-06-27 16:08:56 +02:00
|
|
|
(p_list @ c_list @ (List.map mk_evar_int xl) @ [ exp_of_pattern acc_out ]) ty
|
2011-01-24 16:07:26 +01:00
|
|
|
in
|
2010-11-05 16:03:39 +01:00
|
|
|
let v = translate_var_dec v in
|
2010-07-26 09:33:22 +02:00
|
|
|
let b = mk_block ~locals:v action in
|
2011-02-07 14:24:17 +01:00
|
|
|
let bi = mk_block si in
|
2011-06-27 10:58:14 +02:00
|
|
|
[mk_loop bi xdl n_list], j,
|
|
|
|
[ Aassgn (acc_out, acc_in); mk_loop b xdl n_list]
|
2010-07-26 09:33:22 +02:00
|
|
|
|
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
|
|
|
|
2011-01-24 16:07:26 +01:00
|
|
|
let translate_contract map mem_var_tys =
|
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;
|
|
|
|
} ->
|
2011-01-24 16:07:26 +01:00
|
|
|
let (v, si, j, s_list) = translate_eq_list map empty_call_context eq_list in
|
2010-11-05 16:03:39 +01:00
|
|
|
let d_list = translate_var_dec (v @ d_list) in
|
2010-07-16 09:58:56 +02:00
|
|
|
let d_list = List.filter
|
2011-01-24 16:07:26 +01:00
|
|
|
(fun vd -> not (List.exists (fun (i,_) -> i = vd.v_ident) mem_var_tys)) d_list in
|
2010-07-16 09:58:56 +02:00
|
|
|
(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. *)
|
2011-07-27 16:13:45 +02:00
|
|
|
let subst_map inputs outputs controllables c_locals locals mem_tys =
|
2010-06-18 10:30:23 +02:00
|
|
|
(* Create a map that simply maps each var to itself *)
|
2011-01-24 16:07:26 +01:00
|
|
|
let map =
|
2010-07-13 14:03:39 +02:00
|
|
|
List.fold_left
|
2011-01-24 16:07:26 +01:00
|
|
|
(fun m { Minils.v_ident = x; Minils.v_type = ty } -> Env.add x (mk_pattern ty (Lvar x)) m)
|
2011-07-27 16:13:45 +02:00
|
|
|
Env.empty (inputs @ outputs @ controllables @ c_locals @ locals)
|
2010-06-26 16:53:25 +02:00
|
|
|
in
|
2011-01-24 16:07:26 +01:00
|
|
|
List.fold_left (fun map (x, x_ty) -> Env.add x (mk_pattern x_ty (Lmem x)) map) map mem_tys
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2010-07-13 14:03:39 +02:00
|
|
|
let translate_node
|
2011-03-21 14:30:19 +01: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_stateful = stateful;
|
|
|
|
Minils.n_contract = contract; Minils.n_params = params; Minils.n_loc = loc;
|
2011-04-20 18:20:53 +02:00
|
|
|
Minils.n_mem_alloc = mem_alloc
|
2010-07-16 09:58:56 +02:00
|
|
|
} as n) =
|
2011-01-20 23:05:18 +01:00
|
|
|
Idents.enter_node f;
|
2011-01-24 16:07:26 +01:00
|
|
|
let mem_var_tys = Mls_utils.node_memory_vars n in
|
2011-09-06 11:54:03 +02:00
|
|
|
let c_list, c_locals =
|
2011-07-27 16:13:45 +02:00
|
|
|
match contract with
|
|
|
|
| None -> [], []
|
|
|
|
| Some c -> c.Minils.c_controllables, c.Minils.c_local in
|
|
|
|
let subst_map = subst_map i_list o_list c_list c_locals d_list mem_var_tys in
|
2011-01-20 23:05:18 +01:00
|
|
|
let (v, si, j, s_list) = translate_eq_list subst_map empty_call_context eq_list in
|
2011-01-24 16:07:26 +01:00
|
|
|
let (si', j', s_list', d_list') = translate_contract subst_map mem_var_tys contract in
|
2010-11-05 16:03:39 +01:00
|
|
|
let i_list = translate_var_dec i_list in
|
|
|
|
let o_list = translate_var_dec o_list in
|
|
|
|
let d_list = translate_var_dec (v @ d_list) in
|
2011-04-14 11:17:12 +02:00
|
|
|
let m, d_list = List.partition
|
|
|
|
(fun vd -> List.exists (fun (i,_) -> i = vd.v_ident) mem_var_tys) d_list in
|
2011-11-29 14:08:12 +01:00
|
|
|
let m', o_list =
|
|
|
|
List.partition
|
|
|
|
(fun vd -> List.exists (fun (i,_) -> i = vd.v_ident) mem_var_tys) o_list in
|
2011-04-14 13:56:24 +02:00
|
|
|
let s = s_list @ s_list' in
|
2010-07-22 10:15:11 +02:00
|
|
|
let j = j' @ j in
|
2011-04-14 13:56:24 +02:00
|
|
|
let si = si @ si' in
|
2011-03-21 14:30:19 +01:00
|
|
|
let stepm = { m_name = Mstep; m_inputs = i_list; m_outputs = o_list;
|
|
|
|
m_body = mk_block ~locals:(d_list' @ d_list) s }
|
|
|
|
in
|
|
|
|
let resetm = { m_name = Mreset; m_inputs = []; m_outputs = []; m_body = mk_block si } in
|
|
|
|
if stateful
|
2011-11-29 14:08:12 +01:00
|
|
|
then { cd_name = f; cd_stateful = true; cd_mems = m' @ m; cd_params = params;
|
2011-04-20 18:20:53 +02:00
|
|
|
cd_objs = j; cd_methods = [stepm; resetm]; cd_loc = loc; cd_mem_alloc = mem_alloc }
|
2011-04-14 11:17:12 +02:00
|
|
|
else (
|
|
|
|
(* Functions won't have [Mreset] or memories,
|
|
|
|
they still have [params] and instances (of functions) *)
|
2011-03-21 14:30:19 +01:00
|
|
|
{ cd_name = f; cd_stateful = false; cd_mems = []; cd_params = params;
|
2011-04-20 18:20:53 +02:00
|
|
|
cd_objs = j; cd_methods = [stepm]; cd_loc = loc; cd_mem_alloc = mem_alloc }
|
2011-03-21 14:30:19 +01:00
|
|
|
)
|
2010-07-13 14:03:39 +02:00
|
|
|
|
|
|
|
let translate_ty_def { Minils.t_name = name; Minils.t_desc = tdesc;
|
|
|
|
Minils.t_loc = loc } =
|
2010-09-09 00:35:06 +02:00
|
|
|
let tdesc = match tdesc with
|
|
|
|
| Minils.Type_abs -> Type_abs
|
|
|
|
| Minils.Type_alias ln -> Type_alias ln
|
2010-09-13 13:44:26 +02:00
|
|
|
| Minils.Type_enum tag_name_list -> Type_enum tag_name_list
|
2011-03-21 14:30:19 +01:00
|
|
|
| Minils.Type_struct field_ty_list -> Type_struct field_ty_list
|
|
|
|
in
|
2010-09-13 09:03:15 +02:00
|
|
|
{ t_name = name; t_desc = tdesc; t_loc = loc }
|
2010-07-13 14:03:39 +02:00
|
|
|
|
|
|
|
let translate_const_def { Minils.c_name = name; Minils.c_value = se;
|
|
|
|
Minils.c_type = ty; Minils.c_loc = loc } =
|
2010-09-13 09:03:15 +02:00
|
|
|
{ c_name = name;
|
2010-07-13 14:03:39 +02:00
|
|
|
c_value = se;
|
|
|
|
c_type = ty;
|
|
|
|
c_loc = loc }
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2011-04-18 19:20:03 +02:00
|
|
|
let program { Minils.p_modname = p_modname; Minils.p_opened = p_o; Minils.p_desc = pd; } =
|
|
|
|
build_anon pd;
|
2011-04-19 09:49:00 +02:00
|
|
|
|
2011-04-18 19:20:03 +02:00
|
|
|
let program_desc pd acc = match pd with
|
2011-04-19 18:45:56 +02:00
|
|
|
| Minils.Pnode n when not (Itfusion.is_anon_node n.Minils.n_name) ->
|
|
|
|
Pclass (translate_node n) :: acc
|
2011-04-19 09:49:00 +02:00
|
|
|
(* dont't translate anonymous nodes, they will be inlined *)
|
2011-04-19 18:45:56 +02:00
|
|
|
| Minils.Pnode n -> acc
|
|
|
|
| Minils.Ptype t -> Ptype (translate_ty_def t) :: acc
|
|
|
|
| Minils.Pconst c -> Pconst (translate_const_def c) :: acc
|
|
|
|
in
|
|
|
|
let p_desc = List.fold_right program_desc pd [] in
|
2011-03-21 14:30:19 +01:00
|
|
|
{ p_modname = p_modname;
|
2011-04-19 09:49:00 +02:00
|
|
|
p_opened = p_o;
|
2011-04-18 19:20:03 +02:00
|
|
|
p_desc = p_desc }
|
2010-06-18 10:30:23 +02:00
|
|
|
|
2011-09-06 11:54:03 +02:00
|
|
|
|
|
|
|
let signature s =
|
|
|
|
{ sig_name = s.Minils.sig_name;
|
|
|
|
sig_inputs = s.Minils.sig_inputs;
|
|
|
|
sig_stateful = s.Minils.sig_stateful;
|
|
|
|
sig_outputs = s.Minils.sig_outputs;
|
|
|
|
sig_params = s.Minils.sig_params;
|
|
|
|
sig_param_constraints = s.Minils.sig_param_constraints;
|
|
|
|
sig_loc = s.Minils.sig_loc }
|
|
|
|
|
|
|
|
let interface i =
|
|
|
|
let interface_decl id = match id with
|
|
|
|
| Minils.Itypedef td -> Itypedef (translate_ty_def td)
|
|
|
|
| Minils.Iconstdef cd -> Iconstdef (translate_const_def cd)
|
|
|
|
| Minils.Isignature s -> Isignature (signature s)
|
|
|
|
in
|
|
|
|
{ i_modname = i.Minils.i_modname;
|
|
|
|
i_opened = i.Minils.i_opened;
|
|
|
|
i_desc = List.map interface_decl i.Minils.i_desc }
|