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