Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#2239 incorrect address generated for global with offset

closed-fixed
Ben Shi
None
Front-end
7
2015-01-24
2013-12-29
ClinicallyObeast
No
unsigned char data[100];
unsigned char* datapos;

#define ENT_DAT(_p) (*((unsigned int*)((unsigned char*)(_p) + 3)))

void setFoo()
{
    // this line goes wrong. the address generated should be
    // datapos -5 + 3 but you get datapos + 0x00FE (not 0xFFFE)
    ENT_DAT(datapos - 5) = 0xffff;
}

int main()
{
    datapos = data + 5;
    setFoo();
    return 0;
}

code i get:

_setFoo:
;bug1.c:9: ENT_DAT(datapos - 5) = 0xffff;
    ld  a,(#_datapos + 0)
    add a, #0xFE
    ld  d,a
    ld  a,(#_datapos + 1)
    adc a, #0x00                     <<<<< is wrong, should be 0xff ?
    ld  e,a
    ld  l, d
    ld  h, e
    ld  (hl),#0xFF
    inc hl
    ld  (hl),#0xFF
    ret
_setFoo_end::

sdcc -v
SDCC : mcs51/gbz80/z80/z180/r2k/r3ka/ds390/pic16/pic14/TININative/ds400/hc08/s08
3.3.0 #8604 (May 11 2013) (MINGW32)

more info contact,

hughserious@gmail.com

thanks,
-- hugh.

Discussion

<< < 1 2 (Page 2 of 2)
  • Increasing priority, since bad code is generated silently.

    Philipp

     
    • summary: incorrect address generated for global with offset Z80 --> incorrect address generated for global with offset
     
  • Maarten Brock
    Maarten Brock
    2014-02-24

    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,7 +1,8 @@
    +~~~~
     unsigned char data[100];
     unsigned char* datapos;
    
    -\#define ENT_DAT(_p) (*((unsigned int*)((unsigned char*)(_p) + 3)))
    +#define ENT_DAT(_p) (*((unsigned int*)((unsigned char*)(_p) + 3)))
    
     void setFoo()
     {
    @@ -17,9 +18,10 @@
         setFoo();
         return 0;
     }
    -
    +~~~~
     code i get:
    
    +~~~~
     _setFoo:
     ;bug1.c:9: ENT_DAT(datapos - 5) = 0xffff;
        ld  a,(#_datapos + 0)
    @@ -35,7 +37,7 @@
        ld  (hl),#0xFF
        ret
     _setFoo_end::
    -
    +~~~~
     sdcc -v
     SDCC : mcs51/gbz80/z80/z180/r2k/r3ka/ds390/pic16/pic14/TININative/ds400/hc08/s08
      3.3.0 #8604 (May 11 2013) (MINGW32)
    
     
  • Ben Shi
    Ben Shi
    2015-01-23

    I simplified test case to

    char * p;

    void f1(void)
    {
    * (p + 3 - 5) = 0xaa;
    }

    void f2(void)
    {
    * ((char * ) (p + 3 - 5)) = 0xaa;
    }

    f1() was right, and f2() went wrong even in the AST stage.

     
    Last edit: Ben Shi 2015-01-23
  • Ben Shi
    Ben Shi
    2015-01-23

    • assigned_to: Ben Shi
    • Category: other --> Front-end
     
  • Ben Shi
    Ben Shi
    2015-01-24

    • status: open --> closed-fixed
     
  • Ben Shi
    Ben Shi
    2015-01-24

    Fixed in reversion #9160.

     
<< < 1 2 (Page 2 of 2)