From d7913476c462b480f11e14b6829942f2691c6c01 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Mon, 2 Oct 2000 08:48:32 +0000 Subject: [PATCH] Use the more standard files `xalloc.h' and `xmalloc.c' instead of Bison's `allocate.c' and `alloc.h'. This patch was surprisingly difficult and introduced a lot of core dump. It turns out that Bison used an implementation of `xmalloc' based on `calloc', and at various places it does depend upon the initialization to 0. I have not tried to isolate the pertinent places, and all the former calls to Bison's `xmalloc' are now using `XCALLOC'. Someday, someone should address this issue. * src/allocate.c, src/alloc.h, m4/bison-decl.m4: Remove. * lib/xmalloc.c, lib/xalloc.h, m4/malloc.m4, m4/realloc.m4: New files. Adjust dependencies. * src/warshall.h: New file. Propagate. --- ChangeLog | 30 +++++++++++--- configure.in | 3 +- lib/Makefile.am | 4 +- lib/xalloc.h | 87 +++++++++++++++++++++++++++++++++++++++++ lib/xmalloc.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ m4/Makefile.am | 3 +- m4/bison-decl.m4 | 48 ----------------------- m4/malloc.m4 | 35 +++++++++++++++++ m4/realloc.m4 | 35 +++++++++++++++++ src/LR0.c | 70 ++++++++++++++++----------------- src/Makefile.am | 6 +-- src/alloc.h | 36 ----------------- src/allocate.c | 79 ------------------------------------- src/closure.c | 19 ++++----- src/conflicts.c | 20 +++++----- src/derives.c | 34 ++++++++-------- src/files.c | 10 ++--- src/lalr.c | 98 +++++++++++++++++++++++----------------------- src/lex.c | 6 +-- src/main.c | 1 + src/nullable.c | 22 +++++------ src/output.c | 88 ++++++++++++++++++++--------------------- src/print.c | 4 +- src/reader.c | 51 ++++++++++++------------ src/reduce.c | 30 +++++++------- src/symtab.c | 14 +++---- src/warshall.c | 56 +++++++++++++------------- src/warshall.h | 25 ++++++++++++ 28 files changed, 593 insertions(+), 438 deletions(-) create mode 100644 lib/xalloc.h create mode 100644 lib/xmalloc.c create mode 100644 m4/malloc.m4 create mode 100644 m4/realloc.m4 create mode 100644 src/warshall.h diff --git a/ChangeLog b/ChangeLog index b7a14eb..9448a2e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,10 +1,28 @@ 2000-10-02 Akim Demaille + Use the more standard files `xalloc.h' and `xmalloc.c' instead of + Bison's `allocate.c' and `alloc.h'. This patch was surprisingly + difficult and introduced a lot of core dump. It turns out that + Bison used an implementation of `xmalloc' based on `calloc', and + at various places it does depend upon the initialization to 0. I + have not tried to isolate the pertinent places, and all the former + calls to Bison's `xmalloc' are now using `XCALLOC'. Someday, + someone should address this issue. + + * src/allocate.c, src/alloc.h, m4/bison-decl.m4: Remove. + * lib/xmalloc.c, lib/xalloc.h, m4/malloc.m4, m4/realloc.m4: New + files. + Adjust dependencies. + * src/warshall.h: New file. + Propagate. + +2000-10-02 Akim Demaille + Various anti-`extern in *.c' changes. * src/system.h: Include `assert.h'. - + 2000-10-02 Akim Demaille * src/state.h (nstates, final_state, first_state, first_shift) @@ -17,13 +35,13 @@ Remove a lot of now useless `extern' statements in most files. - + 2000-10-02 Akim Demaille * src/LR0.h: New file. Propagate its use. - + 2000-10-02 Akim Demaille * src/print.h: New file. @@ -33,7 +51,7 @@ (print_results): this new function. Adjust dependencies. - + 2000-10-02 Akim Demaille * src/conflicts.c (conflict_report): New function. @@ -43,14 +61,14 @@ * src/conflicts.h: New file. Propagate its inclusion. - + 2000-10-02 Akim Demaille * src/nullable.h: New file. Propagate its inclusion. * src/nullable.c: Formatting changes. - + 2000-10-02 Akim Demaille * src/reduce.h: New file. diff --git a/configure.in b/configure.in index b7ef080..41c5e27 100644 --- a/configure.in +++ b/configure.in @@ -64,7 +64,8 @@ AM_C_PROTOTYPES # Checks for library functions. AC_FUNC_ALLOCA AC_CHECK_FUNCS(mkstemp setlocale) -BISON_NEED_DECLARATIONS(calloc realloc) +jm_FUNC_MALLOC +jm_FUNC_REALLOC ALL_LINGUAS="de es et fr ja nl ru" AM_GNU_GETTEXT diff --git a/lib/Makefile.am b/lib/Makefile.am index 9a4e37f..b5317f2 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -6,8 +6,8 @@ noinst_LIBRARIES = libbison.a INCLUDES = -I.. -I$(srcdir) -I../intl -libbison_a_SOURCES = getopt.c getopt1.c -noinst_HEADERS = getopt.h +libbison_a_SOURCES = getopt.c getopt1.c xmalloc.c +noinst_HEADERS = getopt.h xalloc.h libbison_a_LIBADD = @LIBOBJS@ @ALLOCA@ libbison_a_DEPENDENCIES = $(libbison_a_LIBADD) diff --git a/lib/xalloc.h b/lib/xalloc.h new file mode 100644 index 0000000..8668e19 --- /dev/null +++ b/lib/xalloc.h @@ -0,0 +1,87 @@ +/* xalloc.h -- malloc with out-of-memory checking + Copyright (C) 1990-1998, 1999 Free Software Foundation, Inc. + + 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 Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#ifndef XALLOC_H_ +# define XALLOC_H_ + +# ifndef PARAMS +# if defined PROTOTYPES || (defined __STDC__ && __STDC__) +# define PARAMS(Args) Args +# else +# define PARAMS(Args) () +# endif +# endif + +# ifndef __attribute__ +# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__ +# define __attribute__(x) +# endif +# endif + +# ifndef ATTRIBUTE_NORETURN +# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) +# endif + +/* Exit value when the requested amount of memory is not available. + It is initialized to EXIT_FAILURE, but the caller may set it to + some other value. */ +extern int xalloc_exit_failure; + +/* If this pointer is non-zero, run the specified function upon each + allocation failure. It is initialized to zero. */ +extern void (*xalloc_fail_func) PARAMS ((void)); + +/* If XALLOC_FAIL_FUNC is undefined or a function that returns, this + message must be non-NULL. It is translated via gettext. + The default value is "Memory exhausted". */ +extern char *const xalloc_msg_memory_exhausted; + +/* This function is always triggered when memory is exhausted. It is + in charge of honoring the three previous items. This is the + function to call when one wants the program to die because of a + memory allocation failure. */ +extern void xalloc_die PARAMS ((void)) ATTRIBUTE_NORETURN; + +void *xmalloc PARAMS ((size_t n)); +void *xcalloc PARAMS ((size_t n, size_t s)); +void *xrealloc PARAMS ((void *p, size_t n)); +char *xstrdup PARAMS ((const char *str)); + +# define XMALLOC(Type, N_items) ((Type *) xmalloc (sizeof (Type) * (N_items))) +# define XCALLOC(Type, N_items) ((Type *) xcalloc (sizeof (Type), (N_items))) +# define XREALLOC(Ptr, Type, N_items) \ + ((Type *) xrealloc ((void *) (Ptr), sizeof (Type) * (N_items))) + +/* Declare and alloc memory for VAR of type TYPE. */ +# define NEW(Type, Var) Type *(Var) = XMALLOC (Type, 1) + +/* Free VAR only if non NULL. */ +# define XFREE(Var) \ + do { \ + if (Var) \ + free (Var); \ + } while (0) + +/* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */ +# define CCLONE(Src, Num) \ + (memcpy (xmalloc (sizeof (*Src) * (Num)), (Src), sizeof (*Src) * (Num))) + +/* Return a malloc'ed copy of SRC. */ +# define CLONE(Src) CCLONE (Src, 1) + + +#endif /* !XALLOC_H_ */ diff --git a/lib/xmalloc.c b/lib/xmalloc.c new file mode 100644 index 0000000..8206a94 --- /dev/null +++ b/lib/xmalloc.c @@ -0,0 +1,117 @@ +/* xmalloc.c -- malloc with out of memory checking + Copyright (C) 1990-1997, 98, 99 Free Software Foundation, Inc. + + 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 Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#if HAVE_CONFIG_H +# include +#endif + +#include + +#if STDC_HEADERS +# include +#else +void *calloc (); +void *malloc (); +void *realloc (); +void free (); +#endif + +#if ENABLE_NLS +# include +# define _(Text) gettext (Text) +#else +# define textdomain(Domain) +# define _(Text) Text +#endif +#define N_(Text) Text + +#include "error.h" +#include "xalloc.h" + +#ifndef EXIT_FAILURE +# define EXIT_FAILURE 1 +#endif + +#ifndef HAVE_DONE_WORKING_MALLOC_CHECK +you must run the autoconf test for a properly working malloc -- see malloc.m4 +#endif + +#ifndef HAVE_DONE_WORKING_REALLOC_CHECK +you must run the autoconf test for a properly working realloc -- see realloc.m4 +#endif + +/* Exit value when the requested amount of memory is not available. + The caller may set it to some other value. */ +int xalloc_exit_failure = EXIT_FAILURE; + +/* If non NULL, call this function when memory is exhausted. */ +void (*xalloc_fail_func) PARAMS ((void)) = 0; + +/* If XALLOC_FAIL_FUNC is NULL, or does return, display this message + before exiting when memory is exhausted. Goes through gettext. */ +char *const xalloc_msg_memory_exhausted = N_("Memory exhausted"); + +void +xalloc_die (void) +{ + if (xalloc_fail_func) + (*xalloc_fail_func) (); + error (xalloc_exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted)); + /* The `noreturn' cannot be given to error, since it may return if + its first argument is 0. To help compilers understand the + xalloc_die does terminate, call exit. */ + exit (EXIT_FAILURE); +} + +/* Allocate N bytes of memory dynamically, with error checking. */ + +void * +xmalloc (size_t n) +{ + void *p; + + p = malloc (n); + if (p == 0) + xalloc_die (); + return p; +} + +/* Change the size of an allocated block of memory P to N bytes, + with error checking. + If P is NULL, run xmalloc. */ + +void * +xrealloc (void *p, size_t n) +{ + p = realloc (p, n); + if (p == 0) + xalloc_die (); + return p; +} + +/* Allocate memory for N elements of S bytes, with error checking. */ + +void * +xcalloc (size_t n, size_t s) +{ + void *p; + + p = calloc (n, s); + if (p == 0) + xalloc_die (); + return p; +} diff --git a/m4/Makefile.am b/m4/Makefile.am index 6212284..15610bd 100644 --- a/m4/Makefile.am +++ b/m4/Makefile.am @@ -1,10 +1,11 @@ ## Process this file with automake to produce Makefile.in -*-Makefile-*- EXTRA_DIST = \ atconfig.m4 \ -bison-decl.m4 \ error.m4 \ gettext.m4 \ lcmessage.m4 \ m4.m4 \ +malloc.m4 \ progtest.m4 \ +realloc.m4 \ warning.m4 diff --git a/m4/bison-decl.m4 b/m4/bison-decl.m4 index b568414..e69de29 100644 --- a/m4/bison-decl.m4 +++ b/m4/bison-decl.m4 @@ -1,48 +0,0 @@ -dnl FIXME: This should be soon obsoleted by AC_CHECK_DECL. - -dnl See whether we need a declaration for a function. -dnl BISON_NEED_DECLARATION(FUNCTION [, EXTRA-HEADER-FILES]) -AC_DEFUN(BISON_NEED_DECLARATION, -[AC_MSG_CHECKING([whether $1 must be declared]) -AC_CACHE_VAL(bison_cv_decl_needed_$1, -[AC_TRY_COMPILE([ -#include -#ifdef HAVE_STRING_H -#include -#else -#ifdef HAVE_STRINGS_H -#include -#endif -#endif -#ifdef HAVE_STDLIB_H -#include -#endif -#ifdef HAVE_UNISTD_H -#include -#endif -#ifndef HAVE_RINDEX -#define rindex strrchr -#endif -#ifndef HAVE_INDEX -#define index strchr -#endif -$2], -[char *(*pfn) = (char *(*)) $1], -eval "bison_cv_decl_needed_$1=no", eval "bison_cv_decl_needed_$1=yes")]) -if eval "test \"`echo '$bison_cv_decl_needed_'$1`\" = yes"; then - AC_MSG_RESULT(yes) - bison_tr_decl=NEED_DECLARATION_`echo $1 | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'` - AC_DEFINE_UNQUOTED($bison_tr_decl) -else - AC_MSG_RESULT(no) -fi -])dnl - -dnl Check multiple functions to see whether each needs a declaration. -dnl BISON_NEED_DECLARATIONS(FUNCTION... [, EXTRA-HEADER-FILES]) -AC_DEFUN(BISON_NEED_DECLARATIONS, -[for ac_func in $1 -do -BISON_NEED_DECLARATION($ac_func, $2) -done -]) diff --git a/m4/malloc.m4 b/m4/malloc.m4 new file mode 100644 index 0000000..4978eaf --- /dev/null +++ b/m4/malloc.m4 @@ -0,0 +1,35 @@ +#serial 3 + +dnl From Jim Meyering. +dnl Determine whether malloc accepts 0 as its argument. +dnl If it doesn't, arrange to use the replacement function. +dnl + +AC_DEFUN(jm_FUNC_MALLOC, +[ + dnl xmalloc.c requires that this symbol be defined so it doesn't + dnl mistakenly use a broken malloc -- as it might if this test were omitted. + AC_DEFINE_UNQUOTED(HAVE_DONE_WORKING_MALLOC_CHECK, 1, + [Define if the malloc check has been performed. ]) + + AC_CACHE_CHECK([for working malloc], jm_cv_func_working_malloc, + [AC_TRY_RUN([ + char *malloc (); + int + main () + { + exit (malloc (0) ? 0 : 1); + } + ], + jm_cv_func_working_malloc=yes, + jm_cv_func_working_malloc=no, + dnl When crosscompiling, assume malloc is broken. + jm_cv_func_working_malloc=no) + ]) + if test $jm_cv_func_working_malloc = no; then + AC_SUBST(LIBOBJS) + LIBOBJS="$LIBOBJS malloc.$ac_objext" + AC_DEFINE_UNQUOTED(malloc, rpl_malloc, + [Define to rpl_malloc if the replacement function should be used.]) + fi +]) diff --git a/m4/realloc.m4 b/m4/realloc.m4 new file mode 100644 index 0000000..bfbef0c --- /dev/null +++ b/m4/realloc.m4 @@ -0,0 +1,35 @@ +#serial 3 + +dnl From Jim Meyering. +dnl Determine whether realloc works when both arguments are 0. +dnl If it doesn't, arrange to use the replacement function. +dnl + +AC_DEFUN(jm_FUNC_REALLOC, +[ + dnl xmalloc.c requires that this symbol be defined so it doesn't + dnl mistakenly use a broken realloc -- as it might if this test were omitted. + AC_DEFINE_UNQUOTED(HAVE_DONE_WORKING_REALLOC_CHECK, 1, + [Define if the realloc check has been performed. ]) + + AC_CACHE_CHECK([for working realloc], jm_cv_func_working_realloc, + [AC_TRY_RUN([ + char *realloc (); + int + main () + { + exit (realloc (0, 0) ? 0 : 1); + } + ], + jm_cv_func_working_realloc=yes, + jm_cv_func_working_realloc=no, + dnl When crosscompiling, assume realloc is broken. + jm_cv_func_working_realloc=no) + ]) + if test $jm_cv_func_working_realloc = no; then + AC_SUBST(LIBOBJS) + LIBOBJS="$LIBOBJS realloc.$ac_objext" + AC_DEFINE_UNQUOTED(realloc, rpl_realloc, + [Define to rpl_realloc if the replacement function should be used.]) + fi +]) diff --git a/src/LR0.c b/src/LR0.c index 57f6459..b8837ac 100644 --- a/src/LR0.c +++ b/src/LR0.c @@ -23,7 +23,7 @@ The entry point is generate_states. */ #include "system.h" -#include "alloc.h" +#include "xalloc.h" #include "gram.h" #include "state.h" #include "complain.h" @@ -68,7 +68,7 @@ allocate_itemsets (void) short *symbol_count; count = 0; - symbol_count = NEW2 (nsyms, short); + symbol_count = XCALLOC (short, nsyms); itemp = ritem; symbol = *itemp++; @@ -88,8 +88,8 @@ allocate_itemsets (void) appears as an item, which is symbol_count[symbol]. We allocate that much space for each symbol. */ - kernel_base = NEW2 (nsyms, short *); - kernel_items = NEW2 (count, short); + kernel_base = XCALLOC (short *, nsyms); + kernel_items = XCALLOC (short, count); count = 0; for (i = 0; i < nsyms; i++) @@ -99,7 +99,7 @@ allocate_itemsets (void) } shift_symbol = symbol_count; - kernel_end = NEW2 (nsyms, short *); + kernel_end = XCALLOC (short *, nsyms); } @@ -108,22 +108,22 @@ allocate_storage (void) { allocate_itemsets (); - shiftset = NEW2 (nsyms, short); - redset = NEW2 (nrules + 1, short); - state_table = NEW2 (STATE_TABLE_SIZE, core *); + shiftset = XCALLOC (short, nsyms); + redset = XCALLOC (short, nrules + 1); + state_table = XCALLOC (core *, STATE_TABLE_SIZE); } static void free_storage (void) { - FREE (shift_symbol); - FREE (redset); - FREE (shiftset); - FREE (kernel_base); - FREE (kernel_end); - FREE (kernel_items); - FREE (state_table); + XFREE (shift_symbol); + XFREE (redset); + XFREE (shiftset); + XFREE (kernel_base); + XFREE (kernel_end); + XFREE (kernel_items); + XFREE (state_table); } @@ -211,7 +211,7 @@ new_state (int symbol) n = iend - isp1; p = - (core *) xmalloc ((unsigned) (sizeof (core) + (n - 1) * sizeof (short))); + (core *) xcalloc ((unsigned) (sizeof (core) + (n - 1) * sizeof (short)), 1); p->accessing_symbol = symbol; p->number = nstates; p->nitems = n; @@ -349,7 +349,7 @@ new_states (void) { core *p; - p = (core *) xmalloc ((unsigned) (sizeof (core) - sizeof (short))); + p = (core *) xcalloc ((unsigned) (sizeof (core) - sizeof (short)), 1); first_state = last_state = this_state = p; nstates = 1; } @@ -363,8 +363,8 @@ save_shifts (void) short *sp2; short *send; - p = (shifts *) xmalloc ((unsigned) (sizeof (shifts) + - (nshifts - 1) * sizeof (short))); + p = (shifts *) xcalloc ((unsigned) (sizeof (shifts) + + (nshifts - 1) * sizeof (short)), 1); p->number = this_state->number; p->nshifts = nshifts; @@ -400,7 +400,7 @@ insert_start_shift (void) core *statep; shifts *sp; - statep = (core *) xmalloc ((unsigned) (sizeof (core) - sizeof (short))); + statep = (core *) xcalloc ((unsigned) (sizeof (core) - sizeof (short)), 1); statep->number = nstates; statep->accessing_symbol = start_symbol; @@ -408,7 +408,7 @@ insert_start_shift (void) last_state = statep; /* Make a shift from this state to (what will be) the final state. */ - sp = NEW (shifts); + sp = XCALLOC (shifts, 1); sp->number = nstates++; sp->nshifts = 1; sp->shifts[0] = nstates; @@ -465,10 +465,10 @@ augment_automaton (void) if (sp && sp->number == k) { - sp2 = (shifts *) xmalloc ((unsigned) (sizeof (shifts) + sp2 = (shifts *) xcalloc ((unsigned) (sizeof (shifts) + sp->nshifts * - sizeof (short))); + sizeof (short)), 1); sp2->number = k; sp2->nshifts = sp->nshifts + 1; sp2->shifts[0] = nstates; @@ -481,11 +481,11 @@ augment_automaton (void) sp1->next = sp2; if (sp == last_shift) last_shift = sp2; - FREE (sp); + XFREE (sp); } else { - sp2 = NEW (shifts); + sp2 = XCALLOC (shifts, 1); sp2->number = k; sp2->nshifts = 1; sp2->shifts[0] = nstates; @@ -504,8 +504,8 @@ augment_automaton (void) going to the next-to-final state (yet to be made). */ sp = first_shift; - sp2 = (shifts *) xmalloc (sizeof (shifts) - + sp->nshifts * sizeof (short)); + sp2 = (shifts *) xcalloc (sizeof (shifts) + + sp->nshifts * sizeof (short), 1); sp2->nshifts = sp->nshifts + 1; /* Stick this shift into the vector at the proper place. */ @@ -527,7 +527,7 @@ augment_automaton (void) if (last_shift == sp) last_shift = sp2; - FREE (sp); + XFREE (sp); /* Create the next-to-final state, with shift to what will be the final state. */ @@ -538,7 +538,7 @@ augment_automaton (void) { /* The initial state didn't even have any shifts. Give it one shift, to the next-to-final state. */ - sp = NEW (shifts); + sp = XCALLOC (shifts, 1); sp->nshifts = 1; sp->shifts[0] = nstates; @@ -556,7 +556,7 @@ augment_automaton (void) /* There are no shifts for any state. Make one shift, from the initial state to the next-to-final state. */ - sp = NEW (shifts); + sp = XCALLOC (shifts, 1); sp->nshifts = 1; sp->shifts[0] = nstates; @@ -572,13 +572,13 @@ augment_automaton (void) /* Make the final state--the one that follows a shift from the next-to-final state. The symbol for that shift is 0 (end-of-file). */ - statep = (core *) xmalloc ((unsigned) (sizeof (core) - sizeof (short))); + statep = (core *) xcalloc ((unsigned) (sizeof (core) - sizeof (short)), 1); statep->number = nstates; last_state->next = statep; last_state = statep; /* Make the shift from the final state to the termination state. */ - sp = NEW (shifts); + sp = XCALLOC (shifts, 1); sp->number = nstates++; sp->nshifts = 1; sp->shifts[0] = nstates; @@ -590,7 +590,7 @@ augment_automaton (void) final_state = nstates; /* Make the termination state. */ - statep = (core *) xmalloc ((unsigned) (sizeof (core) - sizeof (short))); + statep = (core *) xcalloc ((unsigned) (sizeof (core) - sizeof (short)), 1); statep->number = nstates++; last_state->next = statep; last_state = statep; @@ -629,8 +629,8 @@ save_reductions (void) if (count) { - p = (reductions *) xmalloc ((unsigned) (sizeof (reductions) + - (count - 1) * sizeof (short))); + p = (reductions *) xcalloc ((unsigned) (sizeof (reductions) + + (count - 1) * sizeof (short)), 1); p->number = this_state->number; p->nreds = count; diff --git a/src/Makefile.am b/src/Makefile.am index fa7d8fb..53475eb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,7 +10,7 @@ LDADD = @INTLLIBS@ ../lib/libbison.a bin_PROGRAMS = bison -bison_SOURCES = LR0.c allocate.c closure.c complain.c conflicts.c \ +bison_SOURCES = LR0.c closure.c complain.c conflicts.c \ derives.c \ files.c getargs.c gram.c lalr.c lex.c main.c nullable.c \ output.c \ @@ -18,11 +18,11 @@ bison_SOURCES = LR0.c allocate.c closure.c complain.c conflicts.c \ EXTRA_bison_SOURCES = vmsgetargs.c -noinst_HEADERS = LR0.h alloc.h closure.h complain.h conflicts.h \ +noinst_HEADERS = LR0.h closure.h complain.h conflicts.h \ derives.h \ files.h getargs.h gram.h lalr.h lex.h nullable.h \ output.h state.h \ - print.h reader.h reduce.h symtab.h system.h types.h + print.h reader.h reduce.h symtab.h warshall.h system.h types.h data_DATA = bison.simple bison.hairy diff --git a/src/alloc.h b/src/alloc.h index 9685122..e69de29 100644 --- a/src/alloc.h +++ b/src/alloc.h @@ -1,36 +0,0 @@ -/* Storage allocation interface for bison, - Copyright (C) 1984, 1989 Free Software Foundation, Inc. - -This file is part of Bison, the GNU Compiler Compiler. - -Bison 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 Software Foundation; either version 2, or (at your option) -any later version. - -Bison is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with Bison; see the file COPYING. If not, write to -the Free Software Foundation, Inc., 59 Temple Place - Suite 330, -Boston, MA 02111-1307, USA. */ - - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#define NEW(t) ((t *) xmalloc((unsigned) sizeof(t))) -#define NEW2(n, t) ((t *) xmalloc((unsigned) ((n) * sizeof(t)))) - -#ifdef __STDC__ -#define FREE(x) (x ? (void) free((char *) (x)) : (void)0) -#else -#define FREE(x) ((x) != 0 && (free ((char *) (x)), 0)) -#endif - -extern char *xmalloc PARAMS((register unsigned)); -extern char *xrealloc PARAMS((register char *, register unsigned)); diff --git a/src/allocate.c b/src/allocate.c index b11c26f..e69de29 100644 --- a/src/allocate.c +++ b/src/allocate.c @@ -1,79 +0,0 @@ -/* Allocate and clear storage for bison, - Copyright (C) 1984, 1989 Free Software Foundation, Inc. - -This file is part of Bison, the GNU Compiler Compiler. - -Bison 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 Software Foundation; either version 2, or (at your option) -any later version. - -Bison is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with Bison; see the file COPYING. If not, write to -the Free Software Foundation, Inc., 59 Temple Place - Suite 330, -Boston, MA 02111-1307, USA. */ - - -#include "system.h" - -#ifdef NEED_DECLARATION_CALLOC -#if defined (__STDC__) || defined (_MSC_VER) -extern void *calloc (); -#else -extern char *calloc (); -#endif -#endif /* NEED_DECLARATION_CALLOC */ - -#ifdef NEED_DECLARATION_REALLOC -#if defined (__STDC__) || defined (_MSC_VER) -extern void *realloc (); -#else -extern char *realloc (); -#endif -#endif /* NEED_DECLARATION_REALLOC */ - -extern char *xmalloc PARAMS((register unsigned)); -extern char *xrealloc PARAMS((register char *, register unsigned)); - -extern void done PARAMS((int)); - -extern char *program_name; - -char * -xmalloc (register unsigned n) -{ - register char *block; - - /* Avoid uncertainty about what an arg of 0 will do. */ - if (n == 0) - n = 1; - block = calloc (n, 1); - if (block == NULL) - { - fprintf (stderr, _("%s: memory exhausted\n"), program_name); - done (1); - } - - return block; -} - -char * -xrealloc (register char *block, register unsigned n) -{ - /* Avoid uncertainty about what an arg of 0 will do. */ - if (n == 0) - n = 1; - block = realloc (block, n); - if (block == NULL) - { - fprintf (stderr, _("%s: memory exhausted\n"), program_name); - done (1); - } - - return block; -} diff --git a/src/closure.c b/src/closure.c index 68b6469..27acf7c 100644 --- a/src/closure.c +++ b/src/closure.c @@ -19,10 +19,11 @@ 02111-1307, USA. */ #include "system.h" -#include "alloc.h" +#include "xalloc.h" #include "gram.h" #include "closure.h" #include "derives.h" +#include "warshall.h" short *itemset; short *itemsetend; @@ -122,7 +123,7 @@ set_firsts (void) varsetsize = rowsize = WORDSIZE (nvars); - firsts = NEW2 (nvars * rowsize, unsigned); + firsts = XCALLOC (unsigned, nvars * rowsize); row = firsts; for (i = ntokens; i < nsyms; i++) @@ -171,7 +172,7 @@ set_fderives (void) int ruleno; int i; - fderives = NEW2 (nvars * rulesetsize, unsigned) - ntokens * rulesetsize; + fderives = XCALLOC (unsigned, nvars * rulesetsize) - ntokens * rulesetsize; set_firsts (); @@ -208,17 +209,17 @@ set_fderives (void) print_fderives (); #endif - FREE (firsts); + XFREE (firsts); } void new_closure (int n) { - itemset = NEW2 (n, short); + itemset = XCALLOC (short, n); rulesetsize = WORDSIZE (nrules + 1); - ruleset = NEW2 (rulesetsize, unsigned); + ruleset = XCALLOC (unsigned, rulesetsize); set_fderives (); } @@ -310,7 +311,7 @@ closure (short *core, int n) void free_closure (void) { - FREE (itemset); - FREE (ruleset); - FREE (fderives + ntokens * rulesetsize); + XFREE (itemset); + XFREE (ruleset); + XFREE (fderives + ntokens * rulesetsize); } diff --git a/src/conflicts.c b/src/conflicts.c index d6c31ef..0b94848 100644 --- a/src/conflicts.c +++ b/src/conflicts.c @@ -20,7 +20,7 @@ #include "system.h" #include "getargs.h" -#include "alloc.h" +#include "xalloc.h" #include "files.h" #include "gram.h" #include "state.h" @@ -95,7 +95,7 @@ resolve_sr_conflict (int state, int lookaheadnum) unsigned *fp1; unsigned *fp2; int redprec; - errs *errp = (errs *) xmalloc (sizeof (errs) + ntokens * sizeof (short)); + errs *errp = (errs *) xcalloc (sizeof (errs) + ntokens * sizeof (short), 1); short *errtokens = errp->errs; /* find the rule to reduce by to get precedence of reduction */ @@ -175,7 +175,7 @@ resolve_sr_conflict (int state, int lookaheadnum) /* Some tokens have been explicitly made errors. Allocate a permanent errs structure for this state, to record them. */ i = (char *) errtokens - (char *) errp; - err_table[state] = (errs *) xmalloc ((unsigned int) i); + err_table[state] = (errs *) xcalloc ((unsigned int) i, 1); bcopy (errp, err_table[state], i); } else @@ -269,11 +269,11 @@ initialize_conflicts (void) { int i; - conflicts = NEW2 (nstates, char); - shiftset = NEW2 (tokensetsize, unsigned); - lookaheadset = NEW2 (tokensetsize, unsigned); + conflicts = XCALLOC (char, nstates); + shiftset = XCALLOC (unsigned, tokensetsize); + lookaheadset = XCALLOC (unsigned, tokensetsize); - err_table = NEW2 (nstates, errs *); + err_table = XCALLOC (errs *, nstates); any_conflicts = 0; @@ -718,7 +718,7 @@ print_reductions (int state) void finalize_conflicts (void) { - FREE (conflicts); - FREE (shiftset); - FREE (lookaheadset); + XFREE (conflicts); + XFREE (shiftset); + XFREE (lookaheadset); } diff --git a/src/derives.c b/src/derives.c index 9bd37c9..b5aaa2f 100644 --- a/src/derives.c +++ b/src/derives.c @@ -25,7 +25,7 @@ */ #include "system.h" -#include "alloc.h" +#include "xalloc.h" #include "types.h" #include "gram.h" #include "derives.h" @@ -37,8 +37,8 @@ short **derives; static void print_derives (void) { - register int i; - register short *sp; + int i; + short *sp; printf (_("\n\n\nDERIVES\n\n")); @@ -60,15 +60,15 @@ print_derives (void) void set_derives (void) { - register int i; - register int lhs; - register shorts *p; - register short *q; - register shorts **dset; - register shorts *delts; + int i; + int lhs; + shorts *p; + short *q; + shorts **dset; + shorts *delts; - dset = NEW2 (nvars, shorts *) - ntokens; - delts = NEW2 (nrules + 1, shorts); + dset = XCALLOC (shorts *, nvars) - ntokens; + delts = XCALLOC (shorts, nrules + 1); p = delts; for (i = nrules; i > 0; i--) @@ -83,8 +83,8 @@ set_derives (void) } } - derives = NEW2 (nvars, short *) - ntokens; - q = NEW2 (nvars + nrules, short); + derives = XCALLOC (short *, nvars) - ntokens; + q = XCALLOC (short, nvars + nrules); for (i = ntokens; i < nsyms; i++) { @@ -102,13 +102,13 @@ set_derives (void) print_derives (); #endif - FREE (dset + ntokens); - FREE (delts); + XFREE (dset + ntokens); + XFREE (delts); } void free_derives (void) { - FREE (derives[ntokens]); - FREE (derives + ntokens); + XFREE (derives[ntokens]); + XFREE (derives + ntokens); } diff --git a/src/files.c b/src/files.c index 3dc9ca5..6baa13b 100644 --- a/src/files.c +++ b/src/files.c @@ -43,7 +43,7 @@ #include "getargs.h" #include "files.h" -#include "alloc.h" +#include "xalloc.h" #include "gram.h" #include "complain.h" @@ -90,7 +90,7 @@ stringappend (const char *string1, int end1, const char *string2) while (*cp1++) i++; - ostring = NEW2 (i + end1 + 1, char); + ostring = XCALLOC (char, i + end1 + 1); cp = ostring; cp1 = string1; @@ -213,7 +213,7 @@ open_files (void) short_base_length = strlen (spec_file_prefix); /* Count room for `.tab'. */ base_length = short_base_length + 4; - name_base = (char *) xmalloc (base_length + 1); + name_base = XMALLOC (char, base_length + 1); /* Append `.tab'. */ strcpy (name_base, spec_file_prefix); #ifdef VMS @@ -263,7 +263,7 @@ open_files (void) cp = getenv ("INIT"); if (filename == 0 && cp != NULL) { - filename = xmalloc (strlen (cp) + strlen (PFILE) + 2); + filename = XMALLOC (char, strlen (cp) + strlen (PFILE) + 2); strcpy (filename, cp); cp = filename + strlen (filename); *cp++ = '/'; @@ -372,7 +372,7 @@ open_extra_files (void) cp = getenv ("INIT"); if (filename == 0 && cp != NULL) { - filename = xmalloc (strlen (cp) + strlen (PFILE1) + 2); + filename = XMALLOC (char, strlen (cp) + strlen (PFILE1) + 2); strcpy (filename, cp); cp = filename + strlen (filename); *cp++ = '/'; diff --git a/src/lalr.c b/src/lalr.c index af2ea10..40da198 100644 --- a/src/lalr.c +++ b/src/lalr.c @@ -26,7 +26,7 @@ #include "system.h" #include "types.h" #include "LR0.h" -#include "alloc.h" +#include "xalloc.h" #include "gram.h" #include "complain.h" #include "lalr.h" @@ -123,8 +123,8 @@ digraph (short **relation) int i; infinity = ngotos + 2; - INDEX = NEW2 (ngotos + 1, short); - VERTICES = NEW2 (ngotos + 1, short); + INDEX = XCALLOC (short, ngotos + 1); + VERTICES = XCALLOC (short, ngotos + 1); top = 0; R = relation; @@ -138,8 +138,8 @@ digraph (short **relation) traverse (i); } - FREE (INDEX); - FREE (VERTICES); + XFREE (INDEX); + XFREE (VERTICES); } static void @@ -147,7 +147,7 @@ set_state_table (void) { core *sp; - state_table = NEW2 (nstates, core *); + state_table = XCALLOC (core *, nstates); for (sp = first_state; sp; sp = sp->next) state_table[sp->number] = sp; @@ -159,7 +159,7 @@ set_accessing_symbol (void) { core *sp; - accessing_symbol = NEW2 (nstates, short); + accessing_symbol = XCALLOC (short, nstates); for (sp = first_state; sp; sp = sp->next) accessing_symbol[sp->number] = sp->accessing_symbol; @@ -171,7 +171,7 @@ set_shift_table (void) { shifts *sp; - shift_table = NEW2 (nstates, shifts *); + shift_table = XCALLOC (shifts *, nstates); for (sp = first_shift; sp; sp = sp->next) shift_table[sp->number] = sp; @@ -183,7 +183,7 @@ set_reduction_table (void) { reductions *rp; - reduction_table = NEW2 (nstates, reductions *); + reduction_table = XCALLOC (reductions *, nstates); for (rp = first_reduction; rp; rp = rp->next) reduction_table[rp->number] = rp; @@ -227,8 +227,8 @@ initialize_LA (void) shifts *sp; short *np; - consistent = NEW2 (nstates, char); - lookaheads = NEW2 (nstates + 1, short); + consistent = XCALLOC (char, nstates); + lookaheads = XCALLOC (short, nstates + 1); count = 0; for (i = 0; i < nstates; i++) @@ -260,15 +260,15 @@ initialize_LA (void) if (count == 0) { - LA = NEW2 (1 * tokensetsize, unsigned); - LAruleno = NEW2 (1, short); - lookback = NEW2 (1, shorts *); + LA = XCALLOC (unsigned, 1 * tokensetsize); + LAruleno = XCALLOC (short, 1); + lookback = XCALLOC (shorts *, 1); } else { - LA = NEW2 (count * tokensetsize, unsigned); - LAruleno = NEW2 (count, short); - lookback = NEW2 (count, shorts *); + LA = XCALLOC (unsigned, count * tokensetsize); + LAruleno = XCALLOC (short, count); + lookback = XCALLOC (shorts *, count); } np = LAruleno; @@ -295,8 +295,8 @@ set_goto_map (void) int state2; int state1; - goto_map = NEW2 (nvars + 1, short) - ntokens; - temp_map = NEW2 (nvars + 1, short) - ntokens; + goto_map = XCALLOC (short, nvars + 1) - ntokens; + temp_map = XCALLOC (short, nvars + 1) - ntokens; ngotos = 0; for (sp = first_shift; sp; sp = sp->next) @@ -329,8 +329,8 @@ set_goto_map (void) goto_map[nsyms] = ngotos; temp_map[nsyms] = ngotos; - from_state = NEW2 (ngotos, short); - to_state = NEW2 (ngotos, short); + from_state = XCALLOC (short, ngotos); + to_state = XCALLOC (short, ngotos); for (sp = first_shift; sp; sp = sp->next) { @@ -349,7 +349,7 @@ set_goto_map (void) } } - FREE (temp_map + ntokens); + XFREE (temp_map + ntokens); } @@ -402,10 +402,10 @@ initialize_F (void) int nwords; nwords = ngotos * tokensetsize; - F = NEW2 (nwords, unsigned); + F = XCALLOC (unsigned, nwords); - reads = NEW2 (ngotos, short *); - edge = NEW2 (ngotos + 1, short); + reads = XCALLOC (short *, ngotos); + edge = XCALLOC (short, ngotos + 1); nedges = 0; rowp = F; @@ -435,7 +435,7 @@ initialize_F (void) if (nedges) { - reads[i] = rp = NEW2 (nedges + 1, short); + reads[i] = rp = XCALLOC (short, nedges + 1); for (j = 0; j < nedges; j++) rp[j] = edge[j]; @@ -453,11 +453,11 @@ initialize_F (void) for (i = 0; i < ngotos; i++) { if (reads[i]) - FREE (reads[i]); + XFREE (reads[i]); } - FREE (reads); - FREE (edge); + XFREE (reads); + XFREE (edge); } @@ -482,7 +482,7 @@ add_lookback_edge (int stateno, int ruleno, int gotono) assert (found); - sp = NEW (shorts); + sp = XCALLOC (shorts, 1); sp->next = lookback[i]; sp->value = gotono; lookback[i] = sp; @@ -499,7 +499,7 @@ transpose (short **R_arg, int n) int i; int k; - nedges = NEW2 (n, short); + nedges = XCALLOC (short, n); for (i = 0; i < n; i++) { @@ -511,22 +511,22 @@ transpose (short **R_arg, int n) } } - new_R = NEW2 (n, short *); - temp_R = NEW2 (n, short *); + new_R = XCALLOC (short *, n); + temp_R = XCALLOC (short *, n); for (i = 0; i < n; i++) { k = nedges[i]; if (k > 0) { - sp = NEW2 (k + 1, short); + sp = XCALLOC (short, k + 1); new_R[i] = sp; temp_R[i] = sp; sp[k] = -1; } } - FREE (nedges); + XFREE (nedges); for (i = 0; i < n; i++) { @@ -538,7 +538,7 @@ transpose (short **R_arg, int n) } } - FREE (temp_R); + XFREE (temp_R); return new_R; } @@ -565,9 +565,9 @@ build_relations (void) short *states; short **new_includes; - includes = NEW2 (ngotos, short *); - edge = NEW2 (ngotos + 1, short); - states = NEW2 (maxrhs + 1, short); + includes = XCALLOC (short *, ngotos); + edge = XCALLOC (short, ngotos + 1); + states = XCALLOC (short, maxrhs + 1); for (i = 0; i < ngotos; i++) { @@ -619,7 +619,7 @@ build_relations (void) if (nedges) { - includes[i] = shortp = NEW2 (nedges + 1, short); + includes[i] = shortp = XCALLOC (short, nedges + 1); for (j = 0; j < nedges; j++) shortp[j] = edge[j]; shortp[nedges] = -1; @@ -630,14 +630,14 @@ build_relations (void) for (i = 0; i < ngotos; i++) if (includes[i]) - FREE (includes[i]); + XFREE (includes[i]); - FREE (includes); + XFREE (includes); includes = new_includes; - FREE (edge); - FREE (states); + XFREE (edge); + XFREE (states); } @@ -652,10 +652,10 @@ compute_FOLLOWS (void) for (i = 0; i < ngotos; i++) { if (includes[i]) - FREE (includes[i]); + XFREE (includes[i]); } - FREE (includes); + XFREE (includes); } @@ -693,12 +693,12 @@ compute_lookaheads (void) for (sp = lookback[i]; sp; sp = sptmp) { sptmp = sp->next; - FREE (sp); + XFREE (sp); } } - FREE (lookback); - FREE (F); + XFREE (lookback); + XFREE (F); } diff --git a/src/lex.c b/src/lex.c index e2a94ad..abcf199 100644 --- a/src/lex.c +++ b/src/lex.c @@ -24,7 +24,7 @@ #include "getopt.h" /* for optarg */ #include "symtab.h" #include "lex.h" -#include "alloc.h" +#include "xalloc.h" #include "complain.h" #include "gram.h" @@ -48,7 +48,7 @@ void init_lex (void) { maxtoken = 100; - token_buffer = NEW2 (maxtoken + 1, char); + token_buffer = XCALLOC (char, maxtoken + 1); unlexed = -1; } @@ -58,7 +58,7 @@ grow_token_buffer (char *p) { int offset = p - token_buffer; maxtoken *= 2; - token_buffer = (char *) xrealloc (token_buffer, maxtoken + 1); + token_buffer = XREALLOC (token_buffer, char, maxtoken + 1); return token_buffer + offset; } diff --git a/src/main.c b/src/main.c index 6b07e20..eaa8559 100644 --- a/src/main.c +++ b/src/main.c @@ -32,6 +32,7 @@ #include "nullable.h" #include "print.h" #include "LR0.h" +#include "conflicts.h" /* The name this program was run with, for messages. */ char *program_name; diff --git a/src/nullable.c b/src/nullable.c index 8733bc0..dae7a62 100644 --- a/src/nullable.c +++ b/src/nullable.c @@ -26,7 +26,7 @@ #include "system.h" #include "types.h" #include "gram.h" -#include "alloc.h" +#include "xalloc.h" #include "nullable.h" char *nullable = NULL; @@ -52,17 +52,17 @@ set_nullable (void) fprintf (stderr, _("Entering set_nullable")); #endif - nullable = NEW2 (nvars, char) - ntokens; + nullable = XCALLOC (char, nvars) - ntokens; - squeue = NEW2 (nvars, short); + squeue = XCALLOC (short, nvars); s1 = s2 = squeue; - rcount = NEW2 (nrules + 1, short); - rsets = NEW2 (nvars, shorts *) - ntokens; + rcount = XCALLOC (short, nrules + 1); + rsets = XCALLOC (shorts *, nvars) - ntokens; /* This is said to be more elements than we actually use. Supposedly nitems - nrules is enough. But why take the risk? */ - relts = NEW2 (nitems + nvars + 1, shorts); + relts = XCALLOC (shorts, nitems + nvars + 1); p = relts; r = ritem; @@ -122,15 +122,15 @@ set_nullable (void) } } - FREE (squeue); - FREE (rcount); - FREE (rsets + ntokens); - FREE (relts); + XFREE (squeue); + XFREE (rcount); + XFREE (rsets + ntokens); + XFREE (relts); } void free_nullable (void) { - FREE (nullable + ntokens); + XFREE (nullable + ntokens); } diff --git a/src/output.c b/src/output.c index 0da18d0..f86d224 100644 --- a/src/output.c +++ b/src/output.c @@ -92,7 +92,7 @@ #include "system.h" #include "getargs.h" -#include "alloc.h" +#include "xalloc.h" #include "files.h" #include "gram.h" #include "LR0.h" @@ -452,7 +452,7 @@ output_rule_data (void) output_short_table (ftable, "yyr1", rlhs, 0, 1, nrules + 1); - FREE (rlhs + 1); + XFREE (rlhs + 1); putc ('\n', ftable); @@ -483,7 +483,7 @@ static const short yyr2[] = { 0", ftable); putc ('\n', ftable); fprintf (ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1); - FREE (rrhs + 1); + XFREE (rrhs + 1); } @@ -699,8 +699,8 @@ save_row (int state) if (count == 0) return; - froms[state] = sp1 = sp = NEW2 (count, short); - tos[state] = sp2 = NEW2 (count, short); + froms[state] = sp1 = sp = XCALLOC (short, count); + tos[state] = sp2 = XCALLOC (short, count); for (i = 0; i < ntokens; i++) { @@ -728,19 +728,19 @@ static void token_actions (void) { int i; - short *yydefact = NEW2 (nstates, short); + short *yydefact = XCALLOC (short, nstates); - actrow = NEW2 (ntokens, short); + actrow = XCALLOC (short, ntokens); for (i = 0; i < nstates; ++i) { yydefact[i] = action_row (i); save_row (i); } - FREE (actrow); + XFREE (actrow); output_short_table (ftable, "yydefact", yydefact, yydefact[0], 1, nstates); - FREE (yydefact); + XFREE (yydefact); } @@ -749,12 +749,12 @@ free_shifts (void) { shifts *sp, *sptmp; /* JF derefrenced freed ptr */ - FREE (shift_table); + XFREE (shift_table); for (sp = first_shift; sp; sp = sptmp) { sptmp = sp->next; - FREE (sp); + XFREE (sp); } } @@ -764,12 +764,12 @@ free_reductions (void) { reductions *rp, *rptmp; /* JF fixed freed ptr */ - FREE (reduction_table); + XFREE (reduction_table); for (rp = first_reduction; rp; rp = rptmp) { rptmp = rp->next; - FREE (rp); + XFREE (rp); } } @@ -802,8 +802,8 @@ save_column (int symbol, int default_state) symno = symbol - ntokens + nstates; - froms[symno] = sp1 = sp = NEW2 (count, short); - tos[symno] = sp2 = NEW2 (count, short); + froms[symno] = sp1 = sp = XCALLOC (short, count); + tos[symno] = sp2 = XCALLOC (short, count); for (i = m; i < n; i++) { @@ -869,7 +869,7 @@ goto_actions (void) { int i, j, k; - state_count = NEW2 (nstates, short); + state_count = XCALLOC (short, nstates); k = default_goto (ntokens); fprintf (ftable, "\nstatic const short yydefgoto[] = {%6d", k); @@ -896,7 +896,7 @@ goto_actions (void) } fprintf (ftable, "\n};\n"); - FREE (state_count); + XFREE (state_count); } @@ -912,7 +912,7 @@ sort_actions (void) int t; int w; - order = NEW2 (nvectors, short); + order = XCALLOC (short, nvectors); nentries = 0; for (i = 0; i < nvectors; i++) @@ -1049,10 +1049,10 @@ pack_table (void) int place; int state; - base = NEW2 (nvectors, short); - pos = NEW2 (nentries, short); - table = NEW2 (MAXTABLE, short); - check = NEW2 (MAXTABLE, short); + base = XCALLOC (short, nvectors); + pos = XCALLOC (short, nentries); + table = XCALLOC (short, MAXTABLE); + check = XCALLOC (short, MAXTABLE); lowzero = 0; high = 0; @@ -1079,14 +1079,14 @@ pack_table (void) for (i = 0; i < nvectors; i++) { if (froms[i]) - FREE (froms[i]); + XFREE (froms[i]); if (tos[i]) - FREE (tos[i]); + XFREE (tos[i]); } - FREE (froms); - FREE (tos); - FREE (pos); + XFREE (froms); + XFREE (tos); + XFREE (pos); } /* the following functions output yytable, yycheck @@ -1103,7 +1103,7 @@ output_base (void) output_short_table (ftable, "yypgoto", base, base[nstates], nstates + 1, nvectors); - FREE (base); + XFREE (base); } @@ -1113,7 +1113,7 @@ output_table (void) fprintf (ftable, "\n\n#define\tYYLAST\t\t%d\n\n\n", high); output_short_table (ftable, "yytable", table, table[0], 1, high + 1); - FREE (table); + XFREE (table); } @@ -1122,7 +1122,7 @@ output_check (void) { output_short_table (ftable, "yycheck", check, check[0], 1, high + 1); - FREE (check); + XFREE (check); } /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable @@ -1133,23 +1133,23 @@ output_actions (void) { nvectors = nstates + nvars; - froms = NEW2 (nvectors, short *); - tos = NEW2 (nvectors, short *); - tally = NEW2 (nvectors, short); - width = NEW2 (nvectors, short); + froms = XCALLOC (short *, nvectors); + tos = XCALLOC (short *, nvectors); + tally = XCALLOC (short, nvectors); + width = XCALLOC (short, nvectors); token_actions (); free_shifts (); free_reductions (); - FREE (lookaheads); - FREE (LA); - FREE (LAruleno); - FREE (accessing_symbol); + XFREE (lookaheads); + XFREE (LA); + XFREE (LAruleno); + XFREE (accessing_symbol); goto_actions (); - FREE (goto_map + ntokens); - FREE (from_state); - FREE (to_state); + XFREE (goto_map + ntokens); + XFREE (from_state); + XFREE (to_state); sort_actions (); pack_table (); @@ -1264,12 +1264,12 @@ free_itemsets (void) { core *cp, *cptmp; - FREE (state_table); + XFREE (state_table); for (cp = first_state; cp; cp = cptmp) { cptmp = cp->next; - FREE (cp); + XFREE (cp); } } @@ -1321,7 +1321,7 @@ output (void) /* if (semantic_parser) */ /* This is now unconditional because debugging printouts can use it. */ output_gram (); - FREE (ritem); + XFREE (ritem); if (semantic_parser) output_stos (); output_rule_data (); diff --git a/src/print.c b/src/print.c index 9a84853..f578ee0 100644 --- a/src/print.c +++ b/src/print.c @@ -20,7 +20,7 @@ #include "system.h" -#include "alloc.h" +#include "xalloc.h" #include "files.h" #include "gram.h" #include "LR0.h" @@ -29,7 +29,7 @@ #include "getargs.h" #include "state.h" #include "reader.h" - +#include "print.h" #if 0 static void diff --git a/src/reader.c b/src/reader.c index ded07a7..1dfe020 100644 --- a/src/reader.c +++ b/src/reader.c @@ -23,7 +23,7 @@ #include "system.h" #include "getargs.h" #include "files.h" -#include "alloc.h" +#include "xalloc.h" #include "symtab.h" #include "lex.h" #include "gram.h" @@ -390,7 +390,7 @@ parse_token_decl (int what_is, int what_is_not) if (token == TYPENAME) { k = strlen (token_buffer); - typename = NEW2 (k + 1, char); + typename = XCALLOC (char, k + 1); strcpy (typename, token_buffer); value_components_used = 1; symbol = NULL; @@ -516,7 +516,7 @@ parse_type_decl (void) } k = strlen (token_buffer); - name = NEW2 (k + 1, char); + name = XCALLOC (char, k + 1); strcpy (name, token_buffer); for (;;) @@ -585,7 +585,7 @@ parse_assoc_decl (int assoc) case TYPENAME: k = strlen (token_buffer); - name = NEW2 (k + 1, char); + name = XCALLOC (char, k + 1); strcpy (name, token_buffer); break; @@ -774,7 +774,7 @@ parse_thong_decl (void) if (token == TYPENAME) { k = strlen (token_buffer); - typename = NEW2 (k + 1, char); + typename = XCALLOC (char, k + 1); strcpy (typename, token_buffer); value_components_used = 1; token = lex (); /* fetch first token */ @@ -1216,8 +1216,7 @@ record_rule_line (void) if (nrules >= rline_allocated) { rline_allocated = nrules * 2; - rline = (short *) xrealloc ((char *) rline, - rline_allocated * sizeof (short)); + rline = XREALLOC (rline, short, rline_allocated); } rline[nrules] = lineno; } @@ -1263,7 +1262,7 @@ get_type (void) } k = strlen (token_buffer); - name = NEW2 (k + 1, char); + name = XCALLOC (char, k + 1); strcpy (name, token_buffer); for (;;) @@ -1361,7 +1360,7 @@ readgram (void) record_rule_line (); - p = NEW (symbol_list); + p = XCALLOC (symbol_list, 1); p->sym = lhs; crule1 = p1; @@ -1439,19 +1438,19 @@ readgram (void) nrules++; nitems++; record_rule_line (); - p = NEW (symbol_list); + p = XCALLOC (symbol_list, 1); if (crule1) crule1->next = p; else grammar = p; p->sym = sdummy; - crule1 = NEW (symbol_list); + crule1 = XCALLOC (symbol_list, 1); p->next = crule1; crule1->next = crule; /* insert the dummy generated by that rule into this rule. */ nitems++; - p = NEW (symbol_list); + p = XCALLOC (symbol_list, 1); p->sym = sdummy; p1->next = p; p1 = p; @@ -1462,7 +1461,7 @@ readgram (void) if (t == IDENTIFIER) { nitems++; - p = NEW (symbol_list); + p = XCALLOC (symbol_list, 1); p->sym = symval; p1->next = p; p1 = p; @@ -1477,7 +1476,7 @@ readgram (void) } /* end of read rhs of rule */ /* Put an empty link in the list to mark the end of this rule */ - p = NEW (symbol_list); + p = XCALLOC (symbol_list, 1); p1->next = p; p1 = p; @@ -1672,13 +1671,13 @@ packsymbols (void) /* int lossage = 0; JF set but not used */ - tags = NEW2 (nsyms + 1, char *); + tags = XCALLOC (char *, nsyms + 1); tags[0] = DOLLAR; - user_toknums = NEW2 (nsyms + 1, short); + user_toknums = XCALLOC (short, nsyms + 1); user_toknums[0] = 0; - sprec = NEW2 (nsyms, short); - sassoc = NEW2 (nsyms, short); + sprec = XCALLOC (short, nsyms); + sassoc = XCALLOC (short, nsyms); max_user_token_number = 256; last_user_token_number = 256; @@ -1749,7 +1748,7 @@ packsymbols (void) { int j; - token_translations = NEW2 (max_user_token_number + 1, short); + token_translations = XCALLOC (short, max_user_token_number + 1); /* initialize all entries for literal tokens to 2, the internal token number for $undefined., which represents all invalid @@ -1827,12 +1826,12 @@ packgram (void) bucket *ruleprec; - ritem = NEW2 (nitems + 1, short); - rlhs = NEW2 (nrules, short) - 1; - rrhs = NEW2 (nrules, short) - 1; - rprec = NEW2 (nrules, short) - 1; - rprecsym = NEW2 (nrules, short) - 1; - rassoc = NEW2 (nrules, short) - 1; + ritem = XCALLOC (short, nitems + 1); + rlhs = XCALLOC (short, nrules) - 1; + rrhs = XCALLOC (short, nrules) - 1; + rprec = XCALLOC (short, nrules) - 1; + rprecsym = XCALLOC (short, nrules) - 1; + rassoc = XCALLOC (short, nrules) - 1; itemno = 0; ruleno = 1; @@ -1906,7 +1905,7 @@ reader (void) nrules = 0; nitems = 0; rline_allocated = 10; - rline = NEW2 (rline_allocated, short); + rline = XCALLOC (short, rline_allocated); typed = 0; lastprec = 0; diff --git a/src/reduce.c b/src/reduce.c index 8175f29..001b910 100644 --- a/src/reduce.c +++ b/src/reduce.c @@ -29,7 +29,7 @@ #include "getargs.h" #include "files.h" #include "gram.h" -#include "alloc.h" +#include "xalloc.h" #include "complain.h" #include "reduce.h" #include "reader.h" @@ -122,7 +122,7 @@ useless_nonterminals (void) /* N is set as built. Np is set being built this iteration. P is set of all productions which have a RHS all in N. */ - Np = NEW2 (WORDSIZE (nvars), unsigned); + Np = XCALLOC (unsigned, WORDSIZE (nvars)); /* The set being computed is a set of nonterminals which can derive the empty string or strings consisting of all terminals. At each @@ -162,7 +162,7 @@ useless_nonterminals (void) Np = N; N = Ns; } - FREE (N); + XFREE (N); N = Np; } @@ -198,8 +198,8 @@ inaccessable_symbols (void) terminals are printed (if running in verbose mode) so that the user can know. */ - Vp = NEW2 (WORDSIZE (nsyms), unsigned); - Pp = NEW2 (WORDSIZE (nrules + 1), unsigned); + Vp = XCALLOC (unsigned, WORDSIZE (nsyms)); + Pp = XCALLOC (unsigned, WORDSIZE (nrules + 1)); /* If the start symbol isn't useful, then nothing will be useful. */ if (!BITISSET (N, start_symbol - ntokens)) @@ -236,7 +236,7 @@ inaccessable_symbols (void) } end_iteration: - FREE (V); + XFREE (V); V = Vp; /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */ @@ -244,7 +244,7 @@ end_iteration: SETBIT (V, 1); /* error token */ SETBIT (V, 2); /* some undefined token */ - FREE (P); + XFREE (P); P = Pp; nuseful_productions = bits_size (P, WORDSIZE (nrules + 1)); @@ -340,7 +340,7 @@ reduce_grammar_tables (void) number. -1 in the map means it was useless and is being eliminated. */ - nontermmap = NEW2 (nvars, short) - ntokens; + nontermmap = XCALLOC (short, nvars) - ntokens; for (i = ntokens; i < nsyms; i++) nontermmap[i] = -1; @@ -513,10 +513,10 @@ reduce_grammar (void) /* Allocate the global sets used to compute the reduced grammar */ - N = NEW2 (WORDSIZE (nvars), unsigned); - P = NEW2 (WORDSIZE (nrules + 1), unsigned); - V = NEW2 (WORDSIZE (nsyms), unsigned); - V1 = NEW2 (WORDSIZE (nsyms), unsigned); + N = XCALLOC (unsigned, WORDSIZE (nvars)); + P = XCALLOC (unsigned, WORDSIZE (nrules + 1)); + V = XCALLOC (unsigned, WORDSIZE (nsyms)); + V1 = XCALLOC (unsigned, WORDSIZE (nsyms)); useless_nonterminals (); inaccessable_symbols (); @@ -558,7 +558,7 @@ reduce_grammar (void) done_reducing: /* Free the global sets used to compute the reduced grammar */ - FREE (N); - FREE (V); - FREE (P); + XFREE (N); + XFREE (V); + XFREE (P); } diff --git a/src/symtab.c b/src/symtab.c index 451c518..b264f08 100644 --- a/src/symtab.c +++ b/src/symtab.c @@ -20,7 +20,7 @@ Boston, MA 02111-1307, USA. */ #include "system.h" -#include "alloc.h" +#include "xalloc.h" #include "symtab.h" #include "gram.h" @@ -56,7 +56,7 @@ copys (const char *s) for (cp = s; *cp; cp++) i++; - result = xmalloc((unsigned int)i); + result = XMALLOC(char, i); strcpy(result, s); return result; } @@ -67,7 +67,7 @@ tabinit (void) { /* register int i; JF unused */ - symtab = NEW2(TABSIZE, bucket *); + symtab = XCALLOC (bucket *, TABSIZE); firstsymbol = NULL; lastsymbol = NULL; @@ -97,7 +97,7 @@ getsym (const char *key) { nsyms++; - bp = NEW(bucket); + bp = XCALLOC (bucket, 1); bp->link = symtab[hashval]; bp->next = NULL; bp->tag = copys(key); @@ -135,11 +135,11 @@ free_symtab (void) bptmp = bp->link; #if 0 /* This causes crashes because one string can appear more than once. */ if (bp->type_name) - FREE(bp->type_name); + XFREE(bp->type_name); #endif - FREE(bp); + XFREE(bp); bp = bptmp; } } - FREE(symtab); + XFREE(symtab); } diff --git a/src/warshall.c b/src/warshall.c index 6474ef8..9afd7fd 100644 --- a/src/warshall.c +++ b/src/warshall.c @@ -1,41 +1,39 @@ /* Generate transitive closure of a matrix, - Copyright (C) 1984, 1989 Free Software Foundation, Inc. + Copyright (C) 1984, 1989, 2000 Free Software Foundation, Inc. -This file is part of Bison, the GNU Compiler Compiler. + This file is part of Bison, the GNU Compiler Compiler. -Bison 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 Software Foundation; either version 2, or (at your option) -any later version. + Bison 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 Software Foundation; either version 2, or (at your option) + any later version. -Bison is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + Bison is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. -You should have received a copy of the GNU General Public License -along with Bison; see the file COPYING. If not, write to -the Free Software Foundation, Inc., 59 Temple Place - Suite 330, -Boston, MA 02111-1307, USA. */ + You should have received a copy of the GNU General Public License + along with Bison; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ #include "system.h" +#include "warshall.h" -void RTC PARAMS((unsigned *, int)); - - -/* given n by n matrix of bits R, modify its contents - to be the transive closure of what was given. */ +/* Given n by n matrix of bits R, modify its contents to be the + transive closure of what was given. */ static void TC (unsigned *R, int n) { - register int rowsize; - register unsigned mask; - register unsigned *rowj; - register unsigned *rp; - register unsigned *rend; - register unsigned *ccol; + int rowsize; + unsigned mask; + unsigned *rowj; + unsigned *rp; + unsigned *rend; + unsigned *ccol; unsigned *relend; unsigned *cword; @@ -88,10 +86,10 @@ TC (unsigned *R, int n) void RTC (unsigned *R, int n) { - register int rowsize; - register unsigned mask; - register unsigned *rp; - register unsigned *relend; + int rowsize; + unsigned mask; + unsigned *rp; + unsigned *relend; TC(R, n); diff --git a/src/warshall.h b/src/warshall.h new file mode 100644 index 0000000..02c2e75 --- /dev/null +++ b/src/warshall.h @@ -0,0 +1,25 @@ +/* Generate transitive closure of a matrix, + Copyright (C) 1984, 1989, 2000 Free Software Foundation, Inc. + + This file is part of Bison, the GNU Compiler Compiler. + + Bison 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 Software Foundation; either version 2, or (at your option) + any later version. + + Bison is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Bison; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#ifndef WARSHALL_H_ +# define WARSHALL_H_ + +void RTC PARAMS ((unsigned *, int)); +#endif /* !WARSHALL_H_ */ -- 2.7.4