mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 12:32:21 +00:00
ch3: 增加函数栈帧布局图
This commit is contained in:
parent
9f05873fea
commit
2e57dac359
@ -191,6 +191,8 @@ func Foo() {
|
|||||||
|
|
||||||
在前文中我们已经学习过一些汇编实现的函数参数和返回值处理的规则。那么一个显然的问题是,汇编函数的参数是从哪里来的?答案同样明显,被调用函数的参数是有调用方准备的:调用方在栈上设置好空间和数据后调用函数,被调用方在返回前将返回值放如对应的位置,函数通过RET指令返回调用放函数之后,调用方从返回值对应的栈内存位置取出结果。Go语言函数的调用参数和返回值均是通过栈传输的,这样做的有点是函数调用栈比较清晰,缺点是函数调用有一定的性能损耗(Go编译器是通过函数内联来缓解这个问题的影响)。
|
在前文中我们已经学习过一些汇编实现的函数参数和返回值处理的规则。那么一个显然的问题是,汇编函数的参数是从哪里来的?答案同样明显,被调用函数的参数是有调用方准备的:调用方在栈上设置好空间和数据后调用函数,被调用方在返回前将返回值放如对应的位置,函数通过RET指令返回调用放函数之后,调用方从返回值对应的栈内存位置取出结果。Go语言函数的调用参数和返回值均是通过栈传输的,这样做的有点是函数调用栈比较清晰,缺点是函数调用有一定的性能损耗(Go编译器是通过函数内联来缓解这个问题的影响)。
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
为了便于演示,我们先用Go语言构造foo和bar两个函数,其中foo函数内部调用bar函数:
|
为了便于演示,我们先用Go语言构造foo和bar两个函数,其中foo函数内部调用bar函数:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
BIN
images/ch3-func-call-frame-01.ditaa.png
Normal file
BIN
images/ch3-func-call-frame-01.ditaa.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 36 KiB |
51
images/ch3-func-call-frame-01.ditaa.txt
Normal file
51
images/ch3-func-call-frame-01.ditaa.txt
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
function call frame
|
||||||
|
|
||||||
|
+----------+<-=-g.stack.hi
|
||||||
|
| stack |
|
||||||
|
| cRED | func main()
|
||||||
|
| | main frame
|
||||||
|
+----------+<-=-----+----------+
|
||||||
|
| main | | cGRE |
|
||||||
|
| cGRE | | local | func printsum(a, b int)
|
||||||
|
| | | | printsum frame
|
||||||
|
| | +----------+ +----------+
|
||||||
|
| | | | : |
|
||||||
|
| | | +8(SP) |----=--->| b+8(FP) |
|
||||||
|
| call | +8(SP)| cPNK | arg b | cGRE |b+8(FP)
|
||||||
|
| printsum | +----------+ +----------+
|
||||||
|
| | | | : |
|
||||||
|
| | | +0(SP) |----=--->| a+0(FP) |
|
||||||
|
| | +0(SP)| cPNK | arg a | cGRE |a+0(FP)
|
||||||
|
+----------+<-=-----+----------+<-=------+----------+
|
||||||
|
| printsum | | |
|
||||||
|
| cYEL | |var c int | func sum(a, b int) int
|
||||||
|
| | | cYEL |c-8(SP) sum frame
|
||||||
|
| | +----------+ +----------+
|
||||||
|
| call sum | | | : |
|
||||||
|
| | | sum.ret |<----=---|ret+24(FP)|
|
||||||
|
| | +16(SP)| cPNK | return | |ret+16(FP)
|
||||||
|
| | +----------+ +----------+
|
||||||
|
| | | | : |
|
||||||
|
| | | sum.b |-----=-->| b+8(FP) |
|
||||||
|
| | +8(SP)| cPNK | arg b | |b+8(FP)
|
||||||
|
| | +----------+ +----------+
|
||||||
|
| | | | : |
|
||||||
|
| | | sum.a |-----=-->| a+0(FP) |
|
||||||
|
| | +0(SP)| cPNK | arg a | |a+0(FP)
|
||||||
|
+----------+<-=--------------------------+----------+<-=------+----------+
|
||||||
|
| func sum | | cBLU |t-8(SP)
|
||||||
|
| cBLU | | local |
|
||||||
|
| ret a+b | | |
|
||||||
|
+----------+<-=-----------------------------------------------+----------++0(SP)
|
||||||
|
| cAAA |+0(SP)
|
||||||
|
| unused |
|
||||||
|
| |
|
||||||
|
| |
|
||||||
|
| |
|
||||||
|
| |
|
||||||
|
+----------+<-=-g.stack.lo
|
||||||
|
| cRED |
|
||||||
|
|StackLimit|
|
||||||
|
|StackGuard|
|
||||||
|
+----------+
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user