From: Jan Dubiec <jdx@o2.pl>
To: Tom Tromey <tom@tromey.com>
Cc: gdb-patches@sourceware.org, Jeffrey Law <jeffrey.law@oss.qualcomm.com>
Subject: Re: [PATCH] H8/300: sim: Fix simulator hang caused by qsort on Windows/MinGW.
Date: Sun, 30 Aug 2026 03:40:13 +0200 [thread overview]
Message-ID: <140cc872-432a-4b4d-85d6-c93ac879bafb@o2.pl> (raw)
In-Reply-To: <878q5rbngh.fsf@tromey.com>
[-- Attachment #1: Type: text/plain, Size: 1470 bytes --]
On 27.08.2026 17:19, Tom Tromey wrote:
>>>>>> "Jan" == Jan Dubiec <jdx@o2.pl> writes:
>
> Jan> So I think the best solution is to use a sorting algorithm known to be
> Jan> stable on every host, e.g. the insertion sort implementation from my
> Jan> previous message.
>
> Would it be possible to change the comparison function to be stable?
It is impossible by definition, because stability is a property of the
sorting algorithm, not the comparator. However, the comparator can be
modified in such a way that an unstable sorting algorithm produces a
“good enough” result.
That said, the following simple patch is sufficient:
/* 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 */
+ return strcmp(p1->name, p2->name);
However, I decided to take the longer route (see the attached patch) in
order to make the opcode table as close as possible to the one that
would be produced by a stable sorting algorithm.
> I think it would be somewhat nicer not to have a separate sort
> implementation.
>
> If that's too hard, though, I think your approach is fine.
It wasn't difficult, but I’m still inclined to favor a stable,
predictable sorting algorithm. That said, it’s not a big deal to me. Let
me know what you think about it, and then I’ll post a new version of the
patch.
/J.D.
[-- Attachment #2: instruction_comparator.patch --]
[-- Type: text/plain, Size: 1868 bytes --]
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;
}
prev parent reply other threads:[~2026-08-30 1:40 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-22 11:02 Jan Dubiec
2026-08-23 20:50 ` Jan Dubiec
2026-08-27 15:19 ` Tom Tromey
2026-08-30 1:40 ` Jan Dubiec [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=140cc872-432a-4b4d-85d6-c93ac879bafb@o2.pl \
--to=jdx@o2.pl \
--cc=gdb-patches@sourceware.org \
--cc=jeffrey.law@oss.qualcomm.com \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox