What are custom calling conventions?

喜你入骨 提交于 2019-12-08 19:30:09

问题


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?


回答1:


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.




回答2:


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.




回答3:


I don't think you really need to care.

Normal calling conventions are things like __stdcall and __fastcall. They determine how your call signature is converted into a stack layout, who (caller or callee) is responsible for saving and restoring registers, etc. For example, __fastcall should use more registers where __stdcall would use more stack.

Custom calling conventions are optimised specifically for a particular function and the way it is used. They only happen, IIRC, for functions that are local to a particular module. That's how the compiler knows so much about how it's used, and also how you know that no external caller needs to be able to specify the convention.

Based on that, your compiler will use them automatically where appropriate, your code will run slightly faster and/or take slightly less space, but you don't really need to worry about it.




回答4:


Unless you are directly manipulating the stack or writing inline assembly referencing local variables, it does not affect you. Or if your interfacing with libraries linked with different calling conventions

What it is: Most compilers and such use a standard calling convention such as cdecl where function arguments are pushed in a certain order to the stack and such.



来源:https://stackoverflow.com/questions/2320665/what-are-custom-calling-conventions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!