RAUL MARTIN

static variables seems to be allocated on stack (Codewarrior for Microc.. v6.2 and Coldfire V1)

Discussion created by RAUL MARTIN on Mar 29, 2009
Latest reply on Mar 31, 2009 by RAUL MARTIN

Hi, I'm using a math function for my Coldfire MCF51QE128 under Codewarrior v6.2 and it seems it allocates static variables under stack, which makes my stack to overflow. I'm using uCOS-II as RTOS (but i think this is indifferent).

 

Here you have the piece of code wich is making me crazy. It belongs to module CAstr.c (astronomic calculations):

 

static FP64 A;               //FP64 is a double typedef
static INT32U absLat;
static INT32U absLon;
static FP64 AM;
static FP64 AR;
static FP64 C;
static FP64 D;
//... here comes more static variables shared under this module. Next function uses all these 40 variables.

 

/******************************************************************************/
/* FUNCTION:                    ComputeDawnDusk                                 */
/*****************************************************************************/
static INT8U ComputeDawnDusk(INT32U *This, CTimeDate *t, FP32 lat, FP32 lon){
    absLat = (lat < 0)? -lat : lat;
    absLon = (lon < 0)? -lon : lon;

    // date formatting from my own CTimeDate struct...   
    DD=t->date.day;
    MM=t->date.month;
    YY= 2000 + t->date.year;

    // calculating math ecuations....

    ......

    ......

}

 

As you can see all the variables used inside this function are static and has a fixed RAM position, I've verified looking at the MAP file, see below:

 

 00801680 00000008 .sbss   C    (CAstr.c)
 00801688 00000008 .sbss   AR    (CAstr.c)
 00801690 00000008 .sbss   AM    (CAstr.c)
 00801698 00000004 .sbss   absLon    (CAstr.c)
 0080169C 00000004 .sbss   absLat    (CAstr.c)
 008016A0 00000008 .sbss   A    (CAstr.c)

 

But when the function is executed, a huge stack area is reserved and i get an stack overflow. See this dissasemble of the ComputeDawnDusk function:

 

MOVE.L    A6, -(A7)

MOVE.L    A4,-(A7)

LEA         -1988(A7),A7                    //<---- THIS DISPLACEMENT CAUSES THE STACK OVF.

MOVEA.L   2004(A7),A4

CLR.L        4(A7)

MOVE.L      2008(A7),(A7)

.........

 

 I've been trying to use other kind of declaration for such variables (volatile, global) and continuosly occurs the same. It seems as if the compilers optimize the code and uses the stack to store all of such variables, causing the overflow.

 

Please, could you help me with this stuff. Thanks a lot.

Outcomes