Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / printers.ml @ 36454535

History | View | Annotate | Download (14.5 KB)

1
(* ----------------------------------------------------------------------------
2
 * SchedMCore - A MultiCore Scheduling Framework
3
 * Copyright (C) 2009-2011, ONERA, Toulouse, FRANCE - LIFL, Lille, FRANCE
4
 *
5
 * This file is part of Prelude
6
 *
7
 * Prelude is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public License
9
 * as published by the Free Software Foundation ; either version 2 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * Prelude is distributed in the hope that it will be useful, but
13
 * WITHOUT ANY WARRANTY ; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this program ; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
 * USA
21
 *---------------------------------------------------------------------------- *)
22
open Corelang
23
open LustreSpec
24
open Format
25
open Utils
26

    
27
(* Prints [v] as [pp_fun] would do, but adds a backslash at each end of line,
28
   following the C convention for multiple lines macro *)
29
let pp_as_c_macro pp_fun fmt v =
30
  let (out, flush, newline, spaces) = pp_get_all_formatter_output_functions fmt () in
31
  let macro_newline () = (out "\\" 0 1; newline ()) in
32
  begin
33
    pp_set_all_formatter_output_functions fmt out flush macro_newline spaces;
34
    pp_fun fmt v;
35
    pp_set_all_formatter_output_functions fmt out flush newline spaces;
36
  end
37

    
38

    
39
let pp_var_name fmt id = fprintf fmt "%s" id.var_id
40

    
41
let pp_eq_lhs = fprintf_list ~sep:", " pp_print_string
42

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

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

    
47
let pp_node_args = fprintf_list ~sep:"; " pp_node_var 
48

    
49
let pp_quantifiers fmt (q, vars) =
50
  match q with
51
    | Forall -> fprintf fmt "forall %a" (fprintf_list ~sep:"; " pp_var) vars 
52
    | Exists -> fprintf fmt "exists %a" (fprintf_list ~sep:"; " pp_var) vars 
53

    
54
(*
55
let pp_econst fmt c = 
56
  match c with
57
    | EConst_int i -> pp_print_int fmt i
58
    | EConst_real r -> pp_print_string fmt r
59
    | EConst_float r -> pp_print_float fmt r
60
    | EConst_tag  t -> pp_print_string fmt t
61
    | EConst_string s -> pp_print_string fmt ("\"" ^ s ^ "\"")
62

    
63

    
64
let rec pp_eexpr fmt eexpr = 
65
  match eexpr.eexpr_desc with
66
    | EExpr_const c -> pp_econst fmt c
67
    | EExpr_ident id -> pp_print_string fmt id
68
    | EExpr_tuple el -> fprintf_list ~sep:"," pp_eexpr fmt el
69
    | EExpr_arrow (e1, e2) -> fprintf fmt "%a -> %a" pp_eexpr e1 pp_eexpr e2
70
    | EExpr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_eexpr e1 pp_eexpr e2
71
    (* | EExpr_concat (e1, e2) -> fprintf fmt "%a::%a" pp_eexpr e1 pp_eexpr e2 *)
72
    (* | EExpr_tail e -> fprintf fmt "tail %a" pp_eexpr e *)
73
    | EExpr_pre e -> fprintf fmt "pre %a" pp_eexpr e
74
    | EExpr_when (e, id) -> fprintf fmt "%a when %s" pp_eexpr e id
75
    | EExpr_merge (id, e1, e2) -> 
76
      fprintf fmt "merge (%s, %a, %a)" id pp_eexpr e1 pp_eexpr e2
77
    | EExpr_appl (id, e, r) -> pp_eapp fmt id e r
78
    | EExpr_forall (vars, e) -> fprintf fmt "forall %a; %a" pp_node_args vars pp_eexpr e 
79
    | EExpr_exists (vars, e) -> fprintf fmt "exists %a; %a" pp_node_args vars pp_eexpr e 
80

    
81

    
82
    (* | EExpr_whennot _ *)
83
    (* | EExpr_uclock _ *)
84
    (* | EExpr_dclock _ *)
85
    (* | EExpr_phclock _ -> assert false *)
86
and pp_eapp fmt id e r =
87
  match r with
88
  | None ->
89
    (match id, e.eexpr_desc with
90
    | "+", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_eexpr e1 pp_eexpr e2
91
    | "uminus", _ -> fprintf fmt "(- %a)" pp_eexpr e
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
    | "mod", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_eexpr e1 pp_eexpr e2
96
    | "&&", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a && %a)" pp_eexpr e1 pp_eexpr e2
97
    | "||", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a || %a)" pp_eexpr e1 pp_eexpr e2
98
    | "xor", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a ^^ %a)" pp_eexpr e1 pp_eexpr e2
99
    | "impl", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a ==> %a)" pp_eexpr e1 pp_eexpr e2
100
    | "<", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a < %a)" pp_eexpr e1 pp_eexpr e2
101
    | "<=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a <= %a)" pp_eexpr e1 pp_eexpr e2
102
    | ">", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a > %a)" pp_eexpr e1 pp_eexpr e2
103
    | ">=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a >= %a)" pp_eexpr e1 pp_eexpr e2
104
    | "!=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a != %a)" pp_eexpr e1 pp_eexpr e2
105
    | "=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a == %a)" pp_eexpr e1 pp_eexpr e2
106
    | "not", _ -> fprintf fmt "(! %a)" pp_eexpr e
107
    | "ite", EExpr_tuple([e1;e2;e3]) -> fprintf fmt "(if %a then %a else %a)" pp_eexpr e1 pp_eexpr e2 pp_eexpr e3
108
    | _ -> fprintf fmt "%s (%a)" id pp_eexpr e)
109
  | Some x -> fprintf fmt "%s (%a) every %s" id pp_eexpr e x 
110
*)
111

    
112

    
113
let rec pp_struct_const_field fmt (label, c) =
114
  fprintf fmt "%a = %a;" pp_print_string label pp_const c
115
and pp_const fmt c = 
116
  match c with
117
    | Const_int i -> pp_print_int fmt i
118
    | Const_real r -> pp_print_string fmt r
119
    | Const_float r -> pp_print_float fmt r
120
    | Const_tag  t -> pp_print_string fmt t
121
    | Const_array ca -> Format.fprintf fmt "[%a]" (Utils.fprintf_list ~sep:"," pp_const) ca
122
    | Const_struct fl -> Format.fprintf fmt "{%a }" (Utils.fprintf_list ~sep:" " pp_struct_const_field) fl
123
    | Const_string s -> pp_print_string fmt ("\"" ^ s ^ "\"")
124

    
125

    
126
let rec pp_expr fmt expr =
127
  (match expr.expr_annot with 
128
  | None -> fprintf fmt "%t" 
129
  | Some ann -> fprintf fmt "(%a %t)" pp_expr_annot ann)
130
    (fun fmt -> 
131
      match expr.expr_desc with
132
    | Expr_const c -> pp_const fmt c
133
    | Expr_ident id -> Format.fprintf fmt "%s" id
134
    | Expr_array a -> fprintf fmt "[%a]" pp_tuple a
135
    | Expr_access (a, d) -> fprintf fmt "%a[%a]" pp_expr a Dimension.pp_dimension d
136
    | Expr_power (a, d) -> fprintf fmt "(%a^%a)" pp_expr a Dimension.pp_dimension d
137
    | Expr_tuple el -> fprintf fmt "(%a)" pp_tuple el
138
    | Expr_ite (c, t, e) -> fprintf fmt "(if %a then %a else %a)" pp_expr c pp_expr t pp_expr e
139
    | Expr_arrow (e1, e2) -> fprintf fmt "(%a -> %a)" pp_expr e1 pp_expr e2
140
    | Expr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_expr e1 pp_expr e2
141
    | Expr_pre e -> fprintf fmt "pre %a" pp_expr e
142
    | Expr_when (e, id, l) -> fprintf fmt "%a when %s(%s)" pp_expr e l id
143
    | Expr_merge (id, hl) -> 
144
      fprintf fmt "merge %s %a" id pp_handlers hl
145
    | Expr_appl (id, e, r) -> pp_app fmt id e r
146
    )
147
and pp_tuple fmt el =
148
 fprintf_list ~sep:"," pp_expr fmt el
149

    
150
and pp_handler fmt (t, h) =
151
 fprintf fmt "(%s -> %a)" t pp_expr h
152

    
153
and pp_handlers fmt hl =
154
 fprintf_list ~sep:" " pp_handler fmt hl
155

    
156
and pp_app fmt id e r =
157
  match r with
158
  | None ->
159
    (match id, e.expr_desc with
160
    | "+", Expr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_expr e1 pp_expr e2
161
    | "uminus", _ -> fprintf fmt "(- %a)" pp_expr e
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
    | "mod", Expr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_expr e1 pp_expr e2
166
    | "&&", Expr_tuple([e1;e2]) -> fprintf fmt "(%a and %a)" pp_expr e1 pp_expr e2
167
    | "||", Expr_tuple([e1;e2]) -> fprintf fmt "(%a or %a)" pp_expr e1 pp_expr e2
168
    | "xor", Expr_tuple([e1;e2]) -> fprintf fmt "(%a xor %a)" pp_expr e1 pp_expr e2
169
    | "impl", Expr_tuple([e1;e2]) -> fprintf fmt "(%a => %a)" pp_expr e1 pp_expr e2
170
    | "<", Expr_tuple([e1;e2]) -> fprintf fmt "(%a < %a)" pp_expr e1 pp_expr e2
171
    | "<=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a <= %a)" pp_expr e1 pp_expr e2
172
    | ">", Expr_tuple([e1;e2]) -> fprintf fmt "(%a > %a)" pp_expr e1 pp_expr e2
173
    | ">=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a >= %a)" pp_expr e1 pp_expr e2
174
    | "!=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a != %a)" pp_expr e1 pp_expr e2
175
    | "=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a = %a)" pp_expr e1 pp_expr e2
176
    | "not", _ -> fprintf fmt "(not %a)" pp_expr e
177
    | _, Expr_tuple _ -> fprintf fmt "%s %a" id pp_expr e
178
    | _ -> fprintf fmt "%s (%a)" id pp_expr e
179
)
180
  | Some (x, l) -> fprintf fmt "%s (%a) every %s(%s)" id pp_expr e l x 
181

    
182

    
183

    
184
and pp_eexpr fmt e =
185
  fprintf fmt "%a%t %a"
186
    (Utils.fprintf_list ~sep:"; " pp_quantifiers) e.eexpr_quantifiers
187
    (fun fmt -> match e.eexpr_quantifiers with [] -> () | _ -> fprintf fmt ";")
188
    pp_expr e.eexpr_qfexpr
189

    
190

    
191
and pp_expr_annot fmt expr_ann =
192
  let pp_annot fmt (kwds, ee) =
193
    Format.fprintf fmt "(*! %t: %a *)"
194
      (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)
195
      pp_eexpr ee
196
  in
197
  fprintf_list ~sep:"@ " pp_annot fmt expr_ann.annots
198
    
199
	
200
let pp_node_eq fmt eq = 
201
  fprintf fmt "%a = %a;" 
202
    pp_eq_lhs eq.eq_lhs
203
    pp_expr eq.eq_rhs
204

    
205
let pp_node_eqs = fprintf_list ~sep:"@ " pp_node_eq 
206

    
207

    
208
let pp_var_type_dec fmt ty =
209
  let rec pp_var_struct_type_field fmt (label, tdesc) =
210
    fprintf fmt "%a : %a" pp_var_type_dec_desc tdesc pp_print_string label
211
  and pp_var_type_dec_desc fmt tdesc =
212
  match tdesc with 
213
    | Tydec_any -> fprintf fmt "<any>"
214
    | Tydec_int -> fprintf fmt "int"
215
    | Tydec_real -> fprintf fmt "real"
216
    | Tydec_float -> fprintf fmt "float"
217
    | Tydec_bool -> fprintf fmt "bool"
218
    | Tydec_clock t -> fprintf fmt "%a clock" pp_var_type_dec_desc t
219
    | Tydec_const t -> fprintf fmt "%s" t
220
    | Tydec_enum id_list -> fprintf fmt "enum {%a }" (fprintf_list ~sep:", " pp_print_string) id_list
