[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / compiler / codeGen / CodeGen.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
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 import CLabel           ( CLabel, mkSRTLabel, mkClosureLabel )
26
27 import AbsCUtils        ( mkAbstractCs, mkAbsCStmts )
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, CostCentreStack )
37 import CStrings         ( modnameToC )
38 import FiniteMap        ( FiniteMap )
39 import Id               ( Id, idName )
40 import Name             ( Module )
41 import PrimRep          ( getPrimRepSize, PrimRep(..) )
42 import Type             ( Type )
43 import TyCon            ( TyCon )
44 import BasicTypes       ( TopLevelFlag(..) )
45 import Util
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             [CostCentreStack])  -- pre-defined "singleton" cost centre stacks
53         -> [Module]             -- import names
54         -> [TyCon]              -- tycons with data constructors to convert
55         -> FiniteMap TyCon [(Bool, [Maybe Type])]
56                                 -- tycon specialisation info
57         -> [(StgBinding,[Id])]  -- bindings to convert, with SRTs
58         -> AbstractC            -- output
59
60 codeGen mod_name (local_CCs, extern_CCs, singleton_CCSs) 
61         import_names gen_tycons tycon_specs stg_pgm
62   = let
63         maybe_split       = if opt_EnsureSplittableC 
64                                 then CSplitMarker 
65                                 else AbsCNop
66         cinfo             = MkCompInfo mod_name
67     in
68     let 
69         module_code = mkAbstractCs [
70             genStaticConBits cinfo gen_tycons tycon_specs,
71             initC cinfo (cgTopBindings maybe_split stg_pgm) ]
72
73          -- Cost-centre profiling:
74          -- Besides the usual stuff, we must produce:
75          --
76          -- * Declarations for the cost-centres defined in this module;
77          -- * Code to participate in "registering" all the cost-centres
78          --   in the program (done at startup time when the pgm is run).
79          --
80          -- (The local cost-centres involved in this are passed
81          -- into the code-generator, as are the imported-modules' names.)
82          --
83          --
84         cost_centre_stuff 
85                 | not opt_SccProfilingOn = AbsCNop
86                 | otherwise = mkAbstractCs (
87                     map (CCostCentreDecl True)   local_CCs ++
88                     map (CCostCentreDecl False)  extern_CCs ++
89                     map CCostCentreStackDecl     singleton_CCSs ++
90                     mkCcRegister local_CCs singleton_CCSs import_names
91                    )
92    in
93    mkAbstractCs [ cost_centre_stuff, module_code ]
94
95   where
96     -----------------
97     grp_name  = case opt_SccGroup of
98                   Just xx -> _PK_ xx
99                   Nothing -> mod_name   -- default: module name
100
101     -----------------
102     mkCcRegister ccs cc_stacks import_names
103       = let
104             register_ccs     = mkAbstractCs (map mk_register ccs)
105             register_imports
106               = foldr (mkAbsCStmts . mk_import_register) AbsCNop import_names
107             register_cc_stacks = mkAbstractCs (map mk_register_ccs cc_stacks)
108         in
109         [
110             CCallProfCCMacro SLIT("START_REGISTER_CCS") 
111                [ CLitLit (modnameToC (SLIT("_reg") _APPEND_ mod_name)) AddrRep],
112             register_ccs,
113             register_cc_stacks,
114             register_imports,
115             CCallProfCCMacro SLIT("END_REGISTER_CCS") []
116         ]
117       where
118         mk_register cc
119           = CCallProfCCMacro SLIT("REGISTER_CC") [mkCCostCentre cc]
120
121         mk_register_ccs ccs
122           = CCallProfCCMacro SLIT("REGISTER_CCS") [mkCCostCentreStack ccs]
123
124         mk_import_register import_name
125           = CCallProfCCMacro SLIT("REGISTER_IMPORT") 
126               [CLitLit (modnameToC (SLIT("_reg") _APPEND_ import_name)) AddrRep]
127 \end{code}
128
129 %************************************************************************
130 %*                                                                      *
131 \subsection[codegen-top-bindings]{Converting top-level STG bindings}
132 %*                                                                      *
133 %************************************************************************
134
135 @cgTopBindings@ is only used for top-level bindings, since they need
136 to be allocated statically (not in the heap) and need to be labelled.
137 No unboxed bindings can happen at top level.
138
139 In the code below, the static bindings are accumulated in the
140 @MkCgState@, and transferred into the ``statics'' slot by @forkStatics@.
141 This is so that we can write the top level processing in a compositional
142 style, with the increasing static environment being plumbed as a state
143 variable.
144
145 \begin{code}
146 cgTopBindings :: AbstractC -> [(StgBinding,[Id])] -> Code
147
148 cgTopBindings split bindings = mapCs (cgTopBinding split) bindings
149
150 cgTopBinding :: AbstractC -> (StgBinding,[Id]) -> Code
151
152 cgTopBinding split ((StgNonRec name rhs), srt)
153   = absC split                  `thenC`
154     absC (mkSRT srt_label srt)  `thenC`
155     setSRTLabel srt_label (
156     cgTopRhs name rhs           `thenFC` \ (name, info) ->
157     addBindC name info
158     )
159   where
160     srt_label = mkSRTLabel (idName name)
161
162 cgTopBinding split ((StgRec pairs@((name,rhs):_)), srt)
163   = absC split                  `thenC`
164     absC (mkSRT srt_label srt)  `thenC`
165     setSRTLabel srt_label (
166     fixC (\ new_binds -> addBindsC new_binds    `thenC`
167                          mapFCs ( \ (b,e) -> cgTopRhs b e ) pairs
168     )                     `thenFC` \ new_binds ->
169     addBindsC new_binds
170     )
171   where
172     srt_label = mkSRTLabel (idName name)
173
174 mkSRT :: CLabel -> [Id] -> AbstractC
175 mkSRT lbl []  = AbsCNop
176 mkSRT lbl ids = CSRT lbl (map (mkClosureLabel . idName) ids)
177
178 -- Urgh!  I tried moving the forkStatics call from the rhss of cgTopRhs
179 -- to enclose the listFCs in cgTopBinding, but that tickled the
180 -- statics "error" call in initC.  I DON'T UNDERSTAND WHY!
181
182 cgTopRhs :: Id -> StgRhs -> FCode (Id, CgIdInfo)
183         -- the Id is passed along for setting up a binding...
184
185 cgTopRhs bndr (StgRhsCon cc con args)
186   = forkStatics (cgTopRhsCon bndr con args (all zero_size args))
187   where
188     zero_size atom = getPrimRepSize (getArgPrimRep atom) == 0
189
190 cgTopRhs bndr (StgRhsClosure cc bi srt fvs upd_flag args body)
191   = ASSERT(null fvs) -- There should be no free variables
192     forkStatics (cgTopRhsClosure bndr cc bi srt args body lf_info)
193   where
194     lf_info = mkClosureLFInfo bndr TopLevel [{-no fvs-}] upd_flag args
195 \end{code}