Project

General

Profile

Download (13.9 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 (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
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 -> fprintf fmt "real"
34
  | Tydec_bool -> fprintf fmt "bool"
35
  | Tydec_clock cty' -> fprintf fmt "%a clock" print_dec_ty cty'
36
  | Tydec_const c -> fprintf fmt "%s" c
37
  | Tydec_enum taglist -> fprintf fmt "enum {%a }"
38
      (Utils.fprintf_list ~sep:", " pp_print_string) taglist
39
  | Tydec_struct fieldlist -> fprintf fmt "struct {%a }"
40
      (Utils.fprintf_list ~sep:"; " print_dec_struct_ty_field) fieldlist
41
  | Tydec_array (d, cty') -> fprintf fmt "%a^%a" print_dec_ty cty' Dimension.pp_dimension d
42

    
43
let pp_var_name fmt id = fprintf fmt "%s" id.var_id
44

    
45
let pp_eq_lhs = fprintf_list ~sep:", " pp_print_string
46

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

    
66

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

    
91
and pp_handler fmt (t, h) =
92
 fprintf fmt "(%s -> %a)" t pp_expr h
93

    
94
and pp_handlers fmt hl =
95
 fprintf_list ~sep:" " pp_handler fmt hl
96

    
97
and pp_app fmt id e r =
98
  match r with
99
  | None -> pp_call fmt id e
100
  | Some c -> fprintf fmt "%t every (%a)" (fun fmt -> pp_call fmt id e) pp_expr c 
101

    
102
and pp_call fmt id e =
103
  match id, e.expr_desc with
104
  | "+", Expr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_expr e1 pp_expr e2
105
  | "uminus", _ -> fprintf fmt "(- %a)" pp_expr e
106
  | "-", Expr_tuple([e1;e2]) -> fprintf fmt "(%a - %a)" pp_expr e1 pp_expr e2
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
  | "mod", Expr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_expr e1 pp_expr e2
110
  | "&&", Expr_tuple([e1;e2]) -> fprintf fmt "(%a and %a)" pp_expr e1 pp_expr e2
111
  | "||", Expr_tuple([e1;e2]) -> fprintf fmt "(%a or %a)" pp_expr e1 pp_expr e2
112
  | "xor", Expr_tuple([e1;e2]) -> fprintf fmt "(%a xor %a)" pp_expr e1 pp_expr e2
113
  | "impl", Expr_tuple([e1;e2]) -> fprintf fmt "(%a => %a)" pp_expr e1 pp_expr e2
114
  | "<", 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
  | "not", _ -> fprintf fmt "(not %a)" pp_expr e
121
  | _, Expr_tuple _ -> fprintf fmt "%s %a" id pp_expr e
122
  | _ -> fprintf fmt "%s (%a)" id pp_expr e
123

    
124
and pp_eexpr fmt e =
125
  fprintf fmt "%a%t %a"
126
    (Utils.fprintf_list ~sep:"; " pp_quantifiers) e.eexpr_quantifiers
127
    (fun fmt -> match e.eexpr_quantifiers with [] -> () | _ -> fprintf fmt ";")
128
    pp_expr e.eexpr_qfexpr
129

    
130

    
131
and pp_expr_annot fmt expr_ann =
132
  let pp_annot fmt (kwds, ee) =
133
    Format.fprintf fmt "(*! %t: %a; *)"
134
      (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)
135
      pp_eexpr ee
136
  in
137
  fprintf_list ~sep:"@ " pp_annot fmt expr_ann.annots
138
    
139
(*
140
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
141
*)
142
let pp_node_var fmt id =
143
  begin
144
    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;
145
    match id.var_dec_value with
146
    | None -> () 
147
    | Some v -> fprintf fmt " = %a" pp_expr v
148
  end 
149

    
150
let pp_node_args = fprintf_list ~sep:"; " pp_node_var 
151

    
152
let pp_node_eq fmt eq = 
153
  fprintf fmt "%a = %a;" 
154
    pp_eq_lhs eq.eq_lhs
155
    pp_expr eq.eq_rhs
156

    
157
let pp_restart fmt restart =
158
  Format.fprintf fmt "%s" (if restart then "restart" else "resume")
159

    
160
let pp_unless fmt (_, expr, restart, st) =
161
  Format.fprintf fmt "unless %a %a %s@ "
162
    pp_expr expr
163
    pp_restart restart
164
    st
165

    
166
let pp_until fmt (_, expr, restart, st) =
167
  Format.fprintf fmt "until %a %a %s@ "
168
    pp_expr expr
169
    pp_restart restart
170
    st
171

    
172
let rec pp_handler fmt handler =
173
  Format.fprintf fmt "state %s ->@ @[<v 2>  %a%alet@,@[<v 2>  %a@]@,tel%a@]"
174
    handler.hand_state
175
    (Utils.fprintf_list ~sep:"@," pp_unless) handler.hand_unless
176
    (fun fmt locals ->
177
      match locals with [] -> () | _ ->
178
	Format.fprintf fmt "@[<v 4>var %a@]@ " 
179
	  (Utils.fprintf_list ~sep:"@ " 
180
	     (fun fmt v -> Format.fprintf fmt "%a;" pp_node_var v))
181
	  locals)
182
    handler.hand_locals
183
    pp_node_stmts handler.hand_stmts
184
    (Utils.fprintf_list ~sep:"@," pp_until) handler.hand_until
185

    
186
and pp_node_stmt fmt stmt =
187
  match stmt with
188
  | Eq eq -> pp_node_eq fmt eq
189
  | Aut aut -> pp_node_aut fmt aut
190

    
191
and pp_node_stmts fmt stmts = fprintf_list ~sep:"@ " pp_node_stmt fmt stmts
192

    
193
and pp_node_aut fmt aut =
194
  Format.fprintf fmt "@[<v 0>automaton %s@,%a@]"
195
    aut.aut_id
196
    (Utils.fprintf_list ~sep:"@ " pp_handler) aut.aut_handlers
197

    
198
and pp_node_eqs fmt eqs = fprintf_list ~sep:"@ " pp_node_eq fmt eqs
199

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

    
215
let pp_var_type_dec fmt ty =
216
  pp_var_type_dec_desc fmt ty.ty_dec_desc
217

    
218
let pp_typedef fmt ty =
219
  fprintf fmt "type %s = %a;@ " ty.tydef_id pp_var_type_dec_desc ty.tydef_desc
220

    
221
let pp_typedec fmt ty =
222
  fprintf fmt "type %s;@ " ty.tydec_id
223

    
224
(* let rec pp_var_type fmt ty =  *)
225
(*   fprintf fmt "%a" (match ty.tdesc with  *)
226
(*     | Tvar | Tarrow | Tlink | Tunivar -> assert false *)
227
(*     | Tint -> pp_print_string fmt "int" *)
228
(*     | Treal -> pp_print_string fmt "real" *)
229
(*     | Tbool -> pp_print_string fmt "bool" *)
230
(*     | Trat -> pp_print_string fmt "rat" *)
231
(*     | Tclock -> pp_print_string fmt "clock"  *)
232
(*     | Ttuple tel -> fprintf_list ~sep:" * " pp_var_type fmt tel *)
233
(*   ) *)
234

    
235

    
236
let pp_spec fmt spec =
237
  fprintf fmt "@[<hov 2>(*@@ ";
238
  fprintf_list ~sep:"@;@@ " (fun fmt r -> fprintf fmt "requires %a;" pp_eexpr r) fmt spec.requires;
239
  fprintf_list ~sep:"@;@@ " (fun fmt r -> fprintf fmt "ensures %a; " pp_eexpr r) fmt spec.ensures;
240
  fprintf_list ~sep:"@;" (fun fmt (name, assumes, ensures, _) -> 
241
    fprintf fmt "behavior %s:@[@ %a@ %a@]" 
242
      name
243
      (fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "assumes %a;" pp_eexpr r)) assumes
244
      (fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "ensures %a;" pp_eexpr r)) ensures
245
  ) fmt spec.behaviors;
246
  fprintf fmt "@]*)";
247
  ()
248

    
249

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

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

    
295
let pp_const_decl fmt cdecl =
296
  fprintf fmt "%s = %a;" cdecl.const_id pp_const cdecl.const_value
297

    
298
let pp_const_decl_list fmt clist = 
299
  fprintf_list ~sep:"@ " pp_const_decl fmt clist
300

    
301
let pp_decl fmt decl =
302
  match decl.top_decl_desc with
303
  | Node nd -> fprintf fmt "%a@ " pp_node nd
304
  | ImportedNode ind ->
305
    fprintf fmt "imported %a;@ " pp_imported_node ind
306
  | Const c -> fprintf fmt "const %a@ " pp_const_decl c
307
  | Open (local, s) -> if local then fprintf fmt "#open \"%s\"@ " s else fprintf fmt "#open <%s>@ " s
308
  | TypeDef tdef -> fprintf fmt "%a@ " pp_typedef tdef
309

    
310
let pp_prog fmt prog = 
311
  fprintf_list ~sep:"@ " pp_decl fmt prog
312

    
313
let pp_short_decl fmt decl =
314
  match decl.top_decl_desc with
315
  | Node nd -> fprintf fmt "node %s@ " nd.node_id
316
  | ImportedNode ind -> fprintf fmt "imported node %s" ind.nodei_id
317
  | Const c -> fprintf fmt "const %a@ " pp_const_decl c
318
  | Open (local, s) -> if local then fprintf fmt "#open \"%s\"@ " s else fprintf fmt "#open <%s>@ " s
319
  | TypeDef tdef -> fprintf fmt "type %s;@ " tdef.tydef_id
320

    
321
let pp_lusi fmt decl = 
322
  match decl.top_decl_desc with
323
  | ImportedNode ind -> fprintf fmt "%a;@ " pp_imported_node ind
324
  | Const c -> fprintf fmt "const %a@ " pp_const_decl c
325
  | Open (local, s) -> if local then fprintf fmt "#open \"%s\"@ " s else fprintf fmt "#open <%s>@ " s
326
  | TypeDef tdef -> fprintf fmt "%a@ " pp_typedef tdef
327
  | Node _ -> assert false
328

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

    
335
let pp_offset fmt offset =
336
  match offset with
337
  | Index i -> fprintf fmt "[%a]" Dimension.pp_dimension i
338
  | Field f -> fprintf fmt ".%s" f
339

    
340
(* Local Variables: *)
341
(* compile-command:"make -C .." *)
342
(* End: *)
(44-44/53)