2 * Copyright (c) 1983 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution
11 * and in all advertising materials mentioning features or use of this
12 * software. Neither the name of the University nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
35 DFN_Stack dfn_stack
[DFN_DEPTH
];
37 int dfn_counter
= DFN_NAN
;
41 * Is CHILD already numbered?
44 DEFUN (is_numbered
, (child
), Sym
* child
)
46 return child
->cg
.top_order
!= DFN_NAN
&& child
->cg
.top_order
!= DFN_BUSY
;
51 * Is CHILD already busy?
54 DEFUN (is_busy
, (child
), Sym
* child
)
56 if (child
->cg
.top_order
== DFN_NAN
)
65 * CHILD is part of a cycle. Find the top caller into this cycle
66 * that is not part of the cycle and make all functions in cycle
67 * members of that cycle (top caller == caller with smallest
68 * depth-first number).
71 DEFUN (find_cycle
, (child
), Sym
* child
)
78 for (cycle_top
= dfn_depth
; cycle_top
> 0; --cycle_top
)
80 head
= dfn_stack
[cycle_top
].sym
;
85 if (child
->cg
.cyc
.head
!= child
&& child
->cg
.cyc
.head
== head
)
92 fprintf (stderr
, "[find_cycle] couldn't find head of cycle\n");
96 if (debug_level
& DFNDEBUG
)
98 printf ("[find_cycle] dfn_depth %d cycle_top %d ",
99 dfn_depth
, cycle_top
);
106 printf ("<unknown>");
111 if (cycle_top
== dfn_depth
)
114 * This is previous function, e.g. this calls itself. Sort of
117 * Since we are taking out self-cycles elsewhere no need for
118 * the special case, here.
121 printf ("[find_cycle] ");
128 * Glom intervening functions that aren't already glommed into
129 * this cycle. Things have been glommed when their cyclehead
130 * field points to the head of the cycle they are glommed
133 for (tail
= head
; tail
->cg
.cyc
.next
; tail
= tail
->cg
.cyc
.next
)
135 /* void: chase down to tail of things already glommed */
137 printf ("[find_cycle] tail ");
142 * If what we think is the top of the cycle has a cyclehead
143 * field, then it's not really the head of the cycle, which is
144 * really what we want.
146 if (head
->cg
.cyc
.head
!= head
)
148 head
= head
->cg
.cyc
.head
;
149 DBG (DFNDEBUG
, printf ("[find_cycle] new cyclehead ");
153 for (index
= cycle_top
+ 1; index
<= dfn_depth
; ++index
)
155 child
= dfn_stack
[index
].sym
;
156 if (child
->cg
.cyc
.head
== child
)
159 * Not yet glommed anywhere, glom it and fix any
160 * children it has glommed.
162 tail
->cg
.cyc
.next
= child
;
163 child
->cg
.cyc
.head
= head
;
164 DBG (DFNDEBUG
, printf ("[find_cycle] glomming ");
169 for (tail
= child
; tail
->cg
.cyc
.next
; tail
= tail
->cg
.cyc
.next
)
171 tail
->cg
.cyc
.next
->cg
.cyc
.head
= head
;
172 DBG (DFNDEBUG
, printf ("[find_cycle] and its tail ");
173 print_name (tail
->cg
.cyc
.next
);
179 else if (child
->cg
.cyc
.head
!= head
/* firewall */ )
181 fprintf (stderr
, "[find_cycle] glommed, but not to head\n");
190 * Prepare for visiting the children of PARENT. Push a parent onto
191 * the stack and mark it busy.
194 DEFUN (pre_visit
, (parent
), Sym
* parent
)
197 if (dfn_depth
>= DFN_DEPTH
)
199 fprintf (stderr
, "[pre_visit] dfn_stack overflow\n");
202 dfn_stack
[dfn_depth
].sym
= parent
;
203 dfn_stack
[dfn_depth
].cycle_top
= dfn_depth
;
204 parent
->cg
.top_order
= DFN_BUSY
;
205 DBG (DFNDEBUG
, printf ("[pre_visit]\t\t%d:", dfn_depth
);
212 * Done with visiting node PARENT. Pop PARENT off dfn_stack
213 * and number functions if PARENT is head of a cycle.
216 DEFUN (post_visit
, (parent
), Sym
* parent
)
220 DBG (DFNDEBUG
, printf ("[post_visit]\t%d: ", dfn_depth
);
224 * Number functions and things in their cycles unless the function
225 * is itself part of a cycle:
227 if (parent
->cg
.cyc
.head
== parent
)
230 for (member
= parent
; member
; member
= member
->cg
.cyc
.next
)
232 member
->cg
.top_order
= dfn_counter
;
233 DBG (DFNDEBUG
, printf ("[post_visit]\t\tmember ");
235 printf ("-> cg.top_order = %d\n", dfn_counter
));
240 DBG (DFNDEBUG
, printf ("[post_visit]\t\tis part of a cycle\n"));
247 * Given this PARENT, depth first number its children.
250 DEFUN (cg_dfn
, (parent
), Sym
* parent
)
254 DBG (DFNDEBUG
, printf ("[dfn] dfn( ");
258 * If we're already numbered, no need to look any further:
260 if (is_numbered (parent
))
265 * If we're already busy, must be a cycle:
267 if (is_busy (parent
))
274 * Recursively visit children:
276 for (arc
= parent
->cg
.children
; arc
; arc
= arc
->next_child
)
This page took 0.035052 seconds and 4 git commands to generate.