Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / machine_code.ml @ 01d48bb0

History | View | Annotate | Download (21.4 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
  mconst: instr_t list; (* assignments of node constant locals *)
87
  mstep: step_t;
88
  mspec: node_annot option;
89
  mannot: expr_annot list;
90
}
91

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

    
101

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

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

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

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

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

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

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

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

    
148
let arrow_id = "_arrow"
149

    
150
let arrow_typ = Types.new_ty Types.Tunivar
151

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

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

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

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

    
222

    
223
(* translate_<foo> : node -> context -> <foo> -> machine code/expression *)
224
(* the context contains  m : state aka memory variables  *)
225
(*                      si : initialization instructions *)
226
(*                       j : node aka machine instances  *)
227
(*                       d : local variables             *)
228
(*                       s : step instructions           *)
229
let translate_ident node (m, si, j, d, s) id =
230
  try (* id is a node var *)
231
    let var_id = get_node_var id node in
232
    if ISet.exists (fun v -> v.var_id = id) m
233
    then StateVar var_id
234
    else LocalVar var_id
235
  with Not_found ->
236
    try (* id is a constant *)
237
      LocalVar (Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id)))
238
    with Not_found ->
239
      (* id is a tag *)
240
      Cst (Const_tag id)
241

    
242
let rec control_on_clock node ((m, si, j, d, s) as args) ck inst =
243
 match (Clocks.repr ck).cdesc with
244
 | Con    (ck1, cr, l) ->
245
   let id  = Clocks.const_of_carrier cr in
246
   control_on_clock node args ck1 (MBranch (translate_ident node args id,
247
					    [l, [inst]] ))
248
 | _                   -> inst
249

    
250
let rec join_branches hl1 hl2 =
251
 match hl1, hl2 with
252
 | []          , _            -> hl2
253
 | _           , []           -> hl1
254
 | (t1, h1)::q1, (t2, h2)::q2 ->
255
   if t1 < t2 then (t1, h1) :: join_branches q1 hl2 else
256
   if t1 > t2 then (t2, h2) :: join_branches hl1 q2
257
   else (t1, List.fold_right join_guards h1 h2) :: join_branches q1 q2
258

    
259
and join_guards inst1 insts2 =
260
 match inst1, insts2 with
261
 | _                   , []                               ->
262
   [inst1]
263
 | MBranch (x1, hl1), MBranch (x2, hl2) :: q when x1 = x2 ->
264
   MBranch (x1, join_branches (sort_handlers hl1) (sort_handlers hl2))
265
   :: q
266
 | _ -> inst1 :: insts2
267

    
268
let join_guards_list insts =
269
 List.fold_right join_guards insts []
270

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

    
287
let specialize_op expr =
288
  match !Options.output with
289
  | "C" -> specialize_to_c expr
290
  | _   -> expr
291

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

    
322
let translate_guard node args expr =
323
  match expr.expr_desc with
324
  | Expr_ident x  -> translate_ident node args x
325
  | _ -> (Format.eprintf "internal error: translate_guard %s %a@." node.node_id Printers.pp_expr expr;assert false)
326

    
327
let rec translate_act node ((m, si, j, d, s) as args) (y, expr) =
328
  match expr.expr_desc with
329
  | Expr_ite   (c, t, e) -> let g = translate_guard node args c in
330
			    conditional g [translate_act node args (y, t)]
331
                              [translate_act node args (y, e)]
332
  | Expr_merge (x, hl)   -> MBranch (translate_ident node args x, List.map (fun (t,  h) -> t, [translate_act node args (y, h)]) hl)
333
  | _                    ->
334
    MLocalAssign (y, translate_expr node args expr)
335

    
336
let reset_instance node args i r c =
337
  match r with
338
  | None        -> []
339
  | Some r      -> let g = translate_guard node args r in
340
                   [control_on_clock node args c (conditional g [MReset i] [])]
341

    
342
let translate_eq node ((m, si, j, d, s) as args) eq =
343
  (* Format.eprintf "translate_eq %a with clock %a@." Printers.pp_node_eq eq Clocks.print_ck eq.eq_rhs.expr_clock; *)
344
  match eq.eq_lhs, eq.eq_rhs.expr_desc with
345
  | [x], Expr_arrow (e1, e2)                     ->
346
    let var_x = get_node_var x node in
347
    let o = new_instance node arrow_top_decl eq.eq_rhs.expr_tag in
348
    let c1 = translate_expr node args e1 in
349
    let c2 = translate_expr node args e2 in
350
    (m,
351
     MReset o :: si,
352
     Utils.IMap.add o (arrow_top_decl, []) j,
353
     d,
354
     (control_on_clock node args eq.eq_rhs.expr_clock (MStep ([var_x], o, [c1;c2]))) :: s)
355
  | [x], Expr_pre e1 when ISet.mem (get_node_var x node) d     ->
356
    let var_x = get_node_var x node in
357
    (ISet.add var_x m,
358
     si,
359
     j,
360
     d,
361
     control_on_clock node args eq.eq_rhs.expr_clock (MStateAssign (var_x, translate_expr node args e1)) :: s)
362
  | [x], Expr_fby (e1, e2) 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
     MStateAssign (var_x, translate_expr node args e1) :: si,
366
     j,
367
     d,
368
     control_on_clock node args eq.eq_rhs.expr_clock (MStateAssign (var_x, translate_expr node args e2)) :: s)
369

    
370
  | p  , Expr_appl (f, arg, r) when not (Basic_library.is_internal_fun f) ->
371
    let var_p = List.map (fun v -> get_node_var v node) p in
372
    let el = expr_list_of_expr arg in
373
    let vl = List.map (translate_expr node args) el in
374
    let node_f = node_from_name f in
375
    let call_f =
376
      node_f,
377
      NodeDep.filter_static_inputs (node_inputs node_f) el in
378
    let o = new_instance node node_f eq.eq_rhs.expr_tag in
379
    let env_cks = List.fold_right (fun arg cks -> arg.expr_clock :: cks) el [eq.eq_rhs.expr_clock] in
380
    let call_ck = Clock_calculus.compute_root_clock (Clock_predef.ck_tuple env_cks) in
381
    (*Clocks.new_var true in
382
    Clock_calculus.unify_imported_clock (Some call_ck) eq.eq_rhs.expr_clock eq.eq_rhs.expr_loc;
383
    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;*)
384
    (m,
385
     (if Stateless.check_node node_f then si else MReset o :: si),
386
     Utils.IMap.add o call_f j,
387
     d,
388
     (if Stateless.check_node node_f
389
      then []
390
      else reset_instance node args o r call_ck) @
391
       (control_on_clock node args call_ck (MStep (var_p, o, vl))) :: s)
392

    
393
   (* special treatment depending on the active backend. For horn backend, x = ite (g,t,e)
394
      are preserved. While they are replaced as if g then x = t else x = e in  C or Java
395
      backends. *)
396
  | [x], Expr_ite   (c, t, e)
397
    when (match !Options.output with | "horn" -> true | "C" | "java" | _ -> false)
398
      ->
399
    let var_x = get_node_var x node in
400
    (m,
401
     si,
402
     j,
403
     d,
404
     (control_on_clock node args eq.eq_rhs.expr_clock
405
	(MLocalAssign (var_x, translate_expr node args eq.eq_rhs))::s)
406
    )
407

    
408
  | [x], _                                       -> (
409
    let var_x = get_node_var x node in
410
    (m, si, j, d,
411
     control_on_clock
412
       node
413
       args
414
       eq.eq_rhs.expr_clock
415
       (translate_act node args (var_x, eq.eq_rhs)) :: s
416
    )
417
  )
418
  | _                                            ->
419
    begin
420
      Format.eprintf "unsupported equation: %a@?" Printers.pp_node_eq eq;
421
      assert false
422
    end
423

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

    
439
(* Sort the set of equations of node [nd] according
440
   to the computed schedule [sch]
441
*)
442
let sort_equations_from_schedule nd sch =
443
(*Format.eprintf "%s schedule: %a@."
444
		 nd.node_id
445
		 (Utils.fprintf_list ~sep:" ; " Scheduling.pp_eq_schedule) sch;*)
446
  let split_eqs = Splitting.tuple_split_eq_list (get_node_eqs nd) in
447
  let eqs_rev, remainder =
448
    List.fold_left
449
      (fun (accu, node_eqs_remainder) vl ->
450
       if List.exists (fun eq -> List.exists (fun v -> List.mem v eq.eq_lhs) vl) 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
    List.rev eqs_rev
467
  end
468

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

    
480
let translate_eqs node args eqs =
481
  List.fold_right (fun eq args -> translate_eq node args eq) eqs args;;
482

    
483
let translate_decl nd sch =
484
  (*Log.report ~level:1 (fun fmt -> Printers.pp_node fmt nd);*)
485

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

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

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

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

    
552

    
553
let value_of_ident m id =
554
  (* is is a state var *)
555
  try
556
    StateVar (List.find (fun v -> v.var_id = id) m.mmemory)
557
  with Not_found ->
558
  try (* id is a node var *)
559
    LocalVar (get_node_var id m.mname)
560
  with Not_found ->
561
    try (* id is a constant *)
562
      LocalVar (Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id)))
563
    with Not_found ->
564
      (* id is a tag *)
565
      Cst (Const_tag id)
566

    
567
let rec value_of_dimension m dim =
568
  match dim.Dimension.dim_desc with
569
  | Dimension.Dbool b         -> Cst (Const_tag (if b then Corelang.tag_true else Corelang.tag_false))
570
  | Dimension.Dint i          -> Cst (Const_int i)
571
  | Dimension.Dident v        -> value_of_ident m v
572
  | Dimension.Dappl (f, args) -> Fun (f, List.map (value_of_dimension m) args)
573
  | Dimension.Dite (i, t, e)  -> Fun ("ite", List.map (value_of_dimension m) [i; t; e])
574
  | Dimension.Dlink dim'      -> value_of_dimension m dim'
575
  | _                         -> assert false
576

    
577
let rec dimension_of_value value =
578
  match value with
579
  | Cst (Const_tag t) when t = Corelang.tag_true  -> Dimension.mkdim_bool  Location.dummy_loc true
580
  | Cst (Const_tag t) when t = Corelang.tag_false -> Dimension.mkdim_bool  Location.dummy_loc false
581
  | Cst (Const_int i)                             -> Dimension.mkdim_int   Location.dummy_loc i
582
  | LocalVar v                                    -> Dimension.mkdim_ident Location.dummy_loc v.var_id
583
  | Fun (f, args)                                 -> Dimension.mkdim_appl  Location.dummy_loc f (List.map dimension_of_value args)
584
  | _                                             -> assert false
585

    
586
(* Local Variables: *)
587
(* compile-command:"make -C .." *)
588
(* End: *)