register unsigned char _t; // so I don't have to worry about safe
Regs
__asm__ __volatile__
(
"\n"
"_lock_1:\n"
" lwarx %0,0,%1 \n" // load lock and reserve
" addi %0,0,1 \n" // set lock (1 is locked)
" stwcx. %0,0,%1 \n" // try to set lock
" bne- _lock_1 \n" // loop if lost reservation
" isync \n" // import barrier
: "=&r" (_t)
: "r" (__count)
: "cc", "memory"
);
}
volatile unsigned char testB = 0;
int main(void) {
printf("testB is %d\nNow locking.\n", testB);
_lock_(&testB);
printf("testB is now %d\n", testB);
return 0;
}
gcc -O inline-test.c -o inline-test
I get:
testB is 0
Now locking.
testB is now 0
From the assembly (gcc -O inline-test.c -S), it seems that it is
sending the address of testB, but it loaded r29 with the value and is
sending that to printf the second time (if I'm reading this correctly)
See below.
.data
_testB:
.byte 0
.cstring
.align 2
LC0:
.ascii "testB is %d\12Now locking.\12\0"
.align 2
LC1:
.ascii "testB is now %d\12\0"
.align 2
[snip]
addis r29,r31,ha16(_testB-"L00000000001$pb")
la r29,lo16(_testB-"L00000000001$pb")(r29)
addis r3,r31,ha16(LC0-"L00000000001$pb")
la r3,lo16(LC0-"L00000000001$pb")(r3)
lbz r4,0(r29)
bl L_printf$stub
addis r2,r31,ha16(_testB-"L00000000001$pb")
la r2,lo16(_testB-"L00000000001$pb")(r2)