Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / machine_code.ml @ b38ffff3

History | View | Annotate | Download (21.3 KB)

1
(********************************************************************)
2
(*                                                                  *)
3
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
4
(*  Copyright 2012 -    --   ONERA - CNRS - INPT                    *)
5
(*                                                                  *)
6
(*  LustreC is free software, distributed WITHOUT ANY WARRANTY      *)
7
(*  under the terms of the GNU Lesser General Public License        *)
8
(*  version 2.1.                                                    *)
9
(*                                                                  *)
10
(********************************************************************)
11

    
12
open LustreSpec
13
open Corelang
14
open Clocks
15
open Causality
16

    
17
exception NormalizationError
18

    
19
module OrdVarDecl:Map.OrderedType with type t=var_decl =
20
  struct type t = var_decl;; let compare = compare end
21

    
22
module ISet = Set.Make(OrdVarDecl)
23

    
24
type value_t = 
25
  | Cst of constant
26
  | LocalVar of var_decl
27
  | StateVar of var_decl
28
  | Fun of ident * value_t list 
29
  | Array of value_t list
30
  | Access of value_t * value_t
31
  | Power of value_t * value_t
32

    
33
type instr_t =
34
  | MLocalAssign of var_decl * value_t
35
  | MStateAssign of var_decl * value_t
36
  | MReset of ident
37
  | MStep of var_decl list * ident * value_t list
38
  | MBranch of value_t * (label * instr_t list) list
39
 
40
let rec pp_val fmt v =
41
  match v with
42
    | Cst c         -> Printers.pp_const fmt c 
43
    | LocalVar v    -> Format.pp_print_string fmt v.var_id
44
    | StateVar v    -> Format.pp_print_string fmt v.var_id
45
    | Array vl      -> Format.fprintf fmt "[%a]" (Utils.fprintf_list ~sep:", " pp_val)  vl
46
    | Access (t, i) -> Format.fprintf fmt "%a[%a]" pp_val t pp_val i
47
    | Power (v, n)  -> Format.fprintf fmt "(%a^%a)" pp_val v pp_val n
48
    | Fun (n, vl)   -> Format.fprintf fmt "%s (%a)" n (Utils.fprintf_list ~sep:", " pp_val)  vl
49

    
50
let rec pp_instr fmt i =
51
  match i with 
52
    | MLocalAssign (i,v) -> Format.fprintf fmt "%s<-l- %a" i.var_id pp_val v
53
    | MStateAssign (i,v) -> Format.fprintf fmt "%s<-s- %a" i.var_id pp_val v
54
    | MReset i           -> Format.fprintf fmt "reset %s" i
55
    | MStep (il, i, vl)  ->
56
      Format.fprintf fmt "%a = %s (%a)"
57
	(Utils.fprintf_list ~sep:", " (fun fmt v -> Format.pp_print_string fmt v.var_id)) il
58
	i      
59
	(Utils.fprintf_list ~sep:", " pp_val) vl
60
    | MBranch (g,hl)     ->
61
      Format.fprintf fmt "@[<v 2>case(%a) {@,%a@,}@]"
62
	pp_val g
63
	(Utils.fprintf_list ~sep:"@," pp_branch) hl
64

    
65
and pp_branch fmt (t, h) =
66
  Format.fprintf fmt "@[<v 2>%s:@,%a@]" t (Utils.fprintf_list ~sep:"@," pp_instr) h
67

    
68
type step_t = {
69
  step_checks: (Location.t * value_t) list;
70
  step_inputs: var_decl list;
71
  step_outputs: var_decl list;
72
  step_locals: var_decl list;
73
  step_instrs: instr_t list;
74
  step_asserts: value_t list;
75
}
76

    
77
type static_call = top_decl * (Dimension.dim_expr list)
78

    
79
type machine_t = {
80
  mname: node_desc;
81
  mmemory: var_decl list;
82
  mcalls: (ident * static_call) list; (* map from stateful/stateless instance to node, no internals *)
83
  minstances: (ident * static_call) list; (* sub-map of mcalls, from stateful instance to node *)
84
  minit: instr_t list;
85
  mstatic: var_decl list; (* static inputs only *)
86
  mstep: step_t;
87
  mspec: node_annot option;
88
  mannot: expr_annot list;
89
}
90

    
91
let pp_step fmt s =
92
  Format.fprintf fmt "@[<v>inputs : %a@ outputs: %a@ locals : %a@ checks : %a@ instrs : @[%a@]@ asserts : @[%a@]@]@ "
