开发者

Questions about call convention

I remember long ago, when I was using the Turbo C, I didn't need to care about the call convention of a function, every function I used or defin开发者_开发问答ed took the form of C call convention.

But after I move to Windows platform, I found that there're so many call convention speicifiers such as:

WINAPI, STDCALL, __cdecl...

Is this the result of the evolution of the compiler?


No, its simply, or mostly, historical legacy with the Windows API. Most systems outside of Windows do not routinely use different calling conventions (exception: syscalls and kernel mode).


Different calling conventions have different characteristics, and sometimes different features that may be needed by the language or API being used. Check this article for an overview of different calling conventions, and what APIs commonly use them.


WINAPI, STDCALL, ect... are not calling conventions, they are macros that define calling conventions. In reality there are only about two or three actual types. The reason for the macros is for backward compatibility.


No, it's the result of Microsoft idiocy. There's no reason different calling conventions should ever have been used/mixed in a C environment.


Calling convention is VERY important, but you seldomly have to think about.

It becomes relevant when targeting other OS's or objects\libraries written in other languages (like pascal or so).

It determine what and how parameters are pushed on the stack (by the caller) and who is responsible for popping these parameters from the stack when the called function returns.

Since C\C++ supports variable parameter list ,it's the task of the caller to pop the stack (as the callee doesn't know with how many parameters are given to it).

Consequently this results in large generated code (every function call is expanded with stack-pop code).

However languages that do not allow natively for variable parameter lists can lay the burden of popping the stack themsel by de the called function (they always know how it's called). This results in only one piece of stack-popping code (at the end of the callee) and thus lesser generated code.

Further more a language can choose the way HOW parameters are pushed to stack. It can choose to push the parameters in same order they're written in the call (first param first ,last param last) or it can choose to push the last parameter first and the first parameter last (like C\C++ does). The latter allows for 'easy' implementation of variable parameter list.

if you don't use variable parameterlist you can choose a different default calling convention (like PASCAL). However this means that every function that doesn't have a calling convention specified will be treated with your chosen convention. If declarations are not the same as their implementation you probably get unresolved external errors (at best) or severe runtime errors or (at worst) faulty calculation.

As a practical example : I worked as a programmer for a mobile computer manufacturer. They used a special (limited) compiler for generating compact code (the mobile computer had very limited memory) since MSVC generated large objects.

I suggested changing the c-calling convention into pascal-calling convention and some other minor changes. Since then they could use a more sophisticated compiler (MSVC) and do a lot more.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