c51b21d6dd3f82f33691fe6184187b3c1452e7e4
[games.git] / usr.bin / gprof / dfn.c
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  * @(#)dfn.c    8.1 (Berkeley) 6/6/93
34  *
35  * $DragonFly: src/usr.bin/gprof/dfn.c,v 1.3 2003/10/04 20:36:45 hmp Exp $
36  */
37
38 #include <stdio.h>
39 #include "gprof.h"
40
41 #define DFN_DEPTH       100
42 struct dfnstruct {
43     nltype      *nlentryp;
44     int         cycletop;
45 };
46 typedef struct dfnstruct        dfntype;
47
48 dfntype dfn_stack[ DFN_DEPTH ];
49 int     dfn_depth;
50
51 int     dfn_counter;
52
53 dfn_init(void)
54 {
55
56     dfn_depth = 0;
57     dfn_counter = DFN_NAN;
58 }
59
60     /*
61      *  given this parent, depth first number its children.
62      */
63 dfn(nltype *parentp)
64 {
65     arctype     *arcp;
66
67 #   ifdef DEBUG
68         if ( debug & DFNDEBUG ) {
69             printf( "[dfn] dfn(" );
70             printname( parentp );
71             printf( ")\n" );
72         }
73 #   endif DEBUG
74         /*
75          *      if we're already numbered, no need to look any furthur.
76          */
77     if ( dfn_numbered( parentp ) ) {
78         return;
79     }
80         /*
81          *      if we're already busy, must be a cycle
82          */
83     if ( dfn_busy( parentp ) ) {
84         dfn_findcycle( parentp );
85         return;
86     }
87         /*
88          *      visit yourself before your children
89          */
90     dfn_pre_visit( parentp );
91         /*
92          *      visit children
93          */
94     for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
95             if ( arcp -> arc_flags & DEADARC )
96                 continue;
97             dfn( arcp -> arc_childp );
98     }
99         /*
100          *      visit yourself after your children
101          */
102     dfn_post_visit( parentp );
103 }
104
105     /*
106      *  push a parent onto the stack and mark it busy
107      */
108 dfn_pre_visit(nltype *parentp)
109 {
110
111     dfn_depth += 1;
112     if ( dfn_depth >= DFN_DEPTH ) {
113         fprintf( stderr , "[dfn] out of my depth (dfn_stack overflow)\n" );
114         exit( 1 );
115     }
116     dfn_stack[ dfn_depth ].nlentryp = parentp;
117     dfn_stack[ dfn_depth ].cycletop = dfn_depth;
118     parentp -> toporder = DFN_BUSY;
119 #   ifdef DEBUG
120         if ( debug & DFNDEBUG ) {
121             printf( "[dfn_pre_visit]\t\t%d:" , dfn_depth );
122             printname( parentp );
123             printf( "\n" );
124         }
125 #   endif DEBUG
126 }
127
128     /*
129      *  are we already numbered?
130      */
131 bool
132 dfn_numbered(nltype *childp)
133 {
134
135     return ( childp -> toporder != DFN_NAN && childp -> toporder != DFN_BUSY );
136 }
137
138     /*
139      *  are we already busy?
140      */
141 bool
142 dfn_busy(nltype *childp)
143 {
144
145     if ( childp -> toporder == DFN_NAN ) {
146         return FALSE;
147     }
148     return TRUE;
149 }
150
151     /*
152      *  MISSING: an explanation
153      */
154 dfn_findcycle(nltype *childp)
155 {
156     int         cycletop;
157     nltype      *cycleheadp;
158     nltype      *tailp;
159     int         index;
160
161     for ( cycletop = dfn_depth ; cycletop > 0 ; cycletop -= 1 ) {
162         cycleheadp = dfn_stack[ cycletop ].nlentryp;
163         if ( childp == cycleheadp ) {
164             break;
165         }
166         if ( childp -> cyclehead != childp &&
167             childp -> cyclehead == cycleheadp ) {
168             break;
169         }
170     }
171     if ( cycletop <= 0 ) {
172         fprintf( stderr , "[dfn_findcycle] couldn't find head of cycle\n" );
173         exit( 1 );
174     }
175 #   ifdef DEBUG
176         if ( debug & DFNDEBUG ) {
177             printf( "[dfn_findcycle] dfn_depth %d cycletop %d " ,
178                     dfn_depth , cycletop  );
179             printname( cycleheadp );
180             printf( "\n" );
181         }
182 #   endif DEBUG
183     if ( cycletop == dfn_depth ) {
184             /*
185              *  this is previous function, e.g. this calls itself
186              *  sort of boring
187              */
188         dfn_self_cycle( childp );
189     } else {
190             /*
191              *  glom intervening functions that aren't already
192              *  glommed into this cycle.
193              *  things have been glommed when their cyclehead field
194              *  points to the head of the cycle they are glommed into.
195              */
196         for ( tailp = cycleheadp ; tailp -> cnext ; tailp = tailp -> cnext ) {
197             /* void: chase down to tail of things already glommed */
198 #           ifdef DEBUG
199                 if ( debug & DFNDEBUG ) {
200                     printf( "[dfn_findcycle] tail " );
201                     printname( tailp );
202                     printf( "\n" );
203                 }
204 #           endif DEBUG
205         }
206             /*
207              *  if what we think is the top of the cycle
208              *  has a cyclehead field, then it's not really the
209              *  head of the cycle, which is really what we want
210              */
211         if ( cycleheadp -> cyclehead != cycleheadp ) {
212             cycleheadp = cycleheadp -> cyclehead;
213 #           ifdef DEBUG
214                 if ( debug & DFNDEBUG ) {
215                     printf( "[dfn_findcycle] new cyclehead " );
216                     printname( cycleheadp );
217                     printf( "\n" );
218                 }
219 #           endif DEBUG
220         }
221         for ( index = cycletop + 1 ; index <= dfn_depth ; index += 1 ) {
222             childp = dfn_stack[ index ].nlentryp;
223             if ( childp -> cyclehead == childp ) {
224                     /*
225                      *  not yet glommed anywhere, glom it
226                      *  and fix any children it has glommed
227                      */
228                 tailp -> cnext = childp;
229                 childp -> cyclehead = cycleheadp;
230 #               ifdef DEBUG
231                     if ( debug & DFNDEBUG ) {
232                         printf( "[dfn_findcycle] glomming " );
233                         printname( childp );
234                         printf( " onto " );
235                         printname( cycleheadp );
236                         printf( "\n" );
237                     }
238 #               endif DEBUG
239                 for ( tailp = childp ; tailp->cnext ; tailp = tailp->cnext ) {
240                     tailp -> cnext -> cyclehead = cycleheadp;
241 #                   ifdef DEBUG
242                         if ( debug & DFNDEBUG ) {
243                             printf( "[dfn_findcycle] and its tail " );
244                             printname( tailp -> cnext );
245                             printf( " onto " );
246                             printname( cycleheadp );
247                             printf( "\n" );
248                         }
249 #                   endif DEBUG
250                 }
251             } else if ( childp -> cyclehead != cycleheadp /* firewall */ ) {
252                 fprintf( stderr ,
253                         "[dfn_busy] glommed, but not to cyclehead\n" );
254             }
255         }
256     }
257 }
258
259     /*
260      *  deal with self-cycles
261      *  for lint: ARGSUSED
262      */
263 dfn_self_cycle(nltype *parentp)
264 {
265         /*
266          *      since we are taking out self-cycles elsewhere
267          *      no need for the special case, here.
268          */
269 #   ifdef DEBUG
270         if ( debug & DFNDEBUG ) {
271             printf( "[dfn_self_cycle] " );
272             printname( parentp );
273             printf( "\n" );
274         }
275 #   endif DEBUG
276 }
277
278     /*
279      *  visit a node after all its children
280      *  [MISSING: an explanation]
281      *  and pop it off the stack
282      */
283 dfn_post_visit(nltype *parentp)
284 {
285     nltype      *memberp;
286
287 #   ifdef DEBUG
288         if ( debug & DFNDEBUG ) {
289             printf( "[dfn_post_visit]\t%d: " , dfn_depth );
290             printname( parentp );
291             printf( "\n" );
292         }
293 #   endif DEBUG
294         /*
295          *      number functions and things in their cycles
296          *      unless the function is itself part of a cycle
297          */
298     if ( parentp -> cyclehead == parentp ) {
299         dfn_counter += 1;
300         for ( memberp = parentp ; memberp ; memberp = memberp -> cnext ) {
301             memberp -> toporder = dfn_counter;
302 #           ifdef DEBUG
303                 if ( debug & DFNDEBUG ) {
304                     printf( "[dfn_post_visit]\t\tmember " );
305                     printname( memberp );
306                     printf( " -> toporder = %d\n" , dfn_counter );
307                 }
308 #           endif DEBUG
309         }
310     } else {
311 #       ifdef DEBUG
312             if ( debug & DFNDEBUG ) {
313                 printf( "[dfn_post_visit]\t\tis part of a cycle\n" );
314             }
315 #       endif DEBUG
316     }
317     dfn_depth -= 1;
318 }