public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "gcc.hall at gmail dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug inline-asm/49611] Inline asm should support input/output of flags Date: Fri, 30 May 2014 11:38:00 -0000 [thread overview] Message-ID: <bug-49611-4-eTPL2UEHsx@http.gcc.gnu.org/bugzilla/> (raw) In-Reply-To: <bug-49611-4@http.gcc.gnu.org/bugzilla/> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49611 Jeremy <gcc.hall at gmail dot com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |gcc.hall at gmail dot com --- Comment #5 from Jeremy <gcc.hall at gmail dot com> --- It may not be possible, but perhaps a simpler thing might be for the asm() to notionally "return" a single boolean value which reflects ONE flag only. By default this could be the ZF flag (which is probably the most useful). It would not generate any extra code at all. Thus: if( asm( "test %%eax,%%eax" )) would emit: test eax,eax jz rather than the usual route involving setcc and a second test. The actual flag notionally returned could be changed with an attribute: __attribute__ ((asm_return(cc_carry))) __attribute__ ((asm_return(cc_overflow))) __attribute__ ((asm_return(cc_zero))) __attribute__ ((asm_return(cc_sign))) __attribute__ ((asm_return(cc_parity))) or shorter, for example: __attribute__ ((cc_carry)) The inverse condition is simply expressed with !asm(...) The new code would also allow: bool zero = asm( "test %%eax,%%eax" ); where the compiler would emit setz. or x = asm( "test %%eax,%%eax" ) ? 10 : 20; where the compiler might emit cmovz. It would not break any existing code because nothing yet expects a return from an asm, neither would it preclude exporting all the flags with "=cc" in the future. It would be a hard error if a flag is not supported by the current platform as code cannnot be generated. GCC does not examine the asm template string, if the flag is not set then the result is UB, just like use of an undefined variable. A final, more useful, example: // Implement "left *= right" reporting signed integer overflow #define checked_multiply(left,right) \ __attribute__ ((asm_return(cc_overflow))) \ asm( "imul %1,%0" : "+r" (left) : "r" (right) ) allowing an efficient implementation of: if( checked_multiply( a, b ) ) { handle overflow } Possible documentation follows... ======================== Return Value If you are using the asm_return attribute, you are informing the compiler that a comparison has already been done in your assembler code, and the flags have been set appropriately. The compiler can now use those flags to perform conditional jumps, conditional assignments, etc. Which conditional operator should be used is determined by which value is specified to asm_return. For example on i386: if( __attribute__(asm_return(cc_carry)) asm() ) printf ("Carry yes\n"); else printf ("Carry no\n"); indicates that the asm template has set a value in the Carry flag, and GCC should use the jc/jnc instructions to jump to the correct location. Similarly: int a = __attribute__(asm_return(cc_zero)) asm() ? 23 : 42; could use the i386 cmovz instruction to perform the assignment. And bool DidOverflow = __attribute__(asm_return(cc_overflow)) asm(); could use i386's seto. Which flags (if any) are supported depend upon your platform (see Asm Attributes). If no asm_return attribute is specified, it is an error to attempt to use the return value from the asm. It is acceptable for code to ignore the returned flags. ========================
next prev parent reply other threads:[~2014-05-30 11:38 UTC|newest] Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top 2011-07-02 0:02 [Bug inline-asm/49611] New: " scovich at gmail dot com 2011-07-02 12:37 ` [Bug inline-asm/49611] " rguenth at gcc dot gnu.org 2011-07-04 20:32 ` scovich at gmail dot com 2012-04-12 16:39 ` scovich at gmail dot com 2012-06-28 16:06 ` jbemmel at zonnet dot nl 2014-05-30 11:38 ` gcc.hall at gmail dot com [this message] 2014-06-01 23:45 ` andi-gcc at firstfloor dot org 2014-06-02 11:29 ` scovich at gmail dot com 2015-07-17 5:21 ` gccbugzilla at limegreensocks dot com 2015-07-17 5:41 ` gccbugzilla at limegreensocks dot com 2015-07-17 7:33 ` gcc.hall at gmail dot com 2015-07-17 10:58 ` pinskia at gcc dot gnu.org 2015-07-17 21:24 ` gccbugzilla at limegreensocks dot com 2015-07-19 18:02 ` gcc.hall at gmail dot com 2015-07-20 9:05 ` gccbugzilla at limegreensocks dot com 2015-07-20 9:20 ` gcc.hall at gmail dot com
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-49611-4-eTPL2UEHsx@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).