[PATCH 1/8] sim: invert sim_state storage

Mike Frysinger vapier@gentoo.org
Thu May 13 10:07:42 GMT 2021


Currently all ports have to declare sim_state themselves in their
sim-main.h and then embed the common sim_state_base & sim_cpu in it.
This dynamic makes it impossible to share common object code among
multiple ports because the core data structure is always different.

Let's invert this relationship: common code declares sim_state, and
if the port actually needs state on a per-instance basis, it can use
the new arch_data field for it.  Most ports don't actually use it,
so they don't need to declare anything at all.

This is the first in a series of changes: it adds a define to select
between the old & new layouts, then converts all the ports that don't
need custom state over to the new layout.
---
 sim/aarch64/sim-main.h         |  9 ++-------
 sim/arm/sim-main.h             |  9 ++-------
 sim/common/sim-base.h          | 30 +++++++++++++++++++++---------
 sim/common/sim-utils.c         |  9 +++++++--
 sim/cr16/sim-main.h            |  9 ++-------
 sim/d10v/sim-main.h            |  9 ++-------
 sim/example-synacor/sim-main.h |  9 ++-------
 sim/ft32/sim-main.h            |  9 ++-------
 sim/m68hc11/sim-main.h         |  9 ++-------
 sim/mcore/sim-main.h           |  9 ++-------
 sim/microblaze/sim-main.h      |  9 ++-------
 sim/mn10300/sim-main.h         | 13 ++-----------
 sim/moxie/sim-main.h           |  9 ++-------
 sim/msp430/sim-main.h          | 10 ++--------
 sim/pru/sim-main.h             |  7 ++-----
 sim/sh/sim-main.h              |  9 ++-------
 sim/v850/sim-main.h            | 14 ++------------
 17 files changed, 58 insertions(+), 124 deletions(-)

diff --git a/sim/aarch64/sim-main.h b/sim/aarch64/sim-main.h
index a2704a75ba1d..f1f9e5cf681f 100644
--- a/sim/aarch64/sim-main.h
+++ b/sim/aarch64/sim-main.h
@@ -22,6 +22,8 @@
 #ifndef _SIM_MAIN_H
 #define _SIM_MAIN_H
 
+#define SIM_HAVE_COMMON_SIM_STATE
+
 #include "sim-basics.h"
 #include "sim-types.h"
 #include "sim-base.h"
@@ -60,11 +62,4 @@ typedef enum
   AARCH64_MAX_REGNO  = 67
 } aarch64_regno;
 
-/* The simulator state structure used to hold all global variables.  */
-struct sim_state
-{
-  sim_cpu *       cpu[MAX_NR_PROCESSORS];
-  sim_state_base  base;
-};
-
 #endif /* _SIM_MAIN_H */
diff --git a/sim/arm/sim-main.h b/sim/arm/sim-main.h
index 7644b9349086..b47149441339 100644
--- a/sim/arm/sim-main.h
+++ b/sim/arm/sim-main.h
@@ -19,6 +19,8 @@
 #ifndef SIM_MAIN_H
 #define SIM_MAIN_H
 
+#define SIM_HAVE_COMMON_SIM_STATE
+
 #include "sim-basics.h"
 #include "sim-base.h"
 #include "bfd.h"
@@ -33,11 +35,4 @@ struct _sim_cpu {
   sim_cpu_base base;
 };
 
-struct sim_state {
-
-  sim_cpu *cpu[MAX_NR_PROCESSORS];
-
-  sim_state_base base;
-};
-
 #endif
diff --git a/sim/common/sim-base.h b/sim/common/sim-base.h
index 731fc7f6d086..f38e8412aed6 100644
--- a/sim/common/sim-base.h
+++ b/sim/common/sim-base.h
@@ -42,14 +42,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
         sim_cpu_base base;
      };
 
-     struct sim_state {
-       sim_cpu *cpu[MAX_NR_PROCESSORS];
-       ... simulator specific members ...
-       sim_state_base base;
-     };
-
-   Note that `base' appears last.  This makes `base.magic' appear last
-   in the entire struct and helps catch miscompilation errors. */
+   If your sim needs to allocate sim-wide state, use STATE_ARCH_DATA.  */
 
 
 #ifndef SIM_BASE_H
@@ -226,8 +219,27 @@ typedef struct {
 #define STATE_MAGIC(sd) ((sd)->base.magic)
 } sim_state_base;
 
