Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / printers.ml @ b1655a21

History | View | Annotate | Download (14.5 KB)

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 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

    
28
let pp_var_name fmt id = fprintf fmt "%s" id.var_id
29

    
30
let pp_eq_lhs = fprintf_list ~sep:", " pp_print_string
31

    
32
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
33

    
34
let pp_node_var fmt id = 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
35

    
36
let pp_node_args = fprintf_list ~sep:"; " pp_node_var 
37

    
38
let pp_quantifiers fmt (q, vars) =
39
  match q with
40
    | Forall -> fprintf fmt "forall %a" (fprintf_list ~sep:"; " pp_var) vars 
41
    | Exists -> fprintf fmt "exists %a" (fprintf_list ~sep:"; " pp_var) vars 
42

    
43
(*
44
let pp_econst fmt c = 
45
  match c with
46
    | EConst_int i -> pp_print_int fmt i
47
    | EConst_real r -> pp_print_string fmt r
48
    | EConst_float r -> pp_print_float fmt r
49
    | EConst_tag  t -> pp_print_string fmt t
50
    | EConst_string s -> pp_print_string fmt ("\"" ^ s ^ "\"")
51

    
52

    
53
let rec pp_eexpr fmt eexpr = 
54
  match eexpr.eexpr_desc with
55
    | EExpr_const c -> pp_econst fmt c
56
    | EExpr_ident id -> pp_print_string fmt id
57
    | EExpr_tuple el -> fprintf_list ~sep:"," pp_eexpr fmt el
58
    | EExpr_arrow (e1, e2) -> fprintf fmt "%a -> %a" pp_eexpr e1 pp_eexpr e2
59
    | EExpr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_eexpr e1 pp_eexpr e2
60
    (* | EExpr_concat (e1, e2) -> fprintf fmt "%a::%a" pp_eexpr e1 pp_eexpr e2 *)
61
    (* | EExpr_tail e -> fprintf fmt "tail %a" pp_eexpr e *)
62
    | EExpr_pre e -> fprintf fmt "pre %a" pp_eexpr e
63
    | EExpr_when (e, id) -> fprintf fmt "%a when %s" pp_eexpr e id
64
    | EExpr_merge (id, e1, e2) -> 
65
      fprintf fmt "merge (%s, %a, %a)" id pp_eexpr e1 pp_eexpr e2
66
    | EExpr_appl (id, e, r) -> pp_eapp fmt id e r
67
    | EExpr_forall (vars, e) -> fprintf fmt "forall %a; %a" pp_node_args vars pp_eexpr e 
68
    | EExpr_exists (vars, e) -> fprintf fmt "exists %a; %a" pp_node_args vars pp_eexpr e 
69

    
70

    
71
    (* | EExpr_whennot _ *)
72
    (* | EExpr_uclock _ *)
73
    (* | EExpr_dclock _ *)
74
    (* | EExpr_phclock _ -> assert false *)
75
and pp_eapp fmt id e r =
76
  match r with
77
  | None ->
78
    (match id, e.eexpr_desc with
79
    | "+", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_eexpr e1 pp_eexpr e2
80
    | "uminus", _ -> fprintf fmt "(- %a)" pp_eexpr e
81
    | "-", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a - %a)" pp_eexpr e1 pp_eexpr e2
82
    | "*", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a * %a)" pp_eexpr e1 pp_eexpr e2
83
    | "/", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a / %a)" pp_eexpr e1 pp_eexpr e2
84
    | "mod", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_eexpr e1 pp_eexpr e2
85
    | "&&", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a && %a)" pp_eexpr e1 pp_eexpr e2
86
    | "||", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a || %a)" pp_eexpr e1 pp_eexpr e2
87
    | "xor", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a ^^ %a)" pp_eexpr e1 pp_eexpr e2
88
    | "impl", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a ==> %a)" pp_eexpr e1 pp_eexpr e2
