public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Reorganise bfd/doc/chew.c a little
@ 2022-05-30  9:33 Alan Modra
  2022-05-30  9:33 ` Update K&R functions in bfd/doc/chew.c Alan Modra
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Alan Modra @ 2022-05-30  9:33 UTC (permalink / raw)
  To: binutils

This also removes some unused variables, and deletes support for the
"var" keyword which isn't used and was broken.  (No means to set
variables, and add_var used push_number inconsistent with its use
elsewhere.)

	* doc/chew.c: Move typedefs before variables, variables before
	functions.
	(die): Move earlier.
	(word_type, sstack, ssp): Delete.
	(dict_type): Delete var field.
	(add_var): Delete.
	(compile): Remove "var" support.

diff --git a/bfd/doc/chew.c b/bfd/doc/chew.c
index 0c20acbc0be..2f987821bd9 100644
--- a/bfd/doc/chew.c
+++ b/bfd/doc/chew.c
@@ -91,11 +91,6 @@
 #define DEF_SIZE 5000
 #define STACK 50
 
-int internal_wanted;
-int internal_mode;
-
-int warning;
-
 /* Here is a string type ...  */
 
 typedef struct buffer
@@ -105,6 +100,35 @@ typedef struct buffer
   unsigned long size;
 } string_type;
 
+typedef void (*stinst_type)();
+
+typedef struct dict_struct
+{
+  char *word;
+  struct dict_struct *next;
+  stinst_type *code;
+  int code_length;
+  int code_end;
+} dict_type;
+
+int internal_wanted;
+int internal_mode;
+
+int warning;
+
+string_type stack[STACK];
+string_type *tos;
+
+unsigned int idx = 0; /* Pos in input buffer */
+string_type *ptr; /* and the buffer */
+
+long istack[STACK];
+long *isp = &istack[0];
+
+dict_type *root;
+
+stinst_type *pc;
+
 #ifdef __STDC__
 static void init_string_with_size (string_type *, unsigned int);
 static void init_string (string_type *);
@@ -121,6 +145,14 @@ static void catstr (string_type *, string_type *);
 static void die (char *);
 #endif
 
+static void
+die (msg)
+     char *msg;
+{
+  fprintf (stderr, "%s\n", msg);
+  exit (1);
+}
+
 static void
 init_string_with_size (buffer, size)
      string_type *buffer;
@@ -279,42 +311,6 @@ skip_past_newline_1 (ptr, idx)
   return idx;
 }
 
-/***********************************************************************/
-
-string_type stack[STACK];
-string_type *tos;
-
-unsigned int idx = 0; /* Pos in input buffer */
-string_type *ptr; /* and the buffer */
-typedef void (*stinst_type)();
-stinst_type *pc;
-stinst_type sstack[STACK];
-stinst_type *ssp = &sstack[0];
-long istack[STACK];
-long *isp = &istack[0];
-
-typedef int *word_type;
-
-struct dict_struct
-{
-  char *word;
-  struct dict_struct *next;
-  stinst_type *code;
-  int code_length;
-  int code_end;
-  int var;
-};
-
-typedef struct dict_struct dict_type;
-
-static void
-die (msg)
-     char *msg;
-{
-  fprintf (stderr, "%s\n", msg);
-  exit (1);
-}
-
 static void
 check_range ()
 {
@@ -356,7 +352,6 @@ static void perform (void);
 dict_type *newentry (char *);
 unsigned int add_to_definition (dict_type *, stinst_type);
 void add_intrinsic (char *, void (*)());
-void add_var (char *);
 void compile (char *);
 static void bang (void);
 static void atsign (void);
@@ -1230,8 +1225,6 @@ nextword (string, word)
     return NULL;
 }
 
-dict_type *root;
-
 dict_type *
 lookup_word (word)
      char *word;
@@ -1351,16 +1344,6 @@ add_intrinsic (name, func)
   add_to_definition (new_d, 0);
 }
 
-void
-add_var (name)
-     char *name;
-{
-  dict_type *new_d = newentry (name);
-  add_to_definition (new_d, push_number);
-  add_to_definition (new_d, (stinst_type) (&(new_d->var)));
-  add_to_definition (new_d, 0);
-}
-
 void
 compile (string)
      char *string;
@@ -1371,16 +1354,7 @@ compile (string)
   string = nextword (string, &word);
   while (string && *string && word[0])
     {
-      if (strcmp (word, "var") == 0)
-	{
-	  free (word);
-	  string = nextword (string, &word);
-	  if (!string)
-	    continue;
-	  add_var (word);
-	  string = nextword (string, &word);
-	}
-      else if (word[0] == ':')
+      if (word[0] == ':')
 	{
 	  dict_type *ptr;
 

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Update K&R functions in bfd/doc/chew.c
  2022-05-30  9:33 Reorganise bfd/doc/chew.c a little Alan Modra
@ 2022-05-30  9:33 ` Alan Modra
  2022-05-30  9:34 ` use libiberty xmalloc " Alan Modra
  2022-05-30  9:35 ` Use a union to avoid casts " Alan Modra
  2 siblings, 0 replies; 6+ messages in thread
From: Alan Modra @ 2022-05-30  9:33 UTC (permalink / raw)
  To: binutils

	* doc/chew.c: Update function definitions to ISO C, remove
	now unnecessary prototypes.

diff --git a/bfd/doc/chew.c b/bfd/doc/chew.c
index 2f987821bd9..22921e68431 100644
--- a/bfd/doc/chew.c
+++ b/bfd/doc/chew.c
@@ -100,7 +100,7 @@ typedef struct buffer
   unsigned long size;
 } string_type;
 
