Skip to content

Instantly share code, notes, and snippets.

@monadplus
Last active September 30, 2024 09:00
Show Gist options
  • Save monadplus/fe25a08c13cfd1f3ebed73a934334c79 to your computer and use it in GitHub Desktop.
Save monadplus/fe25a08c13cfd1f3ebed73a934334c79 to your computer and use it in GitHub Desktop.
GHC Architecture: overview

GHC Architecture

GHC itself is written in Haskell, but the runtime system for Haskell, essential to run programs, is written in C and C--.

Front end

GHC's frontend - incorporating the lexer, parser and typechecker—is designed to preserve as much information about the source language as possible until after type inference is complete, toward the goal of providing clear error messages to users.

After type checking, the Haskell code is desugared into a typed intermediate language known as Core (based on System F, extended with let and case expressions).

Recently, Core was extended to support generalized algebraic datatypes in its type system, and is now based on an extension to System F known as System FC.

Middle end

In the tradition of type-directed compilation, GHC's simplifier, or "middle end", where most of the optimizations implemented in GHC are performed, is structured as a series of source-to-source transformations on Core code.

The analyses and transformations performed in this compiler stage include:

  • demand analysis
  • user-defined rewrite rules
  • unfolding (inlining)
  • let-floating (which functions args can be unboxed)
  • constructed product result analysis
  • specialization of overloaded functions
  • local transformation: constant folding and beta reduction

Back end

The back end of the compiler transforms Core code into an internal representation of C--, via an intermediate language STG (short for "Spineless Tagless G-machine").

The C-- code can then take one of three routes:

  • printed as C code for compilation with GCC
  • converted directly into native machine code (the traditional "code generation" phase)
  • converted to LLVM virtual machine code for compilation with LLVM. In all three cases, the resultant native code is finally linked against the GHC runtime system to produce an executable.

Source: https://en.wikipedia.org/wiki/Glasgow_Haskell_Compiler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment