Skip to content

[arch][arm] Use conditional l/r register constraint in MMIO macros to fix Thumb-1 assembly#514

Closed
Copilot wants to merge 3 commits into
masterfrom
copilot/fix-arch-mmio-write32-assembler-error
Closed

[arch][arm] Use conditional l/r register constraint in MMIO macros to fix Thumb-1 assembly#514
Copilot wants to merge 3 commits into
masterfrom
copilot/fix-arch-mmio-write32-assembler-error

Conversation

Copilot AI commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

On ARMv6-M (Cortex-M0+) and ARMv8-M Baseline, Thumb-1 16-bit encodings of str/strh/strb/ldr/ldrh/ldrb only have a 3-bit register field — restricting operands to R0–R7. The "r" constraint allows GCC to allocate R8–R15 (e.g. R12/ip), causing:

Error: lo register required -- 'str ip,[r0]'

Changes

  • arch/arm/include/arch/reg.h: Introduce _ARCH_MMIO_REG_CONSTRAINT which expands to "l" when building for Thumb-1 (defined(__thumb__) && !defined(__thumb2__)) and to "r" otherwise. Use this in all six _ARCH_MMIO_READ8/16/32 and _ARCH_MMIO_WRITE8/16/32 macros.

The "l" constraint is applied only where required (Thumb-1), while ARM state and Thumb-2 builds use the unrestricted "r" constraint to avoid unnecessary register pressure.

Copilot AI changed the title [WIP] Fix _ARCH_MMIO_WRITE32 to prevent assembling errors on Cortex-M0+ [arch][arm] Use l register constraint in MMIO macros to fix Thumb-1 assembly Jun 17, 2026
Copilot AI requested a review from travisg June 17, 2026 17:48

@travisg travisg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot can you update the commit message to mention that it fixes issue #513

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates ARM MMIO accessor inline-asm constraints to avoid Thumb-1 assembler failures when GCC allocates high registers (R8–R15) for ldr*/str* operands, which are not encodable in Thumb-1’s 3-bit register fields.

Changes:

  • Switch MMIO read/write inline-asm register constraints from "r"/"=r" to "l"/"=l" in arch/arm/include/arch/reg.h to force low registers (R0–R7) in Thumb mode.
  • Apply the constraint change consistently across 8/16/32-bit load and store macros.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread arch/arm/include/arch/reg.h Outdated
Comment on lines +18 to +22
@@ -19,25 +19,25 @@

#define _ARCH_MMIO_READ8(addr) ({ \
uint8_t val; \
__asm__ volatile("ldrb %0, %1" : "=r"(val) : "m"(*(addr)) : "memory"); \
__asm__ volatile("ldrb %0, %1" : "=l"(val) : "m"(*(addr)) : "memory"); \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a fair request.

@copilot can you make this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in accc632. _ARCH_MMIO_REG_CONSTRAINT now expands to "l" only when defined(__thumb__) && !defined(__thumb2__), and falls back to "r" for ARM state and Thumb-2 builds.

@travisg travisg marked this pull request as ready for review June 17, 2026 19:18
…513

Use the 'l' constraint (lo registers, R0-R7) only when building for Thumb-1
Thumb-1 16-bit str/ldr encodings only have a 3-bit register field.

Fall back to 'r' for ARM state and Thumb-2 builds where the full register
file is available, avoiding unnecessary register pressure in those cases.

Copilot AI commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

@copilot can you update the commit message to mention that it fixes issue #513

Updated in accc632 — commit message now includes fixes #513.

Copilot AI changed the title [arch][arm] Use l register constraint in MMIO macros to fix Thumb-1 assembly [arch][arm] Use conditional l/r register constraint in MMIO macros to fix Thumb-1 assembly Jun 17, 2026
Copilot AI requested a review from travisg June 17, 2026 19:26
@travisg travisg closed this Jun 18, 2026
@travisg travisg deleted the copilot/fix-arch-mmio-write32-assembler-error branch June 18, 2026 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants