#include "libiberty.h"
#include "gprof.h"
#include "search_list.h"
#include "source.h"
#include "symtab.h"
#include "cg_arcs.h"
#include "cg_dfn.h"
#include "utils.h"
#define DFN_INCR_DEPTH (128)
typedef struct
{
Sym *sym;
int cycle_top;
}
DFN_Stack;
static bfd_boolean is_numbered (Sym *);
static bfd_boolean is_busy (Sym *);
static void find_cycle (Sym *);
static void pre_visit (Sym *);
static void post_visit (Sym *);
DFN_Stack *dfn_stack = NULL;
int dfn_maxdepth = 0;
int dfn_depth = 0;
int dfn_counter = DFN_NAN;
static bfd_boolean
is_numbered (Sym *child)
{
return child->cg.top_order != DFN_NAN && child->cg.top_order != DFN_BUSY;
}
static bfd_boolean
is_busy (Sym *child)
{
if (child->cg.top_order == DFN_NAN)
{
return FALSE;
}
return TRUE;
}
static void
find_cycle (Sym *child)
{
Sym *head = 0;
Sym *tail;
int cycle_top;
int index;
for (cycle_top = dfn_depth; cycle_top > 0; --cycle_top)
{
head = dfn_stack[cycle_top].sym;
if (child == head)
{
break;
}
if (child->cg.cyc.head != child && child->cg.cyc.head == head)
{
break;
}
}
if (cycle_top <= 0)
{
fprintf (stderr, "[find_cycle] couldn't find head of cycle\n");
done (1);
}
#ifdef DEBUG
if (debug_level & DFNDEBUG)
{
printf ("[find_cycle] dfn_depth %d cycle_top %d ",
dfn_depth, cycle_top);
if (head)
{
print_name (head);
}
else
{
printf ("<unknown>");
}
printf ("\n");
}
#endif
if (cycle_top == dfn_depth)
{
DBG (DFNDEBUG,
printf ("[find_cycle] ");
print_name (child);
printf ("\n"));
}
else
{
for (tail = head; tail->cg.cyc.next; tail = tail->cg.cyc.next)
{
DBG (DFNDEBUG,
printf ("[find_cycle] tail ");
print_name (tail);
printf ("\n"));
}
if (head->cg.cyc.head != head)
{
head = head->cg.cyc.head;
DBG (DFNDEBUG, printf ("[find_cycle] new cyclehead ");
print_name (head);
printf ("\n"));
}
for (index = cycle_top + 1; index <= dfn_depth; ++index)
{
child = dfn_stack[index].sym;
if (child->cg.cyc.head == child)
{
tail->cg.cyc.next = child;
child->cg.cyc.head = head;
DBG (DFNDEBUG, printf ("[find_cycle] glomming ");
print_name (child);
printf (" onto ");
print_name (head);
printf ("\n"));
for (tail = child; tail->cg.cyc.next; tail = tail->cg.cyc.next)
{
tail->cg.cyc.next->cg.cyc.head = head;
DBG (DFNDEBUG, printf ("[find_cycle] and its tail ");
print_name (tail->cg.cyc.next);
printf (" onto ");
print_name (head);
printf ("\n"));
}
}
else if (child->cg.cyc.head != head )
{
fprintf (stderr, "[find_cycle] glommed, but not to head\n");
done (1);
}
}
}
}
static void
pre_visit (Sym *parent)
{
++dfn_depth;
if (dfn_depth >= dfn_maxdepth)
{
dfn_maxdepth += DFN_INCR_DEPTH;
dfn_stack = xrealloc (dfn_stack, dfn_maxdepth * sizeof *dfn_stack);
}
dfn_stack[dfn_depth].sym = parent;
dfn_stack[dfn_depth].cycle_top = dfn_depth;
parent->cg.top_order = DFN_BUSY;
DBG (DFNDEBUG, printf ("[pre_visit]\t\t%d:", dfn_depth);
print_name (parent);
printf ("\n"));
}
static void
post_visit (Sym *parent)
{
Sym *member;
DBG (DFNDEBUG, printf ("[post_visit]\t%d: ", dfn_depth);
print_name (parent);
printf ("\n"));
if (parent->cg.cyc.head == parent)
{
++dfn_counter;
for (member = parent; member; member = member->cg.cyc.next)
{
member->cg.top_order = dfn_counter;
DBG (DFNDEBUG, printf ("[post_visit]\t\tmember ");
print_name (member);
printf ("-> cg.top_order = %d\n", dfn_counter));
}
}
else
{
DBG (DFNDEBUG, printf ("[post_visit]\t\tis part of a cycle\n"));
}
--dfn_depth;
}
void
cg_dfn (Sym *parent)
{
Arc *arc;
DBG (DFNDEBUG, printf ("[dfn] dfn( ");
print_name (parent);
printf (")\n"));
if (is_numbered (parent))
{
return;
}
if (is_busy (parent))
{
find_cycle (parent);
return;
}
pre_visit (parent);
for (arc = parent->cg.children; arc; arc = arc->next_child)
{
cg_dfn (arc->child);
}
post_visit (parent);
}