From 9301ed0b09b78ff37d6986b09ee5ec9c74249c8a Mon Sep 17 00:00:00 2001 From: Adrien Guatto Date: Mon, 19 Jul 2010 15:16:14 +0200 Subject: [PATCH] Fixes for correct array literal C generation. --- compiler/minils/transformations/normalize.ml | 11 +++++++++++ compiler/obc/c/c.ml | 2 +- compiler/obc/c/cgen.ml | 10 ++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/compiler/minils/transformations/normalize.ml b/compiler/minils/transformations/normalize.ml index d2b7f54..5aff1b3 100644 --- a/compiler/minils/transformations/normalize.ml +++ b/compiler/minils/transformations/normalize.ml @@ -188,6 +188,17 @@ let rec translate kind context e = let context, e_list = translate_app kind context app.a_op e_list in context, { e with e_desc = Eapp(app, e_list, r) } | Eiterator (it, app, n, e_list, reset) -> + (* Add an intermediate equation for each array lit argument. *) + let add_eq_for_array exp (context, e_list) = match exp.e_desc with + | Econst { se_desc = Sarray _; } -> + let id = Ident.fresh "arr" in + let eq = mk_equation (Evarpat id) exp in + let exp = { exp with e_desc = (Evar id); } in + let var = mk_var_dec ~clock:exp.e_ck id exp.e_ty in + ((var :: fst context, eq :: snd context), exp :: e_list) + | _ -> (context, exp :: e_list) in + let context, e_list = + List.fold_right add_eq_for_array e_list (context, []) in let context, e_list = translate_list function_args_kind context e_list in context, { e with e_desc = Eiterator(it, app, n, e_list, reset) } diff --git a/compiler/obc/c/c.ml b/compiler/obc/c/c.ml index f372bfb..eecabce 100644 --- a/compiler/obc/c/c.ml +++ b/compiler/obc/c/c.ml @@ -240,7 +240,7 @@ and pp_cexpr fmt ce = match ce with | Cstructlit (s, el) -> fprintf fmt "(%a){@[%a@]}" pp_string s (pp_list1 pp_cexpr ",") el | Carraylit el -> - fprintf fmt "((int *){@[%a@]})" (pp_list1 pp_cexpr ",") el (* WRONG *) + fprintf fmt "((int []){@[%a@]})" (pp_list1 pp_cexpr ",") el (* WRONG *) and pp_clhs fmt lhs = match lhs with | Cvar s -> pp_string fmt s | Cderef lhs' -> fprintf fmt "*%a" pp_clhs lhs' diff --git a/compiler/obc/c/cgen.ml b/compiler/obc/c/cgen.ml index dabf40d..86f0406 100644 --- a/compiler/obc/c/cgen.ml +++ b/compiler/obc/c/cgen.ml @@ -255,6 +255,7 @@ let rec cexpr_of_static_exp se = | Sfloat f -> Cconst (Ccfloat f) | Sbool b -> Cconst (Ctag (if b then "TRUE" else "FALSE")) | Sconstructor c -> Cconst (Ctag (shortname c)) + | Sarray sl -> Carraylit (List.map cexpr_of_static_exp sl) | Sarray_power(n,c) -> let cc = cexpr_of_static_exp c in Carraylit (repeat_list cc (int_of_static_exp n)) @@ -423,8 +424,13 @@ let rec create_affect_const var_env dest c = match c.se_desc with | Sarray_power(c, n) -> let x = gen_symbol () in - [ Cfor(x, 0, int_of_static_exp n, - create_affect_const var_env (Carray (dest, Clhs (Cvar x))) c) ] + [Cfor(x, 0, int_of_static_exp n, + create_affect_const var_env (Carray (dest, Clhs (Cvar x))) c)] + | Sarray cl -> + let create_affect_idx c (i, affl) = + let dest = Carray (dest, Cconst (Ccint i)) in + (i - 1, create_affect_const var_env dest c @ affl) in + snd (List.fold_right create_affect_idx cl (List.length cl - 1, [])) | _ -> [Caffect (dest, cexpr_of_exp var_env (mk_exp (Econst c)))] (** [cstm_of_act obj_env mods act] translates the Obj action [act] to a list of