Add reset support in C

This commit is contained in:
Benjamin Loison 2022-12-15 19:51:46 +01:00
parent 342aba4426
commit 1a06fc9a6a

View File

@ -82,6 +82,8 @@ let rec pp_asnprevarlist node_name fmt : t_varlist -> unit = function
Format.fprintf fmt "\tpre_%s_%s = %s;\n%a" node_name h h (pp_asnprevarlist node_name) (tl, h' :: l) Format.fprintf fmt "\tpre_%s_%s = %s;\n%a" node_name h h (pp_asnprevarlist node_name) (tl, h' :: l)
| _ -> raise (MyTypeError "This exception should not have beed be raised.") | _ -> raise (MyTypeError "This exception should not have beed be raised.")
let reset_expressions_counter = ref 0;;
let pp_expression node_name = let pp_expression node_name =
let rec pp_expression_aux fmt expression = let rec pp_expression_aux fmt expression =
let rec pp_expression_list fmt exprs = let rec pp_expression_list fmt exprs =
@ -101,12 +103,25 @@ let pp_expression node_name =
Format.fprintf fmt "%a" Format.fprintf fmt "%a"
pp_expression_aux e1 pp_expression_aux e1
end end
(* TODO: *)
| EReset (_, e1, e2) -> | EReset (_, e1, e2) ->
begin begin
Format.fprintf fmt "\t\t\tRESET\n%a\t\t\tRESET\n%a" incr reset_expressions_counter;
(* Use following trick as we can't use `;`:
if(((var = val) && false) || condition)
is equivalent to an incorrect statement like
if({var = val; condition})
We also use this trick with the fact that `0` can be interpreted as a `bool`, an `int` and a `float` *)
(* could use C macros to simplify the C code *)
Format.fprintf fmt "(((tmp_reset[%i] = %a) && false) || init_%s) ? (((init[%i] = tmp_reset[%i]) || true) ? tmp_reset[%i] : 0) : (%a ? init[%i] : tmp_reset[%i])"
(!reset_expressions_counter - 1)
pp_expression_aux e1 pp_expression_aux e1
node_name
(!reset_expressions_counter - 1)
(!reset_expressions_counter - 1)
(!reset_expressions_counter - 1)
pp_expression_aux e2 pp_expression_aux e2
(!reset_expressions_counter - 1)
(!reset_expressions_counter - 1)
end end
| EConst (_, c) -> | EConst (_, c) ->
begin match c with begin match c with
@ -182,15 +197,27 @@ let rec pp_equations node_name fmt: t_eqlist -> unit = function
(pp_expression node_name) expr (pp_expression node_name) expr
(pp_equations node_name) eqs (pp_equations node_name) eqs
let pp_resvars reset_expressions_counter =
(* use the fact that any boolean and any integer can be encoded as a float *)
Format.sprintf "float tmp_reset[%i], init[%i];" reset_expressions_counter reset_expressions_counter
(* TODO: manage general outputs *) (* TODO: manage general outputs *)
let pp_node fmt node = let pp_node fmt node =
(* undefined behavior if the initial code uses a variable with name `init_{NODE_NAME}` or `pre_{NODE_MAIN}_{VARIABLE}` *) (* undefined behavior if the initial code uses a variable with name:
Format.fprintf fmt "bool init_%s = true;\n\n%a\n\n%a\n\n%a\n\n%a %s(%a)\n{\n\t%a\n\n\t%a\n\n%a\n\tinit_%s = false;\n\n%a\n\n%a\n\n%a\n\n\treturn %a;\n}\n" - `init_{NODE_NAME}`
- `tmp_reset_{int}`
- `init_{int}`
- `pre_{NODE_MAIN}_{VARIABLE}` *)
reset_expressions_counter := 0;
let _ = (pp_equations node.n_name) Format.str_formatter node.n_equations in
reset_expressions_counter := 0;
Format.fprintf fmt "bool init_%s = true;\n\n%a\n\n%a\n\n%a\n\n%s\n\n%a %s(%a)\n{\n\t%a\n\n\t%a\n\n%a\n\tinit_%s = false;\n\n%a\n\n%a\n\n%a\n\n\treturn %a;\n}\n"
node.n_name node.n_name
(* could avoid declaring unused variables *) (* could avoid declaring unused variables *)
(pp_prevarlist node.n_name) node.n_inputs (pp_prevarlist node.n_name) node.n_inputs
(pp_prevarlist node.n_name) node.n_local_vars (pp_prevarlist node.n_name) node.n_local_vars
(pp_prevarlist node.n_name) node.n_outputs (pp_prevarlist node.n_name) node.n_outputs
(pp_resvars !reset_expressions_counter)
pp_retvarlist node.n_outputs pp_retvarlist node.n_outputs
node.n_name node.n_name
(* could avoid newlines if they aren't used to seperate statements *) (* could avoid newlines if they aren't used to seperate statements *)