GHC itself is written in Haskell
, but the runtime system for Haskell, essential to run programs, is written in C
and C--
.
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
.
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
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