xref: /freebsd/usr.bin/gprof/dfn.c (revision 97fa9b77399b799457a0e59000391966367c07d4)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1983, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
340fb7a0beSGarrett Wollman #if 0
3597fa9b77SPhilippe Charnier #ifndef lint
369b50d902SRodney W. Grimes static char sccsid[] = "@(#)dfn.c	8.1 (Berkeley) 6/6/93";
379b50d902SRodney W. Grimes #endif /* not lint */
3897fa9b77SPhilippe Charnier #endif
3997fa9b77SPhilippe Charnier 
40e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
41e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
429b50d902SRodney W. Grimes 
4397fa9b77SPhilippe Charnier #include <err.h>
449b50d902SRodney W. Grimes #include "gprof.h"
459b50d902SRodney W. Grimes 
469b50d902SRodney W. Grimes #define	DFN_DEPTH	100
479b50d902SRodney W. Grimes struct dfnstruct {
489b50d902SRodney W. Grimes     nltype	*nlentryp;
499b50d902SRodney W. Grimes     int		cycletop;
509b50d902SRodney W. Grimes };
519b50d902SRodney W. Grimes typedef struct dfnstruct	dfntype;
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes dfntype	dfn_stack[ DFN_DEPTH ];
549b50d902SRodney W. Grimes int	dfn_depth;
559b50d902SRodney W. Grimes 
569b50d902SRodney W. Grimes int	dfn_counter;
579b50d902SRodney W. Grimes 
5897fa9b77SPhilippe Charnier void
599b50d902SRodney W. Grimes dfn_init()
609b50d902SRodney W. Grimes {
619b50d902SRodney W. Grimes 
629b50d902SRodney W. Grimes     dfn_depth = 0;
639b50d902SRodney W. Grimes     dfn_counter = DFN_NAN;
649b50d902SRodney W. Grimes }
659b50d902SRodney W. Grimes 
669b50d902SRodney W. Grimes     /*
679b50d902SRodney W. Grimes      *	given this parent, depth first number its children.
689b50d902SRodney W. Grimes      */
6997fa9b77SPhilippe Charnier void
709b50d902SRodney W. Grimes dfn( parentp )
719b50d902SRodney W. Grimes     nltype	*parentp;
729b50d902SRodney W. Grimes {
739b50d902SRodney W. Grimes     arctype	*arcp;
749b50d902SRodney W. Grimes 
759b50d902SRodney W. Grimes #   ifdef DEBUG
769b50d902SRodney W. Grimes 	if ( debug & DFNDEBUG ) {
779b50d902SRodney W. Grimes 	    printf( "[dfn] dfn(" );
789b50d902SRodney W. Grimes 	    printname( parentp );
799b50d902SRodney W. Grimes 	    printf( ")\n" );
809b50d902SRodney W. Grimes 	}
810fb7a0beSGarrett Wollman #   endif /* DEBUG */
829b50d902SRodney W. Grimes 	/*
8397fa9b77SPhilippe Charnier 	 *	if we're already numbered, no need to look any further.
849b50d902SRodney W. Grimes 	 */
859b50d902SRodney W. Grimes     if ( dfn_numbered( parentp ) ) {
869b50d902SRodney W. Grimes 	return;
879b50d902SRodney W. Grimes     }
889b50d902SRodney W. Grimes 	/*
899b50d902SRodney W. Grimes 	 *	if we're already busy, must be a cycle
909b50d902SRodney W. Grimes 	 */
919b50d902SRodney W. Grimes     if ( dfn_busy( parentp ) ) {
929b50d902SRodney W. Grimes 	dfn_findcycle( parentp );
939b50d902SRodney W. Grimes 	return;
949b50d902SRodney W. Grimes     }
959b50d902SRodney W. Grimes 	/*
969b50d902SRodney W. Grimes 	 *	visit yourself before your children
979b50d902SRodney W. Grimes 	 */
989b50d902SRodney W. Grimes     dfn_pre_visit( parentp );
999b50d902SRodney W. Grimes 	/*
1009b50d902SRodney W. Grimes 	 *	visit children
1019b50d902SRodney W. Grimes 	 */
1029b50d902SRodney W. Grimes     for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
1039b50d902SRodney W. Grimes 	    if ( arcp -> arc_flags & DEADARC )
1049b50d902SRodney W. Grimes 		continue;
1059b50d902SRodney W. Grimes 	    dfn( arcp -> arc_childp );
1069b50d902SRodney W. Grimes     }
1079b50d902SRodney W. Grimes 	/*
1089b50d902SRodney W. Grimes 	 *	visit yourself after your children
1099b50d902SRodney W. Grimes 	 */
1109b50d902SRodney W. Grimes     dfn_post_visit( parentp );
1119b50d902SRodney W. Grimes }
1129b50d902SRodney W. Grimes 
1139b50d902SRodney W. Grimes     /*
1149b50d902SRodney W. Grimes      *	push a parent onto the stack and mark it busy
1159b50d902SRodney W. Grimes      */
11697fa9b77SPhilippe Charnier void
1179b50d902SRodney W. Grimes dfn_pre_visit( parentp )
1189b50d902SRodney W. Grimes     nltype	*parentp;
1199b50d902SRodney W. Grimes {
1209b50d902SRodney W. Grimes 
1219b50d902SRodney W. Grimes     dfn_depth += 1;
12297fa9b77SPhilippe Charnier     if ( dfn_depth >= DFN_DEPTH )
12397fa9b77SPhilippe Charnier 	errx( 1 , "[dfn] out of my depth (dfn_stack overflow)" );
1249b50d902SRodney W. Grimes     dfn_stack[ dfn_depth ].nlentryp = parentp;
1259b50d902SRodney W. Grimes     dfn_stack[ dfn_depth ].cycletop = dfn_depth;
1269b50d902SRodney W. Grimes     parentp -> toporder = DFN_BUSY;
1279b50d902SRodney W. Grimes #   ifdef DEBUG
1289b50d902SRodney W. Grimes 	if ( debug & DFNDEBUG ) {
1299b50d902SRodney W. Grimes 	    printf( "[dfn_pre_visit]\t\t%d:" , dfn_depth );
1309b50d902SRodney W. Grimes 	    printname( parentp );
1319b50d902SRodney W. Grimes 	    printf( "\n" );
1329b50d902SRodney W. Grimes 	}
1330fb7a0beSGarrett Wollman #   endif /* DEBUG */
1349b50d902SRodney W. Grimes }
1359b50d902SRodney W. Grimes 
1369b50d902SRodney W. Grimes     /*
1379b50d902SRodney W. Grimes      *	are we already numbered?
1389b50d902SRodney W. Grimes      */
1399b50d902SRodney W. Grimes bool
1409b50d902SRodney W. Grimes dfn_numbered( childp )
1419b50d902SRodney W. Grimes     nltype	*childp;
1429b50d902SRodney W. Grimes {
1439b50d902SRodney W. Grimes 
1449b50d902SRodney W. Grimes     return ( childp -> toporder != DFN_NAN && childp -> toporder != DFN_BUSY );
1459b50d902SRodney W. Grimes }
1469b50d902SRodney W. Grimes 
1479b50d902SRodney W. Grimes     /*
1489b50d902SRodney W. Grimes      *	are we already busy?
1499b50d902SRodney W. Grimes      */
1509b50d902SRodney W. Grimes bool
1519b50d902SRodney W. Grimes dfn_busy( childp )
1529b50d902SRodney W. Grimes     nltype	*childp;
1539b50d902SRodney W. Grimes {
1549b50d902SRodney W. Grimes 
1559b50d902SRodney W. Grimes     if ( childp -> toporder == DFN_NAN ) {
1569b50d902SRodney W. Grimes 	return FALSE;
1579b50d902SRodney W. Grimes     }
1589b50d902SRodney W. Grimes     return TRUE;
1599b50d902SRodney W. Grimes }
1609b50d902SRodney W. Grimes 
1619b50d902SRodney W. Grimes     /*
1629b50d902SRodney W. Grimes      *	MISSING: an explanation
1639b50d902SRodney W. Grimes      */
16497fa9b77SPhilippe Charnier void
1659b50d902SRodney W. Grimes dfn_findcycle( childp )
1669b50d902SRodney W. Grimes     nltype	*childp;
1679b50d902SRodney W. Grimes {
1689b50d902SRodney W. Grimes     int		cycletop;
1699b50d902SRodney W. Grimes     nltype	*cycleheadp;
1709b50d902SRodney W. Grimes     nltype	*tailp;
1719b50d902SRodney W. Grimes     int		index;
1729b50d902SRodney W. Grimes 
1739b50d902SRodney W. Grimes     for ( cycletop = dfn_depth ; cycletop > 0 ; cycletop -= 1 ) {
1749b50d902SRodney W. Grimes 	cycleheadp = dfn_stack[ cycletop ].nlentryp;
1759b50d902SRodney W. Grimes 	if ( childp == cycleheadp ) {
1769b50d902SRodney W. Grimes 	    break;
1779b50d902SRodney W. Grimes 	}
1789b50d902SRodney W. Grimes 	if ( childp -> cyclehead != childp &&
1799b50d902SRodney W. Grimes 	    childp -> cyclehead == cycleheadp ) {
1809b50d902SRodney W. Grimes 	    break;
1819b50d902SRodney W. Grimes 	}
1829b50d902SRodney W. Grimes     }
18397fa9b77SPhilippe Charnier     if ( cycletop <= 0 )
18497fa9b77SPhilippe Charnier 	errx( 1 , "[dfn_findcycle] couldn't find head of cycle" );
1859b50d902SRodney W. Grimes #   ifdef DEBUG
1869b50d902SRodney W. Grimes 	if ( debug & DFNDEBUG ) {
1879b50d902SRodney W. Grimes 	    printf( "[dfn_findcycle] dfn_depth %d cycletop %d " ,
1889b50d902SRodney W. Grimes 		    dfn_depth , cycletop  );
1899b50d902SRodney W. Grimes 	    printname( cycleheadp );
1909b50d902SRodney W. Grimes 	    printf( "\n" );
1919b50d902SRodney W. Grimes 	}
1920fb7a0beSGarrett Wollman #   endif /* DEBUG */
1939b50d902SRodney W. Grimes     if ( cycletop == dfn_depth ) {
1949b50d902SRodney W. Grimes 	    /*
1959b50d902SRodney W. Grimes 	     *	this is previous function, e.g. this calls itself
1969b50d902SRodney W. Grimes 	     *	sort of boring
1979b50d902SRodney W. Grimes 	     */
1989b50d902SRodney W. Grimes 	dfn_self_cycle( childp );
1999b50d902SRodney W. Grimes     } else {
2009b50d902SRodney W. Grimes 	    /*
2019b50d902SRodney W. Grimes 	     *	glom intervening functions that aren't already
2029b50d902SRodney W. Grimes 	     *	glommed into this cycle.
2039b50d902SRodney W. Grimes 	     *	things have been glommed when their cyclehead field
2049b50d902SRodney W. Grimes 	     *	points to the head of the cycle they are glommed into.
2059b50d902SRodney W. Grimes 	     */
2069b50d902SRodney W. Grimes 	for ( tailp = cycleheadp ; tailp -> cnext ; tailp = tailp -> cnext ) {
2079b50d902SRodney W. Grimes 	    /* void: chase down to tail of things already glommed */
2089b50d902SRodney W. Grimes #	    ifdef DEBUG
2099b50d902SRodney W. Grimes 		if ( debug & DFNDEBUG ) {
2109b50d902SRodney W. Grimes 		    printf( "[dfn_findcycle] tail " );
2119b50d902SRodney W. Grimes 		    printname( tailp );
2129b50d902SRodney W. Grimes 		    printf( "\n" );
2139b50d902SRodney W. Grimes 		}
2140fb7a0beSGarrett Wollman #	    endif /* DEBUG */
2159b50d902SRodney W. Grimes 	}
2169b50d902SRodney W. Grimes 	    /*
2179b50d902SRodney W. Grimes 	     *	if what we think is the top of the cycle
2189b50d902SRodney W. Grimes 	     *	has a cyclehead field, then it's not really the
2199b50d902SRodney W. Grimes 	     *	head of the cycle, which is really what we want
2209b50d902SRodney W. Grimes 	     */
2219b50d902SRodney W. Grimes 	if ( cycleheadp -> cyclehead != cycleheadp ) {
2229b50d902SRodney W. Grimes 	    cycleheadp = cycleheadp -> cyclehead;
2239b50d902SRodney W. Grimes #	    ifdef DEBUG
2249b50d902SRodney W. Grimes 		if ( debug & DFNDEBUG ) {
2259b50d902SRodney W. Grimes 		    printf( "[dfn_findcycle] new cyclehead " );
2269b50d902SRodney W. Grimes 		    printname( cycleheadp );
2279b50d902SRodney W. Grimes 		    printf( "\n" );
2289b50d902SRodney W. Grimes 		}
2290fb7a0beSGarrett Wollman #	    endif /* DEBUG */
2309b50d902SRodney W. Grimes 	}
2319b50d902SRodney W. Grimes 	for ( index = cycletop + 1 ; index <= dfn_depth ; index += 1 ) {
2329b50d902SRodney W. Grimes 	    childp = dfn_stack[ index ].nlentryp;
2339b50d902SRodney W. Grimes 	    if ( childp -> cyclehead == childp ) {
2349b50d902SRodney W. Grimes 		    /*
2359b50d902SRodney W. Grimes 		     *	not yet glommed anywhere, glom it
2369b50d902SRodney W. Grimes 		     *	and fix any children it has glommed
2379b50d902SRodney W. Grimes 		     */
2389b50d902SRodney W. Grimes 		tailp -> cnext = childp;
2399b50d902SRodney W. Grimes 		childp -> cyclehead = cycleheadp;
2409b50d902SRodney W. Grimes #		ifdef DEBUG
2419b50d902SRodney W. Grimes 		    if ( debug & DFNDEBUG ) {
2429b50d902SRodney W. Grimes 			printf( "[dfn_findcycle] glomming " );
2439b50d902SRodney W. Grimes 			printname( childp );
2449b50d902SRodney W. Grimes 			printf( " onto " );
2459b50d902SRodney W. Grimes 			printname( cycleheadp );
2469b50d902SRodney W. Grimes 			printf( "\n" );
2479b50d902SRodney W. Grimes 		    }
2480fb7a0beSGarrett Wollman #		endif /* DEBUG */
2499b50d902SRodney W. Grimes 		for ( tailp = childp ; tailp->cnext ; tailp = tailp->cnext ) {
2509b50d902SRodney W. Grimes 		    tailp -> cnext -> cyclehead = cycleheadp;
2519b50d902SRodney W. Grimes #		    ifdef DEBUG
2529b50d902SRodney W. Grimes 			if ( debug & DFNDEBUG ) {
2539b50d902SRodney W. Grimes 			    printf( "[dfn_findcycle] and its tail " );
2549b50d902SRodney W. Grimes 			    printname( tailp -> cnext );
2559b50d902SRodney W. Grimes 			    printf( " onto " );
2569b50d902SRodney W. Grimes 			    printname( cycleheadp );
2579b50d902SRodney W. Grimes 			    printf( "\n" );
2589b50d902SRodney W. Grimes 			}
2590fb7a0beSGarrett Wollman #		    endif /* DEBUG */
2609b50d902SRodney W. Grimes 		}
2619b50d902SRodney W. Grimes 	    } else if ( childp -> cyclehead != cycleheadp /* firewall */ ) {
2629b50d902SRodney W. Grimes 		fprintf( stderr ,
2639b50d902SRodney W. Grimes 			"[dfn_busy] glommed, but not to cyclehead\n" );
2649b50d902SRodney W. Grimes 	    }
2659b50d902SRodney W. Grimes 	}
2669b50d902SRodney W. Grimes     }
2679b50d902SRodney W. Grimes }
2689b50d902SRodney W. Grimes 
2699b50d902SRodney W. Grimes     /*
2709b50d902SRodney W. Grimes      *	deal with self-cycles
2719b50d902SRodney W. Grimes      *	for lint: ARGSUSED
2729b50d902SRodney W. Grimes      */
27397fa9b77SPhilippe Charnier void
2749b50d902SRodney W. Grimes dfn_self_cycle( parentp )
2759b50d902SRodney W. Grimes     nltype	*parentp;
2769b50d902SRodney W. Grimes {
2779b50d902SRodney W. Grimes 	/*
2789b50d902SRodney W. Grimes 	 *	since we are taking out self-cycles elsewhere
2799b50d902SRodney W. Grimes 	 *	no need for the special case, here.
2809b50d902SRodney W. Grimes 	 */
2819b50d902SRodney W. Grimes #   ifdef DEBUG
2829b50d902SRodney W. Grimes 	if ( debug & DFNDEBUG ) {
2839b50d902SRodney W. Grimes 	    printf( "[dfn_self_cycle] " );
2849b50d902SRodney W. Grimes 	    printname( parentp );
2859b50d902SRodney W. Grimes 	    printf( "\n" );
2869b50d902SRodney W. Grimes 	}
2870fb7a0beSGarrett Wollman #   endif /* DEBUG */
2889b50d902SRodney W. Grimes }
2899b50d902SRodney W. Grimes 
2909b50d902SRodney W. Grimes     /*
2919b50d902SRodney W. Grimes      *	visit a node after all its children
2929b50d902SRodney W. Grimes      *	[MISSING: an explanation]
2939b50d902SRodney W. Grimes      *	and pop it off the stack
2949b50d902SRodney W. Grimes      */
29597fa9b77SPhilippe Charnier void
2969b50d902SRodney W. Grimes dfn_post_visit( parentp )
2979b50d902SRodney W. Grimes     nltype	*parentp;
2989b50d902SRodney W. Grimes {
2999b50d902SRodney W. Grimes     nltype	*memberp;
3009b50d902SRodney W. Grimes 
3019b50d902SRodney W. Grimes #   ifdef DEBUG
3029b50d902SRodney W. Grimes 	if ( debug & DFNDEBUG ) {
3039b50d902SRodney W. Grimes 	    printf( "[dfn_post_visit]\t%d: " , dfn_depth );
3049b50d902SRodney W. Grimes 	    printname( parentp );
3059b50d902SRodney W. Grimes 	    printf( "\n" );
3069b50d902SRodney W. Grimes 	}
3070fb7a0beSGarrett Wollman #   endif /* DEBUG */
3089b50d902SRodney W. Grimes 	/*
3099b50d902SRodney W. Grimes 	 *	number functions and things in their cycles
3109b50d902SRodney W. Grimes 	 *	unless the function is itself part of a cycle
3119b50d902SRodney W. Grimes 	 */
3129b50d902SRodney W. Grimes     if ( parentp -> cyclehead == parentp ) {
3139b50d902SRodney W. Grimes 	dfn_counter += 1;
3149b50d902SRodney W. Grimes 	for ( memberp = parentp ; memberp ; memberp = memberp -> cnext ) {
3159b50d902SRodney W. Grimes 	    memberp -> toporder = dfn_counter;
3169b50d902SRodney W. Grimes #	    ifdef DEBUG
3179b50d902SRodney W. Grimes 		if ( debug & DFNDEBUG ) {
3189b50d902SRodney W. Grimes 		    printf( "[dfn_post_visit]\t\tmember " );
3199b50d902SRodney W. Grimes 		    printname( memberp );
3209b50d902SRodney W. Grimes 		    printf( " -> toporder = %d\n" , dfn_counter );
3219b50d902SRodney W. Grimes 		}
3220fb7a0beSGarrett Wollman #	    endif /* DEBUG */
3239b50d902SRodney W. Grimes 	}
3249b50d902SRodney W. Grimes     } else {
3259b50d902SRodney W. Grimes #	ifdef DEBUG
3269b50d902SRodney W. Grimes 	    if ( debug & DFNDEBUG ) {
3279b50d902SRodney W. Grimes 		printf( "[dfn_post_visit]\t\tis part of a cycle\n" );
3289b50d902SRodney W. Grimes 	    }
3290fb7a0beSGarrett Wollman #	endif /* DEBUG */
3309b50d902SRodney W. Grimes     }
3319b50d902SRodney W. Grimes     dfn_depth -= 1;
3329b50d902SRodney W. Grimes }
333