This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

compiler optimisation bug

There is compiler optimisation (O2) which you may want to be aware of.

A while back (Oct 2010) I found a compiler optimisation bug with optimisation level 2 (O2) in MDK V4.12 (Armcc V4.1.0.481). The problem does not occur in MDK V4.11 (Armcc 4.0.0.728).

It appears that in some situations the compiler locates two local variables at the same address. I sent some demo code to Keil and they acknowledged the problem with the following explanation:

It seems like the compiler treats 8bit writes wrongly when analyzing the lifetime of a local variable. It assumes that a whole 32bit value is written and so assumes the other 3bytes in a word are destroyed/rewritten as well. So it can happen local objects share the same location as in this simplified example:

typedef union {
    char arr[4];
    int val;
} union_t;

void test(char val1, char val2, int *ptr)
{
    union_t a, b;

    a.arr[0] = val1;
    a.arr[1] = val1;
    a.arr[2] = val1;
    a.arr[3] = val1;

    ptr[0] = a.val;

    b.arr[0] = val2;
    b.arr[1] = val2;
    b.arr[2] = val2;
    b.arr[3] = val2;

    ptr[1] = b.val;

    a.arr[0] = val2;

    ptr[2] = a.val;
}


This has not been fixed in MDK 4.14.