函数调用如何工作 [英] How function calls work

查看:60
本文介绍了函数调用如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,

我对编译器如何实现函数调用感兴趣,任何人都可以更正我的理解/指向一些好的文章。


当一个函数被调用时,堆栈指针是否增加了被调用函数中声明的变量的大小

,然后函数将

知道这些在内存中相对于当前堆栈指针的位置

位置。另外如何返回变量,是通过

堆栈中的设定位置??




void func1( )

{

int a;

}


void func2()

{

int a,b;

char c;

}


这些都会以完全相同的速度执行吗?

谢谢


Mike

Guys,
I''m interested in how the compiler implements function calls, can anyone
correct my understanding/point me towards some good articles.

When a function is called, is the stack pointer incremented by the size of
the variables declared in the called function, and then the function will
know where these are in memory relative to the current stack pointer
position. ALso how are variables returned, is that by a set position in the
stack??
ie,

void func1()
{
int a;
}

void func2()
{
int a,b;
char c;
}

will these both execute at exactly the same speed??
Thanks

Mike

推荐答案

Michael写道:
Michael wrote:
伙计们,
我对编译器如何实现函数调用感兴趣,任何人都可以更正我的理解/指向一些好的文章。

当一个函数被调用时,堆栈指针是否会增加被调用函数中声明的变量的大小,然后函数将知道它们在哪里内存相对于当前堆栈指针的位置。另外,如何返回变量,是通过
堆栈中的设定位置??



void func1()
{
int a;
}

void func2()
{
int a,b;
char c;
} 谢谢

迈克
Guys,
I''m interested in how the compiler implements function calls, can anyone
correct my understanding/point me towards some good articles.

When a function is called, is the stack pointer incremented by the size of
the variables declared in the called function, and then the function will
know where these are in memory relative to the current stack pointer
position. ALso how are variables returned, is that by a set position in the
stack??
ie,

void func1()
{
int a;
}

void func2()
{
int a,b;
char c;
}

will these both execute at exactly the same speed??
Thanks

Mike




它取决于所有编译器。有些机器甚至没有堆叠。


至于返回,它还取决于。我已经看到基于寄存器和堆栈的

返回类型,对于基于寄存器的返回类型,它取决于返回的

类型的数据。


例如:Green Hills C-68K。返回D0中的整数类型,指针

在A0中的类型,浮点/双在FP0中。


Whitesmith的Z80 C编译器:在HL中返回,BC / HL或堆栈,

取决于。


它依赖于编译器和芯片架构。



It''s all compiler dependent. Some machines don''t even have a stack.

As for return, it also depends. I''ve seen both register and stack based
return types, and for register based return types, it depends on the
type of data being returned.

Example: Green Hills C-68K. Returned integer types in D0, pointer
types in A0, and float/double in FP0.

Whitesmith''s Z80 C compiler: returned in either HL, BC/HL, or the stack,
depending.

It''s compiler and chip architecture dependent.


Michael写道:
Michael wrote:
伙计们,
我对编译器如何实现函数调用感兴趣,任何人都可以更正我的理解/指向一些好的文章。

当一个函数被调用时,堆栈指针是否增加了被调用函数中声明的变量的大小,然后函数将知道这些函数在哪里在内存中相对于当前堆栈指针的位置。那么如何返回变量,是通过
堆栈中的设定位置??


在一般情况下(带有堆栈的机器!),你是对的。

编译器可以优化本地变量到寄存器,这两个例子你引用的
很可能是这样做的。


如前所述,返回方法非常适合编译器和芯片。通过保留堆栈中的空间,通过

寄存器和大型对象返回适合寄存器或寄存器对的东西是典型的



有些机器(例如Sparc)为''in''

和'out''函数调用变量保留了寄存器集。


Ian

ie,

void func1()
{
int a;
}

void func2()
{/ / b>
char c;
}
这些都会以完全相同的速度执行吗? ?
谢谢

Mike
Guys,
I''m interested in how the compiler implements function calls, can anyone
correct my understanding/point me towards some good articles.

When a function is called, is the stack pointer incremented by the size of
the variables declared in the called function, and then the function will
know where these are in memory relative to the current stack pointer
position. ALso how are variables returned, is that by a set position in the
stack??

In the general case (machines with a stack!), you are correct. The
compiler may optimise the local variable to registers, both the examples
you quote are likely to be done this way.

As said before, return method is very compiler and chip specific. It''s
typical to return things that fit in a register or register pair by
register and large objects by reserving space on the stack.

Some machines (Sparc for example) have register sets reserved for ''in''
and ''out'' variables for function calls.

Ian
ie,

void func1()
{
int a;
}

