<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/82598>82598</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Wrong code generation with inline assembly
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
royger
</td>
</tr>
</table>
<pre>
Hello,
Given the following example:
```
unsigned int func(uint8_t t)
{
return t;
}
static void bar(uint8_t b)
{
int ret_;
register uint8_t di asm("rdi") = b;
register unsigned long si asm("rsi");
register unsigned long dx asm("rdx");
register unsigned long cx asm("rcx");
register unsigned long r8 asm("r8");
register unsigned long r9 asm("r9");
register unsigned long r10 asm("r10");
register unsigned long r11 asm("r11");
asm volatile ( "call %c[addr]"
: "+r" (di), "=r" (si), "=r" (dx),
"=r" (cx), "=r" (r8), "=r" (r9),
"=r" (r10), "=r" (r11), "=a" (ret_)
: [addr] "i" (&(func)), "g" (func)
: "memory" );
}
void foo(unsigned int a)
{
bar(a);
}
```
clang will generate the following assembly with -O2:
```
func: # @func
movl %edi, %eax
retq
foo: # @foo
callq func
retq
```
Note how the cast (truncation) to uint8_t is not done anywhere (either in the caller or the callee), and hence the full value of 'a' gets passed into 'func()'. Gcc however explicitly uses movzbl:
```
func:
movzbl %dil, %eax
ret
foo:
call func
ret
```
This has been raised with the psABI group at:
https://groups.google.com/g/x86-64-abi/c/h7FFh30oS3s
And it seems like the consensus is that the callee shouldn't assume registers to be zero-extended for types < 32 bits.
Godbolt link with the example: https://godbolt.org/z/qdodj64hK
Thanks, Roger.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUll-v4jYTxj-NuRmBwoRAcsHF4SD2fVWpldqVerly4iHxrmOztsOf8-krOwFyKLvdgxAktn-P7Wc8k3DnZK2J1izbsGw74Z1vjF1bc6nJTkojLuv_kVKG4StLtix56X8_ySNp8A3B3ihlTlLXQGfeH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