Project

General

Profile

Download (11.9 KB) Statistics
| Branch: | Tag: | Revision:
1 2863281f ploc
open Lustre_types
2
open Machine_code_types
3
open Corelang
4
  
5
let print_statelocaltag = true
6
7 c35de73b ploc
let is_memory m id =
8 70be4acf ploc
  (List.exists (fun o -> o.var_id = id.var_id) m.mmemory) 
9 c35de73b ploc
10
let rec pp_val m fmt v =
11
  let pp_val = pp_val m in
12 2863281f ploc
  match v.value_desc with
13 c35de73b ploc
  | Cst c         -> Printers.pp_const fmt c 
14
  | Var v    ->
15
     if is_memory m v then
16 2863281f ploc
       if print_statelocaltag then
17 c35de73b ploc
	 Format.fprintf fmt "%s(S)" v.var_id
18 2863281f ploc
       else
19 c35de73b ploc
	 Format.pp_print_string fmt v.var_id 
20
     else     
21 2863281f ploc
       if print_statelocaltag then
22 c35de73b ploc
	 Format.fprintf fmt "%s(L)" v.var_id
23 2863281f ploc
       else
24
	 Format.pp_print_string fmt v.var_id
25 c35de73b ploc
  | Array vl      -> Format.fprintf fmt "[%a]" (Utils.fprintf_list ~sep:", " pp_val)  vl
26
  | Access (t, i) -> Format.fprintf fmt "%a[%a]" pp_val t pp_val i
27
  | Power (v, n)  -> Format.fprintf fmt "(%a^%a)" pp_val v pp_val n
28
  | Fun (n, vl)   -> Format.fprintf fmt "%s (%a)" n (Utils.fprintf_list ~sep:", " pp_val)  vl
29 2863281f ploc
30 c35de73b ploc
let rec  pp_instr m fmt i =
31
 let     pp_val = pp_val m and
32
      pp_branch = pp_branch m in
33 2863281f ploc
  let _ =
34
    match i.instr_desc with
35
    | MLocalAssign (i,v) -> Format.fprintf fmt "%s<-l- %a" i.var_id pp_val v
36
    | MStateAssign (i,v) -> Format.fprintf fmt "%s<-s- %a" i.var_id pp_val v
37
    | MReset i           -> Format.fprintf fmt "reset %s" i
38
    | MNoReset i         -> Format.fprintf fmt "noreset %s" i
39
    | MStep (il, i, vl)  ->
40
       Format.fprintf fmt "%a = %s (%a)"
41
	 (Utils.fprintf_list ~sep:", " (fun fmt v -> Format.pp_print_string fmt v.var_id)) il
42
	 i
43
	 (Utils.fprintf_list ~sep:", " pp_val) vl
44
    | MBranch (g,hl)     ->
45
       Format.fprintf fmt "@[<v 2>case(%a) {@,%a@,}@]"
46
	 pp_val g
47
	 (Utils.fprintf_list ~sep:"@," pp_branch) hl
48
    | MComment s -> Format.pp_print_string fmt s
49 1fd3d002 ploc
    | MSpec s -> Format.pp_print_string fmt ("@" ^ s)
50 2863281f ploc
       
51
  in
52
  (* Annotation *)
53
  (* let _ = *)
54
  (*   match i.lustre_expr with None -> () | Some e -> Format.fprintf fmt " -- original expr: %a" Printers.pp_expr e *)
55
  (* in *)
56
  let _ = 
57
    match i.lustre_eq with None -> () | Some eq -> Format.fprintf fmt " -- original eq: %a" Printers.pp_node_eq eq
58
  in
59
  ()
60
    
61 c35de73b ploc
and pp_branch m fmt (t, h) =
62
  Format.fprintf fmt "@[<v 2>%s:@,%a@]" t (Utils.fprintf_list ~sep:"@," (pp_instr m)) h
63 2863281f ploc
64 c35de73b ploc
and pp_instrs m fmt il =
65
  Format.fprintf fmt "@[<v 2>%a@]" (Utils.fprintf_list ~sep:"@," (pp_instr m)) il
66 2863281f ploc
67
68
(* merge log: get_node_def was in c0f8 *)
69
(* Returns the node/machine associated to id in m calls *)
70
let get_node_def id m =
71
  try
72
    let (decl, _) = List.assoc id m.mcalls in
73
    Corelang.node_of_top decl
74
  with Not_found -> ( 
75
    (* Format.eprintf "Unable to find node %s in list [%a]@.@?" *)
76
    (*   id *)
77
    (*   (Utils.fprintf_list ~sep:", " (fun fmt (n,_) -> Format.fprintf fmt "%s" n)) m.mcalls *)
78
    (* ; *)
79
    raise Not_found
80
  )
81
    
82
(* merge log: machine_vars was in 44686 *)
83
let machine_vars m = m.mstep.step_inputs @ m.mstep.step_locals @ m.mstep.step_outputs @ m.mmemory
84
85 c35de73b ploc
let pp_step m fmt s =
86 2863281f ploc
  Format.fprintf fmt "@[<v>inputs : %a@ outputs: %a@ locals : %a@ checks : %a@ instrs : @[%a@]@ asserts : @[%a@]@]@ "
87
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_inputs
88
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_outputs
89
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_locals
90 c35de73b ploc
    (Utils.fprintf_list ~sep:", " (fun fmt (_, c) -> pp_val m fmt c)) s.step_checks
91
    (Utils.fprintf_list ~sep:"@ " (pp_instr m)) s.step_instrs
92
    (Utils.fprintf_list ~sep:", " (pp_val m)) s.step_asserts
93 2863281f ploc
94
95
let pp_static_call fmt (node, args) =
96
 Format.fprintf fmt "%s<%a>"
97
   (node_name node)
98
   (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) args
99
100
let pp_machine fmt m =
101
  Format.fprintf fmt
102 7ee5f69e Lélio Brun
    "@[<v 2>machine %s@ \
103
     mem      : %a@ \
104
     instances: %a@ \
105
     init     : %a@ \
106
     const    : %a@ \
107
     step     :@   \
108
     @[<v 2>%a@]@ \
109
     spec     : @[%t@]@ \
110
     annot    : @[%a@]@]@ "
111 2863281f ploc
    m.mname.node_id
112
    (Utils.fprintf_list ~sep:", " Printers.pp_var) m.mmemory
113
    (Utils.fprintf_list ~sep:", " (fun fmt (o1, o2) -> Format.fprintf fmt "(%s, %a)" o1 pp_static_call o2)) m.minstances
114 c35de73b ploc
    (Utils.fprintf_list ~sep:"@ " (pp_instr m)) m.minit
115
    (Utils.fprintf_list ~sep:"@ " (pp_instr m)) m.mconst
116
    (pp_step m) m.mstep
117 7ee5f69e Lélio Brun
    (fun fmt -> match m.mspec.mnode_spec with
118
       | None -> ()
119
       | Some (NodeSpec id) -> Format.fprintf fmt "cocospec: %s" id
120
       | Some (Contract spec) -> Printers.pp_spec fmt spec)
121 2863281f ploc
    (Utils.fprintf_list ~sep:"@ " Printers.pp_expr_annot) m.mannot
122
123
let pp_machines fmt ml =
124
  Format.fprintf fmt "@[<v 0>%a@]" (Utils.fprintf_list ~sep:"@," pp_machine) ml
125
126
  
127
let rec is_const_value v =
128
  match v.value_desc with
129
  | Cst _          -> true
130 ca7e8027 Lélio Brun
  | Fun (_, args) -> Basic_library.is_value_internal_fun v && List.for_all is_const_value args
131 2863281f ploc
  | _              -> false
132
133
(* Returns the declared stateless status and the computed one. *)
134 9d693675 Lélio Brun
let get_stateless_status_node n =
135
  (n.node_dec_stateless,
136 1fd3d002 ploc
   try
137 9d693675 Lélio Brun
     Utils.desome n.node_stateless
138
   with _ -> failwith ("stateless status of machine " ^ n.node_id ^ " not computed"))
139
140
let get_stateless_status_top_decl td = match td.top_decl_desc with
141
  | Node n -> get_stateless_status_node n
142
  | ImportedNode n -> n.nodei_stateless, false
143
  | _ -> true, false
144
145
let get_stateless_status m =
146
  get_stateless_status_node m.mname
147 2863281f ploc
148 e4edf171 ploc
let is_stateless m = m.minstances = [] && m.mmemory = []
149
150 ca7e8027 Lélio Brun
(* let is_input m id =
151
 *   List.exists (fun o -> o.var_id = id.var_id) m.mstep.step_inputs *)
152 2863281f ploc
153
let is_output m id =
154
  List.exists (fun o -> o.var_id = id.var_id) m.mstep.step_outputs
155
156
157
let mk_conditional ?lustre_eq c t e =
158
  mkinstr ?lustre_eq:lustre_eq  (MBranch(c, [ (tag_true, t); (tag_false, e) ]))
159
160
161
162
let mk_val v t =
163
  { value_desc = v; 
164
    value_type = t; 
165
    value_annot = None }
166
    
167
let arrow_machine =
168
  let state = "_first" in
169
  let var_state = dummy_var_decl state Type_predef.type_bool(* (Types.new_ty Types.Tbool) *) in
170
  let var_input1 = List.nth Arrow.arrow_desc.node_inputs 0 in
171
  let var_input2 = List.nth Arrow.arrow_desc.node_inputs 1 in
172
  let var_output = List.nth Arrow.arrow_desc.node_outputs 0 in
173
  let cst b = mk_val (Cst (const_of_bool b)) Type_predef.type_bool in
174 867276c9 Guillaume DAVY
  assert(var_input1.var_type = var_input2.var_type);
175
  let t_arg = var_input1.var_type in (* TODO Xavier: c'est bien la bonne def ? Guillaume: Bof preferable de reprendre le type des variables non ? *)
176 2863281f ploc
  {
177
    mname = Arrow.arrow_desc;
178
    mmemory = [var_state];
179
    mcalls = [];
180
    minstances = [];
181
    minit = [mkinstr (MStateAssign(var_state, cst true))];
182
    mstatic = [];
183
    mconst = [];
184
    mstep = {
185
      step_inputs = Arrow.arrow_desc.node_inputs;
186
      step_outputs = Arrow.arrow_desc.node_outputs;
187
      step_locals = [];
188
      step_checks = [];
189 c35de73b ploc
      step_instrs = [mk_conditional (mk_val (Var var_state) Type_predef.type_bool)
190 2863281f ploc
			(List.map mkinstr
191
			[MStateAssign(var_state, cst false);
192 c35de73b ploc
			 MLocalAssign(var_output, mk_val (Var var_input1) t_arg)])
193 2863281f ploc
                        (List.map mkinstr
194 c35de73b ploc
			[MLocalAssign(var_output, mk_val (Var var_input2) t_arg)]) ];
195 2863281f ploc
      step_asserts = [];
196
    };
197 7ee5f69e Lélio Brun
    mspec = { mnode_spec = None; mtransitions = [] };
198 2863281f ploc
    mannot = [];
199 95fb046e ploc
    msch = None
200 2863281f ploc
  }
201
202
let empty_desc =
203
  {
204
    node_id = Arrow.arrow_id;
205
    node_type = Types.bottom;
206
    node_clock = Clocks.bottom;
207
    node_inputs= [];
208
    node_outputs= [];
209
    node_locals= [];
210
    node_gencalls = [];
211
    node_checks = [];
212
    node_asserts = [];
213
    node_stmts= [];
214
    node_dec_stateless = true;
215
    node_stateless = Some true;
216
    node_spec = None;
217 f4cba4b8 ploc
    node_annot = [];
218
    node_iscontract = false;
219
}
220 2863281f ploc
221
let empty_machine =
222
  {
223
    mname = empty_desc;
224
    mmemory = [];
225
    mcalls = [];
226
    minstances = [];
227
    minit = [];
228
    mstatic = [];
229
    mconst = [];
230
    mstep = {
231
      step_inputs = [];
232
      step_outputs = [];
233
      step_locals = [];
234
      step_checks = [];
235
      step_instrs = [];
236
      step_asserts = [];
237
    };
238 7ee5f69e Lélio Brun
    mspec = { mnode_spec = None; mtransitions = [] };
239 2863281f ploc
    mannot = [];
240 95fb046e ploc
    msch = None
241 2863281f ploc
  }
242
243
let new_instance =
244
  let cpt = ref (-1) in
245 59020713 ploc
  fun callee tag ->
246 2863281f ploc
    begin
247
      let o =
248
	if Stateless.check_node callee then
249
	  node_name callee
250
	else
251
	  Printf.sprintf "ni_%d" (incr cpt; !cpt) in
252
      let o =
253
	if !Options.ansi && is_generic_node callee
254 59020713 ploc
	then Printf.sprintf "%s_inst_%d"
255
               o
256
               (incr cpt; !cpt)
257 2863281f ploc
	else o in
258
      o
259
    end
260
261
262 e4edf171 ploc
let get_machine_opt machines name =
263 2863281f ploc
  List.fold_left
264
    (fun res m ->
265
      match res with
266
      | Some _ -> res
267
      | None -> if m.mname.node_id = name then Some m else None)
268
    None machines
269
270 e4edf171 ploc
let get_machine machines node_name =
271
 try
272 c3b0a8c9 ploc
    Utils.desome (get_machine_opt machines node_name) 
273
 with Utils.DeSome ->
274
   Format.eprintf "Unable to find machine %s in machines %a@.@?"
275
     node_name
276
     (Utils.fprintf_list ~sep:", " (fun fmt m -> Format.pp_print_string fmt m.mname.node_id)) machines
277
      ; assert false
278 e4edf171 ploc
     
279 2863281f ploc
let get_const_assign m id =
280
  try
281
    match get_instr_desc (List.find
282
	     (fun instr -> match get_instr_desc instr with
283
	     | MLocalAssign (v, _) -> v == id
284
	     | _ -> false)
285
	     m.mconst
286
    ) with
287
    | MLocalAssign (_, e) -> e
288
    | _                   -> assert false
289
  with Not_found -> assert false
290
291
292
let value_of_ident loc m id =
293
  (* is is a state var *)
294
  try
295
    let v = List.find (fun v -> v.var_id = id) m.mmemory
296 c35de73b ploc
    in mk_val (Var v) v.var_type 
297 2863281f ploc
  with Not_found ->
298
    try (* id is a node var *)
299
      let v = get_node_var id m.mname
300 c35de73b ploc
      in mk_val (Var v) v.var_type
301 2863281f ploc
  with Not_found ->
302
    try (* id is a constant *)
303
      let c = Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id))
