Odd error with the "X" inline assembly constraint

David Brown david.brown@hesbynett.no
Mon Jan 8 18:48:04 GMT 2024


On 07/01/2024 19:24, Segher Boessenkool wrote:
> On Sun, Jan 07, 2024 at 04:10:21PM +0100, David Brown wrote:
>> On 05/01/2024 19:46, Segher Boessenkool wrote:
>>>> However, when I use just "asm ("" : "+X" (x));", I get an error message
>>>> "error: inconsistent operand constraints in an 'asm'".  I have no idea
>>>> why this is an issue.
>>>
>>> The C constraint means "Any operand whatsoever is allowed."  Here you
>>> are saying to use it both as input and as output, and GCC does not know
>>> how to reload wherever it chose to put it.
>>
>> It doesn't need to reload it
> 
> The error message is emitted during reloading.
> 
>>> "+" really creates two operands, and ties them together.  Writing
>>>    asm("oink" : "+X"(bla));
>>> is shorthand for
>>>    asm("oink" : "=X"(bla) : "0"(bla));
>>>
>>
>> I don't know if these are /exactly/ the same, but they are certainly
>> similar.
> 
> They are the same thing.  The "+" syntactic sugar is modified during
> gimplification already, almost nothing in the compiler has to deal with
> it.
> 
> See "gimplify_asm_expr", see the code after
>            /* An input/output operand.  To give the optimizers more
>               flexibility, split it into separate input and output
>               operands.  */
> 
> (Constraints that allow registers get a "0" (or "1" etc.) matching
> constraint for the input duplicate, constraints that do not (like "m")
> are unmodified.  The output duplicate just gets the "+" changed to "=").
> 

Thanks for that explanation - I've learned a bit more now.

So what then does this mean?

	asm("" : "+X" (x) : "0" (x));


On x86-64, I get the error with this :

typedef double T;
T test(T a, T b) {
     T x = a + b;
     asm ("" : "+X" (x));
     return x - b;
}


But not with this:

typedef double T;
T test(T a, T b) {
     T x = a + b;
     asm ("" : "+X" (x) : "0" (x));
     return x - b;
}

(But that still gives an error on ARM-32!)

I'm sure there's a logical pattern here somewhere, but I can't see it yet.

David





More information about the Gcc-help mailing list