89
    | "<", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a < %a)" pp_eexpr e1 pp_eexpr e2
90
    | "<=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a <= %a)" pp_eexpr e1 pp_eexpr e2
91
    | ">", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a > %a)" pp_eexpr e1 pp_eexpr e2
92
    | ">=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a >= %a)" pp_eexpr e1 pp_eexpr e2
93
    | "!=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a != %a)" pp_eexpr e1 pp_eexpr e2
94
    | "=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a == %a)" pp_eexpr e1 pp_eexpr e2
95
    | "not", _ -> fprintf fmt "(! %a)" pp_eexpr e
96
    | "ite", EExpr_tuple([e1;e2;e3]) -> fprintf fmt "(if %a then %a else %a)" pp_eexpr e1 pp_eexpr e2 pp_eexpr e3
97
    | _ -> fprintf fmt "%s (%a)" id pp_eexpr e)
98
  | Some x -> fprintf fmt "%s (%a) every %s" id pp_eexpr e x 
99
*)
100

    
101

    
102
let rec pp_struct_const_field fmt (label, c) =
103
  fprintf fmt "%a = %a;" pp_print_string label pp_const c
104
and pp_const fmt c = 
105
  match c with
106
    | Const_int i -> pp_print_int fmt i
107
    | Const_real r -> pp_print_string fmt r
108
    | Const_float r -> pp_print_float fmt r
109
    | Const_tag  t -> pp_print_string fmt t
110
    | Const_array ca -> Format.fprintf fmt "[%a]" (Utils.fprintf_list ~sep:"," pp_const) ca
111
    | Const_struct fl -> Format.fprintf fmt "{%a }" (Utils.fprintf_list ~sep:" " pp_struct_const_field) fl
112
    | Const_string s -> pp_print_string fmt ("\"" ^ s ^ "\"")
113

    
114

    
115
let rec pp_expr fmt expr =
116
  (match expr.expr_annot with 
117
  | None -> fprintf fmt "%t" 
118
  | Some ann -> fprintf fmt "(%a %t)" pp_expr_annot ann)
119
    (fun fmt -> 
120
      match expr.expr_desc with
121
    | Expr_const c -> pp_const fmt c
122
    | Expr_ident id -> Format.fprintf fmt "%s" id
123
    | Expr_array a -> fprintf fmt "[%a]" pp_tuple a
124
    | Expr_access (a, d) -> fprintf fmt "%a[%a]" pp_expr a Dimension.pp_dimension d
125
    | Expr_power (a, d) -> fprintf fmt "(%a^%a)" pp_expr a Dimension.pp_dimension d
126
    | Expr_tuple el -> fprintf fmt "(%a)" pp_tuple el
127
    | Expr_ite (c, t, e) -> fprintf fmt "(if %a then %a else %a)" pp_expr c pp_expr t pp_expr e
128
    | Expr_arrow (e1, e2) -> fprintf fmt "(%a -> %a)" pp_expr e1 pp_expr e2
129
    | Expr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_expr e1 pp_expr e2
130
    | Expr_pre e -> fprintf fmt "pre %a" pp_expr e
131
    | Expr_when (e, id, l) -> fprintf fmt "%a when %s(%s)" pp_expr e l id
132
    | Expr_merge (id, hl) -> 
133
      fprintf fmt "merge %s %a" id pp_handlers hl
134
    | Expr_appl (id, e, r) -> pp_app fmt id e r
135
    )
136
and pp_tuple fmt el =
137
 fprintf_list ~sep:"," pp_expr fmt el
138

    
139
and pp_handler fmt (t, h) =
140
 fprintf fmt "(%s -> %a)" t pp_expr h
141

    
142
and pp_handlers fmt hl =
143
 fprintf_list ~sep:" " pp_handler fmt hl
144

    
145
and pp_app fmt id e r =
146
  match r with
147
  | None ->
148
    (match id, e.expr_desc with
149
    | "+", Expr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_expr e1 pp_expr e2
150
    | "uminus", _ -> fprintf fmt "(- %a)" pp_expr e
151
    | "-", Expr_tuple([e1;e2]) -> fprintf fmt "(%a - %a)" pp_expr e1 pp_expr e2
152
    | "*", Expr_tuple([e1;e2]) -> fprintf fmt "(%a * %a)" pp_expr e1 pp_expr e2
153
    | "/", Expr_tuple([e1;e2]) -> fprintf fmt "(%a / %a)" pp_expr e1 pp_expr e2
154
    | "mod", Expr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_expr e1 pp_expr e2
155
    | "&&", Expr_tuple([e1;e2]) -> fprintf fmt "(%a and %a)" pp_expr e1 pp_expr e2
156
    | "||", Expr_tuple([e1;e2]) -> fprintf fmt "(%a or %a)" pp_expr e1 pp_expr e2
157
    | "xor", Expr_tuple([e1;e2]) -> fprintf fmt "(%a xor %a)" pp_expr e1 pp_expr e2
158
    | "impl", Expr_tuple([e1;e2]) -> fprintf fmt "(%a => %a)" pp_expr e1 pp_expr e2
159
    | "<", Expr_tuple([e1;e2]) -> fprintf fmt "(%a < %a)" pp_expr e1 pp_expr e2
160
    | "<=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a <= %a)" pp_expr e1 pp_expr e2
161
    | ">", Expr_tuple([e1;e2]) -> fprintf fmt "(%a > %a)" pp_expr e1 pp_expr e2
162
    | ">=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a >= %a)" pp_expr e1 pp_expr e2
163
    | "!=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a != %a)" pp_expr e1 pp_expr e2
164
    | "=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a = %a)" pp_expr e1 pp_expr e2
165
    | "not", _ -> fprintf fmt "(not %a)" pp_expr e
166
    | _, Expr_tuple _ -> fprintf fmt "%s %a" id pp_expr e
167
    | _ -> fprintf fmt "%s (%a)" id pp_expr e
168
)
169
  | Some (x, l) -> fprintf fmt "%s (%a) every %s(%s)" id pp_expr e l x 
170

    
171

    
172

    
173
and pp_eexpr fmt e =
174
  fprintf fmt "%a%t %a"
175
    (Utils.fprintf_list ~sep:"; " pp_quantifiers) e.eexpr_quantifiers
176
    (fun fmt -> match e.eexpr_quantifiers with [] -> () | _ -> fprintf fmt ";")
177
    pp_expr e.eexpr_qfexpr
178

    
179

    
180
and pp_expr_annot fmt expr_ann =
181
  let pp_annot fmt (kwds, ee) =
182
    Format.fprintf fmt "(*! %t: %a *)"
183
      (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)
184
      pp_eexpr ee
185
  in
186
  fprintf_list ~sep:"@ " pp_annot fmt expr_ann.annots
187
    
188
	
189
let pp_node_eq fmt eq = 
190
  fprintf fmt "%a = %a;" 
191
    pp_eq_lhs eq.eq_lhs
192
    pp_expr eq.eq_rhs
193

    
194
let pp_node_eqs = fprintf_list ~sep:"@ " pp_node_eq
195

    
196
let rec pp_var_struct_type_field fmt (label, tdesc) =
197
  fprintf fmt "%a : %a;" pp_print_string label pp_var_type_dec_desc tdesc
198
and pp_var_type_dec_desc fmt tdesc =
199
  match tdesc with 
200
  | Tydec_any -> fprintf fmt "<any>"
201
  | Tydec_int -> fprintf fmt "int"
202
  | Tydec_real -> fprintf fmt "real"
203
  | Tydec_float -> fprintf fmt "float"
204
  | Tydec_bool -> fprintf fmt "bool"
205
  | Tydec_clock t -> fprintf fmt "%a clock" pp_var_type_dec_desc t
206
  | Tydec_const t -> fprintf fmt "%s" t
207
  | Tydec_enum id_list -> fprintf fmt "enum {%a }" (fprintf_list ~sep:", " pp_print_string) id_list
208
  | Tydec_struct f_list -> fprintf fmt "struct {%a }" (fprintf_list ~sep:" " pp_var_struct_type_field) f_list
209
  | Tydec_array (s, t) -> fprintf fmt "%a^%a" pp_var_type_dec_desc t Dimension.pp_dimension s
210

    
211
let pp_var_type_dec fmt ty =
212
  pp_var_type_dec_desc fmt ty.ty_dec_desc
213

    
214
let pp_type_def fmt ty =
215
  fprintf fmt "type %s = %a;@ " ty.ty_def_id pp_var_type_dec_desc ty.ty_def_desc
216

    
217
(* let rec pp_var_type fmt ty =  *)
218
(*   fprintf fmt "%a" (match ty.tdesc with  *)
219
(*     | Tvar | Tarrow | Tlink | Tunivar -> assert false *)
220
(*     | Tint -> pp_print_string fmt "int" *)
221
(*     | Treal -> pp_print_string fmt "real" *)
222
(*     | Tbool -> pp_print_string fmt "bool" *)
223
(*     | Trat -> pp_print_string fmt "rat" *)
224
(*     | Tclock -> pp_print_string fmt "clock"  *)
225
(*     | Ttuple tel -> fprintf_list ~sep:" * " pp_var_type fmt tel *)
226
(*   ) *)
227

    
228

    
229
let pp_spec fmt spec =
230
  fprintf fmt "@[<hov 2>(*@@ ";
231
  fprintf_list ~sep:"@;@@ " (fun fmt r -> fprintf fmt "requires %a;" pp_eexpr r) fmt spec.requires;
232
  fprintf_list ~sep:"@;@@ " (fun fmt r -> fprintf fmt "ensures %a; " pp_eexpr r) fmt spec.ensures;
233
  fprintf_list ~sep:"@;" (fun fmt (name, assumes, ensures, _) -> 
234
    fprintf fmt "behavior %s:@[@ %a@ %a@]" 
235
      name
236
      (fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "assumes %a;" pp_eexpr r)) assumes
237
      (fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "ensures %a;" pp_eexpr r)) ensures
238
  ) fmt spec.behaviors;
239
  fprintf fmt "@]*)";
240
  ()
241

    
242

    
243
let pp_asserts fmt asserts =
244
  match asserts with 
245
  | _::_ -> (
246
  fprintf fmt "(* Asserts definitions *)@ ";
247
  fprintf_list ~sep:"@ " (fun fmt assert_ -> 
248
    let expr = assert_.assert_expr in
249
    fprintf fmt "assert %a;" pp_expr expr 
250
  ) fmt asserts 
251
  )
252
  | _ -> ()
253
    
254
let pp_node fmt nd = 
255
fprintf fmt "@[<v 0>%a%t%s %s (%a) returns (%a)@.%a%alet@.@[<h 2>   @ @[<v>%a@ %a@ %a@]@ @]@.tel@]@."
256
  (fun fmt s -> match s with Some s -> pp_spec fmt s | _ -> ()) nd.node_spec
257
  (fun fmt -> match nd.node_spec with None -> () | Some _ -> Format.fprintf fmt "@.")
258
  (if nd.node_dec_stateless then "function" else "node")
259
  nd.node_id
260
  pp_node_args nd.node_inputs
261
  pp_node_args nd.node_outputs
262
  (fun fmt locals ->
263
  match locals with [] -> () | _ ->
264
    fprintf fmt "@[<v 4>var %a@]@ " 
265
      (fprintf_list ~sep:"@ " 
266
	 (fun fmt v -> fprintf fmt "%a;" pp_node_var v))
267
      locals
268
  ) nd.node_locals
