]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/gprof/dfn.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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
34 #if 0
35 #ifndef lint
36 static char sccsid[] = "@(#)dfn.c       8.1 (Berkeley) 6/6/93";
37 #endif /* not lint */
38 #endif
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include <err.h>
44 #include "gprof.h"
45
46 #define DFN_DEPTH       100
47 struct dfnstruct {
48     nltype      *nlentryp;
49     int         cycletop;
50 };
51 typedef struct dfnstruct        dfntype;
52
53 dfntype dfn_stack[ DFN_DEPTH ];
54 int     dfn_depth;
55
56 int     dfn_counter;
57
58 void
59 dfn_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      */
69 void
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 further.
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 void
117 dfn_pre_visit( parentp )
118     nltype      *parentp;
119 {
120
121     dfn_depth += 1;
122     if ( dfn_depth >= DFN_DEPTH )
123         errx( 1 , "[dfn] out of my depth (dfn_stack overflow)" );
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      */
139 bool
140 dfn_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      */
150 bool
151 dfn_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      */
164 void
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         errx( 1 , "[dfn_findcycle] couldn't find head of cycle" );
185 #   ifdef DEBUG
186         if ( debug & DFNDEBUG ) {
187             printf( "[dfn_findcycle] dfn_depth %d cycletop %d " ,
188                     dfn_depth , cycletop  );
189             printname( cycleheadp );
190             printf( "\n" );
191         }
192 #   endif /* DEBUG */
193     if ( cycletop == dfn_depth ) {
194             /*
195              *  this is previous function, e.g. this calls itself
196              *  sort of boring
197              */
198         dfn_self_cycle( childp );
199     } else {
200             /*
201              *  glom intervening functions that aren't already
202              *  glommed into this cycle.
203              *  things have been glommed when their cyclehead field
204              *  points to the head of the cycle they are glommed into.
205              */
206         for ( tailp = cycleheadp ; tailp -> cnext ; tailp = tailp -> cnext ) {
207             /* void: chase down to tail of things already glommed */
208 #           ifdef DEBUG
209                 if ( debug & DFNDEBUG ) {
210                     printf( "[dfn_findcycle] tail " );
211                     printname( tailp );
212                     printf( "\n" );
213                 }
214 #           endif /* DEBUG */
215         }
216             /*
217              *  if what we think is the top of the cycle
218              *  has a cyclehead field, then it's not really the
219              *  head of the cycle, which is really what we want
220              */
221         if ( cycleheadp -> cyclehead != cycleheadp ) {
222             cycleheadp = cycleheadp -> cyclehead;
223 #           ifdef DEBUG
224                 if ( debug & DFNDEBUG ) {
225                     printf( "[dfn_findcycle] new cyclehead " );
226                     printname( cycleheadp );
227                     printf( "\n" );
228                 }
229 #           endif /* DEBUG */
230         }
231         for ( index = cycletop + 1 ; index <= dfn_depth ; index += 1 ) {
232             childp = dfn_stack[ index ].nlentryp;
233             if ( childp -> cyclehead == childp ) {
234                     /*
235                      *  not yet glommed anywhere, glom it
236                      *  and fix any children it has glommed
237                      */
238                 tailp -> cnext = childp;
239                 childp -> cyclehead = cycleheadp;
240 #               ifdef DEBUG
241                     if ( debug & DFNDEBUG ) {
242                         printf( "[dfn_findcycle] glomming " );
243                         printname( childp );
244                         printf( " onto " );
245                         printname( cycleheadp );
246                         printf( "\n" );
247                     }
248 #               endif /* DEBUG */
249                 for ( tailp = childp ; tailp->cnext ; tailp = tailp->cnext ) {
250                     tailp -> cnext -> cyclehead = cycleheadp;
251 #                   ifdef DEBUG
252                         if ( debug & DFNDEBUG ) {
253                             printf( "[dfn_findcycle] and its tail " );
254                             printname( tailp -> cnext );
255                             printf( " onto " );
256                             printname( cycleheadp );
257                             printf( "\n" );
258                         }
259 #                   endif /* DEBUG */
260                 }
261             } else if ( childp -> cyclehead != cycleheadp /* firewall */ ) {
262                 fprintf( stderr ,
263                         "[dfn_busy] glommed, but not to cyclehead\n" );
264             }
265         }
266     }
267 }
268
269     /*
270      *  deal with self-cycles
271      *  for lint: ARGSUSED
272      */
273 void
274 dfn_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      */
295 void
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 }