1 /* $OpenBSD: dfn.c,v 1.6 2006/03/25 19:06:35 espie Exp $ */ 2 /* $NetBSD: dfn.c,v 1.5 1995/04/19 07:15:56 cgd Exp $ */ 3 4 /* 5 * Copyright (c) 1983, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)dfn.c 8.1 (Berkeley) 6/6/93"; 36 #else 37 static char rcsid[] = "$OpenBSD: dfn.c,v 1.6 2006/03/25 19:06:35 espie Exp $"; 38 #endif 39 #endif /* not lint */ 40 41 #include <stdio.h> 42 #include "gprof.h" 43 44 #define DFN_DEPTH 100 45 struct dfnstruct { 46 nltype *nlentryp; 47 int cycletop; 48 }; 49 typedef struct dfnstruct dfntype; 50 51 dfntype dfn_stack[ DFN_DEPTH ]; 52 int dfn_depth; 53 54 int dfn_counter; 55 56 void 57 dfn_init() 58 { 59 60 dfn_depth = 0; 61 dfn_counter = DFN_NAN; 62 } 63 64 /* 65 * given this parent, depth first number its children. 66 */ 67 void 68 dfn(nltype *parentp) 69 { 70 arctype *arcp; 71 72 # ifdef DEBUG 73 if ( debug & DFNDEBUG ) { 74 printf( "[dfn] dfn(" ); 75 printname( parentp ); 76 printf( ")\n" ); 77 } 78 # endif /* DEBUG */ 79 /* 80 * if we're already numbered, no need to look any furthur. 81 */ 82 if ( dfn_numbered( parentp ) ) { 83 return; 84 } 85 /* 86 * if we're already busy, must be a cycle 87 */ 88 if ( dfn_busy( parentp ) ) { 89 dfn_findcycle( parentp ); 90 return; 91 } 92 /* 93 * visit yourself before your children 94 */ 95 dfn_pre_visit( parentp ); 96 /* 97 * visit children 98 */ 99 for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) { 100 if ( arcp -> arc_flags & DEADARC ) 101 continue; 102 dfn( arcp -> arc_childp ); 103 } 104 /* 105 * visit yourself after your children 106 */ 107 dfn_post_visit( parentp ); 108 } 109 110 /* 111 * push a parent onto the stack and mark it busy 112 */ 113 void 114 dfn_pre_visit(nltype *parentp) 115 { 116 117 dfn_depth += 1; 118 if ( dfn_depth >= DFN_DEPTH ) 119 errx(1, "[dfn] out of my depth (dfn_stack overflow)" ); 120 dfn_stack[ dfn_depth ].nlentryp = parentp; 121 dfn_stack[ dfn_depth ].cycletop = dfn_depth; 122 parentp -> toporder = DFN_BUSY; 123 # ifdef DEBUG 124 if ( debug & DFNDEBUG ) { 125 printf( "[dfn_pre_visit]\t\t%d:" , dfn_depth ); 126 printname( parentp ); 127 printf( "\n" ); 128 } 129 # endif /* DEBUG */ 130 } 131 132 /* 133 * are we already numbered? 134 */ 135 bool 136 dfn_numbered(nltype *childp) 137 { 138 139 return ( childp -> toporder != DFN_NAN && childp -> toporder != DFN_BUSY ); 140 } 141 142 /* 143 * are we already busy? 144 */ 145 bool 146 dfn_busy(nltype *childp) 147 { 148 149 if ( childp -> toporder == DFN_NAN ) { 150 return FALSE; 151 } 152 return TRUE; 153 } 154 155 /* 156 * MISSING: an explanation 157 */ 158 void 159 dfn_findcycle(nltype *childp) 160 { 161 int cycletop; 162 nltype *cycleheadp; 163 nltype *tailp; 164 int index; 165 166 for ( cycletop = dfn_depth ; cycletop > 0 ; cycletop -= 1 ) { 167 cycleheadp = dfn_stack[ cycletop ].nlentryp; 168 if ( childp == cycleheadp ) { 169 break; 170 } 171 if ( childp -> cyclehead != childp && 172 childp -> cyclehead == cycleheadp ) { 173 break; 174 } 175 } 176 if ( cycletop <= 0 ) 177 errx( 1, "[dfn_findcycle] couldn't find head of cycle"); 178 # ifdef DEBUG 179 if ( debug & DFNDEBUG ) { 180 printf( "[dfn_findcycle] dfn_depth %d cycletop %d " , 181 dfn_depth , cycletop ); 182 printname( cycleheadp ); 183 printf( "\n" ); 184 } 185 # endif /* DEBUG */ 186 if ( cycletop == dfn_depth ) { 187 /* 188 * this is previous function, e.g. this calls itself 189 * sort of boring 190 */ 191 dfn_self_cycle( childp ); 192 } else { 193 /* 194 * glom intervening functions that aren't already 195 * glommed into this cycle. 196 * things have been glommed when their cyclehead field 197 * points to the head of the cycle they are glommed into. 198 */ 199 for ( tailp = cycleheadp ; tailp -> cnext ; tailp = tailp -> cnext ) { 200 /* void: chase down to tail of things already glommed */ 201 # ifdef DEBUG 202 if ( debug & DFNDEBUG ) { 203 printf( "[dfn_findcycle] tail " ); 204 printname( tailp ); 205 printf( "\n" ); 206 } 207 # endif /* DEBUG */ 208 } 209 /* 210 * if what we think is the top of the cycle 211 * has a cyclehead field, then it's not really the 212 * head of the cycle, which is really what we want 213 */ 214 if ( cycleheadp -> cyclehead != cycleheadp ) { 215 cycleheadp = cycleheadp -> cyclehead; 216 # ifdef DEBUG 217 if ( debug & DFNDEBUG ) { 218 printf( "[dfn_findcycle] new cyclehead " ); 219 printname( cycleheadp ); 220 printf( "\n" ); 221 } 222 # endif /* DEBUG */ 223 } 224 for ( index = cycletop + 1 ; index <= dfn_depth ; index += 1 ) { 225 childp = dfn_stack[ index ].nlentryp; 226 if ( childp -> cyclehead == childp ) { 227 /* 228 * not yet glommed anywhere, glom it 229 * and fix any children it has glommed 230 */ 231 tailp -> cnext = childp; 232 childp -> cyclehead = cycleheadp; 233 # ifdef DEBUG 234 if ( debug & DFNDEBUG ) { 235 printf( "[dfn_findcycle] glomming " ); 236 printname( childp ); 237 printf( " onto " ); 238 printname( cycleheadp ); 239 printf( "\n" ); 240 } 241 # endif /* DEBUG */ 242 for ( tailp = childp ; tailp->cnext ; tailp = tailp->cnext ) { 243 tailp -> cnext -> cyclehead = cycleheadp; 244 # ifdef DEBUG 245 if ( debug & DFNDEBUG ) { 246 printf( "[dfn_findcycle] and its tail " ); 247 printname( tailp -> cnext ); 248 printf( " onto " ); 249 printname( cycleheadp ); 250 printf( "\n" ); 251 } 252 # endif /* DEBUG */ 253 } 254 } else if ( childp -> cyclehead != cycleheadp /* firewall */ ) 255 warnx("[dfn_busy] glommed, but not to cyclehead"); 256 } 257 } 258 } 259 260 /* 261 * deal with self-cycles 262 * for lint: ARGSUSED 263 */ 264 void 265 dfn_self_cycle(nltype *parentp) 266 { 267 /* 268 * since we are taking out self-cycles elsewhere 269 * no need for the special case, here. 270 */ 271 # ifdef DEBUG 272 if ( debug & DFNDEBUG ) { 273 printf( "[dfn_self_cycle] " ); 274 printname( parentp ); 275 printf( "\n" ); 276 } 277 # endif /* DEBUG */ 278 } 279 280 /* 281 * visit a node after all its children 282 * [MISSING: an explanation] 283 * and pop it off the stack 284 */ 285 void 286 dfn_post_visit(nltype *parentp) 287 { 288 nltype *memberp; 289 290 # ifdef DEBUG 291 if ( debug & DFNDEBUG ) { 292 printf( "[dfn_post_visit]\t%d: " , dfn_depth ); 293 printname( parentp ); 294 printf( "\n" ); 295 } 296 # endif /* DEBUG */ 297 /* 298 * number functions and things in their cycles 299 * unless the function is itself part of a cycle 300 */ 301 if ( parentp -> cyclehead == parentp ) { 302 dfn_counter += 1; 303 for ( memberp = parentp ; memberp ; memberp = memberp -> cnext ) { 304 memberp -> toporder = dfn_counter; 305 # ifdef DEBUG 306 if ( debug & DFNDEBUG ) { 307 printf( "[dfn_post_visit]\t\tmember " ); 308 printname( memberp ); 309 printf( " -> toporder = %d\n" , dfn_counter ); 310 } 311 # endif /* DEBUG */ 312 } 313 } else { 314 # ifdef DEBUG 315 if ( debug & DFNDEBUG ) { 316 printf( "[dfn_post_visit]\t\tis part of a cycle\n" ); 317 } 318 # endif /* DEBUG */ 319 } 320 dfn_depth -= 1; 321 } 322