Skip to content

Reject GCC asm memory operands on register variables (ImportC) - #5206

Open
niy-ati wants to merge 4 commits into
ldc-developers:masterfrom
niy-ati:fix-4967-register-asm
Open

Reject GCC asm memory operands on register variables (ImportC)#5206
niy-ati wants to merge 4 commits into
ldc-developers:masterfrom
niy-ati:fix-4967-register-asm

Conversation

@niy-ati

@niy-ati niy-ati commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Summary

ImportC keeps register as STCregister, but GCC style inline asm with "m" or "=m" constraints takes the operand address during LDC codegen. That is illegal C (same as GDC rejecting it) and was not caught in semantic analysis because the frontend does not interpret asm constraint strings.

This rejects indirect memory asm operands that refer to a register variable in GccAsmStatement_toIR, using the same diagnostic as other ImportC address of register cases.

Fixes #4967.

Test plan

Added tests/dmd/fail_compilation/issue4967.c covering output and input "m" operands on a register int.

The frontend cannot diagnose m / =m constraints on register
variables; reject them in GccAsmStatement_toIR when building indirect
operands, matching C11 and GDC.

Fixes ldc-developers#4967.
niy-ati added 2 commits July 20, 2026 13:36
Do not fatal() after the first register+memory asm error so -verrors=0
can emit diagnostics for every illegal operand, matching GDC. Correct
TEST_OUTPUT line numbers for the two asm statements.
Comment thread gen/asm-gcc.cpp Outdated
}
if (registerMemError)
return;

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 can be more easily integrated in the loop below, where we already check indirect operands for lvalue-ness (and where we shouldn't call fatal() either), inlining checkRegisterMemoryAsmOperand() (no need for an extra function for that).

Comment thread tests/dmd/fail_compilation/issue4967.c Outdated

@kinke kinke Jul 26, 2026

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.

Other LDC-specific file additions to the DMD testsuite are named ldc_github_4967.c, to make clear that these are LDC-specific (incl. issue numbers).

Fold the ldc-developers#4967 check into the existing indirect-operand loop, drop the
helper and fatal() on operand errors, and rename the fail_compilation
test to ldc_github_4967.c.
@niy-ati
niy-ati force-pushed the fix-4967-register-asm branch 2 times, most recently from f601977 to 5340084 Compare July 26, 2026 18:03
@niy-ati
niy-ati requested a review from kinke July 27, 2026 17:06
@kinke

kinke commented Jul 27, 2026

Copy link
Copy Markdown
Member

The other helper was fine, I just meant removing one of the two. And the check can be integrated more nicely, no need for hasErrors etc. I'm probably quicker doing the changes myself, but this fix is of very low priority.

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.

importC: GCC-style inline asm accepts taking the address of a register variable

2 participants