Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / machine_code.ml @ 53206908

History | View | Annotate | Download (23 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
    | MStep (il, i, vl)  ->
42
      Format.fprintf fmt "%a = %s (%a)"
43
	(Utils.fprintf_list ~sep:", " (fun fmt v -> Format.pp_print_string fmt v.var_id)) il
44
	i      
45
	(Utils.fprintf_list ~sep:", " pp_val) vl
46
    | MBranch (g,hl)     ->
47
      Format.fprintf fmt "@[<v 2>case(%a) {@,%a@,}@]"
48
	pp_val g
49
	(Utils.fprintf_list ~sep:"@," pp_branch) hl
50
    | MComment s -> Format.pp_print_string fmt s
51

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

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

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

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

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

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

    
92

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

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

    
110
let rec is_const_value v =
111
  match v.value_desc with
112
  | Cst _          -> true
113
  | Fun (id, args) -> Basic_library.is_value_internal_fun v && List.for_all is_const_value args
114
  | _              -> false
115

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

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

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

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

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

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

    
145
let arrow_id = "_arrow"
146

    
147
let arrow_typ = Types.new_ty Types.Tunivar
148

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

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

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

    
178
let arrow_machine =
179
  let state = "_first" in
180
  let var_state = dummy_var_decl state (Types.new_ty Types.Tbool) in
181
  let var_input1 = List.nth arrow_desc.node_inputs 0 in
182
  let var_input2 = List.nth arrow_desc.node_inputs 1 in
183
  let var_output = List.nth arrow_desc.node_outputs 0 in
184
  let cst b = mk_val (Cst (const_of_bool b)) Type_predef.type_bool in
185
  let t_arg = Types.new_univar () in (* TODO Xavier: c'est bien la bonne def ? *)
186
  {
187
    mname = arrow_desc;
188
    mmemory = [var_state];
189
    mcalls = [];
190
    minstances = [];
191
    minit = [MStateAssign(var_state, cst 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 (mk_val (StateVar var_state) Type_predef.type_bool)
200
			         [MStateAssign(var_state, cst false);
201
                                  MLocalAssign(var_output, mk_val (LocalVar var_input1) t_arg)]
202
                                 [MLocalAssign(var_output, mk_val (LocalVar var_input2) t_arg)] ];
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
(* translate_<foo> : node -> context -> <foo> -> machine code/expression *)
226
(* the context contains  m : state aka memory variables  *)
227
(*                      si : initialization instructions *)
228
(*                       j : node aka machine instances  *)
229
(*                       d : local variables             *)
230
(*                       s : step instructions           *)
231
let translate_ident node (m, si, j, d, s) id =
232
  try (* id is a node var *)
233
    let var_id = get_node_var id node in
234
    if ISet.exists (fun v -> v.var_id = id) m
235
    then mk_val (StateVar var_id) var_id.var_type
236
    else mk_val (LocalVar var_id) var_id.var_type
237
  with Not_found ->
238
    try (* id is a constant *)
239
      let vdecl = (Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id))) in
240
      mk_val (LocalVar vdecl) vdecl.var_type
241
    with Not_found ->
242
      (* id is a tag *)
243
      (* TODO construire une liste des enum declar├ęs et alors chercher dedans la liste
244
	 qui contient id *)
245
      let cst = Const_tag id in
246
      mk_val (Cst cst) (Typing.type_const Location.dummy_loc cst) 
247
	
248
let rec control_on_clock node ((m, si, j, d, s) as args) ck inst =
249
 match (Clocks.repr ck).cdesc with
250
 | Con    (ck1, cr, l) ->
251
   let id  = Clocks.const_of_carrier cr in
252
   control_on_clock node args ck1 (MBranch (translate_ident node args id,
253
					    [l, [inst]] ))
254
 | _                   -> inst
255

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

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

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

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

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

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

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

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

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

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

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

    
399
   (* special treatment depending on the active backend. For horn backend, x = ite (g,t,e)
400
      are preserved. While they are replaced as if g then x = t else x = e in  C or Java
401
      backends. *)
402
  | [x], Expr_ite   (c, t, e) 
403
    when (match !Options.output with | "horn" -> true | "C" | "java" | _ -> false)
404
      -> 
405
    let var_x = get_node_var x node in
406
    (m, 
407
     si, 
408
     j, 
409
     d, 
410
     (control_on_clock node args eq.eq_rhs.expr_clock 
411
	(MLocalAssign (var_x, translate_expr node args eq.eq_rhs))::s)
412
    )
413
      
414
  | [x], _                                       -> (
415
    let var_x = get_node_var x node in
416
    (m, si, j, d, 
417
     control_on_clock 
418
       node
419
       args
420
       eq.eq_rhs.expr_clock
421
       (translate_act node args (var_x, eq.eq_rhs)) :: s
422
    )
423
  )
424
  | _                                            ->
425
    begin
426
      Format.eprintf "internal error: Machine_code.translate_eq %a@?" Printers.pp_node_eq eq;
427
      assert false
428
    end
429

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

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

    
486
let constant_equations nd =
487
 List.fold_right (fun vdecl eqs ->
488
   if vdecl.var_dec_const
489
   then
490
     { eq_lhs = [vdecl.var_id];
491
       eq_rhs = Utils.desome vdecl.var_dec_value;
492
       eq_loc = vdecl.var_loc
493
     } :: eqs
494
   else eqs)
495
   nd.node_locals []
496

    
497
let translate_eqs node args eqs =
498
  List.fold_right (fun eq args -> translate_eq node args eq) eqs args;;
499

    
500
let translate_decl nd sch =
501
  (*Log.report ~level:1 (fun fmt -> Printers.pp_node fmt nd);*)
502

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

    
544
(** takes the global declarations and the scheduling associated to each node *)
545
let translate_prog decls node_schs =
546
  let nodes = get_nodes decls in 
547
  List.map 
548
    (fun decl -> 
549
     let node = node_of_top decl in
550
      let sch = (Utils.IMap.find node.node_id node_schs).Scheduling.schedule in
551
      translate_decl node sch 
552
    ) nodes
553

    
554
let get_machine_opt name machines =  
555
  List.fold_left 
556
    (fun res m -> 
557
      match res with 
558
      | Some _ -> res 
559
      | None -> if m.mname.node_id = name then Some m else None)
560
    None machines
561

    
562
let get_const_assign m id =
563
  try
564
    match (List.find (fun instr -> match instr with MLocalAssign (v, _) -> v == id | _ -> false) m.mconst) with
565
    | MLocalAssign (_, e) -> e
566
    | _                   -> assert false
567
  with Not_found -> assert false
568

    
569

    
570
let value_of_ident m id =
571
  (* is is a state var *)
572
  try
573
    let mem = List.find (fun v -> v.var_id = id) m.mmemory in
574
    mk_val (StateVar mem) mem.var_type
575
  with Not_found ->
576
  try (* id is a node var *)
577
    let var = get_node_var id m.mname in
578
    mk_val (LocalVar var) var.var_type
579
  with Not_found ->
580
    try (* id is a constant *)
581
      let cst = Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id)) in
