Issue 243133
Summary [Mips] Wrong address from %a on a register operand in inline asm
Labels new issue
Assignees
Reporter mu-mu-mu
    On MIPS, the `%a` operand modifier on a register operand that holds a pointer prints a memory reference with a wrong, non-zero offset (e.g. `lw $2, 12($4)` instead of `lw $2, 0($4)`), so the asm loads from the wrong address.

### Reproducer

```c
#include <stdio.h>

int a[8] = {0, 1, 2, 3, 4, 5, 6, 7};

int load(int *p)
{
 int r;
    __asm__("lw %0, %a1" : "=r"(r) : "r"(p) : "memory");
 return r;
}

int main(void)
{
    printf("%d\n", load(&a[0]));    /* expected 0 */
}
```

### Actual behavior

```console
$ clang --version | head -1
clang version 23.1.3 (https://github.com/llvm/llvm-project 0d261d1ca552c95a8f007e061c787ac7132fbcbc)
$ clang --target=mips-linux-gnu -static test.c -o test
$ qemu-mips ./test
3
```

`load` with `-S -O0` (directives omitted):

```
load:
        addiu   $sp, $sp, -24
 sw      $ra, 20($sp)
        sw      $fp, 16($sp)
        move    $fp, $sp
        sw      $4, 12($fp)
        lw      $2, 12($fp)
        #APP
 lw      $2, 12($2)    <- inline assembly
        #NO_APP
        sw $2, 4($fp)
        lw      $1, 4($fp)
        sw      $1, 8($fp)
 lw      $2, 8($fp)
        move    $sp, $fp
        lw      $fp, 16($sp)
        lw      $ra, 20($sp)
        addiu   $sp, $sp, 24
 jr      $ra
        nop
```

`load` with `-S -O2` (directives omitted):

```
load:
        #APP
        lw      $2, 12($4)    <- inline assembly
        #NO_APP
        jr      $ra
 nop
```

### Expected behavior

The program prints `0`: the inline asm should be `lw $2, 0($2)` at -O0 and `lw $2, 0($4)` at -O2.

For reference, the program built with GCC prints `0`.

Reproduced with clang 23.1.3 and trunk (also with assertions; no assertion fires), for mips, mipsel and mips64el.

clang (trunk): https://godbolt.org/z/T9EzqevvG
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to