Project

General

Profile

Revision 53206908 src/machine_code.ml

View differences:

src/machine_code.ml
19 19
module OrdVarDecl:Map.OrderedType with type t=var_decl =
20 20
  struct type t = var_decl;; let compare = compare end
21 21

  
22
module ISet = Set.Make(OrdVarDecl)
23 22

  
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
23
module ISet = Set.Make(OrdVarDecl)
39 24

  
25
 
40 26
let rec pp_val fmt v =
41
  match v with
42
    | Cst c         -> Printers.pp_const fmt c
27
  match v.value_desc with
28
    | Cst c         -> Printers.pp_const fmt c 
43 29
    | LocalVar v    -> Format.pp_print_string fmt v.var_id
44 30
    | StateVar v    -> Format.pp_print_string fmt v.var_id
45 31
    | Array vl      -> Format.fprintf fmt "[%a]" (Utils.fprintf_list ~sep:", " pp_val)  vl
......
48 34
    | Fun (n, vl)   -> Format.fprintf fmt "%s (%a)" n (Utils.fprintf_list ~sep:", " pp_val)  vl
49 35

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

  
65 52
and pp_branch fmt (t, h) =
66 53
  Format.fprintf fmt "@[<v 2>%s:@,%a@]" t (Utils.fprintf_list ~sep:"@," pp_instr) h
......
92 79
  mannot: expr_annot list;
93 80
}
94 81

  
82
let machine_vars m = m.mstep.step_inputs @ m.mstep.step_locals @ m.mstep.step_outputs @ m.mmemory
95 83
let pp_step fmt s =
96 84
  Format.fprintf fmt "@[<v>inputs : %a@ outputs: %a@ locals : %a@ checks : %a@ instrs : @[%a@]@ asserts : @[%a@]@]@ "
97 85
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_inputs
......
119 107
    (fun fmt -> match m.mspec with | None -> () | Some spec -> Printers.pp_spec fmt spec)
120 108
    (Utils.fprintf_list ~sep:"@ " Printers.pp_expr_annot) m.mannot
121 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

  
122 116
(* Returns the declared stateless status and the computed one. *)
123 117
let get_stateless_status m =
124 118
 (m.mname.node_dec_stateless, Utils.desome m.mname.node_stateless)
......
177 171
    top_decl_loc = Location.dummy_loc
178 172
  }
179 173

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

  
180 178
let arrow_machine =
181 179
  let state = "_first" in
182 180
  let var_state = dummy_var_decl state (Types.new_ty Types.Tbool) in
183 181
  let var_input1 = List.nth arrow_desc.node_inputs 0 in
184 182
  let var_input2 = List.nth arrow_desc.node_inputs 1 in
185 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 186
  {
187 187
    mname = arrow_desc;
188 188
    mmemory = [var_state];
189 189
    mcalls = [];
190 190
    minstances = [];
191
    minit = [MStateAssign(var_state, Cst (const_of_bool true))];
191
    minit = [MStateAssign(var_state, cst true)];
192 192
    mconst = [];
193 193
    mstatic = [];
194 194
    mstep = {
......
196 196
      step_outputs = arrow_desc.node_outputs;
197 197
      step_locals = [];
198 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)] ];
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 203
      step_asserts = [];
204 204
    };
205 205
    mspec = None;
......
222 222
      o
223 223
    end
224 224

  
225

  
226 225
(* translate_<foo> : node -> context -> <foo> -> machine code/expression *)
227 226
(* the context contains  m : state aka memory variables  *)
228 227
(*                      si : initialization instructions *)
......
233 232
  try (* id is a node var *)
234 233
    let var_id = get_node_var id node in
235 234
    if ISet.exists (fun v -> v.var_id = id) m
236
    then StateVar var_id
237
    else LocalVar var_id
235
    then mk_val (StateVar var_id) var_id.var_type
236
    else mk_val (LocalVar var_id) var_id.var_type
238 237
  with Not_found ->
239 238
    try (* id is a constant *)
240
      LocalVar (Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id)))
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 241
    with Not_found ->
242 242
      (* id is a tag *)
243
      Cst (Const_tag id)
244

  
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
	
245 248
let rec control_on_clock node ((m, si, j, d, s) as args) ck inst =
246 249
 match (Clocks.repr ck).cdesc with
247 250
 | Con    (ck1, cr, l) ->
......
272 275
 List.fold_right join_guards insts []
273 276

  
274 277
(* specialize predefined (polymorphic) operators
275
   wrt their instances, so that the C semantics
278
   wrt their instances, so that the C semantics 
276 279
   is preserved *)
277 280
let specialize_to_c expr =
278 281
 match expr.expr_desc with
......
292 295
  | "C" -> specialize_to_c expr
293 296
  | _   -> expr
294 297

  
295
let rec translate_expr ?(ite=false) node ((m, si, j, d, s) as args) expr =
298
let rec translate_expr node ((m, si, j, d, s) as args) expr =
296 299
  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
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
325 328

  
326 329
let translate_guard node args expr =
327 330
  match expr.expr_desc with
......
334 337
			    conditional g [translate_act node args (y, t)]
335 338
                              [translate_act node args (y, e)]
336 339
  | 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)
340
  | _                    -> MLocalAssign (y, translate_expr node args expr)
339 341

  
340 342
let reset_instance node args i r c =
341 343
  match r with
......
344 346
                   [control_on_clock node args c (conditional g [MReset i] [])]
345 347

  
346 348
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; *)
349
  (*Format.eprintf "translate_eq %a with clock %a@." Printers.pp_node_eq eq Clocks.print_ck eq.eq_rhs.expr_clock;*)
