歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> ARM FP寄存器及frame pointer介紹

ARM FP寄存器及frame pointer介紹

日期:2017/3/1 9:59:58   编辑:Linux編程

理論上來說,ARM的15個通用寄存器是通用的,但實際上並非如此,特別是在過程調用的過程中。
PCS(Procedure Call Standard for Arm architecture)就定義了過程調用中,寄存器的特殊用途。

Role in the procedure call standard

r15 PC The Program Counter.

r14 LR The Link Register.

r13 SP The Stack Pointer.

r12 IP The Intra-Procedure-call scratch register. (可簡單的認為暫存SP)

實際上,還有一個r11是optional的,被稱為FP,即frame pointer。

1,stack frame

stack我們都知道,每一個進程都有自己的棧。考慮進程執行時發生函數調用的場景,母函數和子函數使用的是同一個棧,在通常的情況下,我們並不需要區分母函數和子函數分別使用了棧的哪個部分。但是,當我們需要在執行過程中對函數調用進行backtrace的時候,這一信息就很重要了。

簡單的說,stack frame就是一個函數所使用的stack的一部分,所有函數的stack frame串起來就組成了一個完整的棧。stack frame的兩個邊界分別由FP和SP來限定。

2,backtrace

在程序執行過程中(通常是發生了某種意外情況而需要進行調試),通過SP和FP所限定的stack frame,就可以得到母函數的SP和FP,從而得到母函數的stack frame(PC,LR,SP,FP會在函數調用的第一時間壓棧),以此追溯,即可得到所有函數的調用順序。

3,gcc關於stack frame的優化選項

看起來FP只是在backtrace的時候有用,所以如果我們沒有backstrace的需求,我們是否可以不使用FP。

其實gcc就有一個關於stack frame的優化選項:

-fomit-frame-pointer

=================================================================================

Don't keep the frame pointer in a register for functions that don't need one. This avoids the instructions to save, set up and restore frame pointers; it also makes an extra register available in many functions. It also makes debugging impossible on some machines.


On some machines, such as the VAX, this flag has no effect, because the standard calling sequence automatically handles the frame pointer and nothing is saved by pretending it doesn't exist. The machine-description macro "FRAME_POINTER_REQUIRED" controls whether a target machine supports this flag.

====================================================

這裡引用別人關於這一參數的實驗,自己就不做了。

從實驗可以看出,優化後的差別是相當明顯的。當然,具體能帶來多大的性能提升,不好界定。

另外,x86中EBP寄存器相當於ARM中的FP寄存器。

====================================================

Copyright © Linux教程網 All Rights Reserved