sail-riscv icon indicating copy to clipboard operation
sail-riscv copied to clipboard

C.ADDI disassembly inaccurate when immediate value is negative

Open scottj97 opened this issue 5 years ago • 7 comments

The opcode 0x1541 should decode into c.addi x10, -16 (according to Spike). Sail is reporting it as c.addi x10, 48.

It seems to execute correctly; if x10 is 0, it gets written with 0xffffffff_fffffff0. It's only the immediate value shown in the disassembly that looks incorrect.

scottj97 avatar Sep 17 '19 23:09 scottj97

Any progress on this one? It's been 2 years....

allenjbaum avatar Oct 01 '21 01:10 allenjbaum

This is not a problem with the disassembly of C.ADDI; It appears to be a problem for every instruction with an immediate value.. @jrtc27 We can fix this particular mnemonic pretty easily by defining an signed variant - but it is a fair number of uses in some dozen or so files. Should we do this, or suffer along with a different disassembly? @ben-marshall: the crypto scalar has a bunch of immediate fields. Are the signed or unsigned or neither (in which case they're effectively unsigned)

allenjbaum avatar Dec 15 '21 05:12 allenjbaum

Looking a bit deeper: the following instructions improperly decode signed constants as unsigned: [F]Loads/Stores, (LUI,AUIPC), JAL[R], btype ops (B**), Addi[w]/xori/ori/andi/slti[u], c.addi[w/16sp], c.[l[u]/and]i, c.j[al], c.bneq, c.beqz

I am assuming that crypto constants, hints, CSR#s, fences and shift amounts are not sign extended. There a bunch of compressed immediates that are specifically zero extended

That's 19 lines in 3 files: base, cext, fext (plus defining signed versions of the hex_bits_n functions, or explicitly sign extending in those 19 lines

allenjbaum avatar Dec 15 '21 08:12 allenjbaum

That's 19 lines in 3 files: base, cext, fext

@allenjbaum It's remarkable that this remained undetected for several years.

martinberger avatar Dec 15 '21 19:12 martinberger

We've known about it for years, just not a priority as it only affects the human-readable disassembly. If someone wants to fix it then great, but the string<->int handling is a bit nasty, so be warned...

jrtc27 avatar Dec 15 '21 19:12 jrtc27

You're right; it is rather...opaque (not good for something that's supposed to be readable and nasty code should be at least comments) and the hex_bits_n function don't seem to have anything to do with hex; it looks strictly binary to decimal string. I'd really prefer if it was hex (I'm not adept at reading Sail, so I could be misreading that... and no comments ....).

allenjbaum avatar Dec 27 '21 06:12 allenjbaum

I tried using the dec_str function in string.sail, and it seems to work well. I'm wondering if it would be possible to solve this issue by making the disassembly display an optional feature or directly changing it to decimal representation. Admittedly, there would be quite a bit of work involved in modifying the corresponding references, but I would like to participate in completing this task. I'm unsure if there is a maintainer willing to accept my pull request (PR), but if it's possible, I will provide detailed explanations of the modifications in my PR. Or is there other thoughts on my proposal?

KotorinMinami avatar Apr 16 '24 23:04 KotorinMinami

Should be closed by #456

jordancarlin avatar May 28 '24 19:05 jordancarlin