A standard ML compiler
Standard ML is a major revision of earlier dialects of the functional language ML. We describe the first compiler written for Standard ML in Standard ML. The compiler incorporates a number of novel features and techniques, and is probably the largest system written to date in Standard ML.
Great attention was paid to modularity in the construction of the compiler, leading to a successful large-scale test of the modular capabilities of Standard ML. The front end is useful for purposes other than compilation, and the back end is easily retargetable (we have code generators for the VAX and MC68020). The module facilities of Standard ML were taken into account early in the design of the compiler, and they particularly influenced the environment management component of the front end. For example, the symbol table structure is designed for fast access to opened structures.
The front end of the compiler is a single phase that integrates parsing, environment management, and type checking. The middle end uses a sophisticated decision tree scheme to produce efficient pattern matching code for functions and case expressions. The abstract syntax produced by the front end is translated into a simple lambda-calculus-based intermediate representation that lends itself to easy case analysis and optimization in the code generator. Special care was taken in designing the runtime data structures for fast allocation and garbage collection.
We describe the overall organization of the compiler and present some of the data representations and algorithms used in its various phases. We conclude with some lessons learned about the ML language itself and about compilers for modern functional languages.
KeywordsAbstract Syntax Abstract Machine Runtime System Exception Handler Functional Language
- 1.P.J. Landin, “The next 700 programming languages,” Comm. ACM, vol. 9, no. 3, pp. 157–166, 1966.Google Scholar
- 2.Luca Cardelli, “Compiling a functional language,” 1984 Symp. on LISP and Functional Programming, pp. 208–217, ACM, 1984.Google Scholar
- 3.Robin Milner, “A proposal for Standard ML,” ACM Symposium on LISP and Functional Programming, pp. 184–197, ACM, 1984.Google Scholar
- 4.Robin Milner, “The Standard ML Core Language,” Polymorphism, vol. 2, no. 2, October 1985.Google Scholar
- 5.David MacQueen, “Modules for Standard ML,” Proc. 1984 ACM Conf. on LISP and Functional Programming, pp. 198–207, ACM, Austin, Texas, 1984.Google Scholar
- 6.David MacQueen, “Modules for Standard ML,” Polymorphism, vol. 2, no. 2, October 1985.Google Scholar
- 7.R. Burstall, D. MacQueen, and D. Sannella, “Hope: an Experimental Applicative Language,” Proceedings of the 1980 LISP Conference, pp. 136–143, Stanford, 1980.Google Scholar
- 8.David C. J. Matthews, “The Poly manual,” SIGPLAN Notices, September 1985.Google Scholar
- 9.David C. J. Matthews, An implementation of Standard ML in Poly, May 1986.Google Scholar
- 10.G. Cousineau, P. L. Curien, and M. Mauny, “The Categorical Abstract Machine,” in Functional Programming Languages and Computer Architecture, LNCS Vol 201, ed. J. P. Jouannaud, pp. 50–64, Springer-Verlag, 1985.Google Scholar
- 11.Robin Milner, “A Theory of Type Polymorphism in Programming,” J. CSS, vol. 17, pp. 348–375, 1978.Google Scholar
- 12.Luca Cardelli, “Basic polymorphic typechecking,” Polymorphism, vol. 2, no. 1, January 1985.Google Scholar
- 13.R. S. Boyer and J Moore, “The sharing of structure in theorem-proving programs,” in Machine Intelligence 7, ed. D. Michie, Edinburgh University Press, 1972.Google Scholar
- 14.Luis Damas, “Type Assignment in Programming Languages,” PhD Thesis, Department of Computer Science, University of Edinburgh, 1985.Google Scholar
- 15.Robert Harper, Robin Milner, and Mads Tofte, “A type discipline for program modules,” ECS-LFCS-87-28, Univ. of Edinburgh, 1987.Google Scholar
- 16.Marianne Baudinet and David MacQueen, Tree Pattern Matching for ML, 1986.Google Scholar
- 17.Guy L. Steele, “Rabbit: a compiler for Scheme,” AI-TR-474, MIT, 1978.Google Scholar
- 18.D. Kranz, R. Kelsey, J. Rees, P. Hudak, J. Philbin, and N. Adams, “ORBIT: An optimizing compiler for Scheme,” Proc. Sigplan '86 Symp. on Compiler Construction, vol. 21 (Sigplan Notices), no. 7, pp. 219–233, July 1986.Google Scholar
- 19.W. Wulf, R. K. Johnsson, C. B. Weinstock, C. B. Hobbs, and C. M. Geschke, Design of an Optimizing Compiler, Elsevier North-Holland, New York, 1975.Google Scholar
- 20.Luca Cardelli, “The functional abstract machine,” Polymorphism, vol. 1, no. 1, January 1983.Google Scholar
- 21.R. G. G. Cattell, “Formalization and automatic derivation of code generators,” Ph.D. Thesis, Carnegie-Mellon University, Pittsburgh, PA, April 1978.Google Scholar
- 22.A. V. Aho, M. Ganapathi, and S. W. K. Tjiang, Code generation using tree matching and dynamic programming, 1986.Google Scholar
- 23.D. A. Turner, “Miranda: a non-strict functional language with polymorphic types,” in Functional Programming Languages and Computer Architecture, LNCS Vol 201, ed. J. P. Jouannaud, pp. 1–16, Springer-Verlag, 1985.Google Scholar
- 24.Henry Lieberman and Carl Hewitt, “A real-time garbage collector based on the lifetimes of objects,” Communications of the ACM, vol. 23, no. 6, pp. 419–429, ACM, 1983.Google Scholar
- 25.A. W. Appel, “Garbage collection can be faster than stack allocation,” Information Processing Letters, vol. (to appear), 1987.Google Scholar
- 26.David A. Moon, “Garbage collection in a large LISP system,” ACM Symposium on LISP and Functional Programming, pp. 235–246, ACM, 1984.Google Scholar
- 27.David Ungar, “Generation scavenging: a non-disruptive high performance storage reclamation algorithm,” SIGPLAN Notices (Proc. ACM SIGSOFT/SIGPLAN Software Eng. Symp. on Practical Software Development Environments), vol. 19, no. 5, pp. 157–167, ACM, 1984.Google Scholar