Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / printers.ml @ 70e1006b

History | View | Annotate | Download (11.3 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
let rec pp_struct_const_field fmt (label, c) =
44
  fprintf fmt "%a = %a;" pp_print_string label pp_const c
45
and pp_const fmt c = 
46
  match c with
47
    | Const_int i -> pp_print_int fmt i
48
    | Const_real r -> pp_print_string fmt r
49
    | Const_float r -> pp_print_float fmt r
50
    | Const_tag  t -> pp_print_string fmt t
51
    | Const_array ca -> Format.fprintf fmt "[%a]" (Utils.fprintf_list ~sep:"," pp_const) ca
52
    | Const_struct fl -> Format.fprintf fmt "{%a }" (Utils.fprintf_list ~sep:" " pp_struct_const_field) fl
53
    | Const_string s -> pp_print_string fmt ("\"" ^ s ^ "\"")
54

    
55

    
56
let rec pp_expr fmt expr =
57
  (match expr.expr_annot with 
58
  | None -> fprintf fmt "%t" 
59
  | Some ann -> fprintf fmt "(%a %t)" pp_expr_annot ann)
60
    (fun fmt -> 
61
      match expr.expr_desc with
62
    | Expr_const c -> pp_const fmt c
63
    | Expr_ident id -> Format.fprintf fmt "%s" id
64
    | Expr_array a -> fprintf fmt "[%a]" pp_tuple a
65
    | Expr_access (a, d) -> fprintf fmt "%a[%a]" pp_expr a Dimension.pp_dimension d
66
    | Expr_power (a, d) -> fprintf fmt "(%a^%a)" pp_expr a Dimension.pp_dimension d
67
    | Expr_tuple el -> fprintf fmt "(%a)" pp_tuple el
68
    | Expr_ite (c, t, e) -> fprintf fmt "(if %a then %a else %a)" pp_expr c pp_expr t pp_expr e
69
    | Expr_arrow (e1, e2) -> fprintf fmt "(%a -> %a)" pp_expr e1 pp_expr e2
70
    | Expr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_expr e1 pp_expr e2
71
    | Expr_pre e -> fprintf fmt "pre %a" pp_expr e
72
    | Expr_when (e, id, l) -> fprintf fmt "%a when %s(%s)" pp_expr e l id
73
    | Expr_merge (id, hl) -> 
74
      fprintf fmt "merge %s %a" id pp_handlers hl
75
    | Expr_appl (id, e, r) -> pp_app fmt id e r
76
    )
77
and pp_tuple fmt el =
78
 fprintf_list ~sep:"," pp_expr fmt el
79

    
80
and pp_handler fmt (t, h) =
81
 fprintf fmt "(%s -> %a)" t pp_expr h
82

    
83
and pp_handlers fmt hl =
84
 fprintf_list ~sep:" " pp_handler fmt hl
85

    
86
and pp_app fmt id e r =
87
  match r with
88
  | None ->
89
    (match id, e.expr_desc with
90
    | "+", Expr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_expr e1 pp_expr e2
91
    | "uminus", _ -> fprintf fmt "(- %a)" pp_expr e
92
    | "-", Expr_tuple([e1;e2]) -> fprintf fmt "(%a - %a)" pp_expr e1 pp_expr e2
93
    | "*", Expr_tuple([e1;e2]) -> fprintf fmt "(%a * %a)" pp_expr e1 pp_expr e2
94
    | "/", Expr_tuple([e1;e2]) -> fprintf fmt "(%a / %a)" pp_expr e1 pp_expr e2
95
    | "mod", Expr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_expr e1 pp_expr e2
96
    | "&&", Expr_tuple([e1;e2]) -> fprintf fmt "(%a and %a)" pp_expr e1 pp_expr e2
97
    | "||", Expr_tuple([e1;e2]) -> fprintf fmt "(%a or %a)" pp_expr e1 pp_expr e2
98
    | "xor", Expr_tuple([e1;e2]) -> fprintf fmt "(%a xor %a)" pp_expr e1 pp_expr e2
99
    | "impl", 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
    | ">=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a >= %a)" pp_expr e1 pp_expr e2
104
    | "!=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a != %a)" pp_expr e1 pp_expr e2
105
    | "=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a = %a)" pp_expr e1 pp_expr e2
106
    | "not", _ -> fprintf fmt "(not %a)" pp_expr e
107
    | _, Expr_tuple _ -> fprintf fmt "%s %a" id pp_expr e
108
    | _ -> fprintf fmt "%s (%a)" id pp_expr e
109
)
110
  | Some (x, l) -> fprintf fmt "%s (%a) every %s(%s)" id pp_expr e l x 
111

    
112

    
113

    
114
and pp_eexpr fmt e =
115
  fprintf fmt "%a%t %a"
116
    (Utils.fprintf_list ~sep:"; " pp_quantifiers) e.eexpr_quantifiers
117
    (fun fmt -> match e.eexpr_quantifiers with [] -> () | _ -> fprintf fmt ";")
118
    pp_expr e.eexpr_qfexpr
119

    
120

    
121
and pp_expr_annot fmt expr_ann =
122
  let pp_annot fmt (kwds, ee) =
123
    Format.fprintf fmt "(*! %t: %a *)"
124
      (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)
125
      pp_eexpr ee
126
  in
127
  fprintf_list ~sep:"@ " pp_annot fmt expr_ann.annots
128
    
129
	
130
let pp_node_eq fmt eq = 
131
  fprintf fmt "%a = %a;" 
132
    pp_eq_lhs eq.eq_lhs
133
    pp_expr eq.eq_rhs
134

    
135
let pp_node_eqs = fprintf_list ~sep:"@ " pp_node_eq
136

    
137
let rec pp_var_struct_type_field fmt (label, tdesc) =
138
  fprintf fmt "%a : %a;" pp_print_string label pp_var_type_dec_desc tdesc
139
and pp_var_type_dec_desc fmt tdesc =
140
  match tdesc with 
141
  | Tydec_any -> fprintf fmt "<any>"
142
  | Tydec_int -> fprintf fmt "int"
143
  | Tydec_real -> fprintf fmt "real"
144
  | Tydec_float -> fprintf fmt "float"
145
  | Tydec_bool -> fprintf fmt "bool"
146
  | Tydec_clock t -> fprintf fmt "%a clock" pp_var_type_dec_desc t
147
  | Tydec_const t -> fprintf fmt "%s" t
148
  | Tydec_enum id_list -> fprintf fmt "enum {%a }" (fprintf_list ~sep:", " pp_print_string) id_list
149
  | Tydec_struct f_list -> fprintf fmt "struct {%a }" (fprintf_list ~sep:" " pp_var_struct_type_field) f_list
150
  | Tydec_array (s, t) -> fprintf fmt "%a^%a" pp_var_type_dec_desc t Dimension.pp_dimension s
151

    
152
let pp_var_type_dec fmt ty =
153
  pp_var_type_dec_desc fmt ty.ty_dec_desc
154

    
155
let pp_typedef fmt ty =
156
  fprintf fmt "type %s = %a;@ " ty.tydef_id pp_var_type_dec_desc ty.tydef_desc
157

    
158
let pp_typedec fmt ty =
159
  fprintf fmt "type %s;@ " ty.tydec_id
160

    
161
(* let rec pp_var_type fmt ty =  *)
162
(*   fprintf fmt "%a" (match ty.tdesc with  *)
163
(*     | Tvar | Tarrow | Tlink | Tunivar -> assert false *)
164
(*     | Tint -> pp_print_string fmt "int" *)
165
(*     | Treal -> pp_print_string fmt "real" *)
166
(*     | Tbool -> pp_print_string fmt "bool" *)
167
(*     | Trat -> pp_print_string fmt "rat" *)
168
(*     | Tclock -> pp_print_string fmt "clock"  *)
169
(*     | Ttuple tel -> fprintf_list ~sep:" * " pp_var_type fmt tel *)
170
(*   ) *)
171

    
172

    
173
let pp_spec fmt spec =
174
  fprintf fmt "@[<hov 2>(*@@ ";
175
  fprintf_list ~sep:"@;@@ " (fun fmt r -> fprintf fmt "requires %a;" pp_eexpr r) fmt spec.requires;
176
  fprintf_list ~sep:"@;@@ " (fun fmt r -> fprintf fmt "ensures %a; " pp_eexpr r) fmt spec.ensures;
177
  fprintf_list ~sep:"@;" (fun fmt (name, assumes, ensures, _) -> 
178
    fprintf fmt "behavior %s:@[@ %a@ %a@]" 
179
      name
180
      (fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "assumes %a;" pp_eexpr r)) assumes
181
      (fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "ensures %a;" pp_eexpr r)) ensures
