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