5
votes

Consider inline assembly like this:

uint64_t flags;
asm ("pushf\n\tpop %0" : "=rm"(flags) : : /* ??? */);

Nonwithstanding the fact that there is probably some kind of intrinsic to get the contents of RFLAGS, how do I indicate to the compiler that my inline assembly clobbers one quadword of memory at the top of stack?

1
AFAIK, you can't. I think the only safe way to write this is by modifying rsp around the pushf/pop so you don't step on the red zone. (Use add -128 / sub -128 so they can use an imm8 encoding). And then of course the output constraint has to be "=r", because a memory operand could use an rsp-relative addressing mode. Avoiding the red-zone is the best anyone's been able to come up with in discussions of the same issue on previous SO questions. - Peter Cordes
You could also save/restore [rsp-8] into a register, but that seems worse than modifying rsp, even if it makes the stack engine insert an extra uop. - Peter Cordes
There is no way to tell extended asm that you are clobbering the stack. That said, what exactly are you trying to accomplish? There might be some other tricks that do what you need (maybe lahf?). - David Wohlferd
@DavidWohlferd I was thinking about how inline assembly that clobbers part of the stack would work and this is a good example for motivation as pushf is the only way to get the entire RFLAGS register. - fuz
related: you can't tell gcc that you want to clobber the whole red zone either (except by compiling the whole file or function with -mno-red-zone). So to make a function call in inline-asm, you have to jump through hoops: See stackoverflow.com/questions/37502841/… and stackoverflow.com/questions/37639993/…. (Calling functions from inline asm is just a bad idea, but people trying to learn asm using inline-asm keep wanting to do it.) - Peter Cordes

1 Answers

1
votes

As far as I am concerned, this is currently not possible.