182
  ) fmt spec.behaviors;
183
  fprintf fmt "@]*)";
184
  ()
185

    
186

    
187
let pp_asserts fmt asserts =
188
  match asserts with 
189
  | _::_ -> (
190
  fprintf fmt "(* Asserts definitions *)@ ";
191
  fprintf_list ~sep:"@ " (fun fmt assert_ -> 
192
    let expr = assert_.assert_expr in
193
    fprintf fmt "assert %a;" pp_expr expr 
194
  ) fmt asserts 
195
  )
196
  | _ -> ()
197
    
198
let pp_node fmt nd = 
199
fprintf fmt "@[<v 0>%a%t%s %s (%a) returns (%a)@.%a%alet@.@[<h 2>   @ @[<v>%a@ %a@ %a@]@ @]@.tel@]@."
200
  (fun fmt s -> match s with Some s -> pp_spec fmt s | _ -> ()) nd.node_spec
201
  (fun fmt -> match nd.node_spec with None -> () | Some _ -> Format.fprintf fmt "@.")
202
  (if nd.node_dec_stateless then "function" else "node")
203
  nd.node_id
204
  pp_node_args nd.node_inputs
205
  pp_node_args nd.node_outputs
206
  (fun fmt locals ->
207
  match locals with [] -> () | _ ->
208
    fprintf fmt "@[<v 4>var %a@]@ " 
209
      (fprintf_list ~sep:"@ " 
210
	 (fun fmt v -> fprintf fmt "%a;" pp_node_var v))
211
      locals
212
  ) nd.node_locals
