xref: /freebsd/usr.bin/gprof/dfn.c (revision 5e3934b1)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1983, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
32e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
3397fa9b77SPhilippe Charnier #include <err.h>
349b50d902SRodney W. Grimes #include "gprof.h"
359b50d902SRodney W. Grimes 
369b50d902SRodney W. Grimes #define	DFN_DEPTH	100
379b50d902SRodney W. Grimes struct dfnstruct {
389b50d902SRodney W. Grimes     nltype	*nlentryp;
399b50d902SRodney W. Grimes     int		cycletop;
409b50d902SRodney W. Grimes };
419b50d902SRodney W. Grimes typedef struct dfnstruct	dfntype;
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes dfntype	dfn_stack[ DFN_DEPTH ];
449b50d902SRodney W. Grimes int	dfn_depth;
459b50d902SRodney W. Grimes 
469b50d902SRodney W. Grimes int	dfn_counter;
479b50d902SRodney W. Grimes 
4897fa9b77SPhilippe Charnier void
dfn_init(void)49af2637dbSPhilippe Charnier dfn_init(void)
509b50d902SRodney W. Grimes {
519b50d902SRodney W. Grimes 
529b50d902SRodney W. Grimes     dfn_depth = 0;
539b50d902SRodney W. Grimes     dfn_counter = DFN_NAN;
549b50d902SRodney W. Grimes }
559b50d902SRodney W. Grimes 
569b50d902SRodney W. Grimes     /*
579b50d902SRodney W. Grimes      *	given this parent, depth first number its children.
589b50d902SRodney W. Grimes      */
5997fa9b77SPhilippe Charnier void
dfn(nltype * parentp)60af2637dbSPhilippe Charnier dfn(nltype *parentp)
619b50d902SRodney W. Grimes {
629b50d902SRodney W. Grimes     arctype	*arcp;
639b50d902SRodney W. Grimes 
649b50d902SRodney W. Grimes #   ifdef DEBUG
659b50d902SRodney W. Grimes 	if ( debug & DFNDEBUG ) {
669b50d902SRodney W. Grimes 	    printf( "[dfn] dfn(" );
679b50d902SRodney W. Grimes 	    printname( parentp );
689b50d902SRodney W. Grimes 	    printf( ")\n" );
699b50d902SRodney W. Grimes 	}
700fb7a0beSGarrett Wollman #   endif /* DEBUG */
719b50d902SRodney W. Grimes 	/*
7297fa9b77SPhilippe Charnier 	 *	if we're already numbered, no need to look any further.
739b50d902SRodney W. Grimes 	 */
749b50d902SRodney W. Grimes     if ( dfn_numbered( parentp ) ) {
759b50d902SRodney W. Grimes 	return;
769b50d902SRodney W. Grimes     }
779b50d902SRodney W. Grimes 	/*
789b50d902SRodney W. Grimes 	 *	if we're already busy, must be a cycle
799b50d902SRodney W. Grimes 	 */
809b50d902SRodney W. Grimes     if ( dfn_busy( parentp ) ) {
819b50d902SRodney W. Grimes 	dfn_findcycle( parentp );
829b50d902SRodney W. Grimes 	return;
839b50d902SRodney W. Grimes     }
849b50d902SRodney W. Grimes 	/*
859b50d902SRodney W. Grimes 	 *	visit yourself before your children
869b50d902SRodney W. Grimes 	 */
879b50d902SRodney W. Grimes     dfn_pre_visit( parentp );
889b50d902SRodney W. Grimes 	/*
899b50d902SRodney W. Grimes 	 *	visit children
909b50d902SRodney W. Grimes 	 */
919b50d902SRodney W. Grimes     for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
929b50d902SRodney W. Grimes 	    if ( arcp -> arc_flags & DEADARC )
939b50d902SRodney W. Grimes 		continue;
949b50d902SRodney W. Grimes 	    dfn( arcp -> arc_childp );
959b50d902SRodney W. Grimes     }
969b50d902SRodney W. Grimes 	/*
979b50d902SRodney W. Grimes 	 *	visit yourself after your children
989b50d902SRodney W. Grimes 	 */
999b50d902SRodney W. Grimes     dfn_post_visit( parentp );
1009b50d902SRodney W. Grimes }
1019b50d902SRodney W. Grimes 
1029b50d902SRodney W. Grimes     /*
1039b50d902SRodney W. Grimes      *	push a parent onto the stack and mark it busy
1049b50d902SRodney W. Grimes      */
10597fa9b77SPhilippe Charnier void
dfn_pre_visit(nltype * parentp)106af2637dbSPhilippe Charnier dfn_pre_visit(nltype *parentp)
1079b50d902SRodney W. Grimes {
1089b50d902SRodney W. Grimes 
1099b50d902SRodney W. Grimes     dfn_depth += 1;
11097fa9b77SPhilippe Charnier     if ( dfn_depth >= DFN_DEPTH )
11197fa9b77SPhilippe Charnier 	errx( 1 , "[dfn] out of my depth (dfn_stack overflow)" );
1129b50d902SRodney W. Grimes     dfn_stack[ dfn_depth ].nlentryp = parentp;
1139b50d902SRodney W. Grimes     dfn_stack[ dfn_depth ].cycletop = dfn_depth;
1149b50d902SRodney W. Grimes     parentp -> toporder = DFN_BUSY;
1159b50d902SRodney W. Grimes #   ifdef DEBUG
1169b50d902SRodney W. Grimes 	if ( debug & DFNDEBUG ) {
1179b50d902SRodney W. Grimes 	    printf( "[dfn_pre_visit]\t\t%d:" , dfn_depth );
1189b50d902SRodney W. Grimes 	    printname( parentp );
1199b50d902SRodney W. Grimes 	    printf( "\n" );
1209b50d902SRodney W. Grimes 	}
1210fb7a0beSGarrett Wollman #   endif /* DEBUG */
1229b50d902SRodney W. Grimes }
1239b50d902SRodney W. Grimes 
1249b50d902SRodney W. Grimes     /*
1259b50d902SRodney W. Grimes      *	are we already numbered?
1269b50d902SRodney W. Grimes      */
1279b50d902SRodney W. Grimes bool
dfn_numbered(nltype * childp)128af2637dbSPhilippe Charnier dfn_numbered(nltype *childp)
1299b50d902SRodney W. Grimes {
1309b50d902SRodney W. Grimes 
1319b50d902SRodney W. Grimes     return ( childp -> toporder != DFN_NAN && childp -> toporder != DFN_BUSY );
1329b50d902SRodney W. Grimes }
1339b50d902SRodney W. Grimes 
1349b50d902SRodney W. Grimes     /*
1359b50d902SRodney W. Grimes      *	are we already busy?
1369b50d902SRodney W. Grimes      */
1379b50d902SRodney W. Grimes bool
dfn_busy(nltype * childp)138af2637dbSPhilippe Charnier dfn_busy(nltype *childp)
1399b50d902SRodney W. Grimes {
1409b50d902SRodney W. Grimes 
1419b50d902SRodney W. Grimes     if ( childp -> toporder == DFN_NAN ) {
1429b50d902SRodney W. Grimes 	return FALSE;
1439b50d902SRodney W. Grimes     }
1449b50d902SRodney W. Grimes     return TRUE;
1459b50d902SRodney W. Grimes }
1469b50d902SRodney W. Grimes 
1479b50d902SRodney W. Grimes     /*
1489b50d902SRodney W. Grimes      *	MISSING: an explanation
1499b50d902SRodney W. Grimes      */
15097fa9b77SPhilippe Charnier void
dfn_findcycle(nltype * childp)151af2637dbSPhilippe Charnier dfn_findcycle(nltype *childp)
1529b50d902SRodney W. Grimes {
1539b50d902SRodney W. Grimes     int		cycletop;
1549b50d902SRodney W. Grimes     nltype	*cycleheadp;
1559b50d902SRodney W. Grimes     nltype	*tailp;
1569b50d902SRodney W. Grimes     int		index;
1579b50d902SRodney W. Grimes 
1589b50d902SRodney W. Grimes     for ( cycletop = dfn_depth ; cycletop > 0 ; cycletop -= 1 ) {
1599b50d902SRodney W. Grimes 	cycleheadp = dfn_stack[ cycletop ].nlentryp;
1609b50d902SRodney W. Grimes 	if ( childp == cycleheadp ) {
1619b50d902SRodney W. Grimes 	    break;
1629b50d902SRodney W. Grimes 	}
1639b50d902SRodney W. Grimes 	if ( childp -> cyclehead != childp &&
1649b50d902SRodney W. Grimes 	    childp -> cyclehead == cycleheadp ) {
1659b50d902SRodney W. Grimes 	    break;
1669b50d902SRodney W. Grimes 	}
1679b50d902SRodney W. Grimes     }
16897fa9b77SPhilippe Charnier     if ( cycletop <= 0 )
16997fa9b77SPhilippe Charnier 	errx( 1 , "[dfn_findcycle] couldn't find head of cycle" );
1709b50d902SRodney W. Grimes #   ifdef DEBUG
1719b50d902SRodney W. Grimes 	if ( debug & DFNDEBUG ) {
1729b50d902SRodney W. Grimes 	    printf( "[dfn_findcycle] dfn_depth %d cycletop %d " ,
1739b50d902SRodney W. Grimes 		    dfn_depth , cycletop  );
1749b50d902SRodney W. Grimes 	    printname( cycleheadp );
1759b50d902SRodney W. Grimes 	    printf( "\n" );
1769b50d902SRodney W. Grimes 	}
1770fb7a0beSGarrett Wollman #   endif /* DEBUG */
1789b50d902SRodney W. Grimes     if ( cycletop == dfn_depth ) {
1799b50d902SRodney W. Grimes 	    /*
1809b50d902SRodney W. Grimes 	     *	this is previous function, e.g. this calls itself
1819b50d902SRodney W. Grimes 	     *	sort of boring
1829b50d902SRodney W. Grimes 	     */
1839b50d902SRodney W. Grimes 	dfn_self_cycle( childp );
1849b50d902SRodney W. Grimes     } else {
1859b50d902SRodney W. Grimes 	    /*
1869b50d902SRodney W. Grimes 	     *	glom intervening functions that aren't already
1879b50d902SRodney W. Grimes 	     *	glommed into this cycle.
1889b50d902SRodney W. Grimes 	     *	things have been glommed when their cyclehead field
1899b50d902SRodney W. Grimes 	     *	points to the head of the cycle they are glommed into.
1909b50d902SRodney W. Grimes 	     */
1919b50d902SRodney W. Grimes 	for ( tailp = cycleheadp ; tailp -> cnext ; tailp = tailp -> cnext ) {
1929b50d902SRodney W. Grimes 	    /* void: chase down to tail of things already glommed */
1939b50d902SRodney W. Grimes #	    ifdef DEBUG
1949b50d902SRodney W. Grimes 		if ( debug & DFNDEBUG ) {
1959b50d902SRodney W. Grimes 		    printf( "[dfn_findcycle] tail " );
1969b50d902SRodney W. Grimes 		    printname( tailp );
1979b50d902SRodney W. Grimes 		    printf( "\n" );
1989b50d902SRodney W. Grimes 		}
1990fb7a0beSGarrett Wollman #	    endif /* DEBUG */
2009b50d902SRodney W. Grimes 	}
2019b50d902SRodney W. Grimes 	    /*
2029b50d902SRodney W. Grimes 	     *	if what we think is the top of the cycle
2039b50d902SRodney W. Grimes 	     *	has a cyclehead field, then it's not really the
2049b50d902SRodney W. Grimes 	     *	head of the cycle, which is really what we want
2059b50d902SRodney W. Grimes 	     */
2069b50d902SRodney W. Grimes 	if ( cycleheadp -> cyclehead != cycleheadp ) {
2079b50d902SRodney W. Grimes 	    cycleheadp = cycleheadp -> cyclehead;
2089b50d902SRodney W. Grimes #	    ifdef DEBUG
2099b50d902SRodney W. Grimes 		if ( debug & DFNDEBUG ) {
2109b50d902SRodney W. Grimes 		    printf( "[dfn_findcycle] new cyclehead " );
2119b50d902SRodney W. Grimes 		    printname( cycleheadp );
2129b50d902SRodney W. Grimes 		    printf( "\n" );
2139b50d902SRodney W. Grimes 		}
2140fb7a0beSGarrett Wollman #	    endif /* DEBUG */
2159b50d902SRodney W. Grimes 	}
2169b50d902SRodney W. Grimes 	for ( index = cycletop + 1 ; index <= dfn_depth ; index += 1 ) {
2179b50d902SRodney W. Grimes 	    childp = dfn_stack[ index ].nlentryp;
2189b50d902SRodney W. Grimes 	    if ( childp -> cyclehead == childp ) {
2199b50d902SRodney W. Grimes 		    /*
2209b50d902SRodney W. Grimes 		     *	not yet glommed anywhere, glom it
2219b50d902SRodney W. Grimes 		     *	and fix any children it has glommed
2229b50d902SRodney W. Grimes 		     */
2239b50d902SRodney W. Grimes 		tailp -> cnext = childp;
2249b50d902SRodney W. Grimes 		childp -> cyclehead = cycleheadp;
2259b50d902SRodney W. Grimes #		ifdef DEBUG
2269b50d902SRodney W. Grimes 		    if ( debug & DFNDEBUG ) {
2279b50d902SRodney W. Grimes 			printf( "[dfn_findcycle] glomming " );
2289b50d902SRodney W. Grimes 			printname( childp );
2299b50d902SRodney W. Grimes 			printf( " onto " );
2309b50d902SRodney W. Grimes 			printname( cycleheadp );
2319b50d902SRodney W. Grimes 			printf( "\n" );
2329b50d902SRodney W. Grimes 		    }
2330fb7a0beSGarrett Wollman #		endif /* DEBUG */
2349b50d902SRodney W. Grimes 		for ( tailp = childp ; tailp->cnext ; tailp = tailp->cnext ) {
2359b50d902SRodney W. Grimes 		    tailp -> cnext -> cyclehead = cycleheadp;
2369b50d902SRodney W. Grimes #		    ifdef DEBUG
2379b50d902SRodney W. Grimes 			if ( debug & DFNDEBUG ) {
2389b50d902SRodney W. Grimes 			    printf( "[dfn_findcycle] and its tail " );
2399b50d902SRodney W. Grimes 			    printname( tailp -> cnext );
2409b50d902SRodney W. Grimes 			    printf( " onto " );
2419b50d902SRodney W. Grimes 			    printname( cycleheadp );
2429b50d902SRodney W. Grimes 			    printf( "\n" );
2439b50d902SRodney W. Grimes 			}
2440fb7a0beSGarrett Wollman #		    endif /* DEBUG */
2459b50d902SRodney W. Grimes 		}
2469b50d902SRodney W. Grimes 	    } else if ( childp -> cyclehead != cycleheadp /* firewall */ ) {
2479b50d902SRodney W. Grimes 		fprintf( stderr ,
2489b50d902SRodney W. Grimes 			"[dfn_busy] glommed, but not to cyclehead\n" );
2499b50d902SRodney W. Grimes 	    }
2509b50d902SRodney W. Grimes 	}
2519b50d902SRodney W. Grimes     }
2529b50d902SRodney W. Grimes }
2539b50d902SRodney W. Grimes 
2549b50d902SRodney W. Grimes     /*
2559b50d902SRodney W. Grimes      *	deal with self-cycles
2569b50d902SRodney W. Grimes      *	for lint: ARGSUSED
2579b50d902SRodney W. Grimes      */
25897fa9b77SPhilippe Charnier void
dfn_self_cycle(nltype * parentp)259af2637dbSPhilippe Charnier dfn_self_cycle(nltype *parentp)
2609b50d902SRodney W. Grimes {
2619b50d902SRodney W. Grimes 	/*
2629b50d902SRodney W. Grimes 	 *	since we are taking out self-cycles elsewhere
2639b50d902SRodney W. Grimes 	 *	no need for the special case, here.
2649b50d902SRodney W. Grimes 	 */
2659b50d902SRodney W. Grimes #   ifdef DEBUG
2669b50d902SRodney W. Grimes 	if ( debug & DFNDEBUG ) {
2679b50d902SRodney W. Grimes 	    printf( "[dfn_self_cycle] " );
2689b50d902SRodney W. Grimes 	    printname( parentp );
2699b50d902SRodney W. Grimes 	    printf( "\n" );
2709b50d902SRodney W. Grimes 	}
2710fb7a0beSGarrett Wollman #   endif /* DEBUG */
2729b50d902SRodney W. Grimes }
2739b50d902SRodney W. Grimes 
2749b50d902SRodney W. Grimes     /*
2759b50d902SRodney W. Grimes      *	visit a node after all its children
2769b50d902SRodney W. Grimes      *	[MISSING: an explanation]
2779b50d902SRodney W. Grimes      *	and pop it off the stack
2789b50d902SRodney W. Grimes      */
27997fa9b77SPhilippe Charnier void
dfn_post_visit(nltype * parentp)280af2637dbSPhilippe Charnier dfn_post_visit(nltype *parentp)
2819b50d902SRodney W. Grimes {
2829b50d902SRodney W. Grimes     nltype	*memberp;
2839b50d902SRodney W. Grimes 
2849b50d902SRodney W. Grimes #   ifdef DEBUG
2859b50d902SRodney W. Grimes 	if ( debug & DFNDEBUG ) {
2869b50d902SRodney W. Grimes 	    printf( "[dfn_post_visit]\t%d: " , dfn_depth );
2879b50d902SRodney W. Grimes 	    printname( parentp );
2889b50d902SRodney W. Grimes 	    printf( "\n" );
2899b50d902SRodney W. Grimes 	}
2900fb7a0beSGarrett Wollman #   endif /* DEBUG */
2919b50d902SRodney W. Grimes 	/*
2929b50d902SRodney W. Grimes 	 *	number functions and things in their cycles
2939b50d902SRodney W. Grimes 	 *	unless the function is itself part of a cycle
2949b50d902SRodney W. Grimes 	 */
2959b50d902SRodney W. Grimes     if ( parentp -> cyclehead == parentp ) {
2969b50d902SRodney W. Grimes 	dfn_counter += 1;
2979b50d902SRodney W. Grimes 	for ( memberp = parentp ; memberp ; memberp = memberp -> cnext ) {
2989b50d902SRodney W. Grimes 	    memberp -> toporder = dfn_counter;
2999b50d902SRodney W. Grimes #	    ifdef DEBUG
3009b50d902SRodney W. Grimes 		if ( debug & DFNDEBUG ) {
3019b50d902SRodney W. Grimes 		    printf( "[dfn_post_visit]\t\tmember " );
3029b50d902SRodney W. Grimes 		    printname( memberp );
3039b50d902SRodney W. Grimes 		    printf( " -> toporder = %d\n" , dfn_counter );
3049b50d902SRodney W. Grimes 		}
3050fb7a0beSGarrett Wollman #	    endif /* DEBUG */
3069b50d902SRodney W. Grimes 	}
3079b50d902SRodney W. Grimes     } else {
3089b50d902SRodney W. Grimes #	ifdef DEBUG
3099b50d902SRodney W. Grimes 	    if ( debug & DFNDEBUG ) {
3109b50d902SRodney W. Grimes 		printf( "[dfn_post_visit]\t\tis part of a cycle\n" );
3119b50d902SRodney W. Grimes 	    }
3120fb7a0beSGarrett Wollman #	endif /* DEBUG */
3139b50d902SRodney W. Grimes     }
3149b50d902SRodney W. Grimes     dfn_depth -= 1;
3159b50d902SRodney W. Grimes }
316