<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/59577>59577</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[avr] Wrong code: Incorrect resolution of operand modifier
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
sprintersb
</td>
</tr>
</table>
<pre>
Compile with
` --target=avr -mmcu=atmega8 -Os -c`
the following C code:
```c
void func (void)
{
__asm volatile ("ldi r16, -hi8(0x3ff)");
}
```
Resulting disassembly from `x.o` reads:
```none
Disassembly of section .text:
00000000 <func>:
0: 0c ef ldi r16, 0xFC ; 252
2: 08 95 ret
```
which is wrong: It's `hi8(0x3ff) = 0x3` and `-0x3 = -hi8(0x3ff) = 0xfd` in 2's complement.
What the assembler appears to be computing is `hi8(-(val))`, at least that's what follows from code for address computations into static arrays like in:
```c
extern int var[];
// Link with -Wl,--defsym,var=0x3ff
void func (int n)
{
return var[n];
}
```
where `x.s` reads:
```
func:
lsl r24
rol r25
subi r24, -lo8(var) ; should be lo8(-(x))
sbci r25, -hi8(var) ; should be hi8(-(x))
...
```
Notice that the semantically correct expressions are `lo8(-x)` and `hi8(-x)` because you want to subtract the negative of a 16-bit value, cf. for example what avr-gcc is doing.
Strangely, that offset computation is correct even when the negation produces a borrow from bit 7 to bit 8.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8lU2PozgTgH-NcykRERO-Djl0J2-kkV7tSruHPo6MKcA7xo5sk5B_vypM0t0zPYtaNK7Udz0FwnvVG8QDy19ZftqIKQzWHfzFKRPQ-WbT2PZ-ONrxojTCTYWBpSeWvrAihSQJwvUYWHYSVwfJOMqJnsOIvagg-dNDIlmRRoswIHRWa3tTpocjSNsiy16e7uKfjOerVS10k5HAeEUHxutVs3yNDwAA378LP8LVahEoPcYrxrluFbhdwfgRkkFVjFfpnHUdeeCc7tnr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