arduino / arduino/ArduinoCore-samd

delayMicroseconds in complicated code causes compilation to fail

Open
#614 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
502
Forks
740
PR merge metrics
No merged PRs in 30d

Description

Hello!

Compilation fails with the error message:
```
maxtron_translator.elf.ltrans0.s: Assembler messages:
maxtron_translator.elf.ltrans0.s:14826: Error: lo register required -- `sub r8,#1'
```
In this code:
```
__asm__ __volatile__(
"1: \n"
" sub %0, #1 \n" // substract 1 from %0 (n)
" bne 1b \n" // if result is not 0 jump to 1
: "+r" (n) // '%0' is n variable with RW constraints
```
%0 gets assigned to a high register (r8-r15) in my case. I suspect the reason is that gcc embeds this assembly in a complicated code using many registers (which is caused by LTO combining large parts of the code into single "functions"). Cortex-M0 doesn't support the sub/subs instruction with every register/immediate combination.

Modifying the register constraints so that only a lower register is suitable for %0 solves the problem for me:
```
__asm__ __volatile__(
"1: \n"
" sub %0, #1 \n" // substract 1 from %0 (n)
" bne 1b \n" // if result is not 0 jump to 1
: "+l" (n) // '%0' is n variable with RW constraints (only Lo register is allowed)
```

SAMD core version: 1.8.9
GCC version: 9.3.1 (I need some C++17 features)

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the delayMicroseconds implementation in the ArduinoCore-samd source and inspect the inline assembly constraint for the loop counter. Reproduce the Cortex-M0 compilation failure with LTO enabled, then verify that the affected code compiles successfully when the counter is restricted to a suitable register.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers, embedded-iot
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.