Skip to contentSkip to conversation

disas/riscv: Further correction to LUI disassembly

PATCH In review Richard Bagley 4 participants 15 messages Jul 31, 2023 – Mar 12, 2024 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 disas/riscv: Further correction to LUI disassembly

The recent commit 36df75a0a9 corrected one aspect of LUI disassembly by recovering the immediate argument from the result of LUI with a shift right by 12. However, the shift right will left-fill with the sign. By applying a mask we recover an unsigned representation of the 20-bit field (which includes a sign bit).

Example:
0xfffff000 >> 12 = 0xffffffff
0xfffff000 >> 12 & 0xfffff = 0x000fffff

Fixes 36df75a0a9 ("riscv/disas: Fix disas output of upper immediates")
---
 disas/riscv.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
disas/riscv.c +6 -3
index 4023e3fc65..690eb4a1ac 100644
@@ -4723,9 +4723,12 @@ static void format_inst(char *buf, size_t buflen, size_t tab, rv_decode *dec)
             break;
         case 'U':
             fmt++;
-            snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12);
-            append(buf, tmp, buflen);
-            if (*fmt == 'o') {
+            if (*fmt == 'i') {
+                snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12 & 0xfffff);
+                append(buf, tmp, buflen);
+            } else if (*fmt == 'o') {
+                snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12);
+                append(buf, tmp, buflen);
                 while (strlen(buf) < tab * 2) {
                     append(buf, " ", buflen);
                 }
Signature · 1 line
2.34.1
#1 lore raw
Quoted 5 lines · Richard Bagley

The recent commit 36df75a0a9 corrected one aspect of LUI disassembly by recovering the immediate argument from the result of LUI with a shift right by 12. However, the shift right will left-fill with the sign. By applying a mask we recover an unsigned representation of the 20-bit field (which includes a sign bit).

Why would you want that? Surely

    lui r1, -1

is more accurate than

    lui r1, 0xfffff

r~

#2 lore raw

I do apologize, but I do not understand your remark at all. Could I trouble you to spell this out.

In:
+ snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12 & 0xfffff);
0xfffff is a mask which recovers the 20 bit field used to represent the immediate in the instruction encoding.

You seem to be responding to the syntax, which is unrelated to my change. But I did notice it is the case that both GCC and LLVM disassemblers do not accept signed integer arguments to LUI:
lui r1, -1
but instead require
lui r1, 0xfffff
I don't see why the former is more accurate, but it would be an aid to the assembly programmer.

I have recommended internally that if the current format cannot support both, then it might be worthwhile to propose a pseudo instruction for RISCV for precisely this syntax variant:
lui.s r1.-1

Richard

Quoted 18 lines · Richard Henderson
Quoted 5 lines · Richard Bagley

The recent commit 36df75a0a9 corrected one aspect of LUI disassembly by recovering the immediate argument from the result of LUI with a shift right by 12. However, the shift right will left-fill with the sign. By applying a mask we recover an unsigned representation of the 20-bit field (which includes a sign bit).

Why would you want that? Surely

     lui r1, -1

is more accurate than

     lui r1, 0xfffff

r~

#3 lore raw
Quoted 11 lines · Richard Bagley

I do apologize, but I do not understand your remark at all. Could I trouble you to spell this out.

In:
+                snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12 & 0xfffff);
0xfffff is a mask which recovers the 20 bit field used to represent the immediate in the instruction encoding.

You seem to be responding to the syntax, which is unrelated to my change.
But I did notice it is the case that both GCC and LLVM disassemblers do not accept signed integer arguments to LUI:
lui r1, -1
but instead require
lui r1, 0xfffff

Your language is confusing. Disassembler or assembler? A disassembler would not "accept" but "output" arguments for LUI.

If the assembler rejects "lui r1, -1", that would be a bug, because the field *is* signed.

For the disassembler, the field *is* signed, therefore outputting a signed value is correct. Outputting an unsigned hex value hides the fact that bit 31 is the sign.

To my mind this is exactly the same as emitting a signed value for the immediate in ADDI.

r~

#4 lore raw
In reply to Richard Bagley
Quoted 21 lines · Richard Bagley

The recent commit 36df75a0a9 corrected one aspect of LUI disassembly by recovering the immediate argument from the result of LUI with a shift right by 12. However, the shift right will left-fill with the sign. By applying a mask we recover an unsigned representation of the 20-bit field (which includes a sign bit).

Example:
0xfffff000 >> 12 = 0xffffffff
0xfffff000 >> 12 & 0xfffff = 0x000fffff

Fixes 36df75a0a9 ("riscv/disas: Fix disas output of upper immediates")
---
 disas/riscv.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
disas/riscv.c +0 -0
index 4023e3fc65..690eb4a1ac 100644

disas/riscv.c static void format_inst(char *buf, size_t buflen, size_t tab, rv_decode *dec) · line 4727

Quoted 8 lines
disas/riscv.c +2 -3
@@ -4723,9 +4723,12 @@ static void format_inst(char *buf, size_t buflen, size_t tab, rv_decode *dec)
             break;
         case 'U':
             fmt++;
-            snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12);
-            append(buf, tmp, buflen);
-            if (*fmt == 'o') {
+            if (*fmt == 'i') {
+                snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12 & 0xfffff);

Why are we correcting LUI's output, but still outputting sign-extended values for AUIPC?

We can't assemble 'auipc a1, 0xffffffff' or 'auipc a1, -1' without getting

Error: lui expression not in range 0..1048575

(and additionally for 0xffffffff)

Error: value of 00000ffffffff000 too large for field of 4 bytes at 0000000000000000

either.

(I see that the assembler's error messages state 'lui', but I was trying 'auipc'.)

I'm using as from gnu binutils 2.40.0.20230214.

(And, FWIW, I agree with Richard Henderson that these instructions should accept negative values.)

Thanks,
drew

Quoted 11 lines
disas/riscv.c +4 -0
+                append(buf, tmp, buflen);
+            } else if (*fmt == 'o') {
+                snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12);
+                append(buf, tmp, buflen);
                 while (strlen(buf) < tab * 2) {
                     append(buf, " ", buflen);
                 }
Signature · 1 line
2.34.1
#5 lore raw
Quoted 44 lines · On Thu, 10 Aug 2023 08:31:46 PDT (-0700), [email protected] wrote:
Quoted 21 lines · Richard Bagley

The recent commit 36df75a0a9 corrected one aspect of LUI disassembly by recovering the immediate argument from the result of LUI with a shift right by 12. However, the shift right will left-fill with the sign. By applying a mask we recover an unsigned representation of the 20-bit field (which includes a sign bit).

Example:
0xfffff000 >> 12 = 0xffffffff
0xfffff000 >> 12 & 0xfffff = 0x000fffff

Fixes 36df75a0a9 ("riscv/disas: Fix disas output of upper immediates")
---
 disas/riscv.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
disas/riscv.c +0 -0
index 4023e3fc65..690eb4a1ac 100644

disas/riscv.c static void format_inst(char *buf, size_t buflen, size_t tab, rv_decode *dec) · line 4727

Quoted 8 lines
disas/riscv.c +2 -3
@@ -4723,9 +4723,12 @@ static void format_inst(char *buf, size_t buflen, size_t tab, rv_decode *dec)
             break;
         case 'U':
             fmt++;
-            snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12);
-            append(buf, tmp, buflen);
-            if (*fmt == 'o') {
+            if (*fmt == 'i') {
+                snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12 & 0xfffff);

Why are we correcting LUI's output, but still outputting sign-extended values for AUIPC?

We can't assemble 'auipc a1, 0xffffffff' or 'auipc a1, -1' without getting

Error: lui expression not in range 0..1048575

(and additionally for 0xffffffff)

Error: value of 00000ffffffff000 too large for field of 4 bytes at 0000000000000000

either.

Quoted 7 lines

(I see that the assembler's error messages state 'lui', but I was trying 'auipc'.)

I'm using as from gnu binutils 2.40.0.20230214.

(And, FWIW, I agree with Richard Henderson that these instructions should accept negative values.)

I'm kind of lost here, and you saying binutils rejects this syntax? If that's the case it's probably just an oversight, can you file a bug in binutils land so folks can see?

Quoted 16 lines

Thanks,
drew

Quoted 11 lines
disas/riscv.c +4 -1
+                append(buf, tmp, buflen);
+            } else if (*fmt == 'o') {
+                snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12);
+                append(buf, tmp, buflen);
                 while (strlen(buf) < tab * 2) {
                     append(buf, " ", buflen);
                 }
--

2.34.1

#6 lore raw
Quoted 48 lines · Palmer Dabbelt
Quoted 47 lines · On Thu, 10 Aug 2023 08:31:46 PDT (-0700), [email protected] wrote:
Quoted 21 lines · Richard Bagley

The recent commit 36df75a0a9 corrected one aspect of LUI disassembly by recovering the immediate argument from the result of LUI with a shift right by 12. However, the shift right will left-fill with the sign. By applying a mask we recover an unsigned representation of the 20-bit field (which includes a sign bit).

Example:
0xfffff000 >> 12 = 0xffffffff
0xfffff000 >> 12 & 0xfffff = 0x000fffff

Fixes 36df75a0a9 ("riscv/disas: Fix disas output of upper immediates")
---
 disas/riscv.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
disas/riscv.c +0 -0
index 4023e3fc65..690eb4a1ac 100644

disas/riscv.c static void format_inst(char *buf, size_t buflen, size_t tab, rv_decode *dec) · line 4727

Quoted 8 lines
disas/riscv.c +2 -3
@@ -4723,9 +4723,12 @@ static void format_inst(char *buf, size_t buflen, size_t tab, rv_decode *dec)
             break;
         case 'U':
             fmt++;
-            snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12);
-            append(buf, tmp, buflen);
-            if (*fmt == 'o') {
+            if (*fmt == 'i') {
+                snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12 & 0xfffff);

Why are we correcting LUI's output, but still outputting sign-extended values for AUIPC?

We can't assemble 'auipc a1, 0xffffffff' or 'auipc a1, -1' without getting

Error: lui expression not in range 0..1048575

(and additionally for 0xffffffff)

Error: value of 00000ffffffff000 too large for field of 4 bytes at 0000000000000000

either.

(I see that the assembler's error messages state 'lui', but I was trying 'auipc'.)

Quoted 8 lines
Quoted 4 lines

I'm using as from gnu binutils 2.40.0.20230214.

(And, FWIW, I agree with Richard Henderson that these instructions should accept negative values.)

I'm kind of lost here, and you saying binutils rejects this syntax? If that's the case it's probably just an oversight, can you file a bug in binutils land so folks can see?

Will do.

Thanks,
drew

Quoted 17 lines
Quoted 17 lines

Thanks,
drew

Quoted 11 lines
disas/riscv.c +4 -1
+                append(buf, tmp, buflen);
+            } else if (*fmt == 'o') {
+                snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12);
+                append(buf, tmp, buflen);
                 while (strlen(buf) < tab * 2) {
                     append(buf, " ", buflen);
                 }
--

2.34.1

#7 lore raw
Quoted 51 lines · Andrew Jones
Quoted 50 lines · Palmer Dabbelt
Quoted 49 lines · On Thu, 10 Aug 2023 08:31:46 PDT (-0700), [email protected] wrote:
Quoted 21 lines · Richard Bagley

The recent commit 36df75a0a9 corrected one aspect of LUI disassembly by recovering the immediate argument from the result of LUI with a shift right by 12. However, the shift right will left-fill with the sign. By applying a mask we recover an unsigned representation of the 20-bit field (which includes a sign bit).

Example:
0xfffff000 >> 12 = 0xffffffff
0xfffff000 >> 12 & 0xfffff = 0x000fffff

Fixes 36df75a0a9 ("riscv/disas: Fix disas output of upper immediates")
---
 disas/riscv.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
disas/riscv.c +0 -0
index 4023e3fc65..690eb4a1ac 100644

disas/riscv.c static void format_inst(char *buf, size_t buflen, size_t tab, rv_decode *dec) · line 4727

Quoted 8 lines
disas/riscv.c +2 -3
@@ -4723,9 +4723,12 @@ static void format_inst(char *buf, size_t buflen, size_t tab, rv_decode *dec)
             break;
         case 'U':
             fmt++;
-            snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12);
-            append(buf, tmp, buflen);
-            if (*fmt == 'o') {
+            if (*fmt == 'i') {
+                snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12 & 0xfffff);

Why are we correcting LUI's output, but still outputting sign-extended values for AUIPC?

We can't assemble 'auipc a1, 0xffffffff' or 'auipc a1, -1' without getting

Error: lui expression not in range 0..1048575

(and additionally for 0xffffffff)

Error: value of 00000ffffffff000 too large for field of 4 bytes at 0000000000000000

either.

(I see that the assembler's error messages state 'lui', but I was trying 'auipc'.)

I'm using as from gnu binutils 2.40.0.20230214.

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

(And, FWIW, I agree with Richard Henderson that these instructions should accept negative values.)

I'm kind of lost here, and you saying binutils rejects this syntax? If that's the case it's probably just an oversight, can you file a bug in binutils land so folks can see?

Will do.

https://sourceware.org/bugzilla/show_bug.cgi?id=30746

Thanks,
drew

#8 lore raw
Quoted 55 lines · Andrew Jones
Quoted 54 lines · Andrew Jones
Quoted 53 lines · Palmer Dabbelt
Quoted 51 lines · On Thu, 10 Aug 2023 08:31:46 PDT (-0700), [email protected] wrote:
Quoted 21 lines · Richard Bagley

The recent commit 36df75a0a9 corrected one aspect of LUI disassembly by recovering the immediate argument from the result of LUI with a shift right by 12. However, the shift right will left-fill with the sign. By applying a mask we recover an unsigned representation of the 20-bit field (which includes a sign bit).

Example:
0xfffff000 >> 12 = 0xffffffff
0xfffff000 >> 12 & 0xfffff = 0x000fffff

Fixes 36df75a0a9 ("riscv/disas: Fix disas output of upper immediates")
---
 disas/riscv.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
disas/riscv.c +0 -0
index 4023e3fc65..690eb4a1ac 100644

disas/riscv.c static void format_inst(char *buf, size_t buflen, size_t tab, rv_decode *dec) · line 4727

Quoted 8 lines
disas/riscv.c +2 -3
@@ -4723,9 +4723,12 @@ static void format_inst(char *buf, size_t buflen, size_t tab, rv_decode *dec)
             break;
         case 'U':
             fmt++;
-            snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12);
-            append(buf, tmp, buflen);
-            if (*fmt == 'o') {
+            if (*fmt == 'i') {
+                snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12 & 0xfffff);

Why are we correcting LUI's output, but still outputting sign-extended values for AUIPC?

We can't assemble 'auipc a1, 0xffffffff' or 'auipc a1, -1' without getting

Error: lui expression not in range 0..1048575

(and additionally for 0xffffffff)

Error: value of 00000ffffffff000 too large for field of 4 bytes at 0000000000000000

either.

(I see that the assembler's error messages state 'lui', but I was trying 'auipc'.)

I'm using as from gnu binutils 2.40.0.20230214.

(And, FWIW, I agree with Richard Henderson that these instructions should accept negative values.)

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

I'm kind of lost here, and you saying binutils rejects this syntax? If that's the case it's probably just an oversight, can you file a bug in binutils land so folks can see?

Will do.

https://sourceware.org/bugzilla/show_bug.cgi?id=30746

But, to try to bring this thread back to the patch under review. While the binutils BZ may address our preferred way of providing immediates to the assembler, this patch is trying to make QEMU's output consistent with objdump. Since objdump always outputs long immediate values as hex, then it doesn't need to care about negative signs. QEMU seems to prefer decimal, though, and so does llvm-objdump, which outputs values for these instructions in the range 0..1048575. So, I guess this patch is making QEMU consistent with llvm-objdump.

Back to making suggestions for this patch...

1. The commit message should probably say something along the lines of what I just wrote in the preceding paragraph to better explain the motivation.

2. Unless I'm missing something, then this patch should also address AUIPC.

Thanks,
drew

author NAK
#9 lore raw

NACK

We have established that the change is a workaround for a bug in the assembler.
I withdraw the merge request.

Thank you for this careful review.

Quoted 102 lines · Andrew Jones
Quoted 3 lines · Andrew Jones
Quoted 2 lines · Andrew Jones
Quoted 1 line · Palmer Dabbelt

On Thu, 10 Aug 2023 08:31:46 PDT (-0700), [email protected]

Quoted 2 lines · wrote:
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines · Richard Bagley
Quoted 1 line

The recent commit 36df75a0a9 corrected one aspect of LUI

disassembly

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

by recovering the immediate argument from the result of LUI with

a

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

shift right by 12. However, the shift right will left-fill with

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

sign. By applying a mask we recover an unsigned representation

of the

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

20-bit field (which includes a sign bit).

Example:
0xfffff000 >> 12 = 0xffffffff
0xfffff000 >> 12 & 0xfffff = 0x000fffff

Fixes 36df75a0a9 ("riscv/disas: Fix disas output of upper

immediates")

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

disas/riscv.c static void format_inst(char *buf, size_t

Quoted 10 lines
---
 disas/riscv.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
disas/riscv.c +0 -0
index 4023e3fc65..690eb4a1ac 100644
@@ -4723,9 +4723,12 @@ static void format_inst(char *buf, size_t

buflen, size_t tab, rv_decode *dec)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

disas/riscv.c static void format_inst(char *buf, size_t buflen, size_t tab, rv_decode *dec)

Quoted 8 lines
disas/riscv.c +2 -3
             break;
         case 'U':
             fmt++;
-            snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12);
-            append(buf, tmp, buflen);
-            if (*fmt == 'o') {
+            if (*fmt == 'i') {
+                snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12

& 0xfffff);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Why are we correcting LUI's output, but still outputting

sign-extended

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

values for AUIPC?

We can't assemble 'auipc a1, 0xffffffff' or 'auipc a1, -1' without

getting

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Error: lui expression not in range 0..1048575

(and additionally for 0xffffffff)

Error: value of 00000ffffffff000 too large for field of 4 bytes

at 0000000000000000

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

either.

(I see that the assembler's error messages state 'lui', but I was

trying

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

'auipc'.)

I'm using as from gnu binutils 2.40.0.20230214.

(And, FWIW, I agree with Richard Henderson that these instructions

should

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

accept negative values.)

I'm kind of lost here, and you saying binutils rejects this syntax?

If

Quoted 1 line
Quoted 1 line
Quoted 1 line

that's the case it's probably just an oversight, can you file a bug

in

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

binutils land so folks can see?

Will do.

https://sourceware.org/bugzilla/show_bug.cgi?id=30746

But, to try to bring this thread back to the patch under review. While the binutils BZ may address our preferred way of providing immediates to the assembler, this patch is trying to make QEMU's output consistent with objdump. Since objdump always outputs long immediate values as hex, then it doesn't need to care about negative signs. QEMU seems to prefer decimal, though, and so does llvm-objdump, which outputs values for these instructions in the range 0..1048575. So, I guess this patch is making QEMU consistent with llvm-objdump.

Back to making suggestions for this patch...

1. The commit message should probably say something along the lines of what I just wrote in the preceding paragraph to better explain the motivation.

2. Unless I'm missing something, then this patch should also address AUIPC.

Thanks,
drew

#10 lore raw

post-nack, one further comment:

One could argue that this change also aligns QEMU with supporting tools (as Andrew observed), and it makes sense to merge this change into QEMU until those tools update to supporting signed decimal numbers with immediates.

As it is, both GNU assembler and the LLVM integrated assembler (or llvm-mc) throws an error with examples such as
auipc s0, -17

On the other hand, I have only seen this problem with the output of the COLLECT plug-in, not (as yet) with QEMU execution proper.
If the problem is confined to COLLECT, perhaps the argument for aligning with other tools is not as strong.

In the meantime, I have adjusted my change locally to include AUIPC, and written a substantive, and I hope, clear commit description. If you would like me to resubmit a patch with this updated change, please let me know.

Quoted 114 lines · Richard Bagley

NACK

We have established that the change is a workaround for a bug in the assembler.
I withdraw the merge request.

Thank you for this careful review.

Quoted 102 lines · Andrew Jones
Quoted 3 lines · Andrew Jones
Quoted 2 lines · Andrew Jones
Quoted 1 line · Palmer Dabbelt

On Thu, 10 Aug 2023 08:31:46 PDT (-0700), [email protected]

Quoted 2 lines · wrote:
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines · Richard Bagley
Quoted 1 line

The recent commit 36df75a0a9 corrected one aspect of LUI

disassembly

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

by recovering the immediate argument from the result of LUI

with a

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

shift right by 12. However, the shift right will left-fill with

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

sign. By applying a mask we recover an unsigned representation

of the

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

20-bit field (which includes a sign bit).

Example:
0xfffff000 >> 12 = 0xffffffff
0xfffff000 >> 12 & 0xfffff = 0x000fffff

Fixes 36df75a0a9 ("riscv/disas: Fix disas output of upper

immediates")

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

disas/riscv.c static void format_inst(char *buf,

Quoted 10 lines
---
 disas/riscv.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
disas/riscv.c +0 -0
index 4023e3fc65..690eb4a1ac 100644
@@ -4723,9 +4723,12 @@ static void format_inst(char *buf,

size_t buflen, size_t tab, rv_decode *dec)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

disas/riscv.c static void format_inst(char *buf, · line 4727

Quoted 8 lines
disas/riscv.c +2 -3
             break;
         case 'U':
             fmt++;
-            snprintf(tmp, sizeof(tmp), "%d", dec->imm >> 12);
-            append(buf, tmp, buflen);
-            if (*fmt == 'o') {
+            if (*fmt == 'i') {
+                snprintf(tmp, sizeof(tmp), "%d", dec->imm >>

12 & 0xfffff);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Why are we correcting LUI's output, but still outputting

sign-extended

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

values for AUIPC?

We can't assemble 'auipc a1, 0xffffffff' or 'auipc a1, -1'

without getting

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Error: lui expression not in range 0..1048575

(and additionally for 0xffffffff)

Error: value of 00000ffffffff000 too large for field of 4 bytes

at 0000000000000000

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

either.

(I see that the assembler's error messages state 'lui', but I was

trying

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

'auipc'.)

I'm using as from gnu binutils 2.40.0.20230214.

(And, FWIW, I agree with Richard Henderson that these

instructions should

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

accept negative values.)

I'm kind of lost here, and you saying binutils rejects this

syntax? If

Quoted 1 line
Quoted 1 line
Quoted 1 line

that's the case it's probably just an oversight, can you file a bug

in

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

binutils land so folks can see?

Will do.

https://sourceware.org/bugzilla/show_bug.cgi?id=30746

But, to try to bring this thread back to the patch under review. While the binutils BZ may address our preferred way of providing immediates to the assembler, this patch is trying to make QEMU's output consistent with objdump. Since objdump always outputs long immediate values as hex, then it doesn't need to care about negative signs. QEMU seems to prefer decimal, though, and so does llvm-objdump, which outputs values for these instructions in the range 0..1048575. So, I guess this patch is making QEMU consistent with llvm-objdump.

Back to making suggestions for this patch...

1. The commit message should probably say something along the lines of what I just wrote in the preceding paragraph to better explain the motivation.

2. Unless I'm missing something, then this patch should also address AUIPC.

Thanks,
drew

#11 lore raw
Quoted 15 lines · Richard Bagley

post-nack, one further comment:

One could argue that this change also aligns QEMU with supporting tools (as Andrew observed), and it makes sense to merge this change into QEMU until those tools update to supporting signed decimal numbers with immediates.

As it is, both GNU assembler and the LLVM integrated assembler (or llvm-mc) throws an error with examples such as
auipc s0, -17

On the other hand, I have only seen this problem with the output of the COLLECT plug-in, not (as yet) with QEMU execution proper.
If the problem is confined to COLLECT, perhaps the argument for aligning with other tools is not as strong.

Quoted 4 lines

In the meantime, I have adjusted my change locally to include AUIPC, and written a substantive, and I hope, clear commit description. If you would like me to resubmit a patch with this updated change, please let me know.

Since the patch is ready for posting, then it might as well be posted (even if it may not get merged right away). If the issue arises again, then we can refer to the latest proposed patch, which will be preserved in the mail archives.

Thanks,
drew

#12 lore raw

I have realized that *the patch is indeed a fix*, not a workaround.

In fact, the argument to LUI and AUIPC in assembly *must* be a number between [0x0, 0xfffff].
RISC-V Assembly Programmer's Manual : Load Upper Immediate's Immediate
<https://github.com/riscv-non-isa/riscv-asm-manual/blob/master/riscv-asm.md#load-upper-immediates-immediate>
Signed decimal numbers are programmed as their two's complement.

I checked: neither GCC nor LLVM will assemble

Quoted 1 line

lui x1, -4

The LLVM compiled models the arguments to LUI and AUIPC as UIMM (unsigned immediate) or UIMM20 (20 bit unsigned immediate).

I should have checked this from the start. I jumped to the conclusion that both formats (signed decimal, two's complement) for negative arguments should be supported, and that I was encountering a bug.
I apologize to all for the unnecessary back-and-forth.

I don't yet see a reason why llvm and gcc could not support a signed number in decimal format, perhaps requiring a pseudo-instruction.
This might be desirable, if only in support of assembly programming. On the other hand, it is easy to make the conversion to a two's-complement number.

Richard

Quoted 31 lines · Andrew Jones
Quoted 3 lines · Richard Bagley

post-nack, one further comment:

One could argue that this change also aligns QEMU with supporting tools

(as

Quoted 4 lines

Andrew observed), and it makes sense to merge this change into QEMU until those tools update to supporting signed decimal numbers with immediates.

As it is, both GNU assembler and the LLVM integrated assembler (or

llvm-mc)

Quoted 8 lines

throws an error with examples such as
auipc s0, -17

On the other hand, I have only seen this problem with the output of the COLLECT plug-in, not (as yet) with QEMU execution proper.
If the problem is confined to COLLECT, perhaps the argument for aligning with other tools is not as strong.

Quoted 4 lines

In the meantime, I have adjusted my change locally to include AUIPC, and written a substantive, and I hope, clear commit description. If you would like me to resubmit a patch with this updated change, please let me know.

Since the patch is ready for posting, then it might as well be posted (even if it may not get merged right away). If the issue arises again, then we can refer to the latest proposed patch, which will be preserved in the mail archives.

Thanks,
drew

#13 lore raw

LUI and AUIPC would naturally be used to build an address.
If you want to supply a signed decimal number to build an immediate, there is always the pseudo-instruction LI.

Quoted 65 lines · Richard Bagley

I have realized that *the patch is indeed a fix*, not a workaround.

In fact, the argument to LUI and AUIPC in assembly *must* be a number between [0x0, 0xfffff].
RISC-V Assembly Programmer's Manual : Load Upper Immediate's Immediate
<https://github.com/riscv-non-isa/riscv-asm-manual/blob/master/riscv-asm.md#load-upper-immediates-immediate>
Signed decimal numbers are programmed as their two's complement.

I checked: neither GCC nor LLVM will assemble

Quoted 1 line

lui x1, -4

The LLVM compiled models the arguments to LUI and AUIPC as UIMM (unsigned immediate) or UIMM20 (20 bit unsigned immediate).

I should have checked this from the start. I jumped to the conclusion that both formats (signed decimal, two's complement) for negative arguments should be supported, and that I was encountering a bug.
I apologize to all for the unnecessary back-and-forth.

I don't yet see a reason why llvm and gcc could not support a signed number in decimal format, perhaps requiring a pseudo-instruction. This might be desirable, if only in support of assembly programming. On the other hand, it is easy to make the conversion to a two's-complement number.

Richard

Quoted 33 lines · Andrew Jones
Quoted 3 lines · Richard Bagley

post-nack, one further comment:

One could argue that this change also aligns QEMU with supporting tools

(as

Quoted 1 line

Andrew observed), and it makes sense to merge this change into QEMU

until

Quoted 3 lines

those tools update to supporting signed decimal numbers with immediates.

As it is, both GNU assembler and the LLVM integrated assembler (or

llvm-mc)

Quoted 11 lines

throws an error with examples such as
auipc s0, -17

On the other hand, I have only seen this problem with the output of the COLLECT plug-in, not (as yet) with QEMU execution proper.
If the problem is confined to COLLECT, perhaps the argument for aligning with other tools is not as strong.

In the meantime, I have adjusted my change locally to include AUIPC, and written a substantive, and I hope, clear commit description. If you would like me to resubmit a patch with this updated change,

please

Quoted 1 line

let me know.

Since the patch is ready for posting, then it might as well be posted (even if it may not get merged right away). If the issue arises again, then we can refer to the latest proposed patch, which will be preserved in the mail archives.

Thanks,
drew

#14 lore raw
In reply to Richard Bagley
Quoted 6 lines · Richard Bagley

I have realized that *the patch is indeed a fix*, not a workaround.

In fact, the argument to LUI and AUIPC in assembly *must* be a number between [0x0, 0xfffff].
RISC-V Assembly Programmer's Manual : Load Upper Immediate's Immediate
<https://github.com/riscv-non-isa/riscv-asm-manual/blob/master/riscv-asm.md#load-upper-immediates-immediate>

I think that's just documenting the current behavior, but the behavior (not accepting a signed decimal number for a signed immediate) doesn't appear to be justified, so I think my suggestion in [1] still stands. That said, I don't really have much of a horse in this race so if somebody comes along and closes that BZ with a simple justification of "we, the people that work on this stuff, agreed we prefer the range [0x0, 0xfffff]", then I won't argue.

[1] https://sourceware.org/bugzilla/show_bug.cgi?id=30746

Thanks,
drew

Quoted 55 lines

Signed decimal numbers are programmed as their two's complement.

I checked: neither GCC nor LLVM will assemble

Quoted 1 line

lui x1, -4

The LLVM compiled models the arguments to LUI and AUIPC as UIMM (unsigned immediate) or UIMM20 (20 bit unsigned immediate).

I should have checked this from the start. I jumped to the conclusion that both formats (signed decimal, two's complement) for negative arguments should be supported, and that I was encountering a bug.
I apologize to all for the unnecessary back-and-forth.

I don't yet see a reason why llvm and gcc could not support a signed number in decimal format, perhaps requiring a pseudo-instruction.
This might be desirable, if only in support of assembly programming. On the other hand, it is easy to make the conversion to a two's-complement number.

Richard

Quoted 31 lines · Andrew Jones
Quoted 3 lines · Richard Bagley

post-nack, one further comment:

One could argue that this change also aligns QEMU with supporting tools

(as

Quoted 4 lines

Andrew observed), and it makes sense to merge this change into QEMU until those tools update to supporting signed decimal numbers with immediates.

As it is, both GNU assembler and the LLVM integrated assembler (or

llvm-mc)

Quoted 8 lines

throws an error with examples such as
auipc s0, -17

On the other hand, I have only seen this problem with the output of the COLLECT plug-in, not (as yet) with QEMU execution proper.
If the problem is confined to COLLECT, perhaps the argument for aligning with other tools is not as strong.

Quoted 4 lines

In the meantime, I have adjusted my change locally to include AUIPC, and written a substantive, and I hope, clear commit description. If you would like me to resubmit a patch with this updated change, please let me know.

Since the patch is ready for posting, then it might as well be posted (even if it may not get merged right away). If the issue arises again, then we can refer to the latest proposed patch, which will be preserved in the mail archives.

Thanks,
drew