Menu

Wrong complier with struct

Jirat
2023-02-03
2023-10-05
  • Jirat

    Jirat - 2023-02-03

    I write code as follow.

    typedef struct
    {
    uint8_t data1;
    uint8_t data2;
    uint8_t data3;
    uint16_t data4;
    uint16_t data5;
    uint16_t data6;
    }testStructdef_t;

    _pdata testStructdef_t test;

    uint16_t convertTest(uint8_t ch)
    {
    uint16_t data;
    data = (uint16_t)
    return
    }

    void initValue(uint8_t ch , testStructdef_t *test)
    {

    //uint16_t buf;
    test->data1 = 4;
    test->data2= 5;
    test->data3 = 6;
    test->data4 = 4;
    test->data5 = convertTest(ch);
    test->data6 = test->data5 * 16;
    }

    main()
    {
    initValue(7,&test)
    while(1)
    {
    }
    }

    After check listing file we found the problem at this line.
    lcall _convertTest
    0001BB AF 82 [24] 567 mov r7,dpl
    0001BD AB 83 [24] 568 mov r3,dph
    0001BF D0 04 [24] 569 pop ar4
    0001C1 D0 05 [24] 570 pop ar5
    0001C3 D0 06 [24] 571 pop ar6
    0001C5 A8 2A [24] 572 mov r0,_bp
    0001C7 08 [12] 573 inc r0
    0001C8 08 [12] 574 inc r0
    0001C9 86 82 [24] 575 mov dpl,@r0
    0001CB 08 [12] 576 inc r0
    0001CC 86 83 [24] 577 mov dph,@r0
    0001CE 08 [12] 578 inc r0
    0001CF 86 F0 [24] 579 mov b,@r0
    0001D1 EF [12] 580 mov a,r7
    0001D2 12 03 DD [24] 581 lcall gptrput
    0001D5 A3 [24] 582 inc dptr
    0001D6 EB [12] 583 mov a,r3
    0001D7 12 03 DD [24] 584 lcall gptrput
    585 ; .\src\main.c:84: test->data6 = (uint16_t) (test->data5 * 16);
    0001DA 74 07 [12] 586 mov a,#0x07
    0001DC 2C [12] 587 add a,r4
    0001DD FC [12] 588 mov r4,a
    0001DE E4 [12] 589 clr a
    0001DF 3D [12] 590 addc a,r5
    0001E0 FD [12] 591 mov r5,a
    0001E1 8F 03 [24] 592 mov ar3,r7
    0001E3 8B 07 [24] 593 mov ar7,r3

    0001E5 EF [12] 594 mov a,r7**

     

    Last edit: Jirat 2023-02-03
  • Jirat

    Jirat - 2023-02-03

    This 2 line are problem .

    0001E1 8F 03 [24] 592 mov ar3,r7
    0001E3 8B 07 [24] 593 mov ar7,r3

    r7 and r3 keep the result from function "convertTest"
    and it destroy itself before use to mul with 16.

    If I rewrite initValue function to

    void initValue( testStructdef_t *test,uint8_t ch )

    Just swap the parameter and pass with new order parameter.
    then the problem will be solved.
    Anyone found this problem before?

     

    Last edit: Jirat 2023-02-03
  • Jirat

    Jirat - 2023-02-03

    Please note such problem will occur once I enable "--stack-auto" flag.

     
  • Benedikt Freisen

    The first thing that catches my eye is that this function here looks a bit broken:

    uint16_t convertTest(uint8_t ch)
    {
        uint16_t data;
        data = (uint16_t)
        return
    }
    

    Shouldn't that rather be something like the following?

    uint16_t convertTest(uint8_t ch)
    {
        uint16_t data;
        data = (uint16_t) ch;
        return data;
    }
    
     
    • Jirat

      Jirat - 2023-02-06

      uint16_t convertTest(uint8_t ch)
      {
      uint16_t data;
      data = (uint16_t) ch;
      return data;
      }

      Yes,you're right, I mistype. But it does not affect anything.
      You could write above code by yourself to check it.
      Please compile to MCS51 mcu ,you will get the same problem.

       
  • Philipp Klaus Krause

    I can reproduce the issue using the attached regression test with current sdcc on my Debian GNU/Linux testing system on amd64.
    It affects the mcs51 port with --stack-auto only; but there I see it for small, medium and large memory model. Other ports with --stack-auto (or ports that put local variables on the stack by default) are apparently not affected.

    Please file a bug report.

     
  • Oleg Endo

    Oleg Endo - 2023-09-02

    This looks like [#3607] to me, which has been fixed.

     

    Related

    Bugs: #3607

  • Maarten Brock

    Maarten Brock - 2023-10-05

    Bug filed as [#3554]

     

    Related

    Bugs: #3554


Log in to post a comment.