I'm catching an assembler error when using inline assembly and a local label. The compiler is GCC, and the machine is PowerPC running AIX. The code reads the timestamp (it is roughly equivalent to rdtsc
):
static unsigned long long cpucycles( void )
{
unsigned long long int result=0;
unsigned long int upper, lower,tmp;
__asm__ __volatile__ (
"0: \n\t"
"mftbu %0 \n\t"
"mftb %1 \n\t"
"mftbu %2 \n\t"
"cmpw %2,%0 \n\t"
"bne- 0b \n\t"
: "=r"(upper),"=r"(lower),"=r"(tmp)
: :
);
result = upper;
result = result<<32;
result = result|lower;
return(result);
}
When the code is assembled it results in:
gcc -O3 -Wall -Wextra -mcpu=power8 -maltivec test.c -o test.exe
Assembler:
test.s: line 103: 1252-142 Syntax error.
Compiling with --save-temps
and examining test.s
:
$ cat -n test.s
...
101 L..5:
102 # 58 "test.c" 1
103 0:
104 mftbu 10
105 mftb 9
106 mftbu 8
107 cmpw 8,10
108 bne 0b
109
It looks like the assembler is having trouble with the local label. Based on IBM's Use of inline assembly and local labels I believe the label and branch are being used correctly:
Only some local labels are legal in inline assembly. You might see labels, such as 0 and 1 in Code C. They are the branching target of instruction
bne- 0b\n\t
andbne 1f\n\t
. (The f suffix for the label means the label behind the branch instruction, and b is for the one ahead)
IBM's error message for 1252-142 is not very helpful:
Cause
If an error occurred in the assembly processing and the error is not defined in the message catalog, this generic error message is used. This message covers both pseudo-ops and instructions. Therefore, a usage statement would be useless.
Action
Determine intent and source line construction, then consult the specific instruction article to correct the source line.
What is the problem and how do I fix it?
Based on @Eric's suggestions in the comments:
__asm__ __volatile__ (
"\n0: \n\t"
"mftbu %0 \n\t"
"mftb %1 \n\t"
"mftbu %2 \n\t"
"cmpw %2,%0 \n\t"
"bne- 0b \n\t"
: "=r"(upper),"=r"(lower),"=r"(tmp)
);
Results in the problem moving one line down:
gcc -O3 -Wall -Wextra -mcpu=power8 -maltivec test.c -o test.exe
Assembler:
test.s: line 104: 1252-142 Syntax error.
But it looks like the label is in column 0:
103
104 0:
105 mftbu 10
106 mftb 9
107 mftbu 8
108 cmpw 8,10
109 bne- 0b