* [PATCH v2] H8/300: sim: Fix simulator hang caused by qsort on Windows/MinGW
@ 2026-09-01 7:05 Jan Dubiec
2026-09-02 3:47 ` Jeff Law
0 siblings, 1 reply; 3+ messages in thread
From: Jan Dubiec @ 2026-09-01 7:05 UTC (permalink / raw)
To: gdb-patches; +Cc: Jan Dubiec, Jeffrey Law, Tom Tromey
On Windows, qsort() uses an unstable sorting algorithm, which results
in a "shuffled" opcode table rather than a properly sorted one, causing
the entire simulator to hang.
The simulator happens to work on Linux, but this behavior is not
guaranteed, because the glibc documentation clearly states that "If
two elements compare equal, their order after sorting is unpredictable."
This patch introduces two additional sort keys to the instruction
comparator function, making the resulting opcode table as close as
possible to the one that would be produced by a stable sorting algorithm.
---
sim/h8300/compile.c | 39 ++++++++++++++++++++++++++++++++++++++-
1 file changed, 38 insertions(+), 1 deletion(-)
diff --git a/sim/h8300/compile.c b/sim/h8300/compile.c
index 06988095228..3cca69730b0 100644
--- a/sim/h8300/compile.c
+++ b/sim/h8300/compile.c
@@ -1586,6 +1586,7 @@ instruction_comparator (const void *p1_, const void *p2_)
{
struct h8_opcode *p1 = (struct h8_opcode *)p1_;
struct h8_opcode *p2 = (struct h8_opcode *)p2_;
+ int cmp;
/* The 1st sort key is based on whether or not the
instruction is even available. This reduces the
@@ -1605,7 +1606,43 @@ instruction_comparator (const void *p1_, const void *p2_)
return p2_available - p1_available;
/* Secondarily sort based on the first opcode nibble. */
- return p1->data.nib[0] - p2->data.nib[0];
+ if (p1->data.nib[0] != p2->data.nib[0])
+ return p1->data.nib[0] - p2->data.nib[0];
+
+ /* The 3rd sort key */
+ cmp = strcmp(p1->name, p2->name);
+ if (cmp)
+ {
+ /* Two different opcodes */
+ size_t l1 = strlen(p1->name);
+ size_t l2 = strlen(p2->name);
+ ptrdiff_t i1 = strchr(p1->name, '.') - p1->name;
+ ptrdiff_t i2 = strchr(p2->name, '.') - p2->name;
+ char c1, c2;
+
+ if ((l1 == l2) && (i1 == i2) && (i1 > 0))
+ {
+ /* Check for different mnemonics, e.g. add.w vs. and.b */
+ cmp = strncmp(p1->name, p2->name, i1);
+ if (cmp)
+ return cmp;
+
+ /* At this point we expect only b, w or l suffix,
+ where b < w < l */
+ c1 = p1->name[i1+1];
+ c2 = p2->name[i2+1];
+ if (c1 == 'b')
+ return -1;
+ else if (c1 == 'w')
+ return (c2 == 'b') ? 1 : -1;
+ else
+ return 1;
+ }
+ return cmp;
+ }
+
+ /* The 4th sort key */
+ return p1->how - p2->how;
}
--
2.55.0
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2] H8/300: sim: Fix simulator hang caused by qsort on Windows/MinGW
2026-09-01 7:05 [PATCH v2] H8/300: sim: Fix simulator hang caused by qsort on Windows/MinGW Jan Dubiec
@ 2026-09-02 3:47 ` Jeff Law
2026-09-02 8:30 ` Jan Dubiec
0 siblings, 1 reply; 3+ messages in thread
From: Jeff Law @ 2026-09-02 3:47 UTC (permalink / raw)
To: Jan Dubiec, gdb-patches; +Cc: Tom Tromey
On 9/1/26 1:05 AM, Jan Dubiec wrote:
> On Windows, qsort() uses an unstable sorting algorithm, which results
> in a "shuffled" opcode table rather than a properly sorted one, causing
> the entire simulator to hang.
>
> The simulator happens to work on Linux, but this behavior is not
> guaranteed, because the glibc documentation clearly states that "If
> two elements compare equal, their order after sorting is unpredictable."
>
> This patch introduces two additional sort keys to the instruction
> comparator function, making the resulting opcode table as close as
> possible to the one that would be produced by a stable sorting algorithm.
Looks sensible to me (I believe the code in question is mine). I'm not
sure what privs I've got for the H8 in the sim repo these days. So wait
for other feedback rather than pushing immediately.
One class of pure formatting nit you should probably fix:
> +
> + /* The 3rd sort key */
> + cmp = strcmp(p1->name, p2->name);
GNU style guidelines require a space between the function name and the
open paren for the argument list. The same nit appears several times in
this patch (strlen, strchr, strncmp calls)
Jeff
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] H8/300: sim: Fix simulator hang caused by qsort on Windows/MinGW
2026-09-02 3:47 ` Jeff Law
@ 2026-09-02 8:30 ` Jan Dubiec
0 siblings, 0 replies; 3+ messages in thread
From: Jan Dubiec @ 2026-09-02 8:30 UTC (permalink / raw)
To: Jeff Law, gdb-patches; +Cc: Tom Tromey
On 2.09.2026 05:47, Jeff Law wrote:
[...]
> One class of pure formatting nit you should probably fix:
>
>
>> +
>> + /* The 3rd sort key */
>> + cmp = strcmp(p1->name, p2->name);
> GNU style guidelines require a space between the function name and the
> open paren for the argument list. The same nit appears several times in
> this patch (strlen, strchr, strncmp calls)
Version 3 has been posted in a new thread.
/J.D.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-09-02 8:31 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-01 7:05 [PATCH v2] H8/300: sim: Fix simulator hang caused by qsort on Windows/MinGW Jan Dubiec
2026-09-02 3:47 ` Jeff Law
2026-09-02 8:30 ` Jan Dubiec
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox