Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / printers.ml @ 5500edb8

History | View | Annotate | Download (13.6 KB)

1 b38ffff3 ploc
(********************************************************************)
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 0cbf0839 ploc
open LustreSpec
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 (out, flush, newline, spaces) = pp_get_all_formatter_output_functions fmt () in
20
  let macro_newline () = (out "\\" 0 1; newline ()) in
21
  begin
22
    pp_set_all_formatter_output_functions fmt out flush macro_newline spaces;
23
    pp_fun fmt v;
24
    pp_set_all_formatter_output_functions fmt out flush newline spaces;
25
  end
26
27 01d48bb0 xthirioux
let rec print_dec_struct_ty_field fmt (label, cty) =
28
  fprintf fmt "%a : %a" pp_print_string label print_dec_ty cty
29
and print_dec_ty fmt cty =
30
  match (*get_repr_type*) cty with
31
  | Tydec_any -> fprintf fmt "Any"
32
  | Tydec_int -> fprintf fmt "int"
33
  | Tydec_real 
34
  | Tydec_float -> fprintf fmt "real"
35
  | Tydec_bool -> fprintf fmt "bool"
36
  | Tydec_clock cty' -> fprintf fmt "%a clock" print_dec_ty cty'
37
  | Tydec_const c -> fprintf fmt "%s" c
38
  | Tydec_enum taglist -> fprintf fmt "enum {%a }"
39
      (Utils.fprintf_list ~sep:", " pp_print_string) taglist
40
  | Tydec_struct fieldlist -> fprintf fmt "struct {%a }"
41
      (Utils.fprintf_list ~sep:"; " print_dec_struct_ty_field) fieldlist
42
  | Tydec_array (d, cty') -> fprintf fmt "%a^%a" print_dec_ty cty' Dimension.pp_dimension d
43 0cbf0839 ploc
44
let pp_var_name fmt id = fprintf fmt "%s" id.var_id
45
46
let pp_eq_lhs = fprintf_list ~sep:", " pp_print_string
47
48 36454535 ploc
let pp_var fmt id = fprintf fmt "%s%s: %a" (if id.var_dec_const then "const " else "") id.var_id Types.print_ty id.var_type
49
50
let pp_quantifiers fmt (q, vars) =
51
  match q with
52
    | Forall -> fprintf fmt "forall %a" (fprintf_list ~sep:"; " pp_var) vars 
53
    | Exists -> fprintf fmt "exists %a" (fprintf_list ~sep:"; " pp_var) vars 
54
55 aa223e69 xthirioux
let rec pp_struct_const_field fmt (label, c) =
56
  fprintf fmt "%a = %a;" pp_print_string label pp_const c
57
and pp_const fmt c = 
58 0cbf0839 ploc
  match c with
59
    | Const_int i -> pp_print_int fmt i
60
    | Const_real r -> pp_print_string fmt r
61
    | Const_float r -> pp_print_float fmt r
62
    | Const_tag  t -> pp_print_string fmt t
63
    | Const_array ca -> Format.fprintf fmt "[%a]" (Utils.fprintf_list ~sep:"," pp_const) ca
64 aa223e69 xthirioux
    | Const_struct fl -> Format.fprintf fmt "{%a }" (Utils.fprintf_list ~sep:" " pp_struct_const_field) fl
65 0038002e ploc
    | Const_string s -> pp_print_string fmt ("\"" ^ s ^ "\"")
66 0cbf0839 ploc
67
68 36454535 ploc
let rec pp_expr fmt expr =
69
  (match expr.expr_annot with 
70
  | None -> fprintf fmt "%t" 
71
  | Some ann -> fprintf fmt "(%a %t)" pp_expr_annot ann)
72
    (fun fmt -> 
73
      match expr.expr_desc with
74 0cbf0839 ploc
    | Expr_const c -> pp_const fmt c
75
    | Expr_ident id -> Format.fprintf fmt "%s" id
76
    | Expr_array a -> fprintf fmt "[%a]" pp_tuple a
77
    | Expr_access (a, d) -> fprintf fmt "%a[%a]" pp_expr a Dimension.pp_dimension d
78
    | Expr_power (a, d) -> fprintf fmt "(%a^%a)" pp_expr a Dimension.pp_dimension d
79
    | Expr_tuple el -> fprintf fmt "(%a)" pp_tuple el
80
    | Expr_ite (c, t, e) -> fprintf fmt "(if %a then %a else %a)" pp_expr c pp_expr t pp_expr e
81 c02d255e ploc
    | Expr_arrow (e1, e2) -> fprintf fmt "(%a -> %a)" pp_expr e1 pp_expr e2
82 0cbf0839 ploc
    | Expr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_expr e1 pp_expr e2
83
    | Expr_pre e -> fprintf fmt "pre %a" pp_expr e
84
    | Expr_when (e, id, l) -> fprintf fmt "%a when %s(%s)" pp_expr e l id
85
    | Expr_merge (id, hl) -> 
86
      fprintf fmt "merge %s %a" id pp_handlers hl
87
    | Expr_appl (id, e, r) -> pp_app fmt id e r
88 36454535 ploc
    )
89 0cbf0839 ploc
and pp_tuple fmt el =
90
 fprintf_list ~sep:"," pp_expr fmt el
91
92
and pp_handler fmt (t, h) =
93
 fprintf fmt "(%s -> %a)" t pp_expr h
94
95
and pp_handlers fmt hl =
96
 fprintf_list ~sep:" " pp_handler fmt hl
97
98
and pp_app fmt id e r =
99
  match r with
100 cfdb4fe9 xthirioux
  | None -> pp_call fmt id e
101 6a1a01d2 xthirioux
  | Some c -> fprintf fmt "%t every (%a)" (fun fmt -> pp_call fmt id e) pp_expr c 
102 cfdb4fe9 xthirioux
103
and pp_call fmt id e =
104
  match id, e.expr_desc with
105
  | "+", Expr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_expr e1 pp_expr e2
106
  | "uminus", _ -> fprintf fmt "(- %a)" pp_expr e
107
  | "-", Expr_tuple([e1;e2]) -> fprintf fmt "(%a - %a)" pp_expr e1 pp_expr e2
108
  | "*", Expr_tuple([e1;e2]) -> fprintf fmt "(%a * %a)" pp_expr e1 pp_expr e2
109
  | "/", Expr_tuple([e1;e2]) -> fprintf fmt "(%a / %a)" pp_expr e1 pp_expr e2
110
  | "mod", Expr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_expr e1 pp_expr e2
111
  | "&&", Expr_tuple([e1;e2]) -> fprintf fmt "(%a and %a)" pp_expr e1 pp_expr e2
112
  | "||", Expr_tuple([e1;e2]) -> fprintf fmt "(%a or %a)" pp_expr e1 pp_expr e2
113
  | "xor", Expr_tuple([e1;e2]) -> fprintf fmt "(%a xor %a)" pp_expr e1 pp_expr e2
114
  | "impl", Expr_tuple([e1;e2]) -> fprintf fmt "(%a => %a)" pp_expr e1 pp_expr e2
115
  | "<", Expr_tuple([e1;e2]) -> fprintf fmt "(%a < %a)" pp_expr e1 pp_expr e2
116
  | "<=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a <= %a)" pp_expr e1 pp_expr e2
117
  | ">", Expr_tuple([e1;e2]) -> fprintf fmt "(%a > %a)" pp_expr e1 pp_expr e2
118
  | ">=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a >= %a)" pp_expr e1 pp_expr e2
119
  | "!=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a != %a)" pp_expr e1 pp_expr e2
120
  | "=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a = %a)" pp_expr e1 pp_expr e2
121
  | "not", _ -> fprintf fmt "(not %a)" pp_expr e
122
  | _, Expr_tuple _ -> fprintf fmt "%s %a" id pp_expr e
123
  | _ -> fprintf fmt "%s (%a)" id pp_expr e
124 36454535 ploc
125
and pp_eexpr fmt e =
126
  fprintf fmt "%a%t %a"
127
    (Utils.fprintf_list ~sep:"; " pp_quantifiers) e.eexpr_quantifiers
128
    (fun fmt -> match e.eexpr_quantifiers with [] -> () | _ -> fprintf fmt ";")
129
    pp_expr e.eexpr_qfexpr
130
131
132
and pp_expr_annot fmt expr_ann =
133
  let pp_annot fmt (kwds, ee) =
134
    Format.fprintf fmt "(*! %t: %a *)"
135
      (fun fmt -> match kwds with | [] -> assert false | [x] -> Format.pp_print_string fmt x | _ -> Format.fprintf fmt "/%a/" (fprintf_list ~sep:"/" Format.pp_print_string) kwds)
136
      pp_eexpr ee
137
  in
138
  fprintf_list ~sep:"@ " pp_annot fmt expr_ann.annots
139
    
140 01d48bb0 xthirioux
(*
141
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
142
*)
143
let pp_node_var fmt id =
144
  begin
145
    fprintf fmt "%s%s: %a%a" (if id.var_dec_const then "const " else "") id.var_id Types.print_node_ty id.var_type Clocks.print_ck_suffix id.var_clock;
146
    match id.var_dec_value with
147
    | None -> () 
148
    | Some v -> fprintf fmt " = %a" pp_expr v
149
  end 
150
151
let pp_node_args = fprintf_list ~sep:"; " pp_node_var 
152
153 0cbf0839 ploc
let pp_node_eq fmt eq = 
154
  fprintf fmt "%a = %a;" 
155
    pp_eq_lhs eq.eq_lhs
156
    pp_expr eq.eq_rhs
157
158 1eda3e78 xthirioux
let pp_restart fmt restart =
159
  Format.fprintf fmt "%s" (if restart then "restart" else "resume")
160
161
let pp_unless fmt (_, expr, restart, st) =
162 6a1a01d2 xthirioux
  Format.fprintf fmt "unless %a %a %s@ "
163 1eda3e78 xthirioux
    pp_expr expr
164
    pp_restart restart
165
    st
166
167
let pp_until fmt (_, expr, restart, st) =
168 6a1a01d2 xthirioux
  Format.fprintf fmt "until %a %a %s@ "
169 1eda3e78 xthirioux
    pp_expr expr
170
    pp_restart restart
171
    st
172
173
let rec pp_handler fmt handler =
174 6a1a01d2 xthirioux
  Format.fprintf fmt "state %s ->@ @[<v 2>  %a%alet@,@[<v 2>  %a@]@,tel%a@]"
175 1eda3e78 xthirioux
    handler.hand_state
176 6a1a01d2 xthirioux
    (Utils.fprintf_list ~sep:"@," pp_unless) handler.hand_unless
177 1eda3e78 xthirioux
    (fun fmt locals ->
178
      match locals with [] -> () | _ ->
179
	Format.fprintf fmt "@[<v 4>var %a@]@ " 
180
	  (Utils.fprintf_list ~sep:"@ " 
181
	     (fun fmt v -> Format.fprintf fmt "%a;" pp_node_var v))
182
	  locals)
183
    handler.hand_locals
184
    pp_node_stmts handler.hand_stmts
185 6a1a01d2 xthirioux
    (Utils.fprintf_list ~sep:"@," pp_until) handler.hand_until
186 1eda3e78 xthirioux
187
and pp_node_stmt fmt stmt =
188
  match stmt with
189
  | Eq eq -> pp_node_eq fmt eq
190
  | Aut aut -> pp_node_aut fmt aut
191
192
and pp_node_stmts fmt stmts = fprintf_list ~sep:"@ " pp_node_stmt fmt stmts
193
194
and pp_node_aut fmt aut =
195 6a1a01d2 xthirioux
  Format.fprintf fmt "@[<v 0>automaton %s@,%a@]"
196 1eda3e78 xthirioux
    aut.aut_id
197
    (Utils.fprintf_list ~sep:"@ " pp_handler) aut.aut_handlers
198
199
and pp_node_eqs fmt eqs = fprintf_list ~sep:"@ " pp_node_eq fmt eqs
200 0cbf0839 ploc
201 6aeb3388 xthirioux
let rec pp_var_struct_type_field fmt (label, tdesc) =
202
  fprintf fmt "%a : %a;" pp_print_string label pp_var_type_dec_desc tdesc
203
and pp_var_type_dec_desc fmt tdesc =
204
  match tdesc with 
205
  | Tydec_any -> fprintf fmt "<any>"
206
  | Tydec_int -> fprintf fmt "int"
207
  | Tydec_real -> fprintf fmt "real"
208
  | Tydec_float -> fprintf fmt "float"
209
  | Tydec_bool -> fprintf fmt "bool"
210
  | Tydec_clock t -> fprintf fmt "%a clock" pp_var_type_dec_desc t
211
  | Tydec_const t -> fprintf fmt "%s" t
212
  | Tydec_enum id_list -> fprintf fmt "enum {%a }" (fprintf_list ~sep:", " pp_print_string) id_list
213
  | Tydec_struct f_list -> fprintf fmt "struct {%a }" (fprintf_list ~sep:" " pp_var_struct_type_field) f_list
214
  | Tydec_array (s, t) -> fprintf fmt "%a^%a" pp_var_type_dec_desc t Dimension.pp_dimension s
215 0cbf0839 ploc
216
let pp_var_type_dec fmt ty =
217 6aeb3388 xthirioux
  pp_var_type_dec_desc fmt ty.ty_dec_desc
218
219 70e1006b xthirioux
let pp_typedef fmt ty =
220
  fprintf fmt "type %s = %a;@ " ty.tydef_id pp_var_type_dec_desc ty.tydef_desc
221
222
let pp_typedec fmt ty =
223
  fprintf fmt "type %s;@ " ty.tydec_id
224 0cbf0839 ploc
225
(* let rec pp_var_type fmt ty =  *)
226
(*   fprintf fmt "%a" (match ty.tdesc with  *)
227
(*     | Tvar | Tarrow | Tlink | Tunivar -> assert false *)
228
(*     | Tint -> pp_print_string fmt "int" *)
229
(*     | Treal -> pp_print_string fmt "real" *)
230
(*     | Tbool -> pp_print_string fmt "bool" *)
231
(*     | Trat -> pp_print_string fmt "rat" *)
232
(*     | Tclock -> pp_print_string fmt "clock"  *)
233
(*     | Ttuple tel -> fprintf_list ~sep:" * " pp_var_type fmt tel *)
234
(*   ) *)
235
236 c02d255e ploc
237
let pp_spec fmt spec =
238
  fprintf fmt "@[<hov 2>(*@@ ";
239 0038002e ploc
  fprintf_list ~sep:"@;@@ " (fun fmt r -> fprintf fmt "requires %a;" pp_eexpr r) fmt spec.requires;
240
  fprintf_list ~sep:"@;@@ " (fun fmt r -> fprintf fmt "ensures %a; " pp_eexpr r) fmt spec.ensures;
241
  fprintf_list ~sep:"@;" (fun fmt (name, assumes, ensures, _) -> 
242 c02d255e ploc
    fprintf fmt "behavior %s:@[@ %a@ %a@]" 
243
      name
244
      (fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "assumes %a;" pp_eexpr r)) assumes
245 0038002e ploc
      (fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "ensures %a;" pp_eexpr r)) ensures
246 c02d255e ploc
  ) fmt spec.behaviors;
247
  fprintf fmt "@]*)";
248
  ()
249
250 36454535 ploc
251
let pp_asserts fmt asserts =
252
  match asserts with 
253
  | _::_ -> (
254
  fprintf fmt "(* Asserts definitions *)@ ";
255
  fprintf_list ~sep:"@ " (fun fmt assert_ -> 
256
    let expr = assert_.assert_expr in
257
    fprintf fmt "assert %a;" pp_expr expr 
258
  ) fmt asserts 
259
  )
260
  | _ -> ()
261
    
262 0cbf0839 ploc
let pp_node fmt nd = 
263 36454535 ploc
fprintf fmt "@[<v 0>%a%t%s %s (%a) returns (%a)@.%a%alet@.@[<h 2>   @ @[<v>%a@ %a@ %a@]@ @]@.tel@]@."
264 c02d255e ploc
  (fun fmt s -> match s with Some s -> pp_spec fmt s | _ -> ()) nd.node_spec
265 d3e4c22f xthirioux
  (fun fmt -> match nd.node_spec with None -> () | Some _ -> Format.fprintf fmt "@.")
266
  (if nd.node_dec_stateless then "function" else "node")
267 0cbf0839 ploc
  nd.node_id
268
  pp_node_args nd.node_inputs
269
  pp_node_args nd.node_outputs
270
  (fun fmt locals ->
271
  match locals with [] -> () | _ ->
272
    fprintf fmt "@[<v 4>var %a@]@ " 
273
      (fprintf_list ~sep:"@ " 
274 8b3afe43 xthirioux
	 (fun fmt v -> fprintf fmt "%a;" pp_node_var v))
275 0cbf0839 ploc
      locals
276
  ) nd.node_locals
277
  (fun fmt checks ->
278
  match checks with [] -> () | _ ->
279
    fprintf fmt "@[<v 4>check@ %a@]@ " 
280
      (fprintf_list ~sep:"@ " 
281
	 (fun fmt d -> fprintf fmt "%a" Dimension.pp_dimension d))
282
      checks
283
  ) nd.node_checks
284 36454535 ploc
  (fprintf_list ~sep:"@ " pp_expr_annot) nd.node_annot
285 1eda3e78 xthirioux
  pp_node_stmts nd.node_stmts
286 36454535 ploc
  pp_asserts nd.node_asserts
287 0cbf0839 ploc
(*fprintf fmt "@ /* Scheduling: %a */ @ " (fprintf_list ~sep:", " pp_print_string) (Scheduling.schedule_node nd)*)
288
289
let pp_imported_node fmt ind = 
290 70e1006b xthirioux
  fprintf fmt "@[<v>%s %s (%a) returns (%a)@]"
291 d3e4c22f xthirioux
    (if ind.nodei_stateless then "function" else "node")
292 0cbf0839 ploc
    ind.nodei_id
293
    pp_node_args ind.nodei_inputs
294
    pp_node_args ind.nodei_outputs
295
296 70e1006b xthirioux
let pp_const_decl fmt cdecl =
297
  fprintf fmt "%s = %a;" cdecl.const_id pp_const cdecl.const_value
298
299
let pp_const_decl_list fmt clist = 
300
  fprintf_list ~sep:"@ " pp_const_decl fmt clist
301 c00d0b42 ploc
302 0cbf0839 ploc
let pp_decl fmt decl =
303
  match decl.top_decl_desc with
304
  | Node nd -> fprintf fmt "%a@ " pp_node nd
305
  | ImportedNode ind ->
306
    fprintf fmt "imported %a;@ " pp_imported_node ind
307 70e1006b xthirioux
  | Const c -> fprintf fmt "const %a@ " pp_const_decl c
308
  | Open (local, s) -> if local then fprintf fmt "#open \"%s\"@ " s else fprintf fmt "#open <%s>@ " s
309
  | TypeDef tdef -> fprintf fmt "%a@ " pp_typedef tdef
310 0cbf0839 ploc
311
let pp_prog fmt prog = 
312
  fprintf_list ~sep:"@ " pp_decl fmt prog
313
314
let pp_short_decl fmt decl =
315
  match decl.top_decl_desc with
316
  | Node nd -> fprintf fmt "node %s@ " nd.node_id
317
  | ImportedNode ind -> fprintf fmt "imported node %s" ind.nodei_id
318 70e1006b xthirioux
  | Const c -> fprintf fmt "const %a@ " pp_const_decl c
319
  | Open (local, s) -> if local then fprintf fmt "#open \"%s\"@ " s else fprintf fmt "#open <%s>@ " s
320
  | TypeDef tdef -> fprintf fmt "type %s;@ " tdef.tydef_id
321 0cbf0839 ploc
322 f22632aa ploc
let pp_lusi fmt decl = 
323
  match decl.top_decl_desc with
324 70e1006b xthirioux
  | ImportedNode ind -> fprintf fmt "%a;@ " pp_imported_node ind
325
  | Const c -> fprintf fmt "const %a@ " pp_const_decl c
326
  | Open (local, s) -> if local then fprintf fmt "#open \"%s\"@ " s else fprintf fmt "#open <%s>@ " s
327
  | TypeDef tdef -> fprintf fmt "%a@ " pp_typedef tdef
328
  | Node _ -> assert false
329
330
let pp_lusi_header fmt basename prog =
331
  fprintf fmt "(* Generated Lustre Interface file from %s.lus *)@." basename;
332 6aeb3388 xthirioux
  fprintf fmt "(* by Lustre-C compiler version %s, %a *)@." Version.number pp_date (Unix.gmtime (Unix.time ()));
333
  fprintf fmt "(* Feel free to mask some of the definitions by removing them from this file. *)@.@.";
334 f22632aa ploc
  List.iter (fprintf fmt "%a@." pp_lusi) prog    
335
  
336 0cbf0839 ploc
(* Local Variables: *)
337
(* compile-command:"make -C .." *)
338
(* End: *)