[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:

http://www.hu.freepascal.org/fpcircbot/cgipastebin?msgid=19098

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
afaik)

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:

http://www.hu.freepascal.org/fpcircbot/cgipastebin?msgid=19099

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"
into
PlainCB := MakeMethodTrampoline(@MyObj.CB);

Regards,

Andrew

PS I've mostly gotten my information from:

http://ref.x86asm.net/coder64-abc.html
http://en.wikipedia.org/wiki/X86_calling_conventions
http://www.codeproject.com/KB/cpp/calling_conventions_demystified.aspx
http://unixwiz.net/techtips/win32-callconv-asm.html
and
 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