From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 7rb7Na1Ew2qMrjoAWB0awg (envelope-from ) for ; Mon, 05 Oct 2026 02:33:17 -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=s20261001 header.b=mrOFXE9h; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id BFD871E04E; Mon, 05 Oct 2026 02:33:17 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.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,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.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 D2E291E04E for ; Mon, 05 Oct 2026 02:33:15 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E8D114BA23F8 for ; Mon, 5 Oct 2026 06:33:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E8D114BA23F8 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=o2.pl header.i=@o2.pl header.a=rsa-sha256 header.s=s20261001 header.b=mrOFXE9h Received: from mx-out.tlen.pl (mx-out.tlen.pl [193.222.135.145]) by sourceware.org (Postfix) with ESMTPS id 606784BA23C7 for ; Mon, 5 Oct 2026 06:32:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 606784BA23C7 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 606784BA23C7 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=193.222.135.145 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1791181971; cv=none; b=wdif3mojT+jdOzTj/mw7smnCjUJht1Qn4s1hRUsJ8SDO0QJ/1/HDdaf62hgThMIKTLkUuZ1mKm2AraKp1F1m3BOJCXOgRqIvWpeHMskBirD/ermMWXcRzpFpcy7bNMW0QRirg9w5BnDw5tHmDBH1Syi+FSqaTf88kc6Hh+wEFzI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1791181971; c=relaxed/simple; bh=ewmMGUI0sW+k2T1zmOebAEHMwNArPC8WVeDXoY5VFEk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=JdUIIm8IX6XVQ0CIPMSJEQFE7O/y1RwsJ28i8GqgSGicTBG0gr2JgbC+zwYesgs7A09CWxs7nO2PTtROXmkMk+enwdvgV19b+mYoPq8LzY1p0gEX7YJV0zW7c5taIv1nZLaALQ/1EwJGRQXkBFRY8rQk3F79OTiXJnT9OoL2J0c= 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=s20261001 header.b=mrOFXE9h DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 606784BA23C7 Received: (wp-smtpd smtp.tlen.pl 19547 invoked from network); 5 Oct 2026 08:06:03 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=o2.pl; s=s20261001; t=1791180364; bh=VoJxFY9cMKt6QPaTMxRl5OG8TNNtWO0Fv68V15x1kTI=; h=From:To:Cc:Subject; b=mrOFXE9hZpuUvYrZSaQqZnZdsACvbC30WaM8x+msikAli/6y+TE/C4j1Uu0nVhuyS Iqjr6bMizoIpxD1o9nJ9FTaLUWYOm96uwfVEpTrysyTQDz83qkvAP7pVBNBpa6SpqW 9uo3uKiaANuO2QRJVZKKkjnhkDlfoTIaq4opvfdPw4dTR7acv5ze2qApdV9d28kJ/B zFEeH5k1ZgIEMIgDEDA/5Xidtt9dJStMPQNCL1GKpDJ5ab5fPPqjQ77zSBviQ+kgXP dWi4V57gxwpI15UZX2y1IX5CCmw2LiiDsDC+FjsasGiX/jjc21akBKWqyx/CN8owop zcMx+6ZuPVPVw== Received: from jdxpc (ip-94-42-230-167.multimo.pl [94.42.230.167]) (envelope-sender ) by smtp.tlen.pl (WP-SMTPD) with TLS_AES_256_GCM_SHA384 encrypted SMTP for ; 5 Oct 2026 08:06:03 +0200 From: Jan Dubiec To: gdb-patches@sourceware.org Cc: Jan Dubiec , Jeffrey Law , Andrew Burgess Subject: [REPOST][PATCH v4] H8/300: sim: Fix simulator hang caused by qsort on Windows/MinGW Date: Mon, 5 Oct 2026 08:05:19 +0200 Message-ID: <20261005060545.242055-1-jdx@o2.pl> X-Mailer: git-send-email 2.55.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-WP-MailID: 65a76915269f3fdaa0e1cda688469f09 X-WP-AV: skaner antywirusowy Poczty o2 X-WP-SPAM: NO 0000006 [wcXT] 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