Menu

#932 Bad optimization during initialization

None
open
nobody
None
5
2024-07-04
2024-07-04
No

Bad optimization during initialization
short __xdata k[16]={0};

tranclate to
;--------------------------------------------------------
; code
;--------------------------------------------------------
    .area CSEG    (CODE)
    .area CSEG    (CODE)
    .area CONST   (CODE)
    .area XINIT   (CODE)
__xinit__k:
    .byte #0x00, #0x00  ;  0
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .db 0x00
    .area CABS    (ABS,CODE)

It is not a good idea to place zeros in the program code. For example, in the gcc compiler, all variables initialized to zero are located in a special segment; this segment is filled with zeros before the start of the main function. Therefore, in the gcc compiler, initializing variables to zero (which is highly recommended to increase the reliability of the program) does not consume additional memory.

sdcc -v
SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/sm83/tlcs90/ez80_z80/z80n/r800/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502/mos65c02 TD- 4.4.0 #14620 (MINGW64)
2 Attachments

Discussion

  • Maarten Brock

    Maarten Brock - 2024-07-04

    Ticket moved from /p/sdcc/bugs/3756/

    Can't be converted:

    • _category: other
     
  • Maarten Brock

    Maarten Brock - 2024-07-04

    The simple work around is not to initialize such global or static variables as the standard requires them to be initialized to zero anyway.

    But sure it would be nice if SDCC could detect that the initializer is all zeroes.

    Inefficient code generation is not a bug though.

     

Log in to post a comment.