Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / machine_code.ml @ ca88e660

History | View | Annotate | Download (22.6 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

    
23
module ISet = Set.Make(OrdVarDecl)
24

    
25
 
26
let rec pp_val fmt v =
27
  match v.value_desc with
28
    | Cst c         -> Printers.pp_const fmt c 
29
    | LocalVar v    -> Format.pp_print_string fmt v.var_id
30
    | StateVar v    -> Format.pp_print_string fmt v.var_id
31
    | Array vl      -> Format.fprintf fmt "[%a]" (Utils.fprintf_list ~sep:", " pp_val)  vl
32
    | Access (t, i) -> Format.fprintf fmt "%a[%a]" pp_val t pp_val i
33
    | Power (v, n)  -> Format.fprintf fmt "(%a^%a)" pp_val v pp_val n
34
    | Fun (n, vl)   -> Format.fprintf fmt "%s (%a)" n (Utils.fprintf_list ~sep:", " pp_val)  vl
35

    
36
let rec pp_instr fmt i =
37
  match i with 
38
    | MLocalAssign (i,v) -> Format.fprintf fmt "%s<-l- %a" i.var_id pp_val v
39
    | MStateAssign (i,v) -> Format.fprintf fmt "%s<-s- %a" i.var_id pp_val v
40
    | MReset i           -> Format.fprintf fmt "reset %s" i
41
    | MNoReset i         -> ()
42
    | MStep (il, i, vl)  ->
43
      Format.fprintf fmt "%a = %s (%a)"
44
	(Utils.fprintf_list ~sep:", " (fun fmt v -> Format.pp_print_string fmt v.var_id)) il
45
	i      
46
	(Utils.fprintf_list ~sep:", " pp_val) vl
47
    | MBranch (g,hl)     ->
48
      Format.fprintf fmt "@[<v 2>case(%a) {@,%a@,}@]"
49
	pp_val g
50
	(Utils.fprintf_list ~sep:"@," pp_branch) hl
51
    | MComment s -> Format.pp_print_string fmt s
52

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

    
56
and pp_instrs fmt il =
57
  Format.fprintf fmt "@[<v 2>%a@]" (Utils.fprintf_list ~sep:"@," pp_instr) il
58

    
59
type step_t = {
60
  step_checks: (Location.t * value_t) list;
61
  step_inputs: var_decl list;
62
  step_outputs: var_decl list;
63
  step_locals: var_decl list;
64
  step_instrs: instr_t list;
65
  step_asserts: value_t list;
66
}
67

    
68
type static_call = top_decl * (Dimension.dim_expr list)
69

    
70
type machine_t = {
71
  mname: node_desc;
72
  mmemory: var_decl list;
73
  mcalls: (ident * static_call) list; (* map from stateful/stateless instance to node, no internals *)
74
  minstances: (ident * static_call) list; (* sub-map of mcalls, from stateful instance to node *)
75
  minit: instr_t list;
76
  mstatic: var_decl list; (* static inputs only *)
77
  mconst: instr_t list; (* assignments of node constant locals *)
78
  mstep: step_t;
79
  mspec: node_annot option;
80
  mannot: expr_annot list;
81
}
82

    
83
let machine_vars m = m.mstep.step_inputs @ m.mstep.step_locals @ m.mstep.step_outputs @ m.mmemory
84
let pp_step fmt s =
85
  Format.fprintf fmt "@[<v>inputs : %a@ outputs: %a@ locals : %a@ checks : %a@ instrs : @[%a@]@ asserts : @[%a@]@]@ "
86
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_inputs
87
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_outputs
88
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_locals
89
    (Utils.fprintf_list ~sep:", " (fun fmt (_, c) -> pp_val fmt c)) s.step_checks
90
    (Utils.fprintf_list ~sep:"@ " pp_instr) s.step_instrs
91
    (Utils.fprintf_list ~sep:", " pp_val) s.step_asserts
92

    
93

    
94
let pp_static_call fmt (node, args) =
95
 Format.fprintf fmt "%s<%a>"
96
   (node_name node)
97
   (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) args
98

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

    
111
let rec is_const_value v =
112
  match v.value_desc with
113
  | Cst _          -> true
114
  | Fun (id, args) -> Basic_library.is_value_internal_fun v && List.for_all is_const_value args
115
  | _              -> false
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_orig = false;
137
    var_dec_type = dummy_type_dec;
138
    var_dec_clock = dummy_clock_dec;
139
    var_dec_const = false;
140
    var_dec_value = None;
141
    var_type =  typ;
142
    var_clock = Clocks.new_ck (Clocks.Cvar Clocks.CSet_all) true;
143
    var_loc = Location.dummy_loc
144
  }
145

    
146
let arrow_id = "_arrow"
147

    
148
let arrow_typ = Types.new_ty Types.Tunivar
149

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

    
167
let arrow_top_decl =
168
  {
169
    top_decl_desc = Node arrow_desc;
170
    top_decl_owner = Version.include_path;
171
    top_decl_itf = false;
172
    top_decl_loc = Location.dummy_loc
173
  }
174

    
175
let mk_val v t = { value_desc = v; 
176
		   value_type = t; 
177
		   value_annot = None }
178

    
179
let arrow_machine =
180
  let state = "_first" in
181
  let var_state = dummy_var_decl state (Types.new_ty Types.Tbool) in
182
  let var_input1 = List.nth arrow_desc.node_inputs 0 in
183
  let var_input2 = List.nth arrow_desc.node_inputs 1 in
184
  let var_output = List.nth arrow_desc.node_outputs 0 in
185
  let cst b = mk_val (Cst (const_of_bool b)) Type_predef.type_bool in
186
  let t_arg = Types.new_univar () in (* TODO Xavier: c'est bien la bonne def ? *)
187
  {
188
    mname = arrow_desc;
189
    mmemory = [var_state];
190
    mcalls = [];
191
    minstances = [];
192
    minit = [MStateAssign(var_state, cst true)];
193
    mconst = [];
194
    mstatic = [];
195
    mstep = {
196
      step_inputs = arrow_desc.node_inputs;
197
      step_outputs = arrow_desc.node_outputs;
198
      step_locals = [];
199
      step_checks = [];
200
      step_instrs = [conditional (mk_val (StateVar var_state) Type_predef.type_bool)
201
			         [MStateAssign(var_state, cst false);
202
                                  MLocalAssign(var_output, mk_val (LocalVar var_input1) t_arg)]
203
                                 [MLocalAssign(var_output, mk_val (LocalVar var_input2) t_arg)] ];
204
      step_asserts = [];
205
    };
206
    mspec = None;
207
    mannot = [];
208
  }
209

    
210
let new_instance =
211
  let cpt = ref (-1) in
212
  fun caller callee tag ->
213
    begin
214
      let o =
215
	if Stateless.check_node callee then
216
	  node_name callee
217
	else
218
	  Printf.sprintf "ni_%d" (incr cpt; !cpt) in
219
      let o =
220
	if !Options.ansi && is_generic_node callee
221
	then Printf.sprintf "%s_inst_%d" o (Utils.position (fun e -> e.expr_tag = tag) caller.node_gencalls)
222
	else o in
223
      o
224
    end
225

    
226
(* translate_<foo> : node -> context -> <foo> -> machine code/expression *)
227
(* the context contains  m : state aka memory variables  *)
228
(*                      si : initialization instructions *)
229
(*                       j : node aka machine instances  *)
230
(*                       d : local variables             *)
231
(*                       s : step instructions           *)
232
let translate_ident node (m, si, j, d, s) id =
233
  try (* id is a node var *)
234
    let var_id = get_node_var id node in
235
    if ISet.exists (fun v -> v.var_id = id) m
236
    then mk_val (StateVar var_id) var_id.var_type
237
    else mk_val (LocalVar var_id) var_id.var_type
238
  with Not_found ->
239
    try (* id is a constant *)
240
      let vdecl = (Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id))) in
241
      mk_val (LocalVar vdecl) vdecl.var_type
242
    with Not_found ->
243
      (* id is a tag *)
244
      (* TODO construire une liste des enum declar├ęs et alors chercher dedans la liste
245
	 qui contient id *)
246
      let cst = Const_tag id in
247
      mk_val (Cst cst) (Typing.type_const Location.dummy_loc cst) 
248
	
249
let rec control_on_clock node ((m, si, j, d, s) as args) ck inst =
250
 match (Clocks.repr ck).cdesc with
251
 | Con    (ck1, cr, l) ->
252
   let id  = Clocks.const_of_carrier cr in
253
   control_on_clock node args ck1 (MBranch (translate_ident node args id,
254
					    [l, [inst]] ))
255
 | _                   -> inst
256

    
257
let rec join_branches hl1 hl2 =
258
 match hl1, hl2 with
259
 | []          , _            -> hl2
260
 | _           , []           -> hl1
261
 | (t1, h1)::q1, (t2, h2)::q2 ->
262
   if t1 < t2 then (t1, h1) :: join_branches q1 hl2 else
263
   if t1 > t2 then (t2, h2) :: join_branches hl1 q2
264
   else (t1, List.fold_right join_guards h1 h2) :: join_branches q1 q2
265

    
266
and join_guards inst1 insts2 =
267
 match inst1, insts2 with
268
 | _                   , []                               ->
269
   [inst1]
270
 | MBranch (x1, hl1), MBranch (x2, hl2) :: q when x1 = x2 ->
271
   MBranch (x1, join_branches (sort_handlers hl1) (sort_handlers hl2))
272
   :: q
273
 | _ -> inst1 :: insts2
274

    
275
let join_guards_list insts =
276
 List.fold_right join_guards insts []
277

    
278
(* specialize predefined (polymorphic) operators
279
   wrt their instances, so that the C semantics 
280
   is preserved *)
281
let specialize_to_c expr =
282
 match expr.expr_desc with
283
 | Expr_appl (id, e, r) ->
284
   if List.exists (fun e -> Types.is_bool_type e.expr_type) (expr_list_of_expr e)
285
   then let id =
286
	  match id with
287
	  | "="  -> "equi"
288
	  | "!=" -> "xor"
289
	  | _    -> id in
290
	{ expr with expr_desc = Expr_appl (id, e, r) }
291
   else expr
292
 | _ -> expr
293

    
294
let specialize_op expr =
295
  match !Options.output with
296
  | "C" -> specialize_to_c expr
297
  | _   -> expr
298

    
299
let rec translate_expr node ((m, si, j, d, s) as args) expr =
300
  let expr = specialize_op expr in
301
  let value_desc = 
302
    match expr.expr_desc with
303
    | Expr_const v                     -> Cst v
304
    | Expr_ident x                     -> (translate_ident node args x).value_desc
305
    | Expr_array el                    -> Array (List.map (translate_expr node args) el)
306
    | Expr_access (t, i)               -> Access (translate_expr node args t, translate_expr node args (expr_of_dimension i))
307
    | Expr_power  (e, n)               -> Power  (translate_expr node args e, translate_expr node args (expr_of_dimension n))
308
    | Expr_tuple _
309
    | Expr_arrow _ 
310
    | Expr_fby _
311
    | Expr_pre _                       -> (Printers.pp_expr Format.err_formatter expr; Format.pp_print_flush Format.err_formatter (); raise NormalizationError)
312
    | Expr_when    (e1, _, _)          -> (translate_expr node args e1).value_desc
313
    | Expr_merge   (x, _)              -> raise NormalizationError
314
    | Expr_appl (id, e, _) when Basic_library.is_expr_internal_fun expr ->
315
      let nd = node_from_name id in
316
      Fun (node_name nd, List.map (translate_expr node args) (expr_list_of_expr e))
317
    | Expr_ite (g,t,e) -> (
318
      (* special treatment depending on the active backend. For horn backend, ite
319
	 are preserved in expression. While they are removed for C or Java
320
	 backends. *)
321
      match !Options.output with | "horn" -> 
322
	Fun ("ite", [translate_expr node args g; translate_expr node args t; translate_expr node args e])
323
      | "C" | "java" | _ -> 
324
	(Printers.pp_expr Format.err_formatter expr; Format.pp_print_flush Format.err_formatter (); raise NormalizationError)
325
    )
326
    | _                   -> raise NormalizationError
327
  in
328
  mk_val value_desc expr.expr_type
329

    
330
let translate_guard node args expr =
331
  match expr.expr_desc with
332
  | Expr_ident x  -> translate_ident node args x
333
  | _ -> (Format.eprintf "internal error: translate_guard %s %a@." node.node_id Printers.pp_expr expr;assert false)
334

    
335
let rec translate_act node ((m, si, j, d, s) as args) (y, expr) =
336
  match expr.expr_desc with
337
  | Expr_ite   (c, t, e) -> let g = translate_guard node args c in
338
			    conditional g [translate_act node args (y, t)]
339
                              [translate_act node args (y, e)]
