False positive: Undefined or garbage value returned to caller

别等时光非礼了梦想. 提交于 2019-12-02 14:08:17

问题


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;
}

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

Is there a way to pacify Analyzer without wasting cycles initializing result?

EDIT: I've tried the suggestion to specify an output constraint:

: "=r"(&result) : "r"(this), "r"(&result)

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".

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.


回答1:


I suspect this is due to the lack of an output constraint.

Try this,

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;
}

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.




回答2:


I found a work-around, though I'm still hoping for a more elegant solution:

    union {
        uint64_t result;
        struct { uint32_t a, b; };
    };

    asm
    ("vldmia        %2, {q0-q1}     \n" // q0-1 = *this

     ⋮

     "vmov        %0, s0        \n"
     "vmov        %1, s1        \n"

     : "=r"(a), "=r"(b) : "r"(this)
     : "q0", "q1");

    return result;


来源:https://stackoverflow.com/questions/17119201/false-positive-undefined-or-garbage-value-returned-to-caller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!