348 350
  match eq.eq_lhs, eq.eq_rhs.expr_desc with
349 351
  | [x], Expr_arrow (e1, e2)                     ->
350 352
    let var_x = get_node_var x node in
......
371 373
     d,
372 374
     control_on_clock node args eq.eq_rhs.expr_clock (MStateAssign (var_x, translate_expr node args e2)) :: s)
373 375

  
374
  | p  , Expr_appl (f, arg, r) when not (Basic_library.is_internal_fun f) ->
376
  | p  , Expr_appl (f, arg, r) when not (Basic_library.is_expr_internal_fun eq.eq_rhs) ->
375 377
    let var_p = List.map (fun v -> get_node_var v node) p in
376 378
    let el = expr_list_of_expr arg in
377 379
    let vl = List.map (translate_expr node args) el in
378 380
    let node_f = node_from_name f in
379 381
    let call_f =
380 382
      node_f,
381
      NodeDep.filter_static_inputs (node_inputs node_f) el in
383
      NodeDep.filter_static_inputs (node_inputs node_f) el in 
382 384
    let o = new_instance node node_f eq.eq_rhs.expr_tag in
383 385
    let env_cks = List.fold_right (fun arg cks -> arg.expr_clock :: cks) el [eq.eq_rhs.expr_clock] in
384 386
    let call_ck = Clock_calculus.compute_root_clock (Clock_predef.ck_tuple env_cks) in
......
397 399
   (* special treatment depending on the active backend. For horn backend, x = ite (g,t,e)
398 400
      are preserved. While they are replaced as if g then x = t else x = e in  C or Java
399 401
      backends. *)
400
  | [x], Expr_ite   (c, t, e)
402
  | [x], Expr_ite   (c, t, e) 
401 403
    when (match !Options.output with | "horn" -> true | "C" | "java" | _ -> false)
402
      ->
404
      -> 
403 405
    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
406
    (m, 
407
     si, 
408
     j, 
409
     d, 
410
     (control_on_clock node args eq.eq_rhs.expr_clock 
409 411
	(MLocalAssign (var_x, translate_expr node args eq.eq_rhs))::s)
410 412
    )
411

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

  
......
436 438
	    assert false
437 439
	  end
438 440
	| hd::tl ->
439
	  if List.exists (fun x -> List.mem x hd.eq_lhs) xl then hd, accu@tl else aux (hd::accu) 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
440 445
    in
441 446
    aux [] eqs
442 447

  
443
(* Sort the set of equations of node [nd] according
448
(* Sort the set of equations of node [nd] according 
444 449
   to the computed schedule [sch]
445 450
*)
446 451
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;*)
452
  (* Format.eprintf "%s schedule: %a@." *)
453
  (* 		 nd.node_id *)
454
  (* 		 (Utils.fprintf_list ~sep:" ; " Scheduling.pp_eq_schedule) sch; *)
450 455
  let split_eqs = Splitting.tuple_split_eq_list (get_node_eqs nd) in
451 456
  let eqs_rev, remainder =
452 457
    List.fold_left
453 458
      (fun (accu, node_eqs_remainder) vl ->
454
       if List.exists (fun eq -> List.exists (fun v -> List.mem v eq.eq_lhs) vl) accu
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
455 465
       then
456 466
	 (accu, node_eqs_remainder)
457 467
       else
......
467 477
		     Printers.pp_node_eqs remainder
468 478
      		     Printers.pp_node_eqs (get_node_eqs nd);
469 479
      assert false);
470
    List.rev eqs_rev
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
471 484
  end
472 485

  
473 486
let constant_equations nd =
......
519 532
	| "horn" -> s
520 533
	| "C" | "java" | _ -> join_guards_list s
521 534
      );
522
      step_asserts =
535
      step_asserts = 
523 536
	let exprl = List.map (fun assert_ -> assert_.assert_expr ) nd.node_asserts in
524 537
	List.map (translate_expr nd init_args) exprl
525 538
	;
......
530 543

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

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

  
......
557 570
let value_of_ident m id =
558 571
  (* is is a state var *)
559 572
  try
560
    StateVar (List.find (fun v -> v.var_id = id) m.mmemory)
573
    let mem = List.find (fun v -> v.var_id = id) m.mmemory in
574
    mk_val (StateVar mem) mem.var_type
561 575
  with Not_found ->
562 576
  try (* id is a node var *)
563
    LocalVar (get_node_var id m.mname)
577
    let var = get_node_var id m.mname in
578
    mk_val (LocalVar var) var.var_type
564 579
  with Not_found ->
565 580
    try (* id is a constant *)
566
      LocalVar (Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id)))
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
567 583
    with Not_found ->
568 584
      (* id is a tag *)
569
      Cst (Const_tag id)
585
      let tag = Const_tag id in
586
      mk_val (Cst tag) (Typing.type_const Location.dummy_loc tag)
570 587

  
571 588
let rec value_of_dimension m dim =
572 589
  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)
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
575 592
  | 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])
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
578 597
  | Dimension.Dlink dim'      -> value_of_dimension m dim'
579 598
  | _                         -> assert false
580 599

  
581 600
let rec dimension_of_value value =
582
  match value with
601
  match value.value_desc with
583 602
  | Cst (Const_tag t) when t = Corelang.tag_true  -> Dimension.mkdim_bool  Location.dummy_loc true
584 603
  | Cst (Const_tag t) when t = Corelang.tag_false -> Dimension.mkdim_bool  Location.dummy_loc false
585 604
  | Cst (Const_int i)                             -> Dimension.mkdim_int   Location.dummy_loc i

Also available in: Unified diff