function calling convention

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

时间: 2024-10-09 13:02:15

function calling convention的相关文章

C and C++ Calling Convention

调用约定(Calling Convention) 是计算机编程中一个比较底层的设计,它主要涉及: 函数参数通过寄存器传递还是栈? 函数参数从左到右还是从右到左压栈? 是否支持可变参数函数(vararg function or variadic function). 是否需要函数原型? 调用者(caller)还是被调用者(called or callee)清理堆栈? 在C和C++中有几种调用约定:__cdecl, __stdcall, __fastcall, __thiscall, __clrca

从栈不平衡问题 理解 calling convention

最近在开发的过程中遇到了几个很诡异的问题,造成了栈不平衡从而导致程序崩溃. 经过几经排查发现是和调用规约(calling convention)相关的问题,特此分享出来. 首先,讲一下什么是调用规约. 函数调用规约,是指当一个函数被调用时,函数的参数会被传递给被调用的函数和返回值会被返回给调用函数.函数的调用规约就是描述参数是怎么传递和由谁平衡堆栈的,当然还有返回值. 名称 谁负责参数出栈 参数压栈顺序 Cdecl Caller(调用者) 从右往左 Pascal Callee(被调用者) 从左往

PatentTips – Java native function calling

BACKGROUND OF INVENTION This invention relates to a system and method for providing a native function call facility. In particular it relates to a system and method for providing a native function call facility in a Java Virtual Machine (JVM) for pla

C++: How is the process of function calling in C++

Following images are from http://www.xuebuyuan.com/528715.html In general, the process of calling function is put the parameters into the stack, move the EBP and ESP (jump into another function), protect the information of father function, restore th

JAVA calling convention

http://stackoverflow.com/questions/28126082/where-does-the-jit-compiled-code-reside https://wiki.openjdk.java.net/display/HotSpot/Linker+methods+for+direct+method+handles http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/9989538b7507/src/share/vm/o

Linux Kernel - Debug Guide (Linux内核调试指南 )

http://blog.csdn.net/blizmax6/article/details/6747601 linux内核调试指南 一些前言 作者前言 知识从哪里来 为什么撰写本文档 为什么需要汇编级调试 ***第一部分:基础知识*** 总纲:内核世界的陷阱 源码阅读的陷阱 代码调试的陷阱 原理理解的陷阱 建立调试环境 发行版的选择和安装 安装交叉编译工具 bin工具集的使用 qemu的使用 initrd.img的原理与制作 x86虚拟调试环境的建立 arm虚拟调试环境的建立 arm开发板调试环

PHP7革新与性能优化

有幸参与2015年的PHP技术峰会(PHPCON),听了鸟哥(惠新宸)的关于PHP7的新特性和性能优化的分享,一切都令人感到激动.鸟哥是国内最权威的PHP专家,他的分享有很多非常有价值的东西,我通过整理分享的PPT和收集相关资料,整理为这篇解读性质的技术文章,希望能给做PHP开发的同学一些帮助. PHP已经走过了20年的历史,直到今天,PHP7都发布了RC版,据说,PHP7正式版应该会在2015年11月份左右发布.PHP7对于上一个系列的PHP5.*,可以说是一个大规模的革新,尤其是在性能方面实

linux内核调试指南

linux内核调试指南 一些前言 作者前言 知识从哪里来 为什么撰写本文档 为什么需要汇编级调试 ***第一部分:基础知识*** 总纲:内核世界的陷阱 源码阅读的陷阱 代码调试的陷阱 原理理解的陷阱 建立调试环境 发行版的选择和安装 安装交叉编译工具 bin工具集的使用 qemu的使用 initrd.img的原理与制作 x86虚拟调试环境的建立 arm虚拟调试环境的建立 arm开发板调试环境的建立 gdb基础 基本命令 gdb之gui gdb技巧 gdb宏 汇编基础--X86篇 用户手册 AT&

(C/C++) Callback Function

原文: http://www.codeguru.com/cpp/cpp/cpp_mfc/callbacks/article.php/c10557/Callback-Functions-Tutorial.htm Callback Functions Tutorial Introduction If you are reading this article, you probably wonder what callback functions are. This article explains