From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by sourceware.org (Postfix) with ESMTPS id 83C0D3851164 for ; Tue, 6 Sep 2022 12:22:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 83C0D3851164 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=vrull.eu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=vrull.eu Received: by mail-ej1-x630.google.com with SMTP id fy31so22807564ejc.6 for ; Tue, 06 Sep 2022 05:22:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=Sgu4xW8NaYloe7kiikLBkazPEpbXbNnySgVCvRhmwG8=; b=euO9opyUiXxweJzkmCRyEQQbgxvCbGhjbfIzImzKMMKKIRfsEqd1nodGXdTvlsme9Y z4esJwyv8idjrrLOdI807wB6TCXjNQZcLpqpEmiwgr4n+vA3CVv/Bqvc61ri/TmklwLf YTfZ7kJxHJ+cKyR9eRUH9hHl9BQ5QS3E/pCio9/kqbhvwYJobYl9Cz5xg82h5rvKo3Q/ A49d9H8awht3Y/XAskXq9R3BQHnyy+vy7a4BcdzB8FjN4QaVmx6idnXp0zpsqjsIfY9B Iz+xA8ISABzOkVbXrvMRsrfPvJNZvVzFdztps4fnEjXiDkuR2pwQRjice7tDVsJBFN5j La5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=Sgu4xW8NaYloe7kiikLBkazPEpbXbNnySgVCvRhmwG8=; b=713Vj7FnqCdW2JTl2YjXTMU0m08B0XyeacdBuOmQPJrqC2Fc+T3GkmbOCesWLUwC2V DIGKI+mxijN2sHxM9L/8NiwN3a0ltbw9X9lbaRg09Rt1Wdp2aPKCwAwbRcr/Iqtt0bjc ebO91M2a5su8qFfDHOkbCg52115VgY6RPvvqJ94xf5M4U9q7gkzrXJlWt6JdpX1futHp oHbbABXcrrx5WW0LQGgPc3q9hfeKcXIi2cZ7VJ2l/z3x6esb0KZvzLdYktK9e8bFuHpr m2tFJqekP6NJYoslYo4ambMIINNxL8POi1eWHRhJCb4bRwkSYv/82R1QnWqIFbovsQH2 VhEQ== X-Gm-Message-State: ACgBeo015hdFXuyL9t3D38fj3m/leiQP6Pmng6a7OMZD4qggqR9hezvk Z0z1UbgwYNTOGK5pOFUkQazVVZ4sMe2Ylw== X-Google-Smtp-Source: AA6agR6QzG1KrSVWhPMo6e3l4PyStRf0JQ5r/aJxP7fWugErFYnixAL278PDnYhOUiJTZanG4OCG7w== X-Received: by 2002:a17:906:794f:b0:745:4d49:139a with SMTP id l15-20020a170906794f00b007454d49139amr18204957ejo.468.1662466943080; Tue, 06 Sep 2022 05:22:23 -0700 (PDT) Received: from beast.fritz.box (62-178-148-172.cable.dynamic.surfer.at. [62.178.148.172]) by smtp.gmail.com with ESMTPSA id y21-20020a170906525500b0073d7e58b1bcsm6571632ejm.157.2022.09.06.05.22.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Sep 2022 05:22:22 -0700 (PDT) From: Christoph Muellner To: binutils@sourceware.org, Nelson Chu , Kito Cheng , Jim Wilson , Philipp Tomsich , Palmer Dabbelt , Cooper Qu , Lifang Xia , Yunhai Shang , Zhiwei Liu Cc: =?UTF-8?q?Christoph=20M=C3=BCllner?= Subject: [PATCH 04/13] RISC-V: Add support for arbitrary immediate encoding formats Date: Tue, 6 Sep 2022 14:22:04 +0200 Message-Id: <20220906122213.1243129-5-christoph.muellner@vrull.eu> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220906122213.1243129-1-christoph.muellner@vrull.eu> References: <20220906122213.1243129-1-christoph.muellner@vrull.eu> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_MANYTO,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,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: From: Christoph Müllner This patch introduces support for arbitrary signed or unsigned immediate encoding formats. The formats have the form "XsN@S" and "XuN@S" with N being the number of bits and S the LSB position. For example an immediate field of 5 bytes that encodes a signed value and is stored in the bits 24-20 of the instruction word can use the format specifier "Xs5@20". Signed-off-by: Christoph Müllner --- gas/config/tc-riscv.c | 74 ++++++++++++++++++++++++++++++++++++++++++ include/opcode/riscv.h | 17 ++++++++++ opcodes/riscv-dis.c | 34 +++++++++++++++++++ 3 files changed, 125 insertions(+) diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c index 34ce68e8252..7f0db9fdb80 100644 --- a/gas/config/tc-riscv.c +++ b/gas/config/tc-riscv.c @@ -441,6 +441,9 @@ static char *expr_end; #define INSERT_OPERAND(FIELD, INSN, VALUE) \ INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD) +#define INSERT_IMM(n, s, INSN, VALUE) \ + INSERT_BITS ((INSN).insn_opcode, VALUE, (1ULL<X_add_number, n)) + as_bad (_("improper immediate value (%lu)"), + (unsigned long) imm_expr->X_add_number); + } + else + { + if (!VALIDATE_S_IMM (imm_expr->X_add_number, n)) + as_bad (_("improper immediate value (%li)"), + (long) imm_expr->X_add_number); + } + INSERT_IMM (n, s, *ip, imm_expr->X_add_number); + imm_expr->X_op = O_absent; + asarg = expr_end; + continue; + default: + goto unknown_riscv_ip_operand; + } + } + break; default: unknown_riscv_ip_operand: as_fatal (_("internal: unknown argument type `%s'"), diff --git a/include/opcode/riscv.h b/include/opcode/riscv.h index 6fdc9c9302e..faef28a3739 100644 --- a/include/opcode/riscv.h +++ b/include/opcode/riscv.h @@ -60,6 +60,7 @@ static const char * const riscv_pred_succ[16] = #define RV_X(x, s, n) (((x) >> (s)) & ((1 << (n)) - 1)) #define RV_IMM_SIGN(x) (-(((x) >> 31) & 1)) +#define RV_X_SIGNED(x, s, n) (RV_X(x, s, n) | ((-(RV_X(x, (s + n - 1), 1))) << (n))) #define EXTRACT_ITYPE_IMM(x) \ (RV_X(x, 20, 12) | (RV_IMM_SIGN(x) << 12)) @@ -347,6 +348,22 @@ static const char * const riscv_pred_succ[16] = #define EXTRACT_OPERAND(FIELD, INSN) \ EXTRACT_BITS ((INSN), OP_MASK_##FIELD, OP_SH_##FIELD) +/* Extract an unsigned immediate operand on position s with n bits. */ +#define EXTRACT_U_IMM(n, s, l) \ + RV_X (l, s, n) + +/* Extract an signed immediate operand on position s with n bits. */ +#define EXTRACT_S_IMM(n, s, l) \ + RV_X_SIGNED (l, s, n) + +/* Validate that unsigned n-bit immediate is within bounds. */ +#define VALIDATE_U_IMM(v, n) \ + ((unsigned long) v < (1UL << n)) + +/* Validate that signed n-bit immediate is within bounds. */ +#define VALIDATE_S_IMM(v, n) \ + (v < (long) (1UL << (n-1)) && v >= -(offsetT) (1UL << (n-1))) + /* The maximal number of subset can be required. */ #define MAX_SUBSET_NUM 4 diff --git a/opcodes/riscv-dis.c b/opcodes/riscv-dis.c index 7ae6e709290..c6ddea16dda 100644 --- a/opcodes/riscv-dis.c +++ b/opcodes/riscv-dis.c @@ -563,7 +563,41 @@ print_insn_args (const char *oparg, insn_t l, bfd_vma pc, disassemble_info *info print (info->stream, dis_style_text, "%d", rs1); break; + case 'X': /* Integer immediate. */ + { + size_t n; + size_t s; + bool sign; + + switch (*++oparg) + { + case 's': /* 'XsN@S' ... N-bit signed immediate at bit S. */ + sign = true; + goto print_imm; + case 'u': /* 'XuN@S' ... N-bit unsigned immediate at bit S. */ + sign = false; + goto print_imm; + print_imm: + n = strtol (++oparg, (char **)&oparg, 10); + if (*oparg != '@') + goto undefined_modifier; + s = strtol (++oparg, (char **)&oparg, 10); + oparg--; + + if (!sign) + print (info->stream, dis_style_immediate, "%u", + (unsigned)EXTRACT_U_IMM (n, s, l)); + else + print (info->stream, dis_style_immediate, "%i", + (unsigned)EXTRACT_S_IMM (n, s, l)); + break; + default: + goto undefined_modifier; + } + } + break; default: + undefined_modifier: /* xgettext:c-format */ print (info->stream, dis_style_text, _("# internal error, undefined modifier (%c)"), -- 2.37.2