221
    | Tydec_struct f_list -> fprintf fmt "struct {%a }" (fprintf_list ~sep:"; " pp_var_struct_type_field) f_list
222
    | Tydec_array (s, t) -> fprintf fmt "%a^%a" pp_var_type_dec_desc t Dimension.pp_dimension s
223
in pp_var_type_dec_desc fmt ty.ty_dec_desc
224

    
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

    
237
let pp_spec fmt spec =
238
  fprintf fmt "@[<hov 2>(*@@ ";
239
  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
    fprintf fmt "behavior %s:@[@ %a@ %a@]" 
243
      name
244
      (fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "assumes %a;" pp_eexpr r)) assumes
245
      (fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "ensures %a;" pp_eexpr r)) ensures
246
  ) fmt spec.behaviors;
247
  fprintf fmt "@]*)";
248
  ()
249

    
250

    
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
let pp_node fmt nd = 
263
fprintf fmt "@[<v 0>%a%t%s %s (%a) returns (%a)@.%a%alet@.@[<h 2>   @ @[<v>%a@ %a@ %a@]@ @]@.tel@]@."
264
  (fun fmt s -> match s with Some s -> pp_spec fmt s | _ -> ()) nd.node_spec
265
  (fun fmt -> match nd.node_spec with None -> () | Some _ -> Format.fprintf fmt "@.")
266
  (if nd.node_dec_stateless then "function" else "node")
267
  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
	 (fun fmt v -> fprintf fmt "%a;" pp_node_var v))
275
      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
  (fprintf_list ~sep:"@ " pp_expr_annot) nd.node_annot
285
  pp_node_eqs nd.node_eqs
286
  pp_asserts nd.node_asserts
287
(*fprintf fmt "@ /* Scheduling: %a */ @ " (fprintf_list ~sep:", " pp_print_string) (Scheduling.schedule_node nd)*)
288

    
289
let pp_imported_node fmt ind = 
290
  fprintf fmt "@[<v>%s %s (%a) returns (%a) %t@]"
291
    (if ind.nodei_stateless then "function" else "node")
292
    ind.nodei_id
293
    pp_node_args ind.nodei_inputs
294
    pp_node_args ind.nodei_outputs
295
    (fun fmt -> if ind.nodei_stateless then Format.fprintf fmt "stateless") 
296

    
297
let pp_const_list fmt clist = 
298
  fprintf_list ~sep:"@ " (fun fmt cdecl ->
299
    fprintf fmt "%s = %a;"
300
      cdecl.const_id pp_const cdecl.const_value) fmt clist
301

    
302
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
  | Consts clist -> (fprintf fmt "const %a@ " pp_const_list clist)
308
  | Open (local, s) -> if local then fprintf fmt "open \"%s\"" s else fprintf fmt "open <%s>" s
309

    
310

    
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
  | Consts clist -> (fprintf fmt "const %a@ " pp_const_list clist)
319
    | Open (local, s) -> if local then fprintf fmt "open \"%s\"" s else fprintf fmt "open <%s>" s
320

    
321
let pp_lusi fmt decl = 
322
  match decl.top_decl_desc with
323
  | Node nd ->  
324
    fprintf fmt 
325
      "@[<v>%s %s (%a) returns (%a);@ @]@ "
326
      (if Stateless.check_node decl then "function" else "node")
327
      nd.node_id
328
      pp_node_args nd.node_inputs
329
      pp_node_args nd.node_outputs
330
| Consts clist -> (fprintf fmt "const %a@ " pp_const_list clist)
331
| ImportedNode _ | Open _ -> ()
332

    
333

    
334

    
335

    
336
let pp_lusi_header fmt filename prog =
337
  fprintf fmt "(* Generated Lustre Interface file from %s *)@." filename;
338
  fprintf fmt "(* generated by Lustre-C compiler version %s, %a *)@." Version.number pp_date (Unix.gmtime (Unix.time ()));
339
  fprintf fmt "(* feel free to mask some of the nodes by removing them from this file. *)@.@.";
340
  List.iter (fprintf fmt "%a@." pp_lusi) prog    
341
  
342
(* Local Variables: *)
343
(* compile-command:"make -C .." *)
344
(* End: *)