Subsections

7. Porting Turbo Pascal Code

Free Pascal was designed to resemble Turbo Pascal as closely as possible. There are, of course, restrictions. Some of these are due to the fact that Free Pascal is a 32-bit compiler. Other restrictions result from the fact that Free Pascal works 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 -So or -S2 switches.

In the following sections we will list the Turbo Pascal constructs which are not supported in Free Pascal, and we will list in what ways Free Pascal extends the Turbo Pascal language.

7.1 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.

  1. Duplicate case labels are not allowed. This is a bug in Turbo Pascal and will not be changed.
  2. Parameter lists of previously defined functions and procedures must match exactly. The reason for this is the function overloading mechanism of Free Pascal. (however, the See So option solves this.)
  3. (* ... *) as comment delimiters are not allowed in versions older than 0.9.1. This can easily be remedied with a grown-up editor.
  4. The MEM, MEMW, MEML and PORT variables for memory and port access are not available in the system unit. This is due to the operating system. Under DOS, the extender unit (GO32.PPU) implements the mem constuct. under LINUX, the ports unit implements such a construct.
  5. PROTECTED, PUBLIC, PUBLISHED, TRY, FINALLY, EXCEPT, RAISE are reserved words. This means you cannot create procedures or variables with the same name. While they are not reserved words in Turbo Pascal, they are in Delphi. Using the -So switch will solve this problem if you want to compile Turbo Pascal code that uses these words.
  6. The reserved words FAR, NEAR are ignored. This is because Free Pascal is a 32 bit compiler, so they're obsolete.
  7. INTERRUPT will work only on the DOS target.
  8. Boolean expressions are only evaluated until their result is completely determined. The rest of the expression will be ignored.
  9. By default the compiler uses AT&T assembler syntax. This is mainly because Free Pascal uses GNU as. However, other assembler forms are available. For more information, see Programmers' guide.
  10. Turbo Vision is not completely available. There is FreeVision, but the degree of compatibility with Turbo Vision is unclear at this time7.1.
  11. The 'overlay' unit is not available. It also isn't necessary, since Free Pascal is a 32 bit compiler, so program size shouldn't be a point.
  12. There are more reserved words. (see appendix [*] for a list of all reserved words.)
  13. The command-line parameters of the compiler are different.
  14. Compiler switches and directives are mostly the same, but some extra exist.
  15. Units are not binary compatible.
  16. Sets are always 4 bytes in Free Pascal; this means that some typecasts which were possible in Turbo Pascal are no longer possible in Free Pascal.
  17. A file is opened for output only (using fmOutput) when it is opened with Rewrite. In order to be able to read from it, it should be reset with Reset.
  18. Destructors cannot have parameters. This restriction can be solved by using the -So switch.
  19. There can be only one destructor. This restriction can also be solved by using the -So switch.
  20. 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 before f(3).

7.2 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.
  1. There are more reserved words. (see appendix [*] for a list of all reserved words.)
  2. Functions can also return complex types, such as records and arrays.
  3. You can handle function results in the function itself, as a variable. 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>4 is a recursive call. To do a recursive call in this you must append () behind the function name:
    function a : longint;
    
    begin
       a:=12;
       { this is the recursive call }
       if a()>4 then
         begin
            {...}
         end;
    end;
    
  4. There is partial support of Delphi constructs. (see the Programmers' guide for more information on this).
  5. The exit call 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;
    
  6. Free Pascal supports 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 DoSomething with an argument of type Longint or Real.
    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 See So switch disables overloading. When you use it, the above will compile, as in Turbo Pascal.
  7. Operator overloading. Free Pascal allows to overload operators, i.e. you can define e.g. the '+' operator for matrices.
  8. On FAT16 and FAT32 systems, long file names are supported.

7.3 Turbo Pascal compatibility mode

When you compile a program with the -So switch, the compiler will attempt to mimic the Turbo Pascal compiler in the following ways:

7.4 A note on long file names under DOS

Under WINDOWS 95 and higher, long filenames are supported. Compiling for the win32 target ensures that long filenames are supported in all functions that do fie or disk access in any way.

Moreover, Free Pascal supports 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 all versions of Windows, with the possible exception of WINDOWS 2000. The system unit will check this by calling DOS function 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.



root
2000-12-20