Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / printers.ml @ c02d255e

History | View | Annotate | Download (12.8 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 rec pp_const fmt c = 
44
  match c with
45
    | Const_int i -> pp_print_int fmt i
46
    | Const_real r -> pp_print_string fmt r
47
    | Const_float r -> pp_print_float fmt r
48
    | Const_tag  t -> pp_print_string fmt t
49
    | Const_array ca -> Format.fprintf fmt "[%a]" (Utils.fprintf_list ~sep:"," pp_const) ca
50

    
51
and pp_var fmt id = fprintf fmt "%s: %a" id.var_id Types.print_ty id.var_type
52

    
53
and pp_expr fmt expr =
54
  match expr.expr_desc with
55
    | Expr_const c -> pp_const fmt c
56
    | Expr_ident id -> Format.fprintf fmt "%s" id
57
(*    | Expr_cst_array (c, e) -> fprintf fmt "%a^%a" pp_expr e pp_const c *)
58
    | Expr_array a -> fprintf fmt "[%a]" pp_tuple a
59
    | Expr_access (a, d) -> fprintf fmt "%a[%a]" pp_expr a Dimension.pp_dimension d
60
    | Expr_power (a, d) -> fprintf fmt "(%a^%a)" pp_expr a Dimension.pp_dimension d
61
    | Expr_tuple el -> fprintf fmt "(%a)" pp_tuple el
62
    | Expr_ite (c, t, e) -> fprintf fmt "(if %a then %a else %a)" pp_expr c pp_expr t pp_expr e
63
    | Expr_arrow (e1, e2) -> fprintf fmt "(%a -> %a)" pp_expr e1 pp_expr e2
64
    | Expr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_expr e1 pp_expr e2
65
    | Expr_pre e -> fprintf fmt "pre %a" pp_expr e
66
    | Expr_when (e, id, l) -> fprintf fmt "%a when %s(%s)" pp_expr e l id
67
    | Expr_merge (id, hl) -> 
68
      fprintf fmt "merge %s %a" id pp_handlers hl
69
    | Expr_appl (id, e, r) -> pp_app fmt id e r
70
    | Expr_uclock _
71
    | Expr_dclock _
72
    | Expr_phclock _ -> assert false
73

    
74
and pp_tuple fmt el =
75
 fprintf_list ~sep:"," pp_expr fmt el
76

    
77
and pp_handler fmt (t, h) =
78
 fprintf fmt "(%s -> %a)" t pp_expr h
79

    
80
and pp_handlers fmt hl =
81
 fprintf_list ~sep:" " pp_handler fmt hl
82

    
83
and pp_app fmt id e r =
84
  match r with
85
  | None ->
86
    (match id, e.expr_desc with
87
    | "+", Expr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_expr e1 pp_expr e2
88
    | "uminus", _ -> fprintf fmt "(- %a)" pp_expr e
89
    | "-", Expr_tuple([e1;e2]) -> fprintf fmt "(%a - %a)" pp_expr e1 pp_expr e2
90
    | "*", Expr_tuple([e1;e2]) -> fprintf fmt "(%a * %a)" pp_expr e1 pp_expr e2
91
    | "/", Expr_tuple([e1;e2]) -> fprintf fmt "(%a / %a)" pp_expr e1 pp_expr e2
92
    | "mod", Expr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_expr e1 pp_expr e2
93
    | "&&", Expr_tuple([e1;e2]) -> fprintf fmt "(%a and %a)" pp_expr e1 pp_expr e2
94
    | "||", Expr_tuple([e1;e2]) -> fprintf fmt "(%a or %a)" pp_expr e1 pp_expr e2
95
    | "xor", Expr_tuple([e1;e2]) -> fprintf fmt "(%a xor %a)" pp_expr e1 pp_expr e2
96
    | "impl", Expr_tuple([e1;e2]) -> fprintf fmt "(%a => %a)" pp_expr e1 pp_expr e2
97
    | "<", Expr_tuple([e1;e2]) -> fprintf fmt "(%a < %a)" pp_expr e1 pp_expr e2
98
    | "<=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a <= %a)" pp_expr e1 pp_expr e2
99
    | ">", Expr_tuple([e1;e2]) -> fprintf fmt "(%a > %a)" pp_expr e1 pp_expr e2
100
    | ">=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a >= %a)" pp_expr e1 pp_expr e2
101
    | "!=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a != %a)" pp_expr e1 pp_expr e2
102
    | "=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a = %a)" pp_expr e1 pp_expr e2
103
    | "not", _ -> fprintf fmt "(not %a)" pp_expr e
104
    | _, Expr_tuple _ -> fprintf fmt "%s %a" id pp_expr e
105
    | _ -> fprintf fmt "%s (%a)" id pp_expr e
106
)
107
  | Some (x, l) -> fprintf fmt "%s (%a) every %s(%s)" id pp_expr e l x 
108
	
109
let pp_node_eq fmt eq = 
110
  fprintf fmt "%a = %a;" 
111
    pp_eq_lhs eq.eq_lhs
112
    pp_expr eq.eq_rhs
113

    
114
let pp_node_eqs = fprintf_list ~sep:"@ " pp_node_eq 
115

    
116
let pp_node_args = fprintf_list ~sep:"; " pp_var 
117

    
118
let pp_var_type_dec fmt ty =
119
  let rec pp_var_type_dec_desc fmt tdesc =
120
  match tdesc with 
121
    | Tydec_any -> fprintf fmt "<any>"
122
    | Tydec_int -> fprintf fmt "int"
123
    | Tydec_real -> fprintf fmt "real"
124
    | Tydec_float -> fprintf fmt "float"
125
    | Tydec_bool -> fprintf fmt "bool"
126
    | Tydec_clock t -> fprintf fmt "%a clock" pp_var_type_dec_desc t
127
    | Tydec_const t -> fprintf fmt "%s" t
128
    | Tydec_enum id_list -> fprintf fmt "enum {%a }" (fprintf_list ~sep:", " pp_print_string) id_list
129
    | Tydec_array (s, t) -> fprintf fmt "%a^%a" pp_var_type_dec_desc t Dimension.pp_dimension s
130
in pp_var_type_dec_desc fmt ty.ty_dec_desc
131

    
132
(* let rec pp_var_type fmt ty =  *)
133
(*   fprintf fmt "%a" (match ty.tdesc with  *)
134
(*     | Tvar | Tarrow | Tlink | Tunivar -> assert false *)
135
(*     | Tint -> pp_print_string fmt "int" *)
136
(*     | Treal -> pp_print_string fmt "real" *)
137
(*     | Tbool -> pp_print_string fmt "bool" *)
138
(*     | Trat -> pp_print_string fmt "rat" *)
139
(*     | Tclock -> pp_print_string fmt "clock"  *)
140
(*     | Ttuple tel -> fprintf_list ~sep:" * " pp_var_type fmt tel *)
141
(*   ) *)
142

    
143

    
144
let pp_econst fmt c = 
145
  match c with
146
    | EConst_int i -> pp_print_int fmt i
147
    | EConst_real r -> pp_print_string fmt r
148
    | EConst_float r -> pp_print_float fmt r
149
    | EConst_bool b -> pp_print_bool fmt b
150
    | EConst_string s -> pp_print_string fmt ("\"" ^ s ^ "\"")
151

    
152
let rec pp_eexpr fmt eexpr = 
153
  match eexpr.eexpr_desc with
154
    | EExpr_const c -> pp_econst fmt c
155
    | EExpr_ident id -> pp_print_string fmt id
156
    | EExpr_tuple el -> fprintf_list ~sep:"," pp_eexpr fmt el
157
    | EExpr_arrow (e1, e2) -> fprintf fmt "%a -> %a" pp_eexpr e1 pp_eexpr e2
158
    | EExpr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_eexpr e1 pp_eexpr e2
159
    (* | EExpr_concat (e1, e2) -> fprintf fmt "%a::%a" pp_eexpr e1 pp_eexpr e2 *)
160
    (* | EExpr_tail e -> fprintf fmt "tail %a" pp_eexpr e *)
161
    | EExpr_pre e -> fprintf fmt "pre %a" pp_eexpr e
162
    | EExpr_when (e, id) -> fprintf fmt "%a when %s" pp_eexpr e id
163
    | EExpr_merge (id, e1, e2) -> 
164
      fprintf fmt "merge (%s, %a, %a)" id pp_eexpr e1 pp_eexpr e2
165
    | EExpr_appl (id, e, r) -> pp_eapp fmt id e r
166
    | EExpr_forall (vars, e) -> fprintf fmt "forall %a; %a" pp_node_args vars pp_eexpr e 
167
    | EExpr_exists (vars, e) -> fprintf fmt "exists %a; %a" pp_node_args vars pp_eexpr e 
168

    
169

    
170
    (* | EExpr_whennot _ *)
171
    (* | EExpr_uclock _ *)
172
    (* | EExpr_dclock _ *)
173
    (* | EExpr_phclock _ -> assert false *)
174
and pp_eapp fmt id e r =
175
  match r with
176
  | None ->
177
    (match id, e.eexpr_desc with
178
    | "+", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_eexpr e1 pp_eexpr e2
179
    | "uminus", _ -> fprintf fmt "(- %a)" pp_eexpr e
180
    | "-", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a - %a)" pp_eexpr e1 pp_eexpr e2
181
    | "*", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a * %a)" pp_eexpr e1 pp_eexpr e2
