Turbo Pascal
Free Pascalwas originally designed to resemble Turbo Pascal as closely as possible. There are, of course, restrictions. Some of these are due to the fact that Turbo Pascal was developed for 16-bit architectures whereas Free Pascalis a 32-bit/64-bit compiler. Other restrictions result from the fact that Free Pascalworks on more than one operating system.
In general we can say that if you keep your program code close to ANSI Pascal, you will have no problems porting from Turbo Pascal, or even Delphi, to Free Pascal. To a large extent, the constructs defined by Turbo Pascal are supported. This is even more so if you use the -Mtp or -MObjfpc switches.
In the following sections we will list the Turbo Pascal and Delphi constructs which are not supported in Free Pascal, and we will list in what ways Free Pascalextends Turbo Pascal.
Things that will not work
Here we give a list of things which are defined/allowed in Turbo Pascal, but which are not supported by Free Pascal. Where possible, we indicate the reason.
Duplicate case labels are permitted in Turbo Pascal, but not in Free Pascal. This is actually a bug in Turbo Pascal, and so support for it will not be implemented in Free Pascal.
In Turbo Pascal, parameter lists of previously defined functions and procedures did not have to match exactly. In Free Pascal, they must. The reason for this is the function overloading mechanism of Free Pascal. However, the option overcomes this restriction.
The Turbo Pascal variables
MEM, MEMW, MEMLandPORTfor memory and port access are not available in the system unit. This is due to the operating system. Under DOS, both the system and the extender unit (GO32) implement the mem constuct. Under Linux, theportsunit implements such a construct for thePortsvariable.Turbo Pascal allows you to create procedure and variable names using words that are not permitted in that role in Free Pascal. This is because there are certain words that are reserved in Free Pascal (and Delphi) that are not reserved in Turbo Pascal, such as:
PROTECTED, PUBLIC, PUBLISHED, TRY, FINALLY, EXCEPT, RAISE. Using the-Mtpswitch will solve this problem if you want to compile Turbo Pascal code that uses these words (chapter Alphabetical list of reserved words for a list of all reserved words).The Turbo Pascal reserved words
FAR, NEARare ignored. This is because their purpose was limited to a 16-bit environment where calls were either far or near. This distinction does not exists in 32/64 bit environments.The Turbo Pascal
INTERRUPTdirective will work only on the Free PascalDOStarget. Other operating systems do not allow handling of interrupts by user programs.By default the Free Pascalcompiler uses
AT&Tassembler syntax. This is mainly because Free Pascaluses GNUas. However, other assembler forms are available. For more information, see the Programmer’s Guide.Turbo Pascal’s Turbo Vision is available in Free Pascalunder the name of FreeVision, which should be almost 100% compatible with Turbo Vision.
Turbo Pascal’s ’overlay’ unit is not available. This unit had it’s purpose in a 16-bit MS-DOS environment, but in modern environments this is no longer relevant.
The command line parameters of the compiler are different.
Compiler switches and directives are mostly the same, but some extra exist.
Units are not binary compatible. That means that you cannot use a
.tpuunit file, produced by Turbo Pascal, in a Free Pascalproject.The Free Pascal
TextRecstructure (for internal description of files) is not binary compatible with TP or Delphi.Sets are by default 4 bytes in Free Pascal; this means that some typecasts which were possible in Turbo Pascal are no longer possible in Free Pascal. However, there is a switch to set the set size, see Programmer’s Guidefor more information.
A file is opened for output only (using
fmOutput) when it is opened withRewrite. In order to be able to read from it, it should be reset withReset.Turbo Pascal destructors allowed parameters. This is not permitted in Free Pascal: by default, in Free Pascal, Destructors cannot have parameters. This restriction can be removed by using the
-Soswitch.Turbo Pascal permits more than one destructor for an object. In Free Pascal, there can be only one destructor. This restriction can also be removed by using the
-Soswitch.The order in which expressions are evaluated is not necessarily the same. In the following expression:
a := g(2) + f(3);
it is not guaranteed that
g(2)will be evaluated beforef(3).In Free Pascal, you need to use the address @ operator when assigning procedural variables.
Things which are extra
Here we give a list of things which are possible in Free Pascal, but which didn’t exist in Turbo Pascal or Delphi.
Free Pascalfunctions can also return complex types, such as records and arrays.
In Free Pascal, you can use the function return value in the function itself, as a variable. For example:
function a : longint; begin a:=12; while a>4 do begin {...} end; end;The example above would work with TP, but the compiler would assume that the
a>4is a recursive call. If a recursive call is actually what is desired, you must append()after the function name:function a : longint; begin a:=12; { this is the recursive call } if a()>4 then begin {...} end; end;In Free Pascal, there is partial support of Delphi constructs. (See the Programmer’s Guidefor more information on this).
The Free Pascal
exitcall accepts a return value for functions.function a : longint; begin a:=12; if a>4 then begin exit(a*67); {function result upon exit is a*67 } end; end;Free Pascalsupports function overloading. That is, you can define many functions with the same name, but with different arguments. For example:
procedure DoSomething (a : longint); begin {...} end; procedure DoSomething (a : real); begin {...} end;You can then call procedure
DoSomethingwith an argument of typeLongintorReal.
This feature has the consequence that a previously declared function must always be defined with the header completely the same:procedure x (v : longint); forward; {...} procedure x;{ This will overload the previously declared x} begin {...} end;This construction will generate a compiler error, because the compiler didn’t find a definition of
procedure x (v : longint);. Instead you should define your procedure x as:procedure x (v : longint); { This correctly defines the previously declared x} begin {...} end;The command line option disables overloading. When you use it, the above will compile, as in Turbo Pascal.
Operator overloading. Free Pascalallows operator overloading, e.g. you can define the ’+’ operator for matrices.
On FAT16 and FAT32 systems, long file names are supported.
Turbo Pascal compatibility mode
When you compile a program with the -Mtp switch, the compiler will attempt to mimic the Turbo Pascal compiler in the following ways:
Assigning a procedural variable doesn’t require an @ operator. One of the differences between Turbo Pascal and Free Pascalis that the latter requires you to specify an address operator when assigning a value to a procedural variable. In Turbo Pascal compatibility mode, this is not required.
Procedure overloading is disabled. If procedure overloading is disabled, the function header doesn’t need to repeat the function header.
Forward defined procedures don’t need the full parameter list when they are defined. Due to the procedure overloading feature of Free Pascal, you must always specify the parameter list of a function when you define it, even when it was declared earlier with
Forward. In Turbo Pascal compatibility mode, there is no function overloading; hence you can omit the parameter list:Procedure a (L : Longint); Forward; ... Procedure a ; { No need to repeat the (L : Longint) } begin ... end;Recursive function calls are handled differently. Consider the following example:
Function expr : Longint; begin ... Expr:=L: Writeln (Expr); ... end;
In Turbo Pascal compatibility mode, the function will be called recursively when the
writelnstatement is processed. In Free Pascal, the function result will be printed. In order to call the function recursively under Free Pascal, you need to implement it as follows :Function expr : Longint; begin ... Expr:=L: Writeln (Expr()); ... end;
You cannot assign procedural variables to untyped pointers; so the following is invalid:
a: Procedure; b: Pointer; begin b := a; // Error will be generated.
The @ operator is typed when applied on procedures.
You cannot nest comments.
Remark: The MemAvail and MaxAvail functions are no longer available in Free Pascalas of version 2.0. The reason for this incompatibility follows:
On modern operating systems, 10 the idea of "Available Free Memory" is not valid for an application. The reasons are:
One processor cycle after an application asked the OS how much memory is free, another application may have allocated everything.
It is not clear what "free memory" means: does it include swap memory, does it include disk cache memory (the disk cache can grow and shrink on modern OS’es), does it include memory allocated to other applications but which can be swapped out, etc.
Therefore, programs using MemAvail and MaxAvail functions should be rewritten so they no longer use these functions, because it does not make sense any more on modern OS’es. There are 3 possibilities:
Use exceptions to catch out-of-memory errors.
Set the global variable "ReturnNilIfGrowHeapFails" to
Trueand check after each allocation whether the pointer is different fromNil.Don’t care and declare a dummy function called
MaxAvailwhich always returnsHigh(LongInt)(or some other constant).
A note on long file names under DOS
Under Windows95 and higher, long filenames are supported. Compiling for the Windowstarget ensures that long filenames are supported in all functions that do file or disk access in any way.
Moreover, Free Pascalsupports the use of long filenames in the system unit and the Dos unit also for go32v2 executables. The system unit contains the boolean variable LFNsupport. If it is set to True then all system unit functions and Dos unit functions will use long file names if they are available. This should be so on Windows95 and 98, but not on WindowsNT or Windows2000. The system unit will check this by calling DOSfunction 71A0h and checking whether long filenames are supported on the C: drive.
It is possible to disable the long filename support by setting the LFNSupport variable to False; but in general it is recommended to compile programs that need long filenames as native Windowsapplications.
An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.
