2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)dfn.c 8.1 (Berkeley) 6/6/93";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
47 typedef struct dfnstruct dfntype;
49 dfntype dfn_stack[ DFN_DEPTH ];
59 dfn_counter = DFN_NAN;
63 * given this parent, depth first number its children.
72 if ( debug & DFNDEBUG ) {
73 printf( "[dfn] dfn(" );
79 * if we're already numbered, no need to look any further.
81 if ( dfn_numbered( parentp ) ) {
85 * if we're already busy, must be a cycle
87 if ( dfn_busy( parentp ) ) {
88 dfn_findcycle( parentp );
92 * visit yourself before your children
94 dfn_pre_visit( parentp );
98 for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
99 if ( arcp -> arc_flags & DEADARC )
101 dfn( arcp -> arc_childp );
104 * visit yourself after your children
106 dfn_post_visit( parentp );
110 * push a parent onto the stack and mark it busy
113 dfn_pre_visit( parentp )
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;
124 if ( debug & DFNDEBUG ) {
125 printf( "[dfn_pre_visit]\t\t%d:" , dfn_depth );
126 printname( parentp );
133 * are we already numbered?
136 dfn_numbered( childp )
140 return ( childp -> toporder != DFN_NAN && childp -> toporder != DFN_BUSY );
144 * are we already busy?
151 if ( childp -> toporder == DFN_NAN ) {
158 * MISSING: an explanation
161 dfn_findcycle( childp )
169 for ( cycletop = dfn_depth ; cycletop > 0 ; cycletop -= 1 ) {
170 cycleheadp = dfn_stack[ cycletop ].nlentryp;
171 if ( childp == cycleheadp ) {
174 if ( childp -> cyclehead != childp &&
175 childp -> cyclehead == cycleheadp ) {
180 errx( 1 , "[dfn_findcycle] couldn't find head of cycle" );
182 if ( debug & DFNDEBUG ) {
183 printf( "[dfn_findcycle] dfn_depth %d cycletop %d " ,
184 dfn_depth , cycletop );
185 printname( cycleheadp );
189 if ( cycletop == dfn_depth ) {
191 * this is previous function, e.g. this calls itself
194 dfn_self_cycle( childp );
197 * glom intervening functions that aren't already
198 * glommed into this cycle.
199 * things have been glommed when their cyclehead field
200 * points to the head of the cycle they are glommed into.
202 for ( tailp = cycleheadp ; tailp -> cnext ; tailp = tailp -> cnext ) {
203 /* void: chase down to tail of things already glommed */
205 if ( debug & DFNDEBUG ) {
206 printf( "[dfn_findcycle] tail " );
213 * if what we think is the top of the cycle
214 * has a cyclehead field, then it's not really the
215 * head of the cycle, which is really what we want
217 if ( cycleheadp -> cyclehead != cycleheadp ) {
218 cycleheadp = cycleheadp -> cyclehead;
220 if ( debug & DFNDEBUG ) {
221 printf( "[dfn_findcycle] new cyclehead " );
222 printname( cycleheadp );
227 for ( index = cycletop + 1 ; index <= dfn_depth ; index += 1 ) {
228 childp = dfn_stack[ index ].nlentryp;
229 if ( childp -> cyclehead == childp ) {
231 * not yet glommed anywhere, glom it
232 * and fix any children it has glommed
234 tailp -> cnext = childp;
235 childp -> cyclehead = cycleheadp;
237 if ( debug & DFNDEBUG ) {
238 printf( "[dfn_findcycle] glomming " );
241 printname( cycleheadp );
245 for ( tailp = childp ; tailp->cnext ; tailp = tailp->cnext ) {
246 tailp -> cnext -> cyclehead = cycleheadp;
248 if ( debug & DFNDEBUG ) {
249 printf( "[dfn_findcycle] and its tail " );
250 printname( tailp -> cnext );
252 printname( cycleheadp );
257 } else if ( childp -> cyclehead != cycleheadp /* firewall */ ) {
259 "[dfn_busy] glommed, but not to cyclehead\n" );
266 * deal with self-cycles
270 dfn_self_cycle( parentp )
274 * since we are taking out self-cycles elsewhere
275 * no need for the special case, here.
278 if ( debug & DFNDEBUG ) {
279 printf( "[dfn_self_cycle] " );
280 printname( parentp );
287 * visit a node after all its children
288 * [MISSING: an explanation]
289 * and pop it off the stack
292 dfn_post_visit( parentp )
298 if ( debug & DFNDEBUG ) {
299 printf( "[dfn_post_visit]\t%d: " , dfn_depth );
300 printname( parentp );
305 * number functions and things in their cycles
306 * unless the function is itself part of a cycle
308 if ( parentp -> cyclehead == parentp ) {
310 for ( memberp = parentp ; memberp ; memberp = memberp -> cnext ) {
311 memberp -> toporder = dfn_counter;
313 if ( debug & DFNDEBUG ) {
314 printf( "[dfn_post_visit]\t\tmember " );
315 printname( memberp );
316 printf( " -> toporder = %d\n" , dfn_counter );
322 if ( debug & DFNDEBUG ) {
323 printf( "[dfn_post_visit]\t\tis part of a cycle\n" );