Project

General

Profile

Download (15.1 KB) Statistics
| Branch: | Tag: | Revision:
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 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 print_dec_struct_ty_field fmt (label, cty) =
32
  fprintf fmt "%a : %a" pp_print_string label print_dec_ty cty
33
and print_dec_ty fmt cty =
34
  match (*get_repr_type*) cty with
35
  | Tydec_any -> fprintf fmt "Any"
36
  | Tydec_int -> fprintf fmt "int"
37
  | Tydec_real -> fprintf fmt "real"
38
  | Tydec_bool -> fprintf fmt "bool"
39
  | Tydec_clock cty' -> fprintf fmt "%a clock" print_dec_ty cty'
40
  | Tydec_const c -> fprintf fmt "%s" c
41
  | Tydec_enum taglist -> fprintf fmt "enum {%a }"
42
      (Utils.fprintf_list ~sep:", " pp_print_string) taglist
43
  | Tydec_struct fieldlist -> fprintf fmt "struct {%a }"
44
      (Utils.fprintf_list ~sep:"; " print_dec_struct_ty_field) fieldlist
45
  | Tydec_array (d, cty') -> fprintf fmt "%a^%a" print_dec_ty cty' Dimension.pp_dimension d
46

    
47
let pp_var_name fmt id = fprintf fmt "%s" id.var_id
48

    
49
let pp_eq_lhs = fprintf_list ~sep:", " pp_print_string
50

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

    
53
let pp_quantifiers fmt (q, vars) =
54
  match q with
55
    | Forall -> fprintf fmt "forall %a" (fprintf_list ~sep:"; " pp_var) vars 
56
    | Exists -> fprintf fmt "exists %a" (fprintf_list ~sep:"; " pp_var) vars 
57

    
58
let rec pp_struct_const_field fmt (label, c) =
59
  fprintf fmt "%a = %a;" pp_print_string label pp_const c
60
and pp_const fmt c = 
61
  match c with
62
    | Const_int i -> pp_print_int fmt i
63
    | 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 *)
64
    (* | Const_float r -> pp_print_float fmt r *)
65
    | Const_tag  t -> pp_print_string fmt t
66
    | Const_array ca -> Format.fprintf fmt "[%a]" (Utils.fprintf_list ~sep:"," pp_const) ca
67
    | Const_struct fl -> Format.fprintf fmt "{%a }" (Utils.fprintf_list ~sep:" " pp_struct_const_field) fl
68
    | Const_string s -> pp_print_string fmt ("\"" ^ s ^ "\"")
69

    
70

    
71
let rec pp_expr fmt expr =
72
  (match expr.expr_annot with 
73
  | None -> fprintf fmt "%t" 
74
  | Some ann -> fprintf fmt "(%a %t)" pp_expr_annot ann)
75
    (fun fmt -> 
76
      match expr.expr_desc with
77
    | Expr_const c -> pp_const fmt c
78
    | Expr_ident id -> Format.fprintf fmt "%s" id
79
    | Expr_array a -> fprintf fmt "[%a]" pp_tuple a
80
    | Expr_access (a, d) -> fprintf fmt "%a[%a]" pp_expr a Dimension.pp_dimension d
81
    | Expr_power (a, d) -> fprintf fmt "(%a^%a)" pp_expr a Dimension.pp_dimension d
82
    | Expr_tuple el -> fprintf fmt "(%a)" pp_tuple el
83
    | Expr_ite (c, t, e) -> fprintf fmt "(if %a then %a else %a)" pp_expr c pp_expr t pp_expr e
84
    | Expr_arrow (e1, e2) -> fprintf fmt "(%a -> %a)" pp_expr e1 pp_expr e2
85
    | Expr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_expr e1 pp_expr e2
86
    | Expr_pre e -> fprintf fmt "pre %a" pp_expr e
87
    | Expr_when (e, id, l) -> fprintf fmt "%a when %s(%s)" pp_expr e l id
88
    | Expr_merge (id, hl) -> 
89
      fprintf fmt "merge %s %a" id pp_handlers hl
90
    | Expr_appl (id, e, r) -> pp_app fmt id e r
91
    )
