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