#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

  • markdown fail.

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

     
  • markdown fail again. too bad.

     
  • Using sdcc 3.3.2 #8930 I get:

    error 10: 'lvalue' required for 'assignment' operation.

    Which looks good to me.

    Philipp

     
    • it's because the "*" were deleted by markdown, try this:

      PS. i have 3.3.0, is there 3.3.2 anywhere to download.

      thanks,
      -- hugh.


      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;
      }

      On 29/12/2013 21:15, Philipp Klaus Krause wrote:

      Using sdcc 3.3.2 #8930 I get:

      error 10: 'lvalue' required for 'assignment' operation.

      Which looks good to me.

      Philipp


       
      Last edit: Maarten Brock 2014-01-06
  • Just attach a file with compileable source. No use trying to get anything meaningful other than natural language across in the bug report itself.

    Philipp

     
    • thanks. file attached.

      On 29/12/2013 22:39, Philipp Klaus Krause wrote:

      Just attach a file with compileable source. No use trying to get
      anything meaningful other than natural language across in the bug
      report itself.

      Philipp


       
      Attachments
    • Category: Z80 --> other
     
  • Thanks. I now see the issue in all ports I tried (z80, mcs51, stm8).

    Philipp

    P.S.: 3.3.2 is not a release. It is just the version reported by the current development verion.

     
  • Maarten Brock
    Maarten Brock
    2014-01-06

    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,7 +1,7 @@
     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()
     {
    
     
    • Priority: 5 --> 7
     
  • 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.