Author Topic: C++ Calling Conventions  (Read 514 times)

0 Members and 1 Guest are viewing this topic.

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1057
  • Karma: 152
    • View Profile
    • Raizor's Dev Blog
C++ Calling Conventions
« on: May 31, 2012 »


I'm just curious about the implications of using different calling conventions in C++ projects. IQ's 64k framework (which has been my goto reference for a lot of things), uses _fastcall calling convention. I've used the same for 'release' builds of most of my stuff, and 'cdecl' for debug builds. Is this something I should spend some time studying? Does it impact code-size and execution speed much?

It's popped up on my radar again due to me working with native C++ and managed C++ and having to fiddle around with calling conventions to get things to work together properly.

Any insight would be great, thanks.
raizor

Challenge Trophies Won:

Offline Ferris

  • Pentium
  • *****
  • Posts: 836
  • Karma: 84
    • View Profile
    • Youth Uprising Home
Re: C++ Calling Conventions
« Reply #1 on: May 31, 2012 »
How much do you know about how the specific conventions work under the hood? From what I remember, cdecl is pretty simple; caller pushes arg's on stack in reverse order, callee handles creation of new stack frame and preservation of ebp/esp (including "cleanup" work for the original argument pushing). fastcall afaik is the same except the first two arguments go through ecx/edx for speed. Anyways, I think it really depends on context as far as size; fastcall should be faster in a general sense unless for some reason the arguments were already available on the stack, which is highly unlikely (but a trick used in some 4k stuff I've seen around).

Honestly, though, I think at 64k it's a bit overkill to worry about such things, as it DOES (in my opinion) make the code a bit less readable for perhaps 2-6 bytes gained per function body (not to mention you're also locking the code to x86 by explicitly setting any conventions). At 4k in asm, however, I often drop conventions all together and define them per-function; for instance using FPU stack for arguments where possible for simple routines. The only time it really matters at that level is interop between non-asm code, in which case cdecl is quite straightforward and easy to target.
http://iamferris.com/
http://youth-uprising.com/

Where the fun's at.
Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1057
  • Karma: 152
    • View Profile
    • Raizor's Dev Blog
Re: C++ Calling Conventions
« Reply #2 on: May 31, 2012 »
Thanks Ferris. I've got a very basic understanding from doing a bit of reading, but wasn't sure of the technical implications. My main concern was that it would introduce extra bloat, but it sounds like that isn't going to be a problem. This isn't for 4k stuff, so a few bytes here and there won't be a problem.

I'll stick with cdecl for this stuff as that seems to work without issues and kicks out fairly small code still. I plugged what exists of my synth at present into a 4k skeleton and it's about 4k or so packed. So all good for now :)

Thanks for the info.
raizor

Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1178
  • Karma: 433
    • View Profile
    • my stuff
Re: C++ Calling Conventions
« Reply #3 on: May 31, 2012 »
I'll stick with cdecl for this stuff as that seems to work without issues and kicks out fairly small code still.
If not otherwise specified all methods of a class are called as "stdcall" by default (just that the "this" pointer is additionally passed in some register).
So the caller is not responsable for cleaning up the stack.
Cleaning the stack is nothing more than a simple "sub esp,<count>", though.



« Last Edit: May 31, 2012 by hellfire »
Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1057
  • Karma: 152
    • View Profile
    • Raizor's Dev Blog
Re: C++ Calling Conventions
« Reply #4 on: May 31, 2012 »
Thanks for the info Hellfire.
raizor

Challenge Trophies Won: