Programmers' manual for Free Pascal, version 0.99.12 1.6
Michaël Van Canneyt
June 1999
It describes some of the peculiarities of the Free Pascal compiler, and provides a glimpse of how the compiler generates its code, and how you can change the generated code. It will not, however, provide you with a detailed account of the inner workings of the compiler, nor will it tell you how to use the compiler (described in the Users' guide). It also will not describe the inner workings of the Run-Time Library (RTL). The best way to learn about the way the RTL is implemented is from the sources themselves.
The things described here are useful if you want to do things which need greater flexibility than the standard Pascal language constructs. (described in the Reference guide)
Since the compiler is continuously under development, this document may get out of date. Wherever possible, the information in this manual will be updated. If you find something which isn't correct, or you think something is missing, feel free to contact me1.
Free Pascal supports compiler directives in your source file. They are not the same as Turbo Pascal directives, although some are supported for compatibility. There is a distinction between local and global directives; local directives take effect from the moment they are encountered, global directives have an effect on all of the compiled code.
Many switches have a long form also. If they do, then the name of the long form is given also. For long switches, the + or - character to switch the option on or off, may be replaced by ON or OFF keywords.
Thus {$I+}
is equivalent to {$IOCHECKS ON}
or
{$IOCHECKS +}
and
{$C-}
is equivalent to {$ASSERTIONS OFF}
or
{$ASSERTIONS -}
The long forms of the switches are the same as their Delphi counterparts.
This switch is recognized for Turbo Pascal Compatibility, but is not yet implemented. The alignment of data will be different in any case, since Free Pascal is a 32-bit compiler.
The {$ASMMODE XXX directive informs the compiler what kind of assembler it can expect in an asm block. The XXX should be replaced by one of the following:
This switch is understood by the Free Pascal compiler, but is ignored. The compiler always uses shortcut evaluation, i.e. the evaluation of a boolean expression is stopped once the result of the total exression is known with certainty.
So, in the following example, the function Bofu, which has a boolean result, will never get called.
If False and Bofu then ...
This switch is recognised for Delphi compatibility only. Assertions are not yet supported by the compiler, but will be implemented in the future.
The directive
{$DEFINE name}defines the symbol name. This symbol remains defined until the end of the current module, or until a $UNDEF name directive is encountered.
If name is already defined, this has no effect. Name is case insensitive.
The {$ELSE } switches between compiling and ignoting the source text delimited by the preceding {$IFxxx} and following {$ENDIF}. Any text after the ELSE keyword but before the brace is ignored:
{$ELSE some ignored text}is the same as
{$ELSE}This is useful for indication what switch is meant.
The {$ENDIF} directive ends the conditional compilation initiated by the last {$IFxxx} directive. Any text after the ENDIF keyword but before the closing brace is ignored:
{$ENDIF some ignored text}is the same as
{$ENDIF}This is useful for indication what switch is meant to be ended.
The following code
{$ERROR This code is erroneous !}will display an error message when the compiler encounters it, and increase the error count of the compiler. The compiler will continue to compile, but no code will be emitted.
As an example, : the following piece of code :
{$F+} Procedure TestProc; begin Writeln ('Hello From TestProc'); end; begin testProc end.Generates the following compiler output:
malpertuus: >pp -vw testf Compiler: ppc386 Units are searched in: /home/michael;/usr/bin/;/usr/lib/ppc/0.9.1/linuxunits Target OS: Linux Compiling testf.pp testf.pp(1) Warning: illegal compiler switch 7739 kB free Calling assembler... Assembled... Calling linker... 12 lines compiled, 1.00000000000000E+0000You can see that the verbosity level was set to display warnings.
If you declare a function as Far (this has the same effect as setting it between {$F+}...{$F-} directives), the compiler also generates a warning :
testf.pp(3) Warning: FAR ignored
The same story is true for procedures declared as Near. The warning displayed in that case is:
testf.pp(3) Warning: NEAR ignored
The following code
{$FATAL This code is erroneous !}will display an error message when the compiler encounters it, and trigger and increase the error count of the compiler. The compiler will immediatly stop the compilation process.
If {$GOTO ON} is specified, the compiler will support Goto statements and Label declarations. By default, $GOTO OFF is assumed. This directive corresponds to the -Sg command-line option.
As an example, the following code can be compiled:
{$GOTO ON} label Theend; begin If ParamCount=0 then GoTo TheEnd; Writeln ('You spcified command-line options'); TheEnd: end.
If {$LONGSTRINGS ON} is specified, the keyword String (no length specifier) will be treated as AnsiString, and the compiler will treat the corresponding varible as an ansistring, and will generate corresponding code.
By default, the use of ansistrings is off, corresponding to {$H-}.
This feature is still experimental, and should be used with caution for the time being.
If the generation of hints is turned on, through the -vh command-line option or the {$HINTS ON} directive, then
{$Hint This code should be optimized }will display a hint message when the compiler encounters it.
{$HINTS ON} switches the generation of hints on. {$HINTS OFF} switches the generation of hints off. Contrary to the command-line option -vh this is a local switch, this is useful for checking parts of your code.
The directive {$IF expr} will continue the compilation if the boolean expression expr evaluates to true. If the compilation evaluates to false, then the source are skipped to the first {$ELSE} or {$ENDIF} directive.
The compiler must be able to evaluate the expression at compile time. This means that you cannot use variables or constants that are defined in the source. Macros and symbols may be used, however.
More information on this can be found in the section about conditionals.
The {$IFDEF name} will skip the compilation of the text that follows it if the symbol name is not defined. If it is defined, then compilation continues as if the directive wasn't there.
The {$IFNDEF name} will skip the compilation of the text that follows it if the symbol name is defined. If it is not defined, then compilation continues as if the directive wasn't there.
The {$IFOPT switch} will compile the text that follows it if the switch switch is currently in the specified state. If it isn't in the specified state, then compilation continues after the corresponding {$ENDIF} directive.
As an example:
{$IFOPT M+} Writeln ('Compiled with type information'); {$ENDIF}Will compile the writeln statement if generation of type information is on.
If the generation of info is turned on, through the -vi command-line option, then
{$INFO This was coded on a rainy day by Bugs Bunny }will display an info message when the compiler encounters it.
Inline code is NOT exported from a unit. This means that if you call an inline procedure from another unit, a normal procedure call will be performed. Only inside units, Inline procedures are really inline.
If you compile using the -Ci compiler switch, the Free Pascal compiler inserts input/output checking code after every input/output call in your program. If an error occurred during input or output, then a run-time error will be generated. Use this switch if you wish to avoid this behavior. If you still want to check if something went wrong, you can use the IOResult function to see if everything went without problems.
Conversely, {$I+} will turn error-checking back on, until another directive is encountered which turns it off again.
The most common use for this switch is to check if the opening of a file went without problems, as in the following piece of code:
... assign (f,'file.txt'); {$I-} rewrite (f); {$I+} if IOResult<>0 then begin Writeln ('Error opening file : "file.txt"'); exit end; ...
The compiler will append the .pp extension to the file if you don't specify an extension yourself. Do not put the filename between quotes, as they will be regarded as part of the file's name.
You can nest included files, but not infinitely deep. The number of files is restricted to the number of file descriptors available to the Free Pascal compiler.
Contrary to Turbo Pascal, include files can cross blocks. I.e. you can start a block in one file (with a Begin keyword) and end it in another (with a End keyword). The smallest entity in an include file must be a token, i.e. an identifier, keyword or operator.
The compiler will look for the file to include in the following places:
In this form:
{$INCLUDE %xxx%}where xxx is one of TIME, DATE, FPCVERSION or FPCTARGET, will generate a macro with the value of these things. If xxx is none of the above, then it is assumed to be the value of an environment variable. It's value will be fetched, and inserted in the coe as if it were a srtring.
For example, the following program
Program InfoDemo; Const User = {$I %USER%}; joe begin Write ('This program was comilped at ',{$I %TIME%}); Writeln (' on ',{$I %DATE%}); Writeln ('By ',User); Writeln ('Compiler version : ',{$I %FPCVERSION%}); Writeln ('Target CPU : ',{$I %FPCTARGET%}); end.Creates the following output :
This program was comilped at 17:40:18 on 1998/09/09 By michael Compiler version : 0.99.7 Target CPU : i386
This switch selects the assembler reader. {$I386_XXX} has the same effect as {$ASMMODE XXX}, section AsmReader
the compiler will look for this file in the following way:
On LINUX systems, the name is case sensitive, and must be typed exactly as it appears on your system.
Remark : Take care that the object file you're linking is in a format the linker understands. Which format this is, depends on the platform you're on. Typing ld on the command line gives a list of formats ld knows about.
You can pass other files and options to the linker using the -k command-line option. You can specify more than one of these options, and they will be passed to the linker, in the order that you specified them on the command line, just before the names of the object files that must be linked.
As an example, consider the following unit:
unit getlen; interface {$LINKLIB c} function strlen (P : pchar) : longint;cdecl; implementation function strlen (P : pchar) : longint;cdecl;external; end.If one would issue the command the command
ppc386 foo.ppwhere foo.pp has the above unit in its uses clause, then the compiler would link your program to the c library, by passing the linker the -lc option.
The same effect could be obtained by removing the linklib directive in the above unit, and specify -k-lc on the command-line:
ppc386 -k-lc foo.pp
For classes that are compiled in the {$M+ } or {$TYPEINFO ON} state, the compiler will generate Run-Time Type Information (RTTI). All descendent objects of an object that was compiled in the {$M+} state will get RTTI information too, as well as any published classes. By default, no Run-Time Type Information is generated. The TPersistent object that is present in the FCL (Free Component Library) is generated in the {$M+} state. The generation of RTTI allows programmers to stream objects, and to access published properties of objects, without knowing the actual class of the object.
The run-time type information is accessible through the TypInfo unit, which is part of the Free Pascal Run-Time Library.
In the {$MACRO ON} state, the compiler allows you to use C-style (although not as elaborate) macros. Macros provide a means for simple text substitution. More information on using macros can be found in the section Macros section. This directive is equivalent to the command-line switch -Sm.
If the generation of info is turned on, through the -vi command-line option, then
{$MESSAGE This was coded on a rainy day by Bugs Bunny }will display an info message when the compiler encounters it. The effect is the same as the {$INFO} directive.
This optimizes certain code parts for the MMX Intel processor, thus greatly improving speed. The speed is noticed mostly when moving large amounts of data. Things that change are
When MMX support is on, you aren't allowed to do floating point arithmetic. You are allowed to move floating point data, but no arithmetic can be done. If you wish to do floating point math anyway, you must first switch of MMX support and clear the FPU using the emms function of the cpu unit.
The following example will make this more clear:
Program MMXDemo; uses cpu; var d1 : double; a : array[0..10000] of double; i : longint; begin d1:=1.0; {$mmx+} { floating point data is used, but we do _no_ arithmetic } for i:=0 to 10000 do a[i]:=d2; { this is done with 64 bit moves } {$mmx-} emms; { clear fpu } { now we can do floating point arithmetic } .... end.See, however, the chapter on MMX (
If the generation of notes is turned on, through the -vn command-line option or the {$NOTES ON} directive, then
{$NOTE Ask Santa Claus to look at this code }will display a note message when the compiler encounters it.
{$NOTES ON} switches the generation of notes on. {$NOTES OFF} switches the generation of notes off. Contrary to the command-line option -vn this is a local switch, this is useful for checking parts of your code.
This directive tells the compiler the minimum number of bytes it should use when storing enumerated types. It is of the following form:
{$PACKENUM xxx} {$MINENUMSIZE xxx}Where the form with $MINENUMSIZE is for Delphi compatibility. varxxx can be one of 1,2 or 4, or NORMAL or DEFAULT, corresponding to the default value of 4.
As an alternative form one can use {$Z1}, {$Z2} {$Z4}. Contrary to Delphi, the default size is 4 bytes ({$Z4}).
So the follwoing code
{$PACKENUM 1} Type Days = (monday, tuesday, wednesday, thursday, friday, saturday, sunday);will use 1 byte to store a variable of type Days, wheras it nomally would use 4 bytes. The above code is equivalent to
{$Z1} Type Days = (monday, tuesday, wednesday, thursday, friday, saturday, sunday);
Remark: Sets are always put in 32 bit or 32 bytes, this cannot be changed
This directive controls the byte alignment of the elements in a record, object or class type definition.
It is of the following form:
{$PACKRECORDS n}
Where n is one of 1,2,4,16 or NORMAL or DEFAULT. This means that the elements of a record that have size greater than n will be aligned on n byte boundaries. Elements with size less than or equal to n will be aligned to a natural boundary, i.e. to a power of two that is equal to or larger than the element's size.
The default alignment (which can be selected with DEFAULT) is 2, contrary to Turbo Pascal, where it is 1.
More information on this and an exmple program can be found in the reference guide, in the section about record types.
Remark: Sets are always put in 32 bit or 32 bytes, this cannot be changed
Remark: Overflow checking behaviour is not the same as in Turbo Pascal since all arithmetic operations are done via 32-bit values. Furthermore, the Inc() and Dec() standard system procedures are checked for overflow in Free Pascal, while in Turbo Pascal they are not.
Using the {$Q-} switch switches off the overflow checking code generation.
The generation of overflow checking code can also be controlled using the -Co command line compiler option (see Users' guide).
The {$RANGECHECKS OFF} switch tells the compiler not to generate range checking code. This may result in faulty program behaviour, but no run-time errors will be generated.
Remark: Range checking for sets and enumerations are not yet fully implemented.
A unit that is compiled in the {$SMARTLINK ON} state will be compiled in such a way that it can be used for smartlinking. This means that the unit is chopped in logical pieces: each procedure is put in it's own object file, and all object files are put together in a big archive. When using such a unit, only the pieces of code that you really need or call, will be linked in your program, thus reducing the size of your executable substantially. Beware that using smartlinked units slows down the compilation process, because a separate object file must be creayed for each procedure. If you have units with many functions and procedures, this can be a time consuming process, even more so if you use an external assembler (the assembler is called to assemble each procedure or function code block).
The smartlinking directive should be specified before the unit declaration part:
{$SMARTLINK ON} Unit MyUnit; Interface ...
This directive is equivalent to the -Cx command-line switch.
This directive is equivalent to the -St command-line option.
The following code
{$STOP This code is erroneous !}will display an error message when the compiler encounters it. The compiler will immediatly stop the compilation process.
It has the same effect as the {$FATAL} directive.
In the {$T+} or {$TYPEDADDRESS ON} state the @ operator, when applied to a variable, returns a result of type ^T, if the type of the variable is T. In the {$T-} state, the result is always an untyped pointer, which is assignment compatible with all other pointer types.
The directive
{$UNDEF name}un-defines the symbol name if it was previously defined. Name is case insensitive.
When in the + or ON state, the compiler checks that strings passed as parameters are of the same, identical, string type as the declared parameters of the procedure.
If the compiler encounters a
{$WAIT }directive, it will resume compiling only after the user has pressed the enter key. If the generation of info messages is turned on, then the compiler will display the follwing message:
Press <return> to continuebefore waiting for a keypress. Careful ! this may interfere with automatic compilation processes. It should be used for debuggig purposes only.
If the generation of warnings is turned on, through the -vw command-line option or the {$WARNINGS ON} directive, then
{$WARNING This is dubious code }will display a warning message when the compiler encounters it.
{$WARNINGS ON} switches the generation of warnings on. {$WARNINGS OFF} switches the generation of warnings off. Contrary to the command-line option -vw this is a local switch, this is useful for checking parts of your code.
The following, for instance, will not compile :
function Func (var Arg : sometype) : longint; begin ... { declaration of Func } end; ... {$X-} Func (A);The reason this construct is supported is that you may wish to call a function for certain side-effects it has, but you don't need the function result. In this case you don't need to assign the function result, saving you an extra variable.
The command-line compiler switch -Sa1 has the same effect as the {$X+} directive.
The {$APPTYPE XXX} accepts one argument that can have two possible values : GUI or CONSOLE. It is used to tell the windows Operating system if an application is a console application or a graphical application. By default, a program compiled by Free Pascal is a console application. Running it will display a console window. Specifying the {$APPTYPE GUI} directive will mark the application as a graphical application; no console window will be opened when the application is run.
Care should be taken when compiling GUI applications; the Input and Output files are not available in a GUI application, and attempting to read from or write to them will result in a run-time error.
It is possible to determine the application type of a windows application at runtime. The IsConsole constant, declared as
Const IsConsole : Booleancontains True if the application is a console application, False if the application is a GUI application.
When this switch is on ({$DEBUGINFO ON}), the compiler inserts GNU debugging information in the executable. The effect of this switch is the same as the command-line switch -g. By default, insertion of debugging information is off.
This switch is recognised for compatibility only, but is ignored completely by the compiler. At a later stage, this switch may be activated.
This directive controls the emulation of the coprocessor. There is no command-line counterpart for this directive.
When this switch is enabled, all floating point instructions which are not supported by standard coprocessor emulators will give out a warning.
The compiler itself doesn't do the emulation of the coprocessor.
To use coprocessor emulation under DOS go32v1 there is nothing special required, as it is handled automatically. (As of version 0.99.10, the go32v1 platform will no longer be supported)
To use coprocessor emulation under DOS go32v2 you must use the emu387 unit, which contains correct initialization code for the emulator.
Under LINUX, the kernel takes care of the coprocessor support.
When the switch is on, no floating point opcodes are emitted by the code generator. Instead, internal run-time library routines are called to do the necessary calculations. In this case all real types are mapped to the single IEEE floating point type.
Remark : By default, emulation is on. It is possible to intermix emulation code with real floating point opcodes, as long as the only type used is single or real.
This option is recognised for Turbo Pascal compatibility, but is ignored,
This option serves to specify the include path, where the compiler looks for include files. {$INCLUDEPATH XXX will add XXX to the include path. XXX can contain one or more paths, separated by semi-colons or colons.
for example
{$INCLUDEPATH ../inc;../i386} {$I strings.inc}
Will add the directories ../inc and ../i386 to the include path of the compiler. The compiler will look for the file strings.inc in both these directories, and will include the first found file. This directive is equivalent to the -Fi command-line switch.
Caution is in order when using this directive: If you distribute files, the places of the files may not be the same as on your machine; moreover, the directory structure may be different. In general it would be fair to say that you should avoid using absolute paths, instead use relative paths, as in the example above. Only use this directive if you are certain of the places where the files reside. If you are not sure, it is better practice to use makefiles and makefile variables.
This switch (not to be confused with the {$L file} file linking directive) is recognised for Turbo Pascal compatibility, but is ignored. generation of symbol information is controlled by the $D switch.
This option serves to specify the library path, where the linker looks for static or dynamic libraries. {$LIBRARYPATH XXX will add XXX to the library path. XXX can contain one or more paths, separated by semi-colons or colons.
for example
{$LIBRARYPATH /usr/X11/lib;/usr/local/lib} {$LINKLIB X11}
Will add the directories /usr/X11/lib and /usr/local/lib to the linker library path. The linker will look for the library libX11.so in both these directories, and use the first found file. This directive is equivalent to the -Fl command-line switch.
Caution is in order when using this directive: If you distribute files, the places of the libraries may not be the same as on your machine; moreover, the directory structure may be different. In general it would be fair to say that you should avoid using this directive. If you are not sure, it is better practice to use makefiles and makefile variables.
This switch can be used to set the heap and stacksize. It's format is as follows:
{$M StackSize,HeapSize}Wher StackSize and HeapSize should be two integer values, greater than 1024. The first number sets the size of the stack, and the second the size of the heap. (Stack setting is ignored under LINUX). The two numbers can be set on the command line using the -Ch and -Cs switches.
The {$MODE} sets the compatibility mode of the compiler. This is equivalent to setting one of the command-line options -So or -Sd or -S2. it has the following arguments:
For an exact description of each of these modes, see appendix ,
on page
This switch is recognised for Turbo Pascal compatibility, but is otherwise ignored, since the compiler always uses the coprocessor for floating point mathematics.
This switch is recognised for Turbo Pascal compatibility, but is otherwise ignored.
This option serves to specify the object path, where the compiler looks for object files. {$OBJECTPATH XXX will add XXX to the object path. XXX can contain one or more paths, separated by semi-colons or colons.
for example
{$OBJECTPATH ../inc;../i386} {$L strings.o}
Will add the directories ../inc and ../i386 to the object path of the compiler. The compiler will look for the file strings.o in both these directories, and will link the first found file in the program. This directive is equivalent to the -Fo command-line switch.
Caution is in order when using this directive: If you distribute files, the places of the files may not be the same as on your machine; moreover, the directory structure may be different. In general it would be fair to say that you should avoid using absolute paths, instead use relative paths, as in the example above. Only use this directive if you are certain of the places where the files reside. If you are not sure, it is better practice to use makefiles and makefile variables.
Specifying {$S-} will turn generation of stack-checking code off.
The command-line compiler switch -Ct has the same effect as the {$S+} directive.
This option serves to specify the unit path, where the compiler looks for unit files. {$UNITPATH XXX} will add XXX to the unit path. XXX can contain one or more paths, separated by semi-colons or colons.
for example
{$UNITPATH ../units;../i386/units} Uses strings;
Will add the directories ../units and ../i386/units to the unit path of the compiler. The compiler will look for the file strings.ppu in both these directories, and will link the first found file in the program. This directive is equivalent to the -Fu command-line switch.
Caution is in order when using this directive: If you distribute files, the places of the files may not be the same as on your machine; moreover, the directory structure may be different. In general it would be fair to say that you should avoid using absolute paths, instead use relative paths, as in the example above. Only use this directive if you are certain of the places where the files reside. If you are not sure, it is better practice to use makefiles and makefile variables.
The {$W} switch directove controls the generation of stackframes. In the on state ({$STACKFRAMES ON}), the compiler will generate a stackframe for every procedure or function.
In the off state, the compiler will omit the generation of a stackframe if the following conditions are satisfied:
This switch controls the generation of browser inforation. It is recognized for compatibility with Turbo Pascal and Delphi only, as Browser information generation is not yet fully supported.
{$Define Symbol }From this point on in your code, the compiler knows the symbol Symbol. Symbols are, like the Pascal language, case insensitive.
You can also define a symbol on the command line. the -dSymbol option defines the symbol Symbol. You can specify as many symbols on the command line as you want.
Undefining an existing symbol is done in a similar way:
{$Undef Symbol }If the symbol didn't exist yet, this doesn't do anything. If the symbol existed previously, the symbol will be erased, and will not be recognized any more in the code following the
{$Undef ...}
statement.
You can also undefine symbols from the command line with the -u command-line switch..
To compile code conditionally, depending on whether a symbol is defined or
not, you can enclose the code in a {$ifdef Symbol}
.. {$endif}
pair. For instance the following code will never be compiled :
{$Undef MySymbol} {$ifdef Mysymbol} DoSomething; ... {$endif}
Similarly, you can enclose your code in a {$Ifndef Symbol}
.. {$endif}
pair. Then the code between the pair will only be compiled when the used
symbol doesn't exist. For example, in the following example, the call to the
DoSomething will always be compiled:
{$Undef MySymbol} {$ifndef Mysymbol} DoSomething; ... {$endif}
You can combine the two alternatives in one structure, namely as follows
{$ifdef Mysymbol} DoSomething; {$else} DoSomethingElse {$endif}In this example, if MySymbol exists, then the call to DoSomething will be compiled. If it doesn't exist, the call to DoSomethingElse is compiled.
The Free Pascal compiler defines some symbols before starting to compile your program or unit. You can use these symbols to differentiate between different versions of the compiler, and between different compilers. In table (Symbols) , a list of pre-defined symbols is given2.1. In that table, you should change v with the version number of the compiler you're using, r with the release number and p with the patch-number of the compiler. 'OS' needs to be changed by the type of operating system. Currently this can be one of DOS, GO32V2, LINUX, OS2, WIN32, MACOS, AMIGA or ATARI.
This symbol is undefined if you specify a target that is different from the platform you're compiling on. The -TSomeOS option on the command line will define the SomeOS symbol, and will undefine the existing platform symbol2.2.
As an example : Version 0.9.1 of the compiler, running on a Linux system, defines the following symbols before reading the command line arguments: FPC, VER0, VER0_9, VER0_9_1 and LINUX. Specifying -TOS2 on the command-line will undefine the LINUX symbol, and will define the OS2 symbol.
Remark: Symbols, even when they're defined in the interface part of a unit, are not available outside that unit.
Except for the Turbo Pascal constructs, from version 0.9.8 and higher, the Free Pascal compiler also supports a stronger conditional compile mechanism: The {$If } construct.
The prototype of this construct is as follows :
{$If expr} CompileTheseLines; {$else} BetterCompileTheseLines; {$endif}In this directive expr is a Pascal expression which is evaluated using strings, unless both parts of a comparision can be evaluated as numbers, in which case they are evaluated using numbers2.3. If the complemete expression evaluates to '0', then it is considered false and rejected. Otherwise it is considered true and accepted. This may have unexpected consequences :
{$If 0}Will evaluate to False and be rejected, while
{$If 00}Will evaluate to True.
You can use any Pascal operator to construct your expression : =, <>, >, <, >=, <=, AND, NOT, OR and you can use round brackets to change the precedence of the operators.
The following example shows you many of the possibilities:
{$ifdef fpc} var y : longint; {$else fpc} var z : longint; {$endif fpc} var x : longint; begin {$if (fpc_version=0) and (fpc_release>6) and (fpc_patch>4)} {$info At least this is version 0.9.5} {$else} {$fatalerror Problem with version check} {$endif} {$define x:=1234} {$if x=1234} {$info x=1234} {$else} {$fatalerror x should be 1234} {$endif} {$if 12asdf and 12asdf} {$info $if 12asdf and 12asdf is ok} {$else} {$fatalerror $if 12asdf and 12asdf rejected} {$endif} {$if 0 or 1} {$info $if 0 or 1 is ok} {$else} {$fatalerror $if 0 or 1 rejected} {$endif} {$if 0} {$fatalerror $if 0 accepted} {$else} {$info $if 0 is ok} {$endif} {$if 12=12} {$info $if 12=12 is ok} {$else} {$fatalerror $if 12=12 rejected} {$endif} {$if 12<>312} {$info $if 12<>312 is ok} {$else} {$fatalerror $if 12<>312 rejected} {$endif} {$if 12<=312} {$info $if 12<=312 is ok} {$else} {$fatalerror $if 12<=312 rejected} {$endif} {$if 12<312} {$info $if 12<312 is ok} {$else} {$fatalerror $if 12<312 rejected} {$endif} {$if a12=a12} {$info $if a12=a12 is ok} {$else} {$fatalerror $if a12=a12 rejected} {$endif} {$if a12<=z312} {$info $if a12<=z312 is ok} {$else} {$fatalerror $if a12<=z312 rejected} {$endif} {$if a12<z312} {$info $if a12<z312 is ok} {$else} {$fatalerror $if a12<z312 rejected} {$endif} {$if not(0)} {$info $if not(0) is OK} {$else} {$fatalerror $if not(0) rejected} {$endif} {$info *************************************************} {$info * Now have to follow at least 2 error messages: *} {$info *************************************************} {$if not(0} {$endif} {$if not(<} {$endif} end.As you can see from the example, this construct isn't useful when used with normal symbols, but it is if you use macros, which are explained in section Macros, they can be very useful. When trying this example, you must switch on macro support, with the -Sm command-line switch.
Warnings can be used if you think some part of your code is still buggy, or if you think that a certain combination of symbols isn't useful. In general anything which may cause problems when compiling.
Error messages can be useful if you need a certain symbol to be defined to warn that a certain variable isn't defined or so, or when the compiler version isn't suitable for your code.
The compiler treats these messages as if they were generated by the compiler. This means that if you haven't turned on warning messages, the warning will not e displayed. Errors are always displayed, and the compiler stops as if an error had occurred.
For messages, the syntax is as follows :
{$Message Message text }Or
{$Info Message text }For notes:
{$Note Message text }For warnings:
{$Warning Warning Message text }For errors :
{$Error Error Message text }Lastly, for fatal errors :
{$FatalError Error Message text }or
{$Stop Error Message text }The difference between $Error and $FatalError or $Stop messages is that when the compiler encounters an error, it still continues to compile. With a fatal error, the compiler stops.
Remark : You cannot use the '}' character in your message, since this will be treated as the closing brace of the message.
As an example, the following piece of code will generate an error when the symbol RequiredVar isn't defined:
{$ifndef RequiredVar} {$Error Requiredvar isn't defined !} {$endif}But the compiler will continue to compile. It will not, however, generate a unit file or a program (since an error occurred).
Defining a macro in your program is done in the same way as defining a symbol; in a {$define } preprocessor statement2.4:
{$define ident:=expr}If the compiler encounters ident in the rest of the source file, it will be replaced immediately by expr. This replacement works recursive, meaning that when the compiler expanded one of your macros, it will look at the resulting expression again to see if another replacement can be made. You need to be careful with this, because an infinite loop can occur in this manner.
Here are two examples which illustrate the use of macros:
{$define sum:=a:=a+b;} ... sum { will be expanded to 'a:=a+b;' remark the absence of the semicolon} ... {$define b:=100} sum { Will be expanded recursively to a:=a+100; } ...The previous example could go wrong :
{$define sum:=a:=a+b;} ... sum { will be expanded to 'a:=a+b;' remark the absence of the semicolon} ... {$define b=sum} { DON'T do this !!!} sum { Will be infinitely recursively expanded... } ...On my system, the last example results in a heap error, causing the compiler to exit with a run-time error 203.
Remark: Macros defined in the interface part of a unit are not available outside that unit ! They can just be used as a notational convenience, or in conditional compiles.
By default, from version 0.9.8 of the compiler on, the compiler predefines three
macros, containing the version number, the release number and the patch
number. They are listed in table (DefMacros) .
Remark: Don't forget that macros support isn't on by default. You need to compile with the -Sm command-line switch.
As of version 0.9.7, Free Pascal supports Intel syntax for the Intel family of Ix86 processors in it's asm blocks.
The Intel syntax in your asm block is converted to AT&T syntax by the compiler, after which it is inserted in the compiled source. The supported assembler constructs are a subset of the normal assembly syntax. In what follows we specify what constructs are not supported in Free Pascal, but which exist in Turbo Pascal:
mov al, byte ptr MyWord -- allowed, mov al, byte(MyWord) -- allowed, mov al, shortint(MyWord) -- not allowed.
const s= 10; const t = 32767;in Turbo Pascal:
mov al, byte(s) -- useless typecast. mov al, byte(t) -- syntax error!In this parser, either of those cases will give out a syntax error.
mov al,byte ptr ['c'] -- not allowed. mov al,byte ptr [100h] -- not allowed.(This is due to the limitation of Turbo Assembler).
mov al,[ds:bx] -- not alloweduse instead:
mov al,ds:[bx]
const myscale = 1; ... mov al,byte ptr [esi+ebx*myscale] -- not allowed.use:
mov al, byte ptr [esi+ebx*1]
@: -- not alloweduse instead, for example:
@1: -- allowed
lds si,@mylabel -- not allowed
mov al,[byte ptr myvar] -- not allowed.use:
mov al,byte ptr [myvar] -- allowed.
The Intel inline assembler supports the following macros :
Section:[Base + Index*Scale + Offs]is written in AT&T syntax as :
Section:Offs(Base,Index,Scale)Where Base and Index are optional 32-bit base and index registers, and Scale is used to multiply Index. It can take the values 1,2,4 and 8. The Section is used to specify an optional section register for the memory operand.
More information about the AT&T syntax can be found in the as manual, although the following differences with normal AT&T assembly must be taken into account :
const myid = 10; ... movl $myid,%eax -- allowed movl myid(%esi),%eax -- not allowed.
The AT&T inline assembler supports the following macros :
Function results are returned in the accumulator, if they fit in the register.
The registers are not saved when calling a function or procedure. If you want to call a procedure or function from assembly language, you must save any registers you wish to preserve.
The first thing a procedure does is saving the base pointer, and setting the base pointer equal to the stack pointer. References to the pushed parameters and local variables are constructed using the base pointer.
When the procedure or function exits, it clears the stack.
When you want your code to be called by a C library or used in a C program, you will run into trouble because of this calling mechanism. In C, the calling procedure is expected to clear the stack, not the called procedure. In other words, the arguments still are on the stack when the procedure exits. To avoid this problem, Free Pascal supports the export modifier. Procedures that are defined using the export modifier, use a C-compatible calling mechanism. This means that they can be called from a C program or library, or that you can use them as a callback function.
This also means that you cannot call this procedure or function from your own program, since your program uses the Pascal calling convention. However, in the exported function, you can of course call other Pascal routines.
As of version 0.9.8, the Free Pascal compiler supports also the cdecl and stdcall modifiers, as found in Delphi. The cdecl modifier does the same as the export modifier, and stdcall does nothing, since Free Pascal pushes the paramaters from right to left by default. In addition to the Delphi cdecl construct, Free Pascal also supports the popstack directive; it is nearly the same a the cdecl directive, only it still mangles the name, i.e. makes it into a name such as the compiler uses internally.
All this is summarized in table (Calling) . The first column lists the modifier you specify for a procedure declaration. The second one lists the order the paramaters are pushed on the stack. The third column specifies who is responsible for cleaning the stack: the caller or the called function. Finally, the last column specifies if registers are used to pass parameters to the function.
More about this can be found in chapter Linking on linking.
pushl %ebp movl %esp,%ebp
The generated exit sequence for procedure and functions looks as follows:
leave ret $xx
Where xx is the total size of the pushed parameters.
To have more information on function return values take a look at the section RegConvs section.
Standard entry code for procedures and functions is as follows on the 680x0 architecture:
move.l a6,-(sp) move.l sp,a6
The generated exit sequence for procedure and functions looks as follows:
unlk a6 move.l (sp)+,a0 ; Get return address add.l #xx,sp ; Remove allocated stack move.l a0,-(sp) ; Put back return address on top of the stack
Where xx is the total size of the pushed parameters.
To have more information on function return values take a look at the section RegConvs section.
asm ... end ['R1',...,'Rn'];Here R1 to Rn are the names of the 32-bit registers you modify in your assembly code.
As an example :
asm movl BP,%eax movl 4(%eax),%eax movl %eax,__RESULT end ['EAX'];This example tells the compiler that the EAX register was modified.
The compiler has different register conventions, depending on the target processor used.
When optimizations are on, no register can be freely modified, without first being saved and then restored. Otherwise, EDI is usually used as a scratch register and can be freely used in assembler blocks.
Registers which can be freely modified without saving are registers D0, D1, D6, A0, A1, and floating point registers FP2 to FP7. All other registers are to be considered reserved and should be saved and then restored when used in assembler blocks.
However, there are times that you want to C libraries, or to external object files that are generated using a C compiler (or even another pascal compiler). The Free Pascal compiler can generate calls to a C function, and can generate functions that can be called from C (exported functions). More on these calling conventions can be found in section Calling.
In general, there are 2 things you must do to use a function that resides in an external library or object file:
The first step in using external code blocks is declaring the function you want to use. Free Pascal supports Delphi syntax, i.e. you must use the external directive. The external directive replaces, in effect, the code block of the function. As such, It cannot be used in an interface section of a unit, but must always reside in the implementation section.
There exist four variants of the external direcive :
Procedure ProcName (Args : TPRocArgs); external;The external directive tells the compiler that the function resides in an external block of code. You can use this together with the {$L } or {$LinkLib } directives to link to a function or procedure in a library or external object file.
Procedure ProcName (Args : TPRocArgs); external 'Name';This tells the compiler that the procedure resides in a library with name 'Name'. This method is equivalent to the following:
Procedure ProcName (Args : TPRocArgs);external; {$LinkLib 'Name'}
Procedure ProcName (Args : TPRocArgs); external 'Name' name 'OtherProcName';This has the same meaning as the previous declaration, only the compiler will use the name 'OtherProcName' when linking to the library. This can be used to give different names to procedures and functions in an external library.
This method is equivalent to the following code:
Procedure OtherProcName (Args : TProcArgs); external; {$LinkLib 'Name'} Procedure ProcName (Args : TPRocArgs); begin OtherProcName (Args); end;
Procedure ProcName (Args : TPRocArgs); external 'Name' Index SomeIndex;This tells the compiler that the procedure ProcName resides in a dynamic link library, with index SomeIndex.
Remark : Note that this is ONLY available under WINDOWS and OS/2.
In earlier versions of the Free Pascal compiler, the following construct was also possible :
Procedure ProcName (Args : TPRocArgs); [ C ];This method is equivalent to the following statement:
Procedure ProcName (Args : TPRocArgs); cdecl; external;However, the [ C ] directive is no longer supported as of version 0.99.5 of Free Pascal, therefore you should use the external directive, with the cdecl directive, if needed.
Some libaries or code blocks have variables which they export. You can access these variables much in the same way as external functions. To access an external variable, you declare it as follows:
Var MyVar : MyType; external name 'varname';The effect of this declaration is twofold:
A second possibility is the declaration:
Var varname : MyType; cvar; external;The effect of this declaration is twofold as in the previous case:
In order to be able to compile such statements, the compiler switch -Sv must be used.
As an example, let's look at the following C file (in extvar.c):
/* Declare a variable, allocate storage */ int extvar = 12;And the following program (in extdemo.pp):
Program ExtDemo; {$L extvar.o} Var { Case sensitive declaration !! } extvar : longint; cvar;external; I : longint; external name 'extvar'; begin { Extvar can be used case insensitive !! } Writeln ('Variable ''extvar'' has value : ',ExtVar); Writeln ('Variable ''I'' has value : ',i); end.Compiling the C file, and the pascal program:
gcc -c -o extvar.o extvar.c ppc386 -Sv extdemoWill produce a program extdemo which will print
Variable 'extvar' has value : 12 Variable 'I' has value : 12on your screen.
Having declared the external function or variable that resides in an object file, you can use it as if it was defined in your own program or unit. To produce an executable, you must still link the object file in. This can be done with the {$L file.o} directive.
This will cause the linker to link in the object file file.o. On LINUX systems, this filename is case sensitive. Under DOS, case isn't important. Note that file.o must be in the current directory if you don't specify a path. The linker will not search for file.o if it isn't found.
You cannot specify libraries in this way, it is for object files only.
Here we present an example. Consider that you have some assembly routine that calculates the nth Fibonacci number :
.text .align 4 .globl Fibonacci .type Fibonacci,@function Fibonacci: pushl %ebp movl %esp,%ebp movl 8(%ebp),%edx xorl %ecx,%ecx xorl %eax,%eax movl $1,%ebx incl %edx loop: decl %edx je endloop movl %ecx,%eax addl %ebx,%eax movl %ebx,%ecx movl %eax,%ebx jmp loop endloop: movl %ebp,%esp popl %ebp retThen you can call this function with the following Pascal Program:
Program FibonacciDemo; var i : longint; Function Fibonacci (L : longint):longint;cdecl;external; {$L fib.o} begin For I:=1 to 40 do writeln ('Fib(',i,') : ',Fibonacci (i)); end.With just two commands, this can be made into a program :
as -o fib.o fib.s ppc386 fibo.ppThis example supposes that you have your assembler routine in fib.s, and your Pascal program in fibo.pp.
To link your program to a library, the procedure depends on how you declared the external procedure.
In case you used the follwing syntax to declare your procedure:
Procedure ProcName (Args : TPRocArgs); external 'Name';You don't need to take additional steps to link your file in, the compiler will do all that is needed for you. On WINDOWS NT it will link to Name.dll, on LINUX your program will be linked to library libname, which can be a static or dynamic library.
In case you used
Procedure ProcName (Args : TPRocArgs); external;You still need to explicity link to the library. This can be done in 2 ways:
{$LinkLib 'gpm'}This will link to the gpm library. On LINUX systems, you needn't specify the extension or 'lib' prefix of the library. The compiler takes care of that. On DOS or WINDOWS systems, you need to specify the full name.
ppc386 -k'-lgpm' myprog.ppIs equivalent to the above method, and tells the linker to link to the gpm library.
As an example; consider the following program :
program printlength; {$linklib c} { Case sensitive } { Declaration for the standard C function strlen } Function strlen (P : pchar) : longint; cdecl;external; begin Writeln (strlen('Programming is easy !')); end.This program can be compiled with :
ppc386 prlen.ppSupposing, of course, that the program source resides in prlen.pp.
You cannot use procedures or functions that have a variable number of arguments in C. Pascal doesn't support this feature of C.
Free Pascal supports making shared or static libraries in a straightforward and easy manner. If you want to make libraries for other Free Pascal programmers, you just need to provide a command line switch. If you want C programmers to be able to use your code as well, you will need to adapt your code a little. This process is described first.
When exporting functions from a library, there are 2 things you must take in account:
The naming conventions can be controlled by 3 modifiers:
If you want to make your procedures and functions available to C programmers, you can do this very easily. All you need to do is declare the functions and procedures that you want to make available as export, as follows:
Procedure ExportedProcedure; export;
Remark : You can only declare a function as exported in the Implementation section of a unit. This function may not appear in the interface part of a unit. This is logical, since a Pascal routine cannot call an exported function, anyway.
However, the generated object file will not contain the name of the function as you declared it. The Free Pascal compiler ''mangles'' the name you give your function. It makes the name all-uppercase, and adds the types of all parameters to it. There are cases when you want to provide a mangled name without changing the calling convention. In such cases, you can use the Alias modifier.
The Alias modifier allows you to specify another name (a nickname) for your function or procedure.
The prototype for an aliased function or procedure is as follows :
Procedure AliasedProc; [ Alias : 'AliasName'];The procedure AliasedProc will also be known as AliasName. Take care, the name you specify is case sensitive (as C is).
Remark: If you use in your unit functions that are in other units, or system functions, then the C program will need to link in the object files from the units too.
Similarly as when you export functions, you can export variables. when exportig variables, one should only consider the names of the variables. To declare a variable that should be used by a C program, one declares it with the cvar modifier:
Var MyVar : MyTpe; cvar;This will tell the compiler that the assembler name of the variable (the one which is used by C programs) should be exactly as specified in the declaration, i.e., case sensitive.
It is not allowed to declare multiple variables as cvar in one statement, i.e. the following code will produce an error:
var Z1,Z2 : longint;cvar;
Once you have your (adapted) code, with exported and other functions, you can compile your unit, and tell the compiler to make it into a library. The compiler will simply compile your unit, and perform the necessary steps to transform it into a static or shared (dynamical) library.
You can do this as follows, for a dynamical library:
ppc386 -CD myunitOn LINUX this will leave you with a file libmyunit.so. On WINDOWS and OS/2, this will leave you with myunit.dll.
If you want a static library, you can do
ppc386 -CS myunitThis will leave you with libmyunit.a and a file myunit.ppu. The myunit.ppu is the unit file needed by the Free Pascal compiler.
The resulting files are then libraries. To make static libraries, you need the ranlib or ar program on your system. It is standard on any LINUX system, and is provided with the GCC compiler under DOS. For the dos distribution, a copy of ar is included in the file gnuutils.zip.
BEWARE: This command doesn't include anything but the current unit in the library. Other units are left out, so if you use code from other units, you must deploy them together with your library.
ppumove -e ppl -o name unit1 unit2 unit3This will move 3 units in 1 library (called libname.so on linux, name.dll on WINDOWS) and it will create 3 files unit1.ppl, unit2.ppl and file3.ppl, which are unit files, but which tell the compiler to look in library name when linking your executable.
The ppumove program has options to create statical or dynammical libraries. It is provided with the compiler.
When you compile a program or unit, the compiler will by default always look for .ppl files. If it doesn't find one, it will look for a .ppu file.
To be able to differentiate between units that have been compiled as static or dynamic libraries, there are 2 switches:
These two switches can be used in conjunction with the configuration file ppc386.cfg. The existence of one of these symbols can be used to decide which unit search path to set. For example:
# Set unit paths #IFDEF FPC_LINK_STATIC -Up/usr/lib/fpc/linuxunits/staticunits #ENDIF #IFDEF FPC_LINK_DYNAMIC -Up/usr/lib/fpc/linuxunits/sharedunits #ENDIFWith such a configuration file, the compiler will look for it's units in different directories, depending on whether -XD or -XS is used.
You can compile your units using smart linking. When you use smartl linking, the compiler creates a series of code blocks that are as small as possible, i.e. a code block will contain only the code for one procedure or function.
When you compile a program that uses a smart-linked unit, the compiler will only link in the code that you actually need, and will leave out all other code. This will result in a smaller binary, which is loaded in memory faster, thus speeding up execution.
To enable smartlinking, one can give the smartlink option on the command line : -Cx, or one can put the {$SMARTLINK ON} directive in the unit file:
Unit Testunit {SMARTLINK ON} Interface ...Smartlinking will slow down the compilation process, expecially for large units.
When a unit foo.pp is smartlinked, the name of the codefile is changed to libfoo.a.
Technically speaking, the compiler makes small assembler files for each procedure and function in the unit, as well as for all global defined variables (whether they're in the interface section or not). It then assembles all these small files, and uses ar to collect the resulting object fioles in one archive.
Smartlinking and the creation of shared (or dynamic) libraries are mutually exclusive, that is, if you turn on smartlinking, then the creation of shared libraries is turned of. The creation of static libraries is still possible. The reason for this is that it has little sense in making a smarlinked dynamica library. The whole shared library is loaded into memory anyway by the dynamic linker (or WINDOWS NT), so there would be no gain in size by making it smartinked.
When the compiler encounters a call to an object's constructor, it sets up the stack frame for the call, and inserts a call to the Help_Constructor procedure before issuing the call to the real constructor. The helper procedure allocates the needed memory (if needed) and inserts the VMT pointer in the object. After that, the real constructor is called.
A call to Help_Destructor is inserted in every destructor declaration, just before the destructor's exit sequence.
If the object you defined has no virtual methods, then a nil is stored in the VMT pointer. This ensures that the size of objects is equal, whether they have virtual methods ore not.
The memory allocated looks as in table (ObjMem) .
The VMT is constructed by the compiler. Every instance of an object receives a pointer to its VMT.
By default (compiler version 0.9.4 and up), the assembly file is removed after it has been compiled. Only in the case of the -s command-line option, the assembly file must be left on disk, so the assembler can be called later. You can disable the erasing of the assembler file with the -a switch.
The unit file contains all the information the compiler needs to use the unit:
The detailed contents and structure of this file are described in the first appendix. You can examine a unit description file using the dumpppu program, which shows the contents of the file.
If you want to distribute a unit without source code, you must provide both the unit description file and the object file.
You can also provide a C header file to go with the object file. In that case, your unit can be used by someone who wishes to write his programs in C. However, you must make this header file yourself since the Free Pascal compiler doesn't make one for you.
When you compile a program, the compiler produces again 2 files :
The assembly language file is converted to an object file by the assembler, and then linked together with the rest of the units and a program header, to form your final program.
The program header file is a small assembly program which provides the entry point for the program. This is where the execution of your program starts, so it depends on the operating system, because operating systems pass parameters to executables in wildly different ways.
It's name is prt0.o, and the source file resides in prt0.s or some variant of this name. It usually resided where the system unit source for your system resides. It's main function is to save the environment and command-line arguments, set up the stack. Then it calls the main program.
Here is an example:
uses MMX; { include some predefined data types } const { tmmxword = array[0..3] of word;, declared by unit MMX } w1 : tmmxword = (111,123,432,4356); w2 : tmmxword = (4213,63456,756,4); var w3 : tmmxword; l : longint; begin if is_mmx_cpu then { is_mmx_cpu is exported from unit mmx } begin {$mmx+} { turn mmx on } w3:=w1+w2; {$mmx-} end else begin for i:=0 to 3 do w3[i]:=w1[i]+w2[i]; end; end.
One important point of MMX is the support of saturated operations. If a operation would cause an overflow, the value stays at the highest or lowest possible value for the data type: If you use byte values you get normally 250+12=6. This is very annoying when doing color manipulations or changing audio samples, when you have to do a word add and check if the value is greater than 255. The solution is saturation: 250+12 gives 255. Saturated operations are supported by the MMX unit. If you want to use them, you have simple turn the switch saturation on: $saturation+
Here is an example:
Program SaturationDemo; { example for saturation, scales data (for example audio) with 1.5 with rounding to negative infinity } var audio1 : tmmxword; const helpdata1 : tmmxword = ($c000,$c000,$c000,$c000); helpdata2 : tmmxword = ($8000,$8000,$8000,$8000); begin { audio1 contains four 16 bit audio samples } {$mmx+} { convert it to $8000 is defined as zero, multiply data with 0.75 } audio1:=tmmxfixed16(audio1+helpdata2)*tmmxfixed(helpdata1); {$saturation+} { avoid overflows (all values>$7fff becomes $ffff) } audio1:=(audio1+helpdata2)-helpdata2; {$saturation-} { now mupltily with 2 and change to integer } audio1:=(audio1 shl 1)-helpdata2; {$mmx-} end.
In the beginning of 1997 the MMX instructions were introduced in the Pentium processors, so multitasking systems wouldn't save the newly introduced MMX registers. To work around that problem, Intel mapped the MMX registers to the FPU register.
The consequence is that you can't mix MMX and floating point operations. After using MMX operations and before using floating point operations, you have to call the routine EMMS of the MMX unit. This routine restores the FPU registers.
careful: The compiler doesn't warn if you mix floating point and MMX operations, so be careful.
The MMX instructions are optimized for multi media (what else?).
So it isn't possible to perform each operation, some opertions
give a type mismatch, see section for the supported
MMX operations
An important restriction is that MMX operations aren't range or overflow checked, even when you turn range and overflow checking on. This is due to the nature of MMX operations.
The MMX unit must be always used when doing MMX operations because the exit code of this unit clears the MMX unit. If it wouldn't do that, other program will crash. A consequence of this is that you can't use MMX operations in the exit code of your units or programs, since they would interfere with the exit code of the MMX unit. The compiler can't check this, so you are responsible for this !
The fact that 32-bit code is used, means that some of the older Turbo Pascal constructs and functions are obsolete. The following is a list of functions which shouldn't be used anymore:
You shouldn't use these functions, since they are very non-portable, they're specific to DOS and the ix86 processor. The Free Pascal compiler is designed to be portable to other platforms, so you should keep your code as portable as possible, and not system specific. That is, unless you're writing some driver units, of course.
When a function or procedure is called, then the following is done by the compiler :
The resulting stack frame upon entering looks as in table (StackFrame) .
The stack is cleared with the ret I386 instruction, meaning that the size of all pushed parameters is limited to 64K.
Under the DOS targets , the default stack is set to 256Kb. This value cannot be modified for the GO32V1 target. But this can be modified with the GO32V2 target using a special DJGPP utility stubedit. It is to note that the stack size may be changed with some compiler switches, this stack size, if greater then the default stack size will be used instead, otherwise the default stack size is used.
Under Linux, stack size is only limited by the available memory by the system.
Under OS/2, stack size is determined by one of the runtime environment variables set for EMX. Therefore, the stack size is user defined.
All depending on the processor target, the stack can be cleared in two manners, if the target processor is a MC68020 or higher, the stack will be cleared with a simple rtd instruction, meaning that the size of all pushed parameters is limited to 32K.
Otherwise on MC68000/68010 processors, the stack clearing mechanism is sligthly more complicated, the exit code will look like this:
{ move.l (sp)+,a0 add.l paramsize,a0 move.l a0,-(sp) rts }
Under AmigaOS, stack size is determined by the user, which sets this value using the stack program. Typical sizes range from 4K to 40K.
Under Atari TOS, stack size is currently limited to 8K, and it cannot be modified. This may change in a future release of the compiler.
The growheap function issues a system call to try to increase the size of the memory available to your program. It first tries to increase memory in a 1 Mb. chunk. If this fails, it tries to increase the heap by the amount you requested from the heap.
If the call to GrowHeap has failed, then a run-time error is generated, or nil is returned, depending on the GrowHeap result.
If the call to GrowHeap was successful, then the needed memory will be allocated.
The run-time library keeps a linked list of allocated blocks with size up to 256 bytes8.1. By default, it keeps 32 of these lists8.2.
When a piece of memory in a block is deallocated, the heap manager doesn't really deallocate the occupied memory. The block is simply put in the linked list corresponding to its size.
When you then again request a block of memory, the manager checks in the list if there is a non-allocated block which fits the size you need (rounded to 8 bytes). If so, the block is used to allocate the memory you requested.
This method of allocating works faster if the heap is very fragmented, and you allocate a lot of small memory chunks.
Since it is invisible to the program, this provides an easy way of improving the performance of the heap manager.
The split heap can be used to quickly release a lot of blocks you alloated previously.
Suppose that in a part of your program, you allocate a lot of memory chunks on the heap. Suppose that you know that you'll release all this memory when this particular part of you program is finished.
In Turbo Pascal, you could foresee this, and mark the position of the heap (using the Mark function) when entering this particular part of your program, and release the occupied memory in one call with the Release call.
For most purposes, this works very good. But sometimes, you may need to allocate something on the heap that you don't want deallocated when you release the allocated memory. That is where the split heap comes in.
When you split the heap, the heap manager keeps 2 heaps: the base heap (the normal heap), and the temporary heap. After the call to split the heap, memory is allocated from the temporary heap. When you're finished using all this memory, you unsplit the heap. This clears all the memory on the split heap with one call. After that, memory will be allocated from the base heap again.
So far, nothing special, nothing that can't be done with calls to mark and release. Suppose now that you have split the heap, and that you've come to a point where you need to allocate memory that is to stay allocated after you unsplit the heap again. At this point, mark and release are of no use. But when using the split heap, you can tell the heap manager to -temporarily- use the base heap again to allocate memory. When you've allocated the needed memory, you can tell the heap manager that it should start using the temporary heap again. When you're finished using the temporary heap, you release it, and the memory you allocated on the base heap will still be allocated.
To use the split-heap, you must recompile the run-time library with the TempHeap symbol defined. This means that the following functions are available :
procedure Split_Heap; procedure Switch_To_Base_Heap; procedure Switch_To_Temp_Heap; procedure Switch_Heap; procedure ReleaseTempHeap; procedure GetempMem(var p : pointer;size : longint);split_heap is used to split the heap. It cannot be called two times in a row, without a call to releasetempheap. Releasetempheap completely releases the memory used by the temporary heap. Switching temporarily back to the base heap can be done using the switch_to_base_heap call, and returning to the temporary heap is done using the switch_to_temp_heap call. Switching from one to the other without knowing on which one your are right now, can be done using the switch_heap call, which will split the heap first if needed.
A call to GetTempMem will allocate a memory block on the temporary heap, whatever the current heap is. The current heap after this call will be the temporary heap.
Typically, what will appear in your code is the following sequence :
Split_Heap ... { Memory allocation } ... { !! non-volatile memory needed !!} Switch_To_Base_Heap; getmem (P,size); Switch_To_Temp_Heap; ... {Memory allocation} ... ReleaseTempHeap; {All allocated memory is now freed, except for the memory pointed to by 'P' } ...
Because Free Pascal is a 32 bit compiler, and uses a DOS extender, accessing DOS memory isn't trivial. What follows is an attempt to an explanation of how to access and use DOS or real mode memory8.3.
In Proteced Mode, memory is accessed through Selectors and Offsets. You can think of Selectors as the protected mode equivalents of segments.
In Free Pascal, a pointer is an offset into the DS selector, which points to the Data of your program.
To access the (real mode) DOS memory, somehow you need a selector that points to the DOS memory. The GO32 unit provides you with such a selector: The DosMemSelector variable, as it is conveniently called.
You can also allocate memory in DOS's memory space, using the global_dos_alloc function of the GO32 unit. This function will allocate memory in a place where DOS sees it.
As an example, here is a function that returns memory in real mode DOS and returns a selector:offset pair for it.
procedure dosalloc(var selector : word; var segment : word; size : longint); var result : longint; begin result := global_dos_alloc(size); selector := word(result); segment := word(result shr 16); end;(you need to free this memory using the global_dos_free function.)
You can access any place in memory using a selector. You can get a selector using the allocate_ldt_descriptor function, and then let this selector point to the physical memory you want using the set_segment_base_address function, and set its length using set_segment_limit function. You can manipulate the memory pointed to by the selector using the functions of the GO32 unit. For instance with the seg_fillchar function. After using the selector, you must free it again using the free_ldt_selector function.
More information on all this can be found in the Unit reference, the chapter on the GO32 unit.
The following sections describe the general optimizations done by the compiler, they are non processor specific. Some of these require some compiler switch override while others are done automatically (those which require a switch will be noted as such).
In Free Pascal, if the operand(s) of an operator are constants, they will be evaluated at compile time.
Example
x:=1+2+3+6+5; will generate the same code as x:=17;
Furthermore, if an array index is a constant, the offset will be evaluated at compile time. This means that accessing MyData[5] is as efficient as accessing a normal variable.
Finally, calling Chr, Hi, Lo, Ord, Pred, or Succ functions with constant parameters generates no run-time library calls, instead, the values are evaluated at compile time.
Using the same constant string two or more times generates only one copy of the string constant.
Evaluation of boolean expression stops as soon as the result is known, which makes code execute faster then if all boolean operands were evaluated.
Using the in operator is always more efficient then using the
equivalent <>
, =
, <=
, >=
, <
and >
operators. This is because range comparisons can be done more easily with
in then with normal comparison operators.
Sets which contain less then 33 elements can be directly encoded using a 32-bit value, therefore no run-time library calls to evaluate operands on these sets are required; they are directly encoded by the code generator.
Assignments of constants to variables are range checked at compile time, which removes the need the generation of runtime range checking code.
Remark: This feature was not implemented before version 0.99.5 of Free Pascal.
When one of the operands in a multiplication is a power of two, they are encoded using arithmetic shifts instructions, which generates more efficient code.
Similarly, if the divisor in a div operation is a power of two, it is encoded using arithmetic shifts instructions.
The same is true when accessing array indexes which are powers of two, the address is calculated using arithmetic shifts instead of the multiply instruction.
By default all variables larger then a byte are guaranteed to be aligned at least on a word boundary.
Furthermore all pointers allocated using the standard runtime library (New and GetMem among others) are guaranteed to return pointers aligned on a quadword boundary (64-bit alignment).
Alignment of variables on the stack depends on the target processor.
Remark: Quadword alignment of pointers is not guaranteed on systems which don't use an internal heap, such as for the Win32 target.
Remark: Alignment is also done between fields in records, objects and classes, this is not the same as in Turbo Pascal and may cause problems when using disk I/O with these types. To get no alignment between fields use the packed directive or the {$PackRecords n} switch. For further information, take a look at the reference manual under the record heading.
This feature removes all unreferenced code in the final executable file, making the executable file much smaller.
Smart linking is switched on with the -Cx command-line switch, or using the {$SMARTLINK ON} global directive.
Remark: Smart linking was implemented starting with version 0.99.6 of Free Pascal.
The following runtime library routines are coded directly into the final executable : Lo, Hi, High, Sizeof, TypeOf, Length, Pred, Succ, Inc, Dec and Assigned.
Remark: Inline Inc and Dec were not completely implemented until version 0.99.6 of Free Pascal.
When using the -O1 (or higher) switch, case statements will be generated using a jump table if appropriate, to make them execute faster.
Under specific conditions, the stack frame (entry and exit code for
the routine, see section ) will be omitted, and the
variable will directly be accessed via the stack pointer.
Conditions for omission of the stack frame :
When using the -Or switch, local variables or parameters which are used very often will be moved to registers for faster access.
Remark: Register variable allocation is currently an experimental feature, and should be used with caution.
Here follows a listing of the opimizing techniques used in the compiler:
Although you can enable uncertain optimizations in most cases, for people who do not understand the following technical explanation, it might be the safest to leave them off.
If uncertain optimizations are enabled, the CSE algortihm assumes that
The following example will produce bad code when you switch on uncertain optimizations:
Var temp: Longint; Procedure Foo(Var Bar: Longint); Begin If (Bar = temp) Then Begin Inc(Bar); If (Bar <> temp) then Writeln('bug!') End End; Begin Foo(Temp); End.The reason it produces bad code is because you access the global variable Temp both through its name Temp and through a pointer, in this case using the Bar variable parameter, which is nothing but a pointer to Temp in the above code.
On the other hand, you can use the uncertain optimizations if you access global/local variables or parameters through pointers, and only access them through this pointer9.1.
For example:
Type TMyRec = Record a, b: Longint; End; PMyRec = ^TMyRec; TMyRecArray = Array [1..100000] of TMyRec; PMyRecArray = ^TMyRecArray; Var MyRecArrayPtr: PMyRecArray; MyRecPtr: PMyRec; Counter: Longint; Begin New(MyRecArrayPtr); For Counter := 1 to 100000 Do Begin MyRecPtr := @MyRecArrayPtr^[Counter]; MyRecPtr^.a := Counter; MyRecPtr^.b := Counter div 2; End; End.Will produce correct code, because the global variable MyRecArrayPtr is not accessed directly, but only through a pointer (MyRecPtr in this case).
In conclusion, one could say that you can use uncertain optimizations only when you know what you're doing.
Using the -O2 switch does several optimizations in the code produced, the most notable being:
subl $4,%esp
") instead of slower, smaller instructions
("enter $4
"). This is the default setting.
movzbl (mem), %eax|to a combination of simpler instructions
xorl %eax, %eax movb (mem), %alfor the Pentium.
Note: Code blocks which contain an assembler block, are not processed at all by the optimizer at this time. Update: as of versino 0.99.11, the Pascal code surrounding the assembler blocks is optimized.
This is where can be found processor specific information on Floating point code generated by the compiler.
All normal floating point types map to their real type, including comp and extended.
Early generations of the Motorola 680x0 processors did not have integrated floating point units, so to circumvent this fact, all floating point operations are emulated (when the $E+ switch ,which is the default) using the IEEE Single floating point type. In other words when emulation is on, Real, Single, Double and Extended all map to the single floating point type.
When the $E switch is turned off, normal 68882/68881/68040 floating point opcodes are emitted. The Real type still maps to Single but the other types map to their true floating point types. Only basic FPU opcodes are used, which means that it can work on 68040 processors correctly.
Remark: Double and Extended types in true floating point mode have not been extensively tested as of version 0.99.5.
Remark: The comp data type is currently not supported.
The best and most updated documentation about the ppu files can be found in ppu.pas and ppudump.pp which can be found in rtl/utils/.
To read or write the ppufile, you can use the ppu unit ppu.pas which has an object called tppufile which holds all routines that deal with ppufile handling. Describing the layout of a ppufile, the methods which can be used for it are described.
A unit file consists of basically five or six parts:
We will first create an object ppufile which will be used below. We are opening unit test.ppu as an example.
var ppufile : pppufile; begin { Initialize object } ppufile:=new(pppufile,init('test.ppu'); { open the unit and read the header, returns false when it fails } if not ppufile.open then error('error opening unit test.ppu'); { here we can read the unit } { close unit } ppufile.close; { release object } dispose(ppufile,done); end;
Note: When a function fails (for example not enough bytes left in an entry) it sets the ppufile.error variable.
The header consists of a record containing 24 bytes:
tppuheader=packed record id : array[1..3] of char; { = 'PPU' } ver : array[1..3] of char; compiler : word; cpu : word; target : word; flags : longint; size : longint; { size of the ppufile without header } checksum : longint; { checksum for this ppufile } end;
The header is already read by the ppufile.open command. You can access all fields using ppufile.header which holds the current header record.
field | description | ||||||||||||||||||
id | this is allways 'PPU', can be checked with function ppufile.CheckPPUId:boolean; | ||||||||||||||||||
ver | ppu version, currently '015', can be checked with function ppufile.GetPPUVersion:longint; (returns 15) | ||||||||||||||||||
compiler | compiler version used to create the unit. Doesn't contain the patchlevel. Currently 0.99 where 0 is the high byte and 99 the low byte | ||||||||||||||||||
cpu | cpu for which this unit is created. 0 = i386 1 = m68k | ||||||||||||||||||
target | target for which this unit is created, this depends also on the
cpu!
For i386:
For m68k:
|
||||||||||||||||||
flag | the unit flags, contains a combination of the uf_ constants which are definied in ppu.pas | ||||||||||||||||||
size | size of this unit without this header | ||||||||||||||||||
checksum | checksum of the interface parts of this unit, which determine if a unit is changed or not, so other units can see if they need to be recompiled |
After this header follow the sections. All sections work the same! A section contains of entries and is ended with also an entry, but containing the specific ibend constant (see ppu.pas for a list).
Each entry starts with an entryheader.
tppuentry=packed record id : byte; nr : byte; size : longint; end;
field | Description |
id | this is 1 or 2 and can be check if it the entry is correctly found. 1 means its a main entry, which says that it is part of the basic layout as explained before. 2 toggles that it it a sub entry of a record or object |
nr | contains the ib constant number which determines what kind of entry it is |
size | size of this entry without the header, can be used to skip entries very easily. |
To read an entry you can simply call ppufile.readentry:byte, it returns the tppuentry.nr field, which holds the type of the entry. A common way how this works is (example is for the symbols):
repeat b:=ppufile.readentry; case b of ib<etc> : begin end; ibendsyms : break; end; until false;
Then you can parse each entry type yourself. ppufile.readentry will take care of skipping unread bytes in the entry an read the next entry correctly! A special function is skipuntilentry(untilb:byte):boolean; which will read the ppufile until it finds entry untilb in the main entries.
Parsing an entry can be done with ppufile.getxxx functions. The available functions are:
procedure ppufile.getdata(var b;len:longint); function getbyte:byte; function getword:word; function getlongint:longint; function getreal:ppureal; function getstring:string;
To check if you're at the end of an entry you can use the following function:
function EndOfEntry:boolean;notes:
A complete list of entries and what their fields contain can be found in ppudump.pp.
Creating a new ppufile works almost the same as writing. First you need to init the object and call create:
ppufile:=new(pppufile,'output.ppu'); ppufile.create;
After that you can simply write all needed entries. You'll have to take care that you write at least the basic entries for the sections:
ibendinterface ibenddefs ibendsyms ibendbrowser (only when you've set uf_has_browser!) ibendimplementation ibend
Writing an entry is a little different than reading it. You need to first put everything in the entry with ppufile.putxxx:
procedure putdata(var b;len:longint); procedure putbyte(b:byte); procedure putword(w:word); procedure putlongint(l:longint); procedure putreal(d:ppureal); procedure putstring(s:string);
After putting all the things in the entry you need to call ppufile.writeentry(ibnr:byte) where ibnr is the entry number you're writing.
At the end of the file you need to call ppufile.writeheader to write the new header to the file. This takes automatically care of the new size of the ppufile. When that is also done you can call ppufile.close and dispose the object.
Extra functions/variables available for writing are:
ppufile.NewHeader; ppufile.NewEntry;This will give you a clean header or entry. Normally called automatically in ppufile.writeentry, so you can't forget it.
ppufile.flush;
to flush the current buffers to the disk
ppufile.do_crc:boolean;set to false if you don't want that the crc is updated, this is necessary if you write for example the browser data.
All compiler source files are in one directory, normally in source/compiler. For more informations about the structure of the compiler have a look at the Compiler Manual which contains also some informations about compiler internals.
Here we list the exact effect of the different compiler modes. They can be set with the $Mode switch, or by command line switches.
These files are installed in the following directories:
The template Makefile searches for the makefile.fpc in the following places :
The following sections explain what variables are set by makefile.fpc, what variables it expects to be set, and what targets it defines. After that, some settings in the template makefile are explained.
The following programs are needed by the makefile to function correctly:
Many variables affect the behaviour of the makefile. The variables can be split in several groups:
In principle, the makefile.fpc only expects one variable to be set:
The first set of variables controls the directories used in the makefile:
The second set of variables controls the targets that are constructed by the makefile:
The following variables control the compiler command-line:
All of the following variables are only set by makefile.fpc, if they aren't already defined. This means that you can override them by setting them on the make command line, or setting them in the makefile you use, BEFORE makefile.fpc is included. The following sets of variables are defined:
The following directories are defined by the makefile:
The following variables are program names, used in makefile targets.
The following variables denote extensions of files. These variables include the . (dot) of the extension. They are appended to object names.
The following variables are defined to make targets and rules easier:
The makefile.fpc defines a series of targets, which can be called by your own targets. They have names that resemble default names (such as 'all', 'clean'), only they have fpc_ prepended.
The makefile makes the following pattern rules:
The following build targets are defined:
The following cleaning targets are defined:
The following archiving targets are defined:
The following targets produce information about the makefile:
The template makefile that comes with Free Pascal does nothing other than offering you some variables to be set for the makefile.fpc. After that it loads the makefile.fpc in the indicated places.
Finally it declares a set of default targets:
You can override each of these targets to suit your setup.
If you just have to compile some units and programs, you only need to set the following variables:
You may want to set some of the following variables:
You may also set any of the variables that appear in the previous sections, to override default behaviour of the makefile.
After having set these variables, you can run 'make info' to see whether all variables are set to you satisfaction. If the makefile.fpc is not found, this command will inform you of this.
After that, a simple 'make all' will make all units and executables.
The Free Pascal team releases at intervals a completely prepared package, with compiler and units all ready to use, the so-called releases. After a release, work on the compiler continues, bugs are fixed and features are added. The Free Pascal team doesn't make a new release whenever they change something in the compiler, instead the sources are available for anyone to use and compile. Compiled versions of RTL and compiler are also made daily, and put on the web.
There are, nevertheless, circumstances when you'll want to compile the compiler yourself. For instance if you made changes to compiler code, or when you download the compiler via CVS.
There are essentially 2 ways of recompiling the compiler: by hand, or using the makefiles. Each of these methods will be discussed.
To compile the compiler easily, it is best to keep the following directory structure (a base directory of /pp/src is supposed, but that may be different):
/pp/src/Makefile /makefile.fpc /rtl/linux /inc /i386 /... /compilerIf you want to use the makefiles, you must use the above directory tree.
The compiler and rtl source are zipped in such a way that if you unzip both files in the same directory (/pp/src in the above) the above directory tree results.
The makefile.fpc and Makefile come from the base.zip file on the ftp site. If you compile manually, you don't need them.
There are 2 ways to start compiling the compiler and RTL. Both ways must be used, depending on the situation. Usually, the RTL must be compiled first, before compiling the compiler, after which the compiler is compiled using the current compiler. In some special cases the compiler must be compiled first, with a previously compiled RTL.
How to decide which should be compiled first? In general, the answer is that you should compile the RTL first. There are 2 exceptions to this rule:
When compiling with make it is necessary to have the above directory structure. Compiling the compiler is achieved with the target cycle.
Under normal circumstances, recompiling the compiler is limited to the following instructions (assuming you start in directory /pp/src):
cd compiler make cycleThis will work only if the makefile.fpc is installed correctly and if the needed tools are present in the PATH. Which tools must be installed can be found in appendix
The above instructions will do the following:
Compiling for another target: When you want to compile the compiler for another target, you must specify the OS_TARGET makefile variable. It can be set to the following values: win32, go32v2, os2 and linux. As an example, cross-compilation for the go32v2 target from the win32 target is chosen:
cd compiler make cycle OS_TARGET=go32v2This will compile the go32v2 RTL, and compile a go32v2 compiler.
If you want to compile a new compiler, but you want the compiler to be compiled first using an existing compiled RTL, you should specify the all target, and specify another RTL directory than the default (which is the ../rtl/$(OS_TARGET) directory). For instance, assuming that the compiled RTL units are in /pp/rtl, you could type
cd compiler make clean make all UNITDIR=/pp/rtl
This will then compile the compiler using the RTL units in /pp/rtl. After this has been done, you can do the 'make cycle', starting with this compiler:
make cycle PP=./ppc386This will do the make cycle from above, but will start with the compiler that was generated by the make all instruction.
In all cases, many options can be passed to make to influence the compile process. In general, the makefiles add any needed compiler options to the command-line, so that the RTL and compiler can be compiled. You can specify additional options (e.g. optimization options) by passing them in OPT.
Compiling by hand is difficult and tedious, but can be done. We'll treat the compilation of RTL and compiler separately.
ppc386 -Tlinux -b- -Fi../inc -Fi../i386 -FE. -di386 -Us -Sg syslinux.pp ppc386 -Tlinux -b- -Fi../inc -Fi../i386 -FE. -di386 ../inc/strings.pp ppc386 -Tlinux -b- -Fi../inc -Fi../i386 -FE. -di386 dos.pp ppc386 -Tlinux -b- -Fi../inc -Fi../i386 -FE. -di386 ../inc/objects.ppThese are the minimum command-line options, needed to compile the RTL.
For another processor, you should change the i386 into the appropriate processor. For another operating system (target) you should change the syslinux in the appropriate system unit file, and you should change the target OS setting (-T).
Depending on the target OS there are other units that you may wish to compile, but which are not strictly needed to recompile the compiler. The following units are available for all plaforms:
Compiling the compiler can be done with one statement. It's always best to remove all units from the compiler directory first, so something like
rm *.ppu *.oon LINUX, and on DOS
del *.ppu del *.oAfter this, the compiler can be compiled with the following command-line:
ppc386 -Tlinux -Fu../rtl/linux -di386 -dGDB pp.pasSo, the minimum options are:
ppc386 -di386 -Sg pp.pas
You can define some other command-line options, but the above are the minimum. A list of recognised options can be found in table (FPCdefines) .
Define | does what |
USE_RHIDE | Generates errors and warnings in a format recognized |
by RHIDE. | |
TP | Needed to compile the compiler with Turbo or Borland Pascal. |
Delphi | Needed to compile the compiler with Delphi from Borland. |
GDB | Support of the GNU Debugger. |
I386 | Generate a compiler for the Intel i386+ processor family. |
M68K | Generate a compiler for the M68000 processor family. |
USEOVERLAY | Compiles a TP version which uses overlays. |
EXTDEBUG | Some extra debug code is executed. |
SUPPORT_MMX | only i386: enables the compiler switch MMX which |
allows the compiler to generate MMX instructions. | |
EXTERN_MSG | Don't compile the msgfiles in the compiler, always use |
external messagefiles (default for TP). | |
NOAG386INT | no Intel Assembler output. |
NOAG386NSM | no NASM output. |
NOAG386BIN | leaves out the binary writer. |