Next: 8.9 Unsupported Turbo Pascal
Up: 8. Using functions and
Previous: 8.7 Assembler functions
Subsections
A function or procedure declaration can contain modifiers. Here we list the
various possibilities:
Modifiers
Free Pascal doesn't support all Turbo Pascal modifiers, but
does support a number of additional modifiers. They are used mainly for assembler and
reference to C object files. More on the use of modifiers can be found in
Programmers' guide.
The Public keyword is used to declare a function globally in a unit.
This is useful if you don't want a function to be accessible from the unit
file, but you do want the function to be accessible from the object file.
as an example:
Unit someunit;
interface
Function First : Real;
Implementation
Function First : Real;
begin
First := 0;
end;
Function Second : Real; [Public];
begin
Second := 1;
end;
end.
If another program or unit uses this unit, it will not be able to use the
function Second, since it isn't declared in the interface part.
However, it will be possible to access the function Second at the
assembly-language level, by using it's mangled name (Programmers' guide).
8.8.2 cdecl
The cdecl modifier can be used to declare a function that uses a C
type calling convention. This must be used if you wish to acces functions in
an object file generated by a C compiler. It allows you to use the function in
your code, and at linking time, you must link the object file containing the
C implementation of the function or procedure.
As an example:
program CmodDemo;
{$LINKLIB c}
Const P : PChar = 'This is fun !';
Function strlen (P : PChar) : Longint; cdecl; external;
begin
WriteLn ('Length of (',p,') : ',strlen(p))
end.
When compiling this, and linking to the C-library, you will be able to call
the strlen function throughout your program. The external
directive tells the compiler that the function resides in an external
object filebrary (see
).
Remark The parameters in our declaration of the C function should
match exactly the ones in the declaration in C. Since C is case
sensitive, this means also that the name of the
function must be exactly the same. the Free Pascal compiler will use the name
exactly as it is typed in the declaration.
8.8.3 popstack
Popstack does the same as cdecl, namely it tells the Free Pascal compiler
that a function uses the C calling convention. In difference with the
cdecl modifier, it still mangles the name of the function as it would
for a normal pascal function.
With popstack you could access functions by their pascal names in a
library.
Sometimes you must provide a callback function for a C library, or you want
your routines to be callable from a C program. Since Free Pascal and C use
different calling schemes for functions and procedures8.1, the compiler must be told to generate code
that can be called from a C routine. This is where the Export modifier
comes in. Contrary to the other modifiers, it must be specified separately,
as follows:
function DoSquare (X : Longint) : Longint; export;
begin
...
end;
The square brackets around the modifier are not allowed in this case.
Remark:
as of version 0.9.8, Free Pascal supports the Delphi cdecl modifier.
This modifier works in the same way as the export modifier.
More information about these modifiers can be found in the Programmers' guide, in the
section on the calling mechanism and the chapter on linking.
As of version 0.9.8, Free Pascal supports the Delphi stdcall modifier.
This modifier does actually nothing, since the Free Pascal compiler by default
pushes parameters from right to left on the stack, which is what the
modifier does under Delphi (which pushes parameters on the stack from left to
right).
More information about this modifier can be found in the Programmers' guide, in the
section on the calling mechanism and the chapter on linking.
The Alias modifier allows you to specify a different name for a
procedure or function. This is mostly useful for referring to this procedure
from assembly language constructs. As an example, consider the following
program:
Program Aliases;
Procedure Printit; [Alias : 'DOIT'];
begin
WriteLn ('In Printit (alias : "DOIT")');
end;
begin
asm
call DOIT
end;
end.
Remark: the specified alias is inserted straight into the assembly
code, thus it is case sensitive.
The Alias modifier, combined with the Public modifier, make a
powerful tool for making externally accessible object files.
root
1999-06-10