这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=31

February 19, 2013

function calling convention

Filed under: c++ — Tags: C convention, C optimization, multi-thread — Raison @ 4:29 am

(original works by Peixu Zhu)

Function calling conventions are important for inter-operations between different languages, and debug inside. Since the birth of C language, the language has evolved for many years (it is older than me !!),  with richer function calling conventions appeared. Some ideas validated before may be 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 will pop off the stack space used to pass arguments, in another word, the caller is responsible to pop off the calling stack.
  • arguments are all pushed into stack, with right to left.
  • ’cause the stack is recovered by the caller function, thus, it is possible to implement functions with variable number of  arguments, like fprintf series functions, since the compiler knows how many arguments are 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 will pop off the stack space used to pass arguments, unless it take a variable number of arguments, i.e., the callee function is responsible to pop off the stack space before the function is returned to caller.
  • arguments are all pushed into stack from right to 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 the register ECX and the second argument(if of integral type) in the register EDX. subsequent and other typed arguments are passed on the stack from left to right.
  • The callee function will pop the arguments off the stack, just like stdcall does.
  • this is compiler dependent, some compilers may 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 to pass arguments number one to number if they are of integral type in registers EAX, EDX, and ECX instead of on the stack.
  • Functions that take a variable number of arguments will continue to be 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 will continue to pass all of their floating point arguments on the stack.
  • 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 are 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 are passed in registers: %r3, %r4, %r5, %r6, %r7, %r8, %r9, %r10.
  • The return address is in register %lr.

Conclusion

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

function calling convention的更多相关文章

  1. C&C++ Calling Convention

    tkorays(tkorays@hotmail.com) 调用约定(Calling Convention) 是计算机编程中一个比较底层的设计,它主要涉及: 函数参数通过寄存器传递还是栈? 函数参数从左 ...

  2. X86调用约定 calling convention

    http://zh.wikipedia.org/wiki/X86%E8%B0%83%E7%94%A8%E7%BA%A6%E5%AE%9A 这里描述了在x86芯片架构上的调用约定(calling con ...

  3. C/C++:函数的调用约定(Calling Convention)和名称修饰(Decorated Name)以及两者不匹配引起的问题

    转自:http://blog.csdn.net/zskof/article/details/3475182 注:C++有着与C不同的名称修饰,主要是为了解决重载(overload):调用约定则影响函数 ...

  4. 从栈不平衡问题 理解 calling convention

    最近在开发的过程中遇到了几个很诡异的问题,造成了栈不平衡从而导致程序崩溃. 经过几经排查发现是和调用规约(calling convention)相关的问题,特此分享出来. 首先,讲一下什么是调用规约. ...

  5. Calling Convention的总结

    因为经常需要和不同的Calling Convention打交道,前段时间整理了一下它们之间的区别,如下: 清理堆栈 参数压栈顺序 命名规则 (MSVC++) 备注 Cdecl 调用者 (Caller) ...

  6. PatentTips – Java native function calling

    BACKGROUND OF INVENTION This invention relates to a system and method for providing a native functio ...

  7. [转]ARM64 Function Calling Conventions

    from apple In general, iOS adheres to the generic ABI specified by ARM for the ARM64 architecture. H ...

  8. sparc v8 stack frame calling convention

    main.c ; int main() { int a, b; int sum; a = ; b = ; sum = add(a, b); ; } int add(int a, int b) { in ...

  9. 调用惯例Calling Convention (或者说:调用约定)

    调用惯例影响执行效率,参数的传递方式以及栈清除的方式.   调用惯例 参数传递顺序 谁负责清除参数 参数是否使用暂存器 register 从左到右 被调用者 是 pascal 从左到右 被调用者 否 ...

随机推荐

  1. window.name应用于浏览器端数据存储

    本代码简单地分享利用window.name实现浏览器端数据存储: 1.在同一个页面一个地方设置window.name = "abc",另外一个地方读取window.name,自然能 ...

  2. codeforces 399B. Red and Blue Balls 解题报告

    题目链接:http://codeforces.com/problemset/problem/399/B 题目意思:给出 n 个只由 R 和 B 组成的字符串(由上到下排列,相当于栈),问最多可以操作多 ...

  3. Linux终端那件事儿

    我们将会讨论如何更好的控制用户终端:也就说是键盘输入与屏幕输出.除了这些,我们还会了解我们编写的程序如何由用户处读取输入,即使是在输入重定向的情况下,以及确保输出到屏幕的正确位置.这里所提供的一些底层 ...

  4. 用python写windows服务

    用python写windows服务(1) 以python2.5 为例需要软件 * python 2.5        * pywin32(与2.5 版本相匹配的) Service Control Ma ...

  5. 装饰器模式(Decorator) C++

    装饰器模式是比较常用的一种设计模式,Python中就内置了对于装饰器的支持. 具体来说,装饰器模式是用来给对象增加某些特性或者对被装饰对象进行某些修改. 如上图所示,需要被装饰的对象在最上方,它自身可 ...

  6. 012--python字符编码和list列表和循环语句

    一.字符编码: ASCII码最多只能表示 256个符号,每一个字符占8位 为什么一个字节占8位?因为计算机在读一串二进制数111011001111101110的时候, 要按照规定的长度截取,才能分清一 ...

  7. ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 17. 基于Claim和Policy的授权 上

    首先补一下昨天没有讲的东西 只有管理员才能访问UserController RoleController都加上这个角色 Cliam 不是管理员角色的用户访问 cliam是name个Value值的键值对 ...

  8. noip 2012 Day2 T2 借教室

    一.暴力简述 甩链接.jpeg 首先我们不难看出,这道题————并不是一道多难的题,因为显然,第一眼看题目时便很容易地想到暴力如何打:枚举每一种订单,然后针对每一种订单,对区间内的每一天进行修改(做减 ...

  9. docker系列(一):docker基础与安装笔记

    1 什么是docker docker是基于GO语言编写的开源容器项目,诞生于2013年初,到目前为止,已经经历了6年的发展演变.现如今,docker已经非常火爆,特别是在一线IT企业,部署.运维等工作 ...

  10. SQL - nulls值排序问题

    给字段排序时遇到的null值问题 当我们使用order by来为指定的字段进行排序时,如果db中该字段的值存在着null值,那么在排序时这些null值会不会参与排序呢?如果参与排序的话,又是以怎样的标 ...