#880 serious code generation problem with 2 dimensional array

closed-fixed
5
2013-05-25
2005-02-14
No

serious code generation problem with 2 dimensional array

This example is done with

SDCC : gbz80/z80 2.4.8 #956 (Feb 13 2005) (UNIX)

but from what i can see the problem is not limited to the z80
code generation.

commands used to compile the example

sdcc -c -mz80 main.c
sdcc -mz80 main.o

ok now the explanation of the bug.

in the loop
for( cntx = 0; cntx < width; cntx++ )

the cntx variable is defined as an unsigned char as the array
index since the maximum value of the array index can be only
80 this is ok.

but looking at the code generation

;main.c:24: for( cntx = 0; cntx < width; cntx++ ) {
; genPlus
; AOP_STK for _makeWindow__1_0
; genPlusIncr
; Can't optimise plus by inc, falling back to the normal
way
ld a,-5(ix)
add a,#0x32
ld -5(ix),a
; genPlus
; AOP_STK for _makeWindow_cntx_1_1
; genPlusIncr
inc -3(ix)

a Temp var is incremented by 0x32 = 50 decimal which is
correct BUT it's only an 8 bit add not a 16 bit add si the 6th
iteration of the loop overflows the counter, and cntx is
incremented with 1 which is correct.

second problem

w->window[width - 1][0].ch = 'k';

;main.c:29: w->window[width - 1][0].ch = 'k';
; genMinus
; AOP_STK for
ld a,4(ix)
add a,#0xFF

width is decreased with 1
and then multiplied by 50

ld c,a
; genMult
push de
ld e,c
ld d,#0x00

The upper 8 bit are not cleared

ld l,e
add hl,hl
add hl,de
add hl,hl
add hl,hl
add hl,hl
add hl,de
add hl,hl
pop de
ld c,l

BUT here only the lower part of the 16 bit result is used

looking at some code i found this in SDCCcast.c at line 2194

case '[':
if (!IS_ARRAY (LTYPE (tree)))
return resultType;
if (DCL_ELEM (LTYPE (tree)) > 0 && DCL_ELEM (LTYPE
(tree)) <= 256)
return RESULT_TYPE_CHAR;

this gives the result type of an array index a type of char if
the index is lower or equal than 256 but what about a 2
dimensional araay that can get bigger
If i change the RESULT_TYPE_CHAR to RESULT_TYPE_INT
then the latter example compiles correctly but this is probably
not the correct way to solve this problem.

I hope this is enough information

wdl at linux-lovers dot be

Discussion

  • Willy De la Court

     
  • Willy De la Court

     
  • Erik Petrich

    Erik Petrich - 2005-04-02
    • status: open --> closed
     
  • Erik Petrich

    Erik Petrich - 2005-04-02
    • labels: --> Icode generator
    • milestone: --> fixed
    • assigned_to: nobody --> epetrich
    • status: closed --> closed-fixed
     
  • Erik Petrich

    Erik Petrich - 2005-04-02

    Logged In: YES
    user_id=635249

    The same bug caused both problems.

    Fixed in src/SDCCicode.c 1.219 (SDCC version 2.4.8 #989)

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks