From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) by sourceware.org (Postfix) with ESMTP id 8057B3858D33 for ; Fri, 22 Dec 2023 00:55:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8057B3858D33 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 8057B3858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=140.211.166.183 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703206529; cv=none; b=lK0Q6ciNJOGEuOmpkWGtixiBOGfVwD+FkS5kjDRTYu7wFI1IeyoJGFPJuVDqBXrPU8Ok2teatggcWQ0WyahRkNXceJkMWN4kqx8vvYx80mWxKLga34vbawzieri8sJvlK6QJbiRa1F2KQ8g9sVR8sXDIrqn6Ei5pUYtNs8G2ByA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703206529; c=relaxed/simple; bh=wiVVEz3W7ZQI54vOpTKo8WIBQMGwy2mmAROcVJUOdjs=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=w96b9m1PY0V5eqwkKXtKnkpCuXGphXtRg8ULDGDQU8d+/su2Li/HEE+Z+u+2junN/ZbLIv/93WpqCsvSoUr40OY5ipTy8fboH8WjrbS8KKZKPmeAZ3dEbYjgp8FMRqq4y9t3gcRyoHvr9bzwy4bVyj+mqyB7RMRqXW9+xOqfSwI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by smtp.gentoo.org (Postfix, from userid 559) id C67413408DD; Fri, 22 Dec 2023 00:55:26 +0000 (UTC) From: Mike Frysinger To: cgen@sourceware.org Subject: [PATCH] sim: avoid shadowing vars when decoding insns Date: Thu, 21 Dec 2023 19:55:24 -0500 Message-ID: <20231222005524.14053-1-vapier@gentoo.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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 List-Id: When generating switch decode tables, the switch statements all use the same variable name "val". This causes nested switches to shadow earlier ones. Use the existing "switch-num" to generate unique names when nesting since it's otherwise unused. Previously we'd have: ... { unsigned int val = (((insn >> 4) & (255 << 0))); switch (val) { ... case 15 : { unsigned int val = (((insn >> 12) & (15 << 0))); switch (val) { ... Leading to: .../sim/cris/decodev10.c: In function ‘crisv10f_decode’: .../sim/cris/decodev10.c:351:24: error: declaration of ‘val’ shadows a previous local [-Werror=shadow=compatible-local] 351 | unsigned int val = (((insn >> 12) & (15 << 0))); | ^~~ .../sim/cris/decodev10.c:331:20: note: shadowed declaration is here 331 | unsigned int val = (((insn >> 4) & (255 << 0))); | ^~~ Now we have: ... { unsigned int val0 = (((insn >> 4) & (255 << 0))); switch (val0) { ... case 15 : { unsigned int val1 = (((insn >> 12) & (15 << 0))); switch (val1) { ... --- utils-sim.scm | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/utils-sim.scm b/utils-sim.scm index 07d776d286d8..dca0f0705036 100644 --- a/utils-sim.scm +++ b/utils-sim.scm @@ -995,8 +995,8 @@ ) ; Generate switch statement to decode TABLE-GUTS. -; SWITCH-NUM is for compatibility with the computed goto decoder and -; isn't used. +; SWITCH-NUM is for compatibility with the computed goto decoder and tracks the +; nesting depth. ; STARTBIT is the bit offset of the instruction value that C variable `insn' ; holds (note that this is independent of LSB0?). ; DECODE-BITSIZE is the number of bits of the insn that `insn' holds. @@ -1017,7 +1017,8 @@ table-guts table-guts-thus-far indent lsb0? invalid-insn fn?) - (let ((new-table-guts-thus-far (append table-guts-thus-far (list table-guts)))) + (let ((new-table-guts-thus-far (append table-guts-thus-far (list table-guts))) + (varname (string-append "val" (number->string switch-num)))) (string-list indent "{\n" @@ -1027,19 +1028,19 @@ (set! startbit (dtable-guts-startbit table-guts)) (set! decode-bitsize (dtable-guts-bitsize table-guts)) ;; FIXME: Bits may get fetched again during extraction. - (string-append indent " unsigned int val;\n" + (string-append indent " unsigned int " varname ";\n" indent " /* Must fetch more bits. */\n" indent " insn = " (gen-ifetch "pc" startbit decode-bitsize) ";\n" - indent " val = ")) - (string-append indent " unsigned int val = ")) + indent " " varname " = ")) + (string-append indent " unsigned int " varname " = ")) (/gen-decode-bits (dtable-guts-bitnums table-guts) (dtable-guts-startbit table-guts) (dtable-guts-bitsize table-guts) "insn" "entire_insn" lsb0?) ";\n" - indent " switch (val)\n" + indent " switch (" varname ")\n" indent " {\n" ;; The code is more readable, and icache use is improved, if we collapse @@ -1067,7 +1068,7 @@ (/gen-decode-expr-entry (car entries) indent invalid-insn fn?)) ((table) (/gen-decode-table-entry (car entries) (cdr entries) - switch-num startbit decode-bitsize + (+ switch-num 1) startbit decode-bitsize new-table-guts-thus-far indent lsb0? invalid-insn fn?)) ) @@ -1105,7 +1106,7 @@ ; Now print it out. - (/gen-decoder-switch "0" 0 decode-bitsize + (/gen-decoder-switch 0 0 decode-bitsize table-guts nil indent lsb0? invalid-insn fn?)) ) -- 2.43.0