From patchwork Sun Jan 7 04:45:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Frysinger X-Patchwork-Id: 83475 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C91473858403 for ; Sun, 7 Jan 2024 04:46:19 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp.gentoo.org (mail.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) by sourceware.org (Postfix) with ESMTP id BD76E3858D20 for ; Sun, 7 Jan 2024 04:45:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BD76E3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gentoo.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gentoo.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BD76E3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:470:ea4a:1:5054:ff:fec7:86e4 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704602760; cv=none; b=hJfAtA9orZLzAHVOwu4wOUstg0LRl+fLWqE/xo0d13IaTKIVe6EmrZN32MTIFVqXJP4UuoKK5O5gH2RmlL4y2O4OBgqfwe+f5FVcve17vTAPet6dPVmgCe+/7ug7WAr4k+DBvrn5uWQ7t6ot2kLFwIAB3m4iTNolY4p3L8ZJioE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704602760; c=relaxed/simple; bh=GBB3gh+BZkS44b8g5f58e52venkVxKOumnSxs6tSJ8w=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=lYLxrqx7S/pjnLd8HK0WONSHphHwj/kZFvUTg+q/JUWOgvhnF1P/2KzM4Nx97GHCS3B1FCJ3WG7nrA8Jbg4D4VGzAVCbiCV399hJ2FSRLbUiAuw9FFmoO8FDhcQ2ZmYp73j0nszNPplDxPCEyD4YKlg8g8TgUhIYRvIDMiiAye0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by smtp.gentoo.org (Postfix, from userid 559) id 09AC3343247; Sun, 7 Jan 2024 04:45:58 +0000 (UTC) From: Mike Frysinger To: gdb-patches@sourceware.org Subject: [PATCH] sim: cgen: rework DI macros to avoid signed left shifts Date: Sat, 6 Jan 2024 23:45:56 -0500 Message-ID: <20240107044556.4626-1-vapier@gentoo.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231224082639.18038-1-vapier@gentoo.org> References: <20231224082639.18038-1-vapier@gentoo.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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+patchwork=sourceware.org@sourceware.org The cgen code uses DI as int64_t and UDI as uint64_t. The DI macros are used to construct 64-bit values from 32-bit values (for the low and high parts). The MAKEDI macro casts the high 32-bit value to a signed 32-bit value before shifting. If this created a negative value, this would be undefined behavior according to the C standard. All we care about is shifting the 32-bits as they are to the high 32-bits, not caring about sign extension (since there's nothing left to shift into), and the low 32-bits being empty. This is what we get from shifting an unsigned value, so cast it to unsigned 32-bit to avoid undefined behavior. While we're here, change the SETLODI macro to truncate the lower value to 32-bits before we set it. If it was passing in a 64-bit value, those high bits would get included too, and that's not what we want. Similarly, tweak the SETHIDI macro to cast the value to an unsigned 64-bit instead of a signed 64-bit. If the value was only 32-bits, the behavior would be the same. If it happened to be signed 64-bit, it would trigger the undefined behavior too. --- sim/common/cgen-types.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sim/common/cgen-types.h b/sim/common/cgen-types.h index 01a3ee9be584..24c2b89216fc 100644 --- a/sim/common/cgen-types.h +++ b/sim/common/cgen-types.h @@ -72,9 +72,9 @@ typedef int64_t DI; typedef uint64_t UDI; #define GETLODI(di) ((SI) (di)) #define GETHIDI(di) ((SI) ((UDI) (di) >> 32)) -#define SETLODI(di, val) ((di) = (((di) & 0xffffffff00000000LL) | (val))) -#define SETHIDI(di, val) ((di) = (((di) & 0xffffffffLL) | (((DI) (val)) << 32))) -#define MAKEDI(hi, lo) ((((DI) (SI) (hi)) << 32) | ((UDI) (USI) (lo))) +#define SETLODI(di, val) ((di) = (((di) & 0xffffffff00000000LL) | (USI) (val))) +#define SETHIDI(di, val) ((di) = (((di) & 0xffffffffLL) | (((UDI) (val)) << 32))) +#define MAKEDI(hi, lo) ((DI) (((UDI) (hi) << 32) | (UDI) (USI) (lo))) /* These are used to record extracted raw data from an instruction, among other things. It must be a host data type, and not a target one. */