clang x86 code generation/binary compatibility with GCC

Hi,

I have this code:

void conv(long *pa, signed char b)
{
  *pa = static_cast<long>(b);
}

GCC 4.3.4 on SLES11, aswell as a recent GCC 4.7.1 compile this to:

  .cfi_startproc
  movsbq %sil, %rsi
  movq %rsi, (%rdi)
  ret
  .cfi_endproc

So the "signed char b" in sil is sign-extended to a 64 bit register.

clang emits this:

# BB#0: # %entry
  movslq %esi, %rax
  movq %rax, (%rdi)
  ret

clang is already passing char arguments in 32-bit registers sign-extended, and the generated code of this function relies on that. I have a case where this causes problems with jitted code that is calling such functions, and the assumption made by clang that the char has already been extended to 32 bit fails there.

Anyone can shine some light on this?

Thanks and best regards,
Martin

I believe LLVM is C ABI compliant. I don't know where it is documented but LLVM's assumption is caller must extend arguments to at least 32-bit.

See discussion here:

http://llvm.org/bugs/show_bug.cgi?id=12207
and
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46942

Looks like gcc does sign / zero extend its call arguments so there shouldn't any compatibility issue mixing llvm and gcc code.

Evan