1 |
589ccf9f
|
Corentin Lauverjat
|
open Format
|
2 |
|
|
|
3 |
|
|
open Lustrec.Machine_code_types
|
4 |
|
|
open Lustrec.Lustre_types
|
5 |
|
|
open Lustrec.Corelang
|
6 |
|
|
open Lustrec.Machine_code_common
|
7 |
|
|
|
8 |
|
|
open Ada_printer
|
9 |
|
|
open Misc_printer
|
10 |
|
|
open Misc_lustre_function
|
11 |
|
|
|
12 |
|
|
(** Exception for unsupported features in Ada backend **)
|
13 |
|
|
exception Ada_not_supported of string
|
14 |
|
|
|
15 |
|
|
(** Print the name of the state variable.
|
16 |
|
|
@param fmt the formater to print on
|
17 |
|
|
**)
|
18 |
|
|
let pp_state_name fmt = fprintf fmt "state"
|
19 |
|
|
(** Print the type of the state variable.
|
20 |
|
|
@param fmt the formater to print on
|
21 |
|
|
**)
|
22 |
|
|
let pp_state_type fmt = fprintf fmt "TState"
|
23 |
|
|
(** Print the name of the reset procedure
|
24 |
|
|
@param fmt the formater to print on
|
25 |
|
|
**)
|
26 |
|
|
let pp_reset_procedure_name fmt = fprintf fmt "reset"
|
27 |
|
|
(** Print the name of the step procedure
|
28 |
|
|
@param fmt the formater to print on
|
29 |
|
|
**)
|
30 |
|
|
let pp_step_procedure_name fmt = fprintf fmt "step"
|
31 |
|
|
(** Print the name of the main procedure.
|
32 |
|
|
@param fmt the formater to print on
|
33 |
|
|
**)
|
34 |
|
|
let pp_main_procedure_name fmt = fprintf fmt "ada_main"
|
35 |
|
|
(** Print the name of the arrow package.
|
36 |
|
|
@param fmt the formater to print on
|
37 |
|
|
**)
|
38 |
|
|
let pp_arrow_package_name fmt = fprintf fmt "Lustrec.Arrow"
|
39 |
|
|
(** Print the type of a polymorphic type.
|
40 |
|
|
@param fmt the formater to print on
|
41 |
|
|
@param id the id of the polymorphic type
|
42 |
|
|
**)
|
43 |
|
|
let pp_polymorphic_type id fmt = fprintf fmt "T_%i" id
|
44 |
|
|
|
45 |
|
|
let pp_past_name nbr fmt = fprintf fmt "past_state_%i" nbr
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
(*TODO Check all this function with unit test, improve this system and
|
56 |
|
|
add support for : "cbrt", "erf", "log10", "pow", "atan2".
|
57 |
|
|
*)
|
58 |
|
|
let ada_supported_funs =
|
59 |
|
|
[("sqrt", ("Ada.Numerics.Elementary_Functions", "Sqrt"));
|
60 |
|
|
("log", ("Ada.Numerics.Elementary_Functions", "Log"));
|
61 |
|
|
("exp", ("Ada.Numerics.Elementary_Functions", "Exp"));
|
62 |
|
|
("pow", ("Ada.Numerics.Elementary_Functions", "**"));
|
63 |
|
|
("sin", ("Ada.Numerics.Elementary_Functions", "Sin"));
|
64 |
|
|
("cos", ("Ada.Numerics.Elementary_Functions", "Cos"));
|
65 |
|
|
("tan", ("Ada.Numerics.Elementary_Functions", "Tan"));
|
66 |
|
|
("asin", ("Ada.Numerics.Elementary_Functions", "Arcsin"));
|
67 |
|
|
("acos", ("Ada.Numerics.Elementary_Functions", "Arccos"));
|
68 |
|
|
("atan", ("Ada.Numerics.Elementary_Functions", "Arctan"));
|
69 |
|
|
("sinh", ("Ada.Numerics.Elementary_Functions", "Sinh"));
|
70 |
|
|
("cosh", ("Ada.Numerics.Elementary_Functions", "Cosh"));
|
71 |
|
|
("tanh", ("Ada.Numerics.Elementary_Functions", "Tanh"));
|
72 |
|
|
("asinh", ("Ada.Numerics.Elementary_Functions", "Arcsinh"));
|
73 |
|
|
("acosh", ("Ada.Numerics.Elementary_Functions", "Arccosh"));
|
74 |
|
|
("atanh", ("Ada.Numerics.Elementary_Functions", "Arctanh"));
|
75 |
|
|
|
76 |
|
|
("ceil", ("", "Float'Ceiling"));
|
77 |
|
|
("floor", ("", "Float'Floor"));
|
78 |
|
|
("fmod", ("", "Float'Remainder"));
|
79 |
|
|
("round", ("", "Float'Rounding"));
|
80 |
|
|
("trunc", ("", "Float'Truncation"));
|
81 |
|
|
|
82 |
|
|
("fabs", ("", "abs"));]
|
83 |
|
|
|
84 |
|
|
let is_builtin_fun ident =
|
85 |
|
|
List.mem ident Lustrec.Basic_library.internal_funs ||
|
86 |
|
|
List.mem_assoc ident ada_supported_funs
|
87 |
|
|
|
88 |
|
|
(** Print the name of a package associated to a machine.
|
89 |
|
|
@param fmt the formater to print on
|
90 |
|
|
@param machine the machine
|
91 |
|
|
**)
|
92 |
|
|
let pp_package_name machine fmt =
|
93 |
|
|
if is_arrow machine then
|
94 |
|
|
fprintf fmt "%t" pp_arrow_package_name
|
95 |
|
|
else
|
96 |
|
|
fprintf fmt "%a" pp_clean_ada_identifier machine.mname.node_id
|
97 |
|
|
|
98 |
|
|
(** Print a type.
|
99 |
|
|
@param fmt the formater to print on
|
100 |
|
|
@param type the type
|
101 |
|
|
**)
|
102 |
|
|
let pp_type fmt typ =
|
103 |
|
|
(match (Lustrec.Types.repr typ).Lustrec.Types.tdesc with
|
104 |
|
|
| Lustrec.Types.Tbasic Lustrec.Types.Basic.Tint -> pp_integer_type fmt
|
105 |
|
|
| Lustrec.Types.Tbasic Lustrec.Types.Basic.Treal -> pp_float_type fmt
|
106 |
|
|
| Lustrec.Types.Tbasic Lustrec.Types.Basic.Tbool -> pp_boolean_type fmt
|
107 |
|
|
| Lustrec.Types.Tunivar -> pp_polymorphic_type typ.Lustrec.Types.tid fmt
|
108 |
|
|
| Lustrec.Types.Tbasic _ -> eprintf "Tbasic@."; assert false (*TODO*)
|
109 |
|
|
| Lustrec.Types.Tconst _ -> eprintf "Tconst@."; assert false (*TODO*)
|
110 |
|
|
| Lustrec.Types.Tclock _ -> eprintf "Tclock@."; assert false (*TODO*)
|
111 |
|
|
| Lustrec.Types.Tarrow _ -> eprintf "Tarrow@."; assert false (*TODO*)
|
112 |
|
|
| Lustrec.Types.Ttuple l -> eprintf "Ttuple %a @." (Lustrec.Utils.fprintf_list ~sep:" " Lustrec.Types.print_ty) l; assert false (*TODO*)
|
113 |
|
|
| Lustrec.Types.Tenum _ -> eprintf "Tenum@."; assert false (*TODO*)
|
114 |
|
|
| Lustrec.Types.Tstruct _ -> eprintf "Tstruct@.";assert false (*TODO*)
|
115 |
|
|
| Lustrec.Types.Tarray _ -> eprintf "Tarray@."; assert false (*TODO*)
|
116 |
|
|
| Lustrec.Types.Tstatic _ -> eprintf "Tstatic@.";assert false (*TODO*)
|
117 |
|
|
| Lustrec.Types.Tlink _ -> eprintf "Tlink@."; assert false (*TODO*)
|
118 |
|
|
| Lustrec.Types.Tvar -> eprintf "Tvar@."; assert false (*TODO*)
|
119 |
|
|
(*| _ -> eprintf "Type error : %a@." Lustrec.Types.print_ty typ; assert false *)
|
120 |
|
|
)
|
121 |
|
|
|
122 |
|
|
(** Return a default ada constant for a given type.
|
123 |
|
|
@param cst_typ the constant type
|
124 |
|
|
**)
|
125 |
|
|
let default_ada_cst cst_typ = match cst_typ with
|
126 |
|
|
| Lustrec.Types.Basic.Tint -> Const_int 0
|
127 |
|
|
| Lustrec.Types.Basic.Treal -> Const_real Lustrec.Real.zero
|
128 |
|
|
| Lustrec.Types.Basic.Tbool -> Const_tag tag_false
|
129 |
|
|
| _ -> assert false
|
130 |
|
|
|
131 |
|
|
(** Make a default value from a given type.
|
132 |
|
|
@param typ the type
|
133 |
|
|
**)
|
134 |
|
|
let mk_default_value typ =
|
135 |
|
|
match (Lustrec.Types.repr typ).Lustrec.Types.tdesc with
|
136 |
|
|
| Lustrec.Types.Tbasic t -> mk_val (Cst (default_ada_cst t)) typ
|
137 |
|
|
| _ -> assert false (*TODO*)
|
138 |
|
|
|
139 |
|
|
(** Print the type of a variable.
|
140 |
|
|
@param fmt the formater to print on
|
141 |
|
|
@param id the variable
|
142 |
|
|
**)
|
143 |
|
|
let pp_var_type fmt id =
|
144 |
|
|
pp_type fmt id.var_type
|
145 |
|
|
|
146 |
|
|
(** Print a package name with polymorphic types specified.
|
147 |
|
|
@param substitution correspondance between polymorphic type id and their instantiation
|
148 |
|
|
@param fmt the formater to print on
|
149 |
|
|
@param machine the machine
|
150 |
|
|
**)
|
151 |
|
|
let pp_package_name_with_polymorphic substitution machine fmt =
|
152 |
|
|
let polymorphic_types = find_all_polymorphic_type machine in
|
153 |
|
|
assert(List.length polymorphic_types = List.length substitution);
|
154 |
|
|
let substituion = List.sort_uniq (fun x y -> fst x - fst y) substitution in
|
155 |
|
|
assert(List.for_all2 (fun poly1 (poly2, _) -> poly1 = poly2)
|
156 |
|
|
polymorphic_types substituion);
|
157 |
|
|
let instantiated_types = snd (List.split substitution) in
|
158 |
|
|
fprintf fmt "%t%t%a"
|
159 |
|
|
(pp_package_name machine)
|
160 |
|
|
(Lustrec.Utils.pp_final_char_if_non_empty "_" instantiated_types)
|
161 |
|
|
(Lustrec.Utils.fprintf_list ~sep:"_" pp_type) instantiated_types
|
162 |
|
|
|
163 |
|
|
(** Print the name of a variable.
|
164 |
|
|
@param fmt the formater to print on
|
165 |
|
|
@param id the variable
|
166 |
|
|
**)
|
167 |
|
|
let pp_var_name id fmt =
|
168 |
|
|
fprintf fmt "%a" pp_clean_ada_identifier id.var_id
|
169 |
|
|
|
170 |
|
|
(** Print the complete name of variable.
|
171 |
|
|
@param m the machine to check if it is memory
|
172 |
|
|
@param fmt the formater to print on
|
173 |
|
|
@param var the variable
|
174 |
|
|
**)
|
175 |
|
|
let pp_var env fmt var =
|
176 |
|
|
match List.assoc_opt var.var_id env with
|
177 |
|
|
| None -> pp_var_name var fmt
|
178 |
|
|
| Some pp_state -> pp_access pp_state (pp_var_name var) fmt
|
179 |
|
|
|
180 |
|
|
(* Expression print functions *)
|
181 |
|
|
|
182 |
|
|
(* Printing functions for basic operations and expressions *)
|
183 |
|
|
(* TODO: refactor code -> use let rec and for basic pretty printing
|
184 |
|
|
function *)
|
185 |
|
|
(** Printing function for Ada tags, mainly booleans.
|
186 |
|
|
|
187 |
|
|
@param fmt the formater to use
|
188 |
|
|
@param t the tag to print
|
189 |
|
|
**)
|
190 |
|
|
let pp_ada_tag fmt t =
|
191 |
|
|
pp_print_string fmt
|
192 |
|
|
(if t = tag_true then "True" else if t = tag_false then "False" else t)
|
193 |
|
|
|
194 |
|
|
(** Printing function for machine type constants. For the moment,
|
195 |
|
|
arrays are not supported.
|
196 |
|
|
|
197 |
|
|
@param fmt the formater to use
|
198 |
|
|
@param c the constant to print
|
199 |
|
|
**)
|
200 |
|
|
let pp_ada_const fmt c =
|
201 |
|
|
match c with
|
202 |
|
|
| Const_int i -> pp_print_int fmt i
|
203 |
|
|
| Const_real r -> Lustrec.Real.pp_ada fmt r
|
204 |
|
|
| Const_tag t -> pp_ada_tag fmt t
|
205 |
|
|
| Const_string _ | Const_modeid _ ->
|
206 |
|
|
(Format.eprintf
|
207 |
|
|
"internal error: Ada_backend_adb.pp_ada_const cannot print string or modeid.";
|
208 |
|
|
assert false)
|
209 |
|
|
| _ ->
|
210 |
|
|
raise (Ada_not_supported "unsupported: Ada_backend_adb.pp_ada_const does not
|
211 |
|
|
support this constant")
|
212 |
|
|
|
213 |
|
|
(** Printing function for expressions [v1 modulo v2]. Depends
|
214 |
|
|
on option [integer_div_euclidean] to choose between mathematical
|
215 |
|
|
modulo or remainder ([rem] in Ada).
|
216 |
|
|
|
217 |
|
|
@param pp_value pretty printer for values
|
218 |
|
|
@param v1 the first value in the expression
|
219 |
|
|
@param v2 the second value in the expression
|
220 |
|
|
@param fmt the formater to print on
|
221 |
|
|
**)
|
222 |
|
|
let pp_mod pp_value v1 v2 fmt =
|
223 |
|
|
if !Lustrec.Options.integer_div_euclidean then
|
224 |
|
|
(* (a rem b) + (a rem b < 0 ? abs(b) : 0) *)
|
225 |
|
|
Format.fprintf fmt
|
226 |
|
|
"((%a rem %a) + (if (%a rem %a) < 0 then abs(%a) else 0))"
|
227 |
|
|
pp_value v1 pp_value v2
|
228 |
|
|
pp_value v1 pp_value v2
|
229 |
|
|
pp_value v2
|
230 |
|
|
else (* Ada behavior for rem *)
|
231 |
|
|
Format.fprintf fmt "(%a rem %a)" pp_value v1 pp_value v2
|
232 |
|
|
|
233 |
|
|
(** Printing function for expressions [v1 div v2]. Depends on
|
234 |
|
|
option [integer_div_euclidean] to choose between mathematic
|
235 |
|
|
division or Ada division.
|
236 |
|
|
|
237 |
|
|
@param pp_value pretty printer for values
|
238 |
|
|
@param v1 the first value in the expression
|
239 |
|
|
@param v2 the second value in the expression
|
240 |
|
|
@param fmt the formater to print in
|
241 |
|
|
**)
|
242 |
|
|
let pp_div pp_value v1 v2 fmt =
|
243 |
|
|
if !Lustrec.Options.integer_div_euclidean then
|
244 |
|
|
(* (a - ((a rem b) + (if a rem b < 0 then abs (b) else 0))) / b) *)
|
245 |
|
|
Format.fprintf fmt "(%a - %t) / %a"
|
246 |
|
|
pp_value v1
|
247 |
|
|
(pp_mod pp_value v1 v2)
|
248 |
|
|
pp_value v2
|
249 |
|
|
else (* Ada behavior for / *)
|
250 |
|
|
Format.fprintf fmt "(%a / %a)" pp_value v1 pp_value v2
|
251 |
|
|
|
252 |
|
|
(** Printing function for basic lib functions.
|
253 |
|
|
|
254 |
|
|
@param pp_value pretty printer for values
|
255 |
|
|
@param i a string representing the function
|
256 |
|
|
@param fmt the formater to print on
|
257 |
|
|
@param vl the list of operands
|
258 |
|
|
**)
|
259 |
|
|
let pp_basic_lib_fun pp_value ident fmt vl =
|
260 |
|
|
match ident, vl with
|
261 |
|
|
| "uminus", [v] ->
|
262 |
|
|
Format.fprintf fmt "(- %a)" pp_value v
|
263 |
|
|
| "not", [v] ->
|
264 |
|
|
Format.fprintf fmt "(not %a)" pp_value v
|
265 |
|
|
| "impl", [v1; v2] ->
|
266 |
|
|
Format.fprintf fmt "(not %a or else %a)" pp_value v1 pp_value v2
|
267 |
|
|
| "=", [v1; v2] ->
|
268 |
|
|
Format.fprintf fmt "(%a = %a)" pp_value v1 pp_value v2
|
269 |
|
|
| "mod", [v1; v2] -> pp_mod pp_value v1 v2 fmt
|
270 |
|
|
| "equi", [v1; v2] ->
|
271 |
|
|
Format.fprintf fmt "((not %a) = (not %a))" pp_value v1 pp_value v2
|
272 |
|
|
| "xor", [v1; v2] ->
|
273 |
|
|
Format.fprintf fmt "((not %a) /= (not %a))" pp_value v1 pp_value v2
|
274 |
|
|
| "/", [v1; v2] -> pp_div pp_value v1 v2 fmt
|
275 |
|
|
| "&&", [v1; v2] ->
|
276 |
|
|
Format.fprintf fmt "(%a %s %a)" pp_value v1 "and then" pp_value v2
|
277 |
|
|
| "||", [v1; v2] ->
|
278 |
|
|
Format.fprintf fmt "(%a %s %a)" pp_value v1 "or else" pp_value v2
|
279 |
|
|
| "!=", [v1; v2] ->
|
280 |
|
|
Format.fprintf fmt "(%a %s %a)" pp_value v1 "/=" pp_value v2
|
281 |
|
|
| "ite", [v1; v2; v3] ->
|
282 |
|
|
Format.fprintf fmt "(if %a then %a else %a)" pp_value v1 pp_value v2 pp_value v3
|
283 |
|
|
| op, [v1; v2] ->
|
284 |
|
|
Format.fprintf fmt "(%a %s %a)" pp_value v1 op pp_value v2
|
285 |
|
|
| op, [v1] when List.mem_assoc ident ada_supported_funs ->
|
286 |
|
|
let pkg, name = try List.assoc ident ada_supported_funs
|
287 |
|
|
with Not_found -> assert false in
|
288 |
|
|
let pkg = pkg^(if String.equal pkg "" then "" else ".") in
|
289 |
|
|
Format.fprintf fmt "%s%s(%a)" pkg name pp_value v1
|
290 |
|
|
| fun_name, _ ->
|
291 |
|
|
(Format.eprintf "internal compilation error: basic function %s@." fun_name; assert false)
|
292 |
|
|
|
293 |
|
|
(** Printing function for values.
|
294 |
|
|
|
295 |
|
|
@param m the machine to know the state variable
|
296 |
|
|
@param fmt the formater to use
|
297 |
|
|
@param value the value to print. Should be a
|
298 |
|
|
{!type:Lustrec.Machine_code_types.value_t} value
|
299 |
|
|
**)
|
300 |
|
|
let rec pp_value env fmt value =
|
301 |
|
|
match value.value_desc with
|
302 |
|
|
| Cst c -> pp_ada_const fmt c
|
303 |
|
|
| Var var -> pp_var env fmt var (* Find better to test if it is memory or not *)
|
304 |
|
|
| Fun (f_ident, vl) -> pp_basic_lib_fun (pp_value env) f_ident fmt vl
|
305 |
|
|
| _ ->
|
306 |
|
|
raise (Ada_not_supported
|
307 |
|
|
"unsupported: Ada_backend.adb.pp_value does not support this value type")
|
308 |
|
|
|
309 |
|
|
|
310 |
|
|
(** Print the filename of a machine package.
|
311 |
|
|
@param extension the extension to append to the package name
|
312 |
|
|
@param fmt the formatter
|
313 |
|
|
@param machine the machine corresponding to the package
|
314 |
|
|
**)
|
315 |
|
|
let pp_machine_filename extension fmt machine =
|
316 |
|
|
pp_filename extension fmt (pp_package_name machine)
|
317 |
|
|
|
318 |
|
|
let pp_main_filename fmt _ = pp_filename "adb" fmt pp_main_procedure_name
|
319 |
|
|
|
320 |
|
|
|
321 |
|
|
(** Print the declaration of a state element of a subinstance of a machine.
|
322 |
|
|
@param machine the machine
|
323 |
|
|
@param fmt the formater to print on
|
324 |
|
|
@param substitution correspondance between polymorphic type id and their instantiation
|
325 |
|
|
@param ident the identifier of the subinstance
|
326 |
|
|
@param submachine the submachine of the subinstance
|
327 |
|
|
**)
|
328 |
|
|
let build_pp_state_decl_from_subinstance mode with_statement (name, (substitution, machine)) =
|
329 |
|
|
let pp_package = pp_package_name_with_polymorphic substitution machine in
|
330 |
|
|
let pp_type = pp_package_access (pp_package, pp_state_type) in
|
331 |
|
|
let pp_name fmt = pp_clean_ada_identifier fmt name in
|
332 |
|
|
(mode, pp_name, pp_type, with_statement)
|
333 |
|
|
|
334 |
|
|
(** Print variable declaration for a local state variable
|
335 |
|
|
@param fmt the formater to print on
|
336 |
|
|
@param mode input/output mode of the parameter
|
337 |
|
|
**)
|
338 |
|
|
let build_pp_state_decl mode with_statement =
|
339 |
|
|
(mode, pp_state_name, pp_state_type, with_statement)
|
340 |
|
|
|
341 |
|
|
let build_pp_var_decl mode with_statement var =
|
342 |
|
|
let pp_name = function fmt -> pp_var_name var fmt in
|
343 |
|
|
let pp_type = function fmt -> pp_var_type fmt var in
|
344 |
|
|
(mode, pp_name, pp_type, with_statement)
|
345 |
|
|
|
346 |
|
|
let build_pp_var_decl_local with_statement var =
|
347 |
|
|
AdaLocalVar (build_pp_var_decl AdaNoMode with_statement var)
|
348 |
|
|
|
349 |
|
|
let build_pp_var_decl_step_input mode with_statement m =
|
350 |
|
|
if m.mstep.step_inputs=[] then [] else
|
351 |
|
|
[List.map (build_pp_var_decl mode with_statement) m.mstep.step_inputs]
|
352 |
|
|
|
353 |
|
|
let build_pp_var_decl_step_output mode with_statement m =
|
354 |
|
|
if m.mstep.step_outputs=[] then [] else
|
355 |
|
|
[List.map (build_pp_var_decl mode with_statement) m.mstep.step_outputs]
|
356 |
|
|
|
357 |
|
|
let build_pp_var_decl_static mode with_statement m =
|
358 |
|
|
if m.mstatic=[] then [] else
|
359 |
|
|
[List.map (build_pp_var_decl mode with_statement) m.mstatic]
|
360 |
|
|
|
361 |
|
|
let build_pp_arg_step m =
|
362 |
|
|
(if is_machine_statefull m then [[build_pp_state_decl AdaInOut None]] else [])
|
363 |
|
|
@ (build_pp_var_decl_step_input AdaIn None m)
|
364 |
|
|
@ (build_pp_var_decl_step_output AdaOut None m)
|
365 |
|
|
|
366 |
|
|
let build_pp_arg_reset m =
|
367 |
|
|
(if is_machine_statefull m then [[build_pp_state_decl AdaOut None]] else [])
|
368 |
|
|
@ (build_pp_var_decl_static AdaIn None m)
|
369 |
|
|
|
370 |
|
|
|
371 |
|
|
let build_pp_arg_transition m =
|
372 |
|
|
(if is_machine_statefull m then [[build_pp_state_decl AdaInOut None]] else [])
|
373 |
|
|
@ (build_pp_var_decl_step_input AdaIn None m)
|
374 |
|
|
@ (build_pp_var_decl_step_output AdaOut None m)
|