Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / machine_code.ml @ cf9cc6f9

History | View | Annotate | Download (22.8 KB)

1 b38ffff3 ploc
(********************************************************************)
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 0cbf0839 ploc
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 58272238 tkahsai
type value_t =
25 0cbf0839 ploc
  | Cst of constant
26
  | LocalVar of var_decl
27
  | StateVar of var_decl
28 58272238 tkahsai
  | Fun of ident * value_t list
29 0cbf0839 ploc
  | 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 cf9cc6f9 Ploc
  | MNoReset of ident (* used to symmetrize the reset function *)
38 0cbf0839 ploc
  | MStep of var_decl list * ident * value_t list
39
  | MBranch of value_t * (label * instr_t list) list
40 58272238 tkahsai
41 0cbf0839 ploc
let rec pp_val fmt v =
42
  match v with
43 58272238 tkahsai
    | Cst c         -> Printers.pp_const fmt c
44 0cbf0839 ploc
    | LocalVar v    -> Format.pp_print_string fmt v.var_id
45
    | StateVar v    -> Format.pp_print_string fmt v.var_id
46
    | Array vl      -> Format.fprintf fmt "[%a]" (Utils.fprintf_list ~sep:", " pp_val)  vl
47
    | Access (t, i) -> Format.fprintf fmt "%a[%a]" pp_val t pp_val i
48
    | Power (v, n)  -> Format.fprintf fmt "(%a^%a)" pp_val v pp_val n
49
    | Fun (n, vl)   -> Format.fprintf fmt "%s (%a)" n (Utils.fprintf_list ~sep:", " pp_val)  vl
50
51
let rec pp_instr fmt i =
52 58272238 tkahsai
  match i with
53 0cbf0839 ploc
    | MLocalAssign (i,v) -> Format.fprintf fmt "%s<-l- %a" i.var_id pp_val v
54
    | MStateAssign (i,v) -> Format.fprintf fmt "%s<-s- %a" i.var_id pp_val v
55
    | MReset i           -> Format.fprintf fmt "reset %s" i
56 cf9cc6f9 Ploc
    | MNoReset i         -> ()
57 0cbf0839 ploc
    | MStep (il, i, vl)  ->
58
      Format.fprintf fmt "%a = %s (%a)"
59
	(Utils.fprintf_list ~sep:", " (fun fmt v -> Format.pp_print_string fmt v.var_id)) il
60 58272238 tkahsai
	i
61 0cbf0839 ploc
	(Utils.fprintf_list ~sep:", " pp_val) vl
62
    | MBranch (g,hl)     ->
63
      Format.fprintf fmt "@[<v 2>case(%a) {@,%a@,}@]"
64
	pp_val g
65
	(Utils.fprintf_list ~sep:"@," pp_branch) hl
66
67
and pp_branch fmt (t, h) =
68
  Format.fprintf fmt "@[<v 2>%s:@,%a@]" t (Utils.fprintf_list ~sep:"@," pp_instr) h
69
70 b3b0dd56 xthirioux
and pp_instrs fmt il =
71
  Format.fprintf fmt "@[<v 2>%a@]" (Utils.fprintf_list ~sep:"@," pp_instr) il
72
73 0cbf0839 ploc
type step_t = {
74
  step_checks: (Location.t * value_t) list;
75
  step_inputs: var_decl list;
76
  step_outputs: var_decl list;
77
  step_locals: var_decl list;
78
  step_instrs: instr_t list;
79 36454535 ploc
  step_asserts: value_t list;
80 0cbf0839 ploc
}
81
82
type static_call = top_decl * (Dimension.dim_expr list)
83
84
type machine_t = {
85
  mname: node_desc;
86
  mmemory: var_decl list;
87
  mcalls: (ident * static_call) list; (* map from stateful/stateless instance to node, no internals *)
88
  minstances: (ident * static_call) list; (* sub-map of mcalls, from stateful instance to node *)
89
  minit: instr_t list;
90
  mstatic: var_decl list; (* static inputs only *)
91 01d48bb0 xthirioux
  mconst: instr_t list; (* assignments of node constant locals *)
92 0cbf0839 ploc
  mstep: step_t;
93
  mspec: node_annot option;
94 0038002e ploc
  mannot: expr_annot list;
95 0cbf0839 ploc
}
96
97
let pp_step fmt s =
98 36454535 ploc
  Format.fprintf fmt "@[<v>inputs : %a@ outputs: %a@ locals : %a@ checks : %a@ instrs : @[%a@]@ asserts : @[%a@]@]@ "
99 0cbf0839 ploc
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_inputs
100
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_outputs
101
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_locals
102
    (Utils.fprintf_list ~sep:", " (fun fmt (_, c) -> pp_val fmt c)) s.step_checks
103
    (Utils.fprintf_list ~sep:"@ " pp_instr) s.step_instrs
104 36454535 ploc
    (Utils.fprintf_list ~sep:", " pp_val) s.step_asserts
105
106 0cbf0839 ploc
107
let pp_static_call fmt (node, args) =
108
 Format.fprintf fmt "%s<%a>"
109
   (node_name node)
110
   (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) args
111
112
let pp_machine fmt m =
113 58272238 tkahsai
  Format.fprintf fmt
114 01d48bb0 xthirioux
    "@[<v 2>machine %s@ mem      : %a@ instances: %a@ init     : %a@ const    : %a@ step     :@   @[<v 2>%a@]@ @  spec : @[%t@]@  annot : @[%a@]@]@ "
115 0cbf0839 ploc
    m.mname.node_id
116
    (Utils.fprintf_list ~sep:", " Printers.pp_var) m.mmemory
117
    (Utils.fprintf_list ~sep:", " (fun fmt (o1, o2) -> Format.fprintf fmt "(%s, %a)" o1 pp_static_call o2)) m.minstances
118
    (Utils.fprintf_list ~sep:"@ " pp_instr) m.minit
119 01d48bb0 xthirioux
    (Utils.fprintf_list ~sep:"@ " pp_instr) m.mconst
120 0cbf0839 ploc
    pp_step m.mstep
121 36454535 ploc
    (fun fmt -> match m.mspec with | None -> () | Some spec -> Printers.pp_spec fmt spec)
122
    (Utils.fprintf_list ~sep:"@ " Printers.pp_expr_annot) m.mannot
123 0cbf0839 ploc
124 5538b7ac xthirioux
(* Returns the declared stateless status and the computed one. *)
125
let get_stateless_status m =
126
 (m.mname.node_dec_stateless, Utils.desome m.mname.node_stateless)
127
128 1174cdd9 xthirioux
let is_input m id =
129
  List.exists (fun o -> o.var_id = id.var_id) m.mstep.step_inputs
130
131 0cbf0839 ploc
let is_output m id =
132
  List.exists (fun o -> o.var_id = id.var_id) m.mstep.step_outputs
133
134 1174cdd9 xthirioux
let is_memory m id =
135
  List.exists (fun o -> o.var_id = id.var_id) m.mmemory
136
137 0cbf0839 ploc
let conditional c t e =
138
  MBranch(c, [ (tag_true, t); (tag_false, e) ])
139
140
let dummy_var_decl name typ =
141
  {
142
    var_id = name;
143 6a1a01d2 xthirioux
    var_orig = false;
144 0cbf0839 ploc
    var_dec_type = dummy_type_dec;
145
    var_dec_clock = dummy_clock_dec;
146
    var_dec_const = false;
147 01d48bb0 xthirioux
    var_dec_value = None;
148 0cbf0839 ploc
    var_type =  typ;
149
    var_clock = Clocks.new_ck (Clocks.Cvar Clocks.CSet_all) true;
150
    var_loc = Location.dummy_loc
151
  }
152
153
let arrow_id = "_arrow"
154
155
let arrow_typ = Types.new_ty Types.Tunivar
156
157
let arrow_desc =
158
  {
159
    node_id = arrow_id;
160
    node_type = Type_predef.type_bin_poly_op;
161
    node_clock = Clock_predef.ck_bin_univ;
162
    node_inputs= [dummy_var_decl "_in1" arrow_typ; dummy_var_decl "_in2" arrow_typ];
163
    node_outputs= [dummy_var_decl "_out" arrow_typ];
164
    node_locals= [];
165
    node_gencalls = [];
166
    node_checks = [];
167
    node_asserts = [];
168 1eda3e78 xthirioux
    node_stmts= [];
169 d3e4c22f xthirioux
    node_dec_stateless = false;
170
    node_stateless = Some false;
171 0cbf0839 ploc
    node_spec = None;
172 0038002e ploc
    node_annot = [];  }
