Hi,
SDCC (not restricted to the pic ports) seems to be buggy with array
indices;
the problem is visible in the iCode as well.
<code>
struct s { int a; int b; char c; } arr[5];
char demo1(unsigned idx) { return arr[idx].c; }
char demo2(unsigned idx) { return arr[idx - 32].c; }
char demo3(unsigned char idx) { return arr[idx].c; }
char demo4(unsigned char idx) { return arr[idx - 32].c; }
char demo5(unsigned char idx) { return arr[(unsigned)(idx - 32)].c; }
char demo6(unsigned char idx) { idx -= 32; return arr[idx].c; }
char demo7(unsigned char idx) { idx -= 32; return arr[(unsigned)idx].c; }
</code>
The problem is the computation of the array index (idx * 5, 5 being
sizeof(struct s)), which might overflow a byte and thus should be
carried out on (unsigned) ints.
demo1 and demo2 work nicely.
demo3 and demo4 use 8x8->8 SUB and MUL and fail.
demo5 implements 16x16->16 SUB (!) and MUL and works.
demo6 implements 8x8->8 SUB and MUL and fails.
demo7 implements 8x8->8 SUB and 16x16->16 MUL and works nicely.
For now, I would recommend to go along demo7() and explicitly cast
the array index to (a 16-bit) unsigned.
> I think so the problem comes from ignoring __mulchar overflow (which
> can and will overflow for r0x1003>51). Is this some kind of bug or
> am I overlooking something? Thanks.
I agree, this seems to be a bug. Array index computation must be
carried out on (full-width) ints.
Does anyone of the SDCC developers have a clue if (and where) that is
to be fixed?!? Probably some AST optimization ...
Best regards
Raphael
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Sdcc-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sdcc-user