#342 Do not zero global variables

open
nobody
None
5
2014-08-22
2011-12-03
No

Currently, sdcc generates code to initialize global/static variables to zero, iff they are initialized explicitly.
I would like to see an option to not generate initialization code for variables that are explicitly initiaized to zero.

Rationale: sdcc relies on crt0 initializing global/static variables to zero anyway for standard-compliant behaviour (implicitly initialized to zero): We assume that crt0 initializes the data RAM to zero. Thus the additional, redundant zeroing for the explicitly initialized ones is a waste of code size.

Philipp

Discussion

  • Maarten Brock
    Maarten Brock
    2011-12-03

    This is probably different for different targets and even for different memory types in the same target. I'll explain for mcs51.

    At startup data memory is cleared in crtclear.asm. After that all variables are explicitly assigned their startup value. So zero-variables could use this optimization, even at byte-level.
    Pdata is cleared and explicitly assigned in the same way.
    Now xdata is divided in initialized and uninitialized memory. Uninitialized (XSEG) is cleared in crtxclear.asm, most probably for uninitialized global and static variables. For local variables (which are overlayed instead of stacked) this is not required in C.
    The initialized part (XISEG) is copied from rom (XINIT) in crtxinit.asm unless --no-xinit-opt is used in which case all variables are explicitly assigned. XISEG is never cleared up front. Zero-variables could be moved to XSEG and have their initializer removed. But variables that are partly zero cannot.
    Constants in code memory need to be initialized. Their initializers cannot be removed.

    Does the Z80 (still) support initialization through a block copy from rom? ISTR I've seen that in the past even with some sort of compression. Or is every variable explicitly assigned?

    And how are other targets doing this?

    Maarten

     
  • >Does the Z80 (still) support initialization through a block copy from rom?
    >ISTR I've seen that in the past even with some sort of compression. Or is
    >every variable explicitly assigned?

    The z80 (and related ports) no longer supports initialization using rle. I disabled it a few years ago, since it was broken for arrays of float.
    The rle initialization only provided an advantage for initializing large arrays.

    Philipp

     
  • Since today the Z80 has the block copy mechanism in place again.
    I agree that moving the zero variables to XSEG/DATA is the right way to do this.

    Philipp