Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / Horn / horn_backend_traces.ml @ 0d54d8a8

History | View | Annotate | Download (8.51 KB)

1 fea041c5 xthirioux
(********************************************************************)
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
(* The compilation presented here was first defined in Garoche, Gurfinkel,
13
   Kahsai, HCSV'14.
14
15
   This is a modified version that handle reset
16
*)
17
18
open Format
19 8446bf03 ploc
open Lustre_types
20 fea041c5 xthirioux
open Corelang
21 089f94be ploc
open Machine_code_types
22 fea041c5 xthirioux
23
open Horn_backend_common
24
open Horn_backend_printers
25
26 2d2144c0 ploc
let pp_traces = (Utils.fprintf_list ~sep:", " (fun fmt (v,e) -> Format.fprintf fmt "%s -> %a"
27
                                                         v
28
                                                         Printers.pp_expr e))
29
30 fea041c5 xthirioux
(* Compute memories associated to each machine *)
31
let compute_mems machines m =
32
  let rec aux fst prefix m =
33
    (List.map (fun mem -> (prefix, mem)) m.mmemory) @
34
      List.fold_left (fun accu (id, (n, _)) ->
35
	let name = node_name n in
36
	if name = "_arrow" then accu else
37
	  let machine_n = get_machine machines name in
38
	  ( aux false ((id,machine_n)::prefix) machine_n )
39
	  @ accu
40
      ) [] m.minstances
41
  in
42
  aux true [] m
43
44
45
(* We extract the annotation dealing with traceability *)
46
let machines_traces machines = 
47
  List.map (fun m ->
48
    let traces : (ident * expr) list=
49
      let all_annots = List.flatten (List.map (fun ann -> ann.annots) m.mannot) in
50
      let filtered =
51
	List.filter (fun (kwds, _) -> kwds = ["traceability"]) all_annots
52
      in
53 2d2144c0 ploc
      (* List.iter (Format.eprintf "Annots: %a@." Printers.pp_expr_annot) (m.mannot); *)
54 fea041c5 xthirioux
      let content = List.map snd filtered in
55
      (* Elements are supposed to be a pair (tuple): variable, expression *)
56
      List.map (fun ee ->
57
	match ee.eexpr_quantifiers, ee.eexpr_qfexpr.expr_desc with
58
	| [], Expr_tuple [v;e] -> (
59
	  match v.expr_desc with
60
	  | Expr_ident vid -> vid, e
61
	  | _ -> assert false )
62
	| _ -> assert false)
63
	content
64
    in
65 2d2144c0 ploc
    (* Format.eprintf "Build traces: %a@." pp_traces traces; *)
66 fea041c5 xthirioux
    m, traces
67
68
  ) machines
69
  
70
let memories_old machines m =
71
  List.map (fun (p, v) ->
72
    let machine = match p with | [] -> m | (_,m')::_ -> m' in
73
    let traces = List.assoc machine (machines_traces machines) in
74
    if List.mem_assoc v.var_id traces then 
75
      (
76
	(* We take the expression associated to variable v in the trace
77
	   info *)
78
79 2d2144c0 ploc
	 (* eprintf "Found variable %a in traces: %a@."  Printers.pp_var v
80
	  *   Printers.pp_expr (List.assoc v.var_id traces);  *)
81 fea041c5 xthirioux
	p, List.assoc v.var_id traces
82
      )
83
    else 
84
      begin
85
86
	(* We keep the variable as is: we create an expression v *)
87
88 2d2144c0 ploc
	 (* eprintf "Unable to found variable %a in traces (%a)@."  Printers.pp_var v
89
	  *   pp_traces traces;  *)
90 fea041c5 xthirioux
91
	p, mkexpr Location.dummy_loc (Expr_ident v.var_id)
92
      end
93
94
  ) (compute_mems machines m)
95
      
96
let memories_next  machines m = (* We remove the topest pre in each expression *)
97
  List.map
98
    (fun (prefix, ee) ->
99 2d2144c0 ploc
      let m = match prefix with | [] -> m | (_,m')::_ -> m' in
100 fea041c5 xthirioux
      match ee.expr_desc with
101
      | Expr_pre e -> prefix, e
102 2d2144c0 ploc
      | Expr_ident var_id -> (
103
        (* This memory was not introduced through
104
           normalization. It shall then be a regular x = pre y
105
           expression. Otherwise it would have been rewritten. We
106
           have to get its definition and extract the argument of
107
           the pre *)
108
        
109
        let selected_def =
110
          try  
111
            List.find
112
              (fun def ->
113
                match def with
114
                | Eq eq -> (match eq.eq_lhs with
115 0d54d8a8 ploc
                            | [v] -> v = var_id
116
                            | _ -> assert false
117 2d2144c0 ploc
                           )
118
                | _ -> false)
119
              m.mname.node_stmts
120
          with _ -> (Format.eprintf
121
                       "Unable to find definition of %s in stmts %a@.prefix=%a@.@?"
122
                       var_id
123
                       Printers.pp_node_stmts m.mname.node_stmts
124
                       (Utils.fprintf_list ~sep:","
125
      	                  (fun fmt (id,n) -> fprintf fmt "(%s,%s)" id n.mname.node_id ))
126
      	               (List.rev prefix)
127
      	            
128
                    ;
129
                      assert false)
130
        in
131
        let def = match selected_def with
132
          | Eq eq -> (
133
            match eq.eq_lhs, eq.eq_rhs.expr_desc with
134
            | [single_var], Expr_pre e -> if single_var = var_id then e else assert false
135
            | _ -> assert false
136
          )
137
          | _ -> assert false
138
        in
139
        prefix, def
140
      )
141
                           
142
      | _ ->
143
         eprintf "Mem Failure: (prefix: %a, eexpr: %a)@.@?"
144
      	   (Utils.fprintf_list ~sep:","
145
      	      (fun fmt (id,n) -> fprintf fmt "(%s,%s)" id n.mname.node_id ))
146
      	   (List.rev prefix)
147
      	   Printers.pp_expr ee;
148
      	 assert false
149
    )
150 fea041c5 xthirioux
    (memories_old machines m)
151
      
152
153
154
let pp_prefix_rev fmt prefix =
155
  Utils.fprintf_list ~sep:"." 
156
    (fun fmt (id,n) -> fprintf fmt "(%s,%s)" id n.mname.node_id) 
157
    fmt 
158
    (List.rev prefix)
159
      
160
161
let traces_file fmt basename prog machines =
162
  fprintf fmt "<?xml version=\"1.0\"?>@.";
163
  fprintf fmt "<Traces xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">@.";
164
  fprintf fmt "@[<v 5>@ %a@ @]@."
165
    (Utils.fprintf_list ~sep:"@ " (fun fmt m ->
166
      let pp_var = pp_horn_var m in
167
      let memories_old = memories_old  machines m in
168
      let memories_next = memories_next  machines m in
169
      
170
      (* fprintf fmt "; Node %s@." m.mname.node_id; *)
171
      fprintf fmt "@[<v 2><Node name=\"%s\">@ " m.mname.node_id;
172
      
173
      let input_vars = (rename_machine_list m.mname.node_id m.mstep.step_inputs) in
174
      let output_vars = (rename_machine_list m.mname.node_id m.mstep.step_outputs) in
175
      fprintf fmt "<input name=\"%a\" type=\"%a\">%a</input>@ "
176
	(Utils.fprintf_list ~sep:" | " (pp_horn_var m)) input_vars
177
	(Utils.fprintf_list ~sep:" | "  (fun fmt id -> pp_type fmt id.var_type)) input_vars
178
	(Utils.fprintf_list ~sep:" | " (pp_horn_var m)) (m.mstep.step_inputs);
179
180
      fprintf fmt "<output name=\"%a\" type=\"%a\">%a</output>@ "
181
	(Utils.fprintf_list ~sep:" | " pp_var)  output_vars
182
	(Utils.fprintf_list ~sep:" | "  (fun fmt id -> pp_type fmt id.var_type)) output_vars
183
	(Utils.fprintf_list ~sep:" | " pp_var) (m.mstep.step_outputs);
184
185 d7e04983 Ploc
      let local_vars =
186
	(try
187
	   full_memory_vars ~without_arrow:true machines m
188
	 with Not_found -> Format.eprintf "machine %s@.@?" m.mname.node_id; assert false
189
	)
190 fea041c5 xthirioux
      in
191 d7e04983 Ploc
      let init_local_vars = rename_next_list local_vars in
192
      let step_local_vars = rename_current_list local_vars in
193 fea041c5 xthirioux
194
      fprintf fmt "<localInit name=\"%a\" type=\"%a\">%t%a</localInit>@ "
195
	(Utils.fprintf_list ~sep:" | " pp_var) init_local_vars
196
	(Utils.fprintf_list ~sep:" | "  (fun fmt id -> pp_type fmt id.var_type)) init_local_vars
197
	(fun fmt -> match memories_next with [] -> () | _ -> fprintf fmt "")
198 397d5ae3 ploc
	(Utils.fprintf_list ~sep:" | " (fun fmt (prefix, ee) -> fprintf fmt "%a" pp_xml_expr ee)) memories_next;
199 fea041c5 xthirioux
200
      fprintf fmt "<localStep name=\"%a\" type=\"%a\">%t%a</localStep>@ "
201
	(Utils.fprintf_list ~sep:" | " pp_var) step_local_vars
202
	(Utils.fprintf_list ~sep:" | "  (fun fmt id -> pp_type fmt id.var_type)) step_local_vars
203
	(fun fmt -> match memories_old with [] -> () | _ -> fprintf fmt "")
204
	(Utils.fprintf_list ~sep:" | " (fun fmt (prefix,ee) -> fprintf fmt "(%a)"
205 397d5ae3 ploc
          pp_xml_expr ee)) (memories_old);
206 fea041c5 xthirioux
207 d7e04983 Ploc
      let arrow_vars = arrow_vars machines m in
208 92ff8f1b Teme
      let arrow_vars_curr = rename_current_list arrow_vars and
209
          arrow_vars_mid = rename_mid_list arrow_vars and
210 d7e04983 Ploc
	  arrow_vars_next = rename_next_list arrow_vars
211
      in
212
      Utils.fprintf_list ~sep:"@ "
213
      	(fun fmt v -> fprintf fmt "<reset name=\"%a\"/>" pp_var v)
214 92ff8f1b Teme
      	fmt (arrow_vars_curr@arrow_vars_mid@arrow_vars_next);
215 fea041c5 xthirioux
      fprintf fmt "@]@ </Node>";
216
     )) (List.rev machines);
217
  fprintf fmt "</Traces>@."
218
  
219
220
(* (Utils.fprintf_list ~sep:" | " (fun fmt (prefix, ee) -> fprintf fmt
221
   "%a%a" pp_prefix_rev prefix Printers.pp_expr ee)) memories_next; *)
222
(* (Utils.fprintf_list ~sep:" | " (fun fmt (prefix,ee) -> fprintf fmt
223
   "%a(%a)" *)
224
(* pp_prefix_rev prefix Printers.pp_expr ee)) (memories_old); *)
225
226
227
(* Local Variables: *)
228
(* compile-command:"make -C ../.." *)
229
(* End: *)