From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id KQsqIUbfl2p1ch4AWB0awg (envelope-from ) for ; Wed, 02 Sep 2026 04:33:10 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=o2.pl header.i=@o2.pl header.a=rsa-sha256 header.s=20241105 header.b=FICkLTxm; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 655781E166; Wed, 02 Sep 2026 04:33:10 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 505EB1E09B for ; Wed, 02 Sep 2026 04:33:09 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2D9664BA23ED for ; Wed, 2 Sep 2026 08:33:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2D9664BA23ED Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=o2.pl header.i=@o2.pl header.a=rsa-sha256 header.s=20241105 header.b=FICkLTxm Received: from mx-out.tlen.pl (mx-out.tlen.pl [193.222.135.148]) by sourceware.org (Postfix) with ESMTPS id 214554BA2E3F for ; Wed, 2 Sep 2026 08:32:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 214554BA2E3F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=o2.pl Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=o2.pl ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 214554BA2E3F Authentication-Results: sourceware.org; arc=none smtp.remote-ip=193.222.135.148 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788337955; cv=none; b=VCslMyWbr9RRl3t4M9RgwfnL2SLZWmOwjwFC1qT/fqXCzqf8VieCpXe7UwuzFq6Rs5Ey9J4li8hf9Oyxdnwa8NlMUFTQNeZI/2lR6zQA5FHzLlizVURXAXkuHzkJaSOQtW3OYiaFaWmmHOJfnkKXh0WdrulL+OxCrkSjwkwONQs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788337955; c=relaxed/simple; bh=EYLEKEIJrDGg/aRAdBujBuWotCJGY+q1TGMXPbHV09M=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=NCDTL47c9r1mpLRsHfQJx/+v6ABp9vWoLf49muQ0icQykEwE7+hShGOIarRFzLeX/UDUxd48MNPEhw4mmfILUvRnVOdYdqUVksbdcLksOOPhpElzwNkV0dq+YwZG3CH3XeCgpoOg6tuAojhTQql2QSbkef8h3P/KJWdPbCdKWBM= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=o2.pl header.i=@o2.pl header.a=rsa-sha256 header.s=20241105 header.b=FICkLTxm DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 214554BA2E3F Received: (wp-smtpd smtp.tlen.pl 40484 invoked from network); 2 Sep 2026 10:25:52 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=o2.pl; s=20241105; t=1788337552; bh=igRRQbK98wR4gs0clcF4B2HCK/StGfL9jJZWGxXb0HA=; h=From:To:Cc:Subject; b=FICkLTxm3e009OT4ozFUcHEjoALLYJ82R9ramXhl6jxT70XuDRAhO7CQCkgETbApE N3Pr5dfSTic2mDyO+J3Kz631trbZJtbTx1rsT+4/8AqtYNF2+dQQHjmFIhq87C0uq1 B9sI+AzopYLuQsJEhQquLu2BVX0yPt6BtnBbvKXynsm9NFX48pYJUeaORcyhjmjypo ecZYPqcitCo+NPfyaI4JwRSvGPj+NGn3KLzKaYUNCd9IVtUvqcvL2hG1YIFWcZJ2E9 aB3HjJRNOsOOK/zejGRkPOjETg/3Qe6vxsS862qkkfGj/3B91jz/4SELe3286W82+Q iUh5afpMcLhww== Received: from jdxpc (ip-94-42-229-65.multimo.pl [94.42.229.65]) (envelope-sender ) by smtp.tlen.pl (WP-SMTPD) with TLS_AES_256_GCM_SHA384 encrypted SMTP for ; 2 Sep 2026 10:25:52 +0200 From: Jan Dubiec To: gdb-patches@sourceware.org Cc: Jan Dubiec , Jeffrey Law , Tom Tromey Subject: [PATCH v3] H8/300: sim: Fix simulator hang caused by qsort on Windows/MinGW Date: Wed, 2 Sep 2026 10:24:50 +0200 Message-ID: <20260902082527.1684695-1-jdx@o2.pl> X-Mailer: git-send-email 2.55.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-WP-MailID: a7f261ef1eeec8d90459878ae28912c3 X-WP-AV: skaner antywirusowy Poczty o2 X-WP-SPAM: NO 0000005 [Iebc] X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 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 | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/sim/h8300/compile.c b/sim/h8300/compile.c index 06988095228..82ce73a70f2 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,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; } -- 2.55.0