OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [binutils-2.20.1/] [gprof/] [cg_dfn.c] - Diff between revs 816 and 818

Only display areas with differences | Details | Blame | View Log

Rev 816 Rev 818
/*
/*
 * Copyright (c) 1983, 1993
 * Copyright (c) 1983, 1993
 *      The Regents of the University of California.  All rights reserved.
 *      The Regents of the University of California.  All rights reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * modification, are permitted provided that the following conditions
 * are met:
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *    without specific prior written permission.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * 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
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * SUCH DAMAGE.
 */
 */
#include "gprof.h"
#include "gprof.h"
#include "libiberty.h"
#include "libiberty.h"
#include "search_list.h"
#include "search_list.h"
#include "source.h"
#include "source.h"
#include "symtab.h"
#include "symtab.h"
#include "cg_arcs.h"
#include "cg_arcs.h"
#include "cg_dfn.h"
#include "cg_dfn.h"
#include "utils.h"
#include "utils.h"
 
 
#define DFN_INCR_DEPTH (128)
#define DFN_INCR_DEPTH (128)
 
 
typedef struct
typedef struct
  {
  {
    Sym *sym;
    Sym *sym;
    int cycle_top;
    int cycle_top;
  }
  }
DFN_Stack;
DFN_Stack;
 
 
static bfd_boolean is_numbered (Sym *);
static bfd_boolean is_numbered (Sym *);
static bfd_boolean is_busy (Sym *);
static bfd_boolean is_busy (Sym *);
static void find_cycle (Sym *);
static void find_cycle (Sym *);
static void pre_visit (Sym *);
static void pre_visit (Sym *);
static void post_visit (Sym *);
static void post_visit (Sym *);
 
 
DFN_Stack *dfn_stack = NULL;
DFN_Stack *dfn_stack = NULL;
int dfn_maxdepth = 0;
int dfn_maxdepth = 0;
int dfn_depth = 0;
int dfn_depth = 0;
int dfn_counter = DFN_NAN;
int dfn_counter = DFN_NAN;
 
 
 
 
/*
/*
 * Is CHILD already numbered?
 * Is CHILD already numbered?
 */
 */
static bfd_boolean
static bfd_boolean
is_numbered (Sym *child)
is_numbered (Sym *child)
{
{
  return child->cg.top_order != DFN_NAN && child->cg.top_order != DFN_BUSY;
  return child->cg.top_order != DFN_NAN && child->cg.top_order != DFN_BUSY;
}
}
 
 
 
 
/*
/*
 * Is CHILD already busy?
 * Is CHILD already busy?
 */
 */
static bfd_boolean
static bfd_boolean
is_busy (Sym *child)
is_busy (Sym *child)
{
{
  if (child->cg.top_order == DFN_NAN)
  if (child->cg.top_order == DFN_NAN)
    {
    {
      return FALSE;
      return FALSE;
    }
    }
  return TRUE;
  return TRUE;
}
}
 
 
 
 
/*
/*
 * CHILD is part of a cycle.  Find the top caller into this cycle
 * CHILD is part of a cycle.  Find the top caller into this cycle
 * that is not part of the cycle and make all functions in cycle
 * that is not part of the cycle and make all functions in cycle
 * members of that cycle (top caller == caller with smallest
 * members of that cycle (top caller == caller with smallest
 * depth-first number).
 * depth-first number).
 */
 */
