The following program can be compiled by SDCC 4.6.0 with -mmos65c02 --stack-auto --std-c23 --max-allocs-per-node 1:
static int i = 99;
int main(void)
{
static int j = 101;
return 0;
}
https://godbolt.org/z/a7bsaGo9b
Surprisingly, although both i and j have static storage duration and therefore their initial values must be constant expressions, only the value of i is placed in .area XINIT and only storage for i is allocated in .area DATA:
.module example
.r65c02
.optsdcc -mmos65c02
.globl _main
.area ZP (PAG)
.area BSS
_main_j_10000_2:
.ds 2
.area DABS (ABS)
.area DATA
_i:
.ds 2
.area _CODE
.area GSINIT
.area GSFINAL
.area GSINIT
ldx #0x65
stx _main_j_10000_2
stz (_main_j_10000_2+1)
.area _CODE
.area _CODE
.area CODE
_main:
lda #0x00
tax
rts
.area CODE
.area RODATA
.area XINIT
__xinit__i:
.dw #0x0063
.area CABS (ABS)
That seems like a misfeature because the initialisation code for j is less efficient than .dw:
ldx #0x65
stx _main_j_10000_2
stz (_main_j_10000_2+1)
Initialising j requires eight bytes of instructions, whereas its initial value occupies only two bytes. Since this program already needs startup copying of initialised data for i, placing j in the same initialisation image appears to reduce code size without requiring another initialisation mechanism. Could block-scope static objects use that mechanism too?
See also [feature-requests:#455].
Related
Feature Requests: #455