-typedef void (*stinst_type)();
+typedef void (*stinst_type) (void);
 
 typedef struct dict_struct
 {
@@ -129,34 +129,15 @@ dict_type *root;
 
 stinst_type *pc;
 
-#ifdef __STDC__
-static void init_string_with_size (string_type *, unsigned int);
-static void init_string (string_type *);
-static int find (string_type *, char *);
-static void write_buffer (string_type *, FILE *);
-static void delete_string (string_type *);
-static char *addr (string_type *, unsigned int);
-static char at (string_type *, unsigned int);
-static void catchar (string_type *, int);
-static void overwrite_string (string_type *, string_type *);
-static void catbuf (string_type *, char *, unsigned int);
-static void cattext (string_type *, char *);
-static void catstr (string_type *, string_type *);
-static void die (char *);
-#endif
-
 static void
-die (msg)
-     char *msg;
+die (char *msg)
 {
   fprintf (stderr, "%s\n", msg);
   exit (1);
 }
 
 static void
-init_string_with_size (buffer, size)
-     string_type *buffer;
-     unsigned int size;
+init_string_with_size (string_type *buffer, unsigned int size)
 {
   buffer->write_idx = 0;
   buffer->size = size;
@@ -164,16 +145,13 @@ init_string_with_size (buffer, size)
 }
 
 static void
-init_string (buffer)
-     string_type *buffer;
+init_string (string_type *buffer)
 {
   init_string_with_size (buffer, DEF_SIZE);
 }
 
 static int
-find (str, what)
-     string_type *str;
-     char *what;
+find (string_type *str, char *what)
 {
   unsigned int i;
   char *p;
@@ -189,9 +167,7 @@ find (str, what)
 }
 
 static void
-write_buffer (buffer, f)
-     string_type *buffer;
-     FILE *f;
+write_buffer (string_type *buffer, FILE *f)
 {
   if (buffer->write_idx != 0
       && fwrite (buffer->ptr, buffer->write_idx, 1, f) != 1)
@@ -199,25 +175,20 @@ write_buffer (buffer, f)
 }
 
 static void
-delete_string (buffer)
-     string_type *buffer;
+delete_string (string_type *buffer)
 {
   free (buffer->ptr);
   buffer->ptr = NULL;
 }
 
 static char *
-addr (buffer, idx)
-     string_type *buffer;
-     unsigned int idx;
+addr (string_type *buffer, unsigned int idx)
 {
   return buffer->ptr + idx;
 }
 
 static char
-at (buffer, pos)
-     string_type *buffer;
-     unsigned int pos;
+at (string_type *buffer, unsigned int pos)
 {
   if (pos >= buffer->write_idx)
     return 0;
@@ -225,9 +196,7 @@ at (buffer, pos)
 }
 
 static void
-catchar (buffer, ch)
-     string_type *buffer;
-     int ch;
+catchar (string_type *buffer, int ch)
 {
   if (buffer->write_idx == buffer->size)
     {
@@ -239,9 +208,7 @@ catchar (buffer, ch)
 }
 
 static void
-overwrite_string (dst, src)
-     string_type *dst;
-     string_type *src;
+overwrite_string (string_type *dst, string_type *src)
 {
   free (dst->ptr);
   dst->size = src->size;
@@ -250,10 +217,7 @@ overwrite_string (dst, src)
 }
 
 static void
-catbuf (buffer, buf, len)
-     string_type *buffer;
-     char *buf;
-     unsigned int len;
+catbuf (string_type *buffer, char *buf, unsigned int len)
 {
   if (buffer->write_idx + len >= buffer->size)
     {
@@ -266,25 +230,19 @@ catbuf (buffer, buf, len)
 }
 
 static void
-cattext (buffer, string)
-     string_type *buffer;
-     char *string;
+cattext (string_type *buffer, char *string)
 {
   catbuf (buffer, string, (unsigned int) strlen (string));
 }
 
 static void
-catstr (dst, src)
-     string_type *dst;
-     string_type *src;
+catstr (string_type *dst, string_type *src)
 {
   catbuf (dst, src->ptr, src->write_idx);
 }
 
 static unsigned int
-skip_white_and_stars (src, idx)
-     string_type *src;
-     unsigned int idx;
+skip_white_and_stars (string_type *src, unsigned int idx)
 {
   char c;
   while ((c = at (src, idx)),
@@ -299,9 +257,7 @@ skip_white_and_stars (src, idx)
 }
 
 static unsigned int
-skip_past_newline_1 (ptr, idx)
-     string_type *ptr;
-     unsigned int idx;
+skip_past_newline_1 (string_type *ptr, unsigned int idx)
 {
   while (at (ptr, idx)
 	 && at (ptr, idx) != '\n')
@@ -312,7 +268,7 @@ skip_past_newline_1 (ptr, idx)
 }
 
 static void
-check_range ()
+check_range (void)
 {
   if (tos < stack)
     die ("underflow in string stack");
@@ -321,7 +277,7 @@ check_range ()
 }
 
 static void
-icheck_range ()
+icheck_range (void)
 {
   if (isp < istack)
     die ("underflow in integer stack");
@@ -329,44 +285,8 @@ icheck_range ()
     die ("overflow in integer stack");
 }
 
-#ifdef __STDC__
-static void exec (dict_type *);
-static void call (void);
-static void remchar (void), strip_trailing_newlines (void), push_number (void);
-static void push_text (void);
-static void remove_noncomments (string_type *, string_type *);
-static void print_stack_level (void);
-static void paramstuff (void), translatecomments (void);
-static void outputdots (void), courierize (void), bulletize (void);
-static void do_fancy_stuff (void);
-static int iscommand (string_type *, unsigned int);
-static int copy_past_newline (string_type *, unsigned int, string_type *);
-static void icopy_past_newline (void), kill_bogus_lines (void), indent (void);
-static void get_stuff_in_command (void), swap (void), other_dup (void);
-static void drop (void), idrop (void);
-static void icatstr (void), skip_past_newline (void), internalmode (void);
-static void maybecatstr (void);
-static char *nextword (char *, char **);
-dict_type *lookup_word (char *);
-static void perform (void);
-dict_type *newentry (char *);
-unsigned int add_to_definition (dict_type *, stinst_type);
-void add_intrinsic (char *, void (*)());
-void compile (char *);
-static void bang (void);
-static void atsign (void);
-static void hello (void);
-static void stdout_ (void);
-static void stderr_ (void);
-static void print (void);
-static void read_in (string_type *, FILE *);
-static void usage (void);
-static void chew_exit (void);
-#endif
-
 static void
-exec (word)
-     dict_type *word;
+exec (dict_type *word)
 {
   pc = word->code;
   while (*pc)
@@ -374,7 +294,7 @@ exec (word)
 }
 
 static void
-call ()
+call (void)
 {
   stinst_type *oldpc = pc;
   dict_type *e;
@@ -384,7 +304,7 @@ call ()
 }
 
 static void
-remchar ()
+remchar (void)
 {
   if (tos->write_idx)
     tos->write_idx--;
@@ -392,7 +312,7 @@ remchar ()
 }
 
 static void
-strip_trailing_newlines ()
+strip_trailing_newlines (void)
 {
   while ((isspace ((unsigned char) at (tos, tos->write_idx - 1))
 	  || at (tos, tos->write_idx - 1) == '\n')
@@ -402,7 +322,7 @@ strip_trailing_newlines ()
 }
 
 static void
-push_number ()
+push_number (void)
 {
   isp++;
   icheck_range ();
@@ -412,7 +332,7 @@ push_number ()
 }
 
 static void
-push_text ()
+push_text (void)
 {
   tos++;
   check_range ();
@@ -428,9 +348,7 @@ push_text ()
    Blank lines are turned into one blank line.  */
 
 static void
-remove_noncomments (src, dst)
-     string_type *src;
-     string_type *dst;
+remove_noncomments (string_type *src, string_type *dst)
 {
   unsigned int idx = 0;
 
@@ -481,7 +399,7 @@ remove_noncomments (src, dst)
 }
 
 static void
-print_stack_level ()
+print_stack_level (void)
 {
   fprintf (stderr, "current string stack depth = %ld, ",
 	   (long) (tos - stack));
@@ -499,7 +417,7 @@ print_stack_level ()
  */
 
 static void
-paramstuff ()
+paramstuff (void)
 {
   unsigned int openp;
   unsigned int fname;
@@ -572,7 +490,7 @@ paramstuff ()
    and *} into comments */
 
 static void
-translatecomments ()
+translatecomments (void)
 {
   unsigned int idx = 0;
   string_type out;
@@ -604,7 +522,7 @@ translatecomments ()
 
 /* Mod tos so that only lines with leading dots remain */
 static void
-outputdots ()
+outputdots (void)
 {
   unsigned int idx = 0;
   string_type out;
@@ -653,7 +571,7 @@ outputdots ()
 
 /* Find lines starting with . and | and put example around them on tos */
 static void
-courierize ()
+courierize (void)
 {
   string_type out;
   unsigned int idx = 0;
@@ -740,7 +658,7 @@ courierize ()
    itemize, inplace at TOS*/
 
 static void
-bulletize ()
+bulletize (void)
 {
   unsigned int idx = 0;
   int on = 0;
@@ -795,7 +713,7 @@ bulletize ()
 /* Turn <<foo>> into @code{foo} in place at TOS*/
 
 static void
-do_fancy_stuff ()
+do_fancy_stuff (void)
 {
   unsigned int idx = 0;
   string_type out;
@@ -834,9 +752,7 @@ do_fancy_stuff ()
 /* A command is all upper case,and alone on a line.  */
 
 static int
-iscommand (ptr, idx)
-     string_type *ptr;
-     unsigned int idx;
+iscommand (string_type *ptr, unsigned int idx)
 {
   unsigned int len = 0;
   while (at (ptr, idx))
@@ -860,10 +776,7 @@ iscommand (ptr, idx)
 }
 
 static int
-copy_past_newline (ptr, idx, dst)
-     string_type *ptr;
-     unsigned int idx;
-     string_type *dst;
+copy_past_newline (string_type *ptr, unsigned int idx, string_type *dst)
 {
   int column = 0;
 
@@ -892,7 +805,7 @@ copy_past_newline (ptr, idx, dst)
 }
 
 static void
-icopy_past_newline ()
+icopy_past_newline (void)
 {
   tos++;
   check_range ();
@@ -905,7 +818,7 @@ icopy_past_newline ()
    Take the string at the top of the stack, do some prettying.  */
 
 static void
-kill_bogus_lines ()
+kill_bogus_lines (void)
 {
   int sl;
 
@@ -992,7 +905,7 @@ kill_bogus_lines ()
 }
 
 static void
-indent ()
+indent (void)
 {
   string_type out;
   int tab = 0;
@@ -1043,7 +956,7 @@ indent ()
 }
 
 static void
-get_stuff_in_command ()
+get_stuff_in_command (void)
 {
   tos++;
   check_range ();
@@ -1059,7 +972,7 @@ get_stuff_in_command ()
 }
 
 static void
-swap ()
+swap (void)
 {
   string_type t;
 
@@ -1070,7 +983,7 @@ swap ()
 }
 
 static void
-other_dup ()
+other_dup (void)
 {
   tos++;
   check_range ();
@@ -1080,7 +993,7 @@ other_dup ()
 }
 
 static void
-drop ()
+drop (void)
 {
   tos--;
   check_range ();
@@ -1089,7 +1002,7 @@ drop ()
 }
 
 static void
-idrop ()
+idrop (void)
 {
   isp--;
   icheck_range ();
@@ -1097,7 +1010,7 @@ idrop ()
 }
 
 static void
-icatstr ()
+icatstr (void)
 {
   tos--;
   check_range ();
@@ -1107,14 +1020,14 @@ icatstr ()
 }
 
 static void
-skip_past_newline ()
+skip_past_newline (void)
 {
   idx = skip_past_newline_1 (ptr, idx);
   pc++;
 }
 
 static void
-internalmode ()
+internalmode (void)
 {
   internal_mode = *(isp);
   isp--;
@@ -1123,7 +1036,7 @@ internalmode ()
 }
 
 static void
-maybecatstr ()
+maybecatstr (void)
 {
   if (internal_wanted == internal_mode)
     {
@@ -1136,9 +1049,7 @@ maybecatstr ()
 }
 
 char *
-nextword (string, word)
-     char *string;
-     char **word;
+nextword (char *string, char **word)
 {
   char *word_start;
   int idx;
@@ -1226,8 +1137,7 @@ nextword (string, word)
 }
 
 dict_type *
-lookup_word (word)
-     char *word;
+lookup_word (char *word)
 {
   dict_type *ptr = root;
   while (ptr)
@@ -1304,8 +1214,7 @@ perform (void)
 }
 
 dict_type *
-newentry (word)
-     char *word;
+newentry (char *word)
 {
   dict_type *new_d = (dict_type *) malloc (sizeof (dict_type));
   new_d->word = word;
@@ -1318,9 +1227,7 @@ newentry (word)
 }
 
 unsigned int
-add_to_definition (entry, word)
-     dict_type *entry;
-     stinst_type word;
+add_to_definition (dict_type *entry, stinst_type word)
 {
   if (entry->code_end == entry->code_length)
     {
@@ -1335,9 +1242,7 @@ add_to_definition (entry, word)
 }
 
 void
-add_intrinsic (name, func)
-     char *name;
-     void (*func) ();
+add_intrinsic (char *name, void (*func) (void))
 {
   dict_type *new_d = newentry (strdup (name));
   add_to_definition (new_d, func);
@@ -1345,8 +1250,7 @@ add_intrinsic (name, func)
 }
 
 void
-compile (string)
-     char *string;
+compile (char *string)
 {
   /* Add words to the dictionary.  */
   char *word;
@@ -1419,7 +1323,7 @@ compile (string)
 }
 
 static void
-bang ()
+bang (void)
 {
   *(long *) ((isp[0])) = isp[-1];
   isp -= 2;
@@ -1428,21 +1332,21 @@ bang ()
 }
 
 static void
-atsign ()
+atsign (void)
 {
   isp[0] = *(long *) (isp[0]);
   pc++;
 }
 
 static void
-hello ()
+hello (void)
 {
   printf ("hello\n");
   pc++;
 }
 
 static void
-stdout_ ()
+stdout_ (void)
 {
   isp++;
   icheck_range ();
@@ -1451,7 +1355,7 @@ stdout_ ()
 }
 
 static void
-stderr_ ()
+stderr_ (void)
 {
   isp++;
   icheck_range ();
@@ -1460,7 +1364,7 @@ stderr_ ()
 }
 
 static void
-print ()
+print (void)
 {
   if (*isp == 1)
     write_buffer (tos, stdout);
@@ -1476,9 +1380,7 @@ print ()
 }
 
 static void
-read_in (str, file)
-     string_type *str;
-     FILE *file;
+read_in (string_type *str, FILE *file)
 {
   char buff[10000];
   unsigned int r;
@@ -1494,7 +1396,7 @@ read_in (str, file)
 }
 
 static void
-usage ()
+usage (void)
 {
   fprintf (stderr, "usage: -[d|i|g] <file >file\n");
   exit (33);
@@ -1506,15 +1408,13 @@ usage ()
    is a pointless waste of time.  */
 
 static void
-chew_exit ()
+chew_exit (void)
 {
   exit (0);
 }
 
 int
-main (ac, av)
-     int ac;
-     char *av[];
+main (int ac, char *av[])
 {
   unsigned int i;
   string_type buffer;

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 6+ messages in thread

* use libiberty xmalloc in bfd/doc/chew.c
  2022-05-30  9:33 Reorganise bfd/doc/chew.c a little Alan Modra
  2022-05-30  9:33 ` Update K&R functions in bfd/doc/chew.c Alan Modra
@ 2022-05-30  9:34 ` Alan Modra
  2022-06-01  0:10   ` H.J. Lu
  2022-05-30  9:35 ` Use a union to avoid casts " Alan Modra
  2 siblings, 1 reply; 6+ messages in thread
From: Alan Modra @ 2022-05-30  9:34 UTC (permalink / raw)
  To: binutils

Catch out-of-memory.

	* doc/chew.c: Include libibery.h.
	(init_string_with_size, nextword): Replace malloc with xmalloc.
	(newentry, add_to_definition): Likewise.
	(catchar, catbuf): Replace realloc with xrealloc.
	(add_intrinsic): Replace strdup with xstrdup.
	* doc/local.mk (LIBIBERTY): Define.
	(chew): Link against libiberty.
	* Makefile.in: Regenerate.

diff --git a/bfd/Makefile.in b/bfd/Makefile.in
index 741e08d603c..53cac75af0e 100644
--- a/bfd/Makefile.in
+++ b/bfd/Makefile.in
@@ -1301,6 +1301,7 @@ doc_bfd_TEXINFOS = $(DOCFILES) doc/bfdsumm.texi
 AM_MAKEINFOFLAGS = --no-split -I "$(srcdir)/doc" -I doc
 TEXI2DVI = texi2dvi -I "$(srcdir)/doc" -I doc
 MKDOC = doc/chew$(EXEEXT_FOR_BUILD)
+LIBIBERTY = ../libiberty/libiberty.a
 
 # We can't replace these rules with an implicit rule, because
 # makes without VPATH support couldn't find the .h files in `..'.
@@ -2487,7 +2488,7 @@ doc/chew.stamp: $(srcdir)/doc/chew.c doc/$(am__dirstamp)
 	$(AM_V_CCLD)$(CC_FOR_BUILD) -o doc/chw$$$$$(EXEEXT_FOR_BUILD) $(CFLAGS_FOR_BUILD) \
 	  $(LDFLAGS_FOR_BUILD) $(H_CFLAGS) \
 	  -I. -I$(srcdir) -Idoc -I$(srcdir)/../include -I$(srcdir)/../intl -I../intl \
-	  $(srcdir)/doc/chew.c && \
+	  $(srcdir)/doc/chew.c $(LIBIBERTY) && \
 	$(SHELL) $(srcdir)/../move-if-change \
 	  doc/chw$$$$$(EXEEXT_FOR_BUILD) $(MKDOC) && \
 	touch $@
diff --git a/bfd/doc/chew.c b/bfd/doc/chew.c
index 22921e68431..9722a89f0e6 100644
--- a/bfd/doc/chew.c
+++ b/bfd/doc/chew.c
@@ -82,6 +82,7 @@
    Foo.  */
 
 #include "ansidecl.h"
+#include "libiberty.h"
 #include <assert.h>
 #include <stdio.h>
 #include <ctype.h>
@@ -141,7 +142,7 @@ init_string_with_size (string_type *buffer, unsigned int size)
 {
   buffer->write_idx = 0;
   buffer->size = size;
-  buffer->ptr = (char *) malloc (size);
+  buffer->ptr = xmalloc (size);
 }
 
 static void
@@ -201,7 +202,7 @@ catchar (string_type *buffer, int ch)
   if (buffer->write_idx == buffer->size)
     {
       buffer->size *= 2;
-      buffer->ptr = (char *) realloc (buffer->ptr, buffer->size);
+      buffer->ptr = xrealloc (buffer->ptr, buffer->size);
     }
 
   buffer->ptr[buffer->write_idx++] = ch;
@@ -223,7 +224,7 @@ catbuf (string_type *buffer, char *buf, unsigned int len)
     {
       while (buffer->write_idx + len >= buffer->size)
 	buffer->size *= 2;
-      buffer->ptr = (char *) realloc (buffer->ptr, buffer->size);
+      buffer->ptr = xrealloc (buffer->ptr, buffer->size);
     }
   memcpy (buffer->ptr + buffer->write_idx, buf, len);
   buffer->write_idx += len;
@@ -1102,7 +1103,7 @@ nextword (char *string, char **word)
 	}
     }
 
-  *word = (char *) malloc (length + 1);
+  *word = xmalloc (length + 1);
 
   dst = *word;
   src = word_start;
@@ -1216,11 +1217,11 @@ perform (void)
 dict_type *
 newentry (char *word)
 {
-  dict_type *new_d = (dict_type *) malloc (sizeof (dict_type));
+  dict_type *new_d = xmalloc (sizeof (*new_d));
   new_d->word = word;
   new_d->next = root;
   root = new_d;
-  new_d->code = (stinst_type *) malloc (sizeof (stinst_type));
+  new_d->code = xmalloc (sizeof (*new_d->code));
   new_d->code_length = 1;
   new_d->code_end = 0;
   return new_d;
@@ -1232,9 +1233,8 @@ add_to_definition (dict_type *entry, stinst_type word)
   if (entry->code_end == entry->code_length)
     {
       entry->code_length += 2;
-      entry->code =
-	(stinst_type *) realloc ((char *) (entry->code),
-				 entry->code_length * sizeof (stinst_type));
+      entry->code = xrealloc (entry->code,
+			      entry->code_length * sizeof (*entry->code));
     }
   entry->code[entry->code_end] = word;
 
@@ -1244,7 +1244,7 @@ add_to_definition (dict_type *entry, stinst_type word)
 void
 add_intrinsic (char *name, void (*func) (void))
 {
-  dict_type *new_d = newentry (strdup (name));
+  dict_type *new_d = newentry (xstrdup (name));
   add_to_definition (new_d, func);
   add_to_definition (new_d, 0);
 }
diff --git a/bfd/doc/local.mk b/bfd/doc/local.mk
index 931942f874c..8c6932802f6 100644
--- a/bfd/doc/local.mk
+++ b/bfd/doc/local.mk
@@ -82,12 +82,14 @@ TEXI2DVI = texi2dvi -I "$(srcdir)/%D%" -I %D%
 
 MKDOC = %D%/chew$(EXEEXT_FOR_BUILD)
 
+LIBIBERTY = ../libiberty/libiberty.a
+
 $(MKDOC): %D%/chew.stamp ; @true
 %D%/chew.stamp: $(srcdir)/%D%/chew.c %D%/$(am__dirstamp)
 	$(AM_V_CCLD)$(CC_FOR_BUILD) -o %D%/chw$$$$$(EXEEXT_FOR_BUILD) $(CFLAGS_FOR_BUILD) \
 	  $(LDFLAGS_FOR_BUILD) $(H_CFLAGS) \
 	  -I. -I$(srcdir) -I%D% -I$(srcdir)/../include -I$(srcdir)/../intl -I../intl \
-	  $(srcdir)/%D%/chew.c && \
+	  $(srcdir)/%D%/chew.c $(LIBIBERTY) && \
 	$(SHELL) $(srcdir)/../move-if-change \
 	  %D%/chw$$$$$(EXEEXT_FOR_BUILD) $(MKDOC) && \
 	touch $@

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Use a union to avoid casts in bfd/doc/chew.c
  2022-05-30  9:33 Reorganise bfd/doc/chew.c a little Alan Modra
  2022-05-30  9:33 ` Update K&R functions in bfd/doc/chew.c Alan Modra
  2022-05-30  9:34 ` use libiberty xmalloc " Alan Modra
@ 2022-05-30  9:35 ` Alan Modra
  2 siblings, 0 replies; 6+ messages in thread
From: Alan Modra @ 2022-05-30  9:35 UTC (permalink / raw)
  To: binutils

This fixes -Wpedantic warnings in chew.c.  Conversion between function
and object pointers is not guaranteed.  They can even be different
sizes, not that we're likely to encounter build machines like that
nowadays.

	PR 29194
	* doc/chew.c (pcu): New union typedef.
	(dict_type, pc): Use it here.  Adjust uses of pc.
	(add_to_definition): Make "word" param a pcu.  Adjust all uses
	of function.
	(stinst_type): Delete.

diff --git a/bfd/doc/chew.c b/bfd/doc/chew.c
index 9722a89f0e6..a12c00370d9 100644
--- a/bfd/doc/chew.c
+++ b/bfd/doc/chew.c
@@ -101,13 +101,21 @@ typedef struct buffer
   unsigned long size;
 } string_type;
 
-typedef void (*stinst_type) (void);
+/* Compiled programs consist of arrays of these.  */
+
+typedef union
+{
+  void (*f) (void);
+  struct dict_struct *e;
+  char *s;
+  long l;
+} pcu;
 
 typedef struct dict_struct
 {
   char *word;
   struct dict_struct *next;
-  stinst_type *code;
+  pcu *code;
   int code_length;
   int code_end;
 } dict_type;
@@ -128,7 +136,7 @@ long *isp = &istack[0];
 
 dict_type *root;
 
-stinst_type *pc;
+pcu *pc;
 
 static void
 die (char *msg)
@@ -290,16 +298,15 @@ static void
 exec (dict_type *word)
 {
   pc = word->code;
-  while (*pc)
-    (*pc) ();
+  while (pc->f)
+    pc->f ();
 }
 
 static void
 call (void)
 {
-  stinst_type *oldpc = pc;
-  dict_type *e;
-  e = (dict_type *) (pc[1]);
+  pcu *oldpc = pc;
+  dict_type *e = pc[1].e;
   exec (e);
   pc = oldpc + 2;
 }
@@ -328,7 +335,7 @@ push_number (void)
   isp++;
   icheck_range ();
   pc++;
-  *isp = (long) (*pc);
+  *isp = pc->l;
   pc++;
 }
 
@@ -339,7 +346,7 @@ push_text (void)
   check_range ();
   init_string (tos);
   pc++;
-  cattext (tos, *((char **) pc));
+  cattext (tos, pc->s);
   pc++;
 }
 
@@ -1166,11 +1173,11 @@ free_words (void)
 	{
 	  int i;
 	  for (i = 0; i < ptr->code_end - 1; i ++)
-	    if (ptr->code[i] == push_text
-		&& ptr->code[i + 1])
+	    if (ptr->code[i].f == push_text
+		&& ptr->code[i + 1].s)
 	      {
-		free ((char *) ptr->code[i + 1] - 1);
-		++ i;
+		free (ptr->code[i + 1].s - 1);
+		++i;
 	      }
 	  free (ptr->code);
 	}
@@ -1228,7 +1235,7 @@ newentry (char *word)
 }
 
 unsigned int
-add_to_definition (dict_type *entry, stinst_type word)
+add_to_definition (dict_type *entry, pcu word)
 {
   if (entry->code_end == entry->code_length)
     {
@@ -1245,8 +1252,10 @@ void
 add_intrinsic (char *name, void (*func) (void))
 {
   dict_type *new_d = newentry (xstrdup (name));
-  add_to_definition (new_d, func);
-  add_to_definition (new_d, 0);
+  pcu p = { func };
+  add_to_definition (new_d, p);
+  p.f = 0;
+  add_to_definition (new_d, p);
 }
 
 void
@@ -1261,6 +1270,7 @@ compile (char *string)
       if (word[0] == ':')
 	{
 	  dict_type *ptr;
+	  pcu p;
 
 	  /* Compile a word and add to dictionary.  */
 	  free (word);
@@ -1283,8 +1293,10 @@ compile (char *string)
 		case '"':
 		  /* got a string, embed magic push string
 		     function */
-		  add_to_definition (ptr, push_text);
-		  add_to_definition (ptr, (stinst_type) (word + 1));
+		  p.f = push_text;
+		  add_to_definition (ptr, p);
+		  p.s = word + 1;
+		  add_to_definition (ptr, p);
 		  break;
 		case '0':
 		case '1':
@@ -1298,19 +1310,24 @@ compile (char *string)
 		case '9':
 		  /* Got a number, embedd the magic push number
 		     function */
-		  add_to_definition (ptr, push_number);
-		  add_to_definition (ptr, (stinst_type) atol (word));
+		  p.f = push_number;
+		  add_to_definition (ptr, p);
+		  p.l = atol (word);
+		  add_to_definition (ptr, p);
 		  free (word);
 		  break;
 		default:
-		  add_to_definition (ptr, call);
-		  add_to_definition (ptr, (stinst_type) lookup_word (word));
+		  p.f = call;
+		  add_to_definition (ptr, p);
+		  p.e = lookup_word (word);
+		  add_to_definition (ptr, p);
 		  free (word);
 		}
 
 	      string = nextword (string, &word);
 	    }
-	  add_to_definition (ptr, 0);
+	  p.f = 0;
+	  add_to_definition (ptr, p);
 	  free (word);
 	  string = nextword (string, &word);
 	}

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: use libiberty xmalloc in bfd/doc/chew.c
  2022-05-30  9:34 ` use libiberty xmalloc " Alan Modra
@ 2022-06-01  0:10   ` H.J. Lu
  2022-06-01  1:27     ` Alan Modra
  0 siblings, 1 reply; 6+ messages in thread
From: H.J. Lu @ 2022-06-01  0:10 UTC (permalink / raw)
  To: Alan Modra; +Cc: Binutils

On Mon, May 30, 2022 at 2:36 AM Alan Modra via Binutils
<binutils@sourceware.org> wrote:
>
> Catch out-of-memory.
>
>         * doc/chew.c: Include libibery.h.
>         (init_string_with_size, nextword): Replace malloc with xmalloc.
>         (newentry, add_to_definition): Likewise.
>         (catchar, catbuf): Replace realloc with xrealloc.
>         (add_intrinsic): Replace strdup with xstrdup.
>         * doc/local.mk (LIBIBERTY): Define.
>         (chew): Link against libiberty.
>         * Makefile.in: Regenerate.
>
> diff --git a/bfd/Makefile.in b/bfd/Makefile.in
> index 741e08d603c..53cac75af0e 100644
> --- a/bfd/Makefile.in
> +++ b/bfd/Makefile.in
> @@ -1301,6 +1301,7 @@ doc_bfd_TEXINFOS = $(DOCFILES) doc/bfdsumm.texi
>  AM_MAKEINFOFLAGS = --no-split -I "$(srcdir)/doc" -I doc
>  TEXI2DVI = texi2dvi -I "$(srcdir)/doc" -I doc
>  MKDOC = doc/chew$(EXEEXT_FOR_BUILD)
> +LIBIBERTY = ../libiberty/libiberty.a

This breaks --enable-pgo-build since ../libiberty/libiberty.a is a host library
compiled with -fprofile-generate, but chew is a build program.  If we want
to use libiberty.a in a build program, we need to build a libiberty.a for build.

>  # We can't replace these rules with an implicit rule, because
>  # makes without VPATH support couldn't find the .h files in `..'.
> @@ -2487,7 +2488,7 @@ doc/chew.stamp: $(srcdir)/doc/chew.c doc/$(am__dirstamp)
>         $(AM_V_CCLD)$(CC_FOR_BUILD) -o doc/chw$$$$$(EXEEXT_FOR_BUILD) $(CFLAGS_FOR_BUILD) \
>           $(LDFLAGS_FOR_BUILD) $(H_CFLAGS) \
>           -I. -I$(srcdir) -Idoc -I$(srcdir)/../include -I$(srcdir)/../intl -I../intl \
> -         $(srcdir)/doc/chew.c && \
> +         $(srcdir)/doc/chew.c $(LIBIBERTY) && \
>         $(SHELL) $(srcdir)/../move-if-change \
>           doc/chw$$$$$(EXEEXT_FOR_BUILD) $(MKDOC) && \
>         touch $@
> diff --git a/bfd/doc/chew.c b/bfd/doc/chew.c
> index 22921e68431..9722a89f0e6 100644
> --- a/bfd/doc/chew.c
> +++ b/bfd/doc/chew.c
> @@ -82,6 +82,7 @@
>     Foo.  */
>
>  #include "ansidecl.h"
> +#include "libiberty.h"
>  #include <assert.h>
>  #include <stdio.h>
>  #include <ctype.h>
> @@ -141,7 +142,7 @@ init_string_with_size (string_type *buffer, unsigned int size)
>  {
>    buffer->write_idx = 0;
>    buffer->size = size;
> -  buffer->ptr = (char *) malloc (size);
> +  buffer->ptr = xmalloc (size);
>  }
>
>  static void
> @@ -201,7 +202,7 @@ catchar (string_type *buffer, int ch)
>    if (buffer->write_idx == buffer->size)
>      {
>        buffer->size *= 2;
> -      buffer->ptr = (char *) realloc (buffer->ptr, buffer->size);
> +      buffer->ptr = xrealloc (buffer->ptr, buffer->size);
>      }
>
>    buffer->ptr[buffer->write_idx++] = ch;
> @@ -223,7 +224,7 @@ catbuf (string_type *buffer, char *buf, unsigned int len)
>      {
>        while (buffer->write_idx + len >= buffer->size)
>         buffer->size *= 2;
> -      buffer->ptr = (char *) realloc (buffer->ptr, buffer->size);
> +      buffer->ptr = xrealloc (buffer->ptr, buffer->size);
>      }
>    memcpy (buffer->ptr + buffer->write_idx, buf, len);
>    buffer->write_idx += len;
> @@ -1102,7 +1103,7 @@ nextword (char *string, char **word)
>         }
>      }
>
> -  *word = (char *) malloc (length + 1);
> +  *word = xmalloc (length + 1);
>
>    dst = *word;
>    src = word_start;
> @@ -1216,11 +1217,11 @@ perform (void)
>  dict_type *
>  newentry (char *word)
>  {
> -  dict_type *new_d = (dict_type *) malloc (sizeof (dict_type));
> +  dict_type *new_d = xmalloc (sizeof (*new_d));
>    new_d->word = word;
>    new_d->next = root;
>    root = new_d;
> -  new_d->code = (stinst_type *) malloc (sizeof (stinst_type));
> +  new_d->code = xmalloc (sizeof (*new_d->code));
>    new_d->code_length = 1;
>    new_d->code_end = 0;
>    return new_d;
> @@ -1232,9 +1233,8 @@ add_to_definition (dict_type *entry, stinst_type word)
>    if (entry->code_end == entry->code_length)
>      {
>        entry->code_length += 2;
> -      entry->code =
> -       (stinst_type *) realloc ((char *) (entry->code),
> -                                entry->code_length * sizeof (stinst_type));
> +      entry->code = xrealloc (entry->code,
> +                             entry->code_length * sizeof (*entry->code));
>      }
>    entry->code[entry->code_end] = word;
>
> @@ -1244,7 +1244,7 @@ add_to_definition (dict_type *entry, stinst_type word)
>  void
>  add_intrinsic (char *name, void (*func) (void))
>  {
> -  dict_type *new_d = newentry (strdup (name));
> +  dict_type *new_d = newentry (xstrdup (name));
>    add_to_definition (new_d, func);
>    add_to_definition (new_d, 0);
>  }
> diff --git a/bfd/doc/local.mk b/bfd/doc/local.mk
> index 931942f874c..8c6932802f6 100644
> --- a/bfd/doc/local.mk
> +++ b/bfd/doc/local.mk
> @@ -82,12 +82,14 @@ TEXI2DVI = texi2dvi -I "$(srcdir)/%D%" -I %D%
>
>  MKDOC = %D%/chew$(EXEEXT_FOR_BUILD)
>
> +LIBIBERTY = ../libiberty/libiberty.a
> +
>  $(MKDOC): %D%/chew.stamp ; @true
>  %D%/chew.stamp: $(srcdir)/%D%/chew.c %D%/$(am__dirstamp)
>         $(AM_V_CCLD)$(CC_FOR_BUILD) -o %D%/chw$$$$$(EXEEXT_FOR_BUILD) $(CFLAGS_FOR_BUILD) \
>           $(LDFLAGS_FOR_BUILD) $(H_CFLAGS) \
>           -I. -I$(srcdir) -I%D% -I$(srcdir)/../include -I$(srcdir)/../intl -I../intl \
> -         $(srcdir)/%D%/chew.c && \
> +         $(srcdir)/%D%/chew.c $(LIBIBERTY) && \
>         $(SHELL) $(srcdir)/../move-if-change \
>           %D%/chw$$$$$(EXEEXT_FOR_BUILD) $(MKDOC) && \
>         touch $@
>
> --
> Alan Modra
> Australia Development Lab, IBM



-- 
H.J.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: use libiberty xmalloc in bfd/doc/chew.c
  2022-06-01  0:10   ` H.J. Lu
@ 2022-06-01  1:27     ` Alan Modra
  0 siblings, 0 replies; 6+ messages in thread
From: Alan Modra @ 2022-06-01  1:27 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Binutils

On Tue, May 31, 2022 at 05:10:16PM -0700, H.J. Lu wrote:
> On Mon, May 30, 2022 at 2:36 AM Alan Modra via Binutils
> <binutils@sourceware.org> wrote:
> >
> > Catch out-of-memory.
> >
> >         * doc/chew.c: Include libibery.h.
> >         (init_string_with_size, nextword): Replace malloc with xmalloc.
> >         (newentry, add_to_definition): Likewise.
> >         (catchar, catbuf): Replace realloc with xrealloc.
> >         (add_intrinsic): Replace strdup with xstrdup.
> >         * doc/local.mk (LIBIBERTY): Define.
> >         (chew): Link against libiberty.
> >         * Makefile.in: Regenerate.
> >
> This breaks --enable-pgo-build since ../libiberty/libiberty.a is a host library
> compiled with -fprofile-generate, but chew is a build program.  If we want
> to use libiberty.a in a build program, we need to build a libiberty.a for build.

Drat, I didn't think of that.  Committing the following:

We can't use libiberty.a in chew.  libiberty is a host library, chew
a build program.  Partly revert commit 7273d78f3f7a, instead define
local versions of the libiberty functions.  ansidecl.h also isn't
needed.

	* doc/chew.c: Don't include libiberty.h or ansidecl.h.
	(xmalloc, xrealloc, xstrdup): New functions.
	* doc/local.mk (LIBIBERTY): Don't define or use.
	* Makefile.in: Regenerate.

diff --git a/bfd/Makefile.in b/bfd/Makefile.in
index 53cac75af0e..741e08d603c 100644
--- a/bfd/Makefile.in
+++ b/bfd/Makefile.in
@@ -1301,7 +1301,6 @@ doc_bfd_TEXINFOS = $(DOCFILES) doc/bfdsumm.texi
 AM_MAKEINFOFLAGS = --no-split -I "$(srcdir)/doc" -I doc
 TEXI2DVI = texi2dvi -I "$(srcdir)/doc" -I doc
 MKDOC = doc/chew$(EXEEXT_FOR_BUILD)
-LIBIBERTY = ../libiberty/libiberty.a
 
 # We can't replace these rules with an implicit rule, because
 # makes without VPATH support couldn't find the .h files in `..'.
@@ -2488,7 +2487,7 @@ doc/chew.stamp: $(srcdir)/doc/chew.c doc/$(am__dirstamp)
 	$(AM_V_CCLD)$(CC_FOR_BUILD) -o doc/chw$$$$$(EXEEXT_FOR_BUILD) $(CFLAGS_FOR_BUILD) \
 	  $(LDFLAGS_FOR_BUILD) $(H_CFLAGS) \
 	  -I. -I$(srcdir) -Idoc -I$(srcdir)/../include -I$(srcdir)/../intl -I../intl \
-	  $(srcdir)/doc/chew.c $(LIBIBERTY) && \
+	  $(srcdir)/doc/chew.c && \
 	$(SHELL) $(srcdir)/../move-if-change \
 	  doc/chw$$$$$(EXEEXT_FOR_BUILD) $(MKDOC) && \
 	touch $@
diff --git a/bfd/doc/chew.c b/bfd/doc/chew.c
index a12c00370d9..1695173970a 100644
--- a/bfd/doc/chew.c
+++ b/bfd/doc/chew.c
@@ -81,8 +81,6 @@
 
    Foo.  */
 
-#include "ansidecl.h"
-#include "libiberty.h"
 #include <assert.h>
 #include <stdio.h>
 #include <ctype.h>
@@ -145,6 +143,45 @@ die (char *msg)
   exit (1);
 }
 
+void *
+xmalloc (size_t size)
+{
+  void *newmem;
+
+  if (size == 0)
+    size = 1;
+  newmem = malloc (size);
+  if (!newmem)
+    die ("out of memory");
+
+  return newmem;
+}
+
+void *
+xrealloc (void *oldmem, size_t size)
+{
+  void *newmem;
+
+  if (size == 0)
+    size = 1;
+  if (!oldmem)
+    newmem = malloc (size);
+  else
+    newmem = realloc (oldmem, size);
+  if (!newmem)
+    die ("out of memory");
+
+  return newmem;
+}
+
+char *
+xstrdup (const char *s)
+{
+  size_t len = strlen (s) + 1;
+  char *ret = xmalloc (len);
+  return memcpy (ret, s, len);
+}
+
 static void
 init_string_with_size (string_type *buffer, unsigned int size)
 {
diff --git a/bfd/doc/local.mk b/bfd/doc/local.mk
index 8c6932802f6..931942f874c 100644
--- a/bfd/doc/local.mk
+++ b/bfd/doc/local.mk
@@ -82,14 +82,12 @@ TEXI2DVI = texi2dvi -I "$(srcdir)/%D%" -I %D%
 
 MKDOC = %D%/chew$(EXEEXT_FOR_BUILD)
 
-LIBIBERTY = ../libiberty/libiberty.a
-
 $(MKDOC): %D%/chew.stamp ; @true
 %D%/chew.stamp: $(srcdir)/%D%/chew.c %D%/$(am__dirstamp)
 	$(AM_V_CCLD)$(CC_FOR_BUILD) -o %D%/chw$$$$$(EXEEXT_FOR_BUILD) $(CFLAGS_FOR_BUILD) \
 	  $(LDFLAGS_FOR_BUILD) $(H_CFLAGS) \
 	  -I. -I$(srcdir) -I%D% -I$(srcdir)/../include -I$(srcdir)/../intl -I../intl \
-	  $(srcdir)/%D%/chew.c $(LIBIBERTY) && \
+	  $(srcdir)/%D%/chew.c && \
 	$(SHELL) $(srcdir)/../move-if-change \
 	  %D%/chw$$$$$(EXEEXT_FOR_BUILD) $(MKDOC) && \
 	touch $@

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-06-01  1:27 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-30  9:33 Reorganise bfd/doc/chew.c a little Alan Modra
2022-05-30  9:33 ` Update K&R functions in bfd/doc/chew.c Alan Modra
2022-05-30  9:34 ` use libiberty xmalloc " Alan Modra
2022-06-01  0:10   ` H.J. Lu
2022-06-01  1:27     ` Alan Modra
2022-05-30  9:35 ` Use a union to avoid casts " Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).