+#ifdef SIM_HAVE_COMMON_SIM_STATE
+/* TODO: Merge sim_state & sim_state_base.  */
+struct sim_state {
+  /* All the cpus for this instance.  */
+  sim_cpu *cpu[MAX_NR_PROCESSORS];
+
+  /* All the common state.  */
+  sim_state_base base;
+
+  /* Pointer for sim target to store arbitrary state data.  Normally the
+     target should define a struct and use it here.  */
+  void *arch_data;
+#define STATE_ARCH_DATA(sd) ((sd)->arch_data)
+};
+#endif
+
 /* Functions for allocating/freeing a sim_state.  */
-SIM_DESC sim_state_alloc (SIM_OPEN_KIND kind, host_callback *callback);
+SIM_DESC sim_state_alloc_extra (SIM_OPEN_KIND kind, host_callback *callback,
+				size_t extra_bytes);
+#define sim_state_alloc(kind, callback) sim_state_alloc_extra(kind, callback, 0)
+
 void sim_state_free (SIM_DESC);
 
 #ifdef __cplusplus
diff --git a/sim/common/sim-utils.c b/sim/common/sim-utils.c
index 86ab059744ce..a44abb03d7b2 100644
--- a/sim/common/sim-utils.c
+++ b/sim/common/sim-utils.c
@@ -45,8 +45,8 @@ zalloc (unsigned long size)
 /* Allocate a sim_state struct.  */
 
 SIM_DESC
-sim_state_alloc (SIM_OPEN_KIND kind,
-		 host_callback *callback)
+sim_state_alloc_extra (SIM_OPEN_KIND kind, host_callback *callback,
+		       size_t extra_bytes)
 {
   SIM_DESC sd = ZALLOC (struct sim_state);
 
@@ -54,6 +54,11 @@ sim_state_alloc (SIM_OPEN_KIND kind,
   STATE_CALLBACK (sd) = callback;
   STATE_OPEN_KIND (sd) = kind;
 
+#ifdef SIM_HAVE_COMMON_SIM_STATE
+  if (extra_bytes)
+    STATE_ARCH_DATA (sd) = zalloc (extra_bytes);
+#endif
+
 #if 0
   {
     int cpu_nr;
diff --git a/sim/cr16/sim-main.h b/sim/cr16/sim-main.h
index 639774ced1d1..7ea5b1d32802 100644
--- a/sim/cr16/sim-main.h
+++ b/sim/cr16/sim-main.h
@@ -19,6 +19,8 @@
 #ifndef SIM_MAIN_H
 #define SIM_MAIN_H
 
+#define SIM_HAVE_COMMON_SIM_STATE
+
 #include "sim-basics.h"
 
 typedef long int           word;
@@ -34,11 +36,4 @@ struct _sim_cpu {
   sim_cpu_base base;
 };
 
-struct sim_state {
-
-  sim_cpu *cpu[MAX_NR_PROCESSORS];
-
-  sim_state_base base;
-};
-
 #endif
diff --git a/sim/d10v/sim-main.h b/sim/d10v/sim-main.h
index 0182263625e3..83b34b06454d 100644
--- a/sim/d10v/sim-main.h
+++ b/sim/d10v/sim-main.h
@@ -19,6 +19,8 @@
 #ifndef SIM_MAIN_H
 #define SIM_MAIN_H
 
+#define SIM_HAVE_COMMON_SIM_STATE
+
 #include "sim-basics.h"
 
 typedef long int           word;
@@ -34,11 +36,4 @@ struct _sim_cpu {
   sim_cpu_base base;
 };
 
-struct sim_state {
-
-  sim_cpu *cpu[MAX_NR_PROCESSORS];
-
-  sim_state_base base;
-};
-
 #endif
diff --git a/sim/example-synacor/sim-main.h b/sim/example-synacor/sim-main.h
index ba0e4efa12a9..32b33b4e90bf 100644
--- a/sim/example-synacor/sim-main.h
+++ b/sim/example-synacor/sim-main.h
@@ -21,6 +21,8 @@
 #ifndef SIM_MAIN_H
 #define SIM_MAIN_H
 
+#define SIM_HAVE_COMMON_SIM_STATE
+
 #include "sim-basics.h"
 #include "sim-base.h"
 
@@ -36,13 +38,6 @@ struct _sim_cpu {
   sim_cpu_base base;
 };
 
-struct sim_state {
-  sim_cpu *cpu[MAX_NR_PROCESSORS];
-
-  /* ... simulator specific members ... */
-  sim_state_base base;
-};
-
 extern void step_once (SIM_CPU *);
 extern void initialize_cpu (SIM_DESC, SIM_CPU *);
 
diff --git a/sim/ft32/sim-main.h b/sim/ft32/sim-main.h
index 7f0f5e607a68..8cf384ceb8b1 100644
--- a/sim/ft32/sim-main.h
+++ b/sim/ft32/sim-main.h
@@ -21,6 +21,8 @@
 #ifndef SIM_MAIN_H
 #define SIM_MAIN_H
 
+#define SIM_HAVE_COMMON_SIM_STATE
+
 #include "sim-basics.h"
 #include "sim-base.h"
 #include "bfd.h"
@@ -36,11 +38,4 @@ struct _sim_cpu {
   sim_cpu_base base;
 };
 
-struct sim_state {
-
-  sim_cpu *cpu[MAX_NR_PROCESSORS];
-
-  sim_state_base base;
-};
-
 #endif
diff --git a/sim/m68hc11/sim-main.h b/sim/m68hc11/sim-main.h
index f42750f0a586..a628f0212a3c 100644
--- a/sim/m68hc11/sim-main.h
+++ b/sim/m68hc11/sim-main.h
@@ -20,6 +20,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifndef _SIM_MAIN_H
 #define _SIM_MAIN_H
 
+#define SIM_HAVE_COMMON_SIM_STATE
+
 #include "sim-basics.h"
 #include "sim-signal.h"
 #include "sim-base.h"
@@ -560,13 +562,6 @@ extern int m68hc11cpu_clear_oscillator (SIM_DESC sd, const char *port);
 extern void m68hc11cpu_set_port (struct hw *me, sim_cpu *cpu,
 				 unsigned addr, uint8 val);
 
-/* The current state of the processor; registers, memory, etc.  */
-
-struct sim_state {
-  sim_cpu        *cpu[MAX_NR_PROCESSORS];
-  sim_state_base base;
-};
-
 extern void sim_board_reset (SIM_DESC sd);
 
 #define PRINT_TIME  0x01
diff --git a/sim/mcore/sim-main.h b/sim/mcore/sim-main.h
index dc749b7e20cd..bfa28d045cea 100644
--- a/sim/mcore/sim-main.h
+++ b/sim/mcore/sim-main.h
@@ -19,6 +19,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifndef SIM_MAIN_H
 #define SIM_MAIN_H
 
+#define SIM_HAVE_COMMON_SIM_STATE
+
 #include "sim-basics.h"
 
 typedef long int           word;
@@ -68,12 +70,5 @@ struct _sim_cpu {
   sim_cpu_base base;
 };
 
-struct sim_state {
-
-  sim_cpu *cpu[MAX_NR_PROCESSORS];
-
-  sim_state_base base;
-};
-
 #endif
 
diff --git a/sim/microblaze/sim-main.h b/sim/microblaze/sim-main.h
index 641771a4af32..d69d814252a4 100644
--- a/sim/microblaze/sim-main.h
+++ b/sim/microblaze/sim-main.h
@@ -18,6 +18,8 @@
 #ifndef MICROBLAZE_SIM_MAIN
 #define MICROBLAZE_SIM_MAIN
 
+#define SIM_HAVE_COMMON_SIM_STATE
+
 #include "microblaze.h"
 #include "sim-basics.h"
 #include "sim-base.h"
@@ -48,11 +50,4 @@ struct _sim_cpu {
   sim_cpu_base base;
 };
 
-struct sim_state {
-
-  sim_cpu *cpu[MAX_NR_PROCESSORS];
-
-  sim_state_base base;
-};
-
 #endif /* MICROBLAZE_SIM_MAIN */
diff --git a/sim/mn10300/sim-main.h b/sim/mn10300/sim-main.h
index 5b3e593a7d0a..5bf068b51fa6 100644
--- a/sim/mn10300/sim-main.h
+++ b/sim/mn10300/sim-main.h
@@ -22,6 +22,8 @@
 #ifndef SIM_MAIN_H
 #define SIM_MAIN_H
 
+#define SIM_HAVE_COMMON_SIM_STATE
+
 #define SIM_ENGINE_HALT_HOOK(SD,LAST_CPU,CIA) 0 /* disable this hook */
 
 #include "sim-basics.h"
@@ -65,17 +67,6 @@ struct _sim_cpu {
   sim_cpu_base base;
 };
 
-
-struct sim_state {
-
-  /* the processors proper */
-  sim_cpu *cpu[MAX_NR_PROCESSORS];
-
-  /* The base class.  */
-  sim_state_base base;
-
-};
-
 /* For compatibility, until all functions converted to passing
    SIM_DESC as an argument */
 extern SIM_DESC simulator;
diff --git a/sim/moxie/sim-main.h b/sim/moxie/sim-main.h
index 24557da1762c..19b9475e9856 100644
--- a/sim/moxie/sim-main.h
+++ b/sim/moxie/sim-main.h
@@ -20,6 +20,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifndef SIM_MAIN_H
 #define SIM_MAIN_H
 
+#define SIM_HAVE_COMMON_SIM_STATE
+
 #include "sim-basics.h"
 #include "sim-base.h"
 #include "bfd.h"
@@ -39,11 +41,4 @@ struct _sim_cpu {
   sim_cpu_base base;
 };
 
-struct sim_state {
-
-  sim_cpu *cpu[MAX_NR_PROCESSORS];
-
-  sim_state_base base;
-};
-
 #endif
diff --git a/sim/msp430/sim-main.h b/sim/msp430/sim-main.h
index 22a53b4c9d3c..f98712c12e5c 100644
--- a/sim/msp430/sim-main.h
+++ b/sim/msp430/sim-main.h
@@ -21,6 +21,8 @@
 #ifndef _MSP430_MAIN_SIM_H_
 #define _MSP430_MAIN_SIM_H_
 
+#define SIM_HAVE_COMMON_SIM_STATE
+
 #include "sim-basics.h"
 #include "sim-signal.h"
 #include "msp430-sim.h"
@@ -33,14 +35,6 @@ struct _sim_cpu
   sim_cpu_base base;
 };
 
-struct sim_state
-{
-  sim_cpu *cpu[MAX_NR_PROCESSORS];
-
-  /* Simulator specific members.  */
-  sim_state_base base;
-};
-
 #define MSP430_CPU(sd)       (STATE_CPU ((sd), 0))
 #define MSP430_CPU_STATE(sd) (MSP430_CPU ((sd)->state))
 
diff --git a/sim/pru/sim-main.h b/sim/pru/sim-main.h
index 966cacf5693d..49e763ab0396 100644
--- a/sim/pru/sim-main.h
+++ b/sim/pru/sim-main.h
@@ -19,6 +19,8 @@
 #ifndef PRU_SIM_MAIN
 #define PRU_SIM_MAIN
 
+#define SIM_HAVE_COMMON_SIM_STATE
+
 #include "config.h"
 
 #include <stdint.h>
@@ -85,9 +87,4 @@ struct _sim_cpu {
   sim_cpu_base base;
 };
 
-struct sim_state {
-  sim_cpu *cpu[MAX_NR_PROCESSORS];
-
-  sim_state_base base;
-};
 #endif /* PRU_SIM_MAIN */
diff --git a/sim/sh/sim-main.h b/sim/sh/sim-main.h
index 090b0ac9a002..58c1436531f1 100644
--- a/sim/sh/sim-main.h
+++ b/sim/sh/sim-main.h
@@ -19,6 +19,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifndef SIM_MAIN_H
 #define SIM_MAIN_H
 
+#define SIM_HAVE_COMMON_SIM_STATE
+
 #include "sim-basics.h"
 #include "sim-base.h"
 
@@ -131,11 +133,4 @@ struct _sim_cpu {
   sim_cpu_base base;
 };
 
-struct sim_state {
-
-  sim_cpu *cpu[MAX_NR_PROCESSORS];
-
-  sim_state_base base;
-};
-
 #endif
diff --git a/sim/v850/sim-main.h b/sim/v850/sim-main.h
index e7276a68f8f6..e0ee2c49d935 100644
--- a/sim/v850/sim-main.h
+++ b/sim/v850/sim-main.h
@@ -1,6 +1,8 @@
 #ifndef SIM_MAIN_H
 #define SIM_MAIN_H
 
+#define SIM_HAVE_COMMON_SIM_STATE
+
 /* The v850 has 32bit words, numbered 31 (MSB) to 0 (LSB) */
 
 #define WITH_TARGET_WORD_MSB 31
@@ -49,18 +51,6 @@ struct _sim_cpu
   sim_cpu_base base;
 };
 
-struct sim_state {
-  sim_cpu *cpu[MAX_NR_PROCESSORS];
-#if 0
-  SIM_ADDR rom_size;
-  SIM_ADDR low_end;
-  SIM_ADDR high_start;
-  SIM_ADDR high_base;
-  void *mem;
-#endif
-  sim_state_base base;
-};
-
 /* For compatibility, until all functions converted to passing
    SIM_DESC as an argument */
 extern SIM_DESC simulator;
-- 
2.31.1



More information about the Gdb-patches mailing list