Why are there so many different calling conventions?
Historically, why does it seem like just about everyone and their kid brother defined their own calling conventions? You've got C, C++, Windows, Pascal, Fortran, Fastcall and probably a zillion others I didn't think to mention. Shouldn't one convention be the most efficient for the vast majority of use cases? Is there ever any g开发者_Python百科ood reason to prefer one over the other?
The calling conventions you mention were designed over the course of decades for different languages and different hardware. They all had different goals. cdecl supported variable arguments for printf. stdcall resulted in smaller code gen, but no variable arguments. Fastcall could greatly speed up the performance of simple functions with only one or two arguments on older machines (but is rarely a speed up today.)
Note than when x64 was introduced, on Windows at least, it was designed to have a single calling convention.
Raymond Chen wrote a great series on the history of calling conventions, you can start here.
Because historically everyone and their kid brother did define their own calling conventions. They were all created for different purposes and thus driven by different performance needs. For instance, C++ favours optimisations for passing the this
parameter.
- Some of them are more efficient regarding performance and others more efficient in code size.
- Some features (variable arguments count) are only supported with some conventions.
More Information: http://en.wikipedia.org/wiki/X86_calling_conventions
Part of the reason is the underlying architecture of the microprocessor (or processor). Most languages start on a specific CPU, and get entangled a bit with that architecture. For example, the old Univac 1100 series computer didn't even have a call stack!
Another part of the reason is that is is not possible to foresee the best solution until you've tried several ways of doing things.
They're created for different purposes, and with different optimization systems.
For instance, to reduce "Stack Overflow," (no pun intended) some people thought of various ideas to call function to make stack overflows impossible.
Another instance is the Lambda Calculus. Not to be too vague, but in Lambda, functions may only pass one argument and return one value, and thus also need their own calling conventions.
精彩评论