Skip to content

Commit 634001b

Browse files
committed
RISC-V: Better support for long instructions (assembler)
Commit bb99669 ("RISC-V/gas: allow generating up to 176-bit instructions with .insn") tried to start supporting long instructions but it was insufficient. 1. It heavily depended on the bignum internals (radix of 2^16), 2. It generates "value conflicts with instruction length" even if a big number instruction encoding does not exceed its expected length and 3. Because long opcode was handled separately (from struct riscv_cl_insn), some information like DWARF line number correspondence was missing. To resolve these problems, this commit: 1. Handles bignum (and its encodings) precisely and 2. Incorporates long opcode handling into regular instruction handling. This commit will be tested on the separate commit. gas/ChangeLog: * config/tc-riscv.c (struct riscv_cl_insn): Add long opcode field. (create_insn) Clear long opcode marker. (install_insn) Install longer opcode as well. (s_riscv_insn) Likewise. (riscv_ip_hardcode): Make big number handling stricter. Length and the value conflicts only if the bignum size exceeds the expected maximum length.
1 parent 97f006b commit 634001b

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

gas/config/tc-riscv.c

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ struct riscv_cl_insn
4242
/* The opcode's entry in riscv_opcodes. */
4343
const struct riscv_opcode *insn_mo;
4444

45-
/* The encoded instruction bits. */
45+
/* The encoded instruction bits
46+
(first bits enough to extract instruction length on a long opcode). */
4647
insn_t insn_opcode;
4748

49+
/* The long encoded instruction bits ([0] is non-zero on a long opcode). */
50+
char insn_long_opcode[RISCV_MAX_INSN_LEN];
51+
4852
/* The frag that contains the instruction. */
4953
struct frag *frag;
5054

@@ -720,6 +724,7 @@ create_insn (struct riscv_cl_insn *insn, const struct riscv_opcode *mo)
720724
{
721725
insn->insn_mo = mo;
722726
insn->insn_opcode = mo->match;
727+
insn->insn_long_opcode[0] = 0;
723728
insn->frag = NULL;
724729
insn->where = 0;
725730
insn->fixp = NULL;
@@ -731,7 +736,10 @@ static void
731736
install_insn (const struct riscv_cl_insn *insn)
732737
{
733738
char *f = insn->frag->fr_literal + insn->where;
734-
number_to_chars_littleendian (f, insn->insn_opcode, insn_length (insn));
739+
if (insn->insn_long_opcode[0] != 0)
740+
memcpy (f, insn->insn_long_opcode, insn_length (insn));
741+
else
742+
number_to_chars_littleendian (f, insn->insn_opcode, insn_length (insn));
735743
}
736744

737745
/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
@@ -3503,7 +3511,9 @@ riscv_ip_hardcode (char *str,
35033511
values[num++] = (insn_t) imm_expr->X_add_number;
35043512
break;
35053513
case O_big:
3506-
values[num++] = generic_bignum[0];
3514+
/* Extract lower 32-bits of a big number.
3515+
Assume that generic_bignum_to_int32 work on such number. */
3516+
values[num++] = (insn_t) generic_bignum_to_int32 ();
35073517
break;
35083518
default:
35093519
/* The first value isn't constant, so it should be
@@ -3530,12 +3540,25 @@ riscv_ip_hardcode (char *str,
35303540

35313541
if (imm_expr->X_op == O_big)
35323542
{
3533-
if (bytes != imm_expr->X_add_number * CHARS_PER_LITTLENUM)
3543+
unsigned int llen = 0;
3544+
for (LITTLENUM_TYPE lval = generic_bignum[imm_expr->X_add_number - 1];
3545+
lval != 0; llen++)
3546+
lval >>= BITS_PER_CHAR;
3547+
unsigned int repr_bytes
3548+
= (imm_expr->X_add_number - 1) * CHARS_PER_LITTLENUM + llen;
3549+
if (bytes < repr_bytes)
35343550
return _("value conflicts with instruction length");
3535-
char *f = frag_more (bytes);
3536-
for (num = 0; num < imm_expr->X_add_number; ++num)
3537-
number_to_chars_littleendian (f + num * CHARS_PER_LITTLENUM,
3538-
generic_bignum[num], CHARS_PER_LITTLENUM);
3551+
for (num = 0; num < imm_expr->X_add_number - 1; ++num)
3552+
number_to_chars_littleendian (
3553+
ip->insn_long_opcode + num * CHARS_PER_LITTLENUM,
3554+
generic_bignum[num],
3555+
CHARS_PER_LITTLENUM);
3556+
if (llen != 0)
3557+
number_to_chars_littleendian (
3558+
ip->insn_long_opcode + num * CHARS_PER_LITTLENUM,
3559+
generic_bignum[num],
3560+
llen);
3561+
memset(ip->insn_long_opcode + repr_bytes, 0, bytes - repr_bytes);
35393562
return NULL;
35403563
}
35413564

@@ -4612,7 +4635,7 @@ s_riscv_insn (int x ATTRIBUTE_UNUSED)
46124635
else
46134636
as_bad ("%s `%s'", error.msg, error.statement);
46144637
}
4615-
else if (imm_expr.X_op != O_big)
4638+
else
46164639
{
46174640
gas_assert (insn.insn_mo->pinfo != INSN_MACRO);
46184641
append_insn (&insn, &imm_expr, imm_reloc);

0 commit comments

Comments
 (0)