| 1 | /*------------------------------------------------------------\ | 
|---|
| 2 | |                                                             | | 
|---|
| 3 | | Tool    :                  systemcass                       | | 
|---|
| 4 | |                                                             | | 
|---|
| 5 | | File    :                 graph.cc                          | | 
|---|
| 6 | |                                                             | | 
|---|
| 7 | | Author  :                 Petrot Frédéric                   | | 
|---|
| 8 | |                           Taktak Sami                       | | 
|---|
| 9 | |                           Buchmann Richard                  | | 
|---|
| 10 | |                                                             | | 
|---|
| 11 | | Date    :                   09_07_2004                      | | 
|---|
| 12 | |                                                             | | 
|---|
| 13 | \------------------------------------------------------------*/ | 
|---|
| 14 |  | 
|---|
| 15 | /* | 
|---|
| 16 | * This file is part of the Disydent Project | 
|---|
| 17 | * Copyright (C) Laboratoire LIP6 - Département ASIM | 
|---|
| 18 | * Universite Pierre et Marie Curie | 
|---|
| 19 | * | 
|---|
| 20 | * Home page          : http://www-asim.lip6.fr/disydent | 
|---|
| 21 | * E-mail             : mailto:richard.buchmann@lip6.fr | 
|---|
| 22 | * | 
|---|
| 23 | * This library is free software; you  can redistribute it and/or modify it | 
|---|
| 24 | * under the terms  of the GNU Library General Public  License as published | 
|---|
| 25 | * by the Free Software Foundation; either version 2 of the License, or (at | 
|---|
| 26 | * your option) any later version. | 
|---|
| 27 | * | 
|---|
| 28 | * Disydent is distributed  in the hope  that it  will be | 
|---|
| 29 | * useful, but WITHOUT  ANY WARRANTY; without even the  implied warranty of | 
|---|
| 30 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | 
|---|
| 31 | * Public License for more details. | 
|---|
| 32 | * | 
|---|
| 33 | * You should have received a copy  of the GNU General Public License along | 
|---|
| 34 | * with the GNU C Library; see the  file COPYING. If not, write to the Free | 
|---|
| 35 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 36 | */ | 
|---|
| 37 |  | 
|---|
| 38 | /* | 
|---|
| 39 | * $Log: graph.cc,v $ | 
|---|
| 40 | * Revision 1.10  2006/06/12 14:02:04  buchmann | 
|---|
| 41 | * - Now sort transition and generation functions by fonction pointer | 
|---|
| 42 | *   (Thank to Nicolas Pouillon) | 
|---|
| 43 | * - Add Nicolas Pouillon as contributor into splash screen | 
|---|
| 44 | * - Rename files and classes from "dependancy" to "dependency". | 
|---|
| 45 | * - Add some references to bibliography file | 
|---|
| 46 | * - Add licence note to every files | 
|---|
| 47 | * - Rename "module graph" to "process graph" | 
|---|
| 48 | * - Now dump module graph when using --t option and CASS scheduling at the same | 
|---|
| 49 | *   time | 
|---|
| 50 | * | 
|---|
| 51 | * Bug fixes : | 
|---|
| 52 | * - check user time ( != 0) before computing simulation performance | 
|---|
| 53 | * - Remove reinterpret_cast (for pending write) because of unexpected results | 
|---|
| 54 | * - Add ASSERT in trace functions | 
|---|
| 55 | * | 
|---|
| 56 | * Revision 1.9  2005/10/12 10:00:59  buchmann | 
|---|
| 57 | * Bug fix : | 
|---|
| 58 | * - test to avoid malloc(0) | 
|---|
| 59 | * | 
|---|
| 60 | * Remove : | 
|---|
| 61 | * - unused variable | 
|---|
| 62 | * | 
|---|
| 63 | * Revision 1.8  2005/09/14 14:08:24  buchmann | 
|---|
| 64 | * Add Werror flag to compile the debug library. | 
|---|
| 65 | * Split sc_clock implementation from sc_port to sc_clock. | 
|---|
| 66 | * Add a helper message to write mealy functions avoiding combinational cycle. | 
|---|
| 67 | * | 
|---|
| 68 | * Bug fix : | 
|---|
| 69 | * - cvs rules is no longer circular | 
|---|
| 70 | * - bit2string | 
|---|
| 71 | * - port binding (sc_in to sc_out) | 
|---|
| 72 | * - error message from FSM rules checker | 
|---|
| 73 | * - sc_time copy operator | 
|---|
| 74 | * | 
|---|
| 75 | * Code cleaning : | 
|---|
| 76 | * - remove duplicated code (entity.cc and sc_port.cc) | 
|---|
| 77 | * - remove duplicated declarations (internal_ext.h and internal.h) | 
|---|
| 78 | * | 
|---|
| 79 | * Revision 1.7  2005/03/29 14:04:30  buchmann | 
|---|
| 80 | * Bug fix : | 
|---|
| 81 | * -  Evaluation order of Mealy functions | 
|---|
| 82 | * | 
|---|
| 83 | * Print more informations when DUMP_STAGE is defined. | 
|---|
| 84 | * | 
|---|
| 85 | * Revision 1.6  2005/01/20 09:15:12  buchmann | 
|---|
| 86 | * add following functions to sc_uint classes : | 
|---|
| 87 | * - operator [] | 
|---|
| 88 | * - range (left,right) | 
|---|
| 89 | * | 
|---|
| 90 | * support to port dependancy declarations. | 
|---|
| 91 | * print used precompiled options in verbose mode. | 
|---|
| 92 | * use pedantic flag. | 
|---|
| 93 | * add some rules to generate documentations. | 
|---|
| 94 | * | 
|---|
| 95 | * Revision 1.5  2004/09/27 14:40:13  buchmann | 
|---|
| 96 | * bug fix : | 
|---|
| 97 | * - allow the use of user-defined structs in sc_signal/sc_in/sc_out/sc_inout | 
|---|
| 98 | * - use sc_dt namespace like official SystemC engine | 
|---|
| 99 | * | 
|---|
| 100 | * add : | 
|---|
| 101 | * - dump the signal/port/module dependancy graph in dot format | 
|---|
| 102 | * | 
|---|
| 103 | * Graph library is now splitted from the SystemCASS specific code. | 
|---|
| 104 | * | 
|---|
| 105 | */ | 
|---|
| 106 |  | 
|---|
| 107 | #include <cstdio> | 
|---|
| 108 | #include <map> | 
|---|
| 109 |  | 
|---|
| 110 | #include "graph.h" | 
|---|
| 111 | #include "sc_sensitive.h" | 
|---|
| 112 | #include "sc_module.h" | 
|---|
| 113 | #include "sc_port.h" | 
|---|
| 114 |  | 
|---|
| 115 | using namespace std; | 
|---|
| 116 |  | 
|---|
| 117 | /* Sorry for that, Mr Knuth :-) */ | 
|---|
| 118 |  | 
|---|
| 119 | Graph *gb_new_graph(int n) | 
|---|
| 120 | { | 
|---|
| 121 | Graph *g; | 
|---|
| 122 |  | 
|---|
| 123 | g = (Graph *) malloc(sizeof(Graph)); | 
|---|
| 124 | g->n = n; | 
|---|
| 125 | if (n) | 
|---|
| 126 | { | 
|---|
| 127 | g->vertices = (Vertex *) malloc(n * sizeof(Vertex)); | 
|---|
| 128 | (void)memset(g->vertices, 0, n * sizeof(Vertex)); | 
|---|
| 129 | } else { | 
|---|
| 130 | g->vertices = NULL; | 
|---|
| 131 | } | 
|---|
| 132 | return g; | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | static void gb_new_arc(Vertex *u, Vertex *v, long l) | 
|---|
| 136 | { | 
|---|
| 137 | Arc *a; | 
|---|
| 138 |  | 
|---|
| 139 | a       = (Arc *) malloc(sizeof(Arc)); | 
|---|
| 140 | a->next = u->arcs; | 
|---|
| 141 | u->arcs = a; | 
|---|
| 142 | a->tip  = v; | 
|---|
| 143 | a->len  = l; | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | /* end Sorry */ | 
|---|
| 147 |  | 
|---|
| 148 | #define VERTEX 220898 | 
|---|
| 149 |  | 
|---|
| 150 | #define type     u.I | 
|---|
| 151 |  | 
|---|
| 152 | void new_arc(Vertex *u, Vertex *v) | 
|---|
| 153 | { | 
|---|
| 154 | Arc *a; | 
|---|
| 155 | #ifdef DEBUG | 
|---|
| 156 | if ((u == NULL) || (v == NULL)) | 
|---|
| 157 | exit(29042004); | 
|---|
| 158 | #endif | 
|---|
| 159 | for (a = u->arcs; a; a = a->next) | 
|---|
| 160 | if (a->tip == v) | 
|---|
| 161 | return; | 
|---|
| 162 | gb_new_arc(u, v, 0L); | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | #define rank z.I | 
|---|
| 166 | #define parent y.V | 
|---|
| 167 | #define untagged x.A | 
|---|
| 168 | #define link w.V | 
|---|
| 169 | #define min v.V | 
|---|
| 170 |  | 
|---|
| 171 | #define infinity g->n | 
|---|
| 172 |  | 
|---|
| 173 | /* strong_component: extracts and topologically sorts the strong components of | 
|---|
| 174 | * a graph. | 
|---|
| 175 | * The returned data structure is a chain_list of chain_lists. | 
|---|
| 176 | * The first list implicitely lists the components in order, and the pointed | 
|---|
| 177 | * to ones list the components contents (one or more vertices) */ | 
|---|
| 178 |  | 
|---|
| 179 | extern strong_component_list_t *strong_component(Graph *g) | 
|---|
| 180 | { | 
|---|
| 181 | long nn; | 
|---|
| 182 | Vertex *active_stack; | 
|---|
| 183 | Vertex *settled_stack; | 
|---|
| 184 | Vertex *vv; | 
|---|
| 185 |  | 
|---|
| 186 | register Vertex *v; | 
|---|
| 187 |  | 
|---|
| 188 | strong_component_list_t *strongcomponents = new strong_component_list_t; | 
|---|
| 189 | typedef std::vector<void*> void_list_t; | 
|---|
| 190 | void_list_t *component; | 
|---|
| 191 |  | 
|---|
| 192 | if (g->vertices == NULL) | 
|---|
| 193 | return strongcomponents; | 
|---|
| 194 |  | 
|---|
| 195 | /* Make all vertices unseen and all arcs untagged */ | 
|---|
| 196 | for (v = g->vertices + g->n - 1; v >= g->vertices; v--) { | 
|---|
| 197 | v->rank = 0; | 
|---|
| 198 | v->untagged = v->arcs; | 
|---|
| 199 | } | 
|---|
| 200 | nn = 0; | 
|---|
| 201 | active_stack = settled_stack = NULL; | 
|---|
| 202 |  | 
|---|
| 203 | for (vv = g->vertices; vv < g->vertices + g->n; vv++) | 
|---|
| 204 | if (vv->rank == 0) { /* vv is still unseen */ | 
|---|
| 205 | v = vv; | 
|---|
| 206 | v->parent = NULL; | 
|---|
| 207 |  | 
|---|
| 208 | v->rank = ++nn; | 
|---|
| 209 | v->link = active_stack; | 
|---|
| 210 | active_stack = v; | 
|---|
| 211 | v->min = v; | 
|---|
| 212 |  | 
|---|
| 213 | do { /* Eplore one step from the current vertex v, possibly moving to another vertex | 
|---|
| 214 | and calling it v */ | 
|---|
| 215 | register Vertex *u;               /* a vertex adjacent to v */ | 
|---|
| 216 | register Arc *a = v->untagged;    /* v's first remaining untagged arc, if any */ | 
|---|
| 217 | if (a) { | 
|---|
| 218 | u = a->tip; | 
|---|
| 219 | v->untagged = a->next;    /* tag the arc from v to u */ | 
|---|
| 220 | if (u->rank) {    /* we've seen u already */ | 
|---|
| 221 | if (u->rank < v->min->rank) /* non-tree arc, jutt update v->min */ | 
|---|
| 222 | v->min = u; | 
|---|
| 223 | } else {   /* u is presently unseen */ | 
|---|
| 224 | u->parent = v; /* the arcfrom v to u is a new tree arc */ | 
|---|
| 225 | v = u;         /* u will now be the currnt vertex */ | 
|---|
| 226 | v->rank = ++nn;  /* make vertex v active */ | 
|---|
| 227 | v->link = active_stack; | 
|---|
| 228 | active_stack = v; | 
|---|
| 229 | v->min = v; | 
|---|
| 230 | } | 
|---|
| 231 | } else { /* all arcs from v are tagged, so v matures */ | 
|---|
| 232 | u = v->parent; /* prepare to backtrack in the tree */ | 
|---|
| 233 | if (v->min == v) { | 
|---|
| 234 | /* Remove v and all its successors on the activestack from the tree, | 
|---|
| 235 | and mark them as a strong component of the graph */ | 
|---|
| 236 | register Vertex *t; /* runs though the vertices of the new strong component */ | 
|---|
| 237 |  | 
|---|
| 238 | t = active_stack; | 
|---|
| 239 | active_stack = v->link; | 
|---|
| 240 | v->link = settled_stack; | 
|---|
| 241 | settled_stack = t; /* we've moved the top of one stack to the other */ | 
|---|
| 242 | component = new void_list_t; | 
|---|
| 243 | /* singe vetex */ | 
|---|
| 244 | if (t != v) { /* NOT singe vetex */ | 
|---|
| 245 | while (t != v) { | 
|---|
| 246 | component->push_back(t->data); | 
|---|
| 247 | t->rank = infinity; /* now t is settled */ | 
|---|
| 248 | t->parent = v;  /* and v represents the new strong component */ | 
|---|
| 249 | t = t->link; | 
|---|
| 250 | } | 
|---|
| 251 | } | 
|---|
| 252 | component->push_back(v->data); | 
|---|
| 253 | strongcomponents->push_back(component); | 
|---|
| 254 | v->rank = infinity; /* v too is settled */ | 
|---|
| 255 | v->parent = v; /* and represents its own strong component */ | 
|---|
| 256 | } else /* the arc from u to v has just matured, making v->min visible from u */ | 
|---|
| 257 | if (v->min->rank < u->min->rank) u->min = v->min; | 
|---|
| 258 | v = u; /* the former parent of v is the new current vertex v */ | 
|---|
| 259 | } | 
|---|
| 260 | } while (v != NULL); | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | return strongcomponents; | 
|---|
| 264 | } | 
|---|
| 265 |  | 
|---|
| 266 | bool | 
|---|
| 267 | has_cycle (const strong_component_list_t &l) | 
|---|
| 268 | { | 
|---|
| 269 | strong_component_list_t::const_iterator it; | 
|---|
| 270 | for (it = l.begin (); it != l.end (); ++it) | 
|---|
| 271 | { | 
|---|
| 272 | if ((*it)->size() > 1) | 
|---|
| 273 | return true; | 
|---|
| 274 | } | 
|---|
| 275 | return false; | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | #if 0 | 
|---|
| 279 | const component_list_t* | 
|---|
| 280 | get_cycle (const strong_component_list_t &l) | 
|---|
| 281 | { | 
|---|
| 282 | strong_component_list_t::const_iterator it; | 
|---|
| 283 | for (it = l.begin (); it != l.end (); ++it) | 
|---|
| 284 | { | 
|---|
| 285 | const component_list_t *c = *it; | 
|---|
| 286 | if (c->size() > 1) | 
|---|
| 287 | return c; | 
|---|
| 288 | } | 
|---|
| 289 | return NULL; | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 | void | 
|---|
| 293 | print_cycle (std::ostream &o, | 
|---|
| 294 | const component_list_t *c) | 
|---|
| 295 | { | 
|---|
| 296 | if (c == NULL) | 
|---|
| 297 | return; | 
|---|
| 298 | component_list_t::const_iterator it; | 
|---|
| 299 | for (it = c->begin (); it != c->end (); ++it) | 
|---|
| 300 | { | 
|---|
| 301 | void *v = *it; | 
|---|
| 302 | o << hex << (int) v << " "; | 
|---|
| 303 | } | 
|---|
| 304 | o << "\n"; | 
|---|
| 305 | } | 
|---|
| 306 | #endif | 
|---|
| 307 |  | 
|---|