2 Replies - 1356 Views - Last Post: 02 January 2011 - 08:38 PM

#1 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

how do compilers know which calling convention to use?

Posted 02 January 2011 - 03:23 PM

ok so i was using an audio library called audiere and linking to it via a .dll. pretty common for this to happen, you have 1 or more binary's and some header files(in this case 1) to declare the functions defined in the binary. well seems pretty simple if everything is standardized but what got me thinking was calling conventions. when you have a member function defined in binary how is the 'this' pointer passed to it? in GCC the 'thiscall' calling convention it pushes on the stack as either the first or last(i can't remember which) argument. however in MSVC it sets ECX to the 'this' pointer(well for x86 i don't know about x64). so as far as i know each compiler would compile the same code differently yet binary's can be made with both and work with both? also how dose it know weather to use cdecl calling convention or std calling convention for function? if cdecl is used then the compiler needs to adjust the stack after the function is called and if std is used it needs leave it alone, so how dose it know witch calling convention to use?

This post has been edited by ishkabible: 02 January 2011 - 03:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: how do compilers know which calling convention to use?

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: how do compilers know which calling convention to use?

Posted 02 January 2011 - 05:45 PM

Compilers generally do not know anything about any foreign code. They need to be told about the function and that can include information about the calling convention. Thus header files were invented to tell the compiler all about the function you would like to call -- that includes calling convention information.

in the function definitions you will often find abbreviations placed before the function name like:
cdecl -- is generally the default C calling convention, parameters pushed right to left, caller cleans up the stack.
stdcall -- parameters are pushed right to left, function cleans the stack
fastcall -- parameters are passed via registers (not standard at this point and so it is compiler dependent).


There are other calling conventions out there (pascal was big once) and so you will see others from time to time and more often then not these are packed in some macro -- for example on windows you will often see "WINAPI" which is generally stdcall except on CE which uses cdecl.

for a nice read on calling conventions on x86 see here
Was This Post Helpful? 2
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: how do compilers know which calling convention to use?

Posted 02 January 2011 - 08:38 PM

ok that makes more sense. thanks Nick, im reading it now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1