340
  | Expr_merge (x, hl)   -> MBranch (translate_ident node args x, List.map (fun (t,  h) -> t, [translate_act node args (y, h)]) hl)
341
  | _                    -> MLocalAssign (y, translate_expr node args expr)
342

    
343
let reset_instance node args i r c =
344
  match r with
345
  | None        -> []
346
  | Some r      -> let g = translate_guard node args r in
347
                   [control_on_clock node args c (conditional g [MReset i] [MNoReset i])]
348

    
349
let translate_eq node ((m, si, j, d, s) as args) eq =
350
  (*Format.eprintf "translate_eq %a with clock %a@." Printers.pp_node_eq eq Clocks.print_ck eq.eq_rhs.expr_clock;*)
351
  match eq.eq_lhs, eq.eq_rhs.expr_desc with
352
  | [x], Expr_arrow (e1, e2)                     ->
353
    let var_x = get_node_var x node in
354
    let o = new_instance node arrow_top_decl eq.eq_rhs.expr_tag in
355
    let c1 = translate_expr node args e1 in
356
    let c2 = translate_expr node args e2 in
357
    (m,
358
     MReset o :: si,
359
     Utils.IMap.add o (arrow_top_decl, []) j,
360
     d,
361
     (control_on_clock node args eq.eq_rhs.expr_clock (MStep ([var_x], o, [c1;c2]))) :: s)
362
  | [x], Expr_pre e1 when ISet.mem (get_node_var x node) d     ->
363
    let var_x = get_node_var x node in
364
    (ISet.add var_x m,
365
     si,
366
     j,
367
     d,
368
     control_on_clock node args eq.eq_rhs.expr_clock (MStateAssign (var_x, translate_expr node args e1)) :: s)
369
  | [x], Expr_fby (e1, e2) when ISet.mem (get_node_var x node) d ->
370
    let var_x = get_node_var x node in
371
    (ISet.add var_x m,
372
     MStateAssign (var_x, translate_expr node args e1) :: si,
373
     j,
374
     d,
375
     control_on_clock node args eq.eq_rhs.expr_clock (MStateAssign (var_x, translate_expr node args e2)) :: s)
376

    
377
  | p  , Expr_appl (f, arg, r) when not (Basic_library.is_expr_internal_fun eq.eq_rhs) ->
378
    let var_p = List.map (fun v -> get_node_var v node) p in
379
    let el = expr_list_of_expr arg in
380
    let vl = List.map (translate_expr node args) el in
381
    let node_f = node_from_name f in
382
    let call_f =
383
      node_f,
384
      NodeDep.filter_static_inputs (node_inputs node_f) el in 
385
    let o = new_instance node node_f eq.eq_rhs.expr_tag in
386
    let env_cks = List.fold_right (fun arg cks -> arg.expr_clock :: cks) el [eq.eq_rhs.expr_clock] in
387
    let call_ck = Clock_calculus.compute_root_clock (Clock_predef.ck_tuple env_cks) in
388
    (*Clocks.new_var true in
389
    Clock_calculus.unify_imported_clock (Some call_ck) eq.eq_rhs.expr_clock eq.eq_rhs.expr_loc;
390
    Format.eprintf "call %a: %a: %a@," Printers.pp_expr eq.eq_rhs Clocks.print_ck (Clock_predef.ck_tuple env_cks) Clocks.print_ck call_ck;*)
391
    (m,
392
     (if Stateless.check_node node_f then si else MReset o :: si),
393
     Utils.IMap.add o call_f j,
394
     d,
395
     (if Stateless.check_node node_f
396
      then []
397
      else reset_instance node args o r call_ck) @
398
       (control_on_clock node args call_ck (MStep (var_p, o, vl))) :: s)
399

    
400
  | [x], _                                       -> (
401
    let var_x = get_node_var x node in
402
    (m, si, j, d, 
403
     control_on_clock 
404
       node
405
       args
406
       eq.eq_rhs.expr_clock
407
       (translate_act node args (var_x, eq.eq_rhs)) :: s
408
    )
409
  )
410
  | _                                            ->
411
    begin
412
      Format.eprintf "internal error: Machine_code.translate_eq %a@?" Printers.pp_node_eq eq;
413
      assert false
414
    end
415

    
416
let find_eq xl eqs =
417
  let rec aux accu eqs =
418
      match eqs with
419
	| [] ->
420
	  begin
421
	    Format.eprintf "Looking for variables %a in the following equations@.%a@."
422
	      (Utils.fprintf_list ~sep:" , " (fun fmt v -> Format.fprintf fmt "%s" v)) xl
423
	      Printers.pp_node_eqs eqs;
424
	    assert false
425
	  end
426
	| hd::tl ->
427
	  if List.exists (fun x -> List.mem x hd.eq_lhs) xl then 
428
		hd, accu@tl 
429
      else 
430
        aux (hd::accu) tl
431
    in
432
    aux [] eqs
433

    
434
(* Sort the set of equations of node [nd] according 
435
   to the computed schedule [sch]
436
*)
437
let sort_equations_from_schedule nd sch =
438
  (* Format.eprintf "%s schedule: %a@." *)
439
  (* 		 nd.node_id *)
440
  (* 		 (Utils.fprintf_list ~sep:" ; " Scheduling.pp_eq_schedule) sch; *)
441
  let split_eqs = Splitting.tuple_split_eq_list (get_node_eqs nd) in
442
  let eqs_rev, remainder =
443
    List.fold_left
444
      (fun (accu, node_eqs_remainder) vl ->
445
       if List.exists 
446
		 (fun eq -> (* This could be also evaluated with a forall.
447
                       But, by construction, each vl should be 
448
                       associated to a single equation *)
449
             List.exists (fun v -> List.mem v eq.eq_lhs) vl)
450
         accu
451
       then
452
	 (accu, node_eqs_remainder)
453
       else
454
	 let eq_v, remainder = find_eq vl node_eqs_remainder in
455
	 eq_v::accu, remainder
456
      )
457
      ([], split_eqs)
458
      sch
459
  in
460
  begin
461
    if List.length remainder > 0 then (
462
      Format.eprintf "Equations not used are@.%a@.Full equation set is:@.%a@.@?"
463
		     Printers.pp_node_eqs remainder
464
      		     Printers.pp_node_eqs (get_node_eqs nd);
465
      assert false);
466
    let res = List.rev eqs_rev in
467
	(* (\* Debug code, to be removed *\) *)
468
	(* List.iteri (fun cpt eq -> Format.eprintf "Eq %i: %a@." cpt (Utils.fprintf_list ~sep:", " Format.pp_print_string) eq.eq_lhs) res; *)
469
	res
470
  end
471

    
472
let constant_equations nd =
473
 List.fold_right (fun vdecl eqs ->
474
   if vdecl.var_dec_const
475
   then
476
     { eq_lhs = [vdecl.var_id];
477
       eq_rhs = Utils.desome vdecl.var_dec_value;
478
       eq_loc = vdecl.var_loc
479
     } :: eqs
480
   else eqs)
481
   nd.node_locals []
482

    
483
let translate_eqs node args eqs =
484
  List.fold_right (fun eq args -> translate_eq node args eq) eqs args;;
485

    
486
let translate_decl nd sch =
487
  (*Log.report ~level:1 (fun fmt -> Printers.pp_node fmt nd);*)
488

    
489
  let sorted_eqs = sort_equations_from_schedule nd sch in
490
  let constant_eqs = constant_equations nd in
491
  
492
  let init_args = ISet.empty, [], Utils.IMap.empty, List.fold_right (fun l -> ISet.add l) nd.node_locals ISet.empty, [] in
493
  (* memories, init instructions, node calls, local variables (including memories), step instrs *)
494
  let m0, init0, j0, locals0, s0 = translate_eqs nd init_args constant_eqs in
495
  assert (ISet.is_empty m0);
496
  assert (init0 = []);
497
  assert (Utils.IMap.is_empty j0);
498
  let m, init, j, locals, s = translate_eqs nd (m0, init0, j0, locals0, []) sorted_eqs in
499
  let mmap = Utils.IMap.fold (fun i n res -> (i, n)::res) j [] in
