Commit 826aa62e authored by Erwan Jahier's avatar Erwan Jahier
Browse files

Some work on the Alice backend.

- Add an option to specify the dir where to put generated C files in.
- Propagate all lutin options in the C part that fork the lutin interpreter
(-rif, -precision, -node, -boot)
- use the Sys.argv.(0) as the lutin interpreter to be called (to make sure
the same lutin is used and to be able to specify the full path in the C
file if needed).
parent 4743771d
......@@ -20,6 +20,7 @@ type alice_args = {
env_in_vars : Exp.var list;
env_out_vars : Exp.var list;
use_sockets: bool;
output_dir:string;
}
......@@ -106,14 +107,15 @@ let (gen_alice_var_tab : string -> string -> Exp.var list -> string) =
let (gen_alice_stub : alice_args -> string) =
fun args ->
let alice_name = args.alice_module_name
let alice_full_name = args.alice_module_name
and alice_name = Filename.basename args.alice_module_name
and fn = args.env_name
and in_vars = args.env_in_vars
and out_vars = args.env_out_vars
in
("
#include \""^alice_name^".h\"
#include \""^alice_full_name^".h\"
#include \"float.h\"
#include \"Ims/ImsMessageStack.h\"
......@@ -223,10 +225,14 @@ structTab* "^alice_name^"::Memories() {
")
let my_open_out fn =
prerr_string ("Generating file " ^ fn ^ "\n");
open_out fn
let (gen_alice_stub_c : alice_args -> unit) =
fun args ->
let oc = open_out (args.alice_module_name ^ ".cpp") in
let amn = Filename.basename args.alice_module_name in
let oc = my_open_out (Filename.concat args.output_dir (amn ^ ".cpp")) in
let put s = output_string oc s in
let putln s = output_string oc (s^"\n") in
let rec putlist = function
......@@ -240,15 +246,22 @@ let (gen_alice_stub_c : alice_args -> unit) =
let (gen_alice_stub_h : alice_args -> unit) =
fun args ->
let amn = args.alice_module_name in
let oc = open_out (amn ^ ".h") in
let amn = Filename.basename args.alice_module_name in
let oc = my_open_out (Filename.concat args.output_dir (amn ^ ".h")) in
let amn = Filename.basename args.alice_module_name in
let putln s = output_string oc (s^"\n") in
let fn = args.env_name in
putln (Util.entete "// ");
putln ("
#include \"AlicesCommon.h\"
#define "^amn^"_interface LINKER_EXPORTED
#ifdef BUILD_"^amn^"
#define "^amn^"_interface LINKER_EXPORTED
#else // BUILD_"^amn^"
#define "^amn^"_interface LINKER_IMPORTED
#endif // BUILD_"^amn^"
extern \"C\"
{
......
......@@ -24,36 +24,51 @@ let step_mode_to_str = function
| Vertices -> "step_vertices"
type optionT = {
mutable pp : string option;
mutable env : string list;
mutable main_node : string option; (* Main node *)
mutable boot : bool;
mutable pp : string option; (* Pre-processor *)
mutable output : string option;
mutable rif : string option;
mutable calling_module_name : string;
mutable gen_mode : gen_mode;
mutable use_sockets : bool;
mutable step_mode : step_mode;
mutable seed : int option;
mutable env : string list;
mutable sock_addr : string
mutable precision : int option;
mutable use_sockets : bool;
mutable sock_addr : string;
mutable output_dir : string;
}
let (option : optionT) = {
env = [];
main_node = None;
pp = None;
boot = false;
output = None;
rif = None;
gen_mode = Nop;
use_sockets = false;
calling_module_name = "XXX_SCADE_MODULE_NAME";
step_mode = Inside;
seed = None;
env = [];
sock_addr = "127.0.0.1"
precision = None;
sock_addr = "127.0.0.1";
output_dir = Filename.current_dir_name
}
(****************************************************************************)
let my_open_out fn =
prerr_string ("Generating file " ^ fn ^ "\n");
open_out fn
let (gen_lustre_ext_h : string -> unit) =
fun fn ->
let oc = open_out (option.calling_module_name ^ "_ext.h") in
let fn = Filename.concat option.output_dir (option.calling_module_name ^ "_ext.h") in
let oc = my_open_out fn in
let putln s = output_string oc (s^"\n") in
putln (Util.entete "// ");
putln ("#include \"" ^ option.calling_module_name ^ "_ext_func.h\"");
......@@ -62,7 +77,7 @@ let (gen_lustre_ext_h : string -> unit) =
let (gen_lustre_ext_func_h : string -> Exp.var list -> Exp.var list -> unit) =
fun fn in_vars out_vars ->
let oc = open_out (option.calling_module_name ^ "_ext_func.h") in
let oc = my_open_out (Filename.concat option.output_dir (option.calling_module_name ^ "_ext_func.h")) in
let put s = output_string oc s in
let putln s = output_string oc (s^"\n") in
let rec putlist = function
......@@ -98,7 +113,7 @@ let (gen_lustre_ext_func_h : string -> Exp.var list -> Exp.var list -> unit) =
let (gen_lustre_ext_func_c : string -> Exp.var list -> Exp.var list -> unit) =
fun fn in_vars out_vars ->
let oc = open_out (option.calling_module_name ^ "_ext_func.c") in
let oc = my_open_out (Filename.concat option.output_dir (option.calling_module_name ^ "_ext_func.c")) in
let put s = output_string oc s in
let putln s = output_string oc (s^"\n") in
let rec putlist = function
......@@ -177,7 +192,7 @@ let (gen_lustre_ext_func_c : string -> Exp.var list -> Exp.var list -> unit) =
(****************************************************************************)
let (gen_h_file : string -> Exp.var list -> Exp.var list -> Exp.var list -> unit) =
fun fn in_vars out_vars loc_vars ->
let oc = open_out (fn ^ ".h") in
let oc = my_open_out (Filename.concat option.output_dir (fn ^ ".h")) in
let put s = output_string oc s in
let putln s = output_string oc (s^"\n") in
let max_buff_size =
......@@ -358,6 +373,8 @@ let var_to_cast var =
| Type.UT _ -> assert false
let lutin = Sys.argv.(0)
let put_socket_func put fn in_vars out_vars loc_vars =
let putln str = put (str^"\n") in
put ("
......@@ -395,6 +412,8 @@ Input procedures must be used:
)
in_vars;
let lut_file = (List.hd option.env) (* only work with lutin XXX fixme? *) in
let lut_dir = Filename.dirname lut_file in
putln ("
/*--------
launch the lutin interpreter and init socket stuff
......@@ -416,16 +435,19 @@ launch the lutin interpreter and init socket stuff
char *sock_addr = \""^option.sock_addr^"\";
const char *args[] = {
#ifdef _WIN32
\"call-via-socket.exe\", sock_addr, portno_str, \"lutin.exe\",
\"call-via-socket.exe\", sock_addr, portno_str, \""^(String.escaped lutin)^"\",
#else
\"call-via-socket\", sock_addr, portno_str, \"lutin\",
\"call-via-socket\", sock_addr, portno_str, \""^(String.escaped lutin)^"\",
#endif
" ^
(match option.seed with None -> "" | Some i -> ("\"-seed\", \""^(string_of_int i)^"\", "))
^" \""^
(List.hd option.env) (* only work with lutin XXX fixme! *)
^"\", \"-rif\",\""^fn^".rif\", NULL};
" ^ (match option.main_node with None -> "" | Some node -> ("\"-main\", \""^node^"\", "))
^ (match option.seed with None -> "" | Some i -> ("\"-seed\", \""^(string_of_int i)^"\", "))
^ (if option.boot then "-boot, " else "")
^ (match option.precision with None -> "" | Some i -> ("\"-precision\", \""^(string_of_int i)^"\", "))
^" \""^ (String.escaped lut_file) ^"\", \"-rif\",\""^(
match option.rif with
Some rif -> String.escaped rif
| None -> String.escaped (fn^".rif"))
^"\", NULL};
// Socket administration stuff
......@@ -594,13 +616,13 @@ void " ^ fn ^ "_reset(" ^ fn ^ "_ctx* ctx){
let (gen_c_file : string -> Exp.var list -> Exp.var list -> Exp.var list -> unit) =
let (gen_c_file : string -> Exp.var list -> Exp.var list -> Exp.var list -> unit) =
fun fn in_vars out_vars loc_vars ->
let oc =
if option.gen_mode = Scade then
open_out (fn ^ "_fctext.c")
my_open_out (Filename.concat option.output_dir (fn ^ "_fctext.c"))
else
open_out (fn ^ ".c")
my_open_out (Filename.concat option.output_dir (fn ^ ".c"))
in
let put s = output_string oc s in
let putln s = output_string oc (s^"\n") in
......@@ -845,6 +867,7 @@ let (main : unit -> unit) =
Luc2alice.env_in_vars = state.s.in_vars ;
Luc2alice.env_out_vars = state.s.out_vars ;
Luc2alice.use_sockets = option.use_sockets ;
Luc2alice.output_dir = option.output_dir;
}
in
......
......@@ -20,16 +20,21 @@ type gen_mode = Lustre | Scade | Alice | Luciole | Nop
type step_mode = Inside | Edges | Vertices
type optionT = {
mutable env : string list; (* lutin/lucky files *)
mutable main_node : string option; (* Main node *)
mutable boot : bool;
mutable pp : string option; (* Pre-processor *)
mutable output : string option; (* A string used in generated files name *)
mutable rif : string option; (* Name of the rif file used to put generated data *)
mutable calling_module_name : string; (* also used in generated files name *)
mutable gen_mode : gen_mode;
mutable use_sockets : bool; (* For the C stubs file generation: use the lutin interpreter via sockets
instead of the interpreter embedded into the C libraries. *)
mutable step_mode : step_mode;
mutable seed : int option;
mutable env : string list; (* lutin/lucky files *)
mutable sock_addr : string
mutable precision : int option;
mutable use_sockets : bool; (* For the C stubs file generation: use the lutin interpreter via sockets
instead of the interpreter embedded into the C libraries. *)
mutable sock_addr : string;
mutable output_dir : string;
}
val option : optionT
......
......@@ -56,6 +56,7 @@ let gen_mode () = !_gen_mode
let _boot = ref false
let boot () = !_boot
let _draw_mode = ref Lucky.StepInside
let draw_mode () = !_draw_mode
......@@ -209,12 +210,6 @@ let mkoptab () = (
(Arg.Unit(function _ -> _gen_mode := GenLuc ; ()))
["generate a lucky program"]
;
mkopt
["-rif"]
~arg:" <string>"
(Arg.String(function s -> _riffile := Some s))
["save the generated data in a file using the RIF format"]
;
mkopt
["-o"]
~arg:" <string>"
......@@ -226,6 +221,15 @@ let mkoptab () = (
(Arg.Unit(function s -> _outpipe := true))
["with -luc: write to stdout"]
;
mkopt
["-rif"]
~arg:" <string>"
(Arg.String(function s ->
_riffile := Some s;
Luc2c.option.Luc2c.rif <- Some s
))
["save the generated data in a file using the RIF format"]
;
mkopt
["-L"; "-lib"]
......@@ -237,7 +241,6 @@ let mkoptab () = (
(* simu *)
mkopt
["-b";"-boot"]
~arg:" <int>"
(Arg.Set _boot)
["perform a first step without requiring inputs";
("fails if inputs are actually required at first step (default: "
......@@ -305,8 +308,7 @@ let mkoptab () = (
(Arg.String(function str ->
_gen_mode := Cstubs;
Luc2c.option.Luc2c.use_sockets <- true;
Luc2c.option.Luc2c.sock_addr <- str;
Luc2c.option.Luc2c.gen_mode <- Luc2c.Nop
Luc2c.option.Luc2c.sock_addr <- str
))
["the same as --2c-4c, but using lutin via sockets."]
;
......@@ -345,6 +347,12 @@ let mkoptab () = (
Luc2c.option.Luc2c.calling_module_name <- str))
["generate C and C++ code to be called from Alice"];
mkopt
["--2c-output-dir"]
~arg:" <string>"
(Arg.String(fun str -> Luc2c.option.Luc2c.output_dir <- str))
["Directory where C files are generated"];
(* to show Hidden opt *)
mkopt
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment