Bug 22699

Summary: sh*: Immediate signedness is incorrect in and/or/tst/xor #imm
Product: binutils Reporter: Andrew Church <achurch+sourceware>
Component: binutilsAssignee: Not yet assigned to anyone <unassigned>
Status: UNCONFIRMED ---    
Severity: normal CC: fenugrec
Priority: P2    
Version: 2.29   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

Description Andrew Church 2018-01-11 06:10:53 UTC
When objdump disassembles SuperH (sh2, sh4, etc.) instructions of the form "AND/OR/TST/XOR #imm,R0", it treats the immediate as a signed 8-bit value, but in fact the value is unsigned.

To reproduce: echo 'tst #128,r0' |sh4-as -o foo.o; sh4-objdump -d foo.o

Expected output:
   0:   80 c8           tst     #128,r0

Actual output:
   0:   80 c8           tst     #-128,r0
Comment 1 fenugrec 2018-01-25 16:41:21 UTC
Correct. To recap:

1) some opcodes need the sign-extended 8-bit immediate value:
- add #imm, Rn (OK)
- cmp/eq #imm, R0 (OK)
- mov #imm, Rn (OK)

2) others take the zero-extended imm8 :
- bitwise logic operators {and, and.b,...} : broken
- trapa : broken

3) some I haven't personally checked :
- ldrc / setrc
- other DSP , SH2A, SH4A opcodes

I would suggest reworking the relevant entries in opcodes/sh-opc.h (maybe replace IMM0_8 by IMM0_8S and IMM0_8U to ensure every occurence is verified manually; and add the proper handling in opcodes/sh-dis.c

Is there a regression test for this already ?