void func2()
{
int a,b;
char c;
}

will these both execute at exactly the same speed??
Thanks

Mike



2004年5月13日星期四23:00:19 + 0000(UTC),Michael

< sl *********** @ hotmail.com>写道:
On Thu, 13 May 2004 23:00:19 +0000 (UTC), "Michael"
<sl***********@hotmail.com> wrote:
伙计们,
我对编译器如何实现函数调用感兴趣,任何人都可以更正我的理解/指向一些好的文章。

当一个函数被调用时,堆栈指针是否会增加被调用函数中声明的变量的大小,然后函数将知道它们在内存中的位置相对于当前堆栈指针的位置。那么如何返回变量,是通过
堆栈中的设定位置?


正如红色所说,这取决于。但这是一个粗略的实际或好像的b / b
任务,需要以某种方式或其他方式发生,或者它们的等价物,在

a函数调用序列中(免责声明) :这是基于25岁的

手工编码的8位编译器实现经验,所以带上一块

粒盐:


1.来电者保存它想要保存的寄存器(除非它知道功能计划

除了它会改变...)


2.调用者评估参数,将它们推入堆栈(通常在

反向订单中,因此第一个位于固定位置,以获得利益
$ b $可变电话的b)


3.来电电话功能


4.(如果#1没有发生)功能保存寄存器计划吹走


5.功能分配堆栈所有本地数据的空间都是同时存在的(一般的误解是块块范围数据是在进入块时分配的;这不是真的它在那里进入范围,但在进入封闭功能时可能已经分配了
。这样可以节省大量的分配/解除分配费用。执行期间堆叠

函数。)


6.函数做它的东西


7.函数de-分配它的本地堆栈帧(un-doing#5)


8.函数将返回值放在哪里


9.函数恢复它保存的寄存器在#4,如果有的话


10.功能返回给来电者


11.来电者从任何地方获取回报价值,并把它放在任何地方,如果需要




12.来电者从堆栈中弹出args(当然都是一次性的)


13.来电者恢复已保存的寄存器(如果有的话)


即,

void func1()
{
int a;
}

void func2()
{
int a,b;
char c;
}
这些都会以完全相同的速度执行吗?
Guys,
I''m interested in how the compiler implements function calls, can anyone
correct my understanding/point me towards some good articles.

When a function is called, is the stack pointer incremented by the size of
the variables declared in the called function, and then the function will
know where these are in memory relative to the current stack pointer
position. ALso how are variables returned, is that by a set position in the
stack??
As red says, it depends. But here''s a rough checklist of actual or "as if"
tasks that need to happen somehow or another, or their equivalents, during
a function call sequence (disclaimer: this is based on 25-year-old
hand-coded 8-bit compiler implementation experience, so take that with a
grain of salt):

1. caller saves registers it wants saved (unless it knows function plans to
save ones it will alter...)

2. caller evaluates arguments, pushes them on the stack (typically in
reverse order, so the first one is in a "fixed" location, for the benefit
of variadic calls)

3. caller "calls" function

4. (if #1 didn''t happen) function saves registers it plans on blowing away

5. function allocates stack space for all its local data, all at once (a
common misconception is that block-scope data is allocated upon entry to
the block; that''s not true. It "comes into scope" there, but has probably
already been allocated upon entry to the enclosing function. That saves an
awful lot of allocation/deallocation on the stack during execution of
functions.)

6. function does its thing

7. function de-allocates its local stack frame (un-doing #5)

8. function puts return value wherever

9. function restores registers it saved in #4, if any

10. function "returns" to caller

11. caller grabs return value from wherever, and puts it wherever, if
necessary

12. caller pops args off the stack (all at once, of course)

13. caller restores saved registers (if any)


ie,

void func1()
{
int a;
}

void func2()
{
int a,b;
char c;
}

will these both execute at exactly the same speed??




谁知道?有点取决于是否清理堆栈。可以通过func2以func1快速完成

。我猜想func1可以通过一个简单的
pop来实现,而func2可能需要几个周期来增加堆栈

指针由一些任意数字(12字节?)。但是,现代编译器可以很好地信任
,就像他们可以做到的那样聪明地做事情,

所以我不会太担心它。 />
-leor

PS免责声明#2:同样,所有这一切只是为了大致了解函数调用所带来的东西,而不是宣称所有的东西都是

这个开销总是花费你几百亿美元的时间。但它确实使函数内联(无论是显式的还是

,编译器都像Java一样)当函数简短而且甜美的时候看起来真的非常好b / b
的想法...

-

Leor Zolman --- BD软件--- www。 bdsoft.com/tools/stlfilt.html


这篇关于函数调用如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