304 c35de73b ploc
      in mk_val (Var c) c.var_type
305 2863281f ploc
    with Not_found ->
306
      (* id is a tag *)
307
      let t = Const_tag id
308
      in mk_val (Cst t) (Typing.type_const loc t)
309
310
(* type of internal fun used in dimension expression *)
311
let type_of_value_appl f args =
312
  if List.mem f Basic_library.arith_funs
313
  then (List.hd args).value_type
314
  else Type_predef.type_bool
315
316
let rec value_of_dimension m dim =
317
  match dim.Dimension.dim_desc with
318
  | Dimension.Dbool b         ->
319 e8f55c25 ploc
     mk_val (Cst (Const_tag (if b then tag_true else tag_false))) Type_predef.type_bool
320 2863281f ploc
  | Dimension.Dint i          ->
321
     mk_val (Cst (Const_int i)) Type_predef.type_int
322
  | Dimension.Dident v        -> value_of_ident dim.Dimension.dim_loc m v
323
  | Dimension.Dappl (f, args) ->
324
     let vargs = List.map (value_of_dimension m) args
325
     in mk_val (Fun (f, vargs)) (type_of_value_appl f vargs) 
326
  | Dimension.Dite (i, t, e)  ->
327
     (match List.map (value_of_dimension m) [i; t; e] with
328
     | [vi; vt; ve] -> mk_val (Fun ("ite", [vi; vt; ve])) vt.value_type
329
     | _            -> assert false)
330
  | Dimension.Dlink dim'      -> value_of_dimension m dim'
331
  | _                         -> assert false
332
333
let rec dimension_of_value value =
334
  match value.value_desc with
335 e8f55c25 ploc
  | Cst (Const_tag t) when t = tag_true  -> Dimension.mkdim_bool  Location.dummy_loc true
336
  | Cst (Const_tag t) when t = tag_false -> Dimension.mkdim_bool  Location.dummy_loc false
337 2863281f ploc
  | Cst (Const_int i)                             -> Dimension.mkdim_int   Location.dummy_loc i
338 c35de73b ploc
  | Var v                                         -> Dimension.mkdim_ident Location.dummy_loc v.var_id
339 2863281f ploc
  | Fun (f, args)                                 -> Dimension.mkdim_appl  Location.dummy_loc f (List.map dimension_of_value args)
340
  | _                                             -> assert false
341
342
343
     let rec join_branches hl1 hl2 =
344
 match hl1, hl2 with
345
 | []          , _            -> hl2
346
 | _           , []           -> hl1
347
 | (t1, h1)::q1, (t2, h2)::q2 ->
348
   if t1 < t2 then (t1, h1) :: join_branches q1 hl2 else
349
   if t1 > t2 then (t2, h2) :: join_branches hl1 q2
350
   else (t1, List.fold_right join_guards h1 h2) :: join_branches q1 q2
351
352
and join_guards inst1 insts2 =
353
 match get_instr_desc inst1, List.map get_instr_desc insts2 with
354
 | _                   , []                               ->
355
   [inst1]
356 ca7e8027 Lélio Brun
 | MBranch (x1, hl1), MBranch (x2, hl2) :: _ when x1 = x2 ->
357 2863281f ploc
    mkinstr
358
      (* TODO on pourrait uniquement concatener les lustres de inst1 et hd(inst2) *)
359
      (MBranch (x1, join_branches (sort_handlers hl1) (sort_handlers hl2)))
360
   :: (List.tl insts2)
361
 | _ -> inst1 :: insts2
362
363
let join_guards_list insts =
364
 List.fold_right join_guards insts []