Skip to content
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

Request: flag output #14

Open
nicbn opened this issue Aug 23, 2020 · 8 comments
Open

Request: flag output #14

nicbn opened this issue Aug 23, 2020 · 8 comments

Comments

@nicbn
Copy link

nicbn commented Aug 23, 2020

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.

@Amanieu
Copy link
Member

Amanieu commented Aug 23, 2020

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.

@nicbn
Copy link
Author

nicbn commented Aug 23, 2020

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.

@programmerjake
Copy link
Member

Isn't this what GCC uses asm goto for?
https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#GotoLabels

@Amanieu
Copy link
Member

Amanieu commented Aug 23, 2020

@Amanieu
Copy link
Member

Amanieu commented Aug 23, 2020

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.

@mina86
Copy link

mina86 commented Sep 27, 2024

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")?

@Lokathor
Copy link

It's just better for different things to be called different names.

@mina86
Copy link

mina86 commented Sep 28, 2024

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.

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

No branches or pull requests

5 participants