This is not a bug report but an optimisation request.
When accessing an element of a structure array with constant array index the constant array index offset and the constant element offset could be added first at compile time and then the array's base address added to the combined offset at runtime. This would result both in halved code size and halved execution time.
Presently produced code:
mov r2,dpl
mov r3,dph
BugReport13.c:11: return s1[3].ch;
mov a,#0x15 ;array index offset 3*7=21
add a,r2
mov r2,a
clr a
addc a,r3
mov r3,a
mov a,#0x06 ;structure element offset
add a,r2
mov dpl,a
clr a
addc a,r3
mov dph,a
clr a
movc a,@a+dptr
mov dpl,a
ret
Optimised code according to the proposal:
mov r2,dpl
mov r3,dph
BugReport13.c:11: return s1[3].ch;
mov a,#0x1B ;combined offset 3*7+6
add a,r2
mov dpl,a
clr a
addc a,r3
mov dph,a
clr a
movc a,@a+dptr
mov dpl,a
ret
I do not know the inner workings of the compiler so I cannot judge the complexity of such combined evaluation but from efficiency point of view it is definitely reasonable.
Command to compile:
C:\Laci\GCR250>sdcc -v
SDCC : mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08 2.7.4 #4972 (Nov 24 2007) (MINGW32)
C:\Laci\GCR250>ver
Microsoft Windows XP [Version 5.1.2600]
Logged In: NO
i think, it would be great if there was a standalone optimizer that is working with an intermediate representation, so that these things can be decoupled from the rest of sdcc and do not unnecessarily bloat sdcc but rather become a separate component of sdcc, which may optionally be extended using plugins or scripts
SDCC has been doing this optimization for quite a while (in the machine-independnet optimizations). I just checked that it works with the code sample provided (after updating the SDCC-specific keywords).