500
  {
501
    mname = nd;
502
    mmemory = ISet.elements m;
503
    mcalls = mmap;
504
    minstances = List.filter (fun (_, (n,_)) -> not (Stateless.check_node n)) mmap;
505
    minit = init;
506
    mconst = s0;
507
    mstatic = List.filter (fun v -> v.var_dec_const) nd.node_inputs;
508
    mstep = {
509
      step_inputs = nd.node_inputs;
510
      step_outputs = nd.node_outputs;
511
      step_locals = ISet.elements (ISet.diff locals m);
512
      step_checks = List.map (fun d -> d.Dimension.dim_loc, translate_expr nd init_args (expr_of_dimension d)) nd.node_checks;
513
      step_instrs = (
514
	(* special treatment depending on the active backend. For horn backend,
515
	   common branches are not merged while they are in C or Java
516
	   backends. *)
517
	match !Options.output with
518
	(* | "horn" -> s TODO 16/12 *)
519
	| "C" | "java" | _ -> join_guards_list s
520
      );
521
      step_asserts = 
522
	let exprl = List.map (fun assert_ -> assert_.assert_expr ) nd.node_asserts in
523
	List.map (translate_expr nd init_args) exprl
524
	;
525
    };
526
    mspec = nd.node_spec;
527
    mannot = nd.node_annot;
528
  }
529

    
530
(** takes the global declarations and the scheduling associated to each node *)
531
let translate_prog decls node_schs =
532
  let nodes = get_nodes decls in 
533
  List.map 
534
    (fun decl -> 
535
     let node = node_of_top decl in
536
      let sch = (Utils.IMap.find node.node_id node_schs).Scheduling.schedule in
537
      translate_decl node sch 
538
    ) nodes
539

    
540
let get_machine_opt name machines =  
541
  List.fold_left 
542
    (fun res m -> 
543
      match res with 
544
      | Some _ -> res 
545
      | None -> if m.mname.node_id = name then Some m else None)
546
    None machines
547

    
548
let get_const_assign m id =
549
  try
550
    match (List.find (fun instr -> match instr with MLocalAssign (v, _) -> v == id | _ -> false) m.mconst) with
551
    | MLocalAssign (_, e) -> e
552
    | _                   -> assert false
553
  with Not_found -> assert false
554

    
555

    
556
let value_of_ident m id =
557
  (* is is a state var *)
558
  try
559
    let mem = List.find (fun v -> v.var_id = id) m.mmemory in
560
    mk_val (StateVar mem) mem.var_type
561
  with Not_found ->
562
  try (* id is a node var *)
563
    let var = get_node_var id m.mname in
564
    mk_val (LocalVar var) var.var_type
565
  with Not_found ->
566
    try (* id is a constant *)
567
      let cst = Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id)) in
568
      mk_val (LocalVar cst) cst.var_type
569
    with Not_found ->
570
      (* id is a tag *)
571
      let tag = Const_tag id in
572
      mk_val (Cst tag) (Typing.type_const Location.dummy_loc tag)
573

    
574
let rec value_of_dimension m dim =
575
  match dim.Dimension.dim_desc with
576
  | Dimension.Dbool b         -> mk_val (Cst (Const_tag (if b then Corelang.tag_true else Corelang.tag_false))) Type_predef.type_bool
577
  | Dimension.Dint i          -> mk_val (Cst (Const_int i)) Type_predef.type_int
578
  | Dimension.Dident v        -> value_of_ident m v
579
  | Dimension.Dappl (f, args) -> let typ = if Basic_library.is_numeric_operator f then Type_predef.type_int else Type_predef.type_bool
580
                                 in mk_val (Fun (f, List.map (value_of_dimension m) args)) typ
581
  | Dimension.Dite (i, t, e)  -> let [vi; vt; ve] = List.map (value_of_dimension m) [i; t; e] in
582
				 mk_val (Fun ("ite", [vi; vt; ve])) vt.value_type
583
  | Dimension.Dlink dim'      -> value_of_dimension m dim'
584
  | _                         -> assert false
585

    
586
let rec dimension_of_value value =
587
  match value.value_desc with
588
  | Cst (Const_tag t) when t = Corelang.tag_true  -> Dimension.mkdim_bool  Location.dummy_loc true
589
  | Cst (Const_tag t) when t = Corelang.tag_false -> Dimension.mkdim_bool  Location.dummy_loc false
590
  | Cst (Const_int i)                             -> Dimension.mkdim_int   Location.dummy_loc i
591
  | LocalVar v                                    -> Dimension.mkdim_ident Location.dummy_loc v.var_id
592
  | Fun (f, args)                                 -> Dimension.mkdim_appl  Location.dummy_loc f (List.map dimension_of_value args)
593
  | _                                             -> assert false
594

    
595
(* Local Variables: *)
596
(* compile-command:"make -C .." *)
597
(* End: *)