OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gnu-src/] [gdb-6.8/] [pre-binutils-2.20.1-sync/] [sim/] [ppc/] [dgen.c] - Diff between revs 157 and 223

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

Rev 157 Rev 223
/*  This file is part of the program psim.
/*  This file is part of the program psim.
 
 
    Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
    Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
 
 
    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 of the License, or
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    (at your option) 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
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 
    */
    */
 
 
 
 
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fcntl.h>
#include <getopt.h>
#include <getopt.h>
#include <stdio.h>
#include <stdio.h>
#include <ctype.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdarg.h>
 
 
#include "config.h"
#include "config.h"
#include "misc.h"
#include "misc.h"
#include "lf.h"
#include "lf.h"
#include "table.h"
#include "table.h"
 
 
#ifdef HAVE_UNISTD_H
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#include <unistd.h>
#endif
#endif
 
 
#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>
#else
#else
#ifdef HAVE_STRINGS_H
#ifdef HAVE_STRINGS_H
#include <strings.h>
#include <strings.h>
#endif
#endif
#endif
#endif
 
 
/****************************************************************/
/****************************************************************/
 
 
int spreg_lookup_table = 1;
int spreg_lookup_table = 1;
enum {
enum {
  nr_of_sprs = 1024,
  nr_of_sprs = 1024,
};
};
 
 
/****************************************************************/
/****************************************************************/
 
 
 
 
typedef enum {
typedef enum {
  spreg_name,
  spreg_name,
  spreg_reg_nr,
  spreg_reg_nr,
  spreg_readonly,
  spreg_readonly,
  spreg_length,
  spreg_length,
  nr_spreg_fields,
  nr_spreg_fields,
} spreg_fields;
} spreg_fields;
 
 
typedef struct _spreg_table_entry spreg_table_entry;
typedef struct _spreg_table_entry spreg_table_entry;
struct _spreg_table_entry {
struct _spreg_table_entry {
  char *name;
  char *name;
  int spreg_nr;
  int spreg_nr;
  int is_readonly;
  int is_readonly;
  int length;
  int length;
  table_entry *entry;
  table_entry *entry;
  spreg_table_entry *next;
  spreg_table_entry *next;
};
};
 
 
typedef struct _spreg_table spreg_table;
typedef struct _spreg_table spreg_table;
struct _spreg_table {
struct _spreg_table {
  spreg_table_entry *sprs;
  spreg_table_entry *sprs;
};
};
 
 
static void
static void
spreg_table_insert(spreg_table *table, table_entry *entry)
spreg_table_insert(spreg_table *table, table_entry *entry)
{
{
  /* create a new spr entry */
  /* create a new spr entry */
  spreg_table_entry *new_spr = ZALLOC(spreg_table_entry);
  spreg_table_entry *new_spr = ZALLOC(spreg_table_entry);
  new_spr->next = NULL;
  new_spr->next = NULL;
  new_spr->entry = entry;
  new_spr->entry = entry;
  new_spr->spreg_nr = atoi(entry->fields[spreg_reg_nr]);
  new_spr->spreg_nr = atoi(entry->fields[spreg_reg_nr]);
  new_spr->is_readonly = (entry->fields[spreg_readonly]
  new_spr->is_readonly = (entry->fields[spreg_readonly]
                          ? atoi(entry->fields[spreg_readonly])
                          ? atoi(entry->fields[spreg_readonly])
                          : 0);
                          : 0);
  new_spr->length = atoi(entry->fields[spreg_length]);
  new_spr->length = atoi(entry->fields[spreg_length]);
  new_spr->name = (char*)zalloc(strlen(entry->fields[spreg_name]) + 1);
  new_spr->name = (char*)zalloc(strlen(entry->fields[spreg_name]) + 1);
  ASSERT(new_spr->name != NULL);
  ASSERT(new_spr->name != NULL);
  {
  {
    int i;
    int i;
    for (i = 0; entry->fields[spreg_name][i] != '\0'; i++) {
    for (i = 0; entry->fields[spreg_name][i] != '\0'; i++) {
      if (isupper(entry->fields[spreg_name][i]))
      if (isupper(entry->fields[spreg_name][i]))
        new_spr->name[i] = tolower(entry->fields[spreg_name][i]);
        new_spr->name[i] = tolower(entry->fields[spreg_name][i]);
      else
      else
        new_spr->name[i] = entry->fields[spreg_name][i];
        new_spr->name[i] = entry->fields[spreg_name][i];
    }
    }
  }
  }
 
 
  /* insert, by spreg_nr order */
  /* insert, by spreg_nr order */
  {
  {
    spreg_table_entry **ptr_to_spreg_entry = &table->sprs;
    spreg_table_entry **ptr_to_spreg_entry = &table->sprs;
    spreg_table_entry *spreg_entry = *ptr_to_spreg_entry;
    spreg_table_entry *spreg_entry = *ptr_to_spreg_entry;
    while (spreg_entry != NULL && spreg_entry->spreg_nr < new_spr->spreg_nr) {
    while (spreg_entry != NULL && spreg_entry->spreg_nr < new_spr->spreg_nr) {
      ptr_to_spreg_entry = &spreg_entry->next;
      ptr_to_spreg_entry = &spreg_entry->next;
      spreg_entry = *ptr_to_spreg_entry;
      spreg_entry = *ptr_to_spreg_entry;
    }
    }
    ASSERT(spreg_entry == NULL || spreg_entry->spreg_nr != new_spr->spreg_nr);
    ASSERT(spreg_entry == NULL || spreg_entry->spreg_nr != new_spr->spreg_nr);
    *ptr_to_spreg_entry = new_spr;
    *ptr_to_spreg_entry = new_spr;
    new_spr->next = spreg_entry;
    new_spr->next = spreg_entry;
  }
  }
 
 
}
}
 
 
 
 
static spreg_table *
static spreg_table *
spreg_table_load(char *file_name)
spreg_table_load(char *file_name)
{
{
  table *file = table_open(file_name, nr_spreg_fields, 0);
  table *file = table_open(file_name, nr_spreg_fields, 0);
  spreg_table *table = ZALLOC(spreg_table);
  spreg_table *table = ZALLOC(spreg_table);
 
 
  {
  {
    table_entry *entry;
    table_entry *entry;
    while ((entry = table_entry_read(file)) != NULL) {
    while ((entry = table_entry_read(file)) != NULL) {
      spreg_table_insert(table, entry);
      spreg_table_insert(table, entry);
    }
    }
  }
  }
 
 
  return table;
  return table;
}
}
 
 
 
 
/****************************************************************/
/****************************************************************/
 
 
char *spreg_attributes[] = {
char *spreg_attributes[] = {
  "is_valid",
  "is_valid",
  "is_readonly",
  "is_readonly",
  "name",
  "name",
  "index",
  "index",
  "length",
  "length",
  0
  0
};
};
 
 
static void
static void
gen_spreg_h(spreg_table *table, lf *file)
gen_spreg_h(spreg_table *table, lf *file)
{
{
  spreg_table_entry *entry;
  spreg_table_entry *entry;
  char **attribute;
  char **attribute;
 
 
  lf_print__gnu_copyleft(file);
  lf_print__gnu_copyleft(file);
  lf_printf(file, "\n");
  lf_printf(file, "\n");
  lf_printf(file, "#ifndef _SPREG_H_\n");
  lf_printf(file, "#ifndef _SPREG_H_\n");
  lf_printf(file, "#define _SPREG_H_\n");
  lf_printf(file, "#define _SPREG_H_\n");
  lf_printf(file, "\n");
  lf_printf(file, "\n");
  lf_printf(file, "typedef unsigned_word spreg;\n");
  lf_printf(file, "typedef unsigned_word spreg;\n");
  lf_printf(file, "\n");
  lf_printf(file, "\n");
  lf_printf(file, "typedef enum {\n");
  lf_printf(file, "typedef enum {\n");
 
 
  for (entry = table->sprs;
  for (entry = table->sprs;
       entry != NULL ;
       entry != NULL ;
       entry = entry->next) {
       entry = entry->next) {
    lf_printf(file, "  spr_%s = %d,\n", entry->name, entry->spreg_nr);
    lf_printf(file, "  spr_%s = %d,\n", entry->name, entry->spreg_nr);
  }
  }
 
 
  lf_printf(file, "  nr_of_sprs = %d\n", nr_of_sprs);
  lf_printf(file, "  nr_of_sprs = %d\n", nr_of_sprs);
  lf_printf(file, "} sprs;\n");
  lf_printf(file, "} sprs;\n");
  lf_printf(file, "\n");
  lf_printf(file, "\n");
  for (attribute = spreg_attributes;
  for (attribute = spreg_attributes;
       *attribute != NULL;
       *attribute != NULL;
       attribute++) {
       attribute++) {
    if (strcmp(*attribute, "name") == 0) {
    if (strcmp(*attribute, "name") == 0) {
      lf_print_function_type(file, "const char *", "INLINE_SPREG", " ");
      lf_print_function_type(file, "const char *", "INLINE_SPREG", " ");
      lf_printf(file, "spr_%s(sprs spr);\n", *attribute);
      lf_printf(file, "spr_%s(sprs spr);\n", *attribute);
    }
    }
    else {
    else {
      lf_print_function_type(file, "int", "INLINE_SPREG", " ");
      lf_print_function_type(file, "int", "INLINE_SPREG", " ");
      lf_printf(file, "spr_%s(sprs spr);\n", *attribute);
      lf_printf(file, "spr_%s(sprs spr);\n", *attribute);
    }
    }
  }
  }
  lf_printf(file, "\n");
  lf_printf(file, "\n");
  lf_printf(file, "#endif /* _SPREG_H_ */\n");
  lf_printf(file, "#endif /* _SPREG_H_ */\n");
}
}
 
 
 
 
static void
static void
gen_spreg_c(spreg_table *table, lf *file)
gen_spreg_c(spreg_table *table, lf *file)
{
{
  spreg_table_entry *entry;
  spreg_table_entry *entry;
  char **attribute;
  char **attribute;
  int spreg_nr;
  int spreg_nr;
 
 
  lf_print__gnu_copyleft(file);
  lf_print__gnu_copyleft(file);
  lf_printf(file, "\n");
  lf_printf(file, "\n");
  lf_printf(file, "#ifndef _SPREG_C_\n");
  lf_printf(file, "#ifndef _SPREG_C_\n");
  lf_printf(file, "#define _SPREG_C_\n");
  lf_printf(file, "#define _SPREG_C_\n");
  lf_printf(file, "\n");
  lf_printf(file, "\n");
  lf_printf(file, "#include \"basics.h\"\n");
  lf_printf(file, "#include \"basics.h\"\n");
  lf_printf(file, "#include \"spreg.h\"\n");
  lf_printf(file, "#include \"spreg.h\"\n");
 
 
  lf_printf(file, "\n");
  lf_printf(file, "\n");
  lf_printf(file, "typedef struct _spreg_info {\n");
  lf_printf(file, "typedef struct _spreg_info {\n");
  lf_printf(file, "  char *name;\n");
  lf_printf(file, "  char *name;\n");
  lf_printf(file, "  int is_valid;\n");
  lf_printf(file, "  int is_valid;\n");
  lf_printf(file, "  int length;\n");
  lf_printf(file, "  int length;\n");
  lf_printf(file, "  int is_readonly;\n");
  lf_printf(file, "  int is_readonly;\n");
  lf_printf(file, "  int index;\n");
  lf_printf(file, "  int index;\n");
  lf_printf(file, "} spreg_info;\n");
  lf_printf(file, "} spreg_info;\n");
  lf_printf(file, "\n");
  lf_printf(file, "\n");
  lf_printf(file, "static spreg_info spr_info[nr_of_sprs+1] = {\n");
  lf_printf(file, "static spreg_info spr_info[nr_of_sprs+1] = {\n");
  entry = table->sprs;
  entry = table->sprs;
  for (spreg_nr = 0; spreg_nr < nr_of_sprs+1; spreg_nr++) {
  for (spreg_nr = 0; spreg_nr < nr_of_sprs+1; spreg_nr++) {
    if (entry == NULL || spreg_nr < entry->spreg_nr)
    if (entry == NULL || spreg_nr < entry->spreg_nr)
      lf_printf(file, "  { 0, 0, 0, 0, %d},\n", spreg_nr);
      lf_printf(file, "  { 0, 0, 0, 0, %d},\n", spreg_nr);
    else {
    else {
      lf_printf(file, "  { \"%s\", %d, %d, %d, spr_%s /*%d*/ },\n",
      lf_printf(file, "  { \"%s\", %d, %d, %d, spr_%s /*%d*/ },\n",
                entry->name, 1, entry->length, entry->is_readonly,
                entry->name, 1, entry->length, entry->is_readonly,
                entry->name, entry->spreg_nr);
                entry->name, entry->spreg_nr);
      entry = entry->next;
      entry = entry->next;
    }
    }
  }
  }
  lf_printf(file, "};\n");
  lf_printf(file, "};\n");
 
 
  for (attribute = spreg_attributes;
  for (attribute = spreg_attributes;
       *attribute != NULL;
       *attribute != NULL;
       attribute++) {
       attribute++) {
    lf_printf(file, "\n");
    lf_printf(file, "\n");
    if (strcmp(*attribute, "name") == 0) {
    if (strcmp(*attribute, "name") == 0) {
      lf_print_function_type(file, "const char *", "INLINE_SPREG", "\n");
      lf_print_function_type(file, "const char *", "INLINE_SPREG", "\n");
    }
    }
    else {
    else {
      lf_print_function_type(file, "int", "INLINE_SPREG", "\n");
      lf_print_function_type(file, "int", "INLINE_SPREG", "\n");
    }
    }
    lf_printf(file, "spr_%s(sprs spr)\n", *attribute);
    lf_printf(file, "spr_%s(sprs spr)\n", *attribute);
    lf_printf(file, "{\n");
    lf_printf(file, "{\n");
    if (spreg_lookup_table
    if (spreg_lookup_table
        || strcmp(*attribute, "name") == 0
        || strcmp(*attribute, "name") == 0
        || strcmp(*attribute, "index") == 0)
        || strcmp(*attribute, "index") == 0)
      lf_printf(file, "  return spr_info[spr].%s;\n",
      lf_printf(file, "  return spr_info[spr].%s;\n",
                *attribute);
                *attribute);
    else {
    else {
      spreg_table_entry *entry;
      spreg_table_entry *entry;
      lf_printf(file, "  switch (spr) {\n");
      lf_printf(file, "  switch (spr) {\n");
      for (entry = table->sprs; entry != NULL; entry = entry->next) {
      for (entry = table->sprs; entry != NULL; entry = entry->next) {
        lf_printf(file, "  case %d:\n", entry->spreg_nr);
        lf_printf(file, "  case %d:\n", entry->spreg_nr);
        if (strcmp(*attribute, "is_valid") == 0)
        if (strcmp(*attribute, "is_valid") == 0)
          lf_printf(file, "    return 1;\n");
          lf_printf(file, "    return 1;\n");
        else if (strcmp(*attribute, "is_readonly") == 0)
        else if (strcmp(*attribute, "is_readonly") == 0)
          lf_printf(file, "    return %d;\n", entry->is_readonly);
          lf_printf(file, "    return %d;\n", entry->is_readonly);
        else if (strcmp(*attribute, "length") == 0)
        else if (strcmp(*attribute, "length") == 0)
          lf_printf(file, "    return %d;\n", entry->length);
          lf_printf(file, "    return %d;\n", entry->length);
        else
        else
          ASSERT(0);
          ASSERT(0);
      }
      }
      lf_printf(file, "  default:\n");
      lf_printf(file, "  default:\n");
      lf_printf(file, "    return 0;\n");
      lf_printf(file, "    return 0;\n");
      lf_printf(file, "  }\n");
      lf_printf(file, "  }\n");
    }
    }
    lf_printf(file, "}\n");
    lf_printf(file, "}\n");
  }
  }
 
 
  lf_printf(file, "\n");
  lf_printf(file, "\n");
  lf_printf(file, "#endif /* _SPREG_C_ */\n");
  lf_printf(file, "#endif /* _SPREG_C_ */\n");
}
}
 
 
 
 
 
 
/****************************************************************/
/****************************************************************/
 
 
 
 
int
int
main(int argc,
main(int argc,
     char **argv,
     char **argv,
     char **envp)
     char **envp)
{
{
  lf_file_references file_references = lf_include_references;
  lf_file_references file_references = lf_include_references;
  spreg_table *sprs = NULL;
  spreg_table *sprs = NULL;
  char *real_file_name = NULL;
  char *real_file_name = NULL;
  int is_header = 0;
  int is_header = 0;
  int ch;
  int ch;
 
 
  if (argc <= 1) {
  if (argc <= 1) {
    printf("Usage: dgen ...\n");
    printf("Usage: dgen ...\n");
    printf("-s  Use switch instead of table\n");
    printf("-s  Use switch instead of table\n");
    printf("-n <file-name>  Use this as cpp line numbering name\n");
    printf("-n <file-name>  Use this as cpp line numbering name\n");
    printf("-h  Output header file\n");
    printf("-h  Output header file\n");
    printf("-p <spreg-file>  Output spreg.h(P) or spreg.c(p)\n");
    printf("-p <spreg-file>  Output spreg.h(P) or spreg.c(p)\n");
    printf("-L  Suppress cpp line numbering in output files\n");
    printf("-L  Suppress cpp line numbering in output files\n");
  }
  }
 
 
 
 
  while ((ch = getopt(argc, argv, "hLsn:r:p:")) != -1) {
  while ((ch = getopt(argc, argv, "hLsn:r:p:")) != -1) {
    fprintf(stderr, "\t-%c %s\n", ch, ( optarg ? optarg : ""));
    fprintf(stderr, "\t-%c %s\n", ch, ( optarg ? optarg : ""));
    switch(ch) {
    switch(ch) {
    case 's':
    case 's':
      spreg_lookup_table = 0;
      spreg_lookup_table = 0;
      break;
      break;
    case 'r':
    case 'r':
      sprs = spreg_table_load(optarg);
      sprs = spreg_table_load(optarg);
      break;
      break;
    case 'n':
    case 'n':
      real_file_name = strdup(optarg);
      real_file_name = strdup(optarg);
      break;
      break;
    case 'L':
    case 'L':
      file_references = lf_omit_references;
      file_references = lf_omit_references;
      break;
      break;
    case 'h':
    case 'h':
      is_header = 1;
      is_header = 1;
      break;
      break;
    case 'p':
    case 'p':
      {
      {
        lf *file = lf_open(optarg, real_file_name, file_references,
        lf *file = lf_open(optarg, real_file_name, file_references,
                           (is_header ? lf_is_h : lf_is_c),
                           (is_header ? lf_is_h : lf_is_c),
                           argv[0]);
                           argv[0]);
        if (is_header)
        if (is_header)
          gen_spreg_h(sprs, file);
          gen_spreg_h(sprs, file);
        else
        else
          gen_spreg_c(sprs, file);
          gen_spreg_c(sprs, file);
        lf_close(file);
        lf_close(file);
        is_header = 0;
        is_header = 0;
      }
      }
      real_file_name = NULL;
      real_file_name = NULL;
      break;
      break;
    default:
    default:
      error("unknown option\n");
      error("unknown option\n");
    }
    }
  }
  }
  return 0;
  return 0;
}
}
 
 

powered by: WebSVN 2.1.0

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