您的位置:首页 > 编程语言 > C语言/C++

C++ reference return/parameter value

2008-03-25 11:36 148 查看
souce C++ codes.

Point getPoint1(Point &p)
{
Point *pp = new Point(p);
return *pp;
}

Point &getPoint2(Point &p)
{
Point *pp = new Point(p);
return *pp;
}

Point getPoint3(Point p)
{
Point *pp = new Point(p);
return *pp;
}

Point &getPoint4(Point p)
{
Point *pp = new Point(p);
return *pp;
}

int main(int argc, char *argv[])
{
Point p0;

Point p1 = getPoint1(p0);
Point p2 = getPoint2(p0);
Point p3 = getPoint1(p0);
Point p4 = getPoint2(p0);
}

2. instructions analysis codes. (Sun C++ 5.6 Patch 117549-02 2005/02/08, SPARC)

Point getPoint1(Point &p)
{
st %i0,[%fp+68] # point the space to store return value, which is allocaded by caller
st %i1,[%fp+72] # point to p object
Point *pp = new Point(p);
call __1c2n6FI_pv_ # call constructor function of Point()
mov 28,%o0 # %o0 should be the address of pp which is return above
mov %o0,%i5
ld [%fp+72],%l0 # load the beginning of p object address
or %g0,28,%g1 # g0 always is 0, and 28 is the size of class Point
1:
subcc %g1,4,%g1
ld [%l0+%g1],%l2 # load value from p
bg 1b
st %l2,[%o0+%g1] # store value (loaded from p) to pp, it's a loop operations
st %i5,[%fp-4]
return *pp;

ld [%fp+68],%l1 # load the address of return Point object
or %g0,28,%g1
1:
subcc %g1,4,%g1
ld [%i5+%g1],%l2 # %i5 is the beginning address of new created object pp
bg 1b
st %l2,[%l1+%g1] # store value of pp to return address
jmp %i7+8
restore
}

Point &getPoint2(Point &p)
{
st %i0,[%fp+68] # point to parameter p
Point *pp = new Point(p);
call __1c2n6FI_pv_
mov 28,%o0 # it should be %o0 = %o0 + 28
mov %o0,%i5
ld [%fp+68],%l0
or %g0,28,%g1
1:
subcc %g1,4,%g1
ld [%l0+%g1],%l2
bg 1b
st %l2,[%o0+%g1] # copy parameter p to local pp
st %i5,[%fp-8]
return *pp;
ba .L9
mov %i5,%i4
mov %i4,%i0 # %i4 = %i5 = %o0 = address of local pp
jmp %i7+8
restore
}

Point getPoint3(Point p)
{ st %i0,[%fp+68] # return value object, it will be copied to really return value
st %i1,[%fp+72] # p object
Point *pp = new Point(p);
call __1c2n6FI_pv_
mov 28,%o0
mov %o0,%i5
ld [%fp+72],%l0
or %g0,28,%g1
1:
subcc %g1,4,%g1
ld [%l0+%g1],%l2
bg 1b
st %l2,[%o0+%g1] # copy value from parameter p to local pp
st %i5,[%fp-4]
return *pp;

ld [%fp+68],%l1
or %g0,28,%g1
1:
subcc %g1,4,%g1
ld [%i5+%g1],%l2
bg 1b
st %l2,[%l1+%g1] # copy value from local pp to temporary return space allocated by caller
jmp %i7+8
restore
}

Point &getPoint4(Point p)
{ st %i0,[%fp+68]
Point *pp = new Point(p);
call __1c2n6FI_pv_
mov 28,%o0
mov %o0,%i5
ld [%fp+68],%l0
or %g0,28,%g1
1:
subcc %g1,4,%g1
ld [%l0+%g1],%l2
bg 1b
st %l2,[%o0+%g1]
st %i5,[%fp-8]
return *pp;

ba .L15
mov %i5,%i4
mov %i4,%i0
jmp %i7+8
restore
}

int main(int argc, char *argv[])
{
st %i0,[%fp+68]
st %i1,[%fp+72]
Point p0;

Point p1 = getPoint1(p0);
add %fp,-60,%o0 # frame of getPoint1
add %fp,-32,%l1 # 60 - 32 = 28, it's the size of class Pointer; which is used in getPoint(...)
# that is to say, we first allocated space
call __1cJgetPoint16FrnFPoint__0_
mov %l1,%o1 # %l1 is the address of new allocated object;

Point p2 = getPoint2(p0); # Point &getPoint2(Point &p)
call __1cJgetPoint26FrnFPoint__1_
mov %l1,%o0
add %fp,-88,%l0
or %g0,28,%g1
1:
subcc %g1,4,%g1
ld [%o0+%g1],%l2
bg 1b
st %l2,[%l0+%g1] # copy the temporary variable to destination address
# there is an assign operation, Point p1 = Point &p2;
Point p3 = getPoint1(p0);
add %fp,-116,%o0
call __1cJgetPoint16FrnFPoint__0_
mov %l1,%o1
Point p4 = getPoint2(p0);
call __1cJgetPoint26FrnFPoint__1_
mov %l1,%o0
add %fp,-144,%l0
or %g0,28,%g1
1:
subcc %g1,4,%g1
ld [%o0+%g1],%l2
bg 1b
st %l2,[%l0+%g1]
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