7.1 Free Pascal compiler modes

The Free Pascal team tries to create a compiler that can compile as much as possible code produced for Turbo Pascal, Delphi or the Mac pascal compilers: this should make sure that porting code that was written for one of these compilers is as easy as possible.

At the same time, the Free Pascal developers have introduced a lot of extensions in the Object Pascal language. To reconcile these different goals, and to make sure that people can produce code which can still be compiled by the Turbo Pascal and Delphi compilers, the compiler has a concepts of ’compiler modes’. In a certain compiler mode, the compiler has certain functionalities switched on or off. This allows to introduce a compatibility mode in which only features supported by the original compiler are supported. Currently, 5 modes are supported:

FPC
This is the original Free Pascal compiler mode: here all language constructs except classes, interfaces and exceptions are supported. Objects are supported in this mode. This is the default mode of the compiler.
OBJFPC
This is the same mode as FPC mode, but it also includes classes, interfaces and exceptions.
TP
Turbo Pascal compatibility mode. In this mode, the compiler tries to mimic the Turbo Pascal compiler as closely as possible. Obviously, only 32-bit or 64-bit code can be compiled.
DELPHI
Delphi compatibility mode. In this mode, the compiler tries to resemble the Delphi compiler as best as it can: Most Delphi 7 and above features are implemented.
DELPHIUNICODE
Delphi compatibility mode. In this mode, the compiler tries to resemble the Delphi compiler as best as it can: All Delphi 2009 and above features are implemented. In this mode, string equals a unicode string.
MACPAS
the Mac Pascal compatibility mode. In this mode, the compiler attempts to allow all constructs that are implemented in Mac pascal. In particular, it attempts to compile the universal interfaces.
ISO
Standard Pascal, ISO 7185 mode. In this mode, the compiler complies with the requirements of level 0 of ISO/IEC 7185.
ExtendedPascal
Standard Extended Pascal, ISO 10206 mode. In this mode, the compiler mostly complies with the requirements of ISO/IEC 10206.

The compiler mode can be set on a per-unit basis: each unit can have its own compiler mode, and it is possible to use units which have been compiled in different modes intertwined. The mode can be set in one of 2 ways:

1.
On the command line, with the -M switch.
2.
In the source file, with the {$MODE } directive.

Both ways take the name of the mode as an argument. If the unit or program source file does not specify a mode, the mode specified on the command-line is used. If the source file specifies a mode, then it overrides the mode given on the command-line.

Thus compiling a unit with the -M switch as follows:

fpc -MOBJFPC myunit

is the same as having the following mode directive in the unit:

{$MODE OBJFPC}  
Unit myunit;

The MODE directive should always be located before the uses clause of the unit interface or program uses clause, because setting the mode may result in the loading of an additional unit as the first unit to be loaded.

Note that the {$MODE } directive is a global directive, i.e. it is valid for the whole unit; Only one directive can be specified.

The mode has no influence on the availability of units: all available units can be used, independent of the mode that is used to compile the current unit or program.