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

Subversion Repositories or1k

[/] [or1k/] [tags/] [VER_5_3/] [gdb-5.3/] [sim/] [common/] [sim-arange.c] - Diff between revs 1182 and 1765

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 1182 Rev 1765
/* Address ranges.
/* Address ranges.
   Copyright (C) 1998 Free Software Foundation, Inc.
   Copyright (C) 1998 Free Software Foundation, Inc.
   Contributed by Cygnus Solutions.
   Contributed by Cygnus Solutions.
 
 
This file is part of the GNU Simulators.
This file is part of the GNU Simulators.
 
 
This program is free software; you can redistribute it and/or modify
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
the Free Software Foundation; either version 2, or (at your option)
any later version.
any later version.
 
 
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License along
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 
/* Tell sim-arange.h it's us.  */
/* Tell sim-arange.h it's us.  */
#define SIM_ARANGE_C
#define SIM_ARANGE_C
 
 
#include "libiberty.h"
#include "libiberty.h"
#include "sim-basics.h"
#include "sim-basics.h"
#include "sim-assert.h"
#include "sim-assert.h"
 
 
#ifdef HAVE_STDLIB_H
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#include <stdlib.h>
#endif
#endif
 
 
#ifdef HAVE_STRING_H
#ifdef HAVE_STRING_H
#include <string.h>
#include <string.h>
#endif
#endif
 
 
#define DEFINE_INLINE_P (! defined (SIM_ARANGE_C_INCLUDED))
#define DEFINE_INLINE_P (! defined (SIM_ARANGE_C_INCLUDED))
#define DEFINE_NON_INLINE_P defined (SIM_ARANGE_C_INCLUDED)
#define DEFINE_NON_INLINE_P defined (SIM_ARANGE_C_INCLUDED)
 
 
#if DEFINE_NON_INLINE_P
#if DEFINE_NON_INLINE_P
 
 
/* Insert a range.  */
/* Insert a range.  */
 
 
static void
static void
insert_range (ADDR_SUBRANGE **pos, ADDR_SUBRANGE *asr)
insert_range (ADDR_SUBRANGE **pos, ADDR_SUBRANGE *asr)
{
{
  asr->next = *pos;
  asr->next = *pos;
  *pos = asr;
  *pos = asr;
}
}
 
 
/* Delete a range.  */
/* Delete a range.  */
 
 
static void
static void
delete_range (ADDR_SUBRANGE **thisasrp)
delete_range (ADDR_SUBRANGE **thisasrp)
{
{
  ADDR_SUBRANGE *thisasr;
  ADDR_SUBRANGE *thisasr;
 
 
  thisasr = *thisasrp;
  thisasr = *thisasrp;
  *thisasrp = thisasr->next;
  *thisasrp = thisasr->next;
 
 
  free (thisasr);
  free (thisasr);
}
}
 
 
/* Add or delete an address range.
/* Add or delete an address range.
   This code was borrowed from linux's locks.c:posix_lock_file().
   This code was borrowed from linux's locks.c:posix_lock_file().
   ??? Todo: Given our simpler needs this could be simplified
   ??? Todo: Given our simpler needs this could be simplified
   (split into two fns).  */
   (split into two fns).  */
 
 