173 0cbf0839 ploc
174
let arrow_top_decl =
175
  {
176
    top_decl_desc = Node arrow_desc;
177 5ae8db15 ploc
    top_decl_owner = Version.include_path;
178 70e1006b xthirioux
    top_decl_itf = false;
179 0cbf0839 ploc
    top_decl_loc = Location.dummy_loc
180
  }
181
182
let arrow_machine =
183
  let state = "_first" in
184
  let var_state = dummy_var_decl state (Types.new_ty Types.Tbool) in
185
  let var_input1 = List.nth arrow_desc.node_inputs 0 in
186
  let var_input2 = List.nth arrow_desc.node_inputs 1 in
187
  let var_output = List.nth arrow_desc.node_outputs 0 in
188
  {
189
    mname = arrow_desc;
190
    mmemory = [var_state];
191
    mcalls = [];
192
    minstances = [];
193
    minit = [MStateAssign(var_state, Cst (const_of_bool true))];
194 01d48bb0 xthirioux
    mconst = [];
195 0cbf0839 ploc
    mstatic = [];
196
    mstep = {
197
      step_inputs = arrow_desc.node_inputs;
198
      step_outputs = arrow_desc.node_outputs;
199
      step_locals = [];
200
      step_checks = [];
201
      step_instrs = [conditional (StateVar var_state)
202
			         [MStateAssign(var_state, Cst (const_of_bool false));
203
                                  MLocalAssign(var_output, LocalVar var_input1)]
204 36454535 ploc
                                 [MLocalAssign(var_output, LocalVar var_input2)] ];
205
      step_asserts = [];
206 0cbf0839 ploc
    };
207
    mspec = None;
208 0038002e ploc
    mannot = [];
209 0cbf0839 ploc
  }
210
211
let new_instance =
212
  let cpt = ref (-1) in
213
  fun caller callee tag ->
214
    begin
215
      let o =
216 5538b7ac xthirioux
	if Stateless.check_node callee then
217 0cbf0839 ploc
	  node_name callee
218
	else
219
	  Printf.sprintf "ni_%d" (incr cpt; !cpt) in
220
      let o =
221
	if !Options.ansi && is_generic_node callee
222
	then Printf.sprintf "%s_inst_%d" o (Utils.position (fun e -> e.expr_tag = tag) caller.node_gencalls)
223
	else o in
224
      o
225
    end
226
227 01d48bb0 xthirioux
228 0cbf0839 ploc
(* translate_<foo> : node -> context -> <foo> -> machine code/expression *)
229
(* the context contains  m : state aka memory variables  *)
230
(*                      si : initialization instructions *)
231
(*                       j : node aka machine instances  *)
232
(*                       d : local variables             *)
233
(*                       s : step instructions           *)
234
let translate_ident node (m, si, j, d, s) id =
235
  try (* id is a node var *)
236 0038002e ploc
    let var_id = get_node_var id node in
237 0cbf0839 ploc
    if ISet.exists (fun v -> v.var_id = id) m
238
    then StateVar var_id
239
    else LocalVar var_id
240 70e1006b xthirioux
  with Not_found ->
241
    try (* id is a constant *)
242
      LocalVar (Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id)))
243
    with Not_found ->
244
      (* id is a tag *)
245
      Cst (Const_tag id)
246 0cbf0839 ploc
247
let rec control_on_clock node ((m, si, j, d, s) as args) ck inst =
248 2ea1e4a6 xthirioux
 match (Clocks.repr ck).cdesc with
249 0cbf0839 ploc
 | Con    (ck1, cr, l) ->
250 84d9893e xthirioux
   let id  = Clocks.const_of_carrier cr in
251 0cbf0839 ploc
   control_on_clock node args ck1 (MBranch (translate_ident node args id,
252
					    [l, [inst]] ))
253
 | _                   -> inst
254
255
let rec join_branches hl1 hl2 =
256
 match hl1, hl2 with
257
 | []          , _            -> hl2
258
 | _           , []           -> hl1
259
 | (t1, h1)::q1, (t2, h2)::q2 ->
260
   if t1 < t2 then (t1, h1) :: join_branches q1 hl2 else
261
   if t1 > t2 then (t2, h2) :: join_branches hl1 q2
262
   else (t1, List.fold_right join_guards h1 h2) :: join_branches q1 q2
263
264
and join_guards inst1 insts2 =
265
 match inst1, insts2 with
266
 | _                   , []                               ->
267
   [inst1]
268
 | MBranch (x1, hl1), MBranch (x2, hl2) :: q when x1 = x2 ->
269
   MBranch (x1, join_branches (sort_handlers hl1) (sort_handlers hl2))
270
   :: q
271
 | _ -> inst1 :: insts2
272
273
let join_guards_list insts =
274
 List.fold_right join_guards insts []
275
276 fa0db9d5 xthirioux
(* specialize predefined (polymorphic) operators
277 58272238 tkahsai
   wrt their instances, so that the C semantics
278 fa0db9d5 xthirioux
   is preserved *)
279
let specialize_to_c expr =
280
 match expr.expr_desc with
281
 | Expr_appl (id, e, r) ->
282
   if List.exists (fun e -> Types.is_bool_type e.expr_type) (expr_list_of_expr e)
283
   then let id =
284
	  match id with
285
	  | "="  -> "equi"
286
	  | "!=" -> "xor"
287
	  | _    -> id in
288
	{ expr with expr_desc = Expr_appl (id, e, r) }
289
   else expr
290
 | _ -> expr
291
292
let specialize_op expr =
293
  match !Options.output with
294
  | "C" -> specialize_to_c expr
295
  | _   -> expr
296
297 7c95dcab ploc
let rec merge_to_ite g hl =
298
  let loc = Location.dummy_loc in
299
  let mkcst x = mkexpr loc (Expr_const (Const_tag x)) in
300
  let g_expr = mkcst g in
301
  match hl with
302
  | [] -> assert false
303
  | [_, e] -> e
304
  | (l_c,l_e)::tl -> 
305
    let cond_expr = 
306
      mkpredef_call loc "=" [g_expr; mkcst l_c]
307
    in
308
    mkexpr loc (Expr_ite (cond_expr, l_e, merge_to_ite g tl))
309
      
310 01d48bb0 xthirioux
let rec translate_expr ?(ite=false) node ((m, si, j, d, s) as args) expr =
311 e42fb618 ploc
  let expr = specialize_op expr in
312 0cbf0839 ploc
 match expr.expr_desc with
313
 | Expr_const v                     -> Cst v
314
 | Expr_ident x                     -> translate_ident node args x
315
 | Expr_array el                    -> Array (List.map (translate_expr node args) el)
316
 | Expr_access (t, i)               -> Access (translate_expr node args t, translate_expr node args (expr_of_dimension i))
317
 | Expr_power  (e, n)               -> Power  (translate_expr node args e, translate_expr node args (expr_of_dimension n))
318
 | Expr_tuple _
319 58272238 tkahsai
 | Expr_arrow _
320 0cbf0839 ploc
 | Expr_fby _
321
 | Expr_pre _                       -> (Printers.pp_expr Format.err_formatter expr; Format.pp_print_flush Format.err_formatter (); raise NormalizationError)
322
 | Expr_when    (e1, _, _)          -> translate_expr node args e1
323 7c95dcab ploc
 | Expr_merge   (g, hl)              -> (
324 cf9cc6f9 Ploc
   (* (\* Special treatment for functional backends. Is transformed into Ite *\) *)
325
   (* match !Options.output with *)
326
   (* | "horn" -> translate_expr node  args (merge_to_ite g hl) *)
327
   (* | ("C" | "java")          -> raise NormalizationError (\* should have been replaced by MBranch *\) *)
328
   (* | _ -> *)
329 7c95dcab ploc
     (Format.eprintf "option:%s@." !Options.output; Printers.pp_expr Format.err_formatter expr; Format.pp_print_flush Format.err_formatter (); raise NormalizationError)
330
331
 )
332
  
333 0cbf0839 ploc
 | Expr_appl (id, e, _) when Basic_library.is_internal_fun id ->
334
   let nd = node_from_name id in
335 fa0db9d5 xthirioux
   Fun (node_name nd, List.map (translate_expr node args) (expr_list_of_expr e))
336 29ad4531 ploc
 | Expr_ite (g,t,e) -> (
337
   (* special treatment depending on the active backend. For horn backend, ite
338
      are preserved in expression. While they are removed for C or Java
339
      backends. *)
340 01d48bb0 xthirioux
   match !Options.output with
341 b3b0dd56 xthirioux
   | "horn" ->
342
     Fun ("ite", [translate_expr node args g; translate_expr node args t; translate_expr node args e])
343 01d48bb0 xthirioux
   | ("C" | "java") when ite ->
344 29ad4531 ploc
     Fun ("ite", [translate_expr node args g; translate_expr node args t; translate_expr node args e])
345 01d48bb0 xthirioux
   | _ ->
346 b3b0dd56 xthirioux
     (Format.eprintf "option:%s@." !Options.output; Printers.pp_expr Format.err_formatter expr; Format.pp_print_flush Format.err_formatter (); raise NormalizationError)
347 29ad4531 ploc
 )
348 0cbf0839 ploc
 | _                   -> raise NormalizationError
349
350
let translate_guard node args expr =
351 29ad4531 ploc
  match expr.expr_desc with
352
  | Expr_ident x  -> translate_ident node args x
353 84d9893e xthirioux
  | _ -> (Format.eprintf "internal error: translate_guard %s %a@." node.node_id Printers.pp_expr expr;assert false)
354 0cbf0839 ploc
355
let rec translate_act node ((m, si, j, d, s) as args) (y, expr) =
356 29ad4531 ploc
  match expr.expr_desc with
357
  | Expr_ite   (c, t, e) -> let g = translate_guard node args c in
358
			    conditional g [translate_act node args (y, t)]
359
                              [translate_act node args (y, e)]
360
  | Expr_merge (x, hl)   -> MBranch (translate_ident node args x, List.map (fun (t,  h) -> t, [translate_act node args (y, h)]) hl)
361 58272238 tkahsai
  | _                    ->
362 e42fb618 ploc
    MLocalAssign (y, translate_expr node args expr)
363 0cbf0839 ploc
364
let reset_instance node args i r c =
365 29ad4531 ploc
  match r with
366
  | None        -> []
367 6a1a01d2 xthirioux
  | Some r      -> let g = translate_guard node args r in
368 cf9cc6f9 Ploc
                   [control_on_clock node args c (conditional g [MReset i] [MNoReset i])]
369 0cbf0839 ploc
370
let translate_eq node ((m, si, j, d, s) as args) eq =
371 e42fb618 ploc
  (* Format.eprintf "translate_eq %a with clock %a@." Printers.pp_node_eq eq Clocks.print_ck eq.eq_rhs.expr_clock; *)
372 0cbf0839 ploc
  match eq.eq_lhs, eq.eq_rhs.expr_desc with
373
  | [x], Expr_arrow (e1, e2)                     ->
374 0038002e ploc
    let var_x = get_node_var x node in
375 0cbf0839 ploc
    let o = new_instance node arrow_top_decl eq.eq_rhs.expr_tag in
376
    let c1 = translate_expr node args e1 in
377
    let c2 = translate_expr node args e2 in
378
    (m,
379
     MReset o :: si,
380
     Utils.IMap.add o (arrow_top_decl, []) j,
381
     d,
382
     (control_on_clock node args eq.eq_rhs.expr_clock (MStep ([var_x], o, [c1;c2]))) :: s)
383 0038002e ploc
  | [x], Expr_pre e1 when ISet.mem (get_node_var x node) d     ->
384
    let var_x = get_node_var x node in
385 0cbf0839 ploc
    (ISet.add var_x m,
386
     si,
387
     j,
388
     d,
389
     control_on_clock node args eq.eq_rhs.expr_clock (MStateAssign (var_x, translate_expr node args e1)) :: s)
390 0038002e ploc
  | [x], Expr_fby (e1, e2) when ISet.mem (get_node_var x node) d ->
391
    let var_x = get_node_var x node in
392 0cbf0839 ploc
    (ISet.add var_x m,
393
     MStateAssign (var_x, translate_expr node args e1) :: si,
394
     j,
395
     d,
396
     control_on_clock node args eq.eq_rhs.expr_clock (MStateAssign (var_x, translate_expr node args e2)) :: s)
