Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / sortProg.ml @ 01c7d5e1

History | View | Annotate | Download (1.31 KB)

1
open Graph
2
open LustreSpec
3
open Corelang
4
module TopologicalDepGraph = Topological.Make(Causality.IdentDepGraph)
5

    
6
let get_node nid prog =
7
  List.find (fun t -> match t.top_decl_desc with Node n -> n.node_id = nid | _ -> false) prog
8

    
9
let check_external_defs x not_nodes = true (* TODO, check whether a node, a function or an include defines this node *)
10

    
11
let sort prog =
12
  let not_nodes, nodes = 
13
    List.partition (fun top -> match top.top_decl_desc with Node _ -> false | _ -> true) prog 
14
  in
15
  let sorted = 
16
    try
17
      let g = Causality.NodeDep.dependence_graph nodes in
18
      Causality.CycleDetection.check_cycles g;
19
    
20
      (
21
	TopologicalDepGraph.fold 
22
	  (fun x accu -> 
23
	    try 
24
	      (get_node x nodes)::accu
25
	    with Not_found -> 
26
	      (* check whether it is an imported node, a function or in the includes *)
27
	      if check_external_defs x not_nodes then
28
		accu 
29
	      else 
30
		(Format.eprintf "Impossible to find node %s@.@?" x; failwith x)
31
	  )
32
	  g []
33
      )
34
  with (Causality.Cycle v) as exc ->
35
    Causality.pp_error Format.err_formatter v;
36
    raise exc
37
  in
38
  Log.report ~level:3 
39
    (fun fmt -> Format.fprintf fmt "Ordered list of declarations:@.%a@.@?" (Utils.fprintf_list ~sep:"@." Printers.pp_short_decl) sorted);
40
  	  not_nodes@sorted
41

    
42
(* Local Variables: *)
43
(* compile-command:"make -C .." *)
44
(* End: *)