00001 /* Call Graph */ 00002 00003 /* Copyright (c) 1995, 1997 Stanford University 00004 00005 All rights reserved. 00006 00007 This software is provided under the terms described in 00008 the "suif_copyright.h" include file. */ 00009 00010 #include "common/suif_copyright.h" 00011 00012 #ifndef CG_GRAPH_H 00013 #define CG_GRAPH_H 00014 00015 #include <stdio.h> 00016 #include "basicnodes/basic_forwarders.h" 00017 #include "common/suif_indexed_list.h" 00018 #include "suifkernel/suifkernel_forwarders.h" 00019 00020 class cg_node; 00021 00022 /* This code builds a call graph for the current fileset. The nodes 00023 * of the call graph have pointers to the proc_sym's of the procedures 00024 * they represent. As the call graph is built, each procedure is 00025 * read in and then flushed from memory. If "include_externs" is true 00026 * then the graph will include nodes for external procedures 00027 * (these procedures will be leaves in the call graph since we can't 00028 * scan them to see if they call any other procedures). Recursive calls 00029 * are represented in the graph, thus the graph may have cycles. 00030 */ 00031 00032 typedef void (*cg_map_f)(cg_node *n, void *x); 00033 00034 class cg { 00035 private: 00036 list< cg_node* > *nds; // array of nodes 00037 cg_node *main; // main function, first node in graph 00038 indexed_list< ProcedureSymbol*, cg_node *> 00039 _map_procedure_symbol_to_node; // maps procedures to nodes 00040 bool ext; 00041 00042 // methods for building the graph 00043 void build_call_graph( FileSetBlock *fileset ); 00044 void process_procedure( ProcedureDefinition *ps ); 00045 00046 public: 00047 cg( bool include_externs = false ); 00048 virtual ~cg(); 00049 00050 s_count_t num_nodes(); 00051 // cg_node *node(unsigned i); 00052 void set_node( s_count_t i, cg_node *n ); 00053 cg_node *get_node( s_count_t i ); 00054 00055 cg_node *main_node(); 00056 bool externs_included(); 00057 00058 void add_node( cg_node *n ); 00059 void set_main_node( cg_node *n ); 00060 00061 void map( cg_map_f f, void *x, 00062 bool bottom_up = true, // otherwise top-down 00063 cg_node *start_node = 0); // if NULL, start @ "main" 00064 00065 00066 virtual cg_node *get_cg_node( ProcedureSymbol *ps ); 00067 void print( FILE *fp = stdout ); 00068 }; 00069 00070 00071 // initialization and finalization functions 00072 // 00073 void init_sbrowser_cg( int *argc, char *argv[] ); 00074 void exit_sbrowser_cg(); 00075 00076 00077 #endif /* CG_GRAPH_H */