Introduction to Free Pascal 2.0

After five years of development, Free Pascal 2.0 is ready. With the new compiler, its authors believe they are ready to become a larger open source development platform. In the MS-DOS world, Pascal was one of the major programming languages and is by means of Borland Delphi an important programming language in the Windows world. In the open source world, Free Pascal is the leading Pascal compiler and while open source is a bit biased using the C language, the Pascal language has a lot to offer to open source programmers.

Free Pascal is a GPL compiler for 32-bit and 64-bit CPU architectures such as Intel 32/64 bit, AMD 32/64 bit, SPARC, PowerPC and Intel ARM.
It is language compatible to Turbo Pascal, Delphi (till version 7), and partly Metrowerks Pascal. It supports many operating systems: Dos, Linux, Windows, OS/2, FreeBSD, NetBSD and OpenBSD, Novell Netware, Mac OS classic, Mac OS X and MorphOS.

It comes with a cross-platform Run-Time Library, many interfaces to existing libraries, and a large set of non-visual classes in the Free Component Library. Last but not least, a text-mode IDE exists on various platforms, and FPC comes with 1800 pages of documentation.

What is new when compared to version 1.0 ?

– More supported CPUs:
AMD64, SPARC, PPC (32 bit), ARM.
– More supported platforms:
Mac OS classic, Mac OS X, MorphOS, Novell Netware.
– Improved inlining
– Improved register variables
– Improved 80×86 assembler optimizer
– Threading support.
– Rudimentary variant support.
– Interfaces support.
– WideString support.
– More Delphi compatibility units.
– Greatly improved database access.
– New compiler mode:
MACPAS for compatibility with Pascal compilers on Mac OS.
– Complete documentation of RTL units, generated using a new documentation
tool, included in the distribution: fpdoc
– Completely rewritten Unix support.
– Completely rewritten compiler, allowing for easier ports to new
architectures.
– IDE
– Free Vision text-mode windowing system.
– Many new packages.
– And of course, Numerous bugfixes.

2. Why Pascal?


The 3d-game Lentil Wars is being written with Free Pascal

Because the open source world is a bit biased to C, we’ll have to answer the question “what is Pascal good for?”. To answer this question, we need to compare Pascal to other modern programming languages which are currently available. Pascal is about as old as the C programming language and there are many similarities between both languages; both are procedural third generation languages that allow the programmer to do structured programming and both languages have been used to write large real world applications.

New languages, like Java and C#, promise to ease programming by providing managed code with automatic memory management. As this frees the programmer from a task this makes programming somewhat easier. Therefore one can ask whether interest in old school languages is still justified. It is the case however, although recently a lot of progress has been made to reduce the speed disadvantages of managed code, these languages tend to produce software that requires large bloated runtime environments and is often perceived as slow by users. At this time, these are reasons for many programmers and users to dislike those new school languages. (By the way, how novel is managed code? Been there, done that, didn’t work. Remember the USCD P-system?)

Anyway, today, the majority of software being written runs directly on the operating system, and we believe this will at least remain the case for quite a while. As of such it is important to have good tools available to write such software. The various C compiler suites provide such a tool and so does Free Pascal. While Pascal and C are similar, the language Pascal has still many things to offer that aren’t available to many programmers. For example, Pascal’s traditional strength, type-safety, is as valid today as it was 30 years ago. Modular programming was introduced in a much nicer way and so are the object oriented extensions.

Pascal removes things for you to worry about. For example, every use of a string in C needs measures to be taken to prevent buffer overflows. Pascal does not remove any freedom from you, so if you would like to build a buffer overflow vulnerability in your code, go ahead! However, using something trivial like a string does not require you to worry about buffer overflows and as a result, they are less likely to happen.

Modern Pascal offers many ways to build better software. In modern object oriented software design, Pascal’s properties give you a powerful tool to guard the integrity of your objects, and interfaces give you the power of multiple inheritance without the semantical difficulties.
The use of RTTI (Run-Time Type Information) provide easy streaming and many RAD design options.
See for instance the Lazarus project, which makes a cross-platform IDE using Free Pascal.

As for Free Pascal, I’m sure many programmers will appreciate it compiles 9 times faster than GCC, provides clear, helpful error messages, and frees you from the task to write make files.

Summing up, programming in Pascal gives you as programmer many advantages over your collegues writing C.

3. Free Pascal


The 3D terrain renderer Voxel World was written with Free pascal.

In the early 90’s, Borland’s Turbo Pascal was the leading Pascal compiler. It had a very large programmer community and when, in 1994 it became clear that it would be discontued many programmers were left in the cold. Some projects were started by such users to write a new compiler and in 1993, Free Pascal, initially known as FPK-Pascal, started out as one of those projects. All early Free Pascal developers were highly satisfied Turbo Pascal users and the goal of the compiler was clear, to build a modern, 32-bit replacement for Turbo Pascal.

The project quickly attracted people from the large Turbo Pascal community and got a large user base. In July 2000, the crown on the work, Free Pascal 1.0 was released. Free Pascal did what it promised, it was 32-bit, modern Pascal compiler that was extremely compatible to Turbo Pascal. It also turned out to be a very reliable compiler and could be trusted to write large programs in. In education, tradionally a Pascal stronghold, Turbo Pascal got replaced by Free Pascal and many code gurus started to design software in FPC. The downloads skyrocketed and as of now, they still do, with the three servers forming ftp.freepascal.org throw out 0,5 terabyte a month of traffic.

However, the world had changed. Turbo Pascal had disappeared from people’s mind, and its successor, Delphi, mostly used to develop business applications, was a now dominant factor in the Pascal world. So, demand changed from a modern Turbo Pascal successor to an open source Delphi alternative. However, this posed challenges the way the compiler had been written. In fact, the compiler had been written by a group of skilled young and crazy programmers, who were at the time relatively unexperienced with compiler design.

Another more important flaw became apparent. Version 0.99 of the compiler had been ported to the Commodore Amiga. This was done by writing a second code generator, one for i386 and one for the M68000. This approach turned out to be totally unmaintainable, since an awfull lot of changes required both code generators to be updated, while correctness of changes could not be guaranteed by people developing on PC’s. On the code generation front, the compiler design was by far not up to the task to generate efficient assembler code.

In other words, a redesign became necessary. Most urgent was a new code generator, but for a solid compiler architecture the parser tree and register allocator had to be rewritten as well.

4. Architecture


Architecture of the Free Pascal 1 compiler

The new compiler uses an architecture we have not yet seen anywhere else and we’re quite proud of it. First we’ll discuss the architecture of the 1.0 compiler. The 1.0 compiler was basically pretty straightforward. A scanner did convert a program into tokens which where then parsed into a parser tree. Then the compiler did a first pass to evualate constant expressions and collect information for code generation.

Then the CPU-dependent second pass generated the actual code, after which a CPU-dependent optimizer could improve the generated code.

Free Pascal 2 does three passes, constant evalution, collection of information and highlevel optimization, and code generation. The code generator has been split in a front-end and back-end. Tree, constant pass, information collection and highlevel code generation are built into an object oriented class structure. The CPU-dependent back-end is also implemented as class that needs to be overridden for each CPU. A CPU-dependent parameter manager helps the code generator to pass parameters to procedures without knowing anything about the CPU and calling convention that is being used.

The advantage of this architecture is that few CPU-dependent code has to be written to port the compiler, one can suffice by implementing the code generator back-end, the parameter manager and supplying the right information to the other parts of the compiler.


Architecture of the Free Pascal 2 compiler

However, any part of the code generation process can be overridden to take advantage of CPU-specific tricks. For example, the x86 archtecture has powerful addressing modes which are very useful for index arrays. To take advantage of this, the x86 part of the code generator overrides the code generation for array indexing and generates x86 specific instructions.

We believe that by using this architecture we are better able to take advantage of weird CPU features than by using a more traditional intermediate code approach. Current processors that benefit a lot from this are the ARM and x86.

Lastly, the new architecture uses a new register allocator. By doing register allocation after code generation, the compiler is able to make much smarter register assignment decisions. Previously, while allocating registers the compiler did not know what code did follow in the future and therefore usually put data in uncomfortable places so extra code had to be generated to move things around.

On the library front, a big overhaul was done on the Unix front, allowing code sharing between Unixes, so many of them are now supported. The already very fast heap manager got an overhaul to behave better in a lot of less common situations and to support deallocation of memory back to operating system (important for daemons).

5. New features


Lazarus is a Delphi-alike RAD tool that allows quick development of GUI applications

A new compiler not only needs a new architecture, it also needs new features. What’s new? Not surprisingly, many ports have been made. Free Pascal 2 is available for the i386, x86_64, PowerPC/32, Sparc and ARM processors. Operating systems did already include Dos, Linux, Windows, OS/2, and FreeBSD. Now, some of these are available on multiple processors and big newcomers are Novell Netware, Mac OS classic and Mac OS X.

Second, the compiler generates better code, does proper inlining, and can use multiple calling conventions. It has some language additions, like 64 bit constant parsing, interfaces, dynamic arrays and more. There is a better heap manager and both the compiler and runtime library now can do multi-threading. Internationalization support helps you to translate your application and widestrings will make it possible to also target those languages with weird character sets. Delphi compatibility has been improved a lot and many quirks are emulated in the special Delphi mode. Apple programmers will be pleased that a Mac Pascal mode implements many language features from Think Pascal and Metrowerks Pascal.

A good compiler is useless without good libraries, and loads of new libraries are available. Describing all improvements here would need a long article on its own, though.

6. Future

What does the future hold? Now the major architectural hurdles have been taken, we expect to make rapid progress on the code optimization front, which still is a bit behind compared with other open source compilers. Big improvements are expected by implementation of a static single assignment variables in the parser tree, which will greatly save on the amount of data moved between temporary locations. We also expect the amount of platforms the compiler is available on to increase even further. Big advances are expected in development tools based on Free Pascal which will become powerful RAD tools.

7. Closing words

Free Pascal 2.0 is the crown on five years of intense compiler development. Within those five years, its code base has evolved from a simple compiler of which you can find many on the internet to an advanced masterpiece and should be considered one of the major open source compilers. It does not implement a new sexy language of which many have appeared and disappeared, but instead builds on a foundation that has proven itself to be reliable, useful and competitive for over 30 years. And if you want to write software that matters, Free Pascal is among the better tools available for the job.


If you would like to see your thoughts or experiences with technology published, please consider writing an article for OSNews.

75 Comments

  1. 2005-05-16 7:09 pm
  2. 2005-05-16 7:21 pm
  3. 2005-05-16 7:44 pm
  4. 2005-05-16 7:47 pm
  5. 2005-05-16 7:53 pm
  6. 2005-05-16 7:57 pm
  7. 2005-05-16 8:02 pm
  8. 2005-05-16 8:02 pm
  9. 2005-05-16 8:11 pm
  10. 2005-05-16 8:14 pm
  11. 2005-05-16 8:56 pm
  12. 2005-05-16 9:02 pm
  13. 2005-05-16 9:04 pm
  14. 2005-05-16 9:21 pm
  15. 2005-05-16 9:23 pm
  16. 2005-05-16 9:36 pm
  17. 2005-05-16 10:05 pm
  18. 2005-05-16 10:06 pm
  19. 2005-05-16 10:08 pm
  20. 2005-05-16 10:11 pm
  21. 2005-05-16 10:13 pm
  22. 2005-05-16 10:14 pm
  23. 2005-05-16 10:37 pm
  24. 2005-05-16 11:16 pm
  25. 2005-05-16 11:22 pm
  26. 2005-05-16 11:30 pm
  27. 2005-05-16 11:53 pm
  28. 2005-05-17 12:30 am
  29. 2005-05-17 12:46 am
  30. 2005-05-17 2:30 am
  31. 2005-05-17 4:25 am
  32. 2005-05-17 6:24 am
  33. 2005-05-17 6:49 am
  34. 2005-05-17 7:34 am
  35. 2005-05-17 7:38 am
  36. 2005-05-17 7:42 am
  37. 2005-05-17 7:56 am
  38. 2005-05-17 8:04 am
  39. 2005-05-17 8:14 am
  40. 2005-05-17 8:18 am
  41. 2005-05-17 8:20 am
  42. 2005-05-17 10:17 am
  43. 2005-05-17 10:22 am
  44. 2005-05-17 10:44 am
  45. 2005-05-17 10:53 am
  46. 2005-05-17 11:09 am
  47. 2005-05-17 11:16 am
  48. 2005-05-17 1:15 pm
  49. 2005-05-17 1:19 pm
  50. 2005-05-17 2:10 pm
  51. 2005-05-17 2:11 pm
  52. 2005-05-17 3:06 pm
  53. 2005-05-17 4:30 pm
  54. 2005-05-17 5:52 pm
  55. 2005-05-17 5:58 pm
  56. 2005-05-17 6:02 pm
  57. 2005-05-17 6:07 pm
  58. 2005-05-17 6:29 pm
  59. 2005-05-17 6:59 pm
  60. 2005-05-17 7:00 pm
  61. 2005-05-18 6:16 am
  62. 2005-05-18 6:33 am
  63. 2005-05-18 8:10 am
  64. 2005-05-18 8:52 am
  65. 2005-05-18 1:43 pm
  66. 2005-05-18 5:49 pm
  67. 2005-05-19 1:50 am
  68. 2005-05-19 2:40 am
  69. 2005-05-19 5:09 am
  70. 2005-05-19 6:27 am
  71. 2005-05-19 7:55 am
  72. 2005-05-19 10:26 am
  73. 2005-05-20 1:15 pm
  74. 2005-05-22 10:28 pm
  75. 2005-05-26 5:11 am