Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / EMF / EMF_common.ml @ 568b5a26

History | View | Annotate | Download (6.82 KB)

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 = 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 c infered_typ fmt =
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 "%s" !Options.int_type
98
  | Const_real _ -> fprintf fmt "%s" !Options.real_type
99
  | _ -> Format.eprintf "cst: %a@." Printers.pp_const c; assert false
100

    
101
let rec pp_infered_type fmt t =
102
  let open Types in
103
  match t.tdesc with
104
  | Tint ->
105
     fprintf fmt "%s" !Options.int_type
106
  | Treal ->
107
     fprintf fmt "%s" !Options.real_type
108
  | Tbool ->
109
     fprintf fmt "bool"
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
  | _ -> Format.eprintf "unhandled type: %a@." Types.print_node_ty t; assert false
123
let rec pp_concrete_type dec_t infered_t fmt =
124
  match dec_t with
125
  | Tydec_int -> fprintf fmt "%s" !Options.int_type
126
  | Tydec_real -> fprintf fmt "%s" !Options.real_type
127
  (* TODO we could add more concrete types here if they were available in
128
     dec_t *)
129
  | Tydec_bool -> fprintf fmt "bool"
130
  | Tydec_clock t -> pp_concrete_type t infered_t fmt
131
  | Tydec_const id -> (
132
    (* This is a type id for a enumerated type, eg. introduced by automata *)
133
    let typ = (Corelang.typedef_of_top (Hashtbl.find Corelang.type_table dec_t)) in
134
    pp_tag_type fmt typ
135
  )
136
  | Tydec_any -> pp_infered_type fmt infered_t 
137
  | _ -> Format.eprintf
138
     "unhandled construct in type printing for EMF backend: %a@."
139
     Printers.pp_var_type_dec_desc dec_t; raise (Failure "var")
140
       
141

    
142
let pp_cst_type fmt v =
143
  match v.value_desc with
144
  | Cst c-> pp_cst_type c v.value_type fmt (* constants do not have declared type (yet) *)
145
  | _ -> assert false
146
     
147
let pp_var_type fmt v =
148
  try
149
  pp_concrete_type v.var_dec_type.ty_dec_desc v.var_type fmt
150
  with Failure _ -> Format.eprintf "failed var: %a@." Printers.pp_var v; assert false
151
(******** Other print functions *)
152
    
153
let pp_emf_var_decl fmt v =
154
  fprintf fmt "@[{\"name\": \"%a\", \"datatype\":\"%a\"}@]"
155
    pp_var_name v
156
    pp_var_type v
157
    
158
let pp_emf_vars_decl fmt vl =
159
  fprintf fmt "@[";
160
  Utils.fprintf_list ~sep:",@ " pp_emf_var_decl fmt vl;
161
  fprintf fmt "@]"
162
  
163
let reset_name id =
164
  "reset_" ^ id
165
  
166
let pp_tag_id fmt t =
167
  let typ = (Corelang.typedef_of_top (Hashtbl.find Corelang.tag_table t)) in
168
  if typ.tydef_id = "bool" then
169
    pp_print_string fmt t
170
  else
171
    let const_list = match typ.tydef_desc with Tydec_enum tl -> tl | _ -> assert false in
172
    fprintf fmt "%i" (get_idx t const_list)
173
     
174
let pp_emf_cst_or_var fmt v =
175
  match v.value_desc with
176
  | Cst ((Const_tag t) as c)->
177
     let typ = (Corelang.typedef_of_top (Hashtbl.find Corelang.tag_table t)) in
178
     if typ.tydef_id = "bool" then (
179
       fprintf fmt "{@[\"type\": \"constant\",@ ";
180
       fprintf fmt"\"value\": \"%a\",@ "
181
	 Printers.pp_const c;
182
       fprintf fmt "\"datatype\": \"%a\"@ " pp_cst_type v;
183
       fprintf fmt "@]}"
184
     )
185
     else (
186
       fprintf fmt "{@[\"type\": \"constant\",@ \"value\": \"%a\",@ " 
187
	 pp_tag_id t;
188
       fprintf fmt "\"origin_type\": \"%s\",@ \"origin_value\": \"%s\",@ "
189
	 typ.tydef_id t;
190
       fprintf fmt "\"datatype\": \"%a\"@ " pp_cst_type v;
191
       fprintf fmt "@]}"
192
     )
193
  | Cst c -> (
194
    fprintf fmt "{@[\"type\": \"constant\",@ \"value\": \"%a\",@ "
195
      Printers.pp_const c;
196
    fprintf fmt "\"datatype\": \"%a\"@ " pp_cst_type v;
197
    fprintf fmt "@]}"
198
  )
199
  | LocalVar v
200
  | StateVar v -> (
201
    fprintf fmt "{@[\"type\": \"variable\",@ \"value\": \"%a\",@ "
202
      pp_var_name v;
203
    fprintf fmt "\"datatype\": \"%a\"@ " pp_var_type v;
204
    fprintf fmt "@]}"
205
  )
206
  | _ -> Format.eprintf "Not of cst or var: %a@." Machine_code.pp_val v ; assert false (* Invalid argument *)
207

    
208

    
209
let pp_emf_cst_or_var_list =
210
  Utils.fprintf_list ~sep:",@ " pp_emf_cst_or_var
211

    
212

    
213
(* Local Variables: *)
214
(* compile-command: "make -C ../.." *)
215
(* End: *)