Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
GHC
GHC
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4,274
    • Issues 4,274
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
    • Iterations
  • Merge Requests 412
    • Merge Requests 412
  • Requirements
    • Requirements
    • List
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
  • Security & Compliance
    • Security & Compliance
    • Dependency List
    • License Compliance
  • Operations
    • Operations
    • Incidents
    • Environments
  • Analytics
    • Analytics
    • CI / CD
    • Code Review
    • Insights
    • Issue
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Glasgow Haskell Compiler
  • GHCGHC
  • Wiki
    • Commentary
  • compiler

Last edited by Takenobu Tani Jun 12, 2020
Page history New page

compiler

GHC Commentary: The Compiler

The compiler itself is written entirely in Haskell, and lives in the many sub-directories of the compiler directory.

  • Compiler Module Dependencies (deals with the arcane mutual recursions among GHC's many data types)

  • Coding guidelines

  • Command line arguments

  • The compilation pipeline

  • Compiling one module: GHC.Driver.Main

    • Overview gives the big picture.

    • Some details of the parser

    • Some details of the renamer

    • Some details of the typechecker

    • Some details of the simplifier

    • Some details of the code generator converts STG to Cmm

    • Backends convert Cmm to native code:

      • C code generator
      • Native code generator
      • LLVM backend
      • GHCi backend
    • A guide to the generated assembly code

  • Key data types

    • The source language: HsSyn

    • RdrNames, Modules, and OccNames

    • ModIface, ModDetails, ModGuts

    • Names

    • Entities: variables (Var), type constructors (TyCon), data constructors (DataCon), and classes (Class).

      • Tying the knot: how we build the circular data structure representing entities
    • Types:

      • Types
      • Kinds
      • Equality types and coercions
    • The core language

    • The STG language

    • The Cmm language

    • Back end types

  • Compiling more than one module at once

  • How data type declarations are compiled

  • The GHC API

  • Symbol names and the Z-encoding

  • Template Haskell

  • Wired-in and known-key things

  • Packages

  • Recompilation Avoidance

  • Incomplete information about FFI exports.

Case studies:

  • Implementation of wired-in Bool data type

Overall Structure

Here is a block diagram of its top-level structure:

GHC.Driver.Main(formerly known as HscMain) deals with compiling a single module. On top of this is built the compilation manager (in blue) that manages the compilation of multiple modules. It exports an interface called the GHC API. On top of this API are four small front ends:

  • GHCi, the interactive environment, is implemented in ghc/GHCi/UI.hs and compiler/GHC/Runtime/Eval.hs. It sits squarely on top of the GHC API.

  • --make is almost a trivial client of the GHC API, and is implemented in compiler/GHC/Driver/Make.hs.

  • -M, the Makefile dependency generator, is also a client of the GHC API and is implemented in compiler/GHC/Driver/MakeFile.hs.

  • The "one-shot" mode, where GHC compiles each file on the command line separately (eg. ghc -c Foo.hs). This mode bypasses the GHC API, and is implemented directly on top of GHC.Driver.Main, since it compiles only one file at a time. In fact, this is all that
    GHC consisted of prior to version 5.00 when GHCi and --make were introduced.

GHC is packaged as a single binary in which all of these front-ends are present, selected by the command-line flags indicated above. There is a single command-line interface implemented in ghc/Main.hs.

In addition, GHC is compiled, without its front ends, as a library which can be imported by any Haskell program; see the GHC API.

Clone repository

GHC Home
GHC User's Guide

Joining In

Newcomers info
Mailing Lists & IRC
The GHC Team

Documentation

GHC Status Info
Working conventions
Building Guide
Debugging
Commentary

Wiki

Title Index
Recent Changes