function calling convention
这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=31
February 19, 2013
function calling convention
(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的更多相关文章
- C&C++ Calling Convention
tkorays(tkorays@hotmail.com) 调用约定(Calling Convention) 是计算机编程中一个比较底层的设计,它主要涉及: 函数参数通过寄存器传递还是栈? 函数参数从左 ...
- X86调用约定 calling convention
http://zh.wikipedia.org/wiki/X86%E8%B0%83%E7%94%A8%E7%BA%A6%E5%AE%9A 这里描述了在x86芯片架构上的调用约定(calling con ...
- C/C++:函数的调用约定(Calling Convention)和名称修饰(Decorated Name)以及两者不匹配引起的问题
转自:http://blog.csdn.net/zskof/article/details/3475182 注:C++有着与C不同的名称修饰,主要是为了解决重载(overload):调用约定则影响函数 ...
- 从栈不平衡问题 理解 calling convention
最近在开发的过程中遇到了几个很诡异的问题,造成了栈不平衡从而导致程序崩溃. 经过几经排查发现是和调用规约(calling convention)相关的问题,特此分享出来. 首先,讲一下什么是调用规约. ...
- Calling Convention的总结
因为经常需要和不同的Calling Convention打交道,前段时间整理了一下它们之间的区别,如下: 清理堆栈 参数压栈顺序 命名规则 (MSVC++) 备注 Cdecl 调用者 (Caller) ...
- PatentTips – Java native function calling
BACKGROUND OF INVENTION This invention relates to a system and method for providing a native functio ...
- [转]ARM64 Function Calling Conventions
from apple In general, iOS adheres to the generic ABI specified by ARM for the ARM64 architecture. H ...
- 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 ...
- 调用惯例Calling Convention (或者说:调用约定)
调用惯例影响执行效率,参数的传递方式以及栈清除的方式. 调用惯例 参数传递顺序 谁负责清除参数 参数是否使用暂存器 register 从左到右 被调用者 是 pascal 从左到右 被调用者 否 ...
随机推荐
- Lucene in action 笔记 term vector——针对特定field建立的词频向量空间,不存!不会!影响搜索,其作用是告诉我们搜索结果是“如何”匹配的,用以提供高亮、计算相似度,在VSM模型中评分计算
摘自:http://makble.com/what-is-term-vector-in-lucene given a document, find all its terms and the posi ...
- JAVA 中 synchronized 详解
看到一篇关于JAVA中synchronized的用法的详解,觉得不错遂转载之..... 原文地址: http://www.cnblogs.com/GnagWang/archive/2011/02/27 ...
- shell脚本只提供整数算术运算(三种方式)—((表达式))、let "表达式"、value=`expr 表达式右边` (转载)
转自:http://blog.163.com/xychenbaihu@yeah/blog/static/132229655201131055455754/ 数值运算: 在bash中只提供了整数运算,一 ...
- 图像分类与KNN
1 图像分类问题 1.1 什么是图像分类 所谓图像分类问题,就是已有固定的分类标签集合,然后对于输入的图像,从分类标签集合中找出一个分类标签,最后把分类标签分配给该输入图像.虽然看起来挺简单的,但这可 ...
- Codeforces Round #422 (Div. 2)D. My pretty girl Noora(递推+数论)
传送门 题意 对于n个女孩,每次分成x人/组,每组比较次数为\(\frac{x(x+1)}{2}\),直到剩余1人 计算\[\sum_{i=l}^{r}t^{i-l}f(i)\],其中f(i)代表i个 ...
- Codeforces702A - Maximum Increase【尺取】
题意: 求一个连续的最长子序列长度: 思路: 没看仔细还wa1了-以为LIS- 然后写了尺取吧...= =太不仔细了.不过收获是LIS特么写挫了然后看了学长的blog<-点我- 题目的挫code ...
- MongoDb 查看用户名列表 , 修改用户密码
修改用户密码: db.addUser("java","java"); 查看一下所有的用户 , 查看当前Db的用户名 db.system.users.find ...
- bzoj 5499: [2019省队联测]春节十二响【堆】
首先看两条链怎么合并,贪心可得是从大到小取max,多条链同理 所以dfs合并子树的大根堆即可,注意为了保证复杂度,合并的时候要合并到最长链上,证明见长链剖分 #include<iostream& ...
- bzoj 2331: [SCOI2011]地板【插头dp】
一开始设计了四种状态,多了一种已经拐弯但是长度为0的情况,后来发现不用,设012表示没插头,没拐弯的插头,拐了弯的插头,然后转移的话12,21,22都不合法,剩下的转移脑补一下即可,ans只能在11, ...
- MySQL 错误码对照
1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件导致删除数据库失败 1010:不能删除数据目 ...