[project @ 1996-04-05 08:26:04 by partain]
[ghc-hetmet.git] / ghc / compiler / codeGen / CodeGen.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[CodeGen]{@CodeGen@: main module of the code generator}
5
6 This module says how things get going at the top level.
7
8 @codeGen@ is the interface to the outside world.  The \tr{cgTop*}
9 functions drive the mangling of top-level bindings.
10
11 %************************************************************************
12 %*                                                                      *
13 \subsection[codeGen-outside-interface]{The code generator's offering to the world}
14 %*                                                                      *
15 %************************************************************************
16
17 \begin{code}
18 #include "HsVersions.h"
19
20 module CodeGen ( codeGen ) where
21
22 import Ubiq{-uitous-}
23
24 import StgSyn
25 import CgMonad
26 import AbsCSyn
27
28 import AbsCUtils        ( mkAbstractCs, mkAbsCStmts )
29 import Bag              ( foldBag )
30 import CgClosure        ( cgTopRhsClosure )
31 import CgCon            ( cgTopRhsCon )
32 import CgConTbls        ( genStaticConBits )
33 import ClosureInfo      ( mkClosureLFInfo )
34 import CmdLineOpts      ( opt_SccProfilingOn, opt_CompilingPrelude,
35                           opt_EnsureSplittableC, opt_SccGroup
36                         )
37 import CStrings         ( modnameToC )
38 import Maybes           ( maybeToBool )
39 import PrimRep          ( getPrimRepSize, PrimRep(..) )
40 import Util             ( panic, assertPanic )
41 \end{code}
42
43 \begin{code}
44 codeGen :: FAST_STRING          -- module name
45         -> ([CostCentre],       -- local cost-centres needing declaring/registering
46             [CostCentre])       -- "extern" cost-centres needing declaring
47         -> Bag FAST_STRING      -- import names
48         -> [TyCon]              -- tycons with data constructors to convert
49         -> FiniteMap TyCon [(Bool, [Maybe Type])]
50                                 -- tycon specialisation info
51         -> [StgBinding] -- bindings to convert
52         -> AbstractC            -- output
53
54 codeGen mod_name (local_CCs, extern_CCs) import_names gen_tycons tycon_specs stg_pgm
55   = let
56         doing_profiling   = opt_SccProfilingOn
57         compiling_prelude = opt_CompilingPrelude
58         maybe_split       = if maybeToBool (opt_EnsureSplittableC)
59                             then CSplitMarker
60                             else AbsCNop
61
62         cinfo = MkCompInfo mod_name
63     in
64     if not doing_profiling then
65         mkAbstractCs [
66             genStaticConBits cinfo gen_tycons tycon_specs,
67             initC cinfo (cgTopBindings maybe_split stg_pgm) ]
68
69     else -- yes, cost-centre profiling:
70          -- Besides the usual stuff, we must produce:
71          --
72          -- * Declarations for the cost-centres defined in this module;
73          -- * Code to participate in "registering" all the cost-centres
74          --   in the program (done at startup time when the pgm is run).
75          --
76          -- (The local cost-centres involved in this are passed
77          -- into the code-generator, as are the imported-modules' names.)
78          --
79          -- Note: we don't register/etc if compiling Prelude bits.
80
81         mkAbstractCs [
82                 if compiling_prelude
83                 then AbsCNop
84                 else mkAbstractCs [mkAbstractCs (map (CCostCentreDecl True)  local_CCs),
85                                    mkAbstractCs (map (CCostCentreDecl False) extern_CCs),
86                                    mkCcRegister local_CCs import_names],
87
88                 genStaticConBits cinfo gen_tycons tycon_specs,
89                 initC cinfo (cgTopBindings maybe_split stg_pgm) ]
90   where
91     -----------------
92     grp_name  = case opt_SccGroup of
93                   Just xx -> xx
94                   Nothing -> mod_name   -- default: module name
95
96     -----------------
97     mkCcRegister ccs import_names
98       = let
99             register_ccs     = mkAbstractCs (map mk_register ccs)
100             register_imports
101               = foldBag mkAbsCStmts mk_import_register AbsCNop import_names
102         in
103         mkAbstractCs [
104             CCallProfCCMacro SLIT("START_REGISTER_CCS") [CLitLit (modnameToC (SLIT("_reg") _APPEND_ mod_name)) AddrRep],
105             register_ccs,
106             register_imports,
107             CCallProfCCMacro SLIT("END_REGISTER_CCS") []
108         ]
109       where
110         mk_register cc
111           = CCallProfCCMacro SLIT("REGISTER_CC") [mkCCostCentre cc]
112
113         mk_import_register import_name
114           = CCallProfCCMacro SLIT("REGISTER_IMPORT") [CLitLit (modnameToC (SLIT("_reg") _APPEND_ import_name)) AddrRep]
115 \end{code}
116
117 %************************************************************************
118 %*                                                                      *
119 \subsection[codegen-top-bindings]{Converting top-level STG bindings}
120 %*                                                                      *
121 %************************************************************************
122
123 @cgTopBindings@ is only used for top-level bindings, since they need
124 to be allocated statically (not in the heap) and need to be labelled.
125 No unboxed bindings can happen at top level.
126
127 In the code below, the static bindings are accumulated in the
128 @MkCgState@, and transferred into the ``statics'' slot by @forkStatics@.
129 This is so that we can write the top level processing in a compositional
130 style, with the increasing static environment being plumbed as a state
131 variable.
132
133 \begin{code}
134 cgTopBindings :: AbstractC -> [StgBinding] -> Code
135
136 cgTopBindings split bindings = mapCs (cgTopBinding split) bindings
137
138 cgTopBinding :: AbstractC -> StgBinding -> Code
139
140 cgTopBinding split (StgNonRec name rhs)
141   = absC split          `thenC`
142     cgTopRhs name rhs   `thenFC` \ (name, info) ->
143     addBindC name info
144
145 cgTopBinding split (StgRec pairs)
146   = absC split          `thenC`
147     fixC (\ new_binds -> addBindsC new_binds    `thenC`
148                          mapFCs ( \ (b,e) -> cgTopRhs b e ) pairs
149     )                   `thenFC` \ new_binds ->
150     addBindsC new_binds
151
152 -- Urgh!  I tried moving the forkStatics call from the rhss of cgTopRhs
153 -- to enclose the listFCs in cgTopBinding, but that tickled the
154 -- statics "error" call in initC.  I DON'T UNDERSTAND WHY!
155
156 cgTopRhs :: Id -> StgRhs -> FCode (Id, CgIdInfo)
157         -- the Id is passed along for setting up a binding...
158
159 cgTopRhs name (StgRhsCon cc con args)
160   = forkStatics (cgTopRhsCon name con args (all zero_size args))
161   where
162     zero_size atom = getPrimRepSize (getArgPrimRep atom) == 0
163
164 cgTopRhs name (StgRhsClosure cc bi fvs upd_flag args body)
165   = ASSERT(null fvs) -- There should be no free variables
166     forkStatics (cgTopRhsClosure name cc bi args body lf_info)
167   where
168     lf_info = mkClosureLFInfo True{-top level-} [{-no fvs-}] upd_flag args body
169 \end{code}