Problem seems not only with operator overloading, It occurs with struct value returning also.<br><br>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).<br>
<br>Code(sample code)<br><br>struct A1 {<br> int x;<br> int y;<br>};<br><br>A1 sum(const A1 one, const A1 two)<br>{<br> A1 plus = {0,0};<br> plus.x = one.x + two.x;<br> plus.y = one.y + two.y;<br><br> return (plus);<br>
}<br><br><br>int main (void)<br>{<br> A1 one= {2,3};<br> A1 two= {4,5};<br> A1 three = sum(one,two);<br> return 0;<br>}<br><br><br>gcc assembley (snippet of sum function)<br><br>_Z3sum2A1S_:<br> .loc 1 8 0<br> pushl %ebp<br>
movl %esp, %ebp<br> .loc 1 9 0<br> movl 8(%ebp), %eax<br> movl $0, (%eax)<br> movl 8(%ebp), %eax<br> movl $0, 4(%eax)<br> .loc 1 10 0<br> movl 12(%ebp), %edx<br> movl 20(%ebp), %eax<br>
addl %eax, %edx<br> movl 8(%ebp), %eax<br> movl %edx, (%eax)<br> .loc 1 11 0<br> movl 16(%ebp), %edx<br> movl 24(%ebp), %eax<br> addl %eax, %edx<br> movl 8(%ebp), %eax<br> movl %edx, 4(%eax)<br>
.LBE2:<br> .loc 1 14 0<br> <b>movl 8(%ebp), %eax</b><br> popl %ebp<br> ret $4<br><br>clang assembly (snippet of sum function)<br>_Z3sum2A1S_: <br> .loc 1 8 0 <br>
pushl %edi<br> pushl %esi<br> leal 24(%esp), %eax<br> leal 16(%esp), %ecx<br> movl 12(%esp), %edx<br> .loc 1 9 17 prologue_end <br> movl $0, 4(%edx)<br> movl $0, (%edx)<br>
.loc 1 10 2 <br> movl (%ecx), %esi<br> movl (%eax), %edi<br> addl %edi, %esi<br> movl %esi, (%edx)<br> .loc 1 11 2 <br> movl 4(%ecx), %ecx<br> movl 4(%eax), %eax<br>
addl %eax, %ecx<br> <b>movl %ecx, 4(%edx)</b><br> .loc 1 13 2 <br> popl %esi<br> popl %edi<br> ret $4<br><br><br>But while returning int value clang returns with eax... as expect. Problem comes with when used struct/class<br>
<br>Is the behaviour of llvm as per the standards or is this a bug??<br><br><br><br><br><br><br><br><br><br><div class="gmail_quote">On Thu, Nov 29, 2012 at 12:40 PM, Mayur Pandey <span dir="ltr"><<a href="mailto:mayurthebond@gmail.com" target="_blank">mayurthebond@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><pre style="text-indent:0px;letter-spacing:normal;font-variant:normal;text-align:start;width:50em;font-style:normal;font-weight:normal;line-height:normal;text-transform:none;font-size:small;white-space:pre-wrap;font-family:monospace;word-spacing:0px">
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</pre><span class="HOEnZb"><font color="#888888"><br clear="all"><br>-- <br><div>Thanx & Regards <br></div>
<div><b>Mayur Pandey </b><br></div><br><div><font color="#3333ff"> <br></font></div>
<div> </div>
<div> </div><br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br><div>Thanx & Regards <br></div>
<div><b>Mayur Pandey </b><br></div><br><div><font color="#3333ff"> <br></font></div>
<div> </div>
<div> </div><br>