Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / automata.ml @ 77a01213

History | View | Annotate | Download (7.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 Utils
13
open LustreSpec
14
open Corelang
15

    
16
let pp_restart fmt restart =
17
  Format.fprintf fmt "%s" (if restart then "restart" else "resume")
18

    
19
let pp_unless fmt (_, expr, restart, st) =
20
  Format.fprintf fmt "unless %a %a %s"
21
    Printers.pp_expr expr
22
    pp_restart restart
23
    st
24

    
25
let pp_until fmt (_, expr, restart, st) =
26
  Format.fprintf fmt "until %a %a %s"
27
    Printers.pp_expr expr
28
    pp_restart restart
29
    st
30

    
31
let pp_handler fmt handler =
32
  Format.fprintf fmt "state %s -> %a %a let %a tel %a"
33
    handler.hand_state
34
    (Utils.fprintf_list ~sep:"@ " pp_unless) handler.hand_unless
35
    (fun fmt locals ->
36
      match locals with [] -> () | _ ->
37
	Format.fprintf fmt "@[<v 4>var %a@]@ " 
38
	  (Utils.fprintf_list ~sep:"@ " 
39
	     (fun fmt v -> Format.fprintf fmt "%a;" Printers.pp_node_var v))
40
	  locals)
41
    handler.hand_locals
42
    Printers.pp_node_eqs handler.hand_eqs
43
    (Utils.fprintf_list ~sep:"@ " pp_until) handler.hand_until
44

    
45
let pp_automata fmt aut =
46
  Format.fprintf fmt "automaton %s %a"
47
    aut.aut_id
48
    (Utils.fprintf_list ~sep:"@ " pp_handler) aut.aut_handlers
49

    
50
let mkbool loc b =
51
 mkexpr loc (Expr_const (const_of_bool b))
52

    
53
let mkident loc id =
54
 mkexpr loc (Expr_ident id)
55

    
56
let mkfby loc e1 e2 =
57
 mkexpr loc (Expr_arrow (e1, mkexpr loc (Expr_pre e2)))
58

    
59
let init loc restart state =
60
 mkexpr loc (Expr_tuple [mkident loc restart; mkident loc state])
61

    
62
let add_branch (loc, expr, restart, st) cont =
63
 mkexpr loc (Expr_ite (expr, mkexpr loc (Expr_tuple [mkbool loc restart; mkident loc st]), cont))
64

    
65
let mkhandler loc st unless until locals (eqs, asserts, annots) =
66
 {hand_state = st;
67
  hand_unless = unless;
68
  hand_until = until;
69
  hand_locals = locals;
70
  hand_eqs = eqs;
71
  hand_asserts = asserts;
72
  hand_annots = annots;
73
  hand_loc = loc}
74

    
75
let mkautomata loc id handlers =
76
  {aut_id = id;
77
   aut_handlers = handlers;
78
   aut_loc = loc}
79

    
80
let handler_read handler =
81
 List.fold_left (fun read eq -> get_expr_vars read eq.eq_rhs) ISet.empty handler.hand_eqs
82

    
83
let handler_write handler =
84
 List.fold_left (fun write eq -> List.fold_left (fun write v -> ISet.add v write) write eq.eq_lhs) ISet.empty handler.hand_eqs
85

    
86
let node_of_handler node aut_id handler =
87
  let locals = List.fold_left (fun locals v -> ISet.add v.var_id locals) ISet.empty handler.hand_locals in 
88
  let inputs = handler_read handler in
89
  let outputs = handler_write handler in
90
  {
91
    node_id = Format.sprintf "%s_%s_handler" aut_id handler.hand_state;
92
    node_type = Types.new_var ();
93
    node_clock = Clocks.new_var true;
94
    node_inputs = List.map (fun v -> get_node_var v node) (ISet.elements (ISet.diff inputs locals));
95
    node_outputs = List.map (fun v -> get_node_var v node) (ISet.elements (ISet.diff outputs locals));
96
    node_locals = handler.hand_locals;
97
    node_gencalls = [];
98
    node_checks = [];
99
    node_asserts = handler.hand_asserts; 
100
    node_eqs = handler.hand_eqs;
101
    node_dec_stateless = false;
102
    node_stateless = None;
103
    node_spec = None;
104
    node_annot = handler.hand_annots
105
  }
106

    
107
let expr_of_exit loc restart state conds tag =
108
  mkexpr loc (Expr_when (List.fold_right add_branch conds (init loc restart state), state, tag))
109

    
110
let expr_of_handler loc restart state node tag =
111
  let arg = mkexpr loc (Expr_tuple (List.map (fun v -> mkident loc v.var_id) node.node_inputs)) in
112
  mkexpr loc (Expr_when (mkexpr loc (Expr_appl (node.node_id, arg, Some (restart, tag_true))), state, tag))
113

    
114
let assign_aut_handlers loc node actual_r actual_s hnodes =
115
  let outputs = (snd (List.hd hnodes)).node_outputs in
116
  let assign_handlers = List.map (fun (hs, n) -> (hs, expr_of_handler loc actual_r actual_s n hs)) hnodes in
117
  let assign_expr = mkexpr loc (Expr_merge (actual_s, assign_handlers)) in
118
  let assign_eq = mkeq loc (List.map (fun v -> v.var_id) outputs, assign_expr) in
119
  assign_eq
120

    
121
let typedef_of_automata node aut =
122
  let tname = Format.sprintf "%s_type" aut.aut_id in
123
  { tydef_id = tname;
124
    tydef_desc = Tydec_enum (List.map (fun h -> h.hand_state) aut.aut_handlers)
125
  }
126

    
127
let expand_automata top_node aut =
128
  let node = node_of_top top_node in
129
  let owner = top_node.top_decl_owner in
130
  let typedef = typedef_of_automata node aut in
131
  let initial = (List.hd aut.aut_handlers).hand_state in
132
  let incoming_r = mk_new_name (get_node_vars node) (aut.aut_id ^ "__restart_in") in
133
  let incoming_s = mk_new_name (get_node_vars node) (aut.aut_id ^ "__state_in") in
134
  let actual_r = mk_new_name (get_node_vars node) (aut.aut_id ^ "__restart_act") in
135
  let actual_s = mk_new_name (get_node_vars node) (aut.aut_id ^ "__state_act") in
136
  let unless_handlers = List.map (fun h -> (h.hand_state, expr_of_exit h.hand_loc incoming_r incoming_s h.hand_unless h.hand_state)) aut.aut_handlers in
137
  let unless_expr = mkexpr aut.aut_loc (Expr_merge (incoming_s, unless_handlers)) in
138
  let unless_eq = mkeq aut.aut_loc ([actual_r; actual_s], unless_expr) in
139
  let until_handlers = List.map (fun h -> (h.hand_state, expr_of_exit h.hand_loc actual_r actual_s h.hand_until h.hand_state)) aut.aut_handlers in
140
  let until_expr = mkexpr aut.aut_loc (Expr_merge (actual_s, until_handlers)) in
141
  let fby_until_expr = mkfby aut.aut_loc (init aut.aut_loc tag_false initial) until_expr in
142
  let until_eq = mkeq aut.aut_loc ([incoming_r; incoming_s], fby_until_expr) in
143
  let hnodes = List.map (fun h -> (h.hand_state, node_of_handler node aut.aut_id h)) aut.aut_handlers in
144
  let assign_eq = assign_aut_handlers aut.aut_loc node actual_r actual_s hnodes in
145
  let tydec_bool = { ty_dec_desc = Tydec_bool; ty_dec_loc = aut.aut_loc } in
146
  let tydec_const id = { ty_dec_desc = Tydec_const id; ty_dec_loc = aut.aut_loc } in
147
  let ckdec_any = { ck_dec_desc = Ckdec_any; ck_dec_loc = aut.aut_loc } in
148
  let locals' = [mkvar_decl aut.aut_loc (incoming_r, tydec_bool, ckdec_any, false);
149
                 mkvar_decl aut.aut_loc (actual_r  , tydec_bool, ckdec_any, false);
150
                 mkvar_decl aut.aut_loc (incoming_s, tydec_const typedef.tydef_id, ckdec_any, false);
151
                 mkvar_decl aut.aut_loc (actual_s  , tydec_const typedef.tydef_id, ckdec_any, false)] in
152
  let eqs' = [unless_eq; assign_eq; until_eq] in
153
  let node' = { node with node_locals = locals'@node.node_locals; node_eqs = eqs'@node.node_eqs } in
154
  (mktop_decl aut.aut_loc owner false (TypeDef typedef)) ::
155
  { top_node with top_decl_desc = Node node' } ::
156
  (List.map2 (fun h (hs, n) -> mktop_decl h.hand_loc owner false (Node n)) aut.aut_handlers hnodes)
157

    
158
let node_extract_automata top_decl =
159
  match top_decl.top_decl_desc with
160
  | Node nd -> [top_decl]
161
  | _ -> [top_decl]
162
(*
163
let extract_automata top_decls =
164
 List.fold_left (fun top_decls top_decl -> ) top_decls
165
*)
166
(* Local Variables: *)
167
(* compile-command:"make -C .." *)
168
(* End: *)