PIPS
toposort.c File Reference
#include <stdio.h>
#include <string.h>
#include "genC.h"
#include "linear.h"
#include "misc.h"
#include "pipsdbm.h"
#include "ri.h"
#include "ri-util.h"
#include "icfg.h"
+ Include dependency graph for toposort.c:

Go to the source code of this file.

Functions

list module_name_to_callees (char *module_name) const
 toposort.c More...
 
list module_to_callees (entity mod)
 
void topological_number_assign_to_module (hash_table hash_module_to_depth, entity mod, size_t n)
 
list module_list_sort (hash_table hash_module_to_depth, list current_list, entity mod, size_t n)
 
list topologically_sorted_module_list (entity mod)
 
void print_module_name_to_toposorts (char *module_name) const
 

Function Documentation

◆ module_list_sort()

list module_list_sort ( hash_table  hash_module_to_depth,
list  current_list,
entity  mod,
size_t  n 
)

create the callees list whose caller has the depth n

free the same_depth_list for later use

create same_depth_list whose depth is n+1

concatenate former current_list with same_depth_list

use module_list_sort recursively

Parameters
hash_module_to_depthash_module_to_depth
current_listurrent_list
modod

Definition at line 123 of file toposort.c.

128 {
129  list callees_list = NIL;
130  static list same_depth_list = NIL;
131  size_t depth;
132 
133  /* create the callees list whose caller has the depth n */
134  if ( same_depth_list == NIL )
135  callees_list = module_to_callees(mod);
136  else {
137  MAPL(pm,{ entity e = ENTITY(CAR(pm));
138  callees_list = gen_nconc(callees_list,
140  },
141  same_depth_list);
142  }
143 
144  /* free the same_depth_list for later use */
145 
146  same_depth_list = NIL;
147 
148  /* create same_depth_list whose depth is n+1 */
149  if ( callees_list != NIL ) {
150  MAPL(pm,{ entity e = ENTITY(CAR(pm));
151  depth = (size_t) hash_get(hash_module_to_depth, (char *) e);
152  if ( depth == n+1 ) {
153  same_depth_list = gen_nconc(same_depth_list,
154  CONS(ENTITY, e, NIL));
155  hash_put(hash_module_to_depth,
156  (char *) e, (char *) -1);
157  }
158  },
159  callees_list);
160 
161  /* concatenate former current_list with same_depth_list */
162  current_list = gen_nconc(current_list, same_depth_list);
163 
164  /* use module_list_sort recursively */
165  current_list = module_list_sort(hash_module_to_depth,
166  current_list,
167  ENTITY(CAR(same_depth_list)),
168  n+1);
169  }
170  return (current_list);
171 }
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
list gen_copy_seq(list l)
Copy a list structure.
Definition: list.c:501
#define CONS(_t_, _i_, _l_)
List element cell constructor (insert an element at the beginning of a list)
Definition: newgen_list.h:150
list gen_nconc(list cp1, list cp2)
physically concatenates CP1 and CP2 but do not duplicates the elements
Definition: list.c:344
#define CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
#define MAPL(_map_list_cp, _code, _l)
Apply some code on the addresses of all the elements of a list.
Definition: newgen_list.h:203
void * hash_get(const hash_table htp, const void *key)
this function retrieves in the hash table pointed to by htp the couple whose key is equal to key.
Definition: hash.c:449
void hash_put(hash_table htp, const void *key, const void *val)
This functions stores a couple (key,val) in the hash table pointed to by htp.
Definition: hash.c:364
size_t size_t
Definition: properties.c:413
#define ENTITY(x)
ENTITY.
Definition: ri.h:2755
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
static int depth
la sequence de nids
list module_list_sort(hash_table hash_module_to_depth, list current_list, entity mod, size_t n)
Definition: toposort.c:123
list module_to_callees(entity mod)
Definition: toposort.c:85

References CAR, CONS, depth, ENTITY, gen_copy_seq(), gen_nconc(), hash_get(), hash_put(), MAPL, module_list_sort(), module_to_callees(), and NIL.

Referenced by module_list_sort(), and topologically_sorted_module_list().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ module_name_to_callees()

list module_name_to_callees ( char*  module_name) const

toposort.c

Entry point: list topologically_sorted_module_list(mod)

returns the list of subroutines/functions called in the module mod sorted as follow: we recursively build the graph of calls, taking mod for root, and we apply on it the topological sort. The first of the list is the module mod itself.

Pierre Berthomier, May 1990 Lei Zhou, January 1991 get all the callees of the module module_name,return the no-empty list of string. if the module has callee(s),the first element of the return list is the module's last callee.

Definition at line 59 of file toposort.c.

61 {
62  callees cl;
63  static hash_table hash_table_to_callees_string;
64  static bool hash_table_is_created = false;
65  list callees_list=NIL;
66 
67  cl = (callees)db_get_memory_resource(DBR_CALLEES,module_name,true);
68 
69  if ( !hash_table_is_created ) {
70  hash_table_to_callees_string = hash_table_make(hash_pointer, 0);
71  hash_table_is_created = true;
72  }
73 
74  callees_list = (list)hash_get(hash_table_to_callees_string, module_name);
75 
76  if ( callees_list == (list)HASH_UNDEFINED_VALUE ) {
77  callees_list = callees_callees(cl);
78  hash_put(hash_table_to_callees_string, module_name,
79  (char *)callees_list);
80  }
81 
82  return(callees_list);
83 }
const char * module_name(const char *s)
Return the module part of an entity name.
Definition: entity_names.c:296
string db_get_memory_resource(const char *rname, const char *oname, bool pure)
Return the pointer to the resource, whatever it is.
Definition: database.c:755
hash_table hash_table_make(hash_key_type key_type, size_t size)
Definition: hash.c:294
@ hash_pointer
Definition: newgen_hash.h:32
#define HASH_UNDEFINED_VALUE
value returned by hash_get() when the key is not found; could also be called HASH_KEY_NOT_FOUND,...
Definition: newgen_hash.h:56
struct cons * list
Definition: newgen_types.h:106
struct _newgen_struct_callees_ * callees
Definition: ri.h:55
#define callees_callees(x)
Definition: ri.h:675

References callees_callees, db_get_memory_resource(), hash_get(), hash_pointer, hash_put(), hash_table_make(), HASH_UNDEFINED_VALUE, module_name(), and NIL.

+ Here is the call graph for this function:

◆ module_to_callees()

list module_to_callees ( entity  mod)
Parameters
modod

Definition at line 85 of file toposort.c.

87 {
88  list return_list = NIL;
89  list callees_list = code_declarations(entity_code(mod));
90 
91  MAPL(ce,{
92  entity e = ENTITY(CAR(ce));
94  return_list = CONS(ENTITY,
96  return_list);
97  },callees_list);
98 
99  return(return_list);
100 }
const char * entity_local_name(entity e)
entity_local_name modified so that it does not core when used in vect_fprint, since someone thought t...
Definition: entity.c:453
entity local_name_to_top_level_entity(const char *n)
This function try to find a top-level entity from a local name.
Definition: entity.c:1450
code entity_code(entity e)
Definition: entity.c:1098
#define type_functional_p(x)
Definition: ri.h:2950
#define code_declarations(x)
Definition: ri.h:784
#define entity_type(x)
Definition: ri.h:2792

References CAR, code_declarations, CONS, ENTITY, entity_code(), entity_local_name(), entity_type, local_name_to_top_level_entity(), MAPL, NIL, and type_functional_p.

Referenced by module_list_sort(), and topological_number_assign_to_module().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ print_module_name_to_toposorts()

void print_module_name_to_toposorts ( char*  module_name) const

Definition at line 195 of file toposort.c.

197 {
198  list sorted_list = NIL;
199  string filename;
200  FILE *fp;
202 
203  pips_assert("print_module_name_to_toposorts", mod != entity_undefined &&
204  entity_module_p(mod));
205 
206  fprintf(stderr, "topological-sorting callees for %s ...\n",
207  entity_name(mod));
208 
210 
211  sorted_list = (list) topologically_sorted_module_list(mod);
213  "/", module_name, ".topo",NULL));
214 
215  fp = safe_fopen(filename, "w");
216 
217  MAPL(pm,
218  { fprintf(fp, "%s\n",entity_name(ENTITY(CAR(pm)))); },
219  sorted_list);
220 
221  safe_fclose(fp, filename);
222  fprintf(stderr, "result written to %s\n", filename);
223 
224  debug_off();
225 }
FILE * safe_fopen(const char *filename, const char *what)
Definition: file.c:67
int safe_fclose(FILE *stream, const char *filename)
Definition: file.c:77
#define ICFG_DEBUG_LEVEL
Definition: icfg-local.h:35
#define debug_on(env)
Definition: misc-local.h:157
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define debug_off()
Definition: misc-local.h:160
string concatenate(const char *,...)
Return the concatenation of the given strings.
Definition: string.c:183
string db_get_current_workspace_directory(void)
Definition: workspace.c:96
bool entity_module_p(entity e)
Definition: entity.c:683
#define entity_undefined
Definition: ri.h:2761
#define entity_name(x)
Definition: ri.h:2790
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
char * strdup()
list topologically_sorted_module_list(entity mod)
Definition: toposort.c:173

References CAR, concatenate(), db_get_current_workspace_directory(), debug_off, debug_on, ENTITY, entity_module_p(), entity_name, entity_undefined, fprintf(), ICFG_DEBUG_LEVEL, local_name_to_top_level_entity(), MAPL, module_name(), NIL, pips_assert, safe_fclose(), safe_fopen(), strdup(), and topologically_sorted_module_list().

+ Here is the call graph for this function:

◆ topological_number_assign_to_module()

void topological_number_assign_to_module ( hash_table  hash_module_to_depth,
entity  mod,
size_t  n 
)

assigns depth n+1 to callees of current module

Parameters
hash_module_to_depthash_module_to_depth
modod

Definition at line 102 of file toposort.c.

106 {
107  size_t depth = (size_t) hash_get(hash_module_to_depth, (char *) mod);
108  list callees_list = module_to_callees(mod);
109 
110  if ((depth == (size_t) ICFG_NOT_FOUND) || (depth < n))
111  hash_put(hash_module_to_depth, (char *) mod, (char *) n);
112 
113  if ( callees_list != NIL ) {
114  /* assigns depth n+1 to callees of current module */
115  MAPL(pm,
116  { entity e = ENTITY(CAR(pm));
117  topological_number_assign_to_module(hash_module_to_depth, e, n+1);
118  },
119  callees_list);
120  }
121 }
#define ICFG_NOT_FOUND
Definition: icfg-local.h:29
void topological_number_assign_to_module(hash_table hash_module_to_depth, entity mod, size_t n)
Definition: toposort.c:102

