This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler: Multiple variables assigned to same memory location in variadic function

Tool/software: TI C/C++ Compiler

I believe I've come across a compiler bug that results in multiple local variables being assigned to the same location in memory. It occurs with optimizations off (-Ooff) in certain cases when a variadic function begins with a block that contains a variable declaration. 

The following function returns 3000 instead of 1110:

int test(int arg, ...)
{
    { int a = 0; }      // movs       r0, #0
                        // str        r0, [sp, #4]

    int b = 10;         // movs       r0, #0xa
                        // str        r0, [sp]

    int c = 100;        // movs       r0, #0x64
                        // str        r0, [sp]

    int d = 1000;       // mov.w      r0, #0x3e8
                        // str        r0, [sp]

    return b + c + d;   // ldr        r2, [sp]
                        // ldr        r0, [sp]
                        // ldr        r1, [sp]
                        // adds       r0, r0, r2
                        // adds       r0, r0, r1
}

If the initializer is removed from line 3 ("{ int a; }") then the function behaves as expected.
If that block is made conditional ("if (arg) { int a; }"), the problem reappears.

Compiler Options:

Building file: "../main.c"
Invoking: ARM Compiler
"C:/ti/ccsv8/tools/compiler/ti-cgt-arm_18.1.2.LTS/bin/armcl" -mv7M4 -me -Ooff --include_path="C:/ti/ccsv8/tools/compiler/ti-cgt-arm_18.1.2.LTS/include" --define=ccs="ccs" --define=PART_TM4C123GH6PM --c99 --preproc_with_compile --preproc_dependency="main.d_raw"  "../main.c"
"../main.c", line 3: warning: variable "a" was declared but never referenced
Finished building: "../main.c"

I've also been able to reproduce it with in C89 mode but the trigger conditions are slightly different.

  • I think we need a complete program. How are you calling test? Shouldn't va_list be the first variable in the function?
  • The rest of the program is just this:

    int main(void)
    {
        int x = test(0, 0);
        while(1);
        return 0;
    }
    

    As far as I can tell the standard does not require the first variable declared to be a va_list. I removed all the va_* stuff to provide a simple test case. In my actual application, the first variable declared in the initial block is a va_list and the first one declared after that block is also a va_list.

  • Thank you for reporting this problem, and for submitting a concise test case.  I can reproduce the same result.  I filed CODEGEN-4829 in the SDOWP system to have this investigated.  You are welcome to follow it with the SDOWP link below in my signature.

    Thanks and regards,

    -George