Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are custom calling conventions?

What are these? And how am I affected by these as a developer?

Related:
What are the different calling conventions in C/C++ and what do each mean?

like image 466
Scott J Avatar asked Feb 23 '10 18:02

Scott J


People also ask

What are different calling conventions?

Microsoft x64 calling convention That means RCX, RDX, R8, R9 for integer, struct or pointer arguments (in that order), and XMM0, XMM1, XMM2, XMM3 for floating point arguments. Additional arguments are pushed onto the stack (right to left). Integer return values (similar to x86) are returned in RAX if 64 bits or less.

What is the purpose of calling convention?

A calling convention governs how functions on a particular architecture and operating system interact. This includes rules about includes how function arguments are placed, where return values go, what registers functions may use, how they may allocate local variables, and so forth.

What is the default calling convention?

__cdecl is the default calling convention for C and C++ programs. Because the stack is cleaned up by the caller, it can do vararg functions. The __cdecl calling convention creates larger executables than __stdcall, because it requires each function call to include stack cleanup code.

What is __ Fastcall in C++?

The __fastcall calling convention specifies that arguments to functions are to be passed in registers, when possible. This calling convention only applies to the x86 architecture.


2 Answers

A calling convention describes how something may call another function. This requires parameters and state to be passed to the other function, so that it can execute and return control correctly. The way in which this is done has to be standardized and specified, so that the compiler knows how to order parameters for consumption by the remote function that's being called. There are several standard calling conventions, but the most common are fastcall, stdcall, and cdecl.

Usually, the term custom calling convention is a bit of a misnomer and refers to one of two things:

  • A non-standard calling convention or one that isn't in widespread use (e.g. if you're building an architecture from scratch).

  • A special optimization that a compiler/linker can perform that uses a one-shot calling convention for the purpose of improving performance.

In the latter case, this causes some values that would otherwise be pushed onto the stack to be stored in registers instead. The compiler will try to make this decision based on how the parameters are being used inside the code. For example, if the parameter is going to be used as a maximum value for a loop index, such that the index is compared against the max on each iteration to see if things should continue, that would be a good case for moving it into a register.

If the optimization is carried out, this typically reduces code size and improves performance.

And how am I affected by these as a developer?

From your standpoint as a developer, you probably don't care; this is an optimization that will happen automatically.

like image 50
John Feminella Avatar answered Sep 28 '22 06:09

John Feminella


Each language, when calling a function, has a convention about what parameters will be passed in register variables vs on the stack, and how return values will be returned.

Sometimes a different convention than the standard one is used and that's referred to as a custom calling convention.

This is most common when interoperating between different languages. For example, C and Pascal have different conventions about how to pass parameters. From C's point of view, the Pascal calling convention could be referred to as a custom calling convention.

like image 29
Eric J. Avatar answered Sep 28 '22 04:09

Eric J.