这是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. CGAffineTransform属性

    transform我们一般称为形变属性,其本质是通过矩阵变化改变控件的大小.位置.角度等,这里我们通过一个例子来看一下具体的操作,在下面的例子中我们也会看到UIImageView控件的常用操作. - ...

  2. 微服务框架go-micro

    微服务框架go-micro https://www.cnblogs.com/li-peng/p/9558421.html 产品嘴里的一个小项目,从立项到开发上线,随着时间和需求的不断激增,会越来越复杂 ...

  3. Android 开发:由模块化到组件化

    在Android SDK一文中,我们谈到模块化和组件化,现在我们来聊聊组件化开发背后的哪些事.最早是在广告SDK中应用组件化,但是同样适用于普通应用开发 以下高能,请做好心理准备,看不懂请发私信来交流 ...

  4. mysql负载均衡方案

    mysql负载均衡方案 一.直接连接 数据库的读写分离方案很多,这里介绍基于mysql数据库的读写分离方案. 比较常见的读写分离方案如下: 1 基于查询分离 最简单的分离方法是将读和写分发到主和从服务 ...

  5. GC 和 Full GC 有什么区别?

    GC(或Minor GC):收集 生命周期短的区域(Young area). Full GC (或Major GC):收集生命周期短的区域(Young area)和生命周期比较长的区域(Old are ...

  6. Lua变量

    Lua 变量 变量在使用前,必须在代码中进行声明,即创建该变量. 编译程序执行代码之前编译器需要知道如何给语句变量开辟存储区,用于存储变量的值. Lua 变量有三种类型:全局变量.局部变量.表中的域. ...

  7. 关于数据库优化2——关于表的连接顺序,和where子句的前后顺序,是否会影响到sql的执行效率问题

    有好多时候,我们常听别人说大表在前,小表在后,包括现在好多百度出来的靠前的答案都有说数据库是从右到左加载的,所以from语句最后关联的那张表会先被处理.如果三表交叉,就选择交叉表来作为基础表.等等一些 ...

  8. python list生成表达式

    列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式.运用列表生成式,可以写出非常简洁的代码. >>> list(ra ...

  9. bzoj 3576: [Hnoi2014]江南乐【博弈论】

    这个东西卡常--预处理的时候要先把i%j,i/j都用变量表示,还要把%2变成&1-- 首先每一堆都是不相关子游戏,所以对于每一堆求sg即可 考虑暴力枚举石子数i,分割块数j,分解成子问题求xo ...

  10. P5110 块速递推

    传送门 为啥我就没看出来有循环节呢-- 打表可得,这个数列是有循环节的,循环节为\(10^9+6\),然后分块预处理,即取\(k=sqrt(10^9+6)\),然后分别预处理出转移矩阵\(A\)的\( ...