269
  (fun fmt checks ->
270
  match checks with [] -> () | _ ->
271
    fprintf fmt "@[<v 4>check@ %a@]@ " 
272
      (fprintf_list ~sep:"@ " 
273
	 (fun fmt d -> fprintf fmt "%a" Dimension.pp_dimension d))
274
      checks
275
  ) nd.node_checks
276
  (fprintf_list ~sep:"@ " pp_expr_annot) nd.node_annot
277
  pp_node_eqs nd.node_eqs
278
  pp_asserts nd.node_asserts
279
(*fprintf fmt "@ /* Scheduling: %a */ @ " (fprintf_list ~sep:", " pp_print_string) (Scheduling.schedule_node nd)*)
280

    
281
let pp_imported_node fmt ind = 
282
  fprintf fmt "@[<v>%s %s (%a) returns (%a) %t@]"
283
    (if ind.nodei_stateless then "function" else "node")
284
    ind.nodei_id
285
    pp_node_args ind.nodei_inputs
286
    pp_node_args ind.nodei_outputs
287
    (fun fmt -> if ind.nodei_stateless then Format.fprintf fmt "stateless") 
288

    
289
let pp_const_list fmt clist = 
290
  fprintf_list ~sep:"@ " (fun fmt cdecl ->
291
    fprintf fmt "%s = %a;"
292
      cdecl.const_id pp_const cdecl.const_value) fmt clist
293

    
294
let pp_decl fmt decl =
295
  match decl.top_decl_desc with
296
  | Node nd -> fprintf fmt "%a@ " pp_node nd
297
  | ImportedNode ind ->
298
    fprintf fmt "imported %a;@ " pp_imported_node ind
299
  | Consts clist -> (fprintf fmt "const %a@ " pp_const_list clist)
300
  | Open (local, s) -> if local then fprintf fmt "open \"%s\"@ " s else fprintf fmt "open <%s>@ " s
301
  | Type tdef -> fprintf fmt "%a@ " pp_type_def tdef
302

    
303
let pp_prog fmt prog = 
304
  fprintf_list ~sep:"@ " pp_decl fmt prog
305

    
306
let pp_short_decl fmt decl =
307
  match decl.top_decl_desc with
308
  | Node nd -> fprintf fmt "node %s@ " nd.node_id
309
  | ImportedNode ind -> fprintf fmt "imported node %s" ind.nodei_id
310
  | Consts clist -> (fprintf fmt "const %a@ " pp_const_list clist)
311
  | Open (local, s) -> if local then fprintf fmt "open \"%s\"@ " s else fprintf fmt "open <%s>@ " s
312
  | Type tdef -> fprintf fmt "type %s;@ " tdef.ty_def_id
313

    
314
let pp_lusi fmt decl = 
315
  match decl.top_decl_desc with
316
  | Node nd ->  
317
    fprintf fmt 
318
      "@[<v>%s %s (%a) returns (%a);@ @]@ "
319
      (if Stateless.check_node decl then "function" else "node")
320
      nd.node_id
321
      pp_node_args nd.node_inputs
322
      pp_node_args nd.node_outputs
323
| Consts clist -> (fprintf fmt "const %a@ " pp_const_list clist)
324
| Open (local, s) -> if local then fprintf fmt "open \"%s\"@ " s else fprintf fmt "open <%s>@ " s
325
| Type tdef -> fprintf fmt "%a@ " pp_type_def tdef
326
| ImportedNode _ -> ()
327

    
328
let pp_lusi_header fmt filename prog =
329
  fprintf fmt "(* Generated Lustre Interface file from %s *)@." filename;
330
  fprintf fmt "(* by Lustre-C compiler version %s, %a *)@." Version.number pp_date (Unix.gmtime (Unix.time ()));
331
  fprintf fmt "(* Feel free to mask some of the definitions by removing them from this file. *)@.@.";
332
  List.iter (fprintf fmt "%a@." pp_lusi) prog    
333
  
334
(* Local Variables: *)
335
(* compile-command:"make -C .." *)
336
(* End: *)