public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/3] [RFC] sim: framework for self-declaring initializers
@ 2021-02-08  4:08 Mike Frysinger
  2021-02-08  4:08 ` [PATCH 1/3] sim: add framework for declaring init callbacks locally Mike Frysinger
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Mike Frysinger @ 2021-02-08  4:08 UTC (permalink / raw)
  To: gdb-patches

GDB has a framework where modules can declare funcs with names that
start with "_initialize" and they'll automatically get called during
startup.  Bring that same framework idea to the sim so we can stop
hardcoding callbacks in a centralized location.

This is the rough basis for it with a few modules converted over as
an example.  I couldn't find much info in the GDB side to show that
this is the wrong direction to go.  Feedback welcome.

Mike Frysinger (3):
  sim: add framework for declaring init callbacks locally
  sim: dv-sockser: localize init callback
  sim: bfin: move option inits to respective modules

 sim/bfin/dv-bfin_mmu.c    | 12 ++++++++++-
 sim/bfin/interp.c         | 11 ----------
 sim/bfin/machs.c          | 12 ++++++++++-
 sim/common/Make-common.in | 24 +++++++++++++++++++++-
 sim/common/dv-sockser.c   |  5 ++++-
 sim/common/dv-sockser.h   |  2 --
 sim/common/sim-module.c   | 42 +++++++++++++++++++++++++--------------
 sim/common/sim-module.h   |  1 +
 8 files changed, 77 insertions(+), 32 deletions(-)

-- 
2.30.0


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

* [PATCH 1/3] sim: add framework for declaring init callbacks locally
  2021-02-08  4:08 [PATCH 0/3] [RFC] sim: framework for self-declaring initializers Mike Frysinger
@ 2021-02-08  4:08 ` Mike Frysinger
  2021-02-08  4:08 ` [PATCH 2/3] sim: dv-sockser: localize init callback Mike Frysinger
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Mike Frysinger @ 2021-02-08  4:08 UTC (permalink / raw)
  To: gdb-patches

To facilitate decentralized module initialization/registration with an
eye towards multi-target support, add a framework to detect init calls
declared in the source and automatically call them.  This is akin to
gdb's _initialize_xxx framework for letting modules autodiscover.
---
 sim/common/Make-common.in | 24 +++++++++++++++++++++++-
 sim/common/sim-module.c   | 38 +++++++++++++++++++++++++++-----------
 sim/common/sim-module.h   |  1 +
 3 files changed, 51 insertions(+), 12 deletions(-)

diff --git a/sim/common/Make-common.in b/sim/common/Make-common.in
index 99d55fb20383..4d757b1e42c5 100644
--- a/sim/common/Make-common.in
+++ b/sim/common/Make-common.in
@@ -251,7 +251,8 @@ LIBDEPS = $(BFD_LIB) $(OPCODES_LIB) $(LIBINTL_DEP) $(LIBIBERTY_LIB) \
 EXTRA_LIBS = $(BFD_LIB) $(OPCODES_LIB) $(LIBINTL) $(LIBIBERTY_LIB) \
 	$(CONFIG_LIBS) $(SIM_EXTRA_LIBS) $(LIBDL)
 
-LIB_OBJS = callback.o syscall.o targ-map.o version.o $(SIM_OBJS)
+LIB_OBJS = callback.o modules.o syscall.o targ-map.o version.o \
+	$(SIM_OBJS)
 
 RUNTESTFLAGS =
 
@@ -415,6 +416,7 @@ all_object_files = $(LIB_OBJS) $(SIM_RUN_OBJS)
 generated_files = \
 	$(SIM_EXTRA_DEPS) \
 	hw-config.h \
+	modules.c \
 	targ-map.c \
 	targ-vals.h \
 	version.c
@@ -452,6 +454,26 @@ test-hw-events: $(srccom)/hw-events.c libsim.a
 	$(CC) $(ALL_CFLAGS) -DMAIN -o test-hw-events$(EXEEXT) \
 		$(srccom)/hw-events.c libsim.a $(EXTRA_LIBS)
 
+# See sim_pre_argv_init and sim_module_install in sim-module.c for more details.
+modules.c: Makefile $(SIM_OBJS:.o=.c)
+	@echo Generating $@
+	@rm -f $@.l-tmp $@.tmp
+	@-LANG=C ; export LANG ; \
+	LC_ALL=C ; export LC_ALL ; \
+	sed -n -e '/^sim_install_/{s/^\(sim_install_[a-z_0-9A-Z]*\).*/\1/;p}' $^ >$@.l-tmp
+	@set -e; (\
+	echo '/* Do not modify this file.  */'; \
+	echo '/* It is created automatically by the Makefile.  */'; \
+	echo '#include "sim-module.h"'; \
+	sed -e 's:\(.*\):extern MODULE_INIT_FN \1;:' $@.l-tmp; \
+	echo 'MODULE_INSTALL_FN * const sim_modules_detected[] = {'; \
+	sed -e 's:\(.*\):  \1,:' $@.l-tmp; \
+	echo '  0,'; \
+	echo '};' \
+	) >$@.tmp
+	@rm -f $@.l-tmp
+	$(SHELL) $(srcroot)/move-if-change $@.tmp $@
+
 # CGEN support.
 
 # For use in Makefile.in for cpu-specific files.
diff --git a/sim/common/sim-module.c b/sim/common/sim-module.c
index ea436902439a..3330e4195e99 100644
--- a/sim/common/sim-module.c
+++ b/sim/common/sim-module.c
@@ -38,8 +38,9 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <stdlib.h>
 
-/* List of all modules.  */
-static MODULE_INSTALL_FN * const modules[] = {
+/* List of all early/core modules.
+   TODO: Should trim this list.  */
+static MODULE_INSTALL_FN * const early_modules[] = {
   standard_install,
   sim_events_install,
   sim_model_install,
@@ -65,6 +66,9 @@ static MODULE_INSTALL_FN * const modules[] = {
 #endif
   0
 };
+
+/* List of dynamicly detected modules.  Declared in generated sim-modules.c.  */
+extern MODULE_INSTALL_FN * const sim_modules_detected[];
 \f
 /* Functions called from sim_open.  */
 
@@ -92,11 +96,12 @@ sim_pre_argv_init (SIM_DESC sd, const char *myname)
 
   sim_config_default (sd);
 
-  /* Install all configured in modules.  */
+  /* Install all early configured in modules.  */
   if (sim_module_install (sd) != SIM_RC_OK)
     return SIM_RC_FAIL;
 
-  return SIM_RC_OK;
+  /* Install all remaining dynamically detected modules.  */
+  return sim_module_install_list (sd, sim_modules_detected);
 }
 
 /* Initialize common parts after argument processing.  */
@@ -121,18 +126,13 @@ sim_post_argv_init (SIM_DESC sd)
   return SIM_RC_OK;
 }
 \f
-/* Install all modules.
+/* Install a list of modules.
    If this fails, no modules are left installed.  */
-
 SIM_RC
-sim_module_install (SIM_DESC sd)
+sim_module_install_list (SIM_DESC sd, MODULE_INSTALL_FN * const *modules)
 {
   MODULE_INSTALL_FN * const *modp;
 
-  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
-  SIM_ASSERT (STATE_MODULES (sd) == NULL);
-
-  STATE_MODULES (sd) = ZALLOC (struct module_list);
   for (modp = modules; *modp != NULL; ++modp)
     {
       if ((*modp) (sd) != SIM_RC_OK)
@@ -142,9 +142,25 @@ sim_module_install (SIM_DESC sd)
 	  return SIM_RC_FAIL;
 	}
     }
+
   return SIM_RC_OK;
 }
 
+/* Install all modules.
+   If this fails, no modules are left installed.  */
+
+SIM_RC
+sim_module_install (SIM_DESC sd)
+{
+  MODULE_INSTALL_FN * const *modp;
+
+  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
+  SIM_ASSERT (STATE_MODULES (sd) == NULL);
+
+  STATE_MODULES (sd) = ZALLOC (struct module_list);
+  return sim_module_install_list (sd, early_modules);
+}
+
 /* Called after all modules have been installed and after argv
    has been processed.  */
 
diff --git a/sim/common/sim-module.h b/sim/common/sim-module.h
index 38c34c284682..80eb6c522a6c 100644
--- a/sim/common/sim-module.h
+++ b/sim/common/sim-module.h
@@ -73,6 +73,7 @@ typedef struct module_info_list {
 /* Functions to register module with various handler lists */
 
 SIM_RC sim_module_install (SIM_DESC);
+SIM_RC sim_module_install_list (SIM_DESC, MODULE_INSTALL_FN * const[]);
 void sim_module_uninstall (SIM_DESC);
 void sim_module_add_init_fn (SIM_DESC sd, MODULE_INIT_FN fn);
 void sim_module_add_resume_fn (SIM_DESC sd, MODULE_RESUME_FN fn);
-- 
2.30.0


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

* [PATCH 2/3] sim: dv-sockser: localize init callback
  2021-02-08  4:08 [PATCH 0/3] [RFC] sim: framework for self-declaring initializers Mike Frysinger
  2021-02-08  4:08 ` [PATCH 1/3] sim: add framework for declaring init callbacks locally Mike Frysinger
@ 2021-02-08  4:08 ` Mike Frysinger
  2021-02-08  4:08 ` [PATCH 3/3] sim: bfin: move option inits to respective modules Mike Frysinger
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Mike Frysinger @ 2021-02-08  4:08 UTC (permalink / raw)
  To: gdb-patches

Now that we don't need to hardcode the module init list in a single
place, move the dv-sockser logic to the place to the one file.
---
 sim/common/dv-sockser.c | 5 ++++-
 sim/common/dv-sockser.h | 2 --
 sim/common/sim-module.c | 4 ----
 3 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/sim/common/dv-sockser.c b/sim/common/dv-sockser.c
index fc552fa0acf0..73c473b15c01 100644
--- a/sim/common/dv-sockser.c
+++ b/sim/common/dv-sockser.c
@@ -222,8 +222,11 @@ dv_sockser_uninstall (SIM_DESC sd)
     }
 }
 
+/* Provide a prototype to silence -Wmissing-prototypes.  */
+extern MODULE_INIT_FN sim_install_dv_sockser;
+
 SIM_RC
-dv_sockser_install (SIM_DESC sd)
+sim_install_dv_sockser (SIM_DESC sd)
 {
   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
   if (sim_add_option_table (sd, NULL, sockser_options) != SIM_RC_OK)
diff --git a/sim/common/dv-sockser.h b/sim/common/dv-sockser.h
index c83ed5a9aa64..36ec0a970b15 100644
--- a/sim/common/dv-sockser.h
+++ b/sim/common/dv-sockser.h
@@ -34,8 +34,6 @@ int dv_sockser_write (SIM_DESC, unsigned char);
 int dv_sockser_write_buffer (SIM_DESC, const unsigned char *, unsigned);
 int dv_sockser_read (SIM_DESC);
 
-SIM_RC dv_sockser_install (SIM_DESC);
-
 #else
 
 /* If dv-sockser isn't available, provide stub functions.  */
diff --git a/sim/common/sim-module.c b/sim/common/sim-module.c
index 3330e4195e99..2dbd6a7982cf 100644
--- a/sim/common/sim-module.c
+++ b/sim/common/sim-module.c
@@ -59,10 +59,6 @@ static MODULE_INSTALL_FN * const early_modules[] = {
 #endif
 #if WITH_HW
   sim_hw_install,
-#endif
-#ifdef HAVE_DV_SOCKSER
-  /* TODO: Shouldn't have device models here.  */
-  dv_sockser_install,
 #endif
   0
 };
-- 
2.30.0


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

* [PATCH 3/3] sim: bfin: move option inits to respective modules
  2021-02-08  4:08 [PATCH 0/3] [RFC] sim: framework for self-declaring initializers Mike Frysinger
  2021-02-08  4:08 ` [PATCH 1/3] sim: add framework for declaring init callbacks locally Mike Frysinger
  2021-02-08  4:08 ` [PATCH 2/3] sim: dv-sockser: localize init callback Mike Frysinger
@ 2021-02-08  4:08 ` Mike Frysinger
  2021-02-08 19:45 ` [PATCH 0/3] [RFC] sim: framework for self-declaring initializers Tom Tromey
  2021-04-24 17:09 ` [PATCH 1/4] sim: arm: move build logic to source files Mike Frysinger
  4 siblings, 0 replies; 9+ messages in thread
From: Mike Frysinger @ 2021-02-08  4:08 UTC (permalink / raw)
  To: gdb-patches

Now that modules can self declare their own init funcs, change the mmu
and mach logic to use it.  We don't need to export the option symbols
or specifically call this logic from the sim_open function anymore.
---
 sim/bfin/dv-bfin_mmu.c | 12 +++++++++++-
 sim/bfin/interp.c      | 11 -----------
 sim/bfin/machs.c       | 12 +++++++++++-
 3 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/sim/bfin/dv-bfin_mmu.c b/sim/bfin/dv-bfin_mmu.c
index 5f28256fad61..cbaaaa2cae15 100644
--- a/sim/bfin/dv-bfin_mmu.c
+++ b/sim/bfin/dv-bfin_mmu.c
@@ -277,7 +277,7 @@ enum {
   OPTION_MMU_SKIP_TABLES = OPTION_START,
 };
 
-const OPTION bfin_mmu_options[] =
+static const OPTION bfin_mmu_options[] =
 {
   { {"mmu-skip-cplbs", no_argument, NULL, OPTION_MMU_SKIP_TABLES },
       '\0', NULL, "Skip parsing of CPLB tables (big speed increase)",
@@ -301,6 +301,16 @@ bfin_mmu_option_handler (SIM_DESC sd, sim_cpu *current_cpu, int opt,
       return SIM_RC_FAIL;
     }
 }
+
+/* Provide a prototype to silence -Wmissing-prototypes.  */
+extern MODULE_INIT_FN sim_install_bfin_mmu;
+
+SIM_RC
+sim_install_bfin_mmu (SIM_DESC sd)
+{
+  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
+  return sim_add_option_table (sd, NULL, bfin_mmu_options);
+}
 \f
 #define MMU_STATE(cpu) DV_STATE_CACHED (cpu, mmu)
 
diff --git a/sim/bfin/interp.c b/sim/bfin/interp.c
index ceb5939f4e6b..a77bcf708f45 100644
--- a/sim/bfin/interp.c
+++ b/sim/bfin/interp.c
@@ -739,17 +739,6 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback,
   if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT)
     STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT;
 
-  /* These options override any module options.
-     Obviously ambiguity should be avoided, however the caller may wish to
-     augment the meaning of an option.  */
-#define e_sim_add_option_table(sd, options) \
-  do { \
-    extern const OPTION options[]; \
-    sim_add_option_table (sd, NULL, options); \
-  } while (0)
-  e_sim_add_option_table (sd, bfin_mmu_options);
-  e_sim_add_option_table (sd, bfin_mach_options);
-
   /* The parser will print an error message for us, so we silently return.  */
   if (sim_parse_args (sd, argv) != SIM_RC_OK)
     {
diff --git a/sim/bfin/machs.c b/sim/bfin/machs.c
index 8b7b1060887b..a2060e8d8986 100644
--- a/sim/bfin/machs.c
+++ b/sim/bfin/machs.c
@@ -1976,7 +1976,7 @@ enum {
   OPTION_MACH_HW_BOARD_FILE,
 };
 
-const OPTION bfin_mach_options[] =
+static const OPTION bfin_mach_options[] =
 {
   { {"sirev", required_argument, NULL, OPTION_MACH_SIREV },
       '\0', "NUMBER", "Set CPU silicon revision",
@@ -2019,3 +2019,13 @@ bfin_mach_option_handler (SIM_DESC sd, sim_cpu *current_cpu, int opt,
       return SIM_RC_FAIL;
     }
 }
+
+/* Provide a prototype to silence -Wmissing-prototypes.  */
+extern MODULE_INIT_FN sim_install_bfin_mach;
+
+SIM_RC
+sim_install_bfin_mach (SIM_DESC sd)
+{
+  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
+  return sim_add_option_table (sd, NULL, bfin_mach_options);
+}
-- 
2.30.0


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

* Re: [PATCH 0/3] [RFC] sim: framework for self-declaring initializers
  2021-02-08  4:08 [PATCH 0/3] [RFC] sim: framework for self-declaring initializers Mike Frysinger
                   ` (2 preceding siblings ...)
  2021-02-08  4:08 ` [PATCH 3/3] sim: bfin: move option inits to respective modules Mike Frysinger
@ 2021-02-08 19:45 ` Tom Tromey
  2021-04-24 17:09 ` [PATCH 1/4] sim: arm: move build logic to source files Mike Frysinger
  4 siblings, 0 replies; 9+ messages in thread
From: Tom Tromey @ 2021-02-08 19:45 UTC (permalink / raw)
  To: Mike Frysinger via Gdb-patches

>>>>> "Mike" == Mike Frysinger via Gdb-patches <gdb-patches@sourceware.org> writes:

Mike> This is the rough basis for it with a few modules converted over as
Mike> an example.  I couldn't find much info in the GDB side to show that
Mike> this is the wrong direction to go.  Feedback welcome.

The main issue on the gdb side has been that, very rarely, an ordering
dependency has crept in.  The most recent one I recall is that I ran
across an internal test case at AdaCore where the results were dependent
on the order in which callbacks were added to an observer.  As a result
of this, in gdb a few things are explicitly initialized before the
generic initializations are done.  This also explains an obscure hack in
the CLI command registration code, namely why the various *list
variables are globals.

Aside from that, IMO this system has been fine.

Tom

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

* [PATCH 1/4] sim: arm: move build logic to source files
  2021-02-08  4:08 [PATCH 0/3] [RFC] sim: framework for self-declaring initializers Mike Frysinger
                   ` (3 preceding siblings ...)
  2021-02-08 19:45 ` [PATCH 0/3] [RFC] sim: framework for self-declaring initializers Tom Tromey
@ 2021-04-24 17:09 ` Mike Frysinger
  2021-04-24 17:09   ` [PATCH 2/4] sim: add framework for declaring init callbacks locally Mike Frysinger
                     ` (2 more replies)
  4 siblings, 3 replies; 9+ messages in thread
From: Mike Frysinger @ 2021-04-24 17:09 UTC (permalink / raw)
  To: gdb-patches

This simplifies the build logic a bit by just having source file
inputs.  It also simplifies code that assumes there's a source
file for each object.
---
 sim/arm/ChangeLog   |  6 ++++++
 sim/arm/Makefile.in | 10 +---------
 sim/arm/armemu32.c  | 18 ++++++++++++++++++
 3 files changed, 25 insertions(+), 9 deletions(-)
 create mode 100644 sim/arm/armemu32.c

diff --git a/sim/arm/ChangeLog b/sim/arm/ChangeLog
index aa3c91bb2c47..fa03a9b37bcf 100644
--- a/sim/arm/ChangeLog
+++ b/sim/arm/ChangeLog
@@ -1,3 +1,9 @@
+2021-04-24  Mike Frysinger  <vapier@gentoo.org>
+
+	* Makefile.in (SIM_OBJS): Change armemu26.o to armemu.o.
+	(armemu26.o, armemu32.o): Delete targets.
+	* armemu32.c: New file.
+
 2021-04-22  Tom Tromey  <tom@tromey.com>
 
 	* configure, config.in: Rebuild.
diff --git a/sim/arm/Makefile.in b/sim/arm/Makefile.in
index 29166952ce16..f339e799b9ef 100644
--- a/sim/arm/Makefile.in
+++ b/sim/arm/Makefile.in
@@ -23,16 +23,8 @@ SIM_EXTRA_LIBS = -lm
 SIM_OBJS = \
 	wrapper.o \
 	$(SIM_NEW_COMMON_OBJS) \
-	armemu26.o armemu32.o arminit.o armos.o armsupp.o \
+	armemu.o armemu32.o arminit.o armos.o armsupp.o \
 	armvirt.o thumbemu.o \
 	armcopro.o maverick.o iwmmxt.o
 
 ## COMMON_POST_CONFIG_FRAG
-
-armemu26.o: armemu.c
-	$(COMPILE) $(srcdir)/armemu.c
-	$(POSTCOMPILE)
-
-armemu32.o: armemu.c
-	$(COMPILE) -DMODE32 $(srcdir)/armemu.c
-	$(POSTCOMPILE)
diff --git a/sim/arm/armemu32.c b/sim/arm/armemu32.c
new file mode 100644
index 000000000000..adf0bd9c0b56
--- /dev/null
+++ b/sim/arm/armemu32.c
@@ -0,0 +1,18 @@
+/* Build armemu.c with ARM32 support.
+   Copyright (C) 1995-2021 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 3 of the License, 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, see <https://www.gnu.org/licenses/>.  */
+
+#define MODE32
+#include "armemu.c"
-- 
2.30.2


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

* [PATCH 2/4] sim: add framework for declaring init callbacks locally
  2021-04-24 17:09 ` [PATCH 1/4] sim: arm: move build logic to source files Mike Frysinger
@ 2021-04-24 17:09   ` Mike Frysinger
  2021-04-24 17:09   ` [PATCH 3/4] sim: dv-sockser: localize init callback Mike Frysinger
  2021-04-24 17:09   ` [PATCH 4/4] sim: bfin: move option inits to respective modules Mike Frysinger
  2 siblings, 0 replies; 9+ messages in thread
From: Mike Frysinger @ 2021-04-24 17:09 UTC (permalink / raw)
  To: gdb-patches

To facilitate decentralized module initialization/registration with an
eye towards multi-target support, add a framework to detect init calls
declared in the source and automatically call them.  This is akin to
gdb's _initialize_xxx framework for letting modules autodiscover.
---
 sim/common/ChangeLog      | 14 +++++++++++
 sim/common/Make-common.in | 24 +++++++++++++++++-
 sim/common/sim-module.c   | 51 +++++++++++++++++++++++++++------------
 sim/common/sim-module.h   |  1 +
 4 files changed, 74 insertions(+), 16 deletions(-)

diff --git a/sim/common/ChangeLog b/sim/common/ChangeLog
index 516e214f0112..7ccaabf1cb11 100644
--- a/sim/common/ChangeLog
+++ b/sim/common/ChangeLog
@@ -1,3 +1,17 @@
+2021-04-24  Mike Frysinger  <vapier@gentoo.org>
+
+	* Make-common.in (LIB_OBJS): Add modules.o.
+	(generated_files): Add modules.c.
+	(modules.c): New target.
+	* sim-module.c (modules): Rename to ...
+	(early_modules): ... this.  Delete 0 sentinel.
+	(early_modules_len): Define.
+	(sim_modules_detected, sim_modules_detected_len): Declare.
+	(sim_pre_argv_init): Call sim_module_install_list.
+	(sim_module_install): New function.
+	(sim_module_install_list): New function.
+	* sim-module.h (sim_module_install_list): Declare.
+
 2021-04-24  Mike Frysinger  <vapier@gentoo.org>
 
 	* dv-cfi.c (attach_cfi_regs): Change %u to PRIiTC.
diff --git a/sim/common/Make-common.in b/sim/common/Make-common.in
index c73801443049..0f8774905098 100644
--- a/sim/common/Make-common.in
+++ b/sim/common/Make-common.in
@@ -250,7 +250,8 @@ LIBDEPS = $(BFD_LIB) $(OPCODES_LIB) $(LIBINTL_DEP) $(LIBIBERTY_LIB)
 EXTRA_LIBS = $(BFD_LIB) $(OPCODES_LIB) $(LIBINTL) $(LIBIBERTY_LIB) \
 	$(CONFIG_LIBS) $(SIM_EXTRA_LIBS) $(LIBDL)
 
-LIB_OBJS = callback.o syscall.o targ-map.o version.o $(SIM_OBJS)
+LIB_OBJS = callback.o modules.o syscall.o targ-map.o version.o \
+	$(SIM_OBJS)
 
 COMPILE_FOR_BUILD = $(CC_FOR_BUILD) $(BUILD_CFLAGS)
 LINK_FOR_BUILD = $(CC_FOR_BUILD) $(BUILD_CFLAGS) $(LDFLAGS_FOR_BUILD) -o $@
@@ -420,6 +421,7 @@ all_object_files = $(LIB_OBJS) $(SIM_RUN_OBJS)
 generated_files = \
 	$(SIM_EXTRA_DEPS) \
 	hw-config.h \
+	modules.c \
 	targ-map.c \
 	targ-vals.h \
 	version.c
@@ -459,6 +461,26 @@ test-hw-events: $(srccom)/hw-events.c libsim.a
 	$(CC) $(ALL_CFLAGS) -DMAIN -o test-hw-events$(EXEEXT) \
 		$(srccom)/hw-events.c libsim.a $(EXTRA_LIBS)
 
+# See sim_pre_argv_init and sim_module_install in sim-module.c for more details.
+modules.c: Makefile $(SIM_OBJS:.o=.c)
+	@echo Generating $@
+	@LANG=C ; export LANG ; \
+	LC_ALL=C ; export LC_ALL ; \
+	sed -n -e '/^sim_install_/{s/^\(sim_install_[a-z_0-9A-Z]*\).*/\1/;p}' $^ | sort >$@.l-tmp
+	@set -e; (\
+	echo '/* Do not modify this file.  */'; \
+	echo '/* It is created automatically by the Makefile.  */'; \
+	echo '#include "libiberty.h"'; \
+	echo '#include "sim-module.h"'; \
+	sed -e 's:\(.*\):extern __attribute__((__weak__)) MODULE_INIT_FN \1;:' $@.l-tmp; \
+	echo 'MODULE_INSTALL_FN * const sim_modules_detected[] = {'; \
+	sed -e 's:\(.*\):  \1,:' $@.l-tmp; \
+	echo '};'; \
+	echo 'const int sim_modules_detected_len = ARRAY_SIZE (sim_modules_detected);'; \
+	) >$@.tmp
+	$(SHELL) $(srcroot)/move-if-change $@.tmp $@
+	@rm -f $@.l-tmp $@.tmp
+
 # CGEN support.
 
 # For use in Makefile.in for cpu-specific files.
diff --git a/sim/common/sim-module.c b/sim/common/sim-module.c
index ea436902439a..e508826922f0 100644
--- a/sim/common/sim-module.c
+++ b/sim/common/sim-module.c
@@ -38,8 +38,9 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <stdlib.h>
 
-/* List of all modules.  */
-static MODULE_INSTALL_FN * const modules[] = {
+/* List of all early/core modules.
+   TODO: Should trim this list by converting to sim_install_* framework.  */
+static MODULE_INSTALL_FN * const early_modules[] = {
   standard_install,
   sim_events_install,
   sim_model_install,
@@ -63,8 +64,12 @@ static MODULE_INSTALL_FN * const modules[] = {
   /* TODO: Shouldn't have device models here.  */
   dv_sockser_install,
 #endif
-  0
 };
+static int early_modules_len = ARRAY_SIZE (early_modules);
+
+/* List of dynamically detected modules.  Declared in generated modules.c.  */
+extern MODULE_INSTALL_FN * const sim_modules_detected[];
+extern const int sim_modules_detected_len;
 \f
 /* Functions called from sim_open.  */
 
@@ -92,11 +97,13 @@ sim_pre_argv_init (SIM_DESC sd, const char *myname)
 
   sim_config_default (sd);
 
-  /* Install all configured in modules.  */
+  /* Install all early configured-in modules.  */
   if (sim_module_install (sd) != SIM_RC_OK)
     return SIM_RC_FAIL;
 
-  return SIM_RC_OK;
+  /* Install all remaining dynamically detected modules.  */
+  return sim_module_install_list (sd, sim_modules_detected,
+				  sim_modules_detected_len);
 }
 
 /* Initialize common parts after argument processing.  */
@@ -121,30 +128,44 @@ sim_post_argv_init (SIM_DESC sd)
   return SIM_RC_OK;
 }
 \f
-/* Install all modules.
+/* Install a list of modules.
    If this fails, no modules are left installed.  */
-
 SIM_RC
-sim_module_install (SIM_DESC sd)
+sim_module_install_list (SIM_DESC sd, MODULE_INSTALL_FN * const *modules,
+			 size_t modules_len)
 {
-  MODULE_INSTALL_FN * const *modp;
+  size_t i;
 
-  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
-  SIM_ASSERT (STATE_MODULES (sd) == NULL);
-
-  STATE_MODULES (sd) = ZALLOC (struct module_list);
-  for (modp = modules; *modp != NULL; ++modp)
+  for (i = 0; i < modules_len; ++i)
     {
-      if ((*modp) (sd) != SIM_RC_OK)
+      MODULE_INSTALL_FN *modp = modules[i];
+
+      if (modp (sd) != SIM_RC_OK)
 	{
 	  sim_module_uninstall (sd);
 	  SIM_ASSERT (STATE_MODULES (sd) == NULL);
 	  return SIM_RC_FAIL;
 	}
     }
+
   return SIM_RC_OK;
 }
 
+/* Install all modules.
+   If this fails, no modules are left installed.  */
+
+SIM_RC
+sim_module_install (SIM_DESC sd)
+{
+  MODULE_INSTALL_FN * const *modp;
+
+  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
+  SIM_ASSERT (STATE_MODULES (sd) == NULL);
+
+  STATE_MODULES (sd) = ZALLOC (struct module_list);
+  return sim_module_install_list (sd, early_modules, early_modules_len);
+}
+
 /* Called after all modules have been installed and after argv
    has been processed.  */
 
diff --git a/sim/common/sim-module.h b/sim/common/sim-module.h
index 38c34c284682..dad557194318 100644
--- a/sim/common/sim-module.h
+++ b/sim/common/sim-module.h
@@ -73,6 +73,7 @@ typedef struct module_info_list {
 /* Functions to register module with various handler lists */
 
 SIM_RC sim_module_install (SIM_DESC);
+SIM_RC sim_module_install_list (SIM_DESC, MODULE_INSTALL_FN * const[], size_t);
 void sim_module_uninstall (SIM_DESC);
 void sim_module_add_init_fn (SIM_DESC sd, MODULE_INIT_FN fn);
 void sim_module_add_resume_fn (SIM_DESC sd, MODULE_RESUME_FN fn);
-- 
2.30.2


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

* [PATCH 3/4] sim: dv-sockser: localize init callback
  2021-04-24 17:09 ` [PATCH 1/4] sim: arm: move build logic to source files Mike Frysinger
  2021-04-24 17:09   ` [PATCH 2/4] sim: add framework for declaring init callbacks locally Mike Frysinger
@ 2021-04-24 17:09   ` Mike Frysinger
  2021-04-24 17:09   ` [PATCH 4/4] sim: bfin: move option inits to respective modules Mike Frysinger
  2 siblings, 0 replies; 9+ messages in thread
From: Mike Frysinger @ 2021-04-24 17:09 UTC (permalink / raw)
  To: gdb-patches

Now that we don't need to hardcode the module init list in a single
place, move the dv-sockser logic to the place to the one file.
---
 sim/common/dv-sockser.c | 5 ++++-
 sim/common/dv-sockser.h | 2 --
 sim/common/sim-module.c | 4 ----
 3 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/sim/common/dv-sockser.c b/sim/common/dv-sockser.c
index fc552fa0acf0..73c473b15c01 100644
--- a/sim/common/dv-sockser.c
+++ b/sim/common/dv-sockser.c
@@ -222,8 +222,11 @@ dv_sockser_uninstall (SIM_DESC sd)
     }
 }
 
+/* Provide a prototype to silence -Wmissing-prototypes.  */
+extern MODULE_INIT_FN sim_install_dv_sockser;
+
 SIM_RC
-dv_sockser_install (SIM_DESC sd)
+sim_install_dv_sockser (SIM_DESC sd)
 {
   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
   if (sim_add_option_table (sd, NULL, sockser_options) != SIM_RC_OK)
diff --git a/sim/common/dv-sockser.h b/sim/common/dv-sockser.h
index c83ed5a9aa64..36ec0a970b15 100644
--- a/sim/common/dv-sockser.h
+++ b/sim/common/dv-sockser.h
@@ -34,8 +34,6 @@ int dv_sockser_write (SIM_DESC, unsigned char);
 int dv_sockser_write_buffer (SIM_DESC, const unsigned char *, unsigned);
 int dv_sockser_read (SIM_DESC);
 
-SIM_RC dv_sockser_install (SIM_DESC);
-
 #else
 
 /* If dv-sockser isn't available, provide stub functions.  */
diff --git a/sim/common/sim-module.c b/sim/common/sim-module.c
index e508826922f0..efbe81696c38 100644
--- a/sim/common/sim-module.c
+++ b/sim/common/sim-module.c
@@ -60,10 +60,6 @@ static MODULE_INSTALL_FN * const early_modules[] = {
 #if WITH_HW
   sim_hw_install,
 #endif
-#ifdef HAVE_DV_SOCKSER
-  /* TODO: Shouldn't have device models here.  */
-  dv_sockser_install,
-#endif
 };
 static int early_modules_len = ARRAY_SIZE (early_modules);
 
-- 
2.30.2


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

* [PATCH 4/4] sim: bfin: move option inits to respective modules
  2021-04-24 17:09 ` [PATCH 1/4] sim: arm: move build logic to source files Mike Frysinger
  2021-04-24 17:09   ` [PATCH 2/4] sim: add framework for declaring init callbacks locally Mike Frysinger
  2021-04-24 17:09   ` [PATCH 3/4] sim: dv-sockser: localize init callback Mike Frysinger
@ 2021-04-24 17:09   ` Mike Frysinger
  2 siblings, 0 replies; 9+ messages in thread
From: Mike Frysinger @ 2021-04-24 17:09 UTC (permalink / raw)
  To: gdb-patches

Now that modules can self declare their own init funcs, change the mmu
and mach logic to use it.  We don't need to export the option symbols
or specifically call this logic from the sim_open function anymore.
---
 sim/bfin/dv-bfin_mmu.c | 12 +++++++++++-
 sim/bfin/interp.c      | 11 -----------
 sim/bfin/machs.c       | 12 +++++++++++-
 3 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/sim/bfin/dv-bfin_mmu.c b/sim/bfin/dv-bfin_mmu.c
index 5f28256fad61..cbaaaa2cae15 100644
--- a/sim/bfin/dv-bfin_mmu.c
+++ b/sim/bfin/dv-bfin_mmu.c
@@ -277,7 +277,7 @@ enum {
   OPTION_MMU_SKIP_TABLES = OPTION_START,
 };
 
-const OPTION bfin_mmu_options[] =
+static const OPTION bfin_mmu_options[] =
 {
   { {"mmu-skip-cplbs", no_argument, NULL, OPTION_MMU_SKIP_TABLES },
       '\0', NULL, "Skip parsing of CPLB tables (big speed increase)",
@@ -301,6 +301,16 @@ bfin_mmu_option_handler (SIM_DESC sd, sim_cpu *current_cpu, int opt,
       return SIM_RC_FAIL;
     }
 }
+
+/* Provide a prototype to silence -Wmissing-prototypes.  */
+extern MODULE_INIT_FN sim_install_bfin_mmu;
+
+SIM_RC
+sim_install_bfin_mmu (SIM_DESC sd)
+{
+  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
+  return sim_add_option_table (sd, NULL, bfin_mmu_options);
+}
 \f
 #define MMU_STATE(cpu) DV_STATE_CACHED (cpu, mmu)
 
diff --git a/sim/bfin/interp.c b/sim/bfin/interp.c
index 49388e74c54c..2aa9279aef89 100644
--- a/sim/bfin/interp.c
+++ b/sim/bfin/interp.c
@@ -735,17 +735,6 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback,
   if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT)
     STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT;
 
-  /* These options override any module options.
-     Obviously ambiguity should be avoided, however the caller may wish to
-     augment the meaning of an option.  */
-#define e_sim_add_option_table(sd, options) \
-  do { \
-    extern const OPTION options[]; \
-    sim_add_option_table (sd, NULL, options); \
-  } while (0)
-  e_sim_add_option_table (sd, bfin_mmu_options);
-  e_sim_add_option_table (sd, bfin_mach_options);
-
   /* The parser will print an error message for us, so we silently return.  */
   if (sim_parse_args (sd, argv) != SIM_RC_OK)
     {
diff --git a/sim/bfin/machs.c b/sim/bfin/machs.c
index 8b7b1060887b..a2060e8d8986 100644
--- a/sim/bfin/machs.c
+++ b/sim/bfin/machs.c
@@ -1976,7 +1976,7 @@ enum {
   OPTION_MACH_HW_BOARD_FILE,
 };
 
-const OPTION bfin_mach_options[] =
+static const OPTION bfin_mach_options[] =
 {
   { {"sirev", required_argument, NULL, OPTION_MACH_SIREV },
       '\0', "NUMBER", "Set CPU silicon revision",
@@ -2019,3 +2019,13 @@ bfin_mach_option_handler (SIM_DESC sd, sim_cpu *current_cpu, int opt,
       return SIM_RC_FAIL;
     }
 }
+
+/* Provide a prototype to silence -Wmissing-prototypes.  */
+extern MODULE_INIT_FN sim_install_bfin_mach;
+
+SIM_RC
+sim_install_bfin_mach (SIM_DESC sd)
+{
+  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
+  return sim_add_option_table (sd, NULL, bfin_mach_options);
+}
-- 
2.30.2


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

end of thread, other threads:[~2021-04-24 17:09 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-08  4:08 [PATCH 0/3] [RFC] sim: framework for self-declaring initializers Mike Frysinger
2021-02-08  4:08 ` [PATCH 1/3] sim: add framework for declaring init callbacks locally Mike Frysinger
2021-02-08  4:08 ` [PATCH 2/3] sim: dv-sockser: localize init callback Mike Frysinger
2021-02-08  4:08 ` [PATCH 3/3] sim: bfin: move option inits to respective modules Mike Frysinger
2021-02-08 19:45 ` [PATCH 0/3] [RFC] sim: framework for self-declaring initializers Tom Tromey
2021-04-24 17:09 ` [PATCH 1/4] sim: arm: move build logic to source files Mike Frysinger
2021-04-24 17:09   ` [PATCH 2/4] sim: add framework for declaring init callbacks locally Mike Frysinger
2021-04-24 17:09   ` [PATCH 3/4] sim: dv-sockser: localize init callback Mike Frysinger
2021-04-24 17:09   ` [PATCH 4/4] sim: bfin: move option inits to respective modules 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).