Understanding Calling Conventions in C/C
In C/C , various calling conventions define the protocols for passing arguments, returning values, and cleaning up the stack when functions are invoked. These conventions include stdcall, extern, pascal, and several others.
Available Calling Conventions
There are numerous calling conventions available in C/C :
-
cdecl: Arguments pushed onto the stack right-to-left, stack balancing handled by the caller.
-
syscall: Similar to cdecl, but EAX, ECX, and EDX registers are not preserved.
-
pascal: Arguments pushed onto the stack left-to-right, caller responsible for stack cleanup.
-
stdcall: Caller pushes arguments right-to-left, callee cleans up the stack, EAX, ECX, and EDX used within the function for specific purposes.
-
fastcall: First two arguments passed via ECX and EDX registers, remaining arguments pushed onto the stack from right-to-left.
-
vectorcall: Uses SIMD registers to pass vector arguments, handles vector types and HVA values.
-
safecall: Encapsulates COM error handling, passes exceptions in EAX, and the result by reference on the stack.
-
Microsoft X64 Calling Convention: Integer arguments passed in specific registers, floating-point arguments passed in XMM registers, shadow space allocated for spills.
Implications of Different Conventions
Each calling convention has its pros and cons. For instance, stdcall simplifies the caller's responsibilities, while fastcall improves performance by reducing stack operations. The choice of calling convention may depend on factors such as the operating system, processor architecture, and coding style preferences.
Additional Resources
- [Wikipedia: C calling conventions](https://en.wikipedia.org/wiki/Calling_convention#C)
- [Call Your Functions! (x86 Calling Conventions)](https://www.agner.org/optimize/calling_conventions.pdf)
- [MSDN: Calling Conventions for Arm64 (AArch64)](https://docs.microsoft.com/en-us/cpp/build/arm64-abi-for-visual-studio?view=vs-2022)
The above is the detailed content of How do Different Calling Conventions in C/C Impact Function Execution?. For more information, please follow other related articles on the PHP Chinese website!