lustrec / src / printers.ml @ d4807c3d
History | View | Annotate | Download (13.2 KB)
1 | 22fe1c93 | ploc | (* ---------------------------------------------------------------------------- |
---|---|---|---|
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 | 12af4908 | xthirioux | 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 | 22fe1c93 | ploc | 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 | 12af4908 | xthirioux | | Const_struct fl -> Format.fprintf fmt "{%a }" (Utils.fprintf_list ~sep:" " pp_struct_const_field) fl |
53 | 22fe1c93 | ploc | |
54 | 7291cb80 | xthirioux | and 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 |
55 | |||
56 | 8f1c7e91 | xthirioux | and 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 |
57 | 22fe1c93 | ploc | |
58 | and pp_expr fmt expr = |
||
59 | match expr.expr_desc with |
||
60 | | Expr_const c -> pp_const fmt c |
||
61 | | Expr_ident id -> Format.fprintf fmt "%s" id |
||
62 | | Expr_array a -> fprintf fmt "[%a]" pp_tuple a |
||
63 | | Expr_access (a, d) -> fprintf fmt "%a[%a]" pp_expr a Dimension.pp_dimension d |
||
64 | | Expr_power (a, d) -> fprintf fmt "(%a^%a)" pp_expr a Dimension.pp_dimension d |
||
65 | | Expr_tuple el -> fprintf fmt "(%a)" pp_tuple el |
||
66 | | Expr_ite (c, t, e) -> fprintf fmt "(if %a then %a else %a)" pp_expr c pp_expr t pp_expr e |
||
67 | e2380d4d | ploc | | Expr_arrow (e1, e2) -> fprintf fmt "(%a -> %a)" pp_expr e1 pp_expr e2 |
68 | 22fe1c93 | ploc | | Expr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_expr e1 pp_expr e2 |
69 | | Expr_pre e -> fprintf fmt "pre %a" pp_expr e |
||
70 | | Expr_when (e, id, l) -> fprintf fmt "%a when %s(%s)" pp_expr e l id |
||
71 | | Expr_merge (id, hl) -> |
||
72 | fprintf fmt "merge %s %a" id pp_handlers hl |
||
73 | | Expr_appl (id, e, r) -> pp_app fmt id e r |
||
74 | | Expr_uclock _ |
||
75 | | Expr_dclock _ |
||
76 | | Expr_phclock _ -> assert false |
||
77 | |||
78 | and pp_tuple fmt el = |
||
79 | fprintf_list ~sep:"," pp_expr fmt el |
||
80 | |||
81 | and pp_handler fmt (t, h) = |
||
82 | fprintf fmt "(%s -> %a)" t pp_expr h |
||
83 | |||
84 | and pp_handlers fmt hl = |
||
85 | fprintf_list ~sep:" " pp_handler fmt hl |
||
86 | |||
87 | and pp_app fmt id e r = |
||
88 | match r with |
||
89 | | None -> |
||
90 | (match id, e.expr_desc with |
||
91 | | "+", Expr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_expr e1 pp_expr e2 |
||
92 | | "uminus", _ -> fprintf fmt "(- %a)" pp_expr e |
||
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 | | "/", Expr_tuple([e1;e2]) -> fprintf fmt "(%a / %a)" pp_expr e1 pp_expr e2 |
||
96 | | "mod", Expr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_expr e1 pp_expr e2 |
||
97 | | "&&", Expr_tuple([e1;e2]) -> fprintf fmt "(%a and %a)" pp_expr e1 pp_expr e2 |
||
98 | | "||", Expr_tuple([e1;e2]) -> fprintf fmt "(%a or %a)" pp_expr e1 pp_expr e2 |
||
99 | | "xor", Expr_tuple([e1;e2]) -> fprintf fmt "(%a xor %a)" pp_expr e1 pp_expr e2 |
||
100 | | "impl", 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 | | "=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a = %a)" pp_expr e1 pp_expr e2 |
||
107 | | "not", _ -> fprintf fmt "(not %a)" pp_expr e |
||
108 | e2380d4d | ploc | | _, Expr_tuple _ -> fprintf fmt "%s %a" id pp_expr e |
109 | | _ -> fprintf fmt "%s (%a)" id pp_expr e |
||
110 | ) |
||
111 | 22fe1c93 | ploc | | Some (x, l) -> fprintf fmt "%s (%a) every %s(%s)" id pp_expr e l x |
112 | |||
113 | let pp_node_eq fmt eq = |
||
114 | fprintf fmt "%a = %a;" |
||
115 | pp_eq_lhs eq.eq_lhs |
||
116 | pp_expr eq.eq_rhs |
||
117 | |||
118 | let pp_node_eqs = fprintf_list ~sep:"@ " pp_node_eq |
||
119 | |||
120 | 7291cb80 | xthirioux | let pp_node_args = fprintf_list ~sep:"; " pp_node_var |
121 | 22fe1c93 | ploc | |
122 | let pp_var_type_dec fmt ty = |
||
123 | 6a6abd76 | xthirioux | let rec pp_var_struct_type_field fmt (label, tdesc) = |
124 | fprintf fmt "%a : %a" pp_var_type_dec_desc tdesc pp_print_string label |
||
125 | and pp_var_type_dec_desc fmt tdesc = |
||
126 | 22fe1c93 | ploc | match tdesc with |
127 | | Tydec_any -> fprintf fmt "<any>" |
||
128 | | Tydec_int -> fprintf fmt "int" |
||
129 | | Tydec_real -> fprintf fmt "real" |
||
130 | | Tydec_float -> fprintf fmt "float" |
||
131 | | Tydec_bool -> fprintf fmt "bool" |
||
132 | | Tydec_clock t -> fprintf fmt "%a clock" pp_var_type_dec_desc t |
||
133 | | Tydec_const t -> fprintf fmt "%s" t |
||
134 | | Tydec_enum id_list -> fprintf fmt "enum {%a }" (fprintf_list ~sep:", " pp_print_string) id_list |
||
135 | 6a6abd76 | xthirioux | | Tydec_struct f_list -> fprintf fmt "struct {%a }" (fprintf_list ~sep:"; " pp_var_struct_type_field) f_list |
136 | 22fe1c93 | ploc | | Tydec_array (s, t) -> fprintf fmt "%a^%a" pp_var_type_dec_desc t Dimension.pp_dimension s |
137 | in pp_var_type_dec_desc fmt ty.ty_dec_desc |
||
138 | |||
139 | (* let rec pp_var_type fmt ty = *) |
||
140 | (* fprintf fmt "%a" (match ty.tdesc with *) |
||
141 | (* | Tvar | Tarrow | Tlink | Tunivar -> assert false *) |
||
142 | (* | Tint -> pp_print_string fmt "int" *) |
||
143 | (* | Treal -> pp_print_string fmt "real" *) |
||
144 | (* | Tbool -> pp_print_string fmt "bool" *) |
||
145 | (* | Trat -> pp_print_string fmt "rat" *) |
||
146 | (* | Tclock -> pp_print_string fmt "clock" *) |
||
147 | (* | Ttuple tel -> fprintf_list ~sep:" * " pp_var_type fmt tel *) |
||
148 | (* ) *) |
||
149 | |||
150 | e2380d4d | ploc | |
151 | let pp_econst fmt c = |
||
152 | match c with |
||
153 | | EConst_int i -> pp_print_int fmt i |
||
154 | | EConst_real r -> pp_print_string fmt r |
||
155 | | EConst_float r -> pp_print_float fmt r |
||
156 | | EConst_bool b -> pp_print_bool fmt b |
||
157 | | EConst_string s -> pp_print_string fmt ("\"" ^ s ^ "\"") |
||
158 | |||
159 | let rec pp_eexpr fmt eexpr = |
||
160 | match eexpr.eexpr_desc with |
||
161 | | EExpr_const c -> pp_econst fmt c |
||
162 | | EExpr_ident id -> pp_print_string fmt id |
||
163 | | EExpr_tuple el -> fprintf_list ~sep:"," pp_eexpr fmt el |
||
164 | | EExpr_arrow (e1, e2) -> fprintf fmt "%a -> %a" pp_eexpr e1 pp_eexpr e2 |
||
165 | | EExpr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_eexpr e1 pp_eexpr e2 |
||
166 | (* | EExpr_concat (e1, e2) -> fprintf fmt "%a::%a" pp_eexpr e1 pp_eexpr e2 *) |
||
167 | (* | EExpr_tail e -> fprintf fmt "tail %a" pp_eexpr e *) |
||
168 | | EExpr_pre e -> fprintf fmt "pre %a" pp_eexpr e |
||
169 | | EExpr_when (e, id) -> fprintf fmt "%a when %s" pp_eexpr e id |
||
170 | | EExpr_merge (id, e1, e2) -> |
||
171 | fprintf fmt "merge (%s, %a, %a)" id pp_eexpr e1 pp_eexpr e2 |
||
172 | | EExpr_appl (id, e, r) -> pp_eapp fmt id e r |
||
173 | | EExpr_forall (vars, e) -> fprintf fmt "forall %a; %a" pp_node_args vars pp_eexpr e |
||
174 | | EExpr_exists (vars, e) -> fprintf fmt "exists %a; %a" pp_node_args vars pp_eexpr e |
||
175 | |||
176 | |||
177 | (* | EExpr_whennot _ *) |
||
178 | (* | EExpr_uclock _ *) |
||
179 | (* | EExpr_dclock _ *) |
||
180 | (* | EExpr_phclock _ -> assert false *) |
||
181 | and pp_eapp fmt id e r = |
||
182 | match r with |
||
183 | | None -> |
||
184 | (match id, e.eexpr_desc with |
||
185 | | "+", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_eexpr e1 pp_eexpr e2 |
||
186 | | "uminus", _ -> fprintf fmt "(- %a)" pp_eexpr e |
||
187 | | "-", 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 | | "mod", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a mod %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 | | "xor", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a ^^ %a)" pp_eexpr e1 pp_eexpr e2 |
||
194 | | "impl", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a ==> %a)" pp_eexpr e1 pp_eexpr e2 |
||
195 | | "<", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a < %a)" pp_eexpr e1 pp_eexpr e2 |
||
196 | | "<=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a <= %a)" pp_eexpr e1 pp_eexpr e2 |
||
197 | | ">", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a > %a)" pp_eexpr e1 pp_eexpr e2 |
||
198 | | ">=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a >= %a)" pp_eexpr e1 pp_eexpr e2 |
||
199 | | "!=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a != %a)" pp_eexpr e1 pp_eexpr e2 |
||
200 | | "=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a == %a)" pp_eexpr e1 pp_eexpr e2 |
||
201 | | "not", _ -> fprintf fmt "(! %a)" pp_eexpr e |
||
202 | | "ite", EExpr_tuple([e1;e2;e3]) -> fprintf fmt "(if %a then %a else %a)" pp_eexpr e1 pp_eexpr e2 pp_eexpr e3 |
||
203 | | _ -> fprintf fmt "%s (%a)" id pp_eexpr e) |
||
204 | | Some x -> fprintf fmt "%s (%a) every %s" id pp_eexpr e x |
||
205 | |||
206 | |||
207 | let pp_ensures fmt e = |
||
208 | match e with |
||
209 | | EnsuresExpr e -> fprintf fmt "ensures %a;@ " pp_eexpr e |
||
210 | | SpecObserverNode (name, args) -> fprintf fmt "observer %s (%a);@ " name (fprintf_list ~sep:", " pp_eexpr) args |
||
211 | |||
212 | let pp_spec fmt spec = |
||
213 | fprintf fmt "@[<hov 2>(*@@ "; |
||
214 | fprintf_list ~sep:"" (fun fmt r -> fprintf fmt "requires %a;@ " pp_eexpr r) fmt spec.requires; |
||
215 | fprintf_list ~sep:"" pp_ensures fmt spec.ensures; |
||
216 | fprintf_list ~sep:"@ " (fun fmt (name, assumes, requires) -> |
||
217 | fprintf fmt "behavior %s:@[@ %a@ %a@]" |
||
218 | name |
||
219 | (fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "assumes %a;" pp_eexpr r)) assumes |
||
220 | (fprintf_list ~sep:"@ " pp_ensures) requires |
||
221 | ) fmt spec.behaviors; |
||
222 | fprintf fmt "@]*)"; |
||
223 | () |
||
224 | |||
225 | 22fe1c93 | ploc | let pp_node fmt nd = |
226 | 52cfee34 | xthirioux | fprintf fmt "@[<v 0>%a%t%s %s (%a) returns (%a)@.%a%alet@.@[<h 2> @ @[%a@]@ @]@.tel@]@." |
227 | e2380d4d | ploc | (fun fmt s -> match s with Some s -> pp_spec fmt s | _ -> ()) nd.node_spec |
228 | 52cfee34 | xthirioux | (fun fmt -> match nd.node_spec with None -> () | Some _ -> Format.fprintf fmt "@.") |
229 | (if nd.node_dec_stateless then "function" else "node") |
||
230 | 22fe1c93 | ploc | nd.node_id |
231 | pp_node_args nd.node_inputs |
||
232 | pp_node_args nd.node_outputs |
||
233 | (fun fmt locals -> |
||
234 | match locals with [] -> () | _ -> |
||
235 | fprintf fmt "@[<v 4>var %a@]@ " |
||
236 | (fprintf_list ~sep:"@ " |
||
237 | 7291cb80 | xthirioux | (fun fmt v -> fprintf fmt "%a;" pp_node_var v)) |
238 | 22fe1c93 | ploc | locals |
239 | ) nd.node_locals |
||
240 | (fun fmt checks -> |
||
241 | match checks with [] -> () | _ -> |
||
242 | fprintf fmt "@[<v 4>check@ %a@]@ " |
||
243 | (fprintf_list ~sep:"@ " |
||
244 | (fun fmt d -> fprintf fmt "%a" Dimension.pp_dimension d)) |
||
245 | checks |
||
246 | ) nd.node_checks |
||
247 | pp_node_eqs nd.node_eqs |
||
248 | (*fprintf fmt "@ /* Scheduling: %a */ @ " (fprintf_list ~sep:", " pp_print_string) (Scheduling.schedule_node nd)*) |
||
249 | |||
250 | let pp_imported_node fmt ind = |
||
251 | 52cfee34 | xthirioux | fprintf fmt "@[<v>%s %s (%a) returns (%a) %t@]" |
252 | (if ind.nodei_stateless then "function" else "node") |
||
253 | 22fe1c93 | ploc | ind.nodei_id |
254 | pp_node_args ind.nodei_inputs |
||
255 | pp_node_args ind.nodei_outputs |
||
256 | (fun fmt -> if ind.nodei_stateless then Format.fprintf fmt "stateless") |
||
257 | |||
258 | let pp_decl fmt decl = |
||
259 | match decl.top_decl_desc with |
||
260 | | Node nd -> fprintf fmt "%a@ " pp_node nd |
||
261 | | ImportedNode ind -> |
||
262 | fprintf fmt "imported %a;@ " pp_imported_node ind |
||
263 | | Consts clist -> ( |
||
264 | fprintf fmt "const %a@ " |
||
265 | (fprintf_list ~sep:"@ " (fun fmt cdecl -> |
||
266 | fprintf fmt "%s = %a;" |
||
267 | cdecl.const_id pp_const cdecl.const_value)) clist) |
||
268 | 5c1184ad | ploc | | Open s -> fprintf fmt "open \"%s\"" s |
269 | 22fe1c93 | ploc | |
270 | |||
271 | let pp_prog fmt prog = |
||
272 | fprintf_list ~sep:"@ " pp_decl fmt prog |
||
273 | |||
274 | let pp_short_decl fmt decl = |
||
275 | match decl.top_decl_desc with |
||
276 | | Node nd -> fprintf fmt "node %s@ " nd.node_id |
||
277 | | ImportedNode ind -> fprintf fmt "imported node %s" ind.nodei_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 | 5c1184ad | ploc | | Open s -> fprintf fmt "open \"%s\"" s |
283 | 22fe1c93 | ploc | |
284 | 5c1184ad | ploc | let pp_lusi fmt decl = |
285 | match decl.top_decl_desc with |
||
286 | | Node nd -> |
||
287 | fprintf fmt |
||
288 | e135421f | xthirioux | "@[<v>%s %s (%a) returns (%a);@ @]@ " |
289 | (if Stateless.check_node decl then "function" else "node") |
||
290 | 5c1184ad | ploc | nd.node_id |
291 | pp_node_args nd.node_inputs |
||
292 | pp_node_args nd.node_outputs |
||
293 | 52cfee34 | xthirioux | | ImportedNode _ | Consts _ | Open _ -> () |
294 | 22fe1c93 | ploc | |
295 | |||
296 | 5c1184ad | ploc | |
297 | |||
298 | let pp_lusi_header fmt filename prog = |
||
299 | fprintf fmt "(* Generated Lustre Interface file from %s *)@." filename; |
||
300 | fprintf fmt "(* generated by Lustre-C compiler version %s, %a *)@." Version.number pp_date (Unix.gmtime (Unix.time ())); |
||
301 | fprintf fmt "(* feel free to mask some of the nodes by removing them from this file. *)@.@."; |
||
302 | List.iter (fprintf fmt "%a@." pp_lusi) prog |
||
303 | |||
304 | 22fe1c93 | ploc | (* Local Variables: *) |
305 | (* compile-command:"make -C .." *) |
||
306 | (* End: *) |