Project

General

Profile

Download (9.28 KB) Statistics
| Branch: | Tag: | Revision:
1
open Lustre_types
2
open Machine_code_types
3
module VSet = Corelang.VSet
4
open Format
5
open Machine_code_common
6

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

    
14
let rec get_expr_vars v =
15
  match v.value_desc with
16
  | Cst c -> VSet.empty
17
  | Var v -> VSet.singleton v
18
  | Fun (_, args) -> List.fold_left (fun accu v -> VSet.union accu (get_expr_vars v)) VSet.empty args
19
  | _ -> assert false (* Invalid argument *)
20

    
21
let is_imported_node f m =
22
  let (decl, _) = List.assoc f m.mcalls in
23
  Corelang.is_imported_node decl
24

    
25
(* Handling of enumerated types: for the moment each of such type is transformed
26
   into an int: the idx number of the constant in the typedef. This is not so
27
   nice but is compatible with basic Simulink types: int, real, bools) *)
28
(*
29
let recorded_enums = ref []
30
let record_types prog =
31
  let typedefs = Corelang.get_typedefs prog in
32
  List.iter (fun top ->
33
    let consts = consts_of_enum_type top in
34
  ) prog
35
*)
36
    
37
(* Basic printing functions *)
38

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

    
69
(********* Printing types ***********)
70
(* Two cases:
71
   - printing a variable definition:
72
     -  we look at the declared type if available
73
     - if not, we print the inferred type
74

    
75
   - printing a constant definion
76
*)
77
  
78
  
79
let pp_tag_type fmt typ =
80
  let rec aux tydec_desc =
81
  match tydec_desc with  
82
  | Tydec_int -> fprintf fmt "int"
83
  | Tydec_real -> fprintf fmt "real"
84
  | Tydec_bool -> fprintf fmt "bool"
85
  | Tydec_clock ty -> aux ty
86
  | Tydec_enum const_list -> (
87
    let size = List.length const_list in
88
    if size < 255 then
89
      fprintf fmt "uint8"
90
    else if size < 65535 then
91
      fprintf fmt "uint16"
92
    else
93
      assert false (* Too much states. This not reasonable *)
94
  )
95
  | Tydec_const _ | Tydec_struct _ | Tydec_array _ | Tydec_any -> eprintf "unhandled cst tag in EMF: %a@." Printers.pp_var_type_dec_desc tydec_desc; assert false
96
  in
97
  aux typ.tydef_desc
98

    
99
     
100
let pp_cst_type fmt c (*infered_typ*) =
101
  match c with
102
  | Const_tag t ->
103
     let typ = (Corelang.typedef_of_top (Hashtbl.find Corelang.tag_table t)) in
104
     if typ.tydef_id = "bool" then
105
       fprintf fmt "bool"
106
     else
107
       pp_tag_type fmt typ
108
  | Const_int _ -> fprintf fmt "int" (*!Options.int_type*)
109
  | Const_real _ -> fprintf fmt "real" (*!Options.real_type*)
110
  | Const_string _ -> fprintf fmt "string" 
111
  | _ -> eprintf "cst: %a@." Printers.pp_const c; assert false
112

    
113
let rec pp_infered_type fmt t =
114
  let open Types in
115
  if is_bool_type t  then fprintf fmt "bool" else
116
  if is_int_type t then fprintf fmt "int" else (* !Options.int_type *)
117
  if is_real_type t then fprintf fmt "real" else (* !Options.real_type *)
118
  match t.tdesc with
119
  | Tclock t ->
120
     pp_infered_type fmt t
121
  | Tstatic (_, t) ->
122
     fprintf fmt "%a" pp_infered_type t
123
  | Tconst id ->
124
    (* This is a type id for a enumerated type, eg. introduced by automata *)
125
     let typ =
126
       (Corelang.typedef_of_top (Hashtbl.find Corelang.type_table (Tydec_const id)))
127
     in
128
     pp_tag_type fmt typ
129
   | Tlink ty -> 
130
       pp_infered_type fmt ty 
131
   | _ -> eprintf "unhandled type: %a@." Types.print_node_ty t; assert false
132
     
133
let rec pp_concrete_type dec_t infered_t fmt =
134
  match dec_t with
135
  | Tydec_int -> fprintf fmt "int" (* !Options.int_type *)
136
  | Tydec_real -> fprintf fmt "real" (* !Options.real_type *)
137
  (* TODO we could add more concrete types here if they were available in
138
     dec_t *)
139
  | Tydec_bool -> fprintf fmt "bool"
140
  | Tydec_clock t -> pp_concrete_type t infered_t fmt
141
  | Tydec_const id -> (
142
    (* This is a type id for a enumerated type, eg. introduced by automata *)
143
    let typ = (Corelang.typedef_of_top (Hashtbl.find Corelang.type_table dec_t)) in
144
    pp_tag_type fmt typ
145
  )
146
  | Tydec_any -> pp_infered_type fmt infered_t 
147
  | _ -> eprintf
148
     "unhandled construct in type printing for EMF backend: %a@."
149
     Printers.pp_var_type_dec_desc dec_t; raise (Failure "var")
150
       
151

    
152
(*let pp_cst_type fmt v =
153
  match v.value_desc with
154
  | Cst c-> pp_cst_type c v.value_type fmt (* constants do not have declared type (yet) *)
155
  | _ -> assert false
156
*)
157
       
158
let pp_var_type fmt v =
159
  try
160
    if Machine_types.is_specified v then
161
      Machine_types.pp_var_type fmt v
162
    else
163
      pp_concrete_type v.var_dec_type.ty_dec_desc v.var_type fmt
164
  with Failure _ -> eprintf "failed var: %a@." Printers.pp_var v; assert false
165
    
166
(******** Other print functions *)
167
    
168
let pp_emf_var_decl fmt v =
169
  fprintf fmt "@[{\"name\": \"%a\", \"datatype\":\"%a\", \"original_name\": \"%a\"}@]"
170
    pp_var_name v
171
    pp_var_type v
172
    Printers.pp_var_name v
173
    
174
let pp_emf_vars_decl fmt vl =
175
  fprintf fmt "@[";
176
  Utils.fprintf_list ~sep:",@ " pp_emf_var_decl fmt vl;
177
  fprintf fmt "@]"
178
  
179
let reset_name id =
180
  "reset_" ^ id
181
  
182
let pp_tag_id fmt t =
183
  let typ = (Corelang.typedef_of_top (Hashtbl.find Corelang.tag_table t)) in
184
  if typ.tydef_id = "bool" then
185
    pp_print_string fmt t
186
  else
187
    let const_list = match typ.tydef_desc with Tydec_enum tl -> tl | _ -> assert false in
188
    fprintf fmt "%i" (get_idx t const_list)
189

    
190
let pp_emf_cst fmt c =
191
  match c with
192
  | Const_tag t->
193
     let typ = (Corelang.typedef_of_top (Hashtbl.find Corelang.tag_table t)) in
194
     if typ.tydef_id = "bool" then (
195
       fprintf fmt "{@[\"type\": \"constant\",@ ";
196
       fprintf fmt"\"value\": \"%a\",@ "
197
	 Printers.pp_const c;
198
       fprintf fmt "\"datatype\": \"%a\"@ " pp_cst_type c;
199
       fprintf fmt "@]}"
200
     )
201
     else (
202
       fprintf fmt "{@[\"type\": \"constant\",@ \"value\": \"%a\",@ " 
203
	 pp_tag_id t;
204
       fprintf fmt "\"origin_type\": \"%s\",@ \"origin_value\": \"%s\",@ "
205
	 typ.tydef_id t;
206
       fprintf fmt "\"datatype\": \"%a\"@ " pp_cst_type c;
207
       fprintf fmt "@]}"
208
     )