static void
static void
frob_range (ADDR_RANGE *ar, address_word start, address_word end, int delete_p)
frob_range (ADDR_RANGE *ar, address_word start, address_word end, int delete_p)
{
{
  ADDR_SUBRANGE *asr;
  ADDR_SUBRANGE *asr;
  ADDR_SUBRANGE *new_asr, *new_asr2;
  ADDR_SUBRANGE *new_asr, *new_asr2;
  ADDR_SUBRANGE *left = NULL;
  ADDR_SUBRANGE *left = NULL;
  ADDR_SUBRANGE *right = NULL;
  ADDR_SUBRANGE *right = NULL;
  ADDR_SUBRANGE **before;
  ADDR_SUBRANGE **before;
  ADDR_SUBRANGE init_caller;
  ADDR_SUBRANGE init_caller;
  ADDR_SUBRANGE *caller = &init_caller;
  ADDR_SUBRANGE *caller = &init_caller;
  int added_p = 0;
  int added_p = 0;
 
 
  memset (caller, 0, sizeof (ADDR_SUBRANGE));
  memset (caller, 0, sizeof (ADDR_SUBRANGE));
  new_asr = ZALLOC (ADDR_SUBRANGE);
  new_asr = ZALLOC (ADDR_SUBRANGE);
  new_asr2 = ZALLOC (ADDR_SUBRANGE);
  new_asr2 = ZALLOC (ADDR_SUBRANGE);
 
 
  caller->start = start;
  caller->start = start;
  caller->end = end;
  caller->end = end;
  before = &ar->ranges;
  before = &ar->ranges;
 
 
  while ((asr = *before) != NULL)
  while ((asr = *before) != NULL)
    {
    {
      if (! delete_p)
      if (! delete_p)
        {
        {
          /* Try next range if current range preceeds new one and not
          /* Try next range if current range preceeds new one and not
             adjacent or overlapping.  */
             adjacent or overlapping.  */
          if (asr->end < caller->start - 1)
          if (asr->end < caller->start - 1)
            goto next_range;
            goto next_range;
 
 
          /* Break out if new range preceeds current one and not
          /* Break out if new range preceeds current one and not
             adjacent or overlapping.  */
             adjacent or overlapping.  */
          if (asr->start > caller->end + 1)
          if (asr->start > caller->end + 1)
            break;
            break;
 
 
          /* If we come here, the new and current ranges are adjacent or
          /* If we come here, the new and current ranges are adjacent or
             overlapping. Make one range yielding from the lower start address
             overlapping. Make one range yielding from the lower start address
             of both ranges to the higher end address.  */
             of both ranges to the higher end address.  */
          if (asr->start > caller->start)
          if (asr->start > caller->start)
            asr->start = caller->start;
            asr->start = caller->start;
          else
          else
            caller->start = asr->start;
            caller->start = asr->start;
          if (asr->end < caller->end)
          if (asr->end < caller->end)
            asr->end = caller->end;
            asr->end = caller->end;
          else
          else
            caller->end = asr->end;
            caller->end = asr->end;
 
 
          if (added_p)
          if (added_p)
            {
            {
              delete_range (before);
              delete_range (before);
              continue;
              continue;
            }
            }
          caller = asr;
          caller = asr;
          added_p = 1;
          added_p = 1;
        }
        }
      else /* deleting a range */
      else /* deleting a range */
        {
        {
          /* Try next range if current range preceeds new one.  */
          /* Try next range if current range preceeds new one.  */
          if (asr->end < caller->start)
          if (asr->end < caller->start)
            goto next_range;
            goto next_range;
 
 
          /* Break out if new range preceeds current one.  */
          /* Break out if new range preceeds current one.  */
          if (asr->start > caller->end)
          if (asr->start > caller->end)
            break;
            break;
 
 
          added_p = 1;
          added_p = 1;
 
 
          if (asr->start < caller->start)
          if (asr->start < caller->start)
            left = asr;
            left = asr;
 
 
          /* If the next range in the list has a higher end
          /* If the next range in the list has a higher end
             address than the new one, insert the new one here.  */
             address than the new one, insert the new one here.  */
          if (asr->end > caller->end)
          if (asr->end > caller->end)
            {
            {
              right = asr;
              right = asr;
              break;
              break;
            }
            }
          if (asr->start >= caller->start)
          if (asr->start >= caller->start)
            {
            {
              /* The new range completely replaces an old
              /* The new range completely replaces an old
                 one (This may happen several times).  */
                 one (This may happen several times).  */
              if (added_p)
              if (added_p)
                {
                {
                  delete_range (before);
                  delete_range (before);
                  continue;
                  continue;
                }
                }
 
 
              /* Replace the old range with the new one.  */
              /* Replace the old range with the new one.  */
              asr->start = caller->start;
              asr->start = caller->start;
              asr->end = caller->end;
              asr->end = caller->end;
              caller = asr;
              caller = asr;
              added_p = 1;
              added_p = 1;
            }
            }
        }
        }
 
 
      /* Go on to next range.  */
      /* Go on to next range.  */
    next_range:
    next_range:
      before = &asr->next;
      before = &asr->next;
    }
    }
 
 
  if (!added_p)
  if (!added_p)
    {
    {
      if (delete_p)
      if (delete_p)
        goto out;
        goto out;
      new_asr->start = caller->start;
      new_asr->start = caller->start;
      new_asr->end = caller->end;
      new_asr->end = caller->end;
      insert_range (before, new_asr);
      insert_range (before, new_asr);
      new_asr = NULL;
      new_asr = NULL;
    }
    }
  if (right)
  if (right)
    {
    {
      if (left == right)
      if (left == right)
        {
        {
          /* The new range breaks the old one in two pieces,
          /* The new range breaks the old one in two pieces,
             so we have to use the second new range.  */
             so we have to use the second new range.  */
          new_asr2->start = right->start;
          new_asr2->start = right->start;
          new_asr2->end = right->end;
          new_asr2->end = right->end;
          left = new_asr2;
          left = new_asr2;
          insert_range (before, left);
          insert_range (before, left);
          new_asr2 = NULL;
          new_asr2 = NULL;
        }
        }
      right->start = caller->end + 1;
      right->start = caller->end + 1;
    }
    }
  if (left)
  if (left)
    {
    {
      left->end = caller->start - 1;
      left->end = caller->start - 1;
    }
    }
 
 
 out:
 out:
  if (new_asr)
  if (new_asr)
    free(new_asr);
    free(new_asr);
  if (new_asr2)
  if (new_asr2)
    free(new_asr2);
    free(new_asr2);
}
}
 
 
/* Free T and all subtrees.  */
/* Free T and all subtrees.  */
 
 
static void
static void
free_search_tree (ADDR_RANGE_TREE *t)
free_search_tree (ADDR_RANGE_TREE *t)
{
{
  if (t != NULL)
  if (t != NULL)
    {
    {
      free_search_tree (t->lower);
      free_search_tree (t->lower);
      free_search_tree (t->higher);
      free_search_tree (t->higher);
      free (t);
      free (t);
    }
    }
}
}
 
 
/* Subroutine of build_search_tree to recursively build a balanced tree.
/* Subroutine of build_search_tree to recursively build a balanced tree.
   ??? It's not an optimum tree though.  */
   ??? It's not an optimum tree though.  */
 
 