static void
static void
find_cycle (Sym *child)
find_cycle (Sym *child)
{
{
  Sym *head = 0;
  Sym *head = 0;
  Sym *tail;
  Sym *tail;
  int cycle_top;
  int cycle_top;
  int index;
  int index;
 
 
  for (cycle_top = dfn_depth; cycle_top > 0; --cycle_top)
  for (cycle_top = dfn_depth; cycle_top > 0; --cycle_top)
    {
    {
      head = dfn_stack[cycle_top].sym;
      head = dfn_stack[cycle_top].sym;
      if (child == head)
      if (child == head)
        {
        {
          break;
          break;
        }
        }
      if (child->cg.cyc.head != child && child->cg.cyc.head == head)
      if (child->cg.cyc.head != child && child->cg.cyc.head == head)
        {
        {
          break;
          break;
        }
        }
    }
    }
  if (cycle_top <= 0)
  if (cycle_top <= 0)
    {
    {
      fprintf (stderr, "[find_cycle] couldn't find head of cycle\n");
      fprintf (stderr, "[find_cycle] couldn't find head of cycle\n");
      done (1);
      done (1);
    }
    }
#ifdef DEBUG
#ifdef DEBUG
  if (debug_level & DFNDEBUG)
  if (debug_level & DFNDEBUG)
    {
    {
      printf ("[find_cycle] dfn_depth %d cycle_top %d ",
      printf ("[find_cycle] dfn_depth %d cycle_top %d ",
              dfn_depth, cycle_top);
              dfn_depth, cycle_top);
      if (head)
      if (head)
        {
        {
          print_name (head);
          print_name (head);
        }
        }
      else
      else
        {
        {
          printf ("<unknown>");
          printf ("<unknown>");
        }
        }
      printf ("\n");
      printf ("\n");
    }
    }
#endif
#endif
  if (cycle_top == dfn_depth)
  if (cycle_top == dfn_depth)
    {
    {
      /*
      /*
       * This is previous function, e.g. this calls itself.  Sort of
       * This is previous function, e.g. this calls itself.  Sort of
       * boring.
       * boring.
       *
       *
       * Since we are taking out self-cycles elsewhere no need for
       * Since we are taking out self-cycles elsewhere no need for
       * the special case, here.
       * the special case, here.
       */
       */
      DBG (DFNDEBUG,
      DBG (DFNDEBUG,
           printf ("[find_cycle] ");
           printf ("[find_cycle] ");
           print_name (child);
           print_name (child);
           printf ("\n"));
           printf ("\n"));
    }
    }
  else
  else
    {
    {
      /*
      /*
       * Glom intervening functions that aren't already glommed into
       * Glom intervening functions that aren't already glommed into
       * this cycle.  Things have been glommed when their cyclehead
       * this cycle.  Things have been glommed when their cyclehead
       * field points to the head of the cycle they are glommed
       * field points to the head of the cycle they are glommed
       * into.
       * into.
       */
       */
      for (tail = head; tail->cg.cyc.next; tail = tail->cg.cyc.next)
      for (tail = head; tail->cg.cyc.next; tail = tail->cg.cyc.next)
        {
        {
          /* void: chase down to tail of things already glommed */
          /* void: chase down to tail of things already glommed */
          DBG (DFNDEBUG,
          DBG (DFNDEBUG,
               printf ("[find_cycle] tail ");
               printf ("[find_cycle] tail ");
               print_name (tail);
               print_name (tail);
               printf ("\n"));
               printf ("\n"));
        }
        }
      /*
      /*
       * If what we think is the top of the cycle has a cyclehead
       * 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
       * field, then it's not really the head of the cycle, which is
       * really what we want.
       * really what we want.
       */
       */
      if (head->cg.cyc.head != head)
      if (head->cg.cyc.head != head)
        {
        {
          head = head->cg.cyc.head;
          head = head->cg.cyc.head;
          DBG (DFNDEBUG, printf ("[find_cycle] new cyclehead ");
          DBG (DFNDEBUG, printf ("[find_cycle] new cyclehead ");
               print_name (head);
               print_name (head);
               printf ("\n"));
               printf ("\n"));
        }
        }
      for (index = cycle_top + 1; index <= dfn_depth; ++index)
      for (index = cycle_top + 1; index <= dfn_depth; ++index)
        {
        {
          child = dfn_stack[index].sym;
          child = dfn_stack[index].sym;
          if (child->cg.cyc.head == child)
          if (child->cg.cyc.head == child)
            {
            {
              /*
              /*
               * Not yet glommed anywhere, glom it and fix any
               * Not yet glommed anywhere, glom it and fix any
               * children it has glommed.
               * children it has glommed.
               */
               */
              tail->cg.cyc.next = child;
              tail->cg.cyc.next = child;
              child->cg.cyc.head = head;
              child->cg.cyc.head = head;
              DBG (DFNDEBUG, printf ("[find_cycle] glomming ");
              DBG (DFNDEBUG, printf ("[find_cycle] glomming ");
                   print_name (child);
                   print_name (child);
                   printf (" onto ");
                   printf (" onto ");
                   print_name (head);
                   print_name (head);
                   printf ("\n"));
                   printf ("\n"));
              for (tail = child; tail->cg.cyc.next; tail = tail->cg.cyc.next)
              for (tail = child; tail->cg.cyc.next; tail = tail->cg.cyc.next)
                {
                {
                  tail->cg.cyc.next->cg.cyc.head = head;
                  tail->cg.cyc.next->cg.cyc.head = head;
                  DBG (DFNDEBUG, printf ("[find_cycle] and its tail ");
                  DBG (DFNDEBUG, printf ("[find_cycle] and its tail ");
                       print_name (tail->cg.cyc.next);
                       print_name (tail->cg.cyc.next);
                       printf (" onto ");
                       printf (" onto ");
                       print_name (head);
                       print_name (head);
                       printf ("\n"));
                       printf ("\n"));
                }
                }
            }
            }
          else if (child->cg.cyc.head != head /* firewall */ )
          else if (child->cg.cyc.head != head /* firewall */ )
            {
            {
              fprintf (stderr, "[find_cycle] glommed, but not to head\n");
              fprintf (stderr, "[find_cycle] glommed, but not to head\n");
              done (1);
              done (1);
            }
            }
        }
        }
    }
    }
}
}
 
 
 
 
/*
/*
 * Prepare for visiting the children of PARENT.  Push a parent onto
 * Prepare for visiting the children of PARENT.  Push a parent onto
 * the stack and mark it busy.
 * the stack and mark it busy.
 */
 */
static void
static void
pre_visit (Sym *parent)
pre_visit (Sym *parent)
{
{
  ++dfn_depth;
  ++dfn_depth;
 
 
  if (dfn_depth >= dfn_maxdepth)
  if (dfn_depth >= dfn_maxdepth)
    {
    {
      dfn_maxdepth += DFN_INCR_DEPTH;
      dfn_maxdepth += DFN_INCR_DEPTH;
      dfn_stack = (DFN_Stack *) xrealloc (dfn_stack,
      dfn_stack = (DFN_Stack *) xrealloc (dfn_stack,
                                          dfn_maxdepth * sizeof *dfn_stack);
                                          dfn_maxdepth * sizeof *dfn_stack);
    }
    }
 
 
  dfn_stack[dfn_depth].sym = parent;
  dfn_stack[dfn_depth].sym = parent;
  dfn_stack[dfn_depth].cycle_top = dfn_depth;
  dfn_stack[dfn_depth].cycle_top = dfn_depth;
  parent->cg.top_order = DFN_BUSY;
  parent->cg.top_order = DFN_BUSY;
  DBG (DFNDEBUG, printf ("[pre_visit]\t\t%d:", dfn_depth);
  DBG (DFNDEBUG, printf ("[pre_visit]\t\t%d:", dfn_depth);
       print_name (parent);
       print_name (parent);
       printf ("\n"));
       printf ("\n"));
}
}
 
 
 
 
/*
/*
 * Done with visiting node PARENT.  Pop PARENT off dfn_stack
 * Done with visiting node PARENT.  Pop PARENT off dfn_stack
 * and number functions if PARENT is head of a cycle.
 * and number functions if PARENT is head of a cycle.
 */
 */
static void
static void
post_visit (Sym *parent)
post_visit (Sym *parent)
{
{
  Sym *member;
  Sym *member;
 
 
  DBG (DFNDEBUG, printf ("[post_visit]\t%d: ", dfn_depth);
  DBG (DFNDEBUG, printf ("[post_visit]\t%d: ", dfn_depth);
       print_name (parent);
       print_name (parent);
       printf ("\n"));
       printf ("\n"));
  /*
  /*
   * Number functions and things in their cycles unless the function
   * Number functions and things in their cycles unless the function
   * is itself part of a cycle:
   * is itself part of a cycle:
   */
   */
  if (parent->cg.cyc.head == parent)
  if (parent->cg.cyc.head == parent)
    {
    {
      ++dfn_counter;
      ++dfn_counter;
      for (member = parent; member; member = member->cg.cyc.next)
      for (member = parent; member; member = member->cg.cyc.next)
        {
        {
          member->cg.top_order = dfn_counter;
          member->cg.top_order = dfn_counter;
          DBG (DFNDEBUG, printf ("[post_visit]\t\tmember ");
          DBG (DFNDEBUG, printf ("[post_visit]\t\tmember ");
               print_name (member);
               print_name (member);
               printf ("-> cg.top_order = %d\n", dfn_counter));
               printf ("-> cg.top_order = %d\n", dfn_counter));
        }
        }
    }
    }
  else
  else
    {
    {
      DBG (DFNDEBUG, printf ("[post_visit]\t\tis part of a cycle\n"));
      DBG (DFNDEBUG, printf ("[post_visit]\t\tis part of a cycle\n"));
    }
    }
  --dfn_depth;
  --dfn_depth;
}
}
 
 
 
 
/*
/*
 * Given this PARENT, depth first number its children.
 * Given this PARENT, depth first number its children.
 */
 */
void
void
cg_dfn (Sym *parent)
cg_dfn (Sym *parent)
{
{
  Arc *arc;
  Arc *arc;
 
 
  DBG (DFNDEBUG, printf ("[dfn] dfn( ");
  DBG (DFNDEBUG, printf ("[dfn] dfn( ");
       print_name (parent);
       print_name (parent);
       printf (")\n"));
       printf (")\n"));
  /*
  /*
   * If we're already numbered, no need to look any further:
   * If we're already numbered, no need to look any further:
   */
   */
  if (is_numbered (parent))
  if (is_numbered (parent))
    {
    {
      return;
      return;
    }
    }
  /*
  /*
   * If we're already busy, must be a cycle:
   * If we're already busy, must be a cycle:
   */
   */
  if (is_busy (parent))
  if (is_busy (parent))
    {
    {
      find_cycle (parent);
      find_cycle (parent);
      return;
      return;
    }
    }
  pre_visit (parent);
  pre_visit (parent);
  /*
  /*
   * Recursively visit children:
   * Recursively visit children:
   */
   */
  for (arc = parent->cg.children; arc; arc = arc->next_child)
  for (arc = parent->cg.children; arc; arc = arc->next_child)
    {
    {
      cg_dfn (arc->child);
      cg_dfn (arc->child);
    }
    }
  post_visit (parent);
  post_visit (parent);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.