Hi,

> Does anyone know what the C standard says about local scoping and storage 
> allocation?

you should buy one exemplar (only $ 15) of the standard from ansi.org.
The last release candidate of the standard is n869.pdf (see google).
You should also read the rationale
C99RationaleV5.10.pdf
the C-FAQs (in the book you will find more than in the online version)
and 
Expert C Programming - Deep C Secretes.pdf
The last 3 documents are for C89 and partially for the ancient K&R C (where 
(-1 > (unsigned char) 5) was evaluated to true!) but they are still good.

Regards

Rolf
Title: [email protected] schrieb am 06.12.04 14:13:38
-----Ursprüngliche Nachricht wurde als Attachment angehängt.-----
Von:[email protected]
Gesendet:06.12.04 14:13:38
An:"Rolf Freitag" <[email protected]>
Betreff:RE: [Mspgcc-users] FW: Lexical Scoping and GCC

Does anyone know what the C standard says about local scoping and storage allocation?

 


From: [email protected] [mailto:[email protected]] On Behalf Of Moussa Ba
Sent: Monday, December 06, 2004 12:36 AM
To: [email protected]
Subject: [Mspgcc-users] FW: Lexical Scoping and GCC

 

 

What is the behavior of the program below as far as stack usage is concerned.  We use inner blocks to lexically isolate variable and to also force the function main to release stack space after we exit a particular scope.  This is necessary as we start the « real » program by calling a function pointer stored in ROM, we want the function to basically have acess to as much stack as it needs.  I am little worried about this code, as I am not really sure that GCC behaves as I expect it to especially with optimization turned on.  I expect that when enterring the second inner block where I referred to r3, stack usage is just 2 bytes for the loop variable.  Can I assume that GCC will put locally declared variable withing braces on the stack and pop them as we leave the scope ?

The assembley language does not look like what I was expecting, it seems like main uses 72 bytes of stack which is consistent with the fact all the locally declared variables within main are not popped off the stack between inner blocks.

 

Am I understanding this correctly ?

 

typedef unsigned char uint8_t;

 

volatile uint8_t r1[20];

int main(void) {

    volatile uint8_t i = 0;

 

    for (i=0;i<20;i++) {

            r1[i] = i;

    }

 

 

 

    {

            volatile uint8_t r2[20];

 

            r2[19] = 0x34;

 

    }

 

    {

            volatile uint8_t r3[50];

 

            r3[49] = 45;

           

    }

            //Start real program.

Boot_rom->start() ;

 

}

 

 

 

.file       "local.c"

            .arch msp430x1132

 

            .text

            .p2align 1,0

.global  main

            .type     main,@function

/***********************

 * Function `main'

 ***********************/

main:

/* prologue: frame size = 72 */

.L__FrameSize_main=0x48

.L__FrameOffset_main=0x4c

            mov      #(__stack-72), r1

            mov      r1,r4

/* prologue end (size=3) */

            mov.b   #llo(0), @r4

            mov.b   #llo(0), @r4

.L2:

            cmp.b   #llo(20), @r4

            jlo         .L5

            jmp       .L3

.L5:

            mov.b   @r4, r15

            and.b    #-1,r15

            mov.b   @r4, _r1(r15)

            add.b    #llo(1), @r4

            jmp       .L2

.L3:

            mov.b   #llo(52), 21(r4)

            mov.b   #llo(45), 71(r4)

/* epilogue: frame size=72 */

            add       #72, r1

            br         #__stop_progExec__

/* epilogue end (size=4) */

/* function main size 31 (24) */

.Lfe1:

            .size     main,.Lfe1-main

/********* End of function ******/

Reply via email to