209
  | Const_string s ->
210
    fprintf fmt "{@[\"type\": \"constant\",@ \"value\": \"%s\",@ " s;
211
    fprintf fmt "\"datatype\": \"%a\"@ " pp_cst_type c;
212
    fprintf fmt "@]}"
213
     
214
  | _ -> (
215
    fprintf fmt "{@[\"type\": \"constant\",@ \"value\": \"%a\",@ "
216
      Printers.pp_const c;
217
    fprintf fmt "\"datatype\": \"%a\"@ " pp_cst_type c;
218
    fprintf fmt "@]}"
219
  )
220
  
221
  
222
let pp_emf_cst_or_var m fmt v =
223
  match v.value_desc with
224
  | Cst c -> pp_emf_cst fmt c
225
  | Var v -> (
226
    fprintf fmt "{@[\"type\": \"variable\",@ \"value\": \"%a\",@ "
227
      pp_var_name v;
228
    (*    fprintf fmt "\"original_name\": \"%a\",@ " Printers.pp_var_name v; *)
229
    fprintf fmt "\"datatype\": \"%a\"@ " pp_var_type v;
230
    fprintf fmt "@]}"
231
  )
232
  | _ -> eprintf "Not of cst or var: %a@." (pp_val m) v ; assert false (* Invalid argument *)
233

    
234

    
235
let pp_emf_cst_or_var_list m =
236
  Utils.fprintf_list ~sep:",@ " (pp_emf_cst_or_var m)
237

    
238
(* Printer lustre expr and eexpr *)
239
    
240
let rec pp_emf_expr fmt e =
241
  match e.expr_desc with
242
  | Expr_const c -> pp_emf_cst fmt c
243
  | Expr_ident id ->
244
     fprintf fmt "{@[\"type\": \"variable\",@ \"value\": \"%a\",@ "
245
       print_protect (fun fmt -> pp_print_string fmt id);
246
    fprintf fmt "\"datatype\": \"%t\"@ "
247
      (pp_concrete_type
248
	 Tydec_any (* don't know much about that time since it was not
249
		      declared. That may not work with clock constants *)
250
	 e.expr_type
251
      );
252
    fprintf fmt "@]}"
253

    
254
  | Expr_tuple el ->
255
     fprintf fmt "[@[<hov 0>%a@ @]]"
256
       (Utils.fprintf_list ~sep:",@ " pp_emf_expr) el
257
  | _ -> (
258
    Log.report ~level:2
259
      (fun fmt ->
260
	fprintf fmt "Warning: unhandled expression %a in annotation.@ "
261
	  Printers.pp_expr e;
262
	fprintf fmt "Will not be produced in the experted JSON EMF"
263
      );    
264
    fprintf fmt "\"unhandled construct, complain to Ploc\""
265
  )
266
(* Remaining constructs *)  
267
(* | Expr_ite   of expr * expr * expr *)
268
(* | Expr_arrow of expr * expr *)
269
(* | Expr_fby of expr * expr *)
270
(* | Expr_array of expr list *)
271
(* | Expr_access of expr * Dimension.dim_expr *)
272
(* | Expr_power of expr * Dimension.dim_expr *)
273
(* | Expr_pre of expr *)
274
(* | Expr_when of expr * ident * label *)
275
(* | Expr_merge of ident * (label * expr) list *)
276
(* | Expr_appl of call_t *)
277
     
278

    
279
let pp_emf_eexpr fmt ee =
280
  fprintf fmt "{@[<hov 0>\"quantifiers\": \"%a\",@ \"qfexpr\": @[%a@]@] }"
281
    (Utils.fprintf_list ~sep:"; " Printers.pp_quantifiers) ee.eexpr_quantifiers
282
    pp_emf_expr ee.eexpr_qfexpr
283
    
284
    
285
(* Local Variables: *)
286
(* compile-command: "make -C ../.." *)
287
(* End: *)
(3-3/4)