static ADDR_RANGE_TREE *
static ADDR_RANGE_TREE *
build_tree_1 (ADDR_SUBRANGE **asrtab, unsigned int n)
build_tree_1 (ADDR_SUBRANGE **asrtab, unsigned int n)
{
{
  unsigned int mid = n / 2;
  unsigned int mid = n / 2;
  ADDR_RANGE_TREE *t;
  ADDR_RANGE_TREE *t;
 
 
  if (n == 0)
  if (n == 0)
    return NULL;
    return NULL;
  t = (ADDR_RANGE_TREE *) xmalloc (sizeof (ADDR_RANGE_TREE));
  t = (ADDR_RANGE_TREE *) xmalloc (sizeof (ADDR_RANGE_TREE));
  t->start = asrtab[mid]->start;
  t->start = asrtab[mid]->start;
  t->end = asrtab[mid]->end;
  t->end = asrtab[mid]->end;
  if (mid != 0)
  if (mid != 0)
    t->lower = build_tree_1 (asrtab, mid);
    t->lower = build_tree_1 (asrtab, mid);
  else
  else
    t->lower = NULL;
    t->lower = NULL;
  if (n > mid + 1)
  if (n > mid + 1)
    t->higher = build_tree_1 (asrtab + mid + 1, n - mid - 1);
    t->higher = build_tree_1 (asrtab + mid + 1, n - mid - 1);
  else
  else
    t->higher = NULL;
    t->higher = NULL;
  return t;
  return t;
}
}
 
 
/* Build a search tree for address range AR.  */
/* Build a search tree for address range AR.  */
 
 
static void
static void
build_search_tree (ADDR_RANGE *ar)
build_search_tree (ADDR_RANGE *ar)
{
{
  /* ??? Simple version for now.  */
  /* ??? Simple version for now.  */
  ADDR_SUBRANGE *asr,**asrtab;
  ADDR_SUBRANGE *asr,**asrtab;
  unsigned int i, n;
  unsigned int i, n;
 
 
  for (n = 0, asr = ar->ranges; asr != NULL; ++n, asr = asr->next)
  for (n = 0, asr = ar->ranges; asr != NULL; ++n, asr = asr->next)
    continue;
    continue;
  asrtab = (ADDR_SUBRANGE **) xmalloc (n * sizeof (ADDR_SUBRANGE *));
  asrtab = (ADDR_SUBRANGE **) xmalloc (n * sizeof (ADDR_SUBRANGE *));
  for (i = 0, asr = ar->ranges; i < n; ++i, asr = asr->next)
  for (i = 0, asr = ar->ranges; i < n; ++i, asr = asr->next)
    asrtab[i] = asr;
    asrtab[i] = asr;
  ar->range_tree = build_tree_1 (asrtab, n);
  ar->range_tree = build_tree_1 (asrtab, n);
  free (asrtab);
  free (asrtab);
}
}
 
 
void
void
sim_addr_range_add (ADDR_RANGE *ar, address_word start, address_word end)
sim_addr_range_add (ADDR_RANGE *ar, address_word start, address_word end)
{
{
  frob_range (ar, start, end, 0);
  frob_range (ar, start, end, 0);
 
 
  /* Rebuild the search tree.  */
  /* Rebuild the search tree.  */
  /* ??? Instead of rebuilding it here it could be done in a module resume
  /* ??? Instead of rebuilding it here it could be done in a module resume
     handler, say by first checking for a `changed' flag, assuming of course
     handler, say by first checking for a `changed' flag, assuming of course
     this would never be done while the simulation is running.  */
     this would never be done while the simulation is running.  */
  free_search_tree (ar->range_tree);
  free_search_tree (ar->range_tree);
  build_search_tree (ar);
  build_search_tree (ar);
}
}
 
 
void
void
sim_addr_range_delete (ADDR_RANGE *ar, address_word start, address_word end)
sim_addr_range_delete (ADDR_RANGE *ar, address_word start, address_word end)
{
{
  frob_range (ar, start, end, 1);
  frob_range (ar, start, end, 1);
 
 
  /* Rebuild the search tree.  */
  /* Rebuild the search tree.  */
  /* ??? Instead of rebuilding it here it could be done in a module resume
  /* ??? Instead of rebuilding it here it could be done in a module resume
     handler, say by first checking for a `changed' flag, assuming of course
     handler, say by first checking for a `changed' flag, assuming of course
     this would never be done while the simulation is running.  */
     this would never be done while the simulation is running.  */
  free_search_tree (ar->range_tree);
  free_search_tree (ar->range_tree);
  build_search_tree (ar);
  build_search_tree (ar);
}
}
 
 
#endif /* DEFINE_NON_INLINE_P */
#endif /* DEFINE_NON_INLINE_P */
 
 
#if DEFINE_INLINE_P
#if DEFINE_INLINE_P
 
 
SIM_ARANGE_INLINE int
SIM_ARANGE_INLINE int
sim_addr_range_hit_p (ADDR_RANGE *ar, address_word addr)
sim_addr_range_hit_p (ADDR_RANGE *ar, address_word addr)
{
{
  ADDR_RANGE_TREE *t = ar->range_tree;
  ADDR_RANGE_TREE *t = ar->range_tree;
 
 
  while (t != NULL)
  while (t != NULL)
    {
    {
      if (addr < t->start)
      if (addr < t->start)
        t = t->lower;
        t = t->lower;
      else if (addr > t->end)
      else if (addr > t->end)
        t = t->higher;
        t = t->higher;
      else
      else
        return 1;
        return 1;
    }
    }
  return 0;
  return 0;
}
}
 
 
#endif /* DEFINE_INLINE_P */
#endif /* DEFINE_INLINE_P */
 
 

powered by: WebSVN 2.1.0

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