92
and pp_tuple fmt el =
93
 fprintf_list ~sep:"," pp_expr fmt el
94

    
95
and pp_handlers fmt hl =
96
  let pp_handler fmt (t, h) =
97
    fprintf fmt "(%s -> %a)" t pp_expr h
98
  in
99
 fprintf_list ~sep:" " pp_handler fmt hl
100

    
101
and pp_app fmt id e r =
102
  match r with
103
  | None -> pp_call fmt id e
104
  | Some c -> fprintf fmt "%t every (%a)" (fun fmt -> pp_call fmt id e) pp_expr c 
105

    
106
and pp_call fmt id e =
107
  match id, e.expr_desc with
108
  | "+", Expr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_expr e1 pp_expr e2
109
  | "uminus", _ -> fprintf fmt "(- %a)" pp_expr e
110
  | "-", Expr_tuple([e1;e2]) -> fprintf fmt "(%a - %a)" pp_expr e1 pp_expr e2
111
  | "*", Expr_tuple([e1;e2]) -> fprintf fmt "(%a * %a)" pp_expr e1 pp_expr e2
112
  | "/", Expr_tuple([e1;e2]) -> fprintf fmt "(%a / %a)" pp_expr e1 pp_expr e2
113
  | "mod", Expr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_expr e1 pp_expr e2
114
  | "&&", Expr_tuple([e1;e2]) -> fprintf fmt "(%a and %a)" pp_expr e1 pp_expr e2
115
  | "||", Expr_tuple([e1;e2]) -> fprintf fmt "(%a or %a)" pp_expr e1 pp_expr e2
116
  | "xor", Expr_tuple([e1;e2]) -> fprintf fmt "(%a xor %a)" pp_expr e1 pp_expr e2
117
  | "impl", 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
  | ">=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a >= %a)" pp_expr e1 pp_expr e2
122
  | "!=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a != %a)" pp_expr e1 pp_expr e2
123
  | "=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a = %a)" pp_expr e1 pp_expr e2
124
  | "not", _ -> fprintf fmt "(not %a)" pp_expr e
125
  | _, Expr_tuple _ -> fprintf fmt "%s %a" id pp_expr e
126
  | _ -> fprintf fmt "%s (%a)" id pp_expr e
127

    
128
and pp_eexpr fmt e =
129
  fprintf fmt "%a%t "
130
    (Utils.fprintf_list ~sep:"; " pp_quantifiers) e.eexpr_quantifiers
131
    (fun fmt -> match e.eexpr_quantifiers with [] -> () | _ -> fprintf fmt ";");
132
  match e.eexpr_normalized with
133
  | None -> fprintf fmt "%a"     
134
     pp_expr e.eexpr_qfexpr
135
  | Some (out, eqs, vdecls) -> (
136
    fprintf fmt "@[<v 0>";
137
    (match vdecls with [] -> () | _ -> fprintf fmt "vars @[<v 0>%a@];@ "
138
      (fprintf_list ~sep:"@ " pp_var) vdecls);
139
    fprintf_list ~sep:"@ " pp_node_eq fmt eqs;
140
    fprintf fmt "@ ";
141
    pp_var_name fmt out;
142
    fprintf fmt "@]@ "
143
)
144

    
145
and  pp_sf_value fmt e =
146
   fprintf fmt "%a"
147
     (* (Utils.fprintf_list ~sep:"; " pp_quantifiers) e.eexpr_quantifiers *)
148
     (* (fun fmt -> match e.eexpr_quantifiers *)
149
     (*             with [] -> () *)
150
     (*                | _ -> fprintf fmt ";") *)
151
     pp_expr e.eexpr_qfexpr
152

    
153
and pp_s_function fmt expr_ann =
154
  let pp_annot fmt (kwds, ee) =
155
    Format.fprintf fmt " %t : %a"
