Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / sortProg.ml @ 22fe1c93

History | View | Annotate | Download (1.3 KB)

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

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

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

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

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