Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top