Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simark@simark.ca>
To: Jan Dubiec <jdx@o2.pl>, gdb-patches@sourceware.org
Cc: Jeffrey Law <jeffrey.law@oss.qualcomm.com>,
	Andrew Burgess <aburgess@redhat.com>
Subject: Re: [PATCH v4] H8/300: sim: Fix simulator hang caused by qsort on Windows/MinGW
Date: Mon, 28 Sep 2026 10:00:12 -0400	[thread overview]
Message-ID: <189579af-6c0d-4643-a26c-338dc34fe8b6@simark.ca> (raw)
In-Reply-To: <20260910020110.493902-1-jdx@o2.pl>

On 9/9/26 9:58 PM, 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.
> 
> It also fixes an issue in the instruction decoder where, for example,
> mov.l @er7+,er1 is recognized as ldm.l @er7+,(er0-er1) if the opcode
> table is sorted �the wrong way�, i.e. when ldm appears before mov

The characters around "the wrong way" seem to suffer from bad encoding.

> @@ -1605,7 +1614,45 @@ 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 of the same length,
> +	     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;

I'd suggest using a

  SIM_ASSERT (p1->how != p2->how)

above, since we never want two entries to compare equal.

Otherwise, I don't have enought knowledge about this architecture to
really review the patch.

Simon

      parent reply	other threads:[~2026-09-28 14:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-10  1:58 Jan Dubiec
2026-09-27  8:55 ` Jan Dubiec
2026-09-28 14:00 ` Simon Marchi [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=189579af-6c0d-4643-a26c-338dc34fe8b6@simark.ca \
    --to=simark@simark.ca \
    --cc=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=jdx@o2.pl \
    --cc=jeffrey.law@oss.qualcomm.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