public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] sim: nltvals: localize TARGET_<open> defines
@ 2021-10-31  8:50 Mike Frysinger
  0 siblings, 0 replies; only message in thread
From: Mike Frysinger @ 2021-10-31  8:50 UTC (permalink / raw)
  To: gdb-patches

Code should not be using these directly, instead they should be
resolving these dynamically via the open_map.  Rework the common
callback code that was using the defines to use symbolic names
instead, and localize some of the defines in the ARM code (since
it's a bit unclear how many different APIs it supports currently),
then remove the defines out of the header so no new code can rely on
them.
---
 sim/arm/armos.c       | 17 +++++++++++------
 sim/common/callback.c | 42 ++++++++++++++++++++++++++++--------------
 sim/common/gentmap.c  |  6 +-----
 3 files changed, 40 insertions(+), 25 deletions(-)

diff --git a/sim/arm/armos.c b/sim/arm/armos.c
index a3713a5c3348..a8ef7e4a82d9 100644
--- a/sim/arm/armos.c
+++ b/sim/arm/armos.c
@@ -31,11 +31,6 @@
 #include <errno.h>
 #include <limits.h>
 #include <string.h>
-#include "targ-vals.h"
-
-#ifndef TARGET_O_BINARY
-#define TARGET_O_BINARY 0
-#endif
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>		/* For SEEK_SET etc.  */
@@ -188,7 +183,17 @@ ARMul_OSInit (ARMul_State * state)
    return TRUE;
 }
 
-static int translate_open_mode[] =
+/* These are libgloss defines, but seem to be common across all supported ARM
+   targets at the moment.  These should get moved to the callback open_map.  */
+#define TARGET_O_BINARY 0
+#define TARGET_O_APPEND 0x8
+#define TARGET_O_CREAT 0x200
+#define TARGET_O_RDONLY 0x0
+#define TARGET_O_RDWR 0x2
+#define TARGET_O_TRUNC 0x400
+#define TARGET_O_WRONLY 0x1
+
+static const int translate_open_mode[] =
 {
   TARGET_O_RDONLY,		/* "r"   */
   TARGET_O_RDONLY + TARGET_O_BINARY,	/* "rb"  */
diff --git a/sim/common/callback.c b/sim/common/callback.c
index 39d068c4866f..941f4302b6e4 100644
--- a/sim/common/callback.c
+++ b/sim/common/callback.c
@@ -37,7 +37,6 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include "sim/callback.h"
-#include "targ-vals.h"
 /* For xmalloc.  */
 #include "libiberty.h"
 
@@ -886,29 +885,44 @@ cb_target_to_host_open (host_callback *cb, int target_val)
 {
   int host_val = 0;
   CB_TARGET_DEFS_MAP *m;
+  int o_rdonly = 0;
+  int o_wronly = 0;
+  int o_rdwr = 0;
+  int o_binary = 0;
+  int o_rdwrmask;
 
+  /* O_RDONLY can be (and usually is) 0 which needs to be treated specially.  */
   for (m = &cb->open_map[0]; m->host_val != -1; ++m)
     {
-      switch (m->target_val)
+      if (!strcmp (m->name, "O_RDONLY"))
+	o_rdonly = m->target_val;
+      else if (!strcmp (m->name, "O_WRONLY"))
+	o_wronly = m->target_val;
+      else if (!strcmp (m->name, "O_RDWR"))
+	o_rdwr = m->target_val;
+      else if (!strcmp (m->name, "O_BINARY"))
+	o_binary = m->target_val;
+    }
+  o_rdwrmask = o_rdonly | o_wronly | o_rdwr;
+
+  for (m = &cb->open_map[0]; m->host_val != -1; ++m)
+    {
+      if (m->target_val == o_rdonly || m->target_val == o_wronly
+	  || m->target_val == o_rdwr)
 	{
-	  /* O_RDONLY can be (and usually is) 0 which needs to be treated
-	     specially.  */
-	case TARGET_O_RDONLY :
-	case TARGET_O_WRONLY :
-	case TARGET_O_RDWR :
-	  if ((target_val & (TARGET_O_RDONLY | TARGET_O_WRONLY | TARGET_O_RDWR))
-	      == m->target_val)
+	  if ((target_val & o_rdwrmask) == m->target_val)
 	    host_val |= m->host_val;
 	  /* Handle the host/target differentiating between binary and
              text mode.  Only one case is of importance */
-#if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
-	  host_val |= O_BINARY;
+#ifdef O_BINARY
+	  if (o_binary == 0)
+	    host_val |= O_BINARY;
 #endif
-	  break;
-	default :
+	}
+      else
+	{
 	  if ((m->target_val & target_val) == m->target_val)
 	    host_val |= m->host_val;
-	  break;
 	}
     }
 
diff --git a/sim/common/gentmap.c b/sim/common/gentmap.c
index fbc290185b52..2c7288bd6056 100644
--- a/sim/common/gentmap.c
+++ b/sim/common/gentmap.c
@@ -39,11 +39,6 @@ gen_targ_vals_h (void)
     printf ("#define TARGET_%s %d\n", t->symbol, t->value);
   printf ("\n");
 
-  printf ("/* open flag values */\n");
-  for (t = &open_tdefs[0]; t->symbol; ++t)
-    printf ("#define TARGET_%s 0x%x\n", t->symbol, t->value);
-  printf ("\n");
-
   printf ("#endif /* TARG_VALS_H */\n");
 }
 
@@ -78,6 +73,7 @@ gen_targ_map_c (void)
   printf ("CB_TARGET_DEFS_MAP cb_init_open_map[] = {\n");
   for (t = &open_tdefs[0]; t->symbol; ++t)
     {
+      printf ("#define TARGET_%s 0x%x\n", t->symbol, t->value);
       printf ("#ifdef %s\n", t->symbol);
       printf ("  { \"%s\", %s, TARGET_%s },\n", t->symbol, t->symbol, t->symbol);
       printf ("#endif\n");
-- 
2.33.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-10-31  8:51 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-31  8:50 [PATCH] sim: nltvals: localize TARGET_<open> defines Mike Frysinger

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).