182
    | "/", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a / %a)" pp_eexpr e1 pp_eexpr e2
183
    | "mod", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_eexpr e1 pp_eexpr e2
184
    | "&&", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a && %a)" pp_eexpr e1 pp_eexpr e2
185
    | "||", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a || %a)" pp_eexpr e1 pp_eexpr e2
186
    | "xor", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a ^^ %a)" pp_eexpr e1 pp_eexpr e2
187
    | "impl", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a ==> %a)" pp_eexpr e1 pp_eexpr e2
188
    | "<", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a < %a)" pp_eexpr e1 pp_eexpr e2
189
    | "<=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a <= %a)" pp_eexpr e1 pp_eexpr e2
190
    | ">", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a > %a)" pp_eexpr e1 pp_eexpr e2
191
    | ">=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a >= %a)" pp_eexpr e1 pp_eexpr e2
192
    | "!=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a != %a)" pp_eexpr e1 pp_eexpr e2
193
    | "=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a == %a)" pp_eexpr e1 pp_eexpr e2
194
    | "not", _ -> fprintf fmt "(! %a)" pp_eexpr e
195
    | "ite", EExpr_tuple([e1;e2;e3]) -> fprintf fmt "(if %a then %a else %a)" pp_eexpr e1 pp_eexpr e2 pp_eexpr e3
196
    | _ -> fprintf fmt "%s (%a)" id pp_eexpr e)
197
  | Some x -> fprintf fmt "%s (%a) every %s" id pp_eexpr e x 
198

    
199
  
200
let pp_ensures fmt e =
201
  match e with
202
    | EnsuresExpr e -> fprintf fmt "ensures %a;@ " pp_eexpr e
203
    | SpecObserverNode (name, args) -> fprintf fmt "observer %s (%a);@ " name (fprintf_list ~sep:", " pp_eexpr) args
204
 
205
let pp_spec fmt spec =
206
  fprintf fmt "@[<hov 2>(*@@ ";
207
  fprintf_list ~sep:"" (fun fmt r -> fprintf fmt "requires %a;@ " pp_eexpr r) fmt spec.requires;
208
  fprintf_list ~sep:"" pp_ensures fmt spec.ensures;
209
  fprintf_list ~sep:"@ " (fun fmt (name, assumes, requires) -> 
210
    fprintf fmt "behavior %s:@[@ %a@ %a@]" 
211
      name
212
      (fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "assumes %a;" pp_eexpr r)) assumes
213
      (fprintf_list ~sep:"@ " pp_ensures) requires
214
  ) fmt spec.behaviors;
215
  fprintf fmt "@]*)";
216
  ()
217

    
218
let pp_node fmt nd = 
219
fprintf fmt "@[<v 0>%a%tnode %s (%a) returns (%a)@.%a%alet@.@[<h 2>   @ @[%a@]@ @]@.tel@]@."
220
  (fun fmt s -> match s with Some s -> pp_spec fmt s | _ -> ()) nd.node_spec
221
  (fun fmt -> match nd.node_spec with None -> () | Some _ -> Format.fprintf fmt "@.") 
222
  nd.node_id
223
  pp_node_args nd.node_inputs
224
  pp_node_args nd.node_outputs
225
  (fun fmt locals ->
226
  match locals with [] -> () | _ ->
227
    fprintf fmt "@[<v 4>var %a@]@ " 
228
      (fprintf_list ~sep:"@ " 
229
	 (fun fmt v -> fprintf fmt "%a;" pp_var v))
230
      locals
231
  ) nd.node_locals
232
  (fun fmt checks ->
233
  match checks with [] -> () | _ ->
234
    fprintf fmt "@[<v 4>check@ %a@]@ " 
235
      (fprintf_list ~sep:"@ " 
236
	 (fun fmt d -> fprintf fmt "%a" Dimension.pp_dimension d))
237
      checks
238
  ) nd.node_checks
239
  pp_node_eqs nd.node_eqs
240
(*fprintf fmt "@ /* Scheduling: %a */ @ " (fprintf_list ~sep:", " pp_print_string) (Scheduling.schedule_node nd)*)
241

    
242
let pp_imported_node fmt ind = 
243
  fprintf fmt "@[<v>node %s (%a) returns (%a) %t@]"
244
    ind.nodei_id
245
    pp_node_args ind.nodei_inputs
246
    pp_node_args ind.nodei_outputs
247
    (fun fmt -> if ind.nodei_stateless then Format.fprintf fmt "stateless") 
248

    
249
let pp_imported_fun fmt ind = 
250
  fprintf fmt "@[<v>function %s (%a) returns (%a)@]"
251
    ind.fun_id
252
    pp_node_args ind.fun_inputs
253
    pp_node_args ind.fun_outputs
254

    
255
let pp_decl fmt decl =
256
  match decl.top_decl_desc with
257
  | Node nd -> fprintf fmt "%a@ " pp_node nd
258
  | ImportedNode ind ->
259
    fprintf fmt "imported %a;@ " pp_imported_node ind
260
  | ImportedFun ind ->
261
    fprintf fmt "%a;@ " pp_imported_fun ind
262
  | Consts clist -> (
263
    fprintf fmt "const %a@ " 
264
      (fprintf_list ~sep:"@ " (fun fmt cdecl ->
265
	fprintf fmt "%s = %a;"
266
	  cdecl.const_id pp_const cdecl.const_value)) clist)
267
  | Open s -> fprintf fmt "open \"%s\"" s
268

    
269

    
270
let pp_prog fmt prog = 
271
  fprintf_list ~sep:"@ " pp_decl fmt prog
272

    
273
let pp_short_decl fmt decl =
274
  match decl.top_decl_desc with
275
  | Node nd -> fprintf fmt "node %s@ " nd.node_id
276
  | ImportedNode ind -> fprintf fmt "imported node %s" ind.nodei_id
277
  | ImportedFun ind -> fprintf fmt "function %s" ind.fun_id
278
  | Consts clist -> (
279
    fprintf fmt "const %a@ " 
280
      (fprintf_list ~sep:"@ " (fun fmt cdecl ->
281
	pp_print_string fmt cdecl.const_id)) clist)
282
  | Open s -> fprintf fmt "open \"%s\"" s
283

    
284
let pp_lusi fmt decl = 
285
  match decl.top_decl_desc with
286
  | Node nd ->  
287
    fprintf fmt 
288
      "@[<v>node %s (%a) returns (%a);@ @]@ "
289
      nd.node_id
290
      pp_node_args nd.node_inputs
291
      pp_node_args nd.node_outputs
292
  | ImportedNode _ | ImportedFun _ | Consts _ | Open _ -> ()
293

    
294

    
295

    
296

    
297
let pp_lusi_header fmt filename prog =
298
  fprintf fmt "(* Generated Lustre Interface file from %s *)@." filename;
299
  fprintf fmt "(* generated by Lustre-C compiler version %s, %a *)@." Version.number pp_date (Unix.gmtime (Unix.time ()));
300
  fprintf fmt "(* feel free to mask some of the nodes by removing them from this file. *)@.@.";
301
  List.iter (fprintf fmt "%a@." pp_lusi) prog    
302
  
303
(* Local Variables: *)
304
(* compile-command:"make -C .." *)
305
(* End: *)