Request: flag output
Opened this issue · 8 comments
To branch off a flag, this is the current way (x86):
let mut r: u8;
asm!(
"cmp {a}, {b}",
"sete {r}",
r = out(reg_byte) r,
a = in(reg) a,
b = in(reg) b,
);
let r: bool = mem::transmute(r); // Matching r and using unreachable_unchecked() also works
if r { ... } else { ... }This causes a test on the r register, and then, in most cases, a jump conditional is called. Ideally, this whole r register would not be a thing and we could just jump conditional right after calling the asm!.
This optmization, however, can not happen because there are no ways of outputting a flag from asm. GCC has an extension which allows it to do this very thing. The desired syntax would be something along the lines of out("z") r, with r: bool.
If we do end up supporting this in the future then it would most likely be done through a special operand type such as out_flag("z").
My main concern with this feature is that it is only supported on x86 and neither Clang nor GCC have any plans to extend it to other architectures.
Assembly is already as architecture-dependent as anything can get, so I don't think it would be a problem to only support this on certain architectures. If we did end up supporting flag outputs for all architectures, it would still be interesting as preserve_flags could be removed in favor of explicit flag clobbering.
A special operand wouldn't be bad, but it would be interesting if the semantics for flags were the same as for registers, i.e. one can set the flag based on a boolean by using in, and by extension inout, lateout and inlateout could all be defined.
Isn't this what GCC uses asm goto for?
https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#GotoLabels
A special operand wouldn't be bad, but it would be interesting if the semantics for flags were the same as for registers, i.e. one can set the flag based on a boolean by using in, and by extension inout, lateout and inlateout could all be defined.
LLVM and GCC only support this as an output, and multiple flag outputs are not allowed. It also directly conflicts with preserves_flags. Since this behaves differently from a normal register operand, a special operand type makes sense.
If we do end up supporting this in the future then it would most likely be done through a special operand type such as out_flag("z").
What’s wrong with out("z") or out("zf")?
It's just better for different things to be called different names.
Are they different things? out(reg) var specifies that at the end of asm block execution value of reg is copied to var. out("zf") var would specify that value of zero flag is copied to var.