Skip to content

c backend: fix some inline assembly constraints #23794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

samy-00007
Copy link
Contributor

This PR adds supports for:

How the last one works: when an input specifies a register which is in the clobber list, I remove said register from the clobber list and input for the input list, and add a new output, marked as +r, and creates a new variable that copy the value.

So, this zig code

pub fn main() void {
    var in: usize = 20;
    _ = ∈
    asm volatile ("":: [in] "{rax}" (in) : "rax");
    foo(in);
}

fn foo(a: usize) void {
    _ = a;
}

produces for the main function

static void main_main__1005(void) {
   uintptr_t t1;
   uintptr_t t0;
   /* file:10:2 */
   t0 = (uintptr_t)20ul;
   /* dbg_var_ptr:in */
   /* file:15:2 */
   t1 = t0;
   register uintptr_t t2 __asm("rax") = t1;
   __asm volatile("": [in]"+r"(t2)::);
   /* file:16:5 */
   t1 = t0;
   /* file:16:5 */
   main_foo__1006(t1);
   return;
}

So, if the assembly uses in as a clobber, it will only change the local variable and not the actual variable.

I have not found a better way to go through the list of clobbers than adding multiple loops ...
By the way, is there a way to add tests for that ?

Also if someone could fix the x86 backend to support %= . I believe it could as simple as adding a counter that increments each time an inline assembly block is lowered to assembly, but I am not confident enough to edit that part of the codebase.

now supports:
- "+" as output constraint
- more than one symbol when register is specified
- register as both input and clobber
@alexrp
Copy link
Member

alexrp commented May 5, 2025

cc @jacobly0

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.

C backend produce incorrect code for inline assembly with input as clobber
2 participants