#1553 bad code generation for code pointers

closed-fixed
5
2013-05-25
2009-07-14
zetalog
No

We are using SDCC2.9.0 with following settings:
ARCH: mcs51
Memory Model: small

Please check with the sample code, which is compiled with --stack-auto:

============================================================
#define NR_TEST_FUNCS 2
typedef code struct test_funcs {
void (*non)(void);
} test_funcs_c;

test_funcs_c * data test_funcs[NR_TEST_FUNCS];
uint8_t nr_test_funcs = 0;

void register_test_func(test_funcs_c *funcs)
{
if (nr_test_funcs < NR_TEST_FUNCS-1) {
test_funcs[nr_test_funcs] = funcs;
nr_test_funcs++;
}
}

void test_non(void)
{
dbg_dump(1);
}

test_funcs_c funcs1 = {
test_non,
};

test_funcs_c funcs2 = {
test_non,
};

static void test_funcs_ptr(void)
{
uint8_t i;
test_funcs_c *p;

register_test_func(&funcs1);
register_test_func(&funcs2);

for (i = 0; i < nr_test_funcs; i++) {
/* the way can work */
p = test_funcs[i];
(p->non)();

#if 0
/* the way cannot work */
(test_funcs[i]->non)();
#endif
}
}
============================================================

It seems a compiler bug, compiler cannot generate correct
codes for structure member pointed by '->' after an array
indexing.
Please look at the generated codes for the differences:

============================================================
; p = test_funcs[i];
mov a,r2
add a,r2
add a,#_test_funcs
mov r0,a
mov ar3,@r0
inc r0
mov ar4,@r0
dec r0
mov dpl,r3
mov dph,r4
; (p->non)();
clr a
movc a,@a+dptr
mov r3,a
inc dptr
clr a
movc a,@a+dptr
mov r4,a
push ar2
push ar3
push ar4
mov dpl,r3
mov dph,r4
lcall __sdcc_call_dptr
pop ar4
pop ar3
pop ar2
============================================================
; (test_funcs[i]->non)();
mov a,r2
add a,r2
add a,#_test_funcs
mov r0,a
mov ar0,@r0
mov ar3,@r0
inc r0
mov ar4,@r0
dec r0
push ar2
push ar3
push ar4
mov dpl,r3
mov dph,r4
lcall __sdcc_call_dptr
pop ar4
pop ar3
pop ar2
============================================================

Reported by: Lv Zheng, lv.zheng@soliton.com.cn

Discussion

  • Maarten Brock

    Maarten Brock - 2009-07-14

    Can you confirm this still exists in the latest snapshot (2.9.1 #5472)?

     
  • zetalog

    zetalog - 2009-07-15

    We tried "mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08 2.9.1
    #5472 (Jul 15 2009) (UNIX)", and problem remains.

     
  • Maarten Brock

    Maarten Brock - 2009-07-17

    Thanks for checking. I thought it had to do with my change before 2.9.0 that needed bugfixes afterward. But further investigation reveals this was already present in 2.6.0 or even before that.

     
  • Maarten Brock

    Maarten Brock - 2009-07-17

    simplified testcase

     
  • Maarten Brock

    Maarten Brock - 2009-07-20

    Fixed in SDCC 2.9.2 #5482.

     
  • Maarten Brock

    Maarten Brock - 2009-07-20
    • labels: 101550 --> Icode generator
    • milestone: --> fixed
    • assigned_to: nobody --> maartenbrock
    • status: open --> closed-fixed
     

Log in to post a comment.

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

Sign up for the SourceForge newsletter:





No, thanks