[fpc-pascal] Header translation, argument list, array of const?

Bernd prof7bit at gmail.com
Sat Jun 30 12:50:02 CEST 2012

2012/6/30 Sven Barth <pascaldragon at googlemail.com>:

> Your explanation is very nice and mostly correct, but in case of external
> cdecl functions in FPC the "varargs" modifier and a "array of const"
> parameter imply the same C compatible parameter passing in the compiler. See
> also here:
> http://freepascal.org/docs-html/ref/refsu77.html#x180-19000014.9.17
> The only difference is when calling the functions. E.g.:
> procedure a(args: array of const); cdecl; external;
> procedure b; cdecl; varargs; external;
> begin
>   a([1, 'foo', somevar]);
>   b(1, 'foo', somevar);
> end.

This means in my case I declared the external function correctly and
only need to pass
to make it a null terminated list? Because I tried this last night and
looked at the generated assembler and if I for example call it with

  purple_prpl_got_user_status(@self, Pointer(AName),
Pointer(AStatusID), [1,2,3,nil]);

then it will generate the following code:

0162EF17 6a00                     push   $0x0
0162EF19 6a03                     push   $0x3
0162EF1B 6a02                     push   $0x2
0162EF1D 6a01                     push   $0x1
0162EF1F ff75f8                   pushl  -0x8(%ebp)
0162EF22 ff75fc                   pushl  -0x4(%ebp)
0162EF25 8b45f4                   mov    -0xc(%ebp),%eax
0162EF28 50                       push   %eax
0162EF29 e8b20afaff               call   0x15cf9e0
<purple_prpl_got_user_status at plt>

so its just just pushing the contents of the list onto the stack as
they are and If I pass [] there would be nothing pushed at all, so the
right thing for me to do in this case would be to call it with

  purple_prpl_got_user_status(@self, Pointer(AName), Pointer(AStatusID), [nil]);

0162EF17 6a00                     push   $0x0
0162EF19 ff75f8                   pushl  -0x8(%ebp)
0162EF1C ff75fc                   pushl  -0x4(%ebp)
0162EF1F 8b45f4                   mov    -0xc(%ebp),%eax
0162EF22 50                       push   %eax
0162EF23 e8b80afaff               call   0x15cf9e0
<purple_prpl_got_user_status at plt>

so that it will push exactly one zero onto the stack, so it gets what
it wants (an empty null-terminated list)? Then my only mistake was to
assume that [] would automatically generate a null terminated list
(for automagic[TM]  C-friendlyness) just like strings do?

More information about the fpc-pascal mailing list