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