Changes between Version 1 and Version 2 of Commentary/Compiler/WiredIn


Ignore:
Timestamp:
Sep 12, 2006 1:48:38 PM (9 years ago)
Author:
simonpj
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Commentary/Compiler/WiredIn

    v1 v2  
     1 
     2
    13= Wired-in and known-key things =
    24
    3 There are two categories of entities (`Class`es, `TyCon`s, `Id`s) that GHC "knows about"; that is, information about them is baked into GHC's source code.
     5There are three categories of entities (`Class`es, `TyCon`s, `Id`s) that GHC "knows about"; that is, information about them is baked into GHC's source code.
    46
    5   * A '''Wired-in thing''' is fully known to GHC. 
     7  * [wiki:Commentary/Compiler/WiredIn#Wiredinthings Wired-in things]
     8  * [wiki:Commentary/Compiler/WiredIn#Knownkeythings Known-key things]
     9  * [wiki:Commentary/Compiler/WiredIn#OrigRdrNamethings Orig RdrName things]
    610
    7   * A '''known-key thing''' has a fixed, pre-allocated `Unique` or '''key'''.  They should really be called "known-Name" things, because the baked-in knowledge is:
    8     * Its defining `Module`
    9     * Its `OccName`
    10     * Its `Unique`
    11     Almost all known-key names are defined in [[GhcFile(compiler/prelude/PrelNames)]]; for example: {{{PrelNames.eqClassName :: Name}}}.
     11== Wired-in things ==
    1212
    13   * An '''Orig RdrName thing''' has a top-level definition of a `RdrName`, using the `Orig` constructor.  Here, the baked-in information is:
    14     * Its defining `Module`
    15     * Its `OccName`
    16     Again, almost all of these are in [[GhcFile(compiler/prelude/PrelNames)]].
    17     Example: {{{PrelNames.not_RDR :: RdrName}}}.
     13A '''Wired-in thing''' is fully known to GHC.  Most of these are `TyCon`s such as `Bool`. It is very convenient to simply be able to refer to `boolTyCon :: TyCon` without having to look it up in an environment. 
     14
     15All [wiki:Commentary/Compiler/TypeType#Classifyingtypes primitive types] are wired-in things, and have wired-in `Name`s.  The primitive types (and their `Names`) are all defined in [[GhcFile(compiler/prelude/TysPrim.lhs)]].
     16
     17The non-primitive wired-in type constructors are defined in [[GhcFile(compiler/prelude/TysWiredIn.lhs)]].  There are a handful of wired-in `Id`s in [[GhcFile(compiler/basicTypes/MkId.lhs)]]. There are no wired-in classes (they are too complicated).
     18
     19All the non-primitive wired-in things are ''also'' defined in GHC's libraries, because even though GHC knows about them we still need to generate code for them. For example, `Bool` is a wired-in type constructor, but it is still defined in `GHC.Base` because we need the info table etc for the data constructors.  Arbitrarily bad things will happen if the wired-in definition in [[GhcFile(compiler/prelude/TysWiredIn.lhs)]] differs from that in the library module.
     20
     21All wired-in things have a `WiredIn` `Name` (see [wiki:Commentary/Compiler/NameType Names]), which in turn contains the thing.  For example ([[GhcFile(compiler/prelude/TysWiredIn.lhs)]]):
     22{{{
     23boolTyCon :: TyCon
     24boolTyCon = mkAlgTyCon boolTyConName ...more details...
     25
     26boolTyConName :: Name
     27boolTyConName = mkWiredInName gHC_BASE (mkOccNameFS tcName FSLIT("Bool"))
     28                              boolTyConKey Nothing
     29                              (ATyCon boolTyCon) UserSyntax
     30}}}
     31Notice that the `TyCon` has a `Name` that contains the `TyCon`.  They each point to the other.
     32
     33== Known-key things ==
     34
     35A '''known-key thing''' has a fixed, pre-allocated `Unique` or '''key'''.  They should really be called "known-Name" things, because the baked-in knowledge is:
     36 * Its defining `Module`
     37 * Its `OccName`
     38 * Its `Unique`
     39Almost all known-key names are defined in [[GhcFile(compiler/prelude/PrelNames)]]; for example: {{{PrelNames.eqClassName :: Name}}}.
     40
     41The point about known-key things is that GHC knows its ''name'', but not its ''definition''.  The definition must still be read from an interface file as usual. The known key just allows an efficient lookup in the environment.
     42
     43== Initialisation ==
     44
     45When reading an interface file, GHC might come across "GHC.Base.Eq", which is the name of the `Eq` class.  How does it match up this occurrence in the interface file with `eqClassName` defined in `PrelNames`?  Because the global name cache maintained by the renamer is initialise with all the known-key names.  This is done by the (hard-to-find) function `HscMain.newHscEnv`:
     46{{{
     47newHscEnv :: DynFlags -> IO HscEnv
     48newHscEnv dflags
     49  = do { ...
     50         nc_var <- newIORef (initNameCache us knownKeyNames)
     51         ...
     52         return (HscEnv { ... hsc_NC = nc_var, ... }) }
     53
     54knownKeyNames :: [Name]
     55knownKeyNames = map getName wiredInThings ++ basicKnownKeyNames ++ templateHaskellNames
     56}}}
     57Notice that the initialisation embraces both the wired-in and ("basic") known-key names.
     58
     59== `Orig` `RdrName` things ==
     60
     61An '''Orig !RdrName thing''' has a top-level definition of a `RdrName`, using the `Orig` constructor.  Here, the baked-in information is:
     62  * Its defining `Module`
     63  * Its `OccName`
     64Again, almost all of these are in [[GhcFile(compiler/prelude/PrelNames)]].
     65Example: {{{PrelNames.not_RDR :: RdrName}}}.