public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Subject: Use a union to avoid casts in bfd/doc/chew.c
Date: Mon, 30 May 2022 19:05:18 +0930	[thread overview]
Message-ID: <YpSP1lUZu95R2yrK@squeak.grove.modra.org> (raw)
In-Reply-To: <YpSPUD+p3Kc+pxls@squeak.grove.modra.org>

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

      parent reply	other threads:[~2022-05-30  9:35 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Alan Modra [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YpSP1lUZu95R2yrK@squeak.grove.modra.org \
    --to=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).