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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-7.1/] [sim/] [ppc/] [lf.c] - Diff between revs 834 and 842

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

Rev 834 Rev 842
/*  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 <stdio.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdarg.h>
#include <ctype.h>
#include <ctype.h>
 
 
#include "build-config.h"
#include "build-config.h"
#include "misc.h"
#include "misc.h"
#include "lf.h"
#include "lf.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>
#else
#else
#ifdef HAVE_STRINGS_H
#ifdef HAVE_STRINGS_H
#include <strings.h>
#include <strings.h>
#endif
#endif
#endif
#endif
 
 
struct _lf {
struct _lf {
  FILE *stream;
  FILE *stream;
  int line_nr; /* nr complete lines written, curr line is line_nr+1 */
  int line_nr; /* nr complete lines written, curr line is line_nr+1 */
  int indent;
  int indent;
  int line_blank;
  int line_blank;
  const char *name;
  const char *name;
  const char *program;
  const char *program;
  lf_file_references references;
  lf_file_references references;
  lf_file_type type;
  lf_file_type type;
};
};
 
 
 
 
lf *
lf *
lf_open(char *name,
lf_open(char *name,
        char *real_name,
        char *real_name,
        lf_file_references references,
        lf_file_references references,
        lf_file_type type,
        lf_file_type type,
        const char *program)
        const char *program)
{
{
  /* create a file object */
  /* create a file object */
  lf *new_lf = ZALLOC(lf);
  lf *new_lf = ZALLOC(lf);
  ASSERT(new_lf != NULL);
  ASSERT(new_lf != NULL);
  new_lf->references = references;
  new_lf->references = references;
  new_lf->type = type;
  new_lf->type = type;
  new_lf->name = (real_name == NULL ? name : real_name);
  new_lf->name = (real_name == NULL ? name : real_name);
  new_lf->program = program;
  new_lf->program = program;
  /* attach to stdout if pipe */
  /* attach to stdout if pipe */
  if (!strcmp(name, "-")) {
  if (!strcmp(name, "-")) {
    new_lf->stream = stdout;
    new_lf->stream = stdout;
  }
  }
  else {
  else {
    /* create a new file */
    /* create a new file */
    new_lf->stream = fopen(name, "w");
    new_lf->stream = fopen(name, "w");
    if (new_lf->stream == NULL) {
    if (new_lf->stream == NULL) {
      perror(name);
      perror(name);
      exit(1);
      exit(1);
    }
    }
  }
  }
  return new_lf;
  return new_lf;
}
}
 
 
 
 
void
void
lf_close(lf *file)
lf_close(lf *file)
{
{
  if (file->stream != stdout) {
  if (file->stream != stdout) {
    if (fclose(file->stream)) {
    if (fclose(file->stream)) {
      perror("lf_close.fclose");
      perror("lf_close.fclose");
      exit(1);
      exit(1);
    }
    }
    free(file);
    free(file);
  }
  }
}
}
 
 
 
 
int
int
lf_putchr(lf *file,
lf_putchr(lf *file,
          const char chr)
          const char chr)
{
{
  int nr = 0;
  int nr = 0;
  if (chr == '\n') {
  if (chr == '\n') {
    file->line_nr += 1;
    file->line_nr += 1;
    file->line_blank = 1;
    file->line_blank = 1;
  }
  }
  else if (file->line_blank) {
  else if (file->line_blank) {
    int pad;
    int pad;
    for (pad = file->indent; pad > 0; pad--)
    for (pad = file->indent; pad > 0; pad--)
      putc(' ', file->stream);
      putc(' ', file->stream);
    nr += file->indent;
    nr += file->indent;
    file->line_blank = 0;
    file->line_blank = 0;
  }
  }
  putc(chr, file->stream);
  putc(chr, file->stream);
  nr += 1;
  nr += 1;
  return nr;
  return nr;
}
}
 
 
void
void
lf_indent_suppress(lf *file)
lf_indent_suppress(lf *file)
{
{
  file->line_blank = 0;
  file->line_blank = 0;
}
}
 
 
 
 
int
int
lf_putstr(lf *file,
lf_putstr(lf *file,
          const char *string)
          const char *string)
{
{
  int nr = 0;
  int nr = 0;
  const char *chp;
  const char *chp;
  if (string != NULL) {
  if (string != NULL) {
    for (chp = string; *chp != '\0'; chp++) {
    for (chp = string; *chp != '\0'; chp++) {
      nr += lf_putchr(file, *chp);
      nr += lf_putchr(file, *chp);
    }
    }
  }
  }
  return nr;
  return nr;
}
}
 
 
static int
static int
do_lf_putunsigned(lf *file,
do_lf_putunsigned(lf *file,
              unsigned u)
              unsigned u)
{
{
  int nr = 0;
  int nr = 0;
  if (u > 0) {
  if (u > 0) {
    nr += do_lf_putunsigned(file, u / 10);
    nr += do_lf_putunsigned(file, u / 10);
    nr += lf_putchr(file, (u % 10) + '0');
    nr += lf_putchr(file, (u % 10) + '0');
  }
  }
  return nr;
  return nr;
}
}
 
 
 
 
int
int
lf_putint(lf *file,
lf_putint(lf *file,
          int decimal)
          int decimal)
{
{
  int nr = 0;
  int nr = 0;
  if (decimal == 0)
  if (decimal == 0)
    nr += lf_putchr(file, '0');
    nr += lf_putchr(file, '0');
  else if (decimal < 0) {
  else if (decimal < 0) {
    nr += lf_putchr(file, '-');
    nr += lf_putchr(file, '-');
    nr += do_lf_putunsigned(file, -decimal);
    nr += do_lf_putunsigned(file, -decimal);
  }
  }
  else if (decimal > 0) {
  else if (decimal > 0) {
    nr += do_lf_putunsigned(file, decimal);
    nr += do_lf_putunsigned(file, decimal);
  }
  }
  else
  else
    ASSERT(0);
    ASSERT(0);
  return nr;
  return nr;
}
}
 
 
 
 
int
int
lf_printf(lf *file,
lf_printf(lf *file,
          const char *fmt,
          const char *fmt,
          ...)
          ...)
{
{
  int nr = 0;
  int nr = 0;
  char buf[1024];
  char buf[1024];
  va_list ap;
  va_list ap;
 
 
  va_start(ap, fmt);
  va_start(ap, fmt);
  vsprintf(buf, fmt, ap);
  vsprintf(buf, fmt, ap);
  /* FIXME - this is really stuffed but so is vsprintf() on a sun! */
  /* FIXME - this is really stuffed but so is vsprintf() on a sun! */
  ASSERT(strlen(buf) > 0 && strlen(buf) < sizeof(buf));
  ASSERT(strlen(buf) > 0 && strlen(buf) < sizeof(buf));
  nr += lf_putstr(file, buf);
  nr += lf_putstr(file, buf);
  va_end(ap);
  va_end(ap);
  return nr;
  return nr;
}
}
 
 
 
 
int
int
lf_print__c_code(lf *file,
lf_print__c_code(lf *file,
                 const char *code)
                 const char *code)
{
{
  int nr = 0;
  int nr = 0;
  const char *chp = code;
  const char *chp = code;
  int in_bit_field = 0;
  int in_bit_field = 0;
  while (*chp != '\0') {
  while (*chp != '\0') {
    if (*chp == '\t')
    if (*chp == '\t')
      chp++;
      chp++;
    if (*chp == '#')
    if (*chp == '#')
      lf_indent_suppress(file);
      lf_indent_suppress(file);
    while (*chp != '\0' && *chp != '\n') {
    while (*chp != '\0' && *chp != '\n') {
      if (chp[0] == '{' && !isspace(chp[1])) {
      if (chp[0] == '{' && !isspace(chp[1])) {
        in_bit_field = 1;
        in_bit_field = 1;
        nr += lf_putchr(file, '_');
        nr += lf_putchr(file, '_');
      }
      }
      else if (in_bit_field && chp[0] == ':') {
      else if (in_bit_field && chp[0] == ':') {
        nr += lf_putchr(file, '_');
        nr += lf_putchr(file, '_');
      }
      }
      else if (in_bit_field && *chp == '}') {
      else if (in_bit_field && *chp == '}') {
        nr += lf_putchr(file, '_');
        nr += lf_putchr(file, '_');
        in_bit_field = 0;
        in_bit_field = 0;
      }
      }
      else {
      else {
        nr += lf_putchr(file, *chp);
        nr += lf_putchr(file, *chp);
      }
      }
      chp++;
      chp++;
    }
    }
    if (in_bit_field)
    if (in_bit_field)
      error("bit field paren miss match some where\n");
      error("bit field paren miss match some where\n");
    if (*chp == '\n') {
    if (*chp == '\n') {
      nr += lf_putchr(file, '\n');
      nr += lf_putchr(file, '\n');
      chp++;
      chp++;
    }
    }
  }
  }
  nr += lf_putchr(file, '\n');
  nr += lf_putchr(file, '\n');
  return nr;
  return nr;
}
}
 
 
 
 
int
int
lf_print__external_reference(lf *file,
lf_print__external_reference(lf *file,
                             int line_nr,
                             int line_nr,
                             const char *file_name)
                             const char *file_name)
{
{
  int nr = 0;
  int nr = 0;
  switch (file->references) {
  switch (file->references) {
  case lf_include_references:
  case lf_include_references:
    lf_indent_suppress(file);
    lf_indent_suppress(file);
    nr += lf_putstr(file, "#line ");
    nr += lf_putstr(file, "#line ");
    nr += lf_putint(file, line_nr);
    nr += lf_putint(file, line_nr);
    nr += lf_putstr(file, " \"");
    nr += lf_putstr(file, " \"");
    nr += lf_putstr(file, file_name);
    nr += lf_putstr(file, file_name);
    nr += lf_putstr(file, "\"\n");
    nr += lf_putstr(file, "\"\n");
    break;
    break;
  case lf_omit_references:
  case lf_omit_references:
    break;
    break;
  }
  }
  return nr;
  return nr;
}
}
 
 
int
int
lf_print__internal_reference(lf *file)
lf_print__internal_reference(lf *file)
{
{
  int nr = 0;
  int nr = 0;
  nr += lf_print__external_reference(file, file->line_nr+2, file->name);
  nr += lf_print__external_reference(file, file->line_nr+2, file->name);
  /* line_nr == last_line, want to number from next */
  /* line_nr == last_line, want to number from next */
  return nr;
  return nr;
}
}
 
 
void
void
lf_indent(lf *file, int delta)
lf_indent(lf *file, int delta)
{
{
  file->indent += delta;
  file->indent += delta;
}
}
 
 
 
 
int
int
lf_print__gnu_copyleft(lf *file)
lf_print__gnu_copyleft(lf *file)
{
{
  int nr = 0;
  int nr = 0;
  switch (file->type) {
  switch (file->type) {
  case lf_is_c:
  case lf_is_c:
  case lf_is_h:
  case lf_is_h:
    nr += lf_printf(file, "\n\
    nr += lf_printf(file, "\n\
/*  This file is part of the program psim.\n\
/*  This file is part of the program psim.\n\
\n\
\n\
    Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>\n\
    Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>\n\
\n\
\n\
    This program is free software; you can redistribute it and/or modify\n\
    This program is free software; you can redistribute it and/or modify\n\
    it under the terms of the GNU General Public License as published by\n\
    it under the terms of the GNU General Public License as published by\n\
    the Free Software Foundation; either version 2 of the License, or\n\
    the Free Software Foundation; either version 2 of the License, or\n\
    (at your option) any later version.\n\
    (at your option) any later version.\n\
\n\
\n\
    This program is distributed in the hope that it will be useful,\n\
    This program is distributed in the hope that it will be useful,\n\
    but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
    but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
    GNU General Public License for more details.\n\
    GNU General Public License for more details.\n\
 \n\
 \n\
    You should have received a copy of the GNU General Public License\n\
    You should have received a copy of the GNU General Public License\n\
    along with this program; if not, write to the Free Software\n\
    along with this program; if not, write to the Free Software\n\
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\
 \n\
 \n\
    --\n\
    --\n\
\n\
\n\
    This file was generated by the program %s */\n\
    This file was generated by the program %s */\n\
", filter_filename(file->program));
", filter_filename(file->program));
    break;
    break;
  default:
  default:
    ASSERT(0);
    ASSERT(0);
    break;
    break;
  }
  }
  return nr;
  return nr;
}
}
 
 
 
 
int
int
lf_putbin(lf *file, int decimal, int width)
lf_putbin(lf *file, int decimal, int width)
{
{
  int nr = 0;
  int nr = 0;
  int bit;
  int bit;
  ASSERT(width > 0);
  ASSERT(width > 0);
  for (bit = 1 << (width-1); bit != 0; bit >>= 1) {
  for (bit = 1 << (width-1); bit != 0; bit >>= 1) {
    if (decimal & bit)
    if (decimal & bit)
      nr += lf_putchr(file, '1');
      nr += lf_putchr(file, '1');
    else
    else
      nr += lf_putchr(file, '0');
      nr += lf_putchr(file, '0');
  }
  }
  return nr;
  return nr;
}
}
 
 
int
int
lf_print__this_file_is_empty(lf *file)
lf_print__this_file_is_empty(lf *file)
{
{
  int nr = 0;
  int nr = 0;
  switch (file->type) {
  switch (file->type) {
  case lf_is_c:
  case lf_is_c:
  case lf_is_h:
  case lf_is_h:
    nr += lf_printf(file,
    nr += lf_printf(file,
                    "/* This generated file (%s) is intentionally left blank */\n",
                    "/* This generated file (%s) is intentionally left blank */\n",
                    file->name);
                    file->name);
    break;
    break;
  default:
  default:
    ASSERT(0);
    ASSERT(0);
  }
  }
  return nr;
  return nr;
}
}
 
 
int
int
lf_print__ucase_filename(lf *file)
lf_print__ucase_filename(lf *file)
{
{
  int nr = 0;
  int nr = 0;
  const char *chp = file->name;
  const char *chp = file->name;
  while (*chp != '\0') {
  while (*chp != '\0') {
    char ch = *chp;
    char ch = *chp;
    if (islower(ch)) {
    if (islower(ch)) {
      nr += lf_putchr(file, toupper(ch));
      nr += lf_putchr(file, toupper(ch));
    }
    }
    else if (ch == '.')
    else if (ch == '.')
      nr += lf_putchr(file, '_');
      nr += lf_putchr(file, '_');
    else
    else
      nr += lf_putchr(file, ch);
      nr += lf_putchr(file, ch);
    chp++;
    chp++;
  }
  }
  return nr;
  return nr;
}
}
 
 
int
int
lf_print__file_start(lf *file)
lf_print__file_start(lf *file)
{
{
  int nr = 0;
  int nr = 0;
  switch (file->type) {
  switch (file->type) {
  case lf_is_h:
  case lf_is_h:
  case lf_is_c:
  case lf_is_c:
    nr += lf_print__gnu_copyleft(file);
    nr += lf_print__gnu_copyleft(file);
    nr += lf_printf(file, "\n");
    nr += lf_printf(file, "\n");
    nr += lf_printf(file, "#ifndef _");
    nr += lf_printf(file, "#ifndef _");
    nr += lf_print__ucase_filename(file);
    nr += lf_print__ucase_filename(file);
    nr += lf_printf(file, "_\n");
    nr += lf_printf(file, "_\n");
    nr += lf_printf(file, "#define _");
    nr += lf_printf(file, "#define _");
    nr += lf_print__ucase_filename(file);
    nr += lf_print__ucase_filename(file);
    nr += lf_printf(file, "_\n");
    nr += lf_printf(file, "_\n");
    nr += lf_printf(file, "\n");
    nr += lf_printf(file, "\n");
    break;
    break;
  default:
  default:
    ASSERT(0);
    ASSERT(0);
  }
  }
  return nr;
  return nr;
}
}
 
 
 
 
int
int
lf_print__file_finish(lf *file)
lf_print__file_finish(lf *file)
{
{
  int nr = 0;
  int nr = 0;
  switch (file->type) {
  switch (file->type) {
  case lf_is_h:
  case lf_is_h:
  case lf_is_c:
  case lf_is_c:
    nr += lf_printf(file, "\n");
    nr += lf_printf(file, "\n");
    nr += lf_printf(file, "#endif /* _");
    nr += lf_printf(file, "#endif /* _");
    nr += lf_print__ucase_filename(file);
    nr += lf_print__ucase_filename(file);
    nr += lf_printf(file, "_*/\n");
    nr += lf_printf(file, "_*/\n");
    break;
    break;
  default:
  default:
    ASSERT(0);
    ASSERT(0);
  }
  }
  return nr;
  return nr;
}
}
 
 
 
 
int
int
lf_print_function_type(lf *file,
lf_print_function_type(lf *file,
                       const char *type,
                       const char *type,
                       const char *prefix,
                       const char *prefix,
                       const char *trailing_space)
                       const char *trailing_space)
{
{
  int nr = 0;
  int nr = 0;
  nr += lf_printf(file, "%s\\\n(%s)", prefix, type);
  nr += lf_printf(file, "%s\\\n(%s)", prefix, type);
  if (trailing_space != NULL)
  if (trailing_space != NULL)
    nr += lf_printf(file, "%s", trailing_space);
    nr += lf_printf(file, "%s", trailing_space);
#if 0
#if 0
  const char *type_pointer = strrchr(type, '*');
  const char *type_pointer = strrchr(type, '*');
  int type_pointer_offset = (type_pointer != NULL
  int type_pointer_offset = (type_pointer != NULL
                             ? type_pointer - type
                             ? type_pointer - type
                             : 0);
                             : 0);
  if (type_pointer == NULL) {
  if (type_pointer == NULL) {
    lf_printf(file, "%s %s", type, prefix);
    lf_printf(file, "%s %s", type, prefix);
  }
  }
  else {
  else {
    char *munged_type = (char*)zalloc(strlen(type)
    char *munged_type = (char*)zalloc(strlen(type)
                                      + strlen(prefix)
                                      + strlen(prefix)
                                      + strlen(" * ")
                                      + strlen(" * ")
                                      + 1);
                                      + 1);
    strcpy(munged_type, type);
    strcpy(munged_type, type);
    munged_type[type_pointer_offset] = '\0';
    munged_type[type_pointer_offset] = '\0';
    if (type_pointer_offset > 0 && type[type_pointer_offset-1] != ' ')
    if (type_pointer_offset > 0 && type[type_pointer_offset-1] != ' ')
      strcat(munged_type, " ");
      strcat(munged_type, " ");
    strcat(munged_type, prefix);
    strcat(munged_type, prefix);
    strcat(munged_type, " ");
    strcat(munged_type, " ");
    strcat(munged_type, type + type_pointer_offset);
    strcat(munged_type, type + type_pointer_offset);
    lf_printf(file, "%s", munged_type);
    lf_printf(file, "%s", munged_type);
    free(munged_type);
    free(munged_type);
  }
  }
  if (trailing_space != NULL && type_pointer_offset < strlen(type) - 1)
  if (trailing_space != NULL && type_pointer_offset < strlen(type) - 1)
    lf_printf(file, trailing_space);
    lf_printf(file, trailing_space);
#endif
#endif
  return nr;
  return nr;
}
}
 
 
 
 

powered by: WebSVN 2.1.0

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