I want to access the EEPROM area as an array. I defined a address pointer to 0x4000 (the EEPROM memory area). Simple access works fine, but the "+=" operator compiles as "=". Sample code:
unsigned char array[32];
#define adr ((unsigned char*)0x4000)
void main()
{
array[0] = 1;
array[0] += 2; // works as expected
adr[0] = 1;
adr[0] += 2; // compiles as adr[0] = 2;
}
Compiling using the lastest snapshot build #10694:
$ sdcc -v -mstm8 -c test.c
SDCC : mcs51/z80/z180/r2k/r3ka/gbz80/tlcs90/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8 3.8.3 #10694 (Linux)
published under GNU General Public License (GPL)
The resulting code for array[0]+=2 is as expected, but adr[0]+=2 is compiled as adr[0]=2:
_main:
; test.c: 7: array[0] = 1;
ldw x, #_array+0
ld a, #0x01
; test.c: 8: array[0] += 2; // works as expected
ld (x), a
add a, #0x02
ld (x), a
; test.c: 10: adr[0] = 1;
mov 0x4000+0, #0x01
; test.c: 11: adr[0] += 2; // compiles as adr[0] = 2;
mov 0x4000+0, #0x02
; test.c: 12: }
ret
Is there an issue with my address definition or is this a compiler bug?
I can reproduce this bug in current SDCC [r10754]. It seems to happen in the front-end (output from --dump-ast looks already wrong) for all backends (tried z80, stm8).
Philipp
Looks like IS_LITERAL is true for the type of adr[0] in decorateType() in SDCCast.c, despite it being an ARRAY_OP, so SDCC tries to optimize away the addition.
Philipp
P.S.: I.e. the type of adr[0] is "unsigned char literal", while the type of array[0] is "unsigned char fixed".
Last edit: Philipp Klaus Krause 2018-12-31
Fixed in [r10782].
Philipp
Isn't this fixed in the wrong place? How did adr[0] become a literal in the first place? Or is it maybe marked isaddr?
And I think this one certainly deserves a regression test case.
The the propertyof being a literal is copied from adr to adr[0] (in the old code in COPYTYPE, in the new code in copyLinkChain()). So I changed it to make array indexing behave more like pointer dereference.
I didn't add a regression test case, since the bug is only observable when adr is defined to a fixed memory, making the regression test case highly target-specific.
Philipp