Default initialization of static

Don Wooton
2006-12-22
2013-03-12
  • Don Wooton

    Don Wooton - 2006-12-22

    The default initialization of static and other global variables
    is not performed by sdcc.  For small systems, the current
    behavior may be desired to save unnecessary startup code.  To
    avoid confusion, IMNSHO it should however be documented as
    non-conforming.

    A fragment from ISO/IEC 9899:TC2:

        If an object that has automatic storage duration is not
        initialized explicitly, its value is indeterminate. If an
        object that has static storage duration is not initialized
        explicitly, then:

        — if it has pointer type, it is initialized to a null
          pointer;

        — if it has arithmetic type, it is initialized to (positive
          or unsigned) zero;

        — if it is an aggregate, every member is initialized
          (recursively) according to these rules;

        — if it is a union, the first named member is initialized
          (recursively) according to these rules.

    A fragment from K&R:

        Static and external variables which are not initialized are
        guaranteed to start off as 0; automatic and register
        variables which are not initialized are guaranteed to start
        off as garbage.

    Consider the following program:

    -----8<---------------Begin file tt.c ---------8<--------------
    typedef struct
    {
        unsigned char a;
        unsigned char b;
    } foo_t;

    static foo_t foo = { 'A' };

    int
    main()
    {
        unsigned char       c;

        c = foo.a;
        return 0;
    }
    -----8<------------ End file tt.c -------------8<--------------

    If we compile this file with sdcc:

    sdcc -S tt.c

    The following fragment shows the global initialization:

    -----8<----- Begin file tt.asm fragment -------8<--------------

    ;--------------------------------------------------------
    ; File Created by SDCC : FreeWare ANSI-C Compiler
    ; Version 2.6.2 #4505 (Dec  9 2006)
    ; This file generated Thu Dec 21 20:13:33 2006
    ;--------------------------------------------------------
            .module tt
            .optsdcc -mmcs51 --model-small

        ... code elided for brevity
    ;--------------------------------------------------------
    ; internal ram data
    ;--------------------------------------------------------
            .area DSEG    (DATA)
    _foo:
            .ds 2
    ;--------------------------------------------------------
        ... code elided for brevity

    ;       tt.c:8: static foo_t foo = { 'A' };
    ;       genPointerSet
    ;       genNearPointerSet
    ;       genDataPointerSet
            mov     _foo,#0x41
            .area GSFINAL (CODE)
            ljmp    __sdcc_program_startup

        ... code elided for brevity
    -----8<------- End file tt.asm fragment -------8<--------------

    Notice that only the first byte is set.

    As a point of comparison, we can compile with gcc:

    gcc -S tt.c

    The following fragment shows the global initialization with zero
    of the remaining byte:

    -----8<----- Begin file tt.s fragment ---------8<--------------
            .file   "tt.c"
            .data
            .type   foo, @object
            .size   foo, 2
    foo:
            .byte   65
            .zero   1
            .text
    .globl main
            .type   main, @function
    main:
        ... code elided for brevity

    -----8<------- End file tt.s fragment ---------8<--------------

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks