2

I've been reading up on the differences in 32bit calling conventions. The fastcall vs. stdcall ordeal that is.

From what I read there was great confusion with the two conventions, and 64 bit was standardized to avoid this confusion.

I have to ask, why was fastcall chosen?

Also, since fastcall and stdcall are win32 terms, what is the UNIX term for function calling that does or does not use registers for passing arguments?

Hawken
  • 2,059
  • 19
  • 34
  • It isn't exactly fastcall, it is the "we've got 8 more registers, let use 'em" calling convention. – Hans Passant Jul 11 '12 at 00:23
  • @hansPassant there really is no saving for using registers for arg passing since you usually have to backup the registers beforehand onto the stack. Fastcall-style calling is only faster in very specific situations. – Hawken Jul 11 '12 at 01:02
  • You assume they have to be "backed-up". That's not a correct assumption in general. The code generator can treat them as scratch or reload them from memory. The "let's use 'em" scheme is not "let's use 'em all". – Hans Passant Jul 11 '12 at 01:07
  • @hansPassant point taken. I guess I see more of a problem with the 'callee' needing to use the registers anyway and having to back up args on the stack, unless it's a very simple function that is. – Hawken Jul 12 '12 at 08:43
  • There's not such thing as '64-bit fastcall'. fastcall is purely an artifact of old 32-bit x86. – Chris Dodd Feb 27 '21 at 23:05

1 Answers1

3

x86 Calling Conventions - Wikipedia, the free encyclopedia provides a list.

The common calling convention on x86-32 is cdecl. GCC provides a function attribute __attribute__((regparm(n))) to indicate that n arguments are passed by register, but this is not the same as fastcall. Either way, arguments are passed in callee-clobberable registers, so there is no additional cost (and saves the effort of adding stack space for arguments then cleaning it up) for function calls relative to cdecl (for regparm) and stdcall (for fastcall).

To aid in your confusion, the x86-64 calling conventions on Windows and Linux are different both from those on x86-32 and from each other. Neither is fastcall, although both use a significant number of registers to pass arguments.

ephemient
  • 198,619
  • 38
  • 280
  • 391
  • I'm more upset about BSD, since they have always had stack based calling, which is both simpler and saves CPU time since you don't need to backup registers beforehand. From what I understand of what you said it would seem that register passing is not required in 64bit? – Hawken Jul 11 '12 at 01:05
  • @Hawken: `regparm` and `fastcall` use registers that are callee-clobbered so they need to be caller-saved even under `cdecl` and `stdcall` – what CPU time is lost? Currently there are no non-default x86-64 calling conventions in use by C compilers. You can see [System V Application Binary Interface \ AMD64 Architecture Processor Supplement](http://www.x86-64.org/documentation/abi.pdf) and [MSDN | Overview of x64 Calling Conventions](http://msdn.microsoft.com/en-us/library/ms235286.aspx) for what's currently in use. – ephemient Jul 11 '12 at 01:17
  • Even the FreeBSD dev-handbook states that register passing is slower. I'm inclined to believe them since FreeBSD devs are likely to have side by side register/stack passing programs for testing since they often port Linux code. Besides one would assume that you need to write passed values to the stack anyway since you'll be using `%e_x` registers in your instructions. Unless your function is only calling another function. – Hawken Jul 12 '12 at 08:39