以下代码使用内联汇编填充结果:
uint64_t Foo::f() {
uint64_t result;
asm volatile
("vldmia %1, {q0-q1} \n" // q0-1 = *this
⋮
"vstmia %0, {d0} \n" // result = d0
:: "r"(&result), "r"(this)
: "q0", "q1");
return result;
}
result
变量在汇编代码中被无条件设置,但 Xcode 的分析器似乎忽略了这一点(流分析直接从声明跳到 return 语句)并提示:
…/BitBoard.cpp:26:9: Undefined or garbage value returned to caller
有没有一种方法可以在不浪费初始化 result
的周期的情况下安抚 Analyzer?
编辑:我尝试了指定输出约束的建议:
: "=r"(&result) : "r"(this), "r"(&result)
但是编译器发出“asm 输出中的无效左值”的声音。删除 &
编译但返回看似随机的结果。将 vstmia %0, {d0}
更改为 vmov %0, d0
也失败,并显示“指令操作数无效”。
我怀疑我必须按照建议将 result
标记为输出,并在汇编代码中以不同方式填充它,但我找不到任何关于这样做的信息。
最佳答案
我怀疑这是因为缺少输出约束。
试试这个,
uint64_t Foo::f() {
uint64_t result;
asm /* volatile */
("vldmia %1, {q0-q1} \n" // q0-1 = *this
⋮
"vstmia %0, {d0} \n" // result = d0
: "=w"(result): "r"(this) : "q0", "q1");
return result;
}
您必须使用输出 约束,"=w"(result)
,告诉编译器汇编器正在设置一个值。如果这样做,您可能不需要 volatile
。至少,这是一个很好的消除问题。
关于c++ - 误报 : Undefined or garbage value returned to caller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17119201/