93
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_inputs
94
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_outputs
95
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_locals
96
    (Utils.fprintf_list ~sep:", " (fun fmt (_, c) -> pp_val fmt c)) s.step_checks
97
    (Utils.fprintf_list ~sep:"@ " pp_instr) s.step_instrs
98
    (Utils.fprintf_list ~sep:", " pp_val) s.step_asserts
99

    
100

    
101
let pp_static_call fmt (node, args) =
102
 Format.fprintf fmt "%s<%a>"
103
   (node_name node)
104
   (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) args
105

    
106
let pp_machine fmt m =
107
  Format.fprintf fmt 
108
    "@[<v 2>machine %s@ mem      : %a@ instances: %a@ init     : %a@ step     :@   @[<v 2>%a@]@ @  spec : @[%t@]@  annot : @[%a@]@]@ "
109
    m.mname.node_id
110
    (Utils.fprintf_list ~sep:", " Printers.pp_var) m.mmemory
111
    (Utils.fprintf_list ~sep:", " (fun fmt (o1, o2) -> Format.fprintf fmt "(%s, %a)" o1 pp_static_call o2)) m.minstances
112
    (Utils.fprintf_list ~sep:"@ " pp_instr) m.minit
113
    pp_step m.mstep
114
    (fun fmt -> match m.mspec with | None -> () | Some spec -> Printers.pp_spec fmt spec)
115
    (Utils.fprintf_list ~sep:"@ " Printers.pp_expr_annot) m.mannot
116

    
117
(* Returns the declared stateless status and the computed one. *)
118
let get_stateless_status m =
119
 (m.mname.node_dec_stateless, Utils.desome m.mname.node_stateless)
120

    
121
let is_input m id =
122
  List.exists (fun o -> o.var_id = id.var_id) m.mstep.step_inputs
123

    
124
let is_output m id =
125
  List.exists (fun o -> o.var_id = id.var_id) m.mstep.step_outputs
126

    
127
let is_memory m id =
128
  List.exists (fun o -> o.var_id = id.var_id) m.mmemory
129

    
130
let conditional c t e =
131
  MBranch(c, [ (tag_true, t); (tag_false, e) ])
132

    
133
let dummy_var_decl name typ =
134
  {
135
    var_id = name;
136
    var_dec_type = dummy_type_dec;
137
    var_dec_clock = dummy_clock_dec;
138
    var_dec_const = false;
139
    var_type =  typ;
140
    var_clock = Clocks.new_ck (Clocks.Cvar Clocks.CSet_all) true;
141
    var_loc = Location.dummy_loc
142
  }
143

    
144
let arrow_id = "_arrow"
145

    
146
let arrow_typ = Types.new_ty Types.Tunivar
147

    
148
let arrow_desc =
149
  {
150
    node_id = arrow_id;
151
    node_type = Type_predef.type_bin_poly_op;
152
    node_clock = Clock_predef.ck_bin_univ;
153
    node_inputs= [dummy_var_decl "_in1" arrow_typ; dummy_var_decl "_in2" arrow_typ];
154
    node_outputs= [dummy_var_decl "_out" arrow_typ];
155
    node_locals= [];
156
    node_gencalls = [];
157
    node_checks = [];
158
    node_asserts = [];
159
    node_eqs= [];
160
    node_dec_stateless = false;
161
    node_stateless = Some false;
162
    node_spec = None;
163
    node_annot = [];  }
164

    
165
let arrow_top_decl =
166
  {
167
    top_decl_desc = Node arrow_desc;
168
    top_decl_loc = Location.dummy_loc
169
  }