397 1ad092fb ploc
398
  | p  , Expr_appl (f, arg, r) when not (Basic_library.is_internal_fun f) ->
399 0038002e ploc
    let var_p = List.map (fun v -> get_node_var v node) p in
400 1837ce98 xthirioux
    let el = expr_list_of_expr arg in
401 0cbf0839 ploc
    let vl = List.map (translate_expr node args) el in
402
    let node_f = node_from_name f in
403
    let call_f =
404
      node_f,
405 58272238 tkahsai
      NodeDep.filter_static_inputs (node_inputs node_f) el in
406 0cbf0839 ploc
    let o = new_instance node node_f eq.eq_rhs.expr_tag in
407 6a1a01d2 xthirioux
    let env_cks = List.fold_right (fun arg cks -> arg.expr_clock :: cks) el [eq.eq_rhs.expr_clock] in
408
    let call_ck = Clock_calculus.compute_root_clock (Clock_predef.ck_tuple env_cks) in
409
    (*Clocks.new_var true in
410 01f1a1f4 xthirioux
    Clock_calculus.unify_imported_clock (Some call_ck) eq.eq_rhs.expr_clock eq.eq_rhs.expr_loc;
411 6a1a01d2 xthirioux
    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;*)
412 0cbf0839 ploc
    (m,
413 5538b7ac xthirioux
     (if Stateless.check_node node_f then si else MReset o :: si),
414 1ad092fb ploc
     Utils.IMap.add o call_f j,
415 0cbf0839 ploc
     d,
416 d5fe9ac9 xthirioux
     (if Stateless.check_node node_f
417
      then []
418
      else reset_instance node args o r call_ck) @
419 84d9893e xthirioux
       (control_on_clock node args call_ck (MStep (var_p, o, vl))) :: s)
420 cf9cc6f9 Ploc
(*
421 29ad4531 ploc
   (* special treatment depending on the active backend. For horn backend, x = ite (g,t,e)
422
      are preserved. While they are replaced as if g then x = t else x = e in  C or Java
423
      backends. *)
424 7c95dcab ploc
  | [x], Expr_ite   _
425
  (* similar treatment for merge, avoid generating MBranch instructions when using Horn backend *)
426
  | [x], Expr_merge _ 
427 cf9cc6f9 Ploc
    when (match !Options.output with | "horn" -> false (* TODO 16/12 was true *) | "C" | "java" | _ -> false)
428 7c95dcab ploc
429
      (* Remark for Ocaml: the when is shared among the two patterns *)
430 58272238 tkahsai
      ->
431 0038002e ploc
    let var_x = get_node_var x node in
432 58272238 tkahsai
    (m,
433
     si,
434
     j,
435
     d,
436
     (control_on_clock node args eq.eq_rhs.expr_clock
437 29ad4531 ploc
	(MLocalAssign (var_x, translate_expr node args eq.eq_rhs))::s)
438 04a7df69 ploc
    )
439 58272238 tkahsai
440 cf9cc6f9 Ploc
*)
441 29ad4531 ploc
  | [x], _                                       -> (
442 0038002e ploc
    let var_x = get_node_var x node in
443 58272238 tkahsai
    (m, si, j, d,
444
     control_on_clock
445 1ad092fb ploc
       node
446
       args
447
       eq.eq_rhs.expr_clock
448
       (translate_act node args (var_x, eq.eq_rhs)) :: s
449
    )
450 29ad4531 ploc
  )
451 0cbf0839 ploc
  | _                                            ->
452
    begin
453
      Format.eprintf "unsupported equation: %a@?" Printers.pp_node_eq eq;
454
      assert false
455
    end
456
457 2cf39a8e xthirioux
let find_eq xl eqs =
458
  let rec aux accu eqs =
459
      match eqs with
460
	| [] ->
461
	  begin
462
	    Format.eprintf "Looking for variables %a in the following equations@.%a@."
463
	      (Utils.fprintf_list ~sep:" , " (fun fmt v -> Format.fprintf fmt "%s" v)) xl
464
	      Printers.pp_node_eqs eqs;
465
	    assert false
466
	  end
467 58272238 tkahsai
	| hd::tl ->
468 2cf39a8e xthirioux
	  if List.exists (fun x -> List.mem x hd.eq_lhs) xl then hd, accu@tl else aux (hd::accu) tl
469
    in
470
    aux [] eqs
471
472 58272238 tkahsai
(* Sort the set of equations of node [nd] according
473 2cf39a8e xthirioux
   to the computed schedule [sch]
474
*)
475
let sort_equations_from_schedule nd sch =
476 01d48bb0 xthirioux
(*Format.eprintf "%s schedule: %a@."
477 cfdb4fe9 xthirioux
		 nd.node_id
478
		 (Utils.fprintf_list ~sep:" ; " Scheduling.pp_eq_schedule) sch;*)
479 1eda3e78 xthirioux
  let split_eqs = Splitting.tuple_split_eq_list (get_node_eqs nd) in
480 2cf39a8e xthirioux
  let eqs_rev, remainder =
481 58272238 tkahsai
    List.fold_left
482
      (fun (accu, node_eqs_remainder) vl ->
483 2cf39a8e xthirioux
       if List.exists (fun eq -> List.exists (fun v -> List.mem v eq.eq_lhs) vl) accu
484
       then
485
	 (accu, node_eqs_remainder)
486
       else
487
	 let eq_v, remainder = find_eq vl node_eqs_remainder in
488
	 eq_v::accu, remainder
489 58272238 tkahsai
      )
490
      ([], split_eqs)
491
      sch
492 2cf39a8e xthirioux
  in
493 cfdb4fe9 xthirioux
  begin
494
    if List.length remainder > 0 then (
495
      Format.eprintf "Equations not used are@.%a@.Full equation set is:@.%a@.@?"
496
		     Printers.pp_node_eqs remainder
497 1eda3e78 xthirioux
      		     Printers.pp_node_eqs (get_node_eqs nd);
498 cfdb4fe9 xthirioux
      assert false);
499
    List.rev eqs_rev
500
  end
501 2cf39a8e xthirioux
502 01d48bb0 xthirioux
let constant_equations nd =
503
 List.fold_right (fun vdecl eqs ->
504
   if vdecl.var_dec_const
505
   then
506
     { eq_lhs = [vdecl.var_id];
507
       eq_rhs = Utils.desome vdecl.var_dec_value;
508
       eq_loc = vdecl.var_loc
509
     } :: eqs
510
   else eqs)
511
   nd.node_locals []
512
513 0cbf0839 ploc
let translate_eqs node args eqs =
514
  List.fold_right (fun eq args -> translate_eq node args eq) eqs args;;
515
516 db1c5c00 ploc
let translate_decl nd sch =
517 0cbf0839 ploc
  (*Log.report ~level:1 (fun fmt -> Printers.pp_node fmt nd);*)
518 2cf39a8e xthirioux
519
  let sorted_eqs = sort_equations_from_schedule nd sch in
520 01d48bb0 xthirioux
  let constant_eqs = constant_equations nd in
521
  
522 0cbf0839 ploc
  let init_args = ISet.empty, [], Utils.IMap.empty, List.fold_right (fun l -> ISet.add l) nd.node_locals ISet.empty, [] in
523 36454535 ploc
  (* memories, init instructions, node calls, local variables (including memories), step instrs *)
524 01d48bb0 xthirioux
  let m0, init0, j0, locals0, s0 = translate_eqs nd init_args constant_eqs in
525
  assert (ISet.is_empty m0);
526
  assert (init0 = []);
527
  assert (Utils.IMap.is_empty j0);
528 55537f48 xthirioux
  let m, init, j, locals, s = translate_eqs nd (m0, init0, j0, locals0, []) sorted_eqs in
529 0cbf0839 ploc
  let mmap = Utils.IMap.fold (fun i n res -> (i, n)::res) j [] in
530
  {
531
    mname = nd;
532
    mmemory = ISet.elements m;
533
    mcalls = mmap;
534 5538b7ac xthirioux
    minstances = List.filter (fun (_, (n,_)) -> not (Stateless.check_node n)) mmap;
535 0cbf0839 ploc
    minit = init;
536 01d48bb0 xthirioux
    mconst = s0;
537 0cbf0839 ploc
    mstatic = List.filter (fun v -> v.var_dec_const) nd.node_inputs;
538
    mstep = {
539
      step_inputs = nd.node_inputs;
540
      step_outputs = nd.node_outputs;
541
      step_locals = ISet.elements (ISet.diff locals m);
542
      step_checks = List.map (fun d -> d.Dimension.dim_loc, translate_expr nd init_args (expr_of_dimension d)) nd.node_checks;
543 29ad4531 ploc
      step_instrs = (
544
	(* special treatment depending on the active backend. For horn backend,
545
	   common branches are not merged while they are in C or Java
546
	   backends. *)
547
	match !Options.output with
548 cf9cc6f9 Ploc
	(* | "horn" -> s TODO 16/12 *)
549 29ad4531 ploc
	| "C" | "java" | _ -> join_guards_list s
550
      );
551 58272238 tkahsai
      step_asserts =
552 36454535 ploc
	let exprl = List.map (fun assert_ -> assert_.assert_expr ) nd.node_asserts in
553
	List.map (translate_expr nd init_args) exprl
554
	;
555 0cbf0839 ploc
    };
556
    mspec = nd.node_spec;
557
    mannot = nd.node_annot;
558
  }
559
560 01d48bb0 xthirioux
(** takes the global declarations and the scheduling associated to each node *)
561 db1c5c00 ploc
let translate_prog decls node_schs =
562 58272238 tkahsai
  let nodes = get_nodes decls in
563
  List.map
564
    (fun decl ->
565 70e1006b xthirioux
     let node = node_of_top decl in
566 9aaee7f9 xthirioux
      let sch = (Utils.IMap.find node.node_id node_schs).Scheduling.schedule in
567 58272238 tkahsai
      translate_decl node sch
568 db1c5c00 ploc
    ) nodes
569 f6923c9e ploc
570 58272238 tkahsai
let get_machine_opt name machines =
571
  List.fold_left
572
    (fun res m ->
573
      match res with
574
      | Some _ -> res
575 f6923c9e ploc
      | None -> if m.mname.node_id = name then Some m else None)
576
    None machines
577 58272238 tkahsai
578 01d48bb0 xthirioux
let get_const_assign m id =
579
  try
580
    match (List.find (fun instr -> match instr with MLocalAssign (v, _) -> v == id | _ -> false) m.mconst) with
581
    | MLocalAssign (_, e) -> e
582
    | _                   -> assert false
583
  with Not_found -> assert false
584
585
586
let value_of_ident m id =
587
  (* is is a state var *)
588
  try
589
    StateVar (List.find (fun v -> v.var_id = id) m.mmemory)
590
  with Not_found ->
591
  try (* id is a node var *)
592
    LocalVar (get_node_var id m.mname)
593
  with Not_found ->
594
    try (* id is a constant *)
595
      LocalVar (Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id)))
596
    with Not_found ->
597
      (* id is a tag *)
598
      Cst (Const_tag id)
599
600
let rec value_of_dimension m dim =
601
  match dim.Dimension.dim_desc with
602
  | Dimension.Dbool b         -> Cst (Const_tag (if b then Corelang.tag_true else Corelang.tag_false))
603
  | Dimension.Dint i          -> Cst (Const_int i)
604
  | Dimension.Dident v        -> value_of_ident m v
605
  | Dimension.Dappl (f, args) -> Fun (f, List.map (value_of_dimension m) args)
606
  | Dimension.Dite (i, t, e)  -> Fun ("ite", List.map (value_of_dimension m) [i; t; e])
607
  | Dimension.Dlink dim'      -> value_of_dimension m dim'
608
  | _                         -> assert false
609
610
let rec dimension_of_value value =
611
  match value with
612
  | Cst (Const_tag t) when t = Corelang.tag_true  -> Dimension.mkdim_bool  Location.dummy_loc true
613
  | Cst (Const_tag t) when t = Corelang.tag_false -> Dimension.mkdim_bool  Location.dummy_loc false
614
  | Cst (Const_int i)                             -> Dimension.mkdim_int   Location.dummy_loc i
615
  | LocalVar v                                    -> Dimension.mkdim_ident Location.dummy_loc v.var_id
616
  | Fun (f, args)                                 -> Dimension.mkdim_appl  Location.dummy_loc f (List.map dimension_of_value args)
617
  | _                                             -> assert false
618 0cbf0839 ploc
619
(* Local Variables: *)
620
(* compile-command:"make -C .." *)
621
(* End: *)