[fpc-devel] Calling conventions

Andrew Haines AndrewD207 at aol.com
Tue Jan 4 07:00:41 CET 2011

On 01/03/11 06:02, Jonas Maebe wrote:
> On 03 Jan 2011, at 04:11, Andrew Haines wrote:
>> On linux32 and win32 what cc does fpc use by default?
> If you mean "i386" by "32", it's Delphi-style fastcall, aka "register".
> This calling convention is the default on all i386 platforms.
> FPC's implementation is not yet 100% Delphi-compatible at this time
> though, so it will still change in the future (since the only reason for
> supporting this is Delphi compatibility).

Thanks for the info :)

If you want to check it out I pasted the code I have so far here:


There are lots of ifdefs so consider yourself warned.

I develop on linux so I haven't tested on windows and in fact I haven't
implemented allocating executable memory there yet either. (VirtualAlloc

StdCall, Register, Cdecl, and linux64 calling conventions should be
complete but there could be bugs, I've tried to be thorough. I have yet
to write the win64 calling convention.

My test program is here:


I just search replace the calling convention I wish to test.

Please go easy on me since I've never written assembler let alone
opcodes and operands before. It's...interesting.

I plan on using rtti to make the wrapper function only need a TMethod arg.

Also my "memory management" code can hardly be called that.

Further questions:

Do registers that are used to pass parameters need to be restored before
my trampoline procedure returns? (since I am shifting them to make room
for "self")

As far as I know eax for sure can be thrashed but what about edx ecx.
And on linux64 rax can be changed but what about rdi rsi rdx rcx r8 r9?

Can something like this be added to the compiler so that when it detects
a TMethod being assigned to a "plain" callback type it inserts some code
to call my function?

i.e. the compiler turns
PlainCB := @MyObj.CB <- compiler error about mismatched "of object"
PlainCB := MakeMethodTrampoline(@MyObj.CB);



PS I've mostly gotten my information from:

 AMD64 Architecture Programmer’s Manual Volume 3: General-Purpose and
System Instructions <- extremely useful
also I examined some fpc asm code with gdb

More information about the fpc-devel mailing list