Hasty Briefsbeta

Compiling a Functional Language to LLVM (2023)

3 days ago
  • #functional-programming
  • #LLVM
  • #compiler
  • The author discusses compiling a small functional language named 'mimsa' to LLVM.
  • Resources like Joseph Morag’s Micro-C series and Mapping High Level Concepts to LLVM IR are mentioned but lack depth on functional programming paradigms.
  • The initial language is a basic calculator supporting operations like addition, subtraction, and multiplication.
  • Abstract Syntax Trees (ASTs) are used to represent programs internally, with annotations for source code locations.
  • The 'megaparsec' library is chosen for parsing due to its speed, good error handling, and whitespace management features.
  • A simple interpreter is implemented to evaluate expressions before moving to LLVM compilation.
  • LLVM is introduced as a tool for compiling high-level languages to various architectures without deep knowledge of each.
  • A minimal C runtime is created to support the language, featuring a 'printint' function.
  • The 'llvm-hs-pure' and 'llvm-hs-pretty' libraries are used to generate and pretty-print LLVM IR from Haskell.
  • The process of converting expressions to LLVM IR is demonstrated, including handling arithmetic operations.
  • The generated LLVM IR for a simple expression (6 * 8 - 3) is shown and explained.
  • The compiler outputs LLVM IR, which is then compiled to an executable using 'clang'.
  • Future plans include adding equality operators and control flow to the language.