I have come across a problem with SDCC for some reason including constant string data twice inside compiled binaries. I am puzzled to explain why this is happening. This is, of course, undesirable when trying to minimise the binary size, which I am currently trying to do.

If I compile a simplified test program like so:

void main(void) {
    static const struct {
        char str[5];
    } foo[] = {
        { "1234" },
        { "5678" },
    };

    while(1);
}

The strings from the array are included twice into the binary, as can be seen when looking at the generated (STM8) assembly listing:

    .area CODE
    .area CONST
_main_foo_65536_11:
    .ascii "1234"
    .db 0x00
    .ascii "5678"
    .db 0x00
    .area CONST
___str_0:
    .ascii "1234"
    .db 0x00
    .area CODE
    .area CONST
___str_1:
    .ascii "5678"
    .db 0x00
    .area CODE
    .area INITIALIZER
    .area CABS (ABS)

Even when one includes code accessing main()'s foo array, the individual string constants (___str_0 & ___str_1) are never referenced, and so are redundantly wasting space.

By the way, if the array is declared at top level, outside of main(), the strings are not duplicated:

static const struct {
    char str[5];
} foo[] = {
    { "abcd" },
    { "wxyz" },
};

void main(void) {
    while(1);
}

    .area CODE
    .area CONST
_foo:
    .ascii "abcd"
    .db 0x00
    .ascii "wxyz"
    .db 0x00
    .area CODE
    .area INITIALIZER
    .area CABS (ABS)

But I would prefer not to do this, in order to restrict the scope of the array only to where it is used (one specific function).

So why is this duplication occurring? Can I do anything to prevent it?

I could just change my struct (which by the way contains other members - I reduced to just the string for demonstration purposes) to store a string pointer instead and let the discrete string constants be used, but as my strings are only short (4 chars), it is more efficient for me in terms of space to store the strings directly in the array. But I fear I might end up having to do this if this duplicated strings problem is unresolvable.

Regards,
Basil Hussain

P.S. SDCC version:

SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/sm83/tlcs90/ez80_z80/z80n/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502 4.2.0 #13081 (MINGW64)


_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user

Reply via email to