156
                   (fun fmt -> match kwds with
157
                               | [] -> assert false
158
                               | [x] -> Format.pp_print_string fmt x
159
                               | _ -> Format.fprintf fmt "%a" (fprintf_list ~sep:"/" Format.pp_print_string) kwds)
160
                   pp_sf_value ee
161
  in
162
  fprintf_list ~sep:"@ " pp_annot fmt expr_ann.annots
163

    
164
and pp_expr_annot fmt expr_ann =
165
  let pp_annot fmt (kwds, ee) =
166
    Format.fprintf fmt "(*! %t: %a; *)"
167
      (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)
168
      pp_eexpr ee
169
  in
170
  fprintf_list ~sep:"@ " pp_annot fmt expr_ann.annots
171
    
172
(*
173
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
174
*)
175
and pp_node_var fmt id =
176
  begin
177
    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;
178
    match id.var_dec_value with
179
    | None -> () 
180
    | Some v -> fprintf fmt " = %a" pp_expr v
181
  end 
182

    
183
and pp_node_args fmt a = fprintf_list ~sep:"; " pp_node_var fmt a 
184

    
185
and pp_node_eq fmt eq = 
186
  fprintf fmt "%a = %a;" 
187
    pp_eq_lhs eq.eq_lhs
188
    pp_expr eq.eq_rhs
189

    
190
and pp_restart fmt restart =
191
  Format.fprintf fmt "%s" (if restart then "restart" else "resume")
192

    
193
and pp_unless fmt (_, expr, restart, st) =
194
  Format.fprintf fmt "unless %a %a %s@ "
195
    pp_expr expr
196
    pp_restart restart
197
    st
198

    
199
and pp_until fmt (_, expr, restart, st) =
200
  Format.fprintf fmt "until %a %a %s@ "
201
    pp_expr expr
202
    pp_restart restart
203
    st
204

    
205
and pp_handler fmt handler =
206
  Format.fprintf fmt "state %s ->@ @[<v 2>  %a%alet@,@[<v 2>  %a@]@,tel%a@]"
207
    handler.hand_state
208
    (Utils.fprintf_list ~sep:"@," pp_unless) handler.hand_unless
209
    (fun fmt locals ->
210
      match locals with [] -> () | _ ->
211
	Format.fprintf fmt "@[<v 4>var %a@]@ " 
212
	  (Utils.fprintf_list ~sep:"@ " 
213
	     (fun fmt v -> Format.fprintf fmt "%a;" pp_node_var v))
214
	  locals)
215
    handler.hand_locals
216
    pp_node_stmts handler.hand_stmts
217
    (Utils.fprintf_list ~sep:"@," pp_until) handler.hand_until
218

    
219
and pp_node_stmt fmt stmt =
220
  match stmt with
221
  | Eq eq -> pp_node_eq fmt eq
222
  | Aut aut -> pp_node_aut fmt aut
223

    
224
and pp_node_stmts fmt stmts = fprintf_list ~sep:"@ " pp_node_stmt fmt stmts
225

    
226
and pp_node_aut fmt aut =
227
  Format.fprintf fmt "@[<v 0>automaton %s@,%a@]"
228
    aut.aut_id
229
    (Utils.fprintf_list ~sep:"@ " pp_handler) aut.aut_handlers
230

    
231
and pp_node_eqs fmt eqs = fprintf_list ~sep:"@ " pp_node_eq fmt eqs
232

    
233
let rec pp_var_struct_type_field fmt (label, tdesc) =
234
  fprintf fmt "%a : %a;" pp_print_string label pp_var_type_dec_desc tdesc
235
and pp_var_type_dec_desc fmt tdesc =
236
  match tdesc with 
237
  | Tydec_any -> fprintf fmt "<any>"
238
  | Tydec_int -> fprintf fmt "int"
239
  | Tydec_real -> fprintf fmt "real"
240
  (* | Tydec_float -> fprintf fmt "float" *)
241
  | Tydec_bool -> fprintf fmt "bool"
242
  | Tydec_clock t -> fprintf fmt "%a clock" pp_var_type_dec_desc t
