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.
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.
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.
- "FreePascal, Page 1/2"
- "FreePascal, Page 2/2"