FreePascal Information Logo Friend of FreePascal Compiler Title
Articles with Feedback, FPC News Library, PDF Collection, Mail Lists, Books, Newsgroups, IRC Open online discussion areas Research and Tutorials Tools, Compilers and Utilities Blurbs about us, advertising, etc.
Welcome to the FoFPC Research Notes: "Calling Conventions"

Calling Conventions

by: G.E. Ozz Nixon Jr.
Published: May 2009
©opyright 2009 by Friends of FPC



     When compiling your software to link with external libraries (any OS), or producing your own libraries, there are conventions to calling external libraries. These calling conventions define to the compiler how data is passed between the application and library layers. The calling conventions also define which layer is responsible for cleaning up the stack if used for passing information. My research notes on name mangling explains how these calling conventions modify the name the exported methods to avoid conflicts.

Calling Convention
Order of Allocation
Stack or Register
Who clean-up
cdecl
right to left
stack
caller
syscall
right to left
stack
caller
optlink
right to left
stack
caller
pascal
left to right
stack
callee
register
left to right
both
callee
stdcall
right to left
stack
callee
Borland fastcall
left to right
both
callee
Microsofti/GCC fastcall
both directions
both
callee
Borland safecall
right to left
stack
callee


     The above chart may be misleading at first, as it looks like there are only a few combination with just different names/aliases. That assumption is incorrect, the above chart should be used to know which layer cleans-up the stack and the order in which the parameters are passed. Below I actually document the internal methods used for these calling conventions, and you will quickly see each one works at least a little bit different from each other - some are drastically different.

cdecl

     The cdecl calling convention is used by many "C" languages for the Intel CPU. Parameters are pushed on the stack in right-to-left order. Result values are returned using the EAX register (except for floating point values, which are returned in the x87 register ST0). Registers EBX, ECX and EDX are available for use in the called routine.

function external_function(a,b,c:Integer):Integer;
   external; cdecl;
{...}
d := external_function(a, b, c);

in assembly routine look like:



G.E. Ozz Nixon Jr.
 Links and Products we find useful



ButtonGenerator.com
Valid XHTML 1.0 Transitional Internet Map
Programmer's Heaven
grat-i-fi-ca-tion - noun
the state of being gratified; great satisfaction.


"Your research documents are head on ... I look forward to seeing more notes on your research."

Brian Ellixson
FreePascal User
Locations of visitors to this page world map hits counter
Copyright 2009 by 3F, LLC. All rights reserved. Worldwide.
Your request was processed by server #3 in 0.435954 secs.

sponsor
Click to visit our paid sponsor