Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / plugins / scopes / scopes.ml @ dc732cf2

History | View | Annotate | Download (14.9 KB)

1
open Lustre_types 
2
open Corelang 
3
open Machine_code_types
4
open Machine_code_common
5

    
6
(* (variable, node name, node instance) *)
7
type scope_t = (var_decl * string * string option) list * var_decl
8

    
9
(* Scope to string list *)
10
let scope_to_sl ((sl, v) : scope_t) : string list=
11
  List.fold_right (
12
    fun (v, nodename, _) accu -> 
13
      v.var_id :: nodename :: accu
14
  ) sl [v.var_id]
15

    
16

    
17
let rec compute_scopes ?(first=true) prog root_node : scope_t list =
18
  let compute_scopes = compute_scopes ~first:false in
19
  (* Format.eprintf "Compute scope of %s@." main_node; *)
20
  try
21
    let node =  get_node root_node prog in    
22
    let all_vars = node.node_inputs @ node.node_locals @  node.node_outputs in
23
    let local_vars = if first then
24
                       node.node_locals
25
                     else
26
                       node.node_inputs @ node.node_locals in
27
    let local_scopes = List.map (fun x -> [], x) local_vars  in
28
    let sub_scopes =
29
      let sub_nodes =
30
	List.fold_left 
31
	  (fun res s -> 
32
	    match s with 
33
	    | Eq ({ eq_rhs ={ expr_desc = Expr_appl (nodeid, _, _); _}; _ } as eq) -> 
34
	      (* Obtaining the var_del associated to the first var of eq_lhs *)
35
	      (
36
		try
37
		  let query v = v.var_id = List.hd eq.eq_lhs in
38
		  let vid = List.find query all_vars in
39
		  (nodeid, vid)::res
40
		with Not_found -> Format.eprintf "eq=%a@.local_vars=%a@." Printers.pp_node_eq eq (Utils.fprintf_list ~sep:"," Printers.pp_var) local_vars; assert false 
41
	      )
42
	    | Eq _ -> res
43
	    | _ -> assert false (* TODO deal with Automaton *)
44
	  ) [] node.node_stmts
45
      in
46
      List.map (fun (nodeid, vid) ->
47
	let scopes = compute_scopes prog nodeid in
48
	List.map (fun (sl,v) -> (vid, nodeid, None)::sl, v) scopes (* instances are not yet known, hence the None *)
49
      ) sub_nodes
50
    in
51
    local_scopes @ (List.flatten sub_scopes) 
52
  with Not_found ->  []
53

    
54

    
55
let print_scopes =
56
  Utils.fprintf_list ~sep:"@ " 
57
    (fun fmt ((_, v) as s) -> Format.fprintf fmt "%a: %a" 
58
      (Utils.fprintf_list ~sep:"." Format.pp_print_string )(scope_to_sl s)
59
      Types.print_ty v.var_type)
60
    
61
     
62
    
63

    
64
(* let print_path fmt p =  *)
65
(*   Utils.fprintf_list ~sep:"." (fun fmt (id, _) -> Format.pp_print_string fmt id) fmt p *)
66

    
67
let get_node_vdecl_of_name name node =
68
  try
69
    List.find 
70
      (fun v -> v.var_id = name) 
71
      (node.node_inputs  @ node.node_outputs  @ node.node_locals ) 
72
  with Not_found -> 
73
    Format.eprintf "Cannot find variable %s in node %s@." name node.node_id;
74
    assert false
75

    
76
let rec get_path prog machines node id_list accu =
77
  let get_path = get_path prog machines in
78
  match id_list, accu with
79
  | [flow], [] ->  (* Special treatment of first level flow: node is here main_node *)
80
     let flow_var = get_node_vdecl_of_name flow node in
81
     [], flow_var, node.node_id
82
  | [id], (_, last_node, _)::_ -> (* last item, it should denote a local
83
				       memory variable (local var, memory or input *)
84
     let id_vdecl = 
85
       get_node_vdecl_of_name id (get_node last_node prog) 
86
     in
87
     List.rev accu, id_vdecl, last_node
88
  | varid::nodename::id_list_tl, _ -> (
89
    let e_machine = get_machine machines node.node_id in 
90
    (* Format.eprintf "Looking for def %s in call %s in machine %a@."  *)
91
    (* 	varid nodename *)
92
    (* 	Machine_code.pp_machine e_machine; *)
93
    let find_var = (fun v -> v.var_id = varid) in
94
    let instance = 
95
      List.find 
96
	(fun i -> match get_instr_desc i with 
97
	          | MStep(p, o, _) -> List.exists find_var p 
98
	          | _ -> false
99
	) 
100
	e_machine.mstep.step_instrs 
101
    in
102
    try
103
      let variable, instance_node, instance_id = 
104
	match get_instr_desc instance with 
105
	| MStep(p, o, _) -> 
106
	   (* Format.eprintf "Looking for machine %s@.@?" o; *)
107
	   let o_fun, _ = List.assoc o e_machine.mcalls in
108
	   if node_name o_fun = nodename then
109
	     List.hd p, o_fun, o 
110
	   else 
111
	     assert false
112
	| _ -> assert false
113
      in
114
      let next_node = node_of_top instance_node in
115
      let accu = (variable, nodename, Some instance_id)::accu in
116
      (* Format.eprintf "Calling get path on %s@.@?" next_node.node_id; *)
117
      get_path next_node id_list_tl accu
118
    with Not_found -> Format.eprintf "toto@."; assert false
119
  )
120
  | _ -> assert false
121

    
122
    
123
let check_scope all_scopes  =
124
  let all_scopes_as_sl = List.map scope_to_sl all_scopes in
125
  fun prog machines main_node_name sl ->
126
  if not (List.mem sl all_scopes_as_sl) then (
127
    Format.eprintf "%s is an invalid scope.@." (String.concat "." sl);
128
    exit 1
129
  )
130
  else (
131
    (* Format.eprintf "@.@.Required path: %s@." (String.concat "." sl) ;  *)
132
    let main_node = get_node main_node_name prog in
133
    let path, flow, mid = get_path prog machines main_node sl [] in
134
    (* Format.eprintf "computed path: %a.%s@." print_path path flow.var_id; *)
135
    path, flow, mid
136
  )
137

    
138

    
139
                                                                
140
(* Build the two maps 
141
   - (scope_name, variable)
142
   - (machine_name, list of selected variables)
143
 *)
144
let check_scopes main_node_name prog machines all_scopes scopes =
145
  let check_scope = check_scope all_scopes prog machines in
146
  List.fold_left
147
    (fun (accu_sl, accu_m) sl ->
148
      let path, flow, mid = check_scope main_node_name sl in
149
      let accu_sl = (sl, (path, flow))::accu_sl in
150
      let accu_m =
151
        let flow_id = flow.var_id in
152
        if List.mem_assoc mid accu_m then
153
          (mid, flow_id::(List.assoc mid accu_m)) :: 
154
            (List.remove_assoc mid accu_m)
155
        else
156
          (mid, [flow_id])::accu_m
157
      in
158
      accu_sl, accu_m
159
    ) ([], []) scopes
160
  
161
  
162

    
163
let scope_var_name vid =  vid ^ "__scope"
164

    
165
(**********************************************************************)
166
(* The following three functions are used in the main function to print
167
   the value of the new memories, storing scopes values               *)
168
(**********************************************************************)
169

    
170
(* TODO: recuperer le type de "flow" et appeler le print correspondant 
171
   iterer sur path pour construire la suite des xx_mem._reg.yy_mem._reg......flow
172
par ex main_mem->n8->n9->_reg.flow
173
*)
174
let extract_scopes_defs scopes =
175
  let rec scope_path_name (path, flow) accu = 
176
    match path with 
177
    | [] -> accu ^ "_reg." ^ (scope_var_name flow.var_id), flow
178
    | (_, _, Some instance_id)::tl -> scope_path_name (tl, flow) ( accu ^ instance_id ^ "->" ) 
179
    | _ -> assert false
180
  in
181
  let scopes_vars = 
182
    List.map 
183
      (fun (sl, scope) -> 
184
	String.concat "." sl, scope_path_name scope "main_mem.") 
185
      scopes 
186
  in
187
  scopes_vars
188
  
189
let pp_scopes_files basename mname fmt scopes =
190
  let scopes_vars = extract_scopes_defs scopes in
191
  List.iteri (fun idx  _(*(id, (var_path, var))*)  ->
192
      C_backend_common.pp_file_decl fmt "out_scopes" idx)
193
    scopes_vars;
194
  Format.fprintf fmt "@[<v 2>if (traces) {@ ";
195
  List.iteri (fun idx  (id, (var_path, var))  ->
196
      let file = C_backend_common.pp_file_open fmt "out_scopes" idx in
197
      Format.fprintf fmt
198
        "fprintf(%s, \"# scope: %s\\n\");@ "
199
        file id;
200
      Format.fprintf fmt
201
        "fprintf(%s, \"# node: %s\\n\");@ "
202
        file (Utils.desome var.var_parent_nodeid);
203
      Format.fprintf fmt
204
        "fprintf(%s, \"# variable: %s\\n\");@ "
205
        file var.var_id
206
    ) scopes_vars;
207
  Format.fprintf fmt "@]}@ "
208
    
209
  
210
let pp_scopes fmt scopes = 
211
  let scopes_vars = extract_scopes_defs scopes in
212
  List.iteri (fun idx (id, (var_path, var)) ->
213
    Format.fprintf fmt "@ %t;" 
214
      (fun fmt -> C_backend_common.print_put_var fmt
215
                    ("_scopes" ^ string_of_int (idx+1))
216
                    id (*var*) var.var_type var_path)
217
  ) scopes_vars
218

    
219
(**********************************************************************)
220
                        
221
let update_machine main_node machine scopes =
222
  let stateassign (vdecl_mem, vdecl_orig) =
223
    mkinstr 
224
    (MStateAssign (vdecl_mem, mk_val (Var vdecl_orig) vdecl_orig.var_type))
225
  in
226
  let selection =
227
    (* We only register inputs for non root node *)
228
    (if machine.mname.node_id = main_node then
229
      []
230
    else
231
      machine.mstep.step_inputs
232
    )
233
    (* @ machine.mstep.step_outputs   *)
234
    @ machine.mmemory 
235
    @ machine.mstep.step_locals
236
  in
237
  let selection = List.filter (fun v -> List.exists (fun vid -> vid = v.var_id) scopes) selection in
238
  let new_mems = List.map (fun v ->
239
                     (* We could copy the variable but then we need to update its type 
240
                        let new_v = copy_var_decl v in
241
                      *)
242
                     let new_v = { v with var_id = scope_var_name v.var_id }  in
243
                     new_v, v
244
                   ) selection
245
  in
246
  { machine with
247
    mmemory = machine.mmemory @ (List.map fst new_mems);
248
    mstep = { 
249
      machine.mstep with 
250
        step_instrs = machine.mstep.step_instrs
251
        @ (mkinstr (MComment "Registering all flows"))::(List.map stateassign new_mems)
252
          
253
    }
254
  }
255
    
256

    
257
let rec is_valid_path path nodename prog machines =
258
  let nodescopes = compute_scopes prog nodename in
259
  let m = get_machine machines nodename in
260
  match path with
261
  | [] -> assert false
262
  | [vid] -> let res = List.exists (fun v -> v.var_id = vid) (m.mmemory @ m.mstep.step_inputs @ m.mstep.step_locals) in
263
	     (* if not res then  *)
264
	     (* 	 Format.eprintf "Variable %s cannot be found in machine %s@.Local vars are %a@." vid m.mname.node_id *)
265
	     (* 	   (Utils.fprintf_list ~sep:", " Printers.pp_var) (m.mmemory @ m.mstep.step_inputs @ m.mstep.step_locals) *)
266
	     (* ; *)
267
	     res
268
	     
269
  | inst::nodename::path' -> (* We use the scopes computed on the prog artifact *)
270
     (* Format.eprintf "Path is %a@ Local scopes: @[<v>%a@ @]@."  *)
271
     (* 	(Utils.fprintf_list ~sep:"." Format.pp_print_string) path *)
272
     (* 	(Utils.fprintf_list ~sep:";@ " *)
273
     (* 	   (fun fmt scope ->  *)
274
     (* 	     Utils.fprintf_list ~sep:"." Format.pp_print_string fmt (scope_to_sl scope)) *)
275
     (* 	)  *)
276
     (* 	nodescopes; *)
277
     if List.mem path (List.map scope_to_sl nodescopes) then (
278
       (* Format.eprintf "Valid local path, checking underneath@."; *)
279
       is_valid_path path' nodename prog machines
280
     )
281
     else
282
       false
283

    
284
      (* let instok = List.exists (fun (inst', node) -> inst' = inst) m.minstances in *)
285
      (* if not instok then Format.eprintf "inst = %s@." inst; *)
286
      (* instok &&  *)
287
      (* let instnode = fst (snd (List.find (fun (inst', node) -> inst' = inst) m.minstances)) in *)
288
      (* is_valid_path path' (Corelang.node_of_top instnode).node_id prog machines *)
289

    
290

    
291

    
292
(****************************************************)
293
      
294
let scopes_def : string list list ref = ref []
295
let inputs = ref []
296

    
297
let option_show_scopes = ref false
298
let option_scopes = ref false
299
let option_all_scopes = ref false
300
(* let option_mems_scopes = ref false 
301
 * let option_input_scopes = ref false *)
302

    
303
let scopes_map : (Lustre_types.ident list  * scope_t) list ref  = ref []
304
      
305
let process_scopes main_node prog machines =
306
  let all_scopes = compute_scopes prog !Options.main_node in
307
  let selected_scopes = if !option_all_scopes then
308
	                  List.map (fun s -> scope_to_sl s) all_scopes
309
                        else
310
	                  !scopes_def
311
  in
312
  (* Making sure all scopes are defined and were not removed by various
313
       optmizationq *)
314
  let selected_scopes = 
315
    List.filter 
316
      (fun sl -> 
317
	let res = is_valid_path sl main_node prog machines in
318
	if not res then
319
	  Format.eprintf "Scope %a is cancelled due to variable removal@." (Utils.fprintf_list ~sep:"." Format.pp_print_string) sl; 
320
	res
321
      ) 
322
      selected_scopes 
323
  in
324
  let scopes_map', machines_scopes = check_scopes main_node prog machines all_scopes selected_scopes in
325
  scopes_map := scopes_map';
326
  (* Each machine is updated with fresh memories and declared as stateful  *)
327
  let machines = List.map (fun m ->
328
                     let mid = m.mname.node_id in
329
                     if List.mem_assoc mid machines_scopes then
330
                       let machine_scopes = List.assoc mid machines_scopes in
331
                       update_machine main_node m machine_scopes
332
                     else
333
                       m) machines in
334
  machines
335

    
336
let activate () = 
337
  option_scopes := true;
338
  Options.optimization := 0; (* no optimization *)
339
  ()
340
  
341
let register_scopes s = 
342
  activate ();
343
  option_all_scopes:=false; 
344
  let scope_list = Str.split (Str.regexp ", *") s in
345
  let scope_list = List.map (fun scope -> Str.split (Str.regexp "\\.") scope) scope_list in
346
  scopes_def := List.rev scope_list
347

    
348
let register_inputs s = 
349
  activate ();
350
  let input_list = Str.split (Str.regexp "[;]") s in
351
  let input_list = List.map (fun s -> match Str.split (Str.regexp "=") s with | [v;e] -> v, e | _ -> raise (Invalid_argument ("Input list error: " ^ s))) input_list in
352
  let input_list = List.map (fun (v, e) -> v, Str.split (Str.regexp "[;]") e) input_list in
353
  inputs := input_list
354

    
355
let register_all_scopes () =
356
  activate ();
357
  option_all_scopes:= true
358
  
359
module Plugin : (
360
  sig
361
    include PluginType.S
362
    val show_scopes: unit -> bool
363
    end) =
364
  struct
365
    include PluginType.Default
366
    let name = "scopes"
367
    let is_active () = 
368
      !option_scopes || !option_show_scopes || !option_all_scopes
369
    (* || !option_mem_scopes || !option_input_scopes *)
370
      
371
    let show_scopes () = 
372
      !option_show_scopes && (
373
        Compiler_common.check_main ();
374
        true)
375

    
376
    let usage fmt =
377
      let open Format in
378
      fprintf fmt "@[<hov 0>Scopes@ enrich@ the@ internal@ memories@ to@ record@ all@ or@ a@ selection@ of@ internals.@ In@ conjunction@ with@ the@ trace@ option@ of@ the@ produced@ binary@ it@ can@ also@ record@ these@ flow@ values@ within@ separated@ log@ files.@]@ @ ";
379
      fprintf fmt "Options are:@ "
380
    
381
    let options = [
382
        "-select", Arg.String register_scopes, "specifies which variables to log";
383
        "-input", Arg.String register_inputs, "specifies the simulation input";
384
        "-show-possible-scopes", Arg.Set option_show_scopes, "list possible variables to log";
385
        "-select-all", Arg.Unit register_all_scopes, "select all possible variables to log";
386
(* "-select-mems", Arg.Set option_mems_scopes, "select all memory variables to log";
387
 * "-select-inputs", Arg.Set option_input_scopes, "select all input variables to log"; *)
388
      ]
389

    
390
  let activate = activate
391

    
392
  let check_force_stateful () = is_active()
393

    
394
  let refine_machine_code prog machine_code =
395
    if show_scopes () then
396
      begin
397
	let all_scopes = compute_scopes prog !Options.main_node in
398
        (* Printing scopes *)
399
        if !Options.verbose_level >= 1 then 
400
	  Format.printf "Possible scopes are:@ ";
401
	Format.printf "@[<v 0>%a@ @]@.@?" print_scopes all_scopes;
402
	exit 0
403
      end;
404
    if is_active () then
405
      process_scopes !Options.main_node prog machine_code
406
    else
407
      machine_code
408
	
409

    
410

    
411
  let c_backend_main_loop_body_suffix fmt () =
412
    if is_active () then
413
      begin
414
	Format.fprintf fmt "@ %a" pp_scopes !scopes_map 
415
      end  
416

    
417
  let c_backend_main_loop_body_prefix basename mname fmt () =
418
    if is_active () then
419
      begin
420
	Format.fprintf fmt "@ %a" (pp_scopes_files basename mname) !scopes_map 
421
      end  
422

    
423

    
424
end
425
    
426
(* Local Variables: *)
427
(* compile-command:"make -C ../.." *)
428
(* End: *)