11590Srgrimes/* 21590Srgrimes * Copyright (c) 1983, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 4. Neither the name of the University nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes */ 291590Srgrimes 30105243Scharnier#if 0 311590Srgrimes#ifndef lint 321590Srgrimesstatic char sccsid[] = "@(#)dfn.c 8.1 (Berkeley) 6/6/93"; 33105243Scharnier#endif /* not lint */ 3497631Swollman#endif 35105243Scharnier 3699112Sobrien#include <sys/cdefs.h> 3799112Sobrien__FBSDID("$FreeBSD: releng/10.3/usr.bin/gprof/dfn.c 246783 2013-02-14 08:16:03Z charnier $"); 381590Srgrimes 39105243Scharnier#include <err.h> 401590Srgrimes#include "gprof.h" 411590Srgrimes 421590Srgrimes#define DFN_DEPTH 100 431590Srgrimesstruct dfnstruct { 441590Srgrimes nltype *nlentryp; 451590Srgrimes int cycletop; 461590Srgrimes}; 471590Srgrimestypedef struct dfnstruct dfntype; 481590Srgrimes 491590Srgrimesdfntype dfn_stack[ DFN_DEPTH ]; 501590Srgrimesint dfn_depth; 511590Srgrimes 521590Srgrimesint dfn_counter; 531590Srgrimes 54105243Scharniervoid 55246783Scharnierdfn_init(void) 561590Srgrimes{ 571590Srgrimes 581590Srgrimes dfn_depth = 0; 591590Srgrimes dfn_counter = DFN_NAN; 601590Srgrimes} 611590Srgrimes 621590Srgrimes /* 631590Srgrimes * given this parent, depth first number its children. 641590Srgrimes */ 65105243Scharniervoid 66246783Scharnierdfn(nltype *parentp) 671590Srgrimes{ 681590Srgrimes arctype *arcp; 691590Srgrimes 701590Srgrimes# ifdef DEBUG 711590Srgrimes if ( debug & DFNDEBUG ) { 721590Srgrimes printf( "[dfn] dfn(" ); 731590Srgrimes printname( parentp ); 741590Srgrimes printf( ")\n" ); 751590Srgrimes } 7697631Swollman# endif /* DEBUG */ 771590Srgrimes /* 78105243Scharnier * if we're already numbered, no need to look any further. 791590Srgrimes */ 801590Srgrimes if ( dfn_numbered( parentp ) ) { 811590Srgrimes return; 821590Srgrimes } 831590Srgrimes /* 841590Srgrimes * if we're already busy, must be a cycle 851590Srgrimes */ 861590Srgrimes if ( dfn_busy( parentp ) ) { 871590Srgrimes dfn_findcycle( parentp ); 881590Srgrimes return; 891590Srgrimes } 901590Srgrimes /* 911590Srgrimes * visit yourself before your children 921590Srgrimes */ 931590Srgrimes dfn_pre_visit( parentp ); 941590Srgrimes /* 951590Srgrimes * visit children 961590Srgrimes */ 971590Srgrimes for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) { 981590Srgrimes if ( arcp -> arc_flags & DEADARC ) 991590Srgrimes continue; 1001590Srgrimes dfn( arcp -> arc_childp ); 1011590Srgrimes } 1021590Srgrimes /* 1031590Srgrimes * visit yourself after your children 1041590Srgrimes */ 1051590Srgrimes dfn_post_visit( parentp ); 1061590Srgrimes} 1071590Srgrimes 1081590Srgrimes /* 1091590Srgrimes * push a parent onto the stack and mark it busy 1101590Srgrimes */ 111105243Scharniervoid 112246783Scharnierdfn_pre_visit(nltype *parentp) 1131590Srgrimes{ 1141590Srgrimes 1151590Srgrimes dfn_depth += 1; 116105243Scharnier if ( dfn_depth >= DFN_DEPTH ) 117105243Scharnier errx( 1 , "[dfn] out of my depth (dfn_stack overflow)" ); 1181590Srgrimes dfn_stack[ dfn_depth ].nlentryp = parentp; 1191590Srgrimes dfn_stack[ dfn_depth ].cycletop = dfn_depth; 1201590Srgrimes parentp -> toporder = DFN_BUSY; 1211590Srgrimes# ifdef DEBUG 1221590Srgrimes if ( debug & DFNDEBUG ) { 1231590Srgrimes printf( "[dfn_pre_visit]\t\t%d:" , dfn_depth ); 1241590Srgrimes printname( parentp ); 1251590Srgrimes printf( "\n" ); 1261590Srgrimes } 12797631Swollman# endif /* DEBUG */ 1281590Srgrimes} 1291590Srgrimes 1301590Srgrimes /* 1311590Srgrimes * are we already numbered? 1321590Srgrimes */ 1331590Srgrimesbool 134246783Scharnierdfn_numbered(nltype *childp) 1351590Srgrimes{ 1368874Srgrimes 1371590Srgrimes return ( childp -> toporder != DFN_NAN && childp -> toporder != DFN_BUSY ); 1381590Srgrimes} 1391590Srgrimes 1401590Srgrimes /* 1411590Srgrimes * are we already busy? 1421590Srgrimes */ 1431590Srgrimesbool 144246783Scharnierdfn_busy(nltype *childp) 1451590Srgrimes{ 1461590Srgrimes 1471590Srgrimes if ( childp -> toporder == DFN_NAN ) { 1481590Srgrimes return FALSE; 1491590Srgrimes } 1501590Srgrimes return TRUE; 1511590Srgrimes} 1521590Srgrimes 1531590Srgrimes /* 1541590Srgrimes * MISSING: an explanation 1551590Srgrimes */ 156105243Scharniervoid 157246783Scharnierdfn_findcycle(nltype *childp) 1581590Srgrimes{ 1591590Srgrimes int cycletop; 1601590Srgrimes nltype *cycleheadp; 1611590Srgrimes nltype *tailp; 1621590Srgrimes int index; 1631590Srgrimes 1641590Srgrimes for ( cycletop = dfn_depth ; cycletop > 0 ; cycletop -= 1 ) { 1651590Srgrimes cycleheadp = dfn_stack[ cycletop ].nlentryp; 1661590Srgrimes if ( childp == cycleheadp ) { 1671590Srgrimes break; 1681590Srgrimes } 1691590Srgrimes if ( childp -> cyclehead != childp && 1701590Srgrimes childp -> cyclehead == cycleheadp ) { 1711590Srgrimes break; 1721590Srgrimes } 1731590Srgrimes } 174105243Scharnier if ( cycletop <= 0 ) 175105243Scharnier errx( 1 , "[dfn_findcycle] couldn't find head of cycle" ); 1761590Srgrimes# ifdef DEBUG 1771590Srgrimes if ( debug & DFNDEBUG ) { 1781590Srgrimes printf( "[dfn_findcycle] dfn_depth %d cycletop %d " , 1791590Srgrimes dfn_depth , cycletop ); 1801590Srgrimes printname( cycleheadp ); 1811590Srgrimes printf( "\n" ); 1821590Srgrimes } 18397631Swollman# endif /* DEBUG */ 1841590Srgrimes if ( cycletop == dfn_depth ) { 1851590Srgrimes /* 1861590Srgrimes * this is previous function, e.g. this calls itself 1871590Srgrimes * sort of boring 1881590Srgrimes */ 1891590Srgrimes dfn_self_cycle( childp ); 1901590Srgrimes } else { 1911590Srgrimes /* 1921590Srgrimes * glom intervening functions that aren't already 1931590Srgrimes * glommed into this cycle. 1941590Srgrimes * things have been glommed when their cyclehead field 1951590Srgrimes * points to the head of the cycle they are glommed into. 1961590Srgrimes */ 1971590Srgrimes for ( tailp = cycleheadp ; tailp -> cnext ; tailp = tailp -> cnext ) { 1981590Srgrimes /* void: chase down to tail of things already glommed */ 1991590Srgrimes# ifdef DEBUG 2001590Srgrimes if ( debug & DFNDEBUG ) { 2011590Srgrimes printf( "[dfn_findcycle] tail " ); 2021590Srgrimes printname( tailp ); 2031590Srgrimes printf( "\n" ); 2041590Srgrimes } 20597631Swollman# endif /* DEBUG */ 2061590Srgrimes } 2071590Srgrimes /* 2081590Srgrimes * if what we think is the top of the cycle 2091590Srgrimes * has a cyclehead field, then it's not really the 2101590Srgrimes * head of the cycle, which is really what we want 2118874Srgrimes */ 2121590Srgrimes if ( cycleheadp -> cyclehead != cycleheadp ) { 2131590Srgrimes cycleheadp = cycleheadp -> cyclehead; 2141590Srgrimes# ifdef DEBUG 2151590Srgrimes if ( debug & DFNDEBUG ) { 2161590Srgrimes printf( "[dfn_findcycle] new cyclehead " ); 2171590Srgrimes printname( cycleheadp ); 2181590Srgrimes printf( "\n" ); 2191590Srgrimes } 22097631Swollman# endif /* DEBUG */ 2211590Srgrimes } 2221590Srgrimes for ( index = cycletop + 1 ; index <= dfn_depth ; index += 1 ) { 2231590Srgrimes childp = dfn_stack[ index ].nlentryp; 2241590Srgrimes if ( childp -> cyclehead == childp ) { 2251590Srgrimes /* 2261590Srgrimes * not yet glommed anywhere, glom it 2271590Srgrimes * and fix any children it has glommed 2281590Srgrimes */ 2291590Srgrimes tailp -> cnext = childp; 2301590Srgrimes childp -> cyclehead = cycleheadp; 2311590Srgrimes# ifdef DEBUG 2321590Srgrimes if ( debug & DFNDEBUG ) { 2331590Srgrimes printf( "[dfn_findcycle] glomming " ); 2341590Srgrimes printname( childp ); 2351590Srgrimes printf( " onto " ); 2361590Srgrimes printname( cycleheadp ); 2371590Srgrimes printf( "\n" ); 2381590Srgrimes } 23997631Swollman# endif /* DEBUG */ 2401590Srgrimes for ( tailp = childp ; tailp->cnext ; tailp = tailp->cnext ) { 2411590Srgrimes tailp -> cnext -> cyclehead = cycleheadp; 2421590Srgrimes# ifdef DEBUG 2431590Srgrimes if ( debug & DFNDEBUG ) { 2441590Srgrimes printf( "[dfn_findcycle] and its tail " ); 2451590Srgrimes printname( tailp -> cnext ); 2461590Srgrimes printf( " onto " ); 2471590Srgrimes printname( cycleheadp ); 2481590Srgrimes printf( "\n" ); 2491590Srgrimes } 25097631Swollman# endif /* DEBUG */ 2511590Srgrimes } 2521590Srgrimes } else if ( childp -> cyclehead != cycleheadp /* firewall */ ) { 2531590Srgrimes fprintf( stderr , 2541590Srgrimes "[dfn_busy] glommed, but not to cyclehead\n" ); 2551590Srgrimes } 2561590Srgrimes } 2571590Srgrimes } 2581590Srgrimes} 2591590Srgrimes 2601590Srgrimes /* 2611590Srgrimes * deal with self-cycles 2621590Srgrimes * for lint: ARGSUSED 2631590Srgrimes */ 264105243Scharniervoid 265246783Scharnierdfn_self_cycle(nltype *parentp) 2661590Srgrimes{ 2671590Srgrimes /* 2681590Srgrimes * since we are taking out self-cycles elsewhere 2691590Srgrimes * no need for the special case, here. 2701590Srgrimes */ 2711590Srgrimes# ifdef DEBUG 2721590Srgrimes if ( debug & DFNDEBUG ) { 2731590Srgrimes printf( "[dfn_self_cycle] " ); 2741590Srgrimes printname( parentp ); 2751590Srgrimes printf( "\n" ); 2761590Srgrimes } 27797631Swollman# endif /* DEBUG */ 2781590Srgrimes} 2791590Srgrimes 2801590Srgrimes /* 2811590Srgrimes * visit a node after all its children 2821590Srgrimes * [MISSING: an explanation] 2831590Srgrimes * and pop it off the stack 2841590Srgrimes */ 285105243Scharniervoid 286246783Scharnierdfn_post_visit(nltype *parentp) 2871590Srgrimes{ 2881590Srgrimes nltype *memberp; 2891590Srgrimes 2901590Srgrimes# ifdef DEBUG 2911590Srgrimes if ( debug & DFNDEBUG ) { 2921590Srgrimes printf( "[dfn_post_visit]\t%d: " , dfn_depth ); 2931590Srgrimes printname( parentp ); 2941590Srgrimes printf( "\n" ); 2951590Srgrimes } 29697631Swollman# endif /* DEBUG */ 2971590Srgrimes /* 2981590Srgrimes * number functions and things in their cycles 2991590Srgrimes * unless the function is itself part of a cycle 3001590Srgrimes */ 3011590Srgrimes if ( parentp -> cyclehead == parentp ) { 3021590Srgrimes dfn_counter += 1; 3031590Srgrimes for ( memberp = parentp ; memberp ; memberp = memberp -> cnext ) { 3041590Srgrimes memberp -> toporder = dfn_counter; 3051590Srgrimes# ifdef DEBUG 3061590Srgrimes if ( debug & DFNDEBUG ) { 3071590Srgrimes printf( "[dfn_post_visit]\t\tmember " ); 3081590Srgrimes printname( memberp ); 3091590Srgrimes printf( " -> toporder = %d\n" , dfn_counter ); 3101590Srgrimes } 31197631Swollman# endif /* DEBUG */ 3121590Srgrimes } 3131590Srgrimes } else { 3141590Srgrimes# ifdef DEBUG 3151590Srgrimes if ( debug & DFNDEBUG ) { 3161590Srgrimes printf( "[dfn_post_visit]\t\tis part of a cycle\n" ); 3171590Srgrimes } 31897631Swollman# endif /* DEBUG */ 3191590Srgrimes } 3201590Srgrimes dfn_depth -= 1; 3211590Srgrimes} 322