線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:6882
推到 Plurk!
推到 Facebook!

請問"__fastcall"的使用時機

答題得分者是:CoffeeX
remmurds
一般會員


發表:17
回覆:14
積分:16
註冊:2006-10-29

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-02-05 17:03:41 IP:140.130.xxx.xxx 訂閱
如題
如果在視窗程式中有自訂的函數
究竟在什麼情況下要在函數前加上"__fastcall"的修飾詞?
還請各位不吝指教

------
隨筆網誌: http://reassert.blogspot.com
CoffeeX
中階會員


發表:18
回覆:121
積分:72
註冊:2005-02-18

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-02-06 13:18:12 IP:218.164.xxx.xxx 訂閱
請善用help檔喔!
不過我還沒研讀過@@,...

<basefont></basefont> Use the __fastcall modifier to declare functions that expect parameters to be passed in registers. The first three parameters are passed (from left to right) in EAX, EDX, and ECX, if they fit in the register. The registers are not used if the parameter is a floating-point or struct type. All form class member functions must use the __fastcall convention. The compiler treats this calling convention as a new language specifier, along the lines of _cdecl and _pascal Functions declared using _cdecl or _pascal cannot have the _fastcall modifier because they use the stack to pass parameters. Likewise, the __fastcall modifier cannot be used together with _export. The compiler prefixes the __fastcall function name with an at-sign ("@"). This prefix applies to both unmangled C function names and to mangled C function names. For Microsoft C style __fastcall implementation, see __msfastcall and __msreturn.

<basefont></basefont> Note: The __fastcall modifier is subject to name mangling. See the description of the -VC option.
------
=.=???
rogan321
高階會員


發表:21
回覆:307
積分:200
註冊:2003-05-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-02-21 02:44:30 IP:219.68.xxx.xxx 訂閱
 簡單講就是你用VCL元件時就請宣告為 __fastcall call...
暗黑破壞神
版主


發表:9
回覆:2301
積分:1627
註冊:2004-10-04

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-02-21 11:17:08 IP:61.225.xxx.xxx 訂閱
換個角度來說。
C 語言在函數呼叫時,它第一個函數到最後一個函數的傳遞方式
跟 pascal 不一樣。
兩者的先後順序不同。所以你要用個 fastcall 來跟你的 compiler 說你這個函數是
要用那個方式來傳參數.
CoffeeX
中階會員


發表:18
回覆:121
積分:72
註冊:2005-02-18

發送簡訊給我
#5 引用回覆 回覆 發表時間:2007-02-21 17:13:30 IP:218.169.xxx.xxx 訂閱
補充一下...
主題: When to use fastcall

http://bcbjournal.com/articles/vol4/0004/When_to_use___fastcall.htm

請自行閱讀,
懂翻譯的麻煩一下了...^^

大意上,
說明的很詳細,
無需自己加上_fastcall,
該加的BCB會自己加,
實測中(_fastcall)並不會有更高的效率,
說錯的話麻煩指正一下,在下的英文不是很好...
------
=.=???
暗黑破壞神
版主


發表:9
回覆:2301
積分:1627
註冊:2004-10-04

發送簡訊給我
#6 引用回覆 回覆 發表時間:2007-02-21 19:48:01 IP:61.225.xxx.xxx 訂閱
其實。你只要寫一段下去測試。看它的 asm 就知道了。
比方說我寫個簡單的測試如下
[code]
int __fastcall TForm1::f1(int i1, int i2, int i3, int i4)
004016C0 55 push ebp
004016C1 8BEC mov ebp,esp

004016C3 83C4F0 add esp,-0x10
004016C6 894DF4 mov [ebp-0x0c],ecx
004016C9 8955F8 mov [ebp-0x08],edx
004016CC 8945FC mov [ebp-0x04],eax
i = i1 i2 i3 i4;
004016CF 8B45F8 mov eax,[ebp-0x08]
004016D2 0345F4 add eax,[ebp-0x0c]
004016D5 03450C add eax,[ebp 0x0c]
004016D8 034508 add eax,[ebp 0x08]
004016DB 8945F0 mov [ebp-0x10],eax
return i;
004016DE 8B45F0 mov eax,[ebp-0x10]
}
}004016E1 8BE5 mov esp,ebp
004016E3 5D pop ebp
004016E4 C20800 ret 0x0008
004016E7 90 nop

int TForm1::f1(int i1, int i2, int i3, int i4)
{
004016E8 55 push ebp
004016E9 8BEC mov ebp,esp
004016EB 51 push ecx
i = i1 i2 i3 i4;
004016EC 8B450C mov eax,[ebp 0x0c]
004016EF 034510 add eax,[ebp 0x10]
004016F2 034514 add eax,[ebp 0x14]
004016F5 034518 add eax,[ebp 0x18]
004016F8 8945FC mov [ebp-0x04],eax

return i;
004016FB 8B45FC mov eax,[ebp-0x04]
}
004016FE 59 pop ecx
004016FF 5D pop ebp
00401700 C3 ret
[/code]這樣看起來。是不是就有所差別了?
它們對於參數的處理方式就不同了。
我所舉的例子為固定參數。也就是沒有像 printf 那種不定個數的參數。
在 C 跟 pascal 不同的處理,應是在不定參數上面。__fastcall 應在那上面有更大的不同。
而我只做了固定參數的測試。你可以測一下不定參數時的情況。
而你所提的那個網頁,我也去看了。
它的測試方法有幾個問題。
1.並沒有考慮到不定參數時的情況。
2.它並沒有考慮到 cache 的情況。
也就是說程式太小。根本還沒被移出 cpu 的 level 1 cache .
由我寫的那一段可以看到指令沒幾個。
所以那樣的測試並不準確。

3.再者。所謂的 fastcall 並不是說它呼叫起來會比較”快”。
這個感覺應是翻譯的問題。
因為 pascal 在傳參數跟 C 不同。
詳細的操作過程你可以去看它的 CPU VIEW 慢慢看就能體會。
太久沒玩這麼低階的東西了。你可以試著去看它的 asm code 應該就可以了解它的差異
系統時間:2024-04-30 20:15:01
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!