References CAR, depth, ENTITY, hash_get(), hash_put(), ICFG_NOT_FOUND, MAPL, module_to_callees(), NIL, and topological_number_assign_to_module().

Referenced by topological_number_assign_to_module(), and topologically_sorted_module_list().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ topologically_sorted_module_list()

list topologically_sorted_module_list ( entity  mod)

"depth" of subroutine or function for topological sort

Parameters
modod

Definition at line 173 of file toposort.c.

175 {
176  /* "depth" of subroutine or function for topological sort */
177  hash_table hash_module_to_depth = (hash_table) NULL;
178  list sorted_list;
179 
180  hash_module_to_depth = hash_table_make(hash_pointer, 0);
182 
183  topological_number_assign_to_module(hash_module_to_depth, mod, 0);
184 
185  sorted_list = module_list_sort(hash_module_to_depth, NIL, mod, 0);
186 
187  pips_assert("free_hash_table",
188  hash_module_to_depth != (hash_table) NULL);
189  hash_table_free(hash_module_to_depth);
190  hash_module_to_depth = (hash_table) NULL;
191 
192  return (sorted_list);
193 }
void hash_dont_warn_on_redefinition(void)
Definition: hash.c:188
void hash_table_free(hash_table htp)
this function deletes a hash table that is no longer useful.
Definition: hash.c:327
struct __hash_table * hash_table
Define hash_table structure which is hidden.
Definition: newgen_hash.h:43

References hash_dont_warn_on_redefinition(), hash_pointer, hash_table_free(), hash_table_make(), module_list_sort(), NIL, pips_assert, and topological_number_assign_to_module().

Referenced by print_module_name_to_toposorts().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: