[PATCH] H8/300: sim: Fix simulator hang caused by qsort on Windows/MinGW.

Jan Dubiec <[email protected]>
Newsgroups gmane.comp.gnu.binutils
Message-ID <[email protected]>
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 <[email protected]>
---
 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.