From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id G91aD1QRomqQcDsAWB0awg (envelope-from ) for ; Wed, 09 Sep 2026 22:09:24 -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=nJtN+LRF; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 29F021E033; Wed, 09 Sep 2026 22:09:24 -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 8AEF41E033 for ; Wed, 09 Sep 2026 22:09:15 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ED41A4BB58E1 for ; Thu, 10 Sep 2026 02:09:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ED41A4BB58E1 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=nJtN+LRF Received: from mx-out.tlen.pl (mx-out.tlen.pl [193.222.135.148]) by sourceware.org (Postfix) with ESMTPS id 619CC4BA2E04 for ; Thu, 10 Sep 2026 02:08:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 619CC4BA2E04 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 619CC4BA2E04 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=1789006129; cv=none; b=oyqMkxRGVhAjEVbriLXj5OKnJnInJgYLPTsa7tKN+ul6j/+xt9VaDfNw8kW58WyQdt6mdr5RhmvVzYWYsYvCD0c37FKdnxCMUkXY/Js4nb4cgUnbscqILGeLm6AGixPH3RIt/ZhqiKVlrupdjVYaCt788rttNnnjyXbMzcHwEe0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1789006129; c=relaxed/simple; bh=0VkKu7yHJWxAVdCXZcbT0JG1wFoIJOAJ1W4tHhDyRk4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=P0FrpBMMe9BZQIv4olg0myOe4KjJVj696gEkjCNmJ3yWKRC8C/bXE9xm7gMIYvtanHUC7tw+3sGHThakIkC92FlmHYAlMlqwHbGJkVQrG3bmS/BzSmaOFLyO3LubR8vYvzei9r2UrpEblT1It2LpdrCwXASfXMTIbEYYM3I/VfY= 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=nJtN+LRF DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 619CC4BA2E04 Received: (wp-smtpd smtp.tlen.pl 9890 invoked from network); 10 Sep 2026 04:02:07 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=o2.pl; s=20241105; t=1789005727; bh=o/wPyFeZDXfKatMg7EGfGWL3J5iW9wuLr5MpN9EAqMA=; h=From:To:Cc:Subject; b=nJtN+LRFu3Qzv6A+inEwVJ45ESVBRZCDgf5BGmv0STKwY1Tm1s/vsv7YhVQucW/Dn GA4IWIzRJMLolns44HKNy325dS9Ld47/GKdg5FcxHLrZtDV1REgXy3sE3A13FXHXvW 5YGwSWP27fWTDPYEjL0TZHuCtvzXHsO9Jn5Ncyno+eSqr5UBP9dELjB7Mw7FPsO0VT EjVt3iqelOcd9j2BbB8UnVciLXfEgakez/flFkKFmr5jhpHAua5FBvIpJgAb/CH6je LLE50OFolFij5NrytSjK5G1tjXNHtY3BmXTl65jDv+i120zRaGaS3/OgvJilJlhC1t 2ZVUNlUCHzRHQ== Received: from jdxpc (ip-94-42-230-142.multimo.pl [94.42.230.142]) (envelope-sender ) by smtp.tlen.pl (WP-SMTPD) with TLS_AES_256_GCM_SHA384 encrypted SMTP for ; 10 Sep 2026 04:02:07 +0200 From: Jan Dubiec To: gdb-patches@sourceware.org Cc: Jan Dubiec , Jeffrey Law , Andrew Burgess Subject: [PATCH v4] H8/300: sim: Fix simulator hang caused by qsort on Windows/MinGW Date: Thu, 10 Sep 2026 03:58:13 +0200 Message-ID: <20260910020110.493902-1-jdx@o2.pl> X-Mailer: git-send-email 2.55.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-WP-MailID: ae7b88d40f8f0fe6d775b063a0363591 X-WP-AV: skaner antywirusowy Poczty o2 X-WP-SPAM: NO 0000005 [ocbi] 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. 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 in the table. No regressions were found. --- sim/h8300/compile.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/sim/h8300/compile.c b/sim/h8300/compile.c index 06988095228..0feb58fe183 100644 --- a/sim/h8300/compile.c +++ b/sim/h8300/compile.c @@ -509,7 +509,15 @@ decode (SIM_DESC sd, sim_cpu *cpu, int addr, unsigned char *data, decoded_inst * } else if ((looking_for & MODE) == DATA) { - ; /* Skip embedded data. */ + /* Special case for ldm.l and stm.l. Otherwise, if the + h8_opcodes[] table is sorted in a particular order, + the instruction decoder may incorrectly recognize + e.g. mov.l @er7+,er1 as ldm.l @er7+,(er0-er1). */ + if (((OP_KIND (q->how) == O_LDM) || + (OP_KIND (q->how) == O_STM)) && + ((thisnib < 1) || (thisnib > 3))) + goto fail; + /* Skip embedded data for the rest. */ } else if ((looking_for & MODE) == DBIT) { @@ -1586,6 +1594,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 +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; } -- 2.55.0