2011-02-14 15:21:57 +01:00
|
|
|
open Misc
|
2011-03-29 20:43:55 +02:00
|
|
|
open Names
|
2011-03-23 20:31:26 +01:00
|
|
|
open Signature
|
2011-01-20 23:05:18 +01:00
|
|
|
open Java
|
|
|
|
open Java_printer
|
|
|
|
|
2011-02-14 15:21:57 +01:00
|
|
|
(** returns the vd and the pat of a fresh ident from [name] *)
|
|
|
|
let mk_var ty name =
|
|
|
|
let id = Idents.gen_var "java_main" name in
|
|
|
|
mk_var_dec id ty, Pvar id
|
2011-01-20 23:05:18 +01:00
|
|
|
|
2010-07-13 14:03:39 +02:00
|
|
|
let program p =
|
2011-04-19 18:45:56 +02:00
|
|
|
(*Scalarize*)
|
|
|
|
let p = Compiler_utils.pass "Scalarize" true Scalarize.program p Obc_compiler.pp in
|
2011-01-24 16:07:26 +01:00
|
|
|
let p_java = Obc2java.program p in
|
2011-02-07 14:24:17 +01:00
|
|
|
let dir = Compiler_utils.build_path "java" in
|
|
|
|
Compiler_utils.ensure_dir dir;
|
2011-02-14 15:21:57 +01:00
|
|
|
|
|
|
|
(* Compile and output the nodes *)
|
|
|
|
output_program dir p_java;
|
|
|
|
|
|
|
|
(* Create a runnable main simulation *)
|
|
|
|
if !Compiler_options.simulation
|
|
|
|
then (
|
|
|
|
let class_name = Obc2java.fresh_classe (!Compiler_options.simulation_node ^ "_sim") in
|
|
|
|
Idents.enter_node class_name;
|
|
|
|
let field_step_dnb, id_step_dnb =
|
|
|
|
let id = Idents.gen_var "java_main" "default_step_nb" in
|
|
|
|
mk_field ~static:true ~final:true ~value:(Some (Sint 30000)) Tint id, id
|
|
|
|
in
|
|
|
|
let main_methode =
|
|
|
|
let vd_step, pat_step = mk_var Tint "step" in
|
2011-04-19 18:45:56 +02:00
|
|
|
let vd_args, pat_args =
|
|
|
|
mk_var (Tarray (Tclass (Names.pervasives_qn "String"), (Sint 0))) "args" in
|
2011-02-14 15:21:57 +01:00
|
|
|
let body =
|
2011-03-23 20:31:26 +01:00
|
|
|
let vd_main, e_main, q_main, ty_main =
|
|
|
|
let q_main = !Compiler_options.simulation_node |> Modules.qualify_value in (*qual*)
|
2011-04-19 18:45:56 +02:00
|
|
|
let ty_main =
|
|
|
|
(Modules.find_value q_main).node_outputs |> types_of_arg_list |> Types.prod in
|
2011-03-23 20:31:26 +01:00
|
|
|
let q_main = Obc2java.qualname_to_package_classe q_main in (*java qual*)
|
|
|
|
let id = Idents.gen_var "java_main" "main" in
|
|
|
|
mk_var_dec id (Tclass q_main), Eval (Pvar id), q_main, ty_main
|
2011-02-14 15:21:57 +01:00
|
|
|
in
|
|
|
|
let acts =
|
|
|
|
let integer = Eval(Pclass(Names.pervasives_qn "Integer")) in
|
|
|
|
let args1 = Eval(Parray_elem(pat_args, Sint 1)) in
|
|
|
|
let out = Eval(Pclass(Names.qualname_of_string "java.lang.System.out")) in
|
2011-03-23 20:31:26 +01:00
|
|
|
let jarrays = Eval(Pclass(Names.qualname_of_string "java.util.Arrays")) in
|
2011-03-29 20:43:55 +02:00
|
|
|
let jint = Eval(Pclass(Names.qualname_of_string "Integer")) in
|
|
|
|
let jfloat = Eval(Pclass(Names.qualname_of_string "Float")) in
|
|
|
|
let jbool = Eval(Pclass(Names.qualname_of_string "Boolean")) in
|
2011-03-23 20:31:26 +01:00
|
|
|
let ret = Emethod_call(e_main, "step", []) in
|
|
|
|
let print_ret = match ty_main with
|
|
|
|
| Types.Tarray (Types.Tarray _, _) -> Emethod_call(jarrays, "deepToString", [ret])
|
|
|
|
| Types.Tarray _ -> Emethod_call(jarrays, "toString", [ret])
|
2011-03-29 20:43:55 +02:00
|
|
|
| t when t = Initial.tint -> Emethod_call(jint, "toString", [ret])
|
|
|
|
| t when t = Initial.tfloat -> Emethod_call(jfloat, "toString", [ret])
|
|
|
|
| t when t = Initial.tbool -> Emethod_call(jbool, "toString", [ret])
|
2011-03-23 20:31:26 +01:00
|
|
|
| _ -> Emethod_call(ret, "toString", [])
|
|
|
|
in
|
2011-02-14 15:21:57 +01:00
|
|
|
[ Anewvar(vd_main, Enew (Tclass q_main, []));
|
|
|
|
Aifelse( Efun(Names.pervasives_qn ">", [Eval (Pfield (pat_args, "length")); Sint 1])
|
|
|
|
, mk_block [Aassgn(pat_step, Emethod_call(integer, "parseInt", [args1]))]
|
|
|
|
, mk_block [Aassgn(pat_step, Eval (Pvar id_step_dnb))]);
|
|
|
|
Obc2java.fresh_for (Eval pat_step)
|
|
|
|
(fun i ->
|
2011-03-23 20:31:26 +01:00
|
|
|
[ Amethod_call(out, "printf", [ Sstring "%d => %s\\n"; Eval (Pvar i); print_ret]) ]
|
2011-03-22 17:43:04 +01:00
|
|
|
)
|
2011-02-14 15:21:57 +01:00
|
|
|
]
|
|
|
|
in
|
|
|
|
mk_block ~locals:[vd_step] acts
|
|
|
|
in
|
2011-03-09 00:02:30 +01:00
|
|
|
mk_methode ~static:true ~args:[vd_args] body "main"
|
2011-02-14 15:21:57 +01:00
|
|
|
in
|
2011-03-09 00:02:30 +01:00
|
|
|
let c = mk_classe ~fields:[field_step_dnb] ~methodes:[main_methode] class_name in
|
2011-02-14 15:21:57 +01:00
|
|
|
output_program dir [c]
|
|
|
|
)
|