function calling convention

Source: Internet
Author: User

This is an old article written in 2013, put on Gegahost.net http://raison.gegahost.net/?p=31

February, 2013function calling conventionfiled under:c++-tags:c Convention, c optimization, Multi-thread-raison @ 4:29 am

(Original works by Peixu Zhu)

Function calling conventions is important for inter-operations between different languages, and debug inside. Since the birth of C language, the language have evolved for many years (it's older than me!), with richer function cal Ling conventions appeared. Some ideas validated before is demanded to refresh.

Let us review current available conventions:

1. cdecl

    • On the i386/x86_64 architecture, the CDECL attributes causes the compiler to assume that the calling function would pop off The stack space used to pass arguments, in another word, the caller are responsible to pop off the calling stack.
    • Arguments is all pushed into the stack with the right to the left.
    • ' Cause the stack is recovered by the caller function, thus, it's possible to implement functions with variable number of Arguments, like fprintf series functions, since the compiler knows how many arguments is pushed in the stack.
    • Returned values normally in register Rax/eax, or ST0.

2. stdcall

    • On the i386/x86_64 architecture, the stdcall attribute cause the compiler to assume that the called function would pop off The stack space used to pass arguments, unless it take a variable number of arguments, i.e., the callee function is Respon Sible to pops off the stack space before the function is returned to caller.
    • Arguments is all pushed into the stack from the right to the left.
    • It is possible to make functions with variable number of arguments, with supporting of compilers.
    • Returned values normally in register Rax/eax.

3. Fastcall

    • On the i386 architecture, the Fastcall atributes causes the compiler to pass the first argument (if of integral type) in th E Register ECX and the second argument (if of integral type) in the register EDX. Subsequent and other typed arguments is passed on the stack from left to right.
    • The callee function would pop the arguments off the stack, just like stdcall does.
    • This is compiler dependent, some compilers could utilize other registers to pass argument.
    • It makes less access of memory, and improve efficiency.

4. Numbered Register parameters

    • On the i386 architectures, the attributes causes the compiler-pass arguments number one to number if they is of Integr Al type in registers EAX, EDX, and ECX instead of the stack.
    • Functions that take a variable number of arguments would continue to being passed all of their arugments on the stack.
    • return value in EAX.
    • This is compiler dependent.

5. SSE Register Parameters

    • On the i386 with SSE support, the attribute causes the compiler to pass up to 3 floating point arguments in SSE registers Instead of on the stack.
    • Functions that take a variable number of arguments would continue to pass all of their floating point arguments on the Stac K.
    • Return value in EAX.
    • This is a compiler dependent and CPU dependent feature.
    • greatly improve efficiency.

6. x86_64

    • In 64-bit Intel programs The first six parameters is passed in registers:%rdi,%rsi,%RDX,%RCX,%r8,%r9.
    • The return address is on the stack.

7. Power PC

    • In Power PC programs, 32-or 64-bit, the first eight parameters is passed in registers:%r3,%R4,%R5,%R6,%r7,%R8,%r 9,%r10.
    • The return address is in register%LR.

Conclusion

Matching calling conventions is basically required.  In practice, compiler optimization could cause the convention be changed potentially. Thus, be careful to specify call conventions of libraries, multi-threaded programs, and make sure mutex object h As been specified volatile, otherwise, the potential register passing may cause nightmare.

function calling convention

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.