Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] H8/300: sim: Fix simulator hang caused by qsort on Windows/MinGW.
@ 2026-08-22 11:02 Jan Dubiec
  2026-08-23 20:50 ` Jan Dubiec
  0 siblings, 1 reply; 4+ messages in thread
From: Jan Dubiec @ 2026-08-22 11:02 UTC (permalink / raw)
  To: gdb-patches; +Cc: Jan Dubiec, Jeffrey Law

See the comment below. I don't know why qsort behaves so strangely. It
could be a bug, or simply a consequence of its being an unstable sort.
Unfortunately, I don't have time to investigate the root cause.

Signed-off-by: Jan Dubiec <jdx@o2.pl>
---
 sim/h8300/compile.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/sim/h8300/compile.c b/sim/h8300/compile.c
index 06988095228..c08f7704ef3 100644
--- a/sim/h8300/compile.c
+++ b/sim/h8300/compile.c
@@ -1568,6 +1568,41 @@ store2 (SIM_DESC sd, ea_type *arg, int n)
   return store_1 (sd, arg, n, 1);
 }
 
+#ifdef __MINGW32__
+/* On Windows/MinGW, qsort for some reason produces a "shuffled" opcode
+table instead of a sorted one, causing the entire simulator to hang.
+
+This is a simple insertion sort implementation, but it has virtually
+no impact on simulator performance.  */
+
+#define qsort opcode_insertion_sort
+
+static void
+opcode_insertion_sort(void *base, size_t nmemb, size_t size,
+                      int (*compar)(const void *, const void *))
+{
+  unsigned char *a = base;
+  struct h8_opcode tmp;
+  size_t i, j;
+
+  if (nmemb < 2 || size == 0)
+    return;
+
+  for (i = 1; i < nmemb; ++i) {
+    memcpy(&tmp, a + i * size, size);
+
+    j = i;
+    while (j > 0 && compar(&tmp, a + (j - 1) * size) < 0) {
+      memcpy(a + j * size, a + (j - 1) * size, size);
+      --j;
+    }
+
+    memcpy(a + j * size, &tmp, size);
+  }
+
+}
+#endif /* #ifdef __MINGW32__ */
+
 /* Callback for qsort.  We sort first based on availability
    (available instructions sort lower).  When availability state
    is the same, then we use the first 4 bit nibble as a secondary
-- 
2.55.0

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

end of thread, other threads:[~2026-08-30  1:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-22 11:02 [PATCH] H8/300: sim: Fix simulator hang caused by qsort on Windows/MinGW Jan Dubiec
2026-08-23 20:50 ` Jan Dubiec
2026-08-27 15:19   ` Tom Tromey
2026-08-30  1:40     ` Jan Dubiec

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox