GCC Inline Assembly for Sparc Architecture

I found on the Internet an implementation of __ sync_val_compare_and_swap :

#define LOCK_PREFIX "lock ; "

struct __xchg_dummy { unsigned long a[100]; };
#define __xg(x) ((struct __xchg_dummy *)(x))

static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
                  unsigned long new, int size)
{
   unsigned long prev;
   switch (size) {
   case 1:
      __asm__ __volatile__(LOCK_PREFIX "cmpxchgb %b1,%2"
                 : "=a"(prev)
                 : "q"(new), "m"(*__xg(ptr)), "0"(old)
                 : "memory");
      return prev;
   case 2:
      __asm__ __volatile__(LOCK_PREFIX "cmpxchgw %w1,%2"
                 : "=a"(prev)
                 : "q"(new), "m"(*__xg(ptr)), "0"(old)
                 : "memory");
      return prev;
   case 4:
      __asm__ __volatile__(LOCK_PREFIX "cmpxchgl %1,%2"
                 : "=a"(prev)
                 : "q"(new), "m"(*__xg(ptr)), "0"(old)
                 : "memory");
      return prev;
   }
   return old;
}

#define cmpxchg(ptr,o,n)\
   ((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)(o),\
               (unsigned long)(n),sizeof(*(ptr))))

When I compile and use this function (cmpxchg) for the i386 architecture, everything is fine! But, when I compiled under the Sparc architecture, I have the following error:

error: impossible constraint in `asm'

What is the problem?

+1
source share
3 answers

On Solaris, it’s better not to write your own code for this (neither on SPARC nor on x86); rather, use functions atomic_cas(3C)for this purpose:

static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
              unsigned long new, int size)
{
    switch (size) {
    case 1: return atomic_cas_8(ptr, (unsigned char)old, (unsigned char)new);
    case 2: return atomic_cas_16(ptr, (unsigned short)old, (unsigned short)new);
    case 4: return atomic_cas_32(ptr, (unsigned int)old, (unsigned int)new);
#ifdef _LP64
    case 8: return atomic_cas_64(ptr, old, new);
#endif
    default: break;    
    }
    return old;
}

This will be done for Solaris.

:, , SPARC (v8 +, aka UltraSPARC) - " ", aka CAS. (sparc ). 32- 64- (CASX) , 8/16- 32bit CAS, /. - , .

Edit2: ( Solaris libc).

+3

cmpxchgb - i386, Sparc.

+5

You cannot compile asm x86 for sparc. Here I use clang:

[~] main% ~/ellcc/bin/sparc-linux-ecc asm.c
asm.c:13:20: error: invalid output constraint '=a' in asm
             : "=a"(prev)

'a' is not a sparc register, it is specific to x86.

Even if you fix the restriction, you will get a build time error when the sparc assembler sees the cmpxchgb opcode that is x86 specific.

+1
source

All Articles