Project

General

Profile

Download (8.94 KB) Statistics
| Branch: | Tag: | Revision:
1
open LustreSpec
2
open Format
3
open Machine_code 
4

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

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

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

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

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

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

    
73
   - printing a constant definion
74
*)
75
  
76
  
77
let pp_tag_type fmt typ =
78
  let const_list = match typ.tydef_desc with Tydec_enum tl -> tl | _ -> assert false in
79
  let size = List.length const_list in
80
  if size < 255 then
81
    fprintf fmt "uint8"
82
  else if size < 65535 then
83
fprintf fmt "uint16"
84
  else
85
    assert false (* Too much states. This not reasonable *)
86
      
87
   
88
     
89
let pp_cst_type fmt c (*infered_typ*) =
90
  match c with
91
  | Const_tag t ->
92
     let typ = (Corelang.typedef_of_top (Hashtbl.find Corelang.tag_table t)) in
93
     if typ.tydef_id = "bool" then
94
       fprintf fmt "bool"
95
     else
96
       pp_tag_type fmt typ
97
  | Const_int _ -> fprintf fmt "int" (*!Options.int_type*)
98
  | Const_real _ -> fprintf fmt "real" (*!Options.real_type*)
99
  | Const_string _ -> fprintf fmt "string" 
100
  | _ -> eprintf "cst: %a@." Printers.pp_const c; assert false
101

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

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

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

    
224

    
225
let pp_emf_cst_or_var_list =
226
  Utils.fprintf_list ~sep:",@ " pp_emf_cst_or_var
227

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

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

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