243
  | Tydec_const t -> fprintf fmt "%s" t
244
  | Tydec_enum id_list -> fprintf fmt "enum {%a }" (fprintf_list ~sep:", " pp_print_string) id_list
245
  | Tydec_struct f_list -> fprintf fmt "struct {%a }" (fprintf_list ~sep:" " pp_var_struct_type_field) f_list
246
  | Tydec_array (s, t) -> fprintf fmt "%a^%a" pp_var_type_dec_desc t Dimension.pp_dimension s
247

    
248
let pp_var_type_dec fmt ty =
249
  pp_var_type_dec_desc fmt ty.ty_dec_desc
250

    
251
let pp_typedef fmt ty =
252
  fprintf fmt "type %s = %a;@ " ty.tydef_id pp_var_type_dec_desc ty.tydef_desc
253

    
254
let pp_typedec fmt ty =
255
  fprintf fmt "type %s;@ " ty.tydec_id
256

    
257
(* let rec pp_var_type fmt ty =  *)
258
(*   fprintf fmt "%a" (match ty.tdesc with  *)
259
(*     | Tvar | Tarrow | Tlink | Tunivar -> assert false *)
260
(*     | Tint -> pp_print_string fmt "int" *)
261
(*     | Treal -> pp_print_string fmt "real" *)
262
(*     | Tbool -> pp_print_string fmt "bool" *)
263
(*     | Trat -> pp_print_string fmt "rat" *)
264
(*     | Tclock -> pp_print_string fmt "clock"  *)
265
(*     | Ttuple tel -> fprintf_list ~sep:" * " pp_var_type fmt tel *)
266
(*   ) *)
267

    
268

    
269
let pp_spec fmt spec =
270
  fprintf fmt "@[<hov 2>(*@@ ";
271
  fprintf_list ~sep:"@;@@ " (fun fmt r -> fprintf fmt "requires %a;" pp_eexpr r) fmt spec.requires;
272
  fprintf_list ~sep:"@;@@ " (fun fmt r -> fprintf fmt "ensures %a; " pp_eexpr r) fmt spec.ensures;
273
  fprintf_list ~sep:"@;" (fun fmt (name, assumes, ensures, _) -> 
274
    fprintf fmt "behavior %s:@[@ %a@ %a@]" 
275
      name
276
      (fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "assumes %a;" pp_eexpr r)) assumes
277
      (fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "ensures %a;" pp_eexpr r)) ensures
278
  ) fmt spec.behaviors;
279
  fprintf fmt "@]*)";
280
  ()
281

    
282

    
283
let pp_asserts fmt asserts =
284
  match asserts with 
285
  | _::_ -> (
286
  fprintf fmt "(* Asserts definitions *)@ ";
287
  fprintf_list ~sep:"@ " (fun fmt assert_ -> 
288
    let expr = assert_.assert_expr in
289
    fprintf fmt "assert %a;" pp_expr expr 
290
  ) fmt asserts 
291
  )
292
  | _ -> ()
293
    
294
let pp_node fmt nd = 
295
fprintf fmt "@[<v 0>%a%t%s %s (%a) returns (%a)@.%a%alet@.@[<h 2>   @ @[<v>%a@ %a@ %a@]@ @]@.tel@]@."
296
  (fun fmt s -> match s with Some s -> pp_spec fmt s | _ -> ()) nd.node_spec
297
  (fun fmt -> match nd.node_spec with None -> () | Some _ -> Format.fprintf fmt "@.")
298
  (if nd.node_dec_stateless then "function" else "node")
299
  nd.node_id
300
  pp_node_args nd.node_inputs
301
  pp_node_args nd.node_outputs
302
  (fun fmt locals ->
303
  match locals with [] -> () | _ ->
304
    fprintf fmt "@[<v 4>var %a@]@ " 
305
      (fprintf_list ~sep:"@ " 
306
	 (fun fmt v -> fprintf fmt "%a;" pp_node_var v))
307
      locals
308
  ) nd.node_locals
309
  (fun fmt checks ->
310
  match checks with [] -> () | _ ->
311
    fprintf fmt "@[<v 4>check@ %a@]@ " 
312
      (fprintf_list ~sep:"@ " 
313
	 (fun fmt d -> fprintf fmt "%a" Dimension.pp_dimension d))
314
      checks
315
  ) nd.node_checks
316
  (fprintf_list ~sep:"@ " pp_expr_annot) nd.node_annot
317
  pp_node_stmts nd.node_stmts
318
  pp_asserts nd.node_asserts
319
(*fprintf fmt "@ /* Scheduling: %a */ @ " (fprintf_list ~sep:", " pp_print_string) (Scheduling.schedule_node nd)*)
320

    
321
let pp_imported_node fmt ind = 
322
  fprintf fmt "@[<v>%s %s (%a) returns (%a)@]"
323
    (if ind.nodei_stateless then "function" else "node")
324
    ind.nodei_id
325
    pp_node_args ind.nodei_inputs
326
    pp_node_args ind.nodei_outputs
327

    
328
let pp_const_decl fmt cdecl =
329
  fprintf fmt "%s = %a;" cdecl.const_id pp_const cdecl.const_value
330

    
331
let pp_const_decl_list fmt clist = 
332
  fprintf_list ~sep:"@ " pp_const_decl fmt clist
333

    
334
let pp_decl fmt decl =
335
  match decl.top_decl_desc with
336
  | Node nd -> fprintf fmt "%a@ " pp_node nd
337
  | ImportedNode ind ->
338
    fprintf fmt "imported %a;@ " pp_imported_node ind
339
  | Const c -> fprintf fmt "const %a@ " pp_const_decl c
340
  | Open (local, s) -> if local then fprintf fmt "#open \"%s\"@ " s else fprintf fmt "#open <%s>@ " s
341
  | TypeDef tdef -> fprintf fmt "%a@ " pp_typedef tdef
342

    
343
let pp_prog fmt prog = 
344
  fprintf_list ~sep:"@ " pp_decl fmt prog
345

    
346
let pp_short_decl fmt decl =
347
  match decl.top_decl_desc with
348
  | Node nd -> fprintf fmt "node %s@ " nd.node_id
349
  | ImportedNode ind -> fprintf fmt "imported node %s" ind.nodei_id
350
  | Const c -> fprintf fmt "const %a@ " pp_const_decl c
351
  | Open (local, s) -> if local then fprintf fmt "#open \"%s\"@ " s else fprintf fmt "#open <%s>@ " s
352
  | TypeDef tdef -> fprintf fmt "type %s;@ " tdef.tydef_id
353

    
354
let pp_lusi fmt decl = 
355
  match decl.top_decl_desc with
356
  | ImportedNode ind -> fprintf fmt "%a;@ " pp_imported_node ind
357
  | Const c -> fprintf fmt "const %a@ " pp_const_decl c
358
  | Open (local, s) -> if local then fprintf fmt "#open \"%s\"@ " s else fprintf fmt "#open <%s>@ " s
359
  | TypeDef tdef -> fprintf fmt "%a@ " pp_typedef tdef
360
  | Node _ -> assert false
361

    
362
let pp_lusi_header fmt basename prog =
363
  fprintf fmt "(* Generated Lustre Interface file from %s.lus *)@." basename;
364
  fprintf fmt "(* by Lustre-C compiler version %s, %a *)@." Version.number pp_date (Unix.gmtime (Unix.time ()));
365
  fprintf fmt "(* Feel free to mask some of the definitions by removing them from this file. *)@.@.";
366
  List.iter (fprintf fmt "%a@." pp_lusi) prog    
367

    
368
let pp_offset fmt offset =
369
  match offset with
370
  | Index i -> fprintf fmt "[%a]" Dimension.pp_dimension i
371
  | Field f -> fprintf fmt ".%s" f
372

    
373
(* Local Variables: *)
374
(* compile-command:"make -C .." *)
375
(* End: *)
(52-52/62)