我在使用内联汇编和本地标签时遇到汇编错误。编译器是 GCC,机器是运行 AIX 的 PowerPC。代码reads the timestamp (它大致等同于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);
}
当代码被组装时,它会产生:
gcc -O3 -Wall -Wextra -mcpu=power8 -maltivec test.c -o test.exe
Assembler:
test.s: line 103: 1252-142 Syntax error.
使用--save-temps
编译并检查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
看起来汇编器在处理本地标签时遇到了问题。基于 IBM 的 Use of inline assembly和本地标签我相信标签和分支被正确使用:
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 的 error message for 1252-142不是很有帮助:
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.
问题是什么,我该如何解决?
根据@Eric 在评论中的建议:
__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)
);
导致向下移动一行的问题:
gcc -O3 -Wall -Wextra -mcpu=power8 -maltivec test.c -o test.exe
Assembler:
test.s: line 104: 1252-142 Syntax error.
但看起来标签在第 0 列:
103
104 0:
105 mftbu 10
106 mftb 9
107 mftbu 8
108 cmpw 8,10
109 bne- 0b
最佳答案
gcc 不直接生成机器代码;它将其 asm 输出提供给系统汇编程序。您可以将 gcc 配置为使用不同的汇编器,例如 GAS,但显然您使用的机器上的默认设置是 GCC 使用 AIX 的汇编器。
显然,与 GNU 汇编器不同,AIX 的汇编器不支持数字标签。当您提到使用 0
之类的标签时,您链接的那篇文章可能是假设 Linux(无意或有意)。
最简单的解决方法可能是让 GCC 对标签自动编号而不是使用本地标签,这样同一个 asm block 可以在同一个编译单元中多次内联/展开而不会出现符号名称冲突。 %=
expands to a unique number in every instance.
IDK 如果 L..
使其成为文件本地标签(不会弄乱调试信息或符号表)。在 Linux/ELF/x86 上,.L
是普通前缀,但您有一个编译器生成的 L..
标签。
__asm__ __volatile__ (
"L..again%=: \n\t"
"mftbu %0 \n\t"
"mftb %1 \n\t"
"mftbu %2 \n\t"
"cmpw %2,%0 \n\t"
"bne- L..again%="
: "=r"(upper),"=r"(lower),"=r"(tmp)
: :
);
或者对于这个特定的 asm 用例,可能有一个读取时间戳寄存器的内置函数,它会像这样编译成 asm。
关于c - 1252-142 由于本地标签,AIX 汇编程序出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51869790/