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