Project

General

Profile

Download (15.4 KB) Statistics
| Branch: | Tag: | Revision:
1
open Utils
2
open Lustre_types
3
open Corelang
4
open Machine_code_types
5
open Format
6
open Machine_code_common
7

    
8
(* Matlab starting counting from 1. simple function to extract the element id in
9
   the list. Starts from 1. *)
10
let rec get_idx x l =
11
  match l with
12
  | hd :: tl ->
13
    if hd = x then 1 else 1 + get_idx x tl
14
  | [] ->
15
    assert false
16

    
17
let rec get_expr_vars v =
18
  match v.value_desc with
19
  | Cst _ ->
20
    VSet.empty
21
  | Var v ->
22
    VSet.singleton v
23
  | Fun (_, args) ->
24
    List.fold_left
25
      (fun accu v -> VSet.union accu (get_expr_vars v))
26
      VSet.empty args
27
  | _ ->
28
    assert false
29
(* Invalid argument *)
30

    
31
let is_imported_node f m =
32
  let decl, _ = List.assoc f m.mcalls in
33
  Corelang.is_imported_node decl
34

    
35
(* Handling of enumerated types: for the moment each of such type is transformed
36
   into an int: the idx number of the constant in the typedef. This is not so
37
   nice but is compatible with basic Simulink types: int, real, bools) *)
38
(* let recorded_enums = ref [] let record_types prog = let typedefs =
39
   Corelang.get_typedefs prog in List.iter (fun top -> let consts =
40
   consts_of_enum_type top in ) prog *)
41

    
42
(* Basic printing functions *)
43

    
44
let hash_map = Hashtbl.create 13
45

    
46
(* If string length of f is longer than 50 chars, we select the 10 first and
47
   last and put a hash in the middle *)
48
let print_protect fmt f =
49
  fprintf str_formatter "%t" f;
50
  let s = flush_str_formatter () in
51
  let l = String.length s in
52
  if l > 30 then (
53
    if
54
      (* let _ = Format.eprintf "Looking for variable %s in hash @[<v 0>%t@]@." *)
55
      (*   s *)
56
      (* (fun fmt -> Hashtbl.iter (fun s new_s -> fprintf fmt "%s -> %s@ " s
57
         new_s) hash_map) *)
58
      (* in *)
59
      Hashtbl.mem hash_map s
60
    then fprintf fmt "%s" (Hashtbl.find hash_map s)
61
    else
62
      let prefix = String.sub s 0 10 and suffix = String.sub s (l - 10) 10 in
63
      let hash = Hashtbl.hash s in
64
      fprintf str_formatter "%s_%i_%s" prefix hash suffix;
65
      let new_s = flush_str_formatter () in
66
      Hashtbl.add hash_map s new_s;
67
      fprintf fmt "%s" new_s)
68
  else fprintf fmt "%s" s
69

    
70
let pp_var_string fmt v =
71
  fprintf fmt "\"%t\"" (fun fmt ->
72
      print_protect fmt (fun fmt -> fprintf fmt "%s" v))
73

    
74
let pp_var_name fmt v =
75
  print_protect fmt (fun fmt -> Printers.pp_var_name fmt v)
76
(*let pp_node_args = fprintf_list ~sep:", " pp_var_name*)
77

    
78
(********* Printing types ***********)
79
(* Two cases: - printing a variable definition: - we look at the declared type
80
   if available - if not, we print the inferred type
81

    
82
   - printing a constant definion *)
83

    
84
let rec pp_emf_dim fmt dim_expr =
85
  fprintf fmt "{";
86
  (let open Dimension in
87
  match dim_expr.dim_desc with
88
  | Dbool b ->
89
    fprintf fmt "\"kind\": \"bool\",@ \"value\": \"%b\"" b
90
  | Dint i ->
91
    fprintf fmt "\"kind\": \"int\",@ \"value\": \"%i\"" i
92
  | Dident s ->
93
    fprintf fmt "\"kind\": \"ident\",@ \"value\": \"%s\"" s
94
  | Dappl (f, args) ->
95
    fprintf fmt "\"kind\": \"fun\",@ \"id\": \"%s\",@ \"args\": [@[%a@]]" f
96
      (pp_comma_list pp_emf_dim) args
97
  | Dite (i, t, e) ->
98
    fprintf fmt
99
      "\"kind\": \"ite\",@ \"guard\": \"%a\",@ \"then\": %a,@ \"else\": %a"
100
      pp_emf_dim i pp_emf_dim t pp_emf_dim e
101
  | Dlink e ->
102
    pp_emf_dim fmt e
103
  | Dvar | Dunivar ->
104
    assert false
105
  (* unresolved *));
106
  fprintf fmt "}"
107

    
108
(* First try to print the declared one *)
109
let rec pp_concrete_type dec_t infered_t fmt =
110
  match dec_t with
111
  | Tydec_any ->
112
    (* Dynamical built variable. No declared type. Shall use the infered one. *)
113
    pp_infered_type fmt infered_t
114
  | Tydec_int ->
115
    fprintf fmt "{ \"kind\": \"int\" }" (* !Options.int_type *)
116
  | Tydec_real ->
117
    fprintf fmt "{ \"kind\": \"real\" }"
118
  (* !Options.real_type *)
119
  (* TODO we could add more concrete types here if they were available in dec_t *)
120
  | Tydec_bool ->
121
    fprintf fmt "{ \"kind\": \"bool\" }"
122
  | Tydec_clock t ->
123
    pp_concrete_type t infered_t fmt
124
  | Tydec_const id ->
125
    (* This is an alias type *)
126

    
127
    (* id for a enumerated type, eg. introduced by automata *)
128
    let typ =
129
      Corelang.typedef_of_top (Hashtbl.find Corelang.type_table dec_t)
130
    in
131
    (* Print the type name associated to this enumerated type. This is basically
132
       an integer *)
133
    pp_tag_type id typ infered_t fmt
134
  | Tydec_struct _ | Tydec_enum _ ->
135
    assert false
136
  (* should not happen. These type are only built when declaring a type in the
137
     prefix of the lustre file. They shall not be associated to variables *)
138
  | Tydec_array (dim, e) ->
139
    let inf_base =
140
      match infered_t.Types.tdesc with
141
      | Types.Tarray (_, t) ->
142
        t
143
      | _ ->
144
        (* returing something useless, hoping that the concrete datatype will
145
           return something usefull *)
146
        Types.new_var ()
147
    in
148
    fprintf fmt "{ \"kind\": \"array\", \"base_type\": %t, \"dim\": %a }"
149
      (pp_concrete_type e inf_base)
150
      pp_emf_dim dim
151

    
152
(* | _ -> eprintf
153
 *          "unhandled construct in type printing for EMF backend: %a@."
154
 *          Printers.pp_var_type_dec_desc dec_t; raise (Failure "var") *)
155
and pp_tag_type id typ inf fmt =
156
  (* We ought to represent these types as values: enum will become int, we keep
157
     the name for structs *)
158
  let rec aux tydec_desc =
159
    match tydec_desc with
160
    | Tydec_int | Tydec_real | Tydec_bool | Tydec_array _ ->
161
      pp_concrete_type tydec_desc inf fmt
162
    | Tydec_const id ->
163
      (* Alias of an alias: unrolling definitions *)
164
      let typ =
165
        Corelang.typedef_of_top (Hashtbl.find Corelang.type_table tydec_desc)
166
      in
167
      pp_tag_type id typ inf fmt
168
    | Tydec_clock ty ->
169
      aux ty
170
    | Tydec_enum const_list ->
171
      (* enum can be mapped to int *)
172
      let size = List.length const_list in
173
      fprintf fmt "{ \"name\": \"%s\", \"kind\": \"enum\", \"size\": \"%i\" }"
174
        id size
175
    | Tydec_struct _ ->
176
      fprintf fmt "{ \"name\": \"%s\", \"kind\": \"struct\" }" id
177
    | Tydec_any ->
178
      (* shall not happen: a declared type cannot be bound to type any *)
179
      assert false
180
  in
181
  aux typ.tydef_desc
182

    
183
and pp_infered_type fmt t =
184
  (* Shall only be used for variable types that were not properly declared. Ie
185
     generated at compile time. *)
186
  let open Types in
187
  if is_bool_type t then fprintf fmt "{ \"kind\": \"bool\" }"
188
  else if is_int_type t then fprintf fmt "{ \"kind\": \"int\" }"
189
  else if (* !Options.int_type *)
190
          is_real_type t then fprintf fmt "{ \"kind\": \"real\" }"
191
  else
192
    (* !Options.real_type *)
193
    match t.tdesc with
194
    | Tclock t ->
195
      pp_infered_type fmt t
196
    | Tstatic (_, t) ->
197
      fprintf fmt "%a" pp_infered_type t
198
    | Tconst id ->
199
      (* This is a type id for a enumerated type, eg. introduced by automata *)
200
      let typ =
201
        Corelang.typedef_of_top
