Menu

#28 OnBC: Fix codegen for functions returning structs

OnBoard_C
open
nobody
None
5
2004-01-13
2004-01-13
No

As per Philippe Guillot's email:

"the generated code for functions that return a
structure is not alway correct. Code like:

typedef union {
UInt32 u;
struct {UInt16 hi;UInt16 lo} hh
} memoryword;

memoryword foo() { blablabla }
memoryword bar() { ... return foo(); }

compiles but does not run properly due to incorrectly
generated code.
(one has to write ... m=foo(); return m; }"

Discussion

  • Steve Little

    Steve Little - 2004-01-13

    Logged In: YES
    user_id=583634

    Slight change of description, plus full details from Philippe:

    "Contrarily to what I said, it does not occur when the return
    value is another function result, but it occurs when the return
    value is an array element:

    This is the source code:
    #pragma segment 2
    typedef union {
    UInt32 u;
    Int32 i;
    struct { UInt16 hi; UInt16 lo; } hh;
    struct { UInt8 b3; UInt8 b2; UInt8 b1; UInt8 b0; } qqqq;
    } MWord;

    MWord* Mem; // a global variable
    MWord brol(int n) {
    return Mem[n];
    }

    And this is the generated code (I added comments) :

    ; function Brol
    0000 link a6,#0
    0004 move.l -4(a5),a0 ; get global Mem address in a0
    0008 move 12(a6),d0 ; get n value in d0 (parameter)
    000C lsl 2,d0 ; multiply by 4 (because size of MWord
    is 4)
    000E add d0,d0 ; ????!!!! this is the bug.
    0010 lea 0(a0,d0),a0 ; get address of Mem[n] in a0
    0014 move.l 8(a6),a1 ; get de destination address in a1
    0018 move.l (a0)+,(a1)+ ; transfert the data
    001A unlk a6 ; epilog
    001C rts

    I have observed this bug only in a function return value.
    Otherwize, the code that generate the address of similar
    expression (such as Mem[n].hh.hi=5;) seems correct."

     
  • Steve Little

    Steve Little - 2004-01-13

    Logged In: YES
    user_id=583634

    A bit more info: (from Philippe)

    " - The same bug occurs if MWord is a 4 bytes structure
    - if MWord is 6 bytes then there are TWO instructions muls
    #6,d0 (in place of lsl 2,d0 above)"

     
  • John Wilund

    John Wilund - 2004-01-15

    Logged In: YES
    user_id=574706

    I sencond! Please make this a voting matter.

     

Log in to post a comment.