Skip to content

inline assembly: enable GCC's %= syntax #23740

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

Merged
merged 1 commit into from
May 2, 2025
Merged

Conversation

samy-00007
Copy link
Contributor

GCC uses the %= syntax to generate a unique number for each asm statement (see on gcc's docs). LLVM uses ${:uid} to do the same.

This PR adds the syntax, since it is not currently possible to use %= or ${:uid} in zig's inline assembly. Also, it is currently impossible to use ${:comment} or ${:private} described here on llvm's docs, and this PR does not change that.

I am unsure if this change requires tests, and how to even test it.

@samy-00007 samy-00007 changed the title inline assembly: implement GCC's %= syntax inline assembly: enable GCC's %= syntax May 1, 2025
@alexrp
Copy link
Member

alexrp commented May 1, 2025

Might want to check if the C backend requires changes for this too.

@samy-00007
Copy link
Contributor Author

I am not very familiar with zig's compiler internals, but does the c backend emit c code ? If yes, then the %= syntax is also supported by clang, so no change should be necessary, I think. However, I am guessing this syntax will not work in the x86 backend.

@alexrp
Copy link
Member

alexrp commented May 2, 2025

What I mean is, just try to put a Zig program using this feature through zig build-exe -ofmt=c to make sure that it gets passed through as expected.

@samy-00007
Copy link
Contributor Author

samy-00007 commented May 2, 2025

I just tried, my program gets passed through as expected (%= stays in the inline asm, and both gcc and clang are able to compile and run the generated c file).

Also, out of scope of this PR, but while playing a bit with inline assembly, I noticed that zig allows to have the same register as both input and clobber, while clang and gcc disallow it.
For instance:

const in: usize = ...;
asm volatile ("" :: [in] "{rax}" (in) : "rax");

works in zig, and seems to behave as I would expect it to be (zig expects the value of rax to change after running the assembly). This is quite nice, since I don't need to manually restore the value of the register if the assembly does change it.

Unfortunately, it cannot be used with -ofmt=c, since it will just cause gcc and clang to complain about it ... Which means it is not possible currently to use this feature in the std since some tests use -ofmt=c.

@alexrp
Copy link
Member

alexrp commented May 2, 2025

Also, out of scope of this PR, but while playing a bit with inline assembly, I noticed that zig allows to have the same register as both input and clobber, while clang and gcc disallow it.

I guess you can file an issue for this if you feel like it, although #10761 is the long term plan.

@alexrp alexrp merged commit c0ec264 into ziglang:master May 2, 2025
17 of 18 checks passed
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.

2 participants