Issue 176830
Summary [AVR] Register numbers mismatch with GCC, causes errors with AVR-libc
Labels libc
Assignees
Reporter GregTheMadMonk
    Hello!

I've been trying to use LLVM (Clang++) to develop for ATMega32u4 and have stumbled upon being unable to use `libc++`'s `<cstring>`. The error was:
```
/usr/lib/gcc/avr/15.1.0/../../../../avr/include/string.h:270:54: error: unknown register name '30' in asm
  270 |          : "=r" (__r24) : "0" (__hay), "r" (__r22) : "30", "31");
      | ^
/usr/lib/gcc/avr/15.1.0/../../../../avr/include/string.h:669:16: error: unknown register name '30' in asm
  669 |              : "30", "31", "memory");
      |                ^
```
which is in the AVR-`libc`'s `string.h` implementation where inline assembly is used to define `strchr`. `libc++` chain-includes this file automatically with

https://github.com/llvm/llvm-project/blob/db48679835d316a420fece71f17a14633497cce3/libcxx/include/string.h#L63-L65

There is no way to disable this, even though (apparently?) all of these functions should be available as Clang's bulitins if another `string.h` isn't present in further include directories:

https://github.com/llvm/llvm-project/blob/db48679835d316a420fece71f17a14633497cce3/libcxx/include/string.h#L94-L96

GCC appears to treat all 8-bit registers separately, so `"30"` and `"31"` correspond to `r30` and `r31`, and `"28"` corresponds to `r28`:

https://github.com/gcc-mirror/gcc/blob/412467a47b9f6f52cc0bfbd8786c5ee5a2c16e7f/gcc/config/avr/avr.h#L424-L429

While LLVM seems to have chosen to use register indices 26+ for register pairs (`"26"` = `r26:r27` = `X`, `"27"` = `r28:r29` = `Y`, `"28"` = `r30:r31` = `Z`, ...)

https://github.com/llvm/llvm-project/blob/9b64509862cbda3db7d4bc03e1f27ed3387e9ee8/clang/lib/Basic/Targets/AVR.h#L78-L98

This creates a discrepancy that makes AVR inline assembly incompatible between GCC and Clang.

Is this intentional? Could this maybe be changed to provide compatibility with AVR libc since it's being included automatically by Clang anyway?

And if it will stay like this, could there maybe be an option to force `libc++`'s `string.h` to use provided builtins instead of going further? Or are they incompatible with AVR?
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to