apical, cdecl, what's the difference?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

apical, cdecl, what's the difference?

timrowledge
Whilst looking at ancient OpenGL code I noted that many primitive calls used apicall and not cdecl. The swiki entry (http://wiki.squeak.org/squeak/2426) for FFI is… vague, and explicitly mentions confusion about the matter. It wold be nice to clear this up, document it on the swiki and maybe even make a Help page to go into the image.

tim
--
tim Rowledge; [hidden email]; http://www.rowledge.org/tim
Strange OpCodes: MII: Mask all Interrupts and then Interrupt



Reply | Threaded
Open this post in threaded view
|

Re: apical, cdecl, what's the difference?

Jakob Reschke-2
Here is what I found, experts please double-check (and I only looked up x86):

The Windows API uses the stdcall calling convention, which has the same argument order and register preservation rules as cdecl, but the callee is responsible to pop arguments from the stack.

In IA32FFIPlugin there is a macro PLATFORM_API_USES_CALLEE_POPS_CONVENTION, which is only 1 on Windows. If this is 1 and apicall is used (isCalleePopsConvention), the foreign function invocation code knows that the callee modifies the stack pointer and reacts accordingly.

So, apicall does stdcall instead of cdecl on Windows, but not on the other x86 platforms.

Stdcall is not the same as the Pascal calling convention, which pushes arguments from left to right, contrary to cdecl and stdcall, which push from right to left.

Best,
Jakob


Am 04.08.2017 3:45 vorm. schrieb "tim Rowledge" <[hidden email]>:
Whilst looking at ancient OpenGL code I noted that many primitive calls used apicall and not cdecl. The swiki entry (http://wiki.squeak.org/squeak/2426) for FFI is… vague, and explicitly mentions confusion about the matter. It wold be nice to clear this up, document it on the swiki and maybe even make a Help page to go into the image.

tim
--
tim Rowledge; [hidden email]; http://www.rowledge.org/tim
Strange OpCodes: MII: Mask all Interrupts and then Interrupt






Reply | Threaded
Open this post in threaded view
|

Re: apical, cdecl, what's the difference?

Bert Freudenberg
Yep. Windows has 2 calling conventions - stdcall for the Microsoft Win32 API, and the default cdecl for everything else. 

Basically you need to know which calling convention the function is expecting, and you need to tell the FFI, otherwise your stack will explode.

​- Bert -​


On Fri, Aug 4, 2017 at 10:47 AM, Jakob Reschke <[hidden email]> wrote:
Here is what I found, experts please double-check (and I only looked up x86):

The Windows API uses the stdcall calling convention, which has the same argument order and register preservation rules as cdecl, but the callee is responsible to pop arguments from the stack.

In IA32FFIPlugin there is a macro PLATFORM_API_USES_CALLEE_POPS_CONVENTION, which is only 1 on Windows. If this is 1 and apicall is used (isCalleePopsConvention), the foreign function invocation code knows that the callee modifies the stack pointer and reacts accordingly.

So, apicall does stdcall instead of cdecl on Windows, but not on the other x86 platforms.

Stdcall is not the same as the Pascal calling convention, which pushes arguments from left to right, contrary to cdecl and stdcall, which push from right to left.

Best,
Jakob


Am 04.08.2017 3:45 vorm. schrieb "tim Rowledge" <[hidden email]>:
Whilst looking at ancient OpenGL code I noted that many primitive calls used apicall and not cdecl. The swiki entry (http://wiki.squeak.org/squeak/2426) for FFI is… vague, and explicitly mentions confusion about the matter. It wold be nice to clear this up, document it on the swiki and maybe even make a Help page to go into the image.

tim
--
tim Rowledge; [hidden email]; http://www.rowledge.org/tim
Strange OpCodes: MII: Mask all Interrupts and then Interrupt










Reply | Threaded
Open this post in threaded view
|

Re: apical, cdecl, what's the difference?

timrowledge
After some investigations I’ve been able to update the swiki entry (http://wiki.squeak.org/squeak/2426) a bit and I think it makes more sense now. If I have correctly understood, the apicall keyword needs to be used when the called function is a Windows API but not when on Windows and calling a function from some other library. Brilliant work by Microsoft. On other platforms both cdecl and apicall actually do a cdecl when they get down to the actual call.

tim
--
tim Rowledge; [hidden email]; http://www.rowledge.org/tim
That’s the second time I’ve seen a Word doc eat a man’s soul. Time for a bug report...