原文:https://docs.microsoft.com/zh-cn/cpp/cpp/argument-passing-and-naming-conventions?view=vs-2017
Visual C++ Compilers允许functions和callers之间约定passing arguments和return values的convention,有的平台并不支持某种convention,在大多数情况下,keywords或者compiler会将不支持的convention更换为default convention。
在x86平台上,所有的参数都被扩展为32bits传递,返回值也同样是32bits,并且存储在EAX register中,并返回,except for 8-byte structures, which are returned in the EDX:EAX register pair.更大的机构体将以指针的形式存储在EAX register中,并返回。Parameters将从右向左入棧,Structures that are not PODs will not be returned in registers.
如果某段代码被引用,compiler将生成prolog和epilog来存储和还原在ESI、EDI、EBX和EBP寄存器中的??(是Parameters吗??)。
备注
When a struct, union, or class is returned from a function by value, all definitions of the type need to be the same, else the program may fail at runtime.
For information on how to define your own function prolog and epilog code, see Naked Function Calls.
For information about the default calling conventions in code that targets x64 platforms, see Overview of x64 Calling Conventions. For information about calling convention issues in code that targets ARM platforms, see Common Visual C++ ARM Migration Issues.
The following calling conventions are supported by the Visual C/C++ compiler.
Keyword | Stack cleanup | Parameter passing |
---|---|---|
__cdecl | Caller | Pushes parameters on the stack, in reverse order (right to left) |
__clrcall | n/a | Load parameters onto CLR expression stack in order (left to right). |
__stdcall | Callee | Pushes parameters on the stack, in reverse order (right to left) |
__fastcall | Callee | Stored in registers, then pushed on stack |
__thiscall | Callee | Pushed on stack; this pointer stored in ECX |
__vectorcall | Callee | Stored in registers, then pushed on stack in reverse order (right to left) |
For related information, see Obsolete Calling Conventions.
疑问:
ESI、EDI、EBX和EBP等等各种寄存器是干嘛?
- Assembly Language Step-by-Step: Programming with Linux中可以找到答案。。。
WinDef.h中的定义如下:
#ifdef _MAC
#define CALLBACK PASCAL
#define WINAPI CDECL
#define WINAPIV CDECL
#define APIENTRY WINAPI
#define APIPRIVATE CDECL
#ifdef _68K_
#define PASCAL __pascal
#else
#define PASCAL
#endif
#elif (_MSC_VER >= 800) || defined(_STDCALL_SUPPORTED)
#define CALLBACK __stdcall
#define WINAPI __stdcall
#define WINAPIV __cdecl
#define APIENTRY WINAPI
#define APIPRIVATE __stdcall
#define PASCAL __stdcall
#else
#define CALLBACK
#define WINAPI
#define WINAPIV
#define APIENTRY WINAPI
#define APIPRIVATE
#define PASCAL pascal
#endif