xref: /freebsd/usr.bin/gprof/dfn.c (revision 40a8ac8f62b535d30349faf28cf47106b7041b83)
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  * 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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #if 0
31 #ifndef lint
32 static char sccsid[] = "@(#)dfn.c	8.1 (Berkeley) 6/6/93";
33 #endif /* not lint */
34 #endif
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <err.h>
40 #include "gprof.h"
41 
42 #define	DFN_DEPTH	100
43 struct dfnstruct {
44     nltype	*nlentryp;
45     int		cycletop;
46 };
47 typedef struct dfnstruct	dfntype;
48 
49 dfntype	dfn_stack[ DFN_DEPTH ];
50 int	dfn_depth;
51 
52 int	dfn_counter;
53 
54 void
55 dfn_init(void)
56 {
57 
58     dfn_depth = 0;
59     dfn_counter = DFN_NAN;
60 }
61 
62     /*
63      *	given this parent, depth first number its children.
64      */
65 void
66 dfn(nltype *parentp)
67 {
68     arctype	*arcp;
69 
70 #   ifdef DEBUG
71 	if ( debug & DFNDEBUG ) {
72 	    printf( "[dfn] dfn(" );
73 	    printname( parentp );
74 	    printf( ")\n" );
75 	}
76 #   endif /* DEBUG */
77 	/*
78 	 *	if we're already numbered, no need to look any further.
79 	 */
80     if ( dfn_numbered( parentp ) ) {
81 	return;
82     }
83 	/*
84 	 *	if we're already busy, must be a cycle
85 	 */
86     if ( dfn_busy( parentp ) ) {
87 	dfn_findcycle( parentp );
88 	return;
89     }
90 	/*
91 	 *	visit yourself before your children
92 	 */
93     dfn_pre_visit( parentp );
94 	/*
95 	 *	visit children
96 	 */
97     for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
98 	    if ( arcp -> arc_flags & DEADARC )
99 		continue;
100 	    dfn( arcp -> arc_childp );
101     }
102 	/*
103 	 *	visit yourself after your children
104 	 */
105     dfn_post_visit( parentp );
106 }
107 
108     /*
109      *	push a parent onto the stack and mark it busy
110      */
111 void
112 dfn_pre_visit(nltype *parentp)
113 {
114 
115     dfn_depth += 1;
116     if ( dfn_depth >= DFN_DEPTH )
117 	errx( 1 , "[dfn] out of my depth (dfn_stack overflow)" );
118     dfn_stack[ dfn_depth ].nlentryp = parentp;
119     dfn_stack[ dfn_depth ].cycletop = dfn_depth;
120     parentp -> toporder = DFN_BUSY;
121 #   ifdef DEBUG
122 	if ( debug & DFNDEBUG ) {
123 	    printf( "[dfn_pre_visit]\t\t%d:" , dfn_depth );
124 	    printname( parentp );
125 	    printf( "\n" );
126 	}
127 #   endif /* DEBUG */
128 }
129 
130     /*
131      *	are we already numbered?
132      */
133 bool
134 dfn_numbered(nltype *childp)
135 {
136 
137     return ( childp -> toporder != DFN_NAN && childp -> toporder != DFN_BUSY );
138 }
139 
140     /*
141      *	are we already busy?
142      */
143 bool
144 dfn_busy(nltype *childp)
145 {
146 
147     if ( childp -> toporder == DFN_NAN ) {
148 	return FALSE;
149     }
150     return TRUE;
151 }
152 
153     /*
154      *	MISSING: an explanation
155      */
156 void
157 dfn_findcycle(nltype *childp)
158 {
159     int		cycletop;
160     nltype	*cycleheadp;
161     nltype	*tailp;
162     int		index;
163 
164     for ( cycletop = dfn_depth ; cycletop > 0 ; cycletop -= 1 ) {
165 	cycleheadp = dfn_stack[ cycletop ].nlentryp;
166 	if ( childp == cycleheadp ) {
167 	    break;
168 	}
169 	if ( childp -> cyclehead != childp &&
170 	    childp -> cyclehead == cycleheadp ) {
171 	    break;
172 	}
173     }
174     if ( cycletop <= 0 )
175 	errx( 1 , "[dfn_findcycle] couldn't find head of cycle" );
176 #   ifdef DEBUG
177 	if ( debug & DFNDEBUG ) {
178 	    printf( "[dfn_findcycle] dfn_depth %d cycletop %d " ,
179 		    dfn_depth , cycletop  );
180 	    printname( cycleheadp );
181 	    printf( "\n" );
182 	}
183 #   endif /* DEBUG */
184     if ( cycletop == dfn_depth ) {
185 	    /*
186 	     *	this is previous function, e.g. this calls itself
187 	     *	sort of boring
188 	     */
189 	dfn_self_cycle( childp );
190     } else {
191 	    /*
192 	     *	glom intervening functions that aren't already
193 	     *	glommed into this cycle.
194 	     *	things have been glommed when their cyclehead field
195 	     *	points to the head of the cycle they are glommed into.
196 	     */
197 	for ( tailp = cycleheadp ; tailp -> cnext ; tailp = tailp -> cnext ) {
198 	    /* void: chase down to tail of things already glommed */
199 #	    ifdef DEBUG
200 		if ( debug & DFNDEBUG ) {
201 		    printf( "[dfn_findcycle] tail " );
202 		    printname( tailp );
203 		    printf( "\n" );
204 		}
205 #	    endif /* DEBUG */
206 	}
207 	    /*
208 	     *	if what we think is the top of the cycle
209 	     *	has a cyclehead field, then it's not really the
210 	     *	head of the cycle, which is really what we want
211 	     */
212 	if ( cycleheadp -> cyclehead != cycleheadp ) {
213 	    cycleheadp = cycleheadp -> cyclehead;
214 #	    ifdef DEBUG
215 		if ( debug & DFNDEBUG ) {
216 		    printf( "[dfn_findcycle] new cyclehead " );
217 		    printname( cycleheadp );
218 		    printf( "\n" );
219 		}
220 #	    endif /* DEBUG */
221 	}
222 	for ( index = cycletop + 1 ; index <= dfn_depth ; index += 1 ) {
223 	    childp = dfn_stack[ index ].nlentryp;
224 	    if ( childp -> cyclehead == childp ) {
225 		    /*
226 		     *	not yet glommed anywhere, glom it
227 		     *	and fix any children it has glommed
228 		     */
229 		tailp -> cnext = childp;
230 		childp -> cyclehead = cycleheadp;
231 #		ifdef DEBUG
232 		    if ( debug & DFNDEBUG ) {
233 			printf( "[dfn_findcycle] glomming " );
234 			printname( childp );
235 			printf( " onto " );
236 			printname( cycleheadp );
237 			printf( "\n" );
238 		    }
239 #		endif /* DEBUG */
240 		for ( tailp = childp ; tailp->cnext ; tailp = tailp->cnext ) {
241 		    tailp -> cnext -> cyclehead = cycleheadp;
242 #		    ifdef DEBUG
243 			if ( debug & DFNDEBUG ) {
244 			    printf( "[dfn_findcycle] and its tail " );
245 			    printname( tailp -> cnext );
246 			    printf( " onto " );
247 			    printname( cycleheadp );
248 			    printf( "\n" );
249 			}
250 #		    endif /* DEBUG */
251 		}
252 	    } else if ( childp -> cyclehead != cycleheadp /* firewall */ ) {
253 		fprintf( stderr ,
254 			"[dfn_busy] glommed, but not to cyclehead\n" );
255 	    }
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