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