202
          (Hashtbl.find Corelang.type_table (Tydec_const id))
203
      in
204
      pp_tag_type id typ t fmt
205
    | Tlink ty ->
206
      pp_infered_type fmt ty
207
    | Tarray (dim, base_t) ->
208
      fprintf fmt "{ \"kind\": \"array\", \"base_type\": %a, \"dim\": %a }"
209
        pp_infered_type base_t pp_emf_dim dim
210
    | _ ->
211
      eprintf "unhandled type: %a@." Types.print_node_ty t;
212
      assert false
213

    
214
(*let pp_cst_type fmt v = match v.value_desc with | Cst c-> pp_cst_type c
215
  v.value_type fmt (* constants do not have declared type (yet) *) | _ -> assert
216
  false *)
217

    
218
(* Provide both the declared type and the infered one. *)
219
let pp_var_type fmt v =
220
  try
221
    if Machine_types.is_specified v then Machine_types.pp_var_type fmt v
222
    else pp_concrete_type v.var_dec_type.ty_dec_desc v.var_type fmt
223
  with Failure msg ->
224
    eprintf "failed var: %a@.%s@." Printers.pp_var v msg;
225
    assert false
226

    
227
(******** Other print functions *)
228

    
229
let pp_emf_list ?(eol : ('a, formatter, unit) Stdlib.format = "") pp fmt l =
230
  match l with
231
  | [] ->
232
    ()
233
  | _ ->
234
    fprintf fmt "@[";
235
    pp_comma_list pp fmt l;
236
    fprintf fmt "@]%(%)" eol
237

    
238
(* Print the variable declaration *)
239
let pp_emf_var_decl fmt v =
240
  fprintf fmt
241
    "@[{\"name\": \"%a\", \"datatype\": %a, \"original_name\": \"%a\"}@]"
242
    pp_var_name v pp_var_type v Printers.pp_var_name v
243

    
244
let pp_emf_vars_decl = pp_emf_list pp_emf_var_decl
245

    
246
let reset_name id = "reset_" ^ id
247

    
248
let pp_tag_id fmt t =
249
  let typ = Corelang.typedef_of_top (Hashtbl.find Corelang.tag_table t) in
250
  if typ.tydef_id = "bool" then pp_print_string fmt t
251
  else
252
    let const_list =
253
      match typ.tydef_desc with Tydec_enum tl -> tl | _ -> assert false
254
    in
255
    fprintf fmt "%i" (get_idx t const_list)
256

    
257
let pp_cst_type c inf fmt (*infered_typ*) =
258
  let pp_basic fmt s = fprintf fmt "{ \"kind\": \"%s\" }" s in
259
  match c with
260
  | Const_tag t ->
261
    let typ = Corelang.typedef_of_top (Hashtbl.find Corelang.tag_table t) in
262
    if typ.tydef_id = "bool" then pp_basic fmt "bool"
263
    else pp_tag_type t typ inf fmt
264
  | Const_int _ ->
265
    pp_basic fmt "int" (*!Options.int_type*)
266
  | Const_real _ ->
267
    pp_basic fmt "real" (*!Options.real_type*)
268
  | Const_string _ ->
269
    pp_basic fmt "string"
270
  | _ ->
271
    eprintf "cst: %a@." Printers.pp_const c;
272
    assert false
273

    
274
let pp_emf_cst c inf fmt =
275
  let pp_typ fmt = fprintf fmt "\"datatype\": %t@ " (pp_cst_type c inf) in
276
  match c with
277
  | Const_tag t ->
278
    let typ = Corelang.typedef_of_top (Hashtbl.find Corelang.tag_table t) in
279
    if typ.tydef_id = "bool" then (
280
      fprintf fmt "{@[\"type\": \"constant\",@ ";
281
      fprintf fmt "\"value\": \"%a\",@ " Printers.pp_const c;
282
      pp_typ fmt;
283
      fprintf fmt "@]}")
284
    else (
285
      fprintf fmt "{@[\"type\": \"constant\",@ \"value\": \"%a\",@ " pp_tag_id t;
286
      fprintf fmt "\"origin_type\": \"%s\",@ \"origin_value\": \"%s\",@ "
287
        typ.tydef_id t;
288
      pp_typ fmt;
289
      fprintf fmt "@]}")
290
  | Const_string s ->
291
    fprintf fmt "{@[\"type\": \"constant\",@ \"value\": \"%s\",@ " s;
292
    pp_typ fmt;
293
    fprintf fmt "@]}"
294
  | _ ->
295
    fprintf fmt "{@[\"type\": \"constant\",@ \"value\": \"%a\",@ "
296
      Printers.pp_const c;
297
    pp_typ fmt;
298
    fprintf fmt "@]}"
299

    
300
(* Print a value: either a constant or a variable value *)
301
let rec pp_emf_cst_or_var m fmt v =
302
  match v.value_desc with
303
  | Cst c ->
304
    pp_emf_cst c v.value_type fmt
305
  | Var v ->
306
    fprintf fmt "{@[\"type\": \"variable\",@ \"value\": \"%a\",@ " pp_var_name v;
307
    (* fprintf fmt "\"original_name\": \"%a\",@ " Printers.pp_var_name v; *)
308
    fprintf fmt "\"datatype\": %a@ " pp_var_type v;
309
    fprintf fmt "@]}"
310
  | Array vl ->
311
    fprintf fmt "{@[\"type\": \"array\",@ \"value\": @[[%a@]]@ "
312
      (pp_emf_cst_or_var_list m) vl;
313
    fprintf fmt "@]}"
314
  | Access (arr, idx) ->
315
    fprintf fmt
316
      "{@[\"type\": \"array access\",@ \"array\": @[[%a@]],@ \"idx\": \
317
       @[[%a@]]@ "
318
      (pp_emf_cst_or_var m) arr (pp_emf_cst_or_var m) idx;
319
    fprintf fmt "@]}"
320
  | Power (v, nb) ->
321
    fprintf fmt
322
      "{@[\"type\": \"power\",@ \"expr\": @[[%a@]],@ \"nb\": @[[%a@]]@ "
323
      (pp_emf_cst_or_var m) v (pp_emf_cst_or_var m) nb;
324
    fprintf fmt "@]}"
325
  | Fun _ ->
326
    eprintf "Fun expression should have been normalized: %a@." (pp_val m) v;
327
    assert false (* Invalid argument *)
328
  | ResetFlag ->
329
    (* TODO: handle reset flag *)
330
    assert false
331

    
332
and pp_emf_cst_or_var_list m =
333
  pp_comma_list (pp_emf_cst_or_var m)
334

    
335
(* Printer lustre expr and eexpr *)
336

    
337
let rec pp_emf_expr fmt e =
338
  match e.expr_desc with
339
  | Expr_const c ->
340
    pp_emf_cst c e.expr_type fmt
341
  | Expr_ident id ->
342
    fprintf fmt "{@[\"type\": \"variable\",@ \"value\": \"%a\",@ " print_protect
343
      (fun fmt -> pp_print_string fmt id);
344
    fprintf fmt "\"datatype\": %t@ "
345
      (pp_concrete_type Tydec_any
346
         (* don't know much about that time since it was not declared. That may
347
            not work with clock constants *)
348
         e.expr_type);
349
    fprintf fmt "@]}"
350
  | Expr_tuple el ->
351
    fprintf fmt "[@[<hov 0>%a@ @]]"
352
      (pp_comma_list pp_emf_expr)
353
      el
354
  (* Missing these | Expr_ite of expr * expr * expr | Expr_arrow of expr * expr
355
     | Expr_fby of expr * expr | Expr_array of expr list | Expr_access of expr *
356
     Dimension.dim_expr | Expr_power of expr * Dimension.dim_expr | Expr_pre of
357
     expr | Expr_when of expr * ident * label | Expr_merge of ident * (label *
358
     expr) list | Expr_appl of call_t *)
359
  | _ ->
360
    Log.report ~level:2 (fun fmt ->
361
        fprintf fmt "Warning: unhandled expression %a in annotation.@ "
362
          Printers.pp_expr e;
363
        fprintf fmt "Will not be produced in the experted JSON EMF@.");
364
    fprintf fmt "\"unhandled construct, complain to Ploc\""
365

    
366
(* Remaining constructs *)
367
(* | Expr_ite   of expr * expr * expr *)
368
(* | Expr_arrow of expr * expr *)
369
(* | Expr_fby of expr * expr *)
370
(* | Expr_array of expr list *)
371
(* | Expr_access of expr * Dimension.dim_expr *)
372
(* | Expr_power of expr * Dimension.dim_expr *)
373
(* | Expr_pre of expr *)
374
(* | Expr_when of expr * ident * label *)
375
(* | Expr_merge of ident * (label * expr) list *)
376
(* | Expr_appl of call_t *)
377

    
378
let pp_emf_exprs = pp_emf_list pp_emf_expr
379

    
380
let pp_emf_const fmt v =
381
  fprintf fmt
382
    "@[<hov 0>{\"name\": \"%a\",@ \"datatype\":%a,@ \"original_name\": \
383
     \"%a\",@ \"value\": %a}@]"
384
    pp_var_name v pp_var_type v Printers.pp_var_name v pp_emf_expr
385
    (match v.var_dec_value with None -> assert false | Some e -> e)
386

    
387
let pp_emf_consts = pp_emf_list pp_emf_const
388

    
389
let pp_emf_eexpr fmt ee =
390
  fprintf fmt "{@[<hov 0>%t\"quantifiers\": \"%a\",@ \"qfexpr\": @[%a@]@] }"
391
    (fun fmt ->
392
      match ee.eexpr_name with
393
      | None ->
394
        ()
395
      | Some name ->
396
        Format.fprintf fmt "\"name\": \"%s\",@ " name)
397
    (pp_print_list ~pp_sep:pp_print_semicolon Printers.pp_quantifiers)
398
    ee.eexpr_quantifiers pp_emf_expr ee.eexpr_qfexpr
399

    
400
let pp_emf_eexprs = pp_emf_list pp_emf_eexpr
401

    
402
(* TODO Thanksgiving
403

    
404
   trouver un moyen de transformer en machine code les instructions de chaque
405
   spec peut etre associer a chaque imported node une minimachine et rajouter un
406
   champ a spec dans machine code pour stoquer memoire et instr *)
407

    
408
let pp_emf_stmt fmt stmt =
409
  match stmt with
410
  | Aut _ ->
411
    assert false
412
  | Eq eq ->
413
    fprintf fmt "@[ @[<v 2>\"%a\": {@ "
414
      (pp_print_list ~pp_sep:(fun fmt () -> pp_print_string fmt "_") pp_print_string)
415
      eq.eq_lhs;
416
    fprintf fmt "\"lhs\": [%a],@ "
417
      (pp_comma_list (fun fmt vid -> fprintf fmt "\"%s\"" vid))
418
      eq.eq_lhs;
419
    fprintf fmt "\"rhs\": %a,@ " pp_emf_expr eq.eq_rhs;
420
    fprintf fmt "@]@]@ }"
421

    
422
let pp_emf_stmts = pp_emf_list pp_emf_stmt
423

    
424
(* Printing the type declaration, not its use *)
425
let rec pp_emf_typ_dec fmt tydef_dec =
426
  fprintf fmt "{";
427
  (match tydef_dec with
428
  | Tydec_any ->
429
    fprintf fmt "\"kind\": \"any\""
430
  | Tydec_int ->
431
    fprintf fmt "\"kind\": \"int\""
432
  | Tydec_real ->
433
    fprintf fmt "\"kind\": \"real\""
434
  | Tydec_bool ->
435
    fprintf fmt "\"kind\": \"bool\""
436
  | Tydec_clock ck ->
437
    pp_emf_typ_dec fmt ck
438
  | Tydec_const c ->
439
    fprintf fmt "\"kind\": \"alias\",@ \"value\": \"%s\"" c
440
  | Tydec_enum el ->
441
    fprintf fmt "\"kind\": \"enum\",@ \"elements\": [%a]"
442
      (pp_comma_list (fun fmt e -> fprintf fmt "\"%s\"" e)) el
443
  | Tydec_struct s ->
444
    fprintf fmt "\"kind\": \"struct\",@ \"fields\": [%a]"
445
      (pp_comma_list (fun fmt (id, typ) ->
446
           fprintf fmt "\"%s\": %a" id pp_emf_typ_dec typ))
447
      s
448
  | Tydec_array (dim, typ) ->
449
    fprintf fmt "\"kind\": \"array\",@ \"dim\": @[%a@],@ \"base\": %a"
450
      pp_emf_dim dim pp_emf_typ_dec typ);
451
  fprintf fmt "}"
452

    
453
let pp_emf_typedef fmt typdef_top =
454
  let typedef = Corelang.typedef_of_top typdef_top in
455
  fprintf fmt "{ \"%s\": @[%a@] }" typedef.tydef_id pp_emf_typ_dec
456
    typedef.tydef_desc
457

    
458
let pp_emf_top_const fmt const_top =
459
  let const = Corelang.const_of_top const_top in
460
  fprintf fmt "{ \"%s\": %t }" const.const_id
461
    (pp_emf_cst const.const_value const.const_type)
462

    
463
(* Local Variables: *)
464
(* compile-command: "make -C ../.." *)
465
(* End: *)
(4-4/7)