Comment 0 for bug 2015686

Revision history for this message
Leyun Gao (bloud) wrote :

Ubuntu 22.04.2 LTS
g++ (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0

When use modifier `z` on `[asmSymbolicName]`, the code won't compile.

Below shows the source file to trigger the bug.

Unexpectedly, the error report says the wrong modifier is 'Z', but what I use is actually 'z'. I believe that something must go wrong when GCC addresses the modifier 'z' followed by [asmSymbolicName].

/*- test.cpp -*/

/*
 * This file reports a bug about asm compilation.
 *
 * When use modifier `z` on `[asmSymbolicName]`,
 * the code won't compile.
 *
 * Ref: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
 */

#include <cstdio>
#include <cinttypes>

#define TRIGGER_ERROR

#ifdef TRIGGER_ERROR

// This one doesn't compiles.

// the one's complement addition on x86_64
template<class T>
inline T onec_add(T a, T b)
{
  asm( "add%z[b]\t%[b], %[a]"
      "\n\tadc%z[b]\t$0, %[a]"
      : [a]"+g"(a) // inputs
      : [b]"g"(b) // outputs
      : "cc" // clobbers
  );
  return a;
}

/*

$g++ --version
g++ (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0

$ g++ -Wall -g -Og test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:42:3: error: invalid 'asm': invalid operand type used with operand code 'Z'
   42 | asm( "add%z[b]\t%[b], %[a]"
      | ^~~
test.cpp:42:3: error: invalid 'asm': invalid operand type used with operand code 'Z'
test.cpp:42:3: error: invalid 'asm': invalid operand type used with operand code 'Z'
   42 | asm( "add%z[b]\t%[b], %[a]"
      | ^~~
test.cpp:42:3: error: invalid 'asm': invalid operand type used with operand code 'Z'
test.cpp:42:3: error: invalid 'asm': invalid operand type used with operand code 'Z'
   42 | asm( "add%z[b]\t%[b], %[a]"
      | ^~~
test.cpp:42:3: error: invalid 'asm': invalid operand type used with operand code 'Z'
test.cpp:42:3: error: invalid 'asm': invalid operand type used with operand code 'Z'
   42 | asm( "add%z[b]\t%[b], %[a]"
      | ^~~
test.cpp:42:3: error: invalid 'asm': invalid operand type used with operand code 'Z'

*/

#else /* TRIGGER_ERROR */

// This one compiles normally.

// the one's complement addition on x86_64
template<class T>
inline T onec_add(T a, T b)
{
  asm( "add%z0\t%[b], %[a]" // NOTE: %z[b] -> %z0
      "\n\tadc%z0\t$0, %[a]"
      : [a]"+g"(a) // inputs
      : [b]"g"(b) // outputs
      : "cc" // clobbers
  );
  return a;
}

#endif /* TRIGGER_ERROR */

int main()
{
  {
    uint64_t a = ((uint64_t)1 << 63);
    uint64_t b = 1;
    uint64_t c = onec_add(a, b);
    printf("%#" PRIx64 "\n", c); // expect: 0x8000000000000001
  }
  {
    uint32_t a = ((uint32_t)1 << 31);
    uint32_t b = 1;
    uint32_t c = onec_add(a, b);
    printf("%#" PRIx32 "\n", c); // expect: 0x80000001
  }
  {
    uint16_t a = ((uint16_t)1 << 15);
    uint16_t b = 1;
    uint16_t c = onec_add(a, b);
    printf("%#" PRIx16 "\n", c); // expect: 0x8001
  }
  {
    uint8_t a = ((uint8_t)1 << 7);
    uint8_t b = 1;
    uint8_t c = onec_add(a, b);
    printf("%#" PRIx8 "\n", c); // expect: 0x81
  }
}