12 - Compilation
Published online by Cambridge University Press: 05 June 2012
Summary
In this chapter, we describe a technique to compile a functional language like Caml into machine code. This compilation technique is strongly connected to the evaluation technique developed in the preceding chapter. In particular, the idea of an environment again plays a central role here, and we keep the idea of closure to represent functional values. However, you will see that these two ideas correspond to slightly different objects in a compiler.
To keep the compiler at a sufficiently conceptual level of description, we have opted to use machine code made up of instructions specially adapted to our compilation scheme. These instructions produce operations more complicated than the “real” instructions of an assembly language. Even so, it is clear that these instructions can be expanded into a list of machine instructions producing the same effect. The compilation scheme we present thus leads directly to a real compiler.
To execute the code produced by our compiler, we use a stack to store the intermediate values needed in computations; we also store the addresses of subprograms there, a conventional and time-honored practice. What is special, though, about compiling functional languages are the instructions to allocate memory needed to build structured values, environments, and closures. To explain these instructions and their role in the compilation of functional languages, we must first explain how we use computer memory to represent complex objects.
- Type
- Chapter
- Information
- The Functional Approach to Programming , pp. 377 - 400Publisher: Cambridge University PressPrint publication year: 1998