Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / EMF / EMF_common.ml @ 8446bf03

History | View | Annotate | Download (8.96 KB)

1
open Lustre_types
2
open Machine_code_types
3

    
4
open Format
5
open Machine_code 
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
  | LocalVar v | StateVar 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 const_list = match typ.tydef_desc with Tydec_enum tl -> tl | _ -> assert false in
81
  let size = List.length const_list in
82
  if size < 255 then
83
    fprintf fmt "uint8"
84
  else if size < 65535 then
85
fprintf fmt "uint16"
86
  else
87
    assert false (* Too much states. This not reasonable *)
88
      
89
   
90
     
91
let pp_cst_type fmt c (*infered_typ*) =
92
  match c with
93
  | Const_tag t ->
94
     let typ = (Corelang.typedef_of_top (Hashtbl.find Corelang.tag_table t)) in
95
     if typ.tydef_id = "bool" then
96
       fprintf fmt "bool"
97
     else
98
       pp_tag_type fmt typ
99
  | Const_int _ -> fprintf fmt "int" (*!Options.int_type*)
100
  | Const_real _ -> fprintf fmt "real" (*!Options.real_type*)
101
  | Const_string _ -> fprintf fmt "string" 
102
  | _ -> eprintf "cst: %a@." Printers.pp_const c; assert false
103

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

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

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

    
226

    
227
let pp_emf_cst_or_var_list =
228
  Utils.fprintf_list ~sep:",@ " pp_emf_cst_or_var
229

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

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

    
271
let pp_emf_eexpr fmt ee =
272
  fprintf fmt "{@[<hov 0>\"quantifiers\": \"%a\",@ \"qfexpr\": @[%a@]@] }"
273
    (Utils.fprintf_list ~sep:"; " Printers.pp_quantifiers) ee.eexpr_quantifiers
274
    pp_emf_expr ee.eexpr_qfexpr
275
    
276
    
277
(* Local Variables: *)
278
(* compile-command: "make -C ../.." *)
279
(* End: *)