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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1ksim/] [cpu/] [common/] [labels.c] - Diff between revs 19 and 100

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

Rev 19 Rev 100
/* abstract.c -- Abstract entities, handling labels
/* abstract.c -- Abstract entities, handling labels
 
 
   Copyright (C) 2001 Marko Mlinar, markom@opencores.org
   Copyright (C) 2001 Marko Mlinar, markom@opencores.org
   Copyright (C) 2008 Embecosm Limited
   Copyright (C) 2008 Embecosm Limited
 
 
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
 
 
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
 
 
   This program is free software; you can redistribute it and/or modify it
   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 the Free
   under the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 3 of the License, or (at your option)
   Software Foundation; either version 3 of the License, or (at your option)
   any later version.
   any later version.
 
 
   This program is distributed in the hope that it will be useful, but WITHOUT
   This program is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   more details.
   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, see <http://www.gnu.org/licenses/>.  */
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 
/* This program is commented throughout in a fashion suitable for processing
/* This program is commented throughout in a fashion suitable for processing
   with Doxygen. */
   with Doxygen. */
 
 
/* Abstract memory and routines that go with this. I need to add all sorts of
/* Abstract memory and routines that go with this. I need to add all sorts of
   other abstract entities. Currently we have only memory. */
   other abstract entities. Currently we have only memory. */
 
 
 
 
/* Autoconf and/or portability configuration */
/* Autoconf and/or portability configuration */
#include "config.h"
#include "config.h"
#include "port.h"
#include "port.h"
 
 
/* System includes */
/* System includes */
#include <stdlib.h>
#include <stdlib.h>
 
 
/* Package includes */
/* Package includes */
#include "labels.h"
#include "labels.h"
 
 
#define LABELS_HASH_SIZE 119
#define LABELS_HASH_SIZE 119
 
 
/* Globally visible list of breakpoints */
/* Globally visible list of breakpoints */
struct breakpoint_entry *breakpoints;
struct breakpoint_entry *breakpoints;
 
 
/* Local list of labels (symbols) */
/* Local list of labels (symbols) */
static struct label_entry *label_hash[LABELS_HASH_SIZE];
static struct label_entry *label_hash[LABELS_HASH_SIZE];
 
 
 
 
void
void
init_labels ()
init_labels ()
{
{
  int i;
  int i;
  for (i = 0; i < LABELS_HASH_SIZE; i++)
  for (i = 0; i < LABELS_HASH_SIZE; i++)
    label_hash[i] = NULL;
    label_hash[i] = NULL;
}
}
 
 
void
void
add_label (oraddr_t addr, char *name)
add_label (oraddr_t addr, char *name)
{
{
  struct label_entry **tmp;
  struct label_entry **tmp;
  tmp = &(label_hash[addr % LABELS_HASH_SIZE]);
  tmp = &(label_hash[addr % LABELS_HASH_SIZE]);
  for (; *tmp; tmp = &((*tmp)->next));
  for (; *tmp; tmp = &((*tmp)->next)); // Find the next NULL label entry pointer (loop while the pointer de-refernce is non-NULL)
  *tmp = malloc (sizeof (**tmp));
  *tmp = malloc (sizeof (**tmp)); // allocate space for the pointer to the hash entry pointer
  (*tmp)->name = malloc (strlen (name) + 1);
  (*tmp)->name = malloc (strlen (name) + 1); // now allocate space for the name string
  (*tmp)->addr = addr;
  (*tmp)->addr = addr;
  strcpy ((*tmp)->name, name);
  strcpy ((*tmp)->name, name);
  (*tmp)->next = NULL;
  (*tmp)->next = NULL;
}
}
 
 
struct label_entry *
struct label_entry *
get_label (oraddr_t addr)
get_label (oraddr_t addr)
{
{
  struct label_entry *tmp = label_hash[addr % LABELS_HASH_SIZE];
  struct label_entry *tmp = label_hash[addr % LABELS_HASH_SIZE];
  while (tmp)
  while (tmp)
    {
    {
      if (tmp->addr == addr)
      if (tmp->addr == addr)
        return tmp;
        return tmp;
      tmp = tmp->next;
      tmp = tmp->next;
    }
    }
  return NULL;
  return NULL;
}
}
 
 
struct label_entry *
struct label_entry *
find_label (char *name)
find_label (char *name)
{
{
  int i;
  int i;
  for (i = 0; i < LABELS_HASH_SIZE; i++)
  for (i = 0; i < LABELS_HASH_SIZE; i++)
    {
    {
      struct label_entry *tmp = label_hash[i % LABELS_HASH_SIZE];
      struct label_entry *tmp = label_hash[i % LABELS_HASH_SIZE];
      while (tmp)
      while (tmp)
        {
        {
          if (strcmp (tmp->name, name) == 0)
          if (strcmp (tmp->name, name) == 0)
            return tmp;
            return tmp;
          tmp = tmp->next;
          tmp = tmp->next;
        }
        }
    }
    }
  return NULL;
  return NULL;
}
}
 
 
/* Searches mem array for a particular label and returns label's address.
/* Searches mem array for a particular label and returns label's address.
   If label does not exist, returns 0. */
   If label does not exist, returns 0. */
oraddr_t
oraddr_t
eval_label (char *name)
eval_label (char *name)
{
{
  struct label_entry *le;
  struct label_entry *le;
  char *plus;
  char *plus;
  char *minus;
  char *minus;
  int positive_offset = 0;
  int positive_offset = 0;
  int negative_offset = 0;
  int negative_offset = 0;
 
 
  if ((plus = strchr (name, '+')))
  if ((plus = strchr (name, '+')))
    {
    {
      *plus = '\0';
      *plus = '\0';
      positive_offset = atoi (++plus);
      positive_offset = atoi (++plus);
    }
    }
 
 
  if ((minus = strchr (name, '-')))
  if ((minus = strchr (name, '-')))
    {
    {
      *minus = '\0';
      *minus = '\0';
      negative_offset = atoi (++minus);
      negative_offset = atoi (++minus);
    }
    }
  le = find_label (name);
  le = find_label (name);
  if (!le)
  if (!le)
    return 0;
    return 0;
 
 
  return le->addr + positive_offset - negative_offset;
  return le->addr + positive_offset - negative_offset;
}
}
 
 
void
void
init_breakpoints ()
init_breakpoints ()
{
{
  breakpoints = 0;
  breakpoints = 0;
}
}
 
 
void
void
add_breakpoint (oraddr_t addr)
add_breakpoint (oraddr_t addr)
{
{
  struct breakpoint_entry *tmp;
  struct breakpoint_entry *tmp;
  tmp = (struct breakpoint_entry *) malloc (sizeof (struct breakpoint_entry));
  tmp = (struct breakpoint_entry *) malloc (sizeof (struct breakpoint_entry));
  tmp->next = breakpoints;
  tmp->next = breakpoints;
  tmp->addr = addr;
  tmp->addr = addr;
  breakpoints = tmp;
  breakpoints = tmp;
}
}
 
 
void
void
remove_breakpoint (oraddr_t addr)
remove_breakpoint (oraddr_t addr)
{
{
  struct breakpoint_entry **tmp = &breakpoints;
  struct breakpoint_entry **tmp = &breakpoints;
  while (*tmp)
  while (*tmp)
    {
    {
      if ((*tmp)->addr == addr)
      if ((*tmp)->addr == addr)
        {
        {
          struct breakpoint_entry *t = *tmp;
          struct breakpoint_entry *t = *tmp;
          (*tmp) = t->next;
          (*tmp) = t->next;
          free (t);
          free (t);
        }
        }
      else
      else
        tmp = &((*tmp)->next);
        tmp = &((*tmp)->next);
    }
    }
}
}
 
 
void
void
print_breakpoints ()
print_breakpoints ()
{
{
  struct breakpoint_entry **tmp = &breakpoints;
  struct breakpoint_entry **tmp = &breakpoints;
  int i = 1;
  int i = 1;
  printf ("---[breakpoints]------------------\n");
  printf ("---[breakpoints]------------------\n");
  while (*tmp)
  while (*tmp)
    {
    {
      printf ("Breakpoint %i at 0x%" PRIxADDR "\n", i, (*tmp)->addr);
      printf ("Breakpoint %i at 0x%" PRIxADDR "\n", i, (*tmp)->addr);
      tmp = &((*tmp)->next);
      tmp = &((*tmp)->next);
    }
    }
  printf ("---[breakpoints end]--------------\n");
  printf ("---[breakpoints end]--------------\n");
}
}
 
 
int
int
has_breakpoint (oraddr_t addr)
has_breakpoint (oraddr_t addr)
{
{
  struct breakpoint_entry *tmp = breakpoints;
  struct breakpoint_entry *tmp = breakpoints;
  while (tmp)
  while (tmp)
    {
    {
      if (tmp->addr == addr)
      if (tmp->addr == addr)
        return 1;
        return 1;
      tmp = tmp->next;
      tmp = tmp->next;
    }
    }
  return 0;
  return 0;
}
}
 
 

powered by: WebSVN 2.1.0

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