Calling Conventions in C
Calling Conventions in C
https://scc.ustc.edu.cn/zlsc/sugon/intel/compiler_c/main_cls/bldaps_cls/common/bldap
https://www.agner.org/optimize/calling_conventions.pdf
cdecl - In cdecl, subroutine arguments are passed on the stack. Integer values
and memory addresses are returned in the EAX register, floating point values in
the ST0 x87 register. Registers EAX, ECX, and EDX are caller-saved, and the
rest are callee-saved. The x87 floating point registers ST0 to ST7 must be empty
(popped or freed) when calling a new function, and ST1 to ST7 must be empty on
.exiting a function. ST0 must also be empty when not used for returning a value
pascal - the parameters are pushed on the stack in left-to-right order (opposite
of cdecl), and the callee is responsible for balancing the stack before return.
syscall - This is similar to cdecl in that arguments are pushed right-to-left. EAX,
ECX, and EDX are not preserved. The size of the parameter list in double words
is passed in AL.
safecall - n Delphi and Free Pascal on Microsoft Windows, the safecall calling
convention encapsulates COM (Component Object Model) error handling, thus
exceptions aren't leaked out to the caller, but are reported in the HRESULT return
value, as required by COM/OLE. When calling a safecall function from Delphi
code, Delphi also automatically checks the returned HRESULT and raises an
exception if necessary.
The safecall calling convention is the same as the stdcall calling convention,
except that exceptions are passed back to the caller in EAX as a HResult
(instead of in FS:[0]), while the function result is passed by reference on the stack
as though it were a final "out" parameter. When calling a Delphi function from
Delphi this calling convention will appear just like any other calling convention,
because although exceptions are passed back in EAX, they are automatically
converted back to proper exceptions by the caller. When using COM objects
created in other languages, the HResults will be automatically raised as
exceptions, and the result for Get functions is in the result rather than a
parameter. When creating COM objects in Delphi with safecall, there is no need
to worry about HResults, as exceptions can be raised as normal but will be seen
as HResults in other languages.
The registers RAX, RCX, RDX, R8, R9, R10, R11 are considered volatile (caller-
saved).[15]
The registers RBX, RBP, RDI, RSI, RSP, R12, R13, R14, and R15 are
considered nonvolatile (callee-saved).[15]
For example, a function taking 5 integer arguments will take the first to fourth in
registers, and the fifth will be pushed on the top of the shadow space. So when
the called function is entered, the stack will be composed of (in ascending order)
the return address, followed by the shadow space (32 bytes) followed by the fifth
parameter.
In x86-64, Visual Studio 2008 stores floating point numbers in XMM6 and XMM7
(as well as XMM8 through XMM15); consequently, for x86-64, user-written
assembly language routines must preserve XMM6 and XMM7 (as compared to
x86 wherein user-written assembly language routines did not need to preserve
XMM6 and XMM7). In other words, user-written assembly language routines
must be updated to save/restore XMM6 and XMM7 before/after the function
when being ported from x86 to x86-64.
((As a side-note: all of the listed above are applicable to externally linked functions,
and functions that CAN be externally called (a pointer to function is taken somewhere
in the unit). If a compiler (I'm speaking of x86 MSVC, but it may be extended to others)
can deduce that the function is linked only internally (within single compilation unit -
o/obj) AND it also considers it a less overhead by not inlining one, it is free in how it
assigns registers / stack slots to arguments))
>>>Videos<<<
https://www.youtube.com/watch?v=hlpW_BqGmvM
>>> (CDECL) :
https://www.youtube.com/watch?v=frqPX7EHscM