[LLVMdev] operator overloading fails while debugging with gdb for i386

Mayur Pandey mayurthebond at gmail.com
Sat Dec 1 03:13:12 PST 2012


Problem seems not only with operator overloading,  It occurs with struct
value returning also.

gdb while debugging expects the return value in eax, gcc does returns in
eax, But Clang returns in edx(it can be checked in gdb by printing the
contents of edx).

Code(sample code)

struct A1 {
  int x;
  int y;
};

A1 sum(const A1 one, const A1 two)
{
 A1 plus = {0,0};
 plus.x = one.x + two.x;
 plus.y = one.y + two.y;

 return (plus);
}


int main (void)
{
 A1 one= {2,3};
 A1 two= {4,5};
 A1 three = sum(one,two);
 return 0;
}


gcc assembley (snippet of sum function)

_Z3sum2A1S_:
    .loc 1 8 0
    pushl    %ebp
    movl    %esp, %ebp
    .loc 1 9 0
    movl    8(%ebp), %eax
    movl    $0, (%eax)
    movl    8(%ebp), %eax
    movl    $0, 4(%eax)
    .loc 1 10 0
    movl    12(%ebp), %edx
    movl    20(%ebp), %eax
    addl    %eax, %edx
    movl    8(%ebp), %eax
    movl    %edx, (%eax)
    .loc 1 11 0
    movl    16(%ebp), %edx
    movl    24(%ebp), %eax
    addl    %eax, %edx
    movl    8(%ebp), %eax
    movl    %edx, 4(%eax)
.LBE2:
    .loc 1 14 0
    *movl    8(%ebp), %eax*
    popl    %ebp
    ret    $4

clang assembly (snippet of sum function)
_Z3sum2A1S_:
   .loc    1 8 0
    pushl    %edi
    pushl    %esi
    leal    24(%esp), %eax
    leal    16(%esp), %ecx
    movl    12(%esp), %edx
    .loc    1 9 17 prologue_end
    movl    $0, 4(%edx)
    movl    $0, (%edx)
    .loc    1 10 2
    movl    (%ecx), %esi
    movl    (%eax), %edi
    addl    %edi, %esi
    movl    %esi, (%edx)
    .loc    1 11 2
    movl    4(%ecx), %ecx
    movl    4(%eax), %eax
    addl    %eax, %ecx
    *movl    %ecx, 4(%edx)*
    .loc    1 13 2
    popl    %esi
    popl    %edi
    ret    $4


But while returning int value clang returns with eax... as expect. Problem
comes with when used struct/class

Is the behaviour of llvm as per the standards or is this a bug??









On Thu, Nov 29, 2012 at 12:40 PM, Mayur Pandey <mayurthebond at gmail.com>wrote:

> For the given test:
>
> class A1 {
>   int x;
>   int y;
>
>   public:
>
>   A1(int a, int b)
>   {
>    x=a;
>    y=b;
>   }
>
> A1 operator+(const A1&);
> };
>
>
> A1 A1::operator+(const A1& second)
> {
>  A1 sum(0,0);
>  sum.x = x + second.x;
>  sum.y = y + second.y;
>
>  return (sum);
> }
>
>
> int main (void)
> {
>  A1 one(2,3);
>  A1 two(4,5);
>
>  return 0;
> }
>
>
> when the exectable of this code is debugged in gdb for i386, we dont get the
> expected results.
>
> when we break at return 0; and give the command: print one + two, the result
> should be $1 = {x = 6, y = 8}, but the result obtained is $1 = {x = 2, y = 3}.
>
> This is related to debug information generated, as normally the overloading is
> occuring.
> eg: A1 three = one + two results {x = 6, y = 8}.
>
>
> On checking the assembly, a suspicious entry is found which may be related for
> the failure:
>
>     #DEBUG_VALUE: operator+:this <- undef
>     #DEBUG_VALUE: operator+:second <- undef
>
>
>
> --
> Thanx & Regards
> *Mayur Pandey *
>
>
>
>
>
>


-- 
Thanx & Regards
*Mayur Pandey *
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121201/7ca25356/attachment.html>


More information about the llvm-dev mailing list