213
  (fun fmt checks ->
214
  match checks with [] -> () | _ ->
215
    fprintf fmt "@[<v 4>check@ %a@]@ " 
216
      (fprintf_list ~sep:"@ " 
217
	 (fun fmt d -> fprintf fmt "%a" Dimension.pp_dimension d))
218
      checks
219
  ) nd.node_checks
220
  (fprintf_list ~sep:"@ " pp_expr_annot) nd.node_annot
221
  pp_node_eqs nd.node_eqs
222
  pp_asserts nd.node_asserts
223
(*fprintf fmt "@ /* Scheduling: %a */ @ " (fprintf_list ~sep:", " pp_print_string) (Scheduling.schedule_node nd)*)
224

    
225
let pp_imported_node fmt ind = 
226
  fprintf fmt "@[<v>%s %s (%a) returns (%a)@]"
227
    (if ind.nodei_stateless then "function" else "node")
228
    ind.nodei_id
229
    pp_node_args ind.nodei_inputs
230
    pp_node_args ind.nodei_outputs
231

    
232
let pp_const_decl fmt cdecl =
233
  fprintf fmt "%s = %a;" cdecl.const_id pp_const cdecl.const_value
234

    
235
let pp_const_decl_list fmt clist = 
236
  fprintf_list ~sep:"@ " pp_const_decl fmt clist
237

    
238
let pp_decl fmt decl =
239
  match decl.top_decl_desc with
240
  | Node nd -> fprintf fmt "%a@ " pp_node nd
241
  | ImportedNode ind ->
242
    fprintf fmt "imported %a;@ " pp_imported_node ind
243
  | Const c -> fprintf fmt "const %a@ " pp_const_decl c
244
  | Open (local, s) -> if local then fprintf fmt "#open \"%s\"@ " s else fprintf fmt "#open <%s>@ " s
245
  | TypeDef tdef -> fprintf fmt "%a@ " pp_typedef tdef
246

    
247
let pp_prog fmt prog = 
248
  fprintf_list ~sep:"@ " pp_decl fmt prog
249

    
250
let pp_short_decl fmt decl =
251
  match decl.top_decl_desc with
252
  | Node nd -> fprintf fmt "node %s@ " nd.node_id
253
  | ImportedNode ind -> fprintf fmt "imported node %s" ind.nodei_id
254
  | Const c -> fprintf fmt "const %a@ " pp_const_decl c
255
  | Open (local, s) -> if local then fprintf fmt "#open \"%s\"@ " s else fprintf fmt "#open <%s>@ " s
256
  | TypeDef tdef -> fprintf fmt "type %s;@ " tdef.tydef_id
257

    
258
let pp_lusi fmt decl = 
259
  match decl.top_decl_desc with
260
  | ImportedNode ind -> fprintf fmt "%a;@ " pp_imported_node ind
261
  | Const c -> fprintf fmt "const %a@ " pp_const_decl c
262
  | Open (local, s) -> if local then fprintf fmt "#open \"%s\"@ " s else fprintf fmt "#open <%s>@ " s
263
  | TypeDef tdef -> fprintf fmt "%a@ " pp_typedef tdef
264
  | Node _ -> assert false
265

    
266
let pp_lusi_header fmt basename prog =
267
  fprintf fmt "(* Generated Lustre Interface file from %s.lus *)@." basename;
268
  fprintf fmt "(* by Lustre-C compiler version %s, %a *)@." Version.number pp_date (Unix.gmtime (Unix.time ()));
269
  fprintf fmt "(* Feel free to mask some of the definitions by removing them from this file. *)@.@.";
270
  List.iter (fprintf fmt "%a@." pp_lusi) prog    
271
  
272
(* Local Variables: *)
273
(* compile-command:"make -C .." *)
274
(* End: *)