ABSTRACT
A compiler is a program that translates source code written in a particular language into another language. Internally, the whole process is typically split into multiple stages that handle one particular aspect of this translation. One of these consists of translating the high-level representation of the program, typically an abstract syntax tree, into a simpler form that is suitable for analysis, optimizations, and code generation. This tutorial paper focuses on this process, and uses LLVM to compile programs into optimized machine code. LLVM is a language-agnostic compiler toolchain that handles program optimization and code generation. It is based on its own internal representation, called LLVM IR, which is then transformed into machine code.
We give a brief introduction to LLVM IR, and describe a few patterns to translate high-level language constructs expressed as abstract syntax trees. We implement these patterns in a compiler for a toy programming language, named Cocodol, which supports dynamic typing, unbounded loops, and higher-order functions.
- Ron Cytron, Jeanne Ferrante, Barry K. Rosen, Mark N. Wegman, and F. Kenneth Zadeck. 1991. Efficiently Computing Static Single Assignment Form and the Control Dependence Graph. ACM Transactions on Programming Languages and Systems 13, 4 (1991), 451–490. https://doi.org/10.1145/115372.115320Google ScholarDigital Library
- Changyi Gu. 2016. Power On and Bootloader. Apress, Berkeley, CA, 5–25. https://doi.org/10.1007/978-1-4842-1919-5_2Google Scholar
- Chris Lattner and Vikram S. Adve. 2004. LLVM: A Compilation Framework for Lifelong Program Analysis & Transformation. In 2nd IEEE / ACM International Symposium on Code Generation and Optimization (CGO 2004), 20-24 March 2004, San Jose, CA, USA. IEEE Computer Society, USA, 75–88. https://doi.org/10.1109/CGO.2004.1281665Google Scholar
- LLVM Project. 2021. Kaleidoscope: Implementing a Language with LLVM. https://llvm.org/docs/tutorial/. [Online; accessed 07-April-2021].Google Scholar
- Michael Rodler. 2018. Mapping High Level Constructs to LLVM IR. https://mapping-high-level-constructs-to-llvm-ir.readthedocs.io. [Online; accessed 07-April-2021].Google Scholar
From ASTs to Machine Code with LLVM
Recommendations
Type-safe runtime code generation: accelerate to LLVM
Haskell '15: Proceedings of the 2015 ACM SIGPLAN Symposium on HaskellEmbedded languages are often compiled at application runtime; thus, embedded compile-time errors become application runtime errors. We argue that advanced type system features, such as GADTs and type families, play a crucial role in minimising such ...
An llVM backend for GHC
Haskell '10: Proceedings of the third ACM Haskell symposium on HaskellIn the presence of ever-changing computer architectures, high-quality optimising compiler backends are moving targets that require specialist knowledge and sophisticated algorithms. In this paper, we explore a new backend for the Glasgow Haskell ...
Bringing low-level languages to the JVM: efficient execution of LLVM IR on Truffle
VMIL 2016: Proceedings of the 8th International Workshop on Virtual Machines and Intermediate LanguagesAlthough the Java platform has been used as a multi-language platform, most of the low-level languages (such as C, Fortran, and C++) cannot be executed efficiently on the JVM. We propose Sulong, a system that can execute LLVM-based languages on the ...
Comments