170

    
171
let arrow_machine =
172
  let state = "_first" in
173
  let var_state = dummy_var_decl state (Types.new_ty Types.Tbool) in
174
  let var_input1 = List.nth arrow_desc.node_inputs 0 in
175
  let var_input2 = List.nth arrow_desc.node_inputs 1 in
176
  let var_output = List.nth arrow_desc.node_outputs 0 in
177
  {
178
    mname = arrow_desc;
179
    mmemory = [var_state];
180
    mcalls = [];
181
    minstances = [];
182
    minit = [MStateAssign(var_state, Cst (const_of_bool true))];
183
    mstatic = [];
184
    mstep = {
185
      step_inputs = arrow_desc.node_inputs;
186
      step_outputs = arrow_desc.node_outputs;
187
      step_locals = [];
188
      step_checks = [];
189
      step_instrs = [conditional (StateVar var_state)
190
			         [MStateAssign(var_state, Cst (const_of_bool false));
191
                                  MLocalAssign(var_output, LocalVar var_input1)]
192
                                 [MLocalAssign(var_output, LocalVar var_input2)] ];
193
      step_asserts = [];
194
    };
195
    mspec = None;
196
    mannot = [];
197
  }
198

    
199
let new_instance =
200
  let cpt = ref (-1) in
201
  fun caller callee tag ->
202
    begin
203
      let o =
204
	if Stateless.check_node callee then
205
	  node_name callee
206
	else
207
	  Printf.sprintf "ni_%d" (incr cpt; !cpt) in
208
      let o =
209
	if !Options.ansi && is_generic_node callee
210
	then Printf.sprintf "%s_inst_%d" o (Utils.position (fun e -> e.expr_tag = tag) caller.node_gencalls)
211
	else o in
212
      o
213
    end
214

    
215
(* translate_<foo> : node -> context -> <foo> -> machine code/expression *)
216
(* the context contains  m : state aka memory variables  *)
217
(*                      si : initialization instructions *)
218
(*                       j : node aka machine instances  *)
219
(*                       d : local variables             *)
220
(*                       s : step instructions           *)
221
let translate_ident node (m, si, j, d, s) id =
222
  try (* id is a node var *)
223
    let var_id = get_node_var id node in
224
    if ISet.exists (fun v -> v.var_id = id) m
225
    then StateVar var_id
226
    else LocalVar var_id
227
  with Not_found -> (* id is a constant *)
228
    LocalVar (Corelang.var_decl_of_const (Hashtbl.find Corelang.consts_table id))
229

    
230
let rec control_on_clock node ((m, si, j, d, s) as args) ck inst =
231
 match (Clocks.repr ck).cdesc with
232
 | Con    (ck1, cr, l) ->
233
   let id  = Clocks.const_of_carrier cr in
234
   control_on_clock node args ck1 (MBranch (translate_ident node args id,
235
					    [l, [inst]] ))
236
 | _                   -> inst
237

    
238
let rec join_branches hl1 hl2 =
239
 match hl1, hl2 with
240
 | []          , _            -> hl2
241
 | _           , []           -> hl1
242
 | (t1, h1)::q1, (t2, h2)::q2 ->
243
   if t1 < t2 then (t1, h1) :: join_branches q1 hl2 else
244
   if t1 > t2 then (t2, h2) :: join_branches hl1 q2
245
   else (t1, List.fold_right join_guards h1 h2) :: join_branches q1 q2
246

    
247
and join_guards inst1 insts2 =
248
 match inst1, insts2 with
249
 | _                   , []                               ->
250
   [inst1]
251
 | MBranch (x1, hl1), MBranch (x2, hl2) :: q when x1 = x2 ->
252
   MBranch (x1, join_branches (sort_handlers hl1) (sort_handlers hl2))
253
   :: q
254
 | _ -> inst1 :: insts2
255

    
256
let join_guards_list insts =
257
 List.fold_right join_guards insts []
258

    
259
(* specialize predefined (polymorphic) operators
260
   wrt their instances, so that the C semantics 
261
   is preserved *)
262
let specialize_to_c expr =
263
 match expr.expr_desc with
264
 | Expr_appl (id, e, r) ->
265
   if List.exists (fun e -> Types.is_bool_type e.expr_type) (expr_list_of_expr e)
266
   then let id =
267
	  match id with
268
	  | "="  -> "equi"
269
	  | "!=" -> "xor"
270
	  | _    -> id in
271
	{ expr with expr_desc = Expr_appl (id, e, r) }
272
   else expr
273
 | _ -> expr
274

    
275
let specialize_op expr =
276
  match !Options.output with
277
  | "C" -> specialize_to_c expr
278
  | _   -> expr
279

    
280
let rec translate_expr node ((m, si, j, d, s) as args) expr =
281
 let expr = specialize_op expr in
282
 match expr.expr_desc with
283
 | Expr_const v                     -> Cst v
284
 | Expr_ident x                     -> translate_ident node args x
285
 | Expr_array el                    -> Array (List.map (translate_expr node args) el)
286
 | Expr_access (t, i)               -> Access (translate_expr node args t, translate_expr node args (expr_of_dimension i))
287
 | Expr_power  (e, n)               -> Power  (translate_expr node args e, translate_expr node args (expr_of_dimension n))
288
 | Expr_tuple _
289
 | Expr_arrow _ 
290
 | Expr_fby _
291
 | Expr_pre _                       -> (Printers.pp_expr Format.err_formatter expr; Format.pp_print_flush Format.err_formatter (); raise NormalizationError)
292
 | Expr_when    (e1, _, _)          -> translate_expr node args e1
293
 | Expr_merge   (x, _)              -> raise NormalizationError
294
 | Expr_appl (id, e, _) when Basic_library.is_internal_fun id ->
295
   let nd = node_from_name id in
296
   Fun (node_name nd, List.map (translate_expr node args) (expr_list_of_expr e))
297
 | Expr_ite (g,t,e) -> (
298
   (* special treatment depending on the active backend. For horn backend, ite
299
      are preserved in expression. While they are removed for C or Java
300
      backends. *)
301
   match !Options.output with | "horn" -> 
302
     Fun ("ite", [translate_expr node args g; translate_expr node args t; translate_expr node args e])
303
   | "C" | "java" | _ -> 
304
     (Printers.pp_expr Format.err_formatter expr; Format.pp_print_flush Format.err_formatter (); raise NormalizationError)
305
 )
306
 | _                   -> raise NormalizationError
307

    
308
let translate_guard node args expr =
309
  match expr.expr_desc with
310
  | Expr_ident x  -> translate_ident node args x
311
  | _ -> (Format.eprintf "internal error: translate_guard %s %a@." node.node_id Printers.pp_expr expr;assert false)
312

    
313
let rec translate_act node ((m, si, j, d, s) as args) (y, expr) =
314
  match expr.expr_desc with
315
  | Expr_ite   (c, t, e) -> let g = translate_guard node args c in
316
			    conditional g [translate_act node args (y, t)]
317
                              [translate_act node args (y, e)]
318
  | Expr_merge (x, hl)   -> MBranch (translate_ident node args x, List.map (fun (t,  h) -> t, [translate_act node args (y, h)]) hl)
319
  | _                    -> MLocalAssign (y, translate_expr node args expr)
320

    
321
let reset_instance node args i r c =
322
  match r with
323
  | None        -> []
324
  | Some (x, l) -> [control_on_clock node args c (MBranch (translate_ident node args x, [l, [MReset i]]))]
325

    
326
let translate_eq node ((m, si, j, d, s) as args) eq =
327
  (*Format.eprintf "translate_eq %a with clock %a@." Printers.pp_node_eq eq Clocks.print_ck eq.eq_rhs.expr_clock;*)
328
  match eq.eq_lhs, eq.eq_rhs.expr_desc with
329
  | [x], Expr_arrow (e1, e2)                     ->
330
    let var_x = get_node_var x node in
331
    let o = new_instance node arrow_top_decl eq.eq_rhs.expr_tag in
332
    let c1 = translate_expr node args e1 in
333
    let c2 = translate_expr node args e2 in
334
    (m,
335
     MReset o :: si,
336
     Utils.IMap.add o (arrow_top_decl, []) j,
337
     d,
338
     (control_on_clock node args eq.eq_rhs.expr_clock (MStep ([var_x], o, [c1;c2]))) :: s)
339
  | [x], Expr_pre e1 when ISet.mem (get_node_var x node) d     ->
340
    let var_x = get_node_var x node in
341
    (ISet.add var_x m,
342
     si,
343
     j,
344
     d,
345
     control_on_clock node args eq.eq_rhs.expr_clock (MStateAssign (var_x, translate_expr node args e1)) :: s)
346
  | [x], Expr_fby (e1, e2) when ISet.mem (get_node_var x node) d ->
347
    let var_x = get_node_var x node in
348
    (ISet.add var_x m,
349
     MStateAssign (var_x, translate_expr node args e1) :: si,
350
     j,
351
     d,
352
     control_on_clock node args eq.eq_rhs.expr_clock (MStateAssign (var_x, translate_expr node args e2)) :: s)
353

    
354
  | p  , Expr_appl (f, arg, r) when not (Basic_library.is_internal_fun f) ->
355
    let var_p = List.map (fun v -> get_node_var v node) p in
356
    let el = expr_list_of_expr arg in
357
    let vl = List.map (translate_expr node args) el in
358
    let node_f = node_from_name f in
359
    let call_f =
360
      node_f,
361
      NodeDep.filter_static_inputs (node_inputs node_f) el in 
362
    let o = new_instance node node_f eq.eq_rhs.expr_tag in
363
    let call_ck = Clocks.new_var true in
364
    Clock_calculus.unify_imported_clock (Some call_ck) eq.eq_rhs.expr_clock;
365
    (m,
366
     (if Stateless.check_node node_f then si else MReset o :: si),
367
     Utils.IMap.add o call_f j,
368
     d,
369
     reset_instance node args o r eq.eq_rhs.expr_clock @
370
       (control_on_clock node args call_ck (MStep (var_p, o, vl))) :: s)
371

    
372
   (* special treatment depending on the active backend. For horn backend, x = ite (g,t,e)
373
      are preserved. While they are replaced as if g then x = t else x = e in  C or Java
374
      backends. *)
375
  | [x], Expr_ite   (c, t, e) 
376
    when (match !Options.output with | "horn" -> true | "C" | "java" | _ -> false)
377
      -> 
378
    let var_x = get_node_var x node in
379
    (m, 
380
     si, 
381
     j, 
382
     d, 
383
     (control_on_clock node args eq.eq_rhs.expr_clock 
384
	(MLocalAssign (var_x, translate_expr node args eq.eq_rhs))::s)
385
    )
386
      
387
  | [x], _                                       -> (
388
    let var_x = get_node_var x node in
389
    (m, si, j, d, 
390
     control_on_clock 
391
       node
392
       args
393
       eq.eq_rhs.expr_clock
394
       (translate_act node args (var_x, eq.eq_rhs)) :: s
395
    )
396
  )
397
  | _                                            ->
398
    begin
399
      Format.eprintf "unsupported equation: %a@?" Printers.pp_node_eq eq;
400
      assert false
401
    end
402

    
403
let find_eq xl eqs =
404
  let rec aux accu eqs =
405
      match eqs with
406
	| [] ->
407
	  begin
408
	    Format.eprintf "Looking for variables %a in the following equations@.%a@."
409
	      (Utils.fprintf_list ~sep:" , " (fun fmt v -> Format.fprintf fmt "%s" v)) xl
410
	      Printers.pp_node_eqs eqs;
411
	    assert false
412
	  end
413
	| hd::tl -> 
414
	  if List.exists (fun x -> List.mem x hd.eq_lhs) xl then hd, accu@tl else aux (hd::accu) tl
415
    in
416
    aux [] eqs
417

    
418
(* Sort the set of equations of node [nd] according 
419
   to the computed schedule [sch]
420
*)
421
let sort_equations_from_schedule nd sch =
422
  let split_eqs = Splitting.tuple_split_eq_list nd.node_eqs in
423
  let eqs_rev, remainder =
424
    List.fold_left 
425
      (fun (accu, node_eqs_remainder) vl -> 
426
       if List.exists (fun eq -> List.exists (fun v -> List.mem v eq.eq_lhs) vl) accu
427
       then
428
	 (accu, node_eqs_remainder)
429
       else
430
	 let eq_v, remainder = find_eq vl node_eqs_remainder in
431
	 eq_v::accu, remainder
432
      ) 
433
      ([], split_eqs) 
434
      sch 
435
  in
436
  if List.length remainder > 0 then (
437
    Format.eprintf "Equations not used are@.%a@.Full equation set is:@.%a@.@?"
438
		   Printers.pp_node_eqs remainder
439
      		   Printers.pp_node_eqs nd.node_eqs;
440
    assert false);
441
  List.rev eqs_rev
442

    
443
let translate_eqs node args eqs =
444
  List.fold_right (fun eq args -> translate_eq node args eq) eqs args;;
445

    
446
let translate_decl nd sch =
447
  (*Log.report ~level:1 (fun fmt -> Printers.pp_node fmt nd);*)
448

    
449
(*
450
  let eqs_rev, remainder = 
451
    List.fold_left 
452
      (fun (accu, node_eqs_remainder) v -> 
453
	  if List.exists (fun eq -> List.mem v eq.eq_lhs) accu
454
	  then
455
	    (accu, node_eqs_remainder)
456
	  else
457
	    (*if   List.exists (fun vdecl -> vdecl.var_id = v) nd.node_locals
458
	      || List.exists (fun vdecl -> vdecl.var_id = v) nd.node_outputs
459
	    then*)
460
	      let eq_v, remainder = find_eq v node_eqs_remainder in
461
	      eq_v::accu, remainder
462
	    (* else it is a constant value, checked during typing phase
463
	    else	 
464
	      accu, node_eqs_remainder *)
465
      ) 
466
      ([], split_eqs) 
467
      sch 
468
  in
469
 *)
470
  let sorted_eqs = sort_equations_from_schedule nd sch in
471

    
472
  let init_args = ISet.empty, [], Utils.IMap.empty, List.fold_right (fun l -> ISet.add l) nd.node_locals ISet.empty, [] in
473
  (* memories, init instructions, node calls, local variables (including memories), step instrs *)
474
  let m, init, j, locals, s = translate_eqs nd init_args sorted_eqs in
475
  let mmap = Utils.IMap.fold (fun i n res -> (i, n)::res) j [] in
476
  {
477
    mname = nd;
478
    mmemory = ISet.elements m;
479
    mcalls = mmap;
480
    minstances = List.filter (fun (_, (n,_)) -> not (Stateless.check_node n)) mmap;
481
    minit = init;
482
    mstatic = List.filter (fun v -> v.var_dec_const) nd.node_inputs;
483
    mstep = {
484
      step_inputs = nd.node_inputs;
485
      step_outputs = nd.node_outputs;
486
      step_locals = ISet.elements (ISet.diff locals m);
487
      step_checks = List.map (fun d -> d.Dimension.dim_loc, translate_expr nd init_args (expr_of_dimension d)) nd.node_checks;
488
      step_instrs = (
489
	(* special treatment depending on the active backend. For horn backend,
490
	   common branches are not merged while they are in C or Java
491
	   backends. *)
492
	match !Options.output with
493
	| "horn" -> s
494
	| "C" | "java" | _ -> join_guards_list s
495
      );
496
      step_asserts = 
497
	let exprl = List.map (fun assert_ -> assert_.assert_expr ) nd.node_asserts in
498
	List.map (translate_expr nd init_args) exprl
499
	;
500
    };
501
    mspec = nd.node_spec;
502
    mannot = nd.node_annot;
503
  }
504

    
505
(** takes the global delcarations and the scheduling associated to each node *)
506
let translate_prog decls node_schs =
507
  let nodes = get_nodes decls in 
508
  List.map 
509
    (fun node -> 
510
      let sch = (Utils.IMap.find node.node_id node_schs).Scheduling.schedule in
511
      translate_decl node sch 
512
    ) nodes
513

    
514
let get_machine_opt name machines =  
515
  List.fold_left 
516
    (fun res m -> 
517
      match res with 
518
      | Some _ -> res 
519
      | None -> if m.mname.node_id = name then Some m else None)
520
    None machines
521
    
522
(* variable substitution for optimizing purposes *)
523

    
524
(* checks whether an [instr] is skip and can be removed from program *)
525
let rec instr_is_skip instr =
526
  match instr with
527
  | MLocalAssign (i, LocalVar v) when i = v -> true
528
  | MStateAssign (i, StateVar v) when i = v -> true
529
  | MBranch (g, hl) -> List.for_all (fun (_, il) -> instrs_are_skip il) hl
530
  | _               -> false
531
and instrs_are_skip instrs =
532
  List.for_all instr_is_skip instrs
533

    
534
let instr_cons instr cont =
535
 if instr_is_skip instr then cont else instr::cont
536

    
537
let rec instr_remove_skip instr cont =
538
  match instr with
539
  | MLocalAssign (i, LocalVar v) when i = v -> cont
540
  | MStateAssign (i, StateVar v) when i = v -> cont
541
  | MBranch (g, hl) -> MBranch (g, List.map (fun (h, il) -> (h, instrs_remove_skip il [])) hl) :: cont
542
  | _               -> instr::cont
543

    
544
and instrs_remove_skip instrs cont =
545
  List.fold_right instr_remove_skip instrs cont
546

    
547
let rec value_replace_var fvar value =
548
  match value with
549
  | Cst c -> value
550
  | LocalVar v -> LocalVar (fvar v)
551
  | StateVar v -> value
552
  | Fun (id, args) -> Fun (id, List.map (value_replace_var fvar) args) 
553
  | Array vl -> Array (List.map (value_replace_var fvar) vl)
554
  | Access (t, i) -> Access(value_replace_var fvar t, i)
555
  | Power (v, n) -> Power(value_replace_var fvar v, n)
556

    
557
let rec instr_replace_var fvar instr cont =
558
  match instr with
559
  | MLocalAssign (i, v) -> instr_cons (MLocalAssign (fvar i, value_replace_var fvar v)) cont
560
  | MStateAssign (i, v) -> instr_cons (MStateAssign (i, value_replace_var fvar v)) cont
561
  | MReset i            -> instr_cons instr cont
562
  | MStep (il, i, vl)   -> instr_cons (MStep (List.map fvar il, i, List.map (value_replace_var fvar) vl)) cont
563
  | MBranch (g, hl)     -> instr_cons (MBranch (value_replace_var fvar g, List.map (fun (h, il) -> (h, instrs_replace_var fvar il [])) hl)) cont
564

    
565
and instrs_replace_var fvar instrs cont =
566
  List.fold_right (instr_replace_var fvar) instrs cont
567

    
568
let step_replace_var fvar step =
569
  { step with
570
    step_checks = List.map (fun (l, v) -> (l, value_replace_var fvar v)) step.step_checks;
571
    step_locals = Utils.remove_duplicates (List.map fvar step.step_locals);
572
    step_instrs = instrs_replace_var fvar step.step_instrs [];
573
}
574

    
575
let rec machine_replace_var fvar m =
576
  { m with
577
    mstep = step_replace_var fvar m.mstep
578
  }
579

    
580
let machine_reuse_var m reuse =
581
  let fvar v =
582
    try
583
      Hashtbl.find reuse v.var_id
584
    with Not_found -> v in
585
  machine_replace_var fvar m
586

    
587
let prog_reuse_var prog node_schs =
588
  List.map 
589
    (fun m -> 
590
      machine_reuse_var m (Utils.IMap.find m.mname.node_id node_schs).Scheduling.reuse_table
591
    ) prog
592

    
593
(* Local Variables: *)
594
(* compile-command:"make -C .." *)
595
(* End: *)