From 215b602383247b2993608d9a7cc048cdf841c9aa Mon Sep 17 00:00:00 2001 From: Tom Barthe Date: Wed, 30 Dec 2020 01:10:45 +0100 Subject: [PATCH] Add a simple_simul compiler option --- compiler/obc/c/cmain.ml | 94 +++++++++++-------- compiler/utilities/global/compiler_options.ml | 2 + 2 files changed, 59 insertions(+), 37 deletions(-) diff --git a/compiler/obc/c/cmain.ml b/compiler/obc/c/cmain.ml index 828ea2b..c5a7cc3 100644 --- a/compiler/obc/c/cmain.ml +++ b/compiler/obc/c/cmain.ml @@ -295,10 +295,13 @@ let main_def_of_class_def cd = @ [Csexpr funcall] @ printf_calls @ - (if !Compiler_options.hepts_simulation + (if !Compiler_options.hepts_simulation || !Compiler_options.simple_simul then [] else [Csexpr (Cfun_call ("puts", [Cconst (Cstrlit "")]))]) - @ [Csexpr (Cfun_call ("fflush", [Cvar "stdout"]))] in + @ (if !Compiler_options.simple_simul + then [] + else [Csexpr (Cfun_call ("fflush", [Cvar "stdout"]))]) + in (* Do not forget to initialize memory via reset if needed. *) let rst_i = @@ -312,45 +315,62 @@ let main_def_of_class_def cd = (** [main_skel var_list prologue body] generates a C main() function using the variable list [var_list], prologue [prologue] and loop body [body]. *) let main_skel var_list prologue body = + let args = if !Compiler_options.simple_simul then + [] + else + [("argc", Cty_int); ("argv", Cty_ptr (Cty_ptr Cty_char))] + in + let cvars = if !Compiler_options.simple_simul then + var_list + else + (step_counter, Cty_int) :: (max_step, Cty_int) :: var_list + in + let body_block = if !Compiler_options.simple_simul then + prologue + @ [ + Cwhile (mk_int 1, body); + Creturn (mk_int 0) + ] + else + [ + (* + step_count = 0; + max_step = 0; + if (argc == 2) + max_step = atoi(argv[1]); + *) + Caffect (CLvar step_counter, mk_int 0); + Caffect (CLvar max_step, mk_int 0); + Cif (Cbop ("==", Cvar "argc", mk_int 2), + [Caffect (CLvar max_step, + Cfun_call ("atoi", + [Carray (Cvar "argv", + mk_int 1)]))], []); + ] + @ prologue + (* while (!max_step || step_c < max_step) *) + @ [ + Cwhile (Cbop ("||", + Cuop ("!", Cvar max_step), + Cbop ("<", + Cvar step_counter, + Cvar max_step)), + (* step_counter = step_counter + 1; *) + Caffect (CLvar step_counter, + Cbop ("+", + Cvar step_counter, + mk_int 1)) + :: body); + Creturn (mk_int 0) + ] + in Cfundef { C.f_name = "main"; f_retty = Cty_int; - f_args = [("argc", Cty_int); ("argv", Cty_ptr (Cty_ptr Cty_char))]; + f_args = args; f_body = { - var_decls = vardecl_of_cvars - ((step_counter, Cty_int) :: (max_step, Cty_int) :: var_list); - block_body = - [ - (* - step_count = 0; - max_step = 0; - if (argc == 2) - max_step = atoi(argv[1]); - *) - Caffect (CLvar step_counter, mk_int 0); - Caffect (CLvar max_step, mk_int 0); - Cif (Cbop ("==", Cvar "argc", mk_int 2), - [Caffect (CLvar max_step, - Cfun_call ("atoi", - [Carray (Cvar "argv", - mk_int 1)]))], []); - ] - @ prologue - (* while (!max_step || step_c < max_step) *) - @ [ - Cwhile (Cbop ("||", - Cuop ("!", Cvar max_step), - Cbop ("<", - Cvar step_counter, - Cvar max_step)), - (* step_counter = step_counter + 1; *) - Caffect (CLvar step_counter, - Cbop ("+", - Cvar step_counter, - mk_int 1)) - :: body); - Creturn (mk_int 0); - ]; + var_decls = vardecl_of_cvars cvars; + block_body = body_block } } diff --git a/compiler/utilities/global/compiler_options.ml b/compiler/utilities/global/compiler_options.ml index a6801fa..46c052e 100644 --- a/compiler/utilities/global/compiler_options.ml +++ b/compiler/utilities/global/compiler_options.ml @@ -84,6 +84,8 @@ let hepts_simulation = ref false let create_object_file = ref false +let simple_simul = ref true + let boolean = ref false let nosink = ref false