首页 技术 正文
技术 2022年11月23日
0 收藏 347 点赞 3,205 浏览 3129 个字

这是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.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,956
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,480
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,327
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,110
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,741
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,776