Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2011-01

Calling conventions

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Andrew Haines

Hi,

I am writing a unit to "automatically" write Trampoline functions on the fly. So assigning "plain" callbacks to TMethod's work.

Currently it can handle cdecl on linux32/64. I've tested it with up to 9 parameters and it works well.

I would like to implement whatever is common to interface winapi(stdcall?) on win32 and just wanted to know if stdcall is for sure what I want.

I know on x64 there is only one calling convention each for windows64 and everything_else_64.

On linux32 and win32 what cc does fpc use by default?

Thanks,

Andrew Haines

Reply by email

Joost van der Sluis
to Andrew Haines
Andrew Haines wrote 8 lines
I would like to implement whatever is common to interface winapi(stdcall?) on win32 and just wanted to know if stdcall is for sure what I want. I know on x64 there is only one calling convention each for windows64 and everything_else_64. On linux32 and win32 what cc does fpc use by default?

Open Lazarus, write some code which uses an api call, use ctrl+click on the name of the api call, and look for yourself how the call is defined.

Probably stdcall, yes. And please note that this has nothing to do with fpc. It's Microsoft that decided what calling convention to use, and no-one can differ from that.

Joost.

Reply by email

Jonas Maebe
to Andrew Haines: I am writing a unit to "automatically" write Trampoline functions on the fly. So…
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).

Jonas

Reply by email

Andrew Haines
to Jonas Maebe
Jonas Maebe wrote 13 lines
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

Reply by email

Jonas Maebe
to Andrew Haines
Andrew Haines wrote 5 lines
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 doubt it. There is also already a project out there whose sole focus is on offering a high level interface to calling functions with different calling conventions, and afaik it also contains trampoline support. See http://sourceware.org/libffi/ for more information.

Jonas

Reply by email