static char *sccsid
= "@(#)dfn.c 1.2 (Berkeley) %G%";
typedef struct dfnstruct dfntype
;
dfntype dfn_stack
[ DFN_DEPTH
];
* 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
!= 0 && childp
-> toporder
!= DFN_BUSY
);
if ( childp
-> toporder
== 0 ) {
* 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" );