Changes between Version 5 and Version 6 of Commentary/Compiler/API

Sep 11, 2006 12:05:20 PM (11 years ago)



  • Commentary/Compiler/API

    v5 v6  
    2525Note that since GHC is packaged as a single binary, all of these front-ends are present, and there is a single command-line interface implemented in [[GhcFile(compiler/main/Main.hs)]].
     28== The GHC API ==
     30The GHC API is rather stateful; the state of an interaction with GHC is stored in an abstract value of type {{{GHC.Session}}}.  The only fundamental reason for this choice is that the {{{Session}}} models the state of the RTS's linker, which must be single-threaded.
     32Although the GHC API apparently supports multiple clients, because each can be interacting with a different {{{Session}}}, in fact it only supports one client that is actually executing code, because the [wiki:Commentary/Rts/Interpreter#Linker RTS linker] has a single global symbol table.
     34This part of the commentary is not a tutorial on ''using'' the GHC API: for that, see [ Using GHC as a Library].  Here we are going to talk about the implementation.
     36A typical interaction with the GHC API goes something like the following:
     38 * Create a new session: {{{newSession}}}
     39 * Add some ''targets'': {{{setTargets}}}, {{{addTarget}}}, {{{guessTarget}}}
     40 * Perform [[ref(Dependency Analysis)]]: {{{depanal}}}
     41 * Load (compile) the source files: {{{load}}}
     43=== Targets ===
     45The targets specify the source files or modules at the top of the dependency tree.  For a Haskell program there is often just a single target {{{Main.hs}}}, but for a library the targets would consist of every visible module in the library.
     47The {{{Target}}} type is defined in [[GhcFile(compiler/main/HscTypes.lhs)]].  Note that a {{{Target}}} includes not just the file or module name, but also optionally the complete source text of the module as a {{{StringBuffer}}}: this is to support an interactive development environment where the source file is being edited, and the in-memory copy of the source file is to be used in preference to the version on disk.
     49=== Dependency Analysis ===
     51The dependency analysis phase determines all the Haskell source files that are to be compiled or loaded in the current session, by traversing the transitive dependencies of the targets.  This process is called the ''downsweep'' because we are traversing the dependency tree downwards from the targets.  (The ''upsweep'', where we compile all these files happens in the opposite direction of course).
     53The {{{downsweep}}} function takes the targets and returns a list of {{{ModSummary}}} consisting of all the modules to be compiled/loaded.
     55=== The !ModSummary type ===
     57A {{{ModSummary}}} (defined in [[GhcFile(compiler/main/HscTypes.h)]]) contains various information about a module:
     59 * Its {{{Module}}}, which includes the package that it belongs to
     60 * Its {{{ModLocation}}}, which lists the pathnames of all the files associated with the module
     61 * The modules that it imports
     62 * The time it was last modified
     63 * ... some other things
     65We collect {{{ModSumary}}} information for all the modules we are interested in during the ''downsweep'', below.  Extracting the information about the module name and the imports from a source file is the job of [[GhcFile(compiler/main/HeaderInfo.hs)]] which partially parses the source file.
     67Converting a given module name into a {{{ModSummary}}} is done by {{{summariseModule}}} in [[GhcFile(compiler/main/GHC.hs)]].  Similarly, if we have a filename rather than a module name, we generate a {{{ModSummary}}} using {{{summariseFile}}}.
     69=== Loading (compiling) the Modules ===
     71When the dependency analysis is complete, we can load these modules by calling {{{GHC.load}}}.  The same interface is used regardless of whether we are loading modules into GHCi with the {{{:load}}} command, or compiling a program with {{{ghc --make}}}: we always end up calling {{{GHC.load}}}.
     73The process in principle is fairly simple:
     75 * Visit each module in the dependency tree from the bottom up, invoking [wiki:Commentary/Compiler/HscMain HscMain]
     76   to compile it (the ''upsweep'').
     77 * Finally, link all the code together.  In GHCi this involves loading all the object code into memory and linking it
     78   with the [wiki:Commentary/Rts/Interpreter#Linker RTS linker], and then linking all the byte-code together.  In
     79   {{{--make}}} mode this involves invoking the external linker to link the object code into a binary.
     81The process is made more tricky in practice for two reasons:
     83 * We might not need to compile certain modules, if none of their dependencies have changed.  GHC's
     84   [wiki:Commentary/Compiler/RecompilationChecker recompilation checker] determines whether a module really needs
     85   to be compiled or not.
     86 * In GHCi, we might just be reloading the program after making some changes, so we don't even want to re-link
     87   modules for which no dependencies have changed.
     89=== Stable Modules ===