Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extended asm with goto, including an example from the gcc docs, fails to compile

Some extended assembly statements using the goto qualifier fail to compile with GCC 10.1.0. Specifically,

int foo(int count)
{
  asm goto ("dec %0; jb %l[stop]"
            : "+r" (count)
            :
            :
            : stop);
  return count;
stop:
  return 0;
}

(which is an example in the GCC extended asm docs) fails to compile with the message expected ‘:’ before string constant. Removing the "+r" (count) and the dec %0 allows it to compile successfully, but regardless of what I try whenever an output operand is supplied in the same asm statement as a goto label, it errors in this same way.

like image 302
Josh Op Avatar asked Nov 25 '20 17:11

Josh Op


2 Answers

It appears the current development GCC documentation which you are referencing applies to the latest trunk branches of GCC and doesn't apply to any of the official releases of GCC. Official releases of GCC do not as of this time support asm goto with any output or input/output constraints. You can see this on godbolt. Latest trunk works but 10.2 and 10.1 don't. The fix is to wait for the next major release of GCC (version 11.x); download and compile the latest trunk release; modify your inline assembly so that it doesn't rely on any output or output/input constraints.

Up until recently the documentation for GCC up to version 10.x had this to say:

An asm goto statement cannot have outputs. This is due to an internal restriction of the compiler: control transfer instructions cannot have outputs. If the assembler code does modify anything, use the "memory" clobber to force the optimizers to flush all register values to memory and reload them if necessary after the asm statement.

A list of all the documentation for the official releases and the current development documentation can be found at this URL. The current development documentation is at the bottom of the page. Rule of thumb is that you should consult the documentation for your specific version of GCC. I believe that all 10.x release documentation is the same as the latest 10.x version of the documentation on the GCC webpage.

Recent versions of CLANG/LLVM (11.0+) do support this feature but that is relatively recent addition as well.

like image 128
Michael Petch Avatar answered Sep 24 '22 06:09

Michael Petch


asm goto doesn't allow output operands.

It is a gnu decision. in the function c_parser_for_statement from c-parser.c you can find :

/* For asm goto, we don't allow output operands, but reserve
the slot for a future extension that does allow them.  */

https://github.com/gcc-mirror/gcc/blob/releases/gcc-10/gcc/c/c-parser.c

However may be this situation will change since in the master branch this comment is not present anymore.

like image 34
yflelion Avatar answered Sep 22 '22 06:09

yflelion