Feature: Java main will give bin args as static params to the main.
consider having a main node like this one: node main<<i : int; b : bool>> () returns... Then if to call the generated program, you'll need ot give an int and a bool. Furthermore, if you give an additional arg, it'll be used as the wanted iteration number. Conflicts: compiler/obc/java/java_main.ml
This commit is contained in:
parent
4ac4347dc8
commit
ab3823874c
2 changed files with 59 additions and 12 deletions
|
@ -120,6 +120,8 @@ let names_of_arg_list l = List.map (fun ad -> ad.a_name) l
|
|||
|
||||
let types_of_arg_list l = List.map (fun ad -> ad.a_type) l
|
||||
|
||||
let types_of_param_list l = List.map (fun p -> p.p_type) l
|
||||
|
||||
let mk_arg name ty ck = { a_type = ty; a_name = name; a_clock = ck }
|
||||
|
||||
let mk_param name ty = { p_name = name; p_type = ty }
|
||||
|
|
|
@ -10,6 +10,7 @@ let mk_var ty name =
|
|||
let id = Idents.gen_var "java_main" name in
|
||||
mk_var_dec id ty, Pvar id, Evar id
|
||||
|
||||
|
||||
let program p =
|
||||
(*Scalarize*)
|
||||
let p = Compiler_utils.pass "Scalarize" true Scalarize.program p Obc_compiler.pp in
|
||||
|
@ -29,7 +30,9 @@ let program p =
|
|||
Format.eprintf "Unable to find main node: %s@." !Compiler_options.simulation_node;
|
||||
raise Errors.Error
|
||||
in
|
||||
let ty_main = (find_value q_main).node_outputs |> types_of_arg_list |> Types.prod in
|
||||
let sig_main = find_value q_main in
|
||||
let ty_main = sig_main.node_outputs |> types_of_arg_list |> Types.prod in
|
||||
let ty_main_args = sig_main.node_params |> types_of_param_list in
|
||||
let class_name = Obc2java.fresh_classe (!Compiler_options.simulation_node ^ "_sim") in
|
||||
Idents.enter_node class_name;
|
||||
let field_step_dnb, id_step_dnb =
|
||||
|
@ -37,9 +40,21 @@ let program p =
|
|||
mk_field ~static:true ~final:true ~value:(Some (Sint 30000)) Tint id, id
|
||||
in
|
||||
let main_methode =
|
||||
|
||||
(* step is the current iteration step *)
|
||||
let vd_step, pat_step, exp_step = mk_var Tint "step" in
|
||||
|
||||
let vd_args, _, exp_args =
|
||||
mk_var (Tarray (Tclass (Names.pervasives_qn "String"), (Sint 0))) "args" in
|
||||
|
||||
let get_arg i = Earray_elem(exp_args, Sint i) in
|
||||
|
||||
(* (* argnb is the current argument during the parsing *)
|
||||
let vd_argnb, pat_argnb, exp_argnb = mk_var Tint "argNb" in
|
||||
let init_argnb = Aassgn (pat_argnb, Sint 0) in
|
||||
let incr_argnb = Aassgn (pat_argnb, Efun(pervasives_qn "+", [exp_argnb; Sint 1])) in
|
||||
let exp_current_arg = Earray_elem(exp_args, exp_argnb) in
|
||||
*)
|
||||
let body =
|
||||
let vd_main, e_main, q_main, ty_main =
|
||||
let q_main = Obc2java.qualname_to_package_classe q_main in (*java qual*)
|
||||
|
@ -47,13 +62,38 @@ let program p =
|
|||
mk_var_dec id (Tclass q_main), Evar id, q_main, ty_main
|
||||
in
|
||||
let acts =
|
||||
let integer = Eclass(Names.pervasives_qn "Integer") in
|
||||
let args1 = Earray_elem(exp_args, Sint 1) in
|
||||
let out = Eclass(Names.qualname_of_string "java.lang.System.out") in
|
||||
let jarrays = Eclass(Names.qualname_of_string "java.util.Arrays") in
|
||||
let jint = Eclass(Names.qualname_of_string "Integer") in
|
||||
let jfloat = Eclass(Names.qualname_of_string "Float") in
|
||||
let jbool = Eclass(Names.qualname_of_string "Boolean") in
|
||||
|
||||
(* parse arguments to give to the main *)
|
||||
let rec parse_args t_l i = match t_l with
|
||||
| [] -> []
|
||||
| t::t_l when t = Initial.tint ->
|
||||
(Emethod_call(jint, "parseInt", [get_arg i]))
|
||||
:: parse_args t_l (i+1)
|
||||
| t::t_l when t = Initial.tfloat ->
|
||||
(Emethod_call(jfloat, "parseFloat", [get_arg i]))
|
||||
:: parse_args t_l (i+1)
|
||||
| t::t_l when t = Initial.tint ->
|
||||
(Emethod_call(jbool, "parseBool", [get_arg i]))
|
||||
:: parse_args t_l (i+1)
|
||||
| _ -> Misc.unsupported "java main does not support parsing complexe static args"
|
||||
in
|
||||
let main_args = parse_args ty_main_args 0 in
|
||||
|
||||
let parse_max_iteration =
|
||||
let t_size = List.length ty_main_args in
|
||||
(* no more arg to give to main, the last one if it exists is the iteration nb *)
|
||||
Aifelse(Efun(Names.pervasives_qn ">", [ Efield (exp_args, "length"); Sint t_size ]),
|
||||
(* given max number of iterations *)
|
||||
mk_block [Aassgn(pat_step,
|
||||
Emethod_call(jint, "parseInt", [get_arg t_size]))],
|
||||
(* default max number of iterations *)
|
||||
mk_block [Aassgn(pat_step, Evar id_step_dnb)]);
|
||||
in
|
||||
let ret = Emethod_call(e_main, "step", []) in
|
||||
let print_ret = match ty_main with
|
||||
| Types.Tarray (Types.Tarray _, _) -> Emethod_call(jarrays, "deepToString", [ret])
|
||||
|
@ -62,16 +102,21 @@ let program p =
|
|||
| t when t = Initial.tfloat -> Emethod_call(jfloat, "toString", [ret])
|
||||
| t when t = Initial.tbool -> Emethod_call(jbool, "toString", [ret])
|
||||
| _ -> Emethod_call(ret, "toString", [])
|
||||
let main_for_loop i =
|
||||
[Aexp (Emethod_call(out, "printf",
|
||||
[Sstring "%d => %s\\n"; Evar i; print_ret]))]
|
||||
in
|
||||
[ Anewvar(vd_main, Enew (Tclass q_main, []));
|
||||
Aifelse( Efun(Names.pervasives_qn ">", [Efield (exp_args, "length"); Sint 1])
|
||||
, mk_block [Aassgn(pat_step, Emethod_call(integer, "parseInt", [args1]))]
|
||||
, mk_block [Aassgn(pat_step, Evar id_step_dnb)]);
|
||||
Obc2java.fresh_for exp_step
|
||||
(fun i ->
|
||||
[Aexp (Emethod_call(out, "printf",
|
||||
[Sstring "%d => %s\\n"; Evar i; print_ret]))]
|
||||
)
|
||||
let vd_t1, e_t1 =
|
||||
let id = Idents.gen_var "java_main" "t" in
|
||||
mk_var_dec id Tlong, Evar id
|
||||
in
|
||||
[ Anewvar(vd_main, Enew (Tclass q_main, main_args));
|
||||
parse_max_iteration;
|
||||
Anewvar(vd_t1, Emethod_call(jsys, "currentTimeMillis", []));
|
||||
Obc2java.fresh_for exp_step main_for_loop;
|
||||
Aexp (Emethod_call(out, "printf",
|
||||
[ Sstring "time : %d\\n";
|
||||
Efun(jminus, [Emethod_call(jsys, "currentTimeMillis", []); e_t1])]))
|
||||
]
|
||||
in
|
||||
mk_block ~locals:[vd_step] acts
|
||||
|
|
Loading…
Reference in a new issue