误报:未定义或垃圾值返回给调用者 [英] False positive: Undefined or garbage value returned to caller
问题描述
以下代码使用内联汇编填充结果:
The following code populates a result using inline assembly:
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的分析器似乎忽略了这一点(流程分析直接从声明跳到返回语句)并抱怨:
The result
variable is unconditionally set in the assembly code, but Xcode's Analyzer seems to ignore this (the flow analysis skips straight from the declaration to the return statement) and complains:
…/BitBoard.cpp:26:9: Undefined or garbage value returned to caller
有没有办法在不浪费初始化result
的周期的情况下安抚分析器?
Is there a way to pacify Analyzer without wasting cycles initializing result
?
我已经尝试过指定输出约束的建议:
I've tried the suggestion to specify an output constraint:
: "=r"(&result) : "r"(this), "r"(&result)
但是编译器发出无效的 asm 输出中的左值"的声音.删除 &
编译但返回看似随机的结果.将 vstmia %0, {d0}
更改为 vmov %0, d0
也会失败,并显示指令操作数无效".
But the compiler croaks with "Invalid lvalue in asm output". Removing the &
compiles but returns seemingly random results. Changing vstmia %0, {d0}
to vmov %0, d0
also fails, with "Invalid operand for instruction".
我怀疑我必须按照建议将 result
标记为输出,并在汇编代码中以不同的方式填充它,但我找不到任何关于知道这样做的信息.>
I suspect that I have to mark result
as an output, as suggested, and populate it differently in the assembly code, but I can't find any information on know to do so.
推荐答案
我怀疑这是由于缺乏输出约束造成的.
I suspect this is due to the lack of an output constraint.
试试这个,
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
.至少,这是一个需要消除的好问题.
You must use an output constraint, "=w"(result)
, to tell the compiler that the assembler is setting a value. You probably don't need the volatile
if you do this. At least, this is a good issue to eliminate.
这篇关于误报:未定义或垃圾值返回给调用者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!