1
|
(********************************************************************)
|
2
|
(* *)
|
3
|
(* The LustreC compiler toolset / The LustreC Development Team *)
|
4
|
(* Copyright 2012 - -- ONERA - CNRS - INPT *)
|
5
|
(* *)
|
6
|
(* LustreC is free software, distributed WITHOUT ANY WARRANTY *)
|
7
|
(* under the terms of the GNU Lesser General Public License *)
|
8
|
(* version 2.1. *)
|
9
|
(* *)
|
10
|
(********************************************************************)
|
11
|
|
12
|
open Lustre_types
|
13
|
open Format
|
14
|
open Utils
|
15
|
|
16
|
(* Prints [v] as [pp_fun] would do, but adds a backslash at each end of line,
|
17
|
following the C convention for multiple lines macro *)
|
18
|
let pp_as_c_macro pp_fun fmt v =
|
19
|
let formatter_out_funs = pp_get_formatter_out_functions fmt () in
|
20
|
let macro_newline () =
|
21
|
begin
|
22
|
formatter_out_funs.out_string "\\" 0 1;
|
23
|
formatter_out_funs.out_newline ()
|
24
|
end in
|
25
|
begin
|
26
|
pp_set_formatter_out_functions fmt { formatter_out_funs with out_newline = macro_newline };
|
27
|
pp_fun fmt v;
|
28
|
pp_set_formatter_out_functions fmt formatter_out_funs;
|
29
|
end
|
30
|
|
31
|
let rec pp_var_struct_type_field fmt (label, tdesc) =
|
32
|
fprintf fmt "%a : %a;" pp_print_string label pp_var_type_dec_desc tdesc
|
33
|
and pp_var_type_dec_desc fmt tdesc =
|
34
|
match tdesc with
|
35
|
| Tydec_any -> fprintf fmt "<any>"
|
36
|
| Tydec_int -> fprintf fmt "int"
|
37
|
| Tydec_real -> fprintf fmt "real"
|
38
|
(* | Tydec_float -> fprintf fmt "float" *)
|
39
|
| Tydec_bool -> fprintf fmt "bool"
|
40
|
| Tydec_clock t -> fprintf fmt "%a clock" pp_var_type_dec_desc t
|
41
|
| Tydec_const t -> fprintf fmt "%s" t
|
42
|
| Tydec_enum id_list -> fprintf fmt "enum {%a }" (fprintf_list ~sep:", " pp_print_string) id_list
|
43
|
| Tydec_struct f_list -> fprintf fmt "struct {%a }" (fprintf_list ~sep:" " pp_var_struct_type_field) f_list
|
44
|
| Tydec_array (s, t) -> fprintf fmt "%a^%a" pp_var_type_dec_desc t Dimension.pp_dimension s
|
45
|
|
46
|
let pp_var_type_dec fmt ty =
|
47
|
pp_var_type_dec_desc fmt ty.ty_dec_desc
|
48
|
|
49
|
let pp_var_name fmt id = fprintf fmt "%s" id.var_id
|
50
|
let pp_var_type fmt id =
|
51
|
if !Options.print_dec_types then
|
52
|
pp_var_type_dec fmt id.var_dec_type
|
53
|
else
|
54
|
Types.print_node_ty fmt id.var_type
|
55
|
let pp_var_clock fmt id = Clocks.print_ck_suffix fmt id.var_clock
|
56
|
|
57
|
let pp_eq_lhs = fprintf_list ~sep:", " pp_print_string
|
58
|
|
59
|
let pp_var fmt id =
|
60
|
fprintf fmt "%s%s: %a"
|
61
|
(if id.var_dec_const then "const " else "")
|
62
|
id.var_id
|
63
|
pp_var_type id
|
64
|
|
65
|
let pp_vars fmt vars =
|
66
|
fprintf_list ~sep:"; " pp_var fmt vars
|
67
|
|
68
|
let pp_quantifiers fmt (q, vars) =
|
69
|
match q with
|
70
|
| Forall -> fprintf fmt "forall %a" pp_vars vars
|
71
|
| Exists -> fprintf fmt "exists %a" pp_vars vars
|
72
|
|
73
|
let rec pp_struct_const_field fmt (label, c) =
|
74
|
fprintf fmt "%a = %a;" pp_print_string label pp_const c
|
75
|
and pp_const fmt c =
|
76
|
match c with
|
77
|
| Const_int i -> pp_print_int fmt i
|
78
|
| Const_real (c, e, s) -> pp_print_string fmt s (*if e = 0 then pp_print_int fmt c else if e < 0 then Format.fprintf fmt "%ie%i" c (-e) else Format.fprintf fmt "%ie-%i" c e *)
|
79
|
(* | Const_float r -> pp_print_float fmt r *)
|
80
|
| Const_tag t -> pp_print_string fmt t
|
81
|
| Const_array ca -> fprintf fmt "[%a]" (Utils.fprintf_list ~sep:"," pp_const) ca
|
82
|
| Const_struct fl -> fprintf fmt "{%a }" (Utils.fprintf_list ~sep:" " pp_struct_const_field) fl
|
83
|
|
84
|
(* used only for annotations *)
|
85
|
| Const_string s -> pp_print_string fmt ("\"" ^ s ^ "\"")
|
86
|
| Const_modeid s -> pp_print_string fmt ("\"" ^ s ^ "\"")
|
87
|
|
88
|
|
89
|
let pp_annot_key fmt kwds =
|
90
|
match kwds with
|
91
|
| [] -> assert false
|
92
|
| [x] -> pp_print_string fmt x
|
93
|
| _ -> fprintf fmt "/%a/" (fprintf_list ~sep:"/" pp_print_string) kwds
|
94
|
|
95
|
let rec pp_expr fmt expr =
|
96
|
(match expr.expr_annot with
|
97
|
| None -> fprintf fmt "%t"
|
98
|
| Some ann -> fprintf fmt "@[(%a %t)@]" pp_expr_annot ann)
|
99
|
(fun fmt ->
|
100
|
match expr.expr_desc with
|
101
|
| Expr_const c -> pp_const fmt c
|
102
|
| Expr_ident id -> fprintf fmt "%s" id
|
103
|
| Expr_array a -> fprintf fmt "[%a]" pp_tuple a
|
104
|
| Expr_access (a, d) -> fprintf fmt "%a[%a]" pp_expr a Dimension.pp_dimension d
|
105
|
| Expr_power (a, d) -> fprintf fmt "(%a^%a)" pp_expr a Dimension.pp_dimension d
|
106
|
| Expr_tuple el -> fprintf fmt "(%a)" pp_tuple el
|
107
|
| Expr_ite (c, t, e) -> fprintf fmt "@[<hov 1>(if %a then@ @[<hov 2>%a@]@ else@ @[<hov 2>%a@]@])" pp_expr c pp_expr t pp_expr e
|
108
|
| Expr_arrow (e1, e2) -> fprintf fmt "(%a -> %a)" pp_expr e1 pp_expr e2
|
109
|
| Expr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_expr e1 pp_expr e2
|
110
|
| Expr_pre e -> fprintf fmt "pre %a" pp_expr e
|
111
|
| Expr_when (e, id, l) -> fprintf fmt "%a when %s(%s)" pp_expr e l id
|
112
|
| Expr_merge (id, hl) ->
|
113
|
fprintf fmt "merge %s %a" id pp_handlers hl
|
114
|
| Expr_appl (id, e, r) -> pp_app fmt id e r
|
115
|
)
|
116
|
and pp_tuple fmt el =
|
117
|
fprintf_list ~sep:"," pp_expr fmt el
|
118
|
|
119
|
and pp_handler fmt (t, h) =
|
120
|
fprintf fmt "(%s -> %a)" t pp_expr h
|
121
|
|
122
|
and pp_handlers fmt hl =
|
123
|
fprintf_list ~sep:" " pp_handler fmt hl
|
124
|
|
125
|
and pp_app fmt id e r =
|
126
|
match r with
|
127
|
| None -> pp_call fmt id e
|
128
|
| Some c -> fprintf fmt "%t every (%a)" (fun fmt -> pp_call fmt id e) pp_expr c
|
129
|
|
130
|
and pp_call fmt id e =
|
131
|
match id, e.expr_desc with
|
132
|
| "+", Expr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_expr e1 pp_expr e2
|
133
|
| "uminus", _ -> fprintf fmt "(- %a)" pp_expr e
|
134
|
| "-", Expr_tuple([e1;e2]) -> fprintf fmt "(%a - %a)" pp_expr e1 pp_expr e2
|
135
|
| "*", Expr_tuple([e1;e2]) -> fprintf fmt "(%a * %a)" pp_expr e1 pp_expr e2
|
136
|
| "/", Expr_tuple([e1;e2]) -> fprintf fmt "(%a / %a)" pp_expr e1 pp_expr e2
|
137
|
| "mod", Expr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_expr e1 pp_expr e2
|
138
|
| "&&", Expr_tuple([e1;e2]) -> fprintf fmt "(%a and %a)" pp_expr e1 pp_expr e2
|
139
|
| "||", Expr_tuple([e1;e2]) -> fprintf fmt "(%a or %a)" pp_expr e1 pp_expr e2
|
140
|
| "xor", Expr_tuple([e1;e2]) -> fprintf fmt "(%a xor %a)" pp_expr e1 pp_expr e2
|
141
|
| "impl", Expr_tuple([e1;e2]) -> fprintf fmt "(%a => %a)" pp_expr e1 pp_expr e2
|
142
|
| "<", Expr_tuple([e1;e2]) -> fprintf fmt "(%a < %a)" pp_expr e1 pp_expr e2
|
143
|
| "<=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a <= %a)" pp_expr e1 pp_expr e2
|
144
|
| ">", Expr_tuple([e1;e2]) -> fprintf fmt "(%a > %a)" pp_expr e1 pp_expr e2
|
145
|
| ">=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a >= %a)" pp_expr e1 pp_expr e2
|
146
|
| "!=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a <> %a)" pp_expr e1 pp_expr e2
|
147
|
| "=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a = %a)" pp_expr e1 pp_expr e2
|
148
|
| "not", _ -> fprintf fmt "(not %a)" pp_expr e
|
149
|
| _, Expr_tuple _ -> fprintf fmt "%s %a" id pp_expr e
|
150
|
| _ -> fprintf fmt "%s (%a)" id pp_expr e
|
151
|
|
152
|
and pp_eexpr fmt e =
|
153
|
fprintf fmt "%a%t %a"
|
154
|
(Utils.fprintf_list ~sep:"; " pp_quantifiers) e.eexpr_quantifiers
|
155
|
(fun fmt -> match e.eexpr_quantifiers with [] -> () | _ -> fprintf fmt ";")
|
156
|
pp_expr e.eexpr_qfexpr
|
157
|
|
158
|
and pp_sf_value fmt e =
|
159
|
fprintf fmt "%a"
|
160
|
(* (Utils.fprintf_list ~sep:"; " pp_quantifiers) e.eexpr_quantifiers *)
|
161
|
(* (fun fmt -> match e.eexpr_quantifiers *)
|
162
|
(* with [] -> () *)
|
163
|
(* | _ -> fprintf fmt ";") *)
|
164
|
pp_expr e.eexpr_qfexpr
|
165
|
|
166
|
and pp_s_function fmt expr_ann =
|
167
|
let pp_annot fmt (kwds, ee) =
|
168
|
fprintf fmt " %t : %a"
|
169
|
(fun fmt -> match kwds with
|
170
|
| [] -> assert false
|
171
|
| [x] -> pp_print_string fmt x
|
172
|
| _ -> fprintf fmt "%a" (fprintf_list ~sep:"/" pp_print_string) kwds)
|
173
|
pp_sf_value ee
|
174
|
in
|
175
|
fprintf_list ~sep:"@ " pp_annot fmt expr_ann.annots
|
176
|
|
177
|
and pp_expr_annot fmt expr_ann =
|
178
|
let pp_annot fmt (kwds, ee) =
|
179
|
fprintf fmt "(*! %a: %a; *)"
|
180
|
pp_annot_key kwds
|
181
|
pp_eexpr ee
|
182
|
in
|
183
|
fprintf_list ~sep:"@ " pp_annot fmt expr_ann.annots
|
184
|
|
185
|
|
186
|
let pp_asserts fmt asserts =
|
187
|
match asserts with
|
188
|
| _::_ -> (
|
189
|
fprintf fmt "(* Asserts definitions *)@ ";
|
190
|
fprintf_list ~sep:"@ " (fun fmt assert_ ->
|
191
|
let expr = assert_.assert_expr in
|
192
|
fprintf fmt "assert %a;" pp_expr expr
|
193
|
) fmt asserts
|
194
|
)
|
195
|
| _ -> ()
|
196
|
|
197
|
(*
|
198
|
let pp_node_var fmt id = fprintf fmt "%s%s: %a(%a)%a" (if id.var_dec_const then "const " else "") id.var_id print_dec_ty id.var_dec_type.ty_dec_desc Types.print_ty id.var_type Clocks.print_ck_suffix id.var_clock
|
199
|
*)
|
200
|
let pp_node_var fmt id =
|
201
|
begin
|
202
|
fprintf fmt "%s%s: %a%a"
|
203
|
(if id.var_dec_const then "const " else "")
|
204
|
id.var_id
|
205
|
pp_var_type id
|
206
|
pp_var_clock id;
|
207
|
match id.var_dec_value with
|
208
|
| None -> ()
|
209
|
| Some v -> fprintf fmt " = %a" pp_expr v
|
210
|
end
|
211
|
|
212
|
let pp_node_args = fprintf_list ~sep:";@ " pp_node_var
|
213
|
|
214
|
let pp_node_eq fmt eq =
|
215
|
fprintf fmt "%a = %a;"
|
216
|
pp_eq_lhs eq.eq_lhs
|
217
|
pp_expr eq.eq_rhs
|
218
|
|
219
|
let pp_restart fmt restart =
|
220
|
fprintf fmt "%s" (if restart then "restart" else "resume")
|
221
|
|
222
|
let pp_unless fmt (_, expr, restart, st) =
|
223
|
fprintf fmt "unless %a %a %s"
|
224
|
pp_expr expr
|
225
|
pp_restart restart
|
226
|
st
|
227
|
|
228
|
let pp_until fmt (_, expr, restart, st) =
|
229
|
fprintf fmt "until %a %a %s"
|
230
|
pp_expr expr
|
231
|
pp_restart restart
|
232
|
st
|
233
|
|
234
|
let rec pp_handler fmt handler =
|
235
|
fprintf fmt "state %s:@ @[<v 2> %a%t%alet@,@[<v 2> %a@ %a@ %a@]@,tel@ %a@]"
|
236
|
handler.hand_state
|
237
|
(Utils.fprintf_list ~sep:"@ " pp_unless) handler.hand_unless
|
238
|
(fun fmt -> if not ([] = handler.hand_unless) then fprintf fmt "@ ")
|
239
|
(fun fmt locals ->
|
240
|
match locals with [] -> () | _ ->
|
241
|
fprintf fmt "@[<v 4>var %a@]@ "
|
242
|
(Utils.fprintf_list ~sep:"@ "
|
243
|
(fun fmt v -> fprintf fmt "%a;" pp_node_var v))
|
244
|
locals)
|
245
|
handler.hand_locals
|
246
|
(fprintf_list ~sep:"@ " pp_expr_annot) handler.hand_annots
|
247
|
pp_node_stmts handler.hand_stmts
|
248
|
pp_asserts handler.hand_asserts
|
249
|
(Utils.fprintf_list ~sep:"@," pp_until) handler.hand_until
|
250
|
|
251
|
and pp_node_stmt fmt stmt =
|
252
|
match stmt with
|
253
|
| Eq eq -> pp_node_eq fmt eq
|
254
|
| Aut aut -> pp_node_aut fmt aut
|
255
|
|
256
|
and pp_node_stmts fmt stmts = fprintf_list ~sep:"@ " pp_node_stmt fmt stmts
|
257
|
|
258
|
and pp_node_aut fmt aut =
|
259
|
fprintf fmt "@[<v 0>automaton %s@,%a@]"
|
260
|
aut.aut_id
|
261
|
(Utils.fprintf_list ~sep:"@ " pp_handler) aut.aut_handlers
|
262
|
|
263
|
and pp_node_eqs fmt eqs = fprintf_list ~sep:"@ " pp_node_eq fmt eqs
|
264
|
|
265
|
let pp_typedef fmt ty =
|
266
|
fprintf fmt "type %s = %a;" ty.tydef_id pp_var_type_dec_desc ty.tydef_desc
|
267
|
|
268
|
let pp_typedec fmt ty =
|
269
|
fprintf fmt "type %s;" ty.tydec_id
|
270
|
|
271
|
(* let rec pp_var_type fmt ty = *)
|
272
|
(* fprintf fmt "%a" (match ty.tdesc with *)
|
273
|
(* | Tvar | Tarrow | Tlink | Tunivar -> assert false *)
|
274
|
(* | Tint -> pp_print_string fmt "int" *)
|
275
|
(* | Treal -> pp_print_string fmt "real" *)
|
276
|
(* | Tbool -> pp_print_string fmt "bool" *)
|
277
|
(* | Trat -> pp_print_string fmt "rat" *)
|
278
|
(* | Tclock -> pp_print_string fmt "clock" *)
|
279
|
(* | Ttuple tel -> fprintf_list ~sep:" * " pp_var_type fmt tel *)
|
280
|
(* ) *)
|
281
|
|
282
|
|
283
|
|
284
|
(* let pp_quantifiers fmt (q, vars) =
|
285
|
* match q with
|
286
|
* | Forall -> fprintf fmt "forall %a" pp_vars vars
|
287
|
* | Exists -> fprintf fmt "exists %a" (fprintf_list ~sep:"; " pp_var) vars *)
|
288
|
|
289
|
(*let pp_eexpr fmt e =
|
290
|
fprintf fmt "%a%t %a"
|
291
|
(Utils.fprintf_list ~sep:"; " pp_quantifiers) e.eexpr_quantifiers
|
292
|
(fun fmt -> match e.eexpr_quantifiers with [] -> () | _ -> fprintf fmt ";")
|
293
|
pp_expr e.eexpr_qfexpr
|
294
|
*)
|
295
|
|
296
|
let pp_spec_eq fmt eq =
|
297
|
fprintf fmt "var %a : %a = %a;"
|
298
|
pp_eq_lhs eq.eq_lhs
|
299
|
Types.print_node_ty eq.eq_rhs.expr_type
|
300
|
pp_expr eq.eq_rhs
|
301
|
|
302
|
let pp_spec_stmt fmt stmt =
|
303
|
match stmt with
|
304
|
| Eq eq -> pp_spec_eq fmt eq
|
305
|
| Aut aut -> assert false (* Not supported yet *)
|
306
|
|
307
|
|
308
|
let pp_spec fmt spec =
|
309
|
(* const are prefixed with const in pp_var and with nothing for regular
|
310
|
variables. We adapt the call to produce the appropriate output. *)
|
311
|
fprintf_list ~eol:"@, " ~sep:"@, " (fun fmt v ->
|
312
|
fprintf fmt "%a = %t;"
|
313
|
pp_var v
|
314
|
(fun fmt -> match v.var_dec_value with None -> assert false | Some e -> pp_expr fmt e)
|
315
|
) fmt spec.consts;
|
316
|
|
317
|
fprintf_list ~eol:"@, " ~sep:"@, " (fun fmt s -> pp_spec_stmt fmt s) fmt spec.stmts;
|
318
|
fprintf_list ~eol:"@, " ~sep:"@, " (fun fmt r -> fprintf fmt "assume %a;" pp_eexpr r) fmt spec.assume;
|
319
|
fprintf_list ~eol:"@, " ~sep:"@, " (fun fmt r -> fprintf fmt "guarantees %a;" pp_eexpr r) fmt spec.guarantees;
|
320
|
fprintf_list ~eol:"@, " ~sep:"@, " (fun fmt mode ->
|
321
|
fprintf fmt "mode %s (@[<v 0>%a@ %a@]);"
|
322
|
mode.mode_id
|
323
|
(fprintf_list ~eol:"" ~sep:"@ " (fun fmt r -> fprintf fmt "require %a;" pp_eexpr r)) mode.require
|
324
|
(fprintf_list ~eol:"" ~sep:"@ " (fun fmt r -> fprintf fmt "ensure %a;" pp_eexpr r)) mode.ensure
|
325
|
) fmt spec.modes;
|
326
|
fprintf_list ~eol:"@, " ~sep:"@, " (fun fmt import ->
|
327
|
fprintf fmt "import %s (%a) returns (%a);"
|
328
|
import.import_nodeid
|
329
|
pp_expr import.inputs
|
330
|
pp_expr import.outputs
|
331
|
) fmt spec.imports
|
332
|
|
333
|
(* Project the contract node as a pure contract: local memories are pushed back in the contract definition. Should mainly be used to print it *)
|
334
|
let node_as_contract nd =
|
335
|
match nd.node_spec with
|
336
|
| None | Some (NodeSpec _) -> raise (Invalid_argument "Not a contract")
|
337
|
| Some (Contract c) -> (
|
338
|
(* While a processed contract shall have no locals, sttms nor
|
339
|
consts, an unprocessed one could. So we conservatively merge
|
340
|
elements, to enable printing unprocessed contracts *)
|
341
|
let consts, locals = List.partition(fun v -> v.var_dec_const) nd.node_locals in
|
342
|
{ c with
|
343
|
consts = consts @ c.consts;
|
344
|
locals = locals @ c.locals;
|
345
|
stmts = nd.node_stmts @ c.stmts;
|
346
|
}
|
347
|
)
|
348
|
|
349
|
let pp_contract fmt nd =
|
350
|
|
351
|
let c = node_as_contract nd in
|
352
|
fprintf fmt "@[<v 2>(*@@ contract %s(%a) returns (%a);@ "
|
353
|
nd.node_id
|
354
|
pp_node_args nd.node_inputs
|
355
|
pp_node_args nd.node_outputs;
|
356
|
fprintf fmt "@[<v 2>let@ ";
|
357
|
pp_spec fmt c;
|
358
|
fprintf fmt "@]@ tel@ @]*)@ "
|
359
|
|
360
|
let pp_spec_as_comment fmt (inl, outl, spec) =
|
361
|
match spec with
|
362
|
| Contract c -> (* should have been processed by now *)
|
363
|
fprintf fmt "@[<hov 2>(*@@ ";
|
364
|
pp_spec fmt c;
|
365
|
fprintf fmt "@]*)@ "
|
366
|
|
367
|
| NodeSpec name -> (* Pushing stmts in contract. We update the
|
368
|
original information with the computed one in
|
369
|
nd. *)
|
370
|
let pp_l = fprintf_list ~sep:"," pp_var_name in
|
371
|
fprintf fmt "@[<hov 2>(*@@ contract import %s(%a) returns (%a)@]*)@ "
|
372
|
name
|
373
|
pp_l inl
|
374
|
pp_l outl
|
375
|
|
376
|
|
377
|
let pp_node fmt nd =
|
378
|
fprintf fmt "@[<v 0>";
|
379
|
(* Prototype *)
|
380
|
fprintf fmt "%s @[<hov 0>%s (@[%a)@]@ returns (@[%a)@]@]@ "
|
381
|
(if nd.node_dec_stateless then "function" else "node")
|
382
|
nd.node_id
|
383
|
pp_node_args nd.node_inputs
|
384
|
pp_node_args nd.node_outputs;
|
385
|
(* Contracts *)
|
386
|
fprintf fmt "%a"
|
387
|
(fun fmt s -> match s with Some s -> pp_spec_as_comment fmt (nd.node_inputs, nd.node_outputs, s) | _ -> ()) nd.node_spec
|
388
|
(* (fun fmt -> match nd.node_spec with None -> () | Some _ -> fprintf fmt "@ ") *);
|
389
|
(* Locals *)
|
390
|
fprintf fmt "%a" (fun fmt locals ->
|
391
|
match locals with [] -> () | _ ->
|
392
|
fprintf fmt "@[<v 4>var %a@]@ "
|
393
|
(fprintf_list ~sep:"@ "
|
394
|
(fun fmt v -> fprintf fmt "%a;" pp_node_var v))
|
395
|
locals
|
396
|
) nd.node_locals;
|
397
|
(* Checks *)
|
398
|
fprintf fmt "%a"
|
399
|
(fun fmt checks ->
|
400
|
match checks with [] -> () | _ ->
|
401
|
fprintf fmt "@[<v 4>check@ %a@]@ "
|
402
|
(fprintf_list ~sep:"@ "
|
403
|
(fun fmt d -> fprintf fmt "%a" Dimension.pp_dimension d))
|
404
|
checks
|
405
|
) nd.node_checks;
|
406
|
(* Body *)
|
407
|
fprintf fmt "let@[<h 2> @ @[<v>";
|
408
|
(* Annotations *)
|
409
|
fprintf fmt "%a@ " (fprintf_list ~sep:"@ " pp_expr_annot) nd.node_annot;
|
410
|
(* Statements *)
|
411
|
fprintf fmt "%a@ " pp_node_stmts nd.node_stmts;
|
412
|
(* Asserts *)
|
413
|
fprintf fmt "%a" pp_asserts nd.node_asserts;
|
414
|
(* closing boxes body (2) and node (1) *)
|
415
|
fprintf fmt "@]@]@ tel@]@ "
|
416
|
|
417
|
|
418
|
(*fprintf fmt "@ /* Scheduling: %a */ @ " (fprintf_list ~sep:", " pp_print_string) (Scheduling.schedule_node nd)*)
|
419
|
|
420
|
let pp_node fmt nd =
|
421
|
match nd.node_spec, nd.node_iscontract with
|
422
|
| None, false
|
423
|
| Some (NodeSpec _), false
|
424
|
-> pp_node fmt nd
|
425
|
| Some (Contract _), false -> pp_node fmt nd (* may happen early in the compil process *)
|
426
|
| Some (Contract _), true -> pp_contract fmt nd
|
427
|
| _ -> assert false
|
428
|
|
429
|
let pp_imported_node fmt ind =
|
430
|
fprintf fmt "@[<v 0>";
|
431
|
(* Prototype *)
|
432
|
fprintf fmt "%s @[<hov 0>%s (@[%a)@]@ returns (@[%a)@]@]@ "
|
433
|
(if ind.nodei_stateless then "function" else "node")
|
434
|
ind.nodei_id
|
435
|
pp_node_args ind.nodei_inputs
|
436
|
pp_node_args ind.nodei_outputs;
|
437
|
(* Contracts *)
|
438
|
fprintf fmt "%a%t"
|
439
|
(fun fmt s -> match s with Some s -> pp_spec_as_comment fmt (ind.nodei_inputs, ind.nodei_outputs, s) | _ -> ()) ind.nodei_spec
|
440
|
(fun fmt -> match ind.nodei_spec with None -> () | Some _ -> fprintf fmt "@ ");
|
441
|
fprintf fmt "@]@ "
|
442
|
|
443
|
|
444
|
let pp_const_decl fmt cdecl =
|
445
|
fprintf fmt "%s = %a;" cdecl.const_id pp_const cdecl.const_value
|
446
|
|
447
|
let pp_const_decl_list fmt clist =
|
448
|
fprintf_list ~sep:"@ " pp_const_decl fmt clist
|
449
|
|
450
|
|
451
|
|
452
|
let pp_decl fmt decl =
|
453
|
match decl.top_decl_desc with
|
454
|
| Node nd -> fprintf fmt "%a" pp_node nd
|
455
|
| ImportedNode ind ->
|
456
|
fprintf fmt "imported %a;" pp_imported_node ind
|
457
|
| Const c -> fprintf fmt "const %a" pp_const_decl c
|
458
|
| Open (local, s) -> if local then fprintf fmt "#open \"%s\"" s else fprintf fmt "#open <%s>" s
|
459
|
| Include s -> fprintf fmt "include \"%s\"" s
|
460
|
| TypeDef tdef -> fprintf fmt "%a" pp_typedef tdef
|
461
|
|
462
|
let pp_prog fmt prog =
|
463
|
(* we first print types: the function SortProg.sort could do the job but ut
|
464
|
introduces a cyclic dependance *)
|
465
|
|
466
|
let open_decl, prog =
|
467
|
List.partition (fun decl -> match decl.top_decl_desc with Open _ -> true | _ -> false) prog
|
468
|
in
|
469
|
let type_decl, prog =
|
470
|
List.partition (fun decl -> match decl.top_decl_desc with TypeDef _ -> true | _ -> false) prog
|
471
|
in
|
472
|
fprintf fmt "@[<v 0>%a@]" (fprintf_list ~sep:"@ " pp_decl) (open_decl@type_decl@prog)
|
473
|
|
474
|
(* Gives a short overview of model content. Do not print all node content *)
|
475
|
let pp_short_decl fmt decl =
|
476
|
match decl.top_decl_desc with
|
477
|
| Node nd -> fprintf fmt "node %s@ " nd.node_id
|
478
|
| ImportedNode ind -> fprintf fmt "imported node %s" ind.nodei_id
|
479
|
| Const c -> fprintf fmt "const %a@ " pp_const_decl c
|
480
|
| Include s -> fprintf fmt "include \"%s\"" s
|
481
|
| Open (local, s) -> if local then fprintf fmt "#open \"%s\"@ " s else fprintf fmt "#open <%s>@ " s
|
482
|
| TypeDef tdef -> fprintf fmt "type %s;@ " tdef.tydef_id
|
483
|
|
484
|
let pp_lusi fmt decl =
|
485
|
match decl.top_decl_desc with
|
486
|
| ImportedNode ind -> fprintf fmt "%a;@ " pp_imported_node ind
|
487
|
| Const c -> fprintf fmt "const %a@ " pp_const_decl c
|
488
|
| Include s -> fprintf fmt "include \"%s\"" s
|
489
|
| Open (local, s) -> if local then fprintf fmt "#open \"%s\"@ " s else fprintf fmt "#open <%s>@ " s
|
490
|
| TypeDef tdef -> fprintf fmt "%a@ " pp_typedef tdef
|
491
|
| Node _ -> assert false
|
492
|
|
493
|
let pp_lusi_header fmt basename prog =
|
494
|
fprintf fmt "@[<v 0>";
|
495
|
fprintf fmt "(* Generated Lustre Interface file from %s.lus *)@ " basename;
|
496
|
fprintf fmt "(* by Lustre-C compiler version %s, %a *)@ " Version.number pp_date (Unix.gmtime (Unix.time ()));
|
497
|
fprintf fmt "(* Feel free to mask some of the definitions by removing them from this file. *)@ @ ";
|
498
|
List.iter (fprintf fmt "%a@ " pp_lusi) prog;
|
499
|
fprintf fmt "@]@."
|
500
|
|
501
|
let pp_offset fmt offset =
|
502
|
match offset with
|
503
|
| Index i -> fprintf fmt "[%a]" Dimension.pp_dimension i
|
504
|
| Field f -> fprintf fmt ".%s" f
|
505
|
|
506
|
(* Local Variables: *)
|
507
|
(* compile-command:"make -C .." *)
|
508
|
(* End: *)
|