Project

General

Profile

Download (21.6 KB) Statistics
| Branch: | Tag: | Revision:
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
and pp_instrs fmt il =
69
  Format.fprintf fmt "@[<v 2>%a@]" (Utils.fprintf_list ~sep:"@," pp_instr) il
70

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

    
80
type static_call = top_decl * (Dimension.dim_expr list)
81

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

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

    
104

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

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

    
122
(* Returns the declared stateless status and the computed one. *)
123
let get_stateless_status m =
124
 (m.mname.node_dec_stateless, Utils.desome m.mname.node_stateless)
125

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

    
129
let is_output m id =
130
  List.exists (fun o -> o.var_id = id.var_id) m.mstep.step_outputs
131

    
132
let is_memory m id =
133
  List.exists (fun o -> o.var_id = id.var_id) m.mmemory
134

    
135
let conditional c t e =
136
  MBranch(c, [ (tag_true, t); (tag_false, e) ])
137

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

    
151
let arrow_id = "_arrow"
152

    
153
let arrow_typ = Types.new_ty Types.Tunivar
154

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

    
172
let arrow_top_decl =
173
  {
174
    top_decl_desc = Node arrow_desc;
175
    top_decl_owner = Version.include_path;
176
    top_decl_itf = false;
177
    top_decl_loc = Location.dummy_loc
178
  }
179

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

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

    
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 StateVar var_id
237
    else LocalVar var_id
238
  with Not_found ->
239
    try (* id is a constant *)
240
      LocalVar (Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id)))
241
    with Not_found ->
242
      (* id is a tag *)
243
      Cst (Const_tag id)
244

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

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

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

    
271
let join_guards_list insts =
272
 List.fold_right join_guards insts []
273

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

    
290
let specialize_op expr =
291
  match !Options.output with
292
  | "C" -> specialize_to_c expr
293
  | _   -> expr
294

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

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

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

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

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

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

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

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

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

    
443
(* Sort the set of equations of node [nd] according
444
   to the computed schedule [sch]
445
*)
446
let sort_equations_from_schedule nd sch =
447
(*Format.eprintf "%s schedule: %a@."
448
		 nd.node_id
449
		 (Utils.fprintf_list ~sep:" ; " Scheduling.pp_eq_schedule) sch;*)
450
  let split_eqs = Splitting.tuple_split_eq_list (get_node_eqs nd) in
451
  let eqs_rev, remainder =
452
    List.fold_left
453
      (fun (accu, node_eqs_remainder) vl ->
454
       if List.exists (fun eq -> List.exists (fun v -> List.mem v eq.eq_lhs) vl) accu
455
       then
456
	 (accu, node_eqs_remainder)
457
       else
458
	 let eq_v, remainder = find_eq vl node_eqs_remainder in
459
	 eq_v::accu, remainder
460
      )
461
      ([], split_eqs)
462
      sch
463
  in
464
  begin
465
    if List.length remainder > 0 then (
466
      Format.eprintf "Equations not used are@.%a@.Full equation set is:@.%a@.@?"
467
		     Printers.pp_node_eqs remainder
468
      		     Printers.pp_node_eqs (get_node_eqs nd);
469
      assert false);
470
    List.rev eqs_rev
471
  end
472

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

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

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

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

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

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

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

    
556

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

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

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

    
590
(* Local Variables: *)
591
(* compile-command:"make -C .." *)
592
(* End: *)
(30-30/50)