* Copyright (c) 1983 Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)dfn.c 5.4 (Berkeley) 6/1/90";
typedef struct dfnstruct dfntype
;
dfntype dfn_stack
[ DFN_DEPTH
];
int dfn_counter
= DFN_NAN
;
* given this parent, depth first number its children.
if ( debug
& DFNDEBUG
) {
* if we're already numbered, no need to look any furthur.
if ( dfn_numbered( parentp
) ) {
* if we're already busy, must be a cycle
if ( dfn_busy( parentp
) ) {
dfn_findcycle( parentp
);
* visit yourself before your children
dfn_pre_visit( parentp
);
for ( arcp
= parentp
-> children
; arcp
; arcp
= arcp
-> arc_childlist
) {
dfn( arcp
-> arc_childp
);
* visit yourself after your children
dfn_post_visit( parentp
);
* push a parent onto the stack and mark it busy
if ( dfn_depth
>= DFN_DEPTH
) {
fprintf( stderr
, "[dfn] out of my depth (dfn_stack overflow)\n" );
dfn_stack
[ dfn_depth
].nlentryp
= parentp
;
dfn_stack
[ dfn_depth
].cycletop
= dfn_depth
;
parentp
-> toporder
= DFN_BUSY
;
if ( debug
& DFNDEBUG
) {
printf( "[dfn_pre_visit]\t\t%d:" , dfn_depth
);
* are we already numbered?
return ( childp
-> toporder
!= DFN_NAN
&& childp
-> toporder
!= DFN_BUSY
);
if ( childp
-> toporder
== DFN_NAN
) {
* MISSING: an explanation
for ( cycletop
= dfn_depth
; cycletop
> 0 ; cycletop
-= 1 ) {
cycleheadp
= dfn_stack
[ cycletop
].nlentryp
;
if ( childp
== cycleheadp
) {
if ( childp
-> cyclehead
!= childp
&&
childp
-> cyclehead
== cycleheadp
) {
fprintf( stderr
, "[dfn_findcycle] couldn't find head of cycle\n" );
if ( debug
& DFNDEBUG
) {
printf( "[dfn_findcycle] dfn_depth %d cycletop %d " ,
if ( cycletop
== dfn_depth
) {
* this is previous function, e.g. this calls itself
dfn_self_cycle( childp
);
* glom intervening functions that aren't already
* glommed into this cycle.
* things have been glommed when their cyclehead field
* points to the head of the cycle they are glommed into.
for ( tailp
= cycleheadp
; tailp
-> cnext
; tailp
= tailp
-> cnext
) {
/* void: chase down to tail of things already glommed */
if ( debug
& DFNDEBUG
) {
printf( "[dfn_findcycle] tail " );
* if what we think is the top of the cycle
* has a cyclehead field, then it's not really the
* head of the cycle, which is really what we want
if ( cycleheadp
-> cyclehead
!= cycleheadp
) {
cycleheadp
= cycleheadp
-> cyclehead
;
if ( debug
& DFNDEBUG
) {
printf( "[dfn_findcycle] new cyclehead " );
for ( index
= cycletop
+ 1 ; index
<= dfn_depth
; index
+= 1 ) {
childp
= dfn_stack
[ index
].nlentryp
;
if ( childp
-> cyclehead
== childp
) {
* not yet glommed anywhere, glom it
* and fix any children it has glommed
childp
-> cyclehead
= cycleheadp
;
if ( debug
& DFNDEBUG
) {
printf( "[dfn_findcycle] glomming " );
for ( tailp
= childp
; tailp
->cnext
; tailp
= tailp
->cnext
) {
tailp
-> cnext
-> cyclehead
= cycleheadp
;
if ( debug
& DFNDEBUG
) {
printf( "[dfn_findcycle] and its tail " );
printname( tailp
-> cnext
);
} else if ( childp
-> cyclehead
!= cycleheadp
/* firewall */ ) {
"[dfn_busy] glommed, but not to cyclehead\n" );
dfn_self_cycle( parentp
)
* since we are taking out self-cycles elsewhere
* no need for the special case, here.
if ( debug
& DFNDEBUG
) {
printf( "[dfn_self_cycle] " );
* visit a node after all its children
* [MISSING: an explanation]
* and pop it off the stack
dfn_post_visit( parentp
)
if ( debug
& DFNDEBUG
) {
printf( "[dfn_post_visit]\t%d: " , dfn_depth
);
* number functions and things in their cycles
* unless the function is itself part of a cycle
if ( parentp
-> cyclehead
== parentp
) {
for ( memberp
= parentp
; memberp
; memberp
= memberp
-> cnext
) {
memberp
-> toporder
= dfn_counter
;
if ( debug
& DFNDEBUG
) {
printf( "[dfn_post_visit]\t\tmember " );
printf( " -> toporder = %d\n" , dfn_counter
);
if ( debug
& DFNDEBUG
) {
printf( "[dfn_post_visit]\t\tis part of a cycle\n" );