582
      mk_val (LocalVar cst) cst.var_type
583
    with Not_found ->
584
      (* id is a tag *)
585
      let tag = Const_tag id in
586
      mk_val (Cst tag) (Typing.type_const Location.dummy_loc tag)
587

    
588
let rec value_of_dimension m dim =
589
  match dim.Dimension.dim_desc with
590
  | Dimension.Dbool b         -> mk_val (Cst (Const_tag (if b then Corelang.tag_true else Corelang.tag_false))) Type_predef.type_bool
591
  | Dimension.Dint i          -> mk_val (Cst (Const_int i)) Type_predef.type_int
592
  | Dimension.Dident v        -> value_of_ident m v
593
  | Dimension.Dappl (f, args) -> let typ = if Basic_library.is_numeric_operator f then Type_predef.type_int else Type_predef.type_bool
594
                                 in mk_val (Fun (f, List.map (value_of_dimension m) args)) typ
595
  | Dimension.Dite (i, t, e)  -> let [vi; vt; ve] = List.map (value_of_dimension m) [i; t; e] in
596
				 mk_val (Fun ("ite", [vi; vt; ve])) vt.value_type
597
  | Dimension.Dlink dim'      -> value_of_dimension m dim'
598
  | _                         -> assert false
599

    
600
let rec dimension_of_value value =
601
  match value.value_desc with
602
  | Cst (Const_tag t) when t = Corelang.tag_true  -> Dimension.mkdim_bool  Location.dummy_loc true
603
  | Cst (Const_tag t) when t = Corelang.tag_false -> Dimension.mkdim_bool  Location.dummy_loc false
604
  | Cst (Const_int i)                             -> Dimension.mkdim_int   Location.dummy_loc i
605
  | LocalVar v                                    -> Dimension.mkdim_ident Location.dummy_loc v.var_id
606
  | Fun (f, args)                                 -> Dimension.mkdim_appl  Location.dummy_loc f (List.map dimension_of_value args)
607
  | _                                             -> assert false
608

    
609
(* Local Variables: *)
610
(* compile-command:"make -C .." *)
611
(* End: *)