[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / compiler / codeGen / CgConTbls.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[CgConTbls]{Info tables and update bits for constructors}
5
6 \begin{code}
7 module CgConTbls ( genStaticConBits ) where
8
9 #include "HsVersions.h"
10
11 import AbsCSyn
12 import CgMonad
13
14 import StgSyn           ( SRT(..) )
15 import AbsCUtils        ( mkAbstractCs )
16 import CgTailCall       ( performReturn, mkStaticAlgReturnCode )
17 import CLabel           ( mkConEntryLabel, mkStaticClosureLabel )
18 import ClosureInfo      ( layOutStaticClosure, layOutDynCon,
19                           mkConLFInfo, ClosureInfo
20                         )
21 import CostCentre       ( dontCareCCS )
22 import FiniteMap        ( fmToList, FiniteMap )
23 import DataCon          ( DataCon, dataConTag, dataConName, dataConRawArgTys )
24 import Const            ( Con(..) )
25 import Name             ( getOccString )
26 import PrimRep          ( getPrimRepSize, PrimRep(..) )
27 import TyCon            ( tyConDataCons, TyCon )
28 import Type             ( typePrimRep, Type )
29 import BasicTypes       ( TopLevelFlag(..) )
30 import Outputable       
31 \end{code}
32
33 For every constructor we generate the following info tables:
34         A static info table, for static instances of the constructor,
35
36         Plus:
37
38 \begin{tabular}{lll}
39 Info tbls &      Macro  &            Kind of constructor \\
40 \hline
41 info & @CONST_INFO_TABLE@&    Zero arity (no info -- compiler uses static closure)\\
42 info & @CHARLIKE_INFO_TABLE@& Charlike   (no info -- compiler indexes fixed array)\\
43 info & @INTLIKE_INFO_TABLE@&  Intlike; the one macro generates both info tbls\\
44 info & @SPEC_INFO_TABLE@&     SPECish, and bigger than or equal to @MIN_UPD_SIZE@\\
45 info & @GEN_INFO_TABLE@&      GENish (hence bigger than or equal to @MIN_UPD_SIZE@)\\
46 \end{tabular}
47
48 Possible info tables for constructor con:
49
50 \begin{description}
51 \item[@_con_info@:]
52 Used for dynamically let(rec)-bound occurrences of
53 the constructor, and for updates.  For constructors
54 which are int-like, char-like or nullary, when GC occurs,
55 the closure tries to get rid of itself.
56
57 \item[@_static_info@:]
58 Static occurrences of the constructor
59 macro: @STATIC_INFO_TABLE@.
60 \end{description}
61
62 For zero-arity constructors, \tr{con}, we also generate a static closure:
63
64 \begin{description}
65 \item[@_closure@:]
66 A single static copy of the (zero-arity) constructor itself.
67 \end{description}
68
69 For charlike and intlike closures there is a fixed array of static
70 closures predeclared.
71
72 \begin{code}
73 genStaticConBits :: CompilationInfo     -- global info about the compilation
74                  -> [TyCon]             -- tycons to generate
75                  -> FiniteMap TyCon [(Bool, [Maybe Type])]
76                                         -- tycon specialisation info
77                  -> AbstractC           -- output
78
79 genStaticConBits comp_info gen_tycons tycon_specs
80   = ASSERT( null (fmToList tycon_specs) )
81         -- We don't do specialised type constructors any more
82
83     -- for each type constructor:
84     --   grab all its data constructors;
85     --      for each one, generate an info table
86     -- for each specialised type constructor
87     --   for each specialisation of the type constructor
88     --     grab data constructors, and generate info tables
89
90     -- ToDo: for tycons and specialisations which are not
91     --       declared in this module we must ensure that the
92     --       C labels are local to this module i.e. static
93     --       since they may be duplicated in other modules
94
95     mkAbstractCs [ gen_for_tycon tc | tc <- gen_tycons ]
96   where
97     gen_for_tycon :: TyCon -> AbstractC
98     gen_for_tycon tycon
99       = mkAbstractCs (map (genConInfo comp_info tycon) (tyConDataCons tycon))
100 \end{code}
101
102 %************************************************************************
103 %*                                                                      *
104 \subsection[CgConTbls-info-tables]{Generating info tables for constructors}
105 %*                                                                      *
106 %************************************************************************
107
108 Generate the entry code, info tables, and (for niladic constructor) the
109 static closure, for a constructor.
110
111 \begin{code}
112 genConInfo :: CompilationInfo -> TyCon -> DataCon -> AbstractC
113
114 genConInfo comp_info tycon data_con
115   = mkAbstractCs [
116                   CSplitMarker,
117                   closure_code,
118                   static_code,
119                   closure_maybe]
120         -- Order of things is to reduce forward references
121   where
122     (closure_info, body_code) = mkConCodeAndInfo data_con
123
124     -- To allow the debuggers, interpreters, etc to cope with static
125     -- data structures (ie those built at compile time), we take care that
126     -- info-table contains the information we need.
127     (static_ci,_) = layOutStaticClosure con_name typePrimRep arg_tys 
128                                 (mkConLFInfo data_con)
129
130     body       = (initC comp_info (
131                       profCtrC SLIT("TICK_ENT_CON") [CReg node] `thenC`
132                       body_code))
133
134     entry_addr = CLbl entry_label CodePtrRep
135     con_descr  = getOccString data_con
136
137     -- Don't need any dynamic closure code for zero-arity constructors
138     closure_code = if zero_arity_con then 
139                         AbsCNop 
140                    else 
141                         CClosureInfoAndCode closure_info body Nothing 
142                            srt_info con_descr
143
144     srt_info = (error "genConInfo: no srt label", NoSRT)
145
146     static_code  = CClosureInfoAndCode static_ci body Nothing 
147                         srt_info con_descr
148
149     tag          = dataConTag data_con
150
151     cost_centre  = mkCCostCentreStack dontCareCCS -- not worried about static data costs
152
153     -- For zero-arity data constructors, or, more accurately,
154     --   those which only have VoidRep args (or none):
155     --  We make the closure too (not just info tbl), so that we can share
156     --  one copy throughout.
157     closure_maybe = if not zero_arity_con then
158                         AbsCNop
159                     else
160                         CStaticClosure  closure_label           -- Label for closure
161                                         static_ci               -- Info table
162                                         cost_centre
163                                         [{-No args!  A slight lie for constrs 
164                                            with VoidRep args-}]
165
166     zero_size arg_ty = getPrimRepSize (typePrimRep arg_ty) == 0
167
168     zero_arity_con   = all zero_size arg_tys
169
170     arg_tys         = dataConRawArgTys     data_con
171     entry_label     = mkConEntryLabel      con_name
172     closure_label   = mkStaticClosureLabel con_name
173     con_name        = dataConName data_con
174 \end{code}
175
176 \begin{code}
177 mkConCodeAndInfo :: DataCon             -- Data constructor
178                  -> (ClosureInfo, Code) -- The info table
179
180 mkConCodeAndInfo con
181   = let
182         arg_tys = dataConRawArgTys con
183
184         (closure_info, arg_things)
185                 = layOutDynCon con typePrimRep arg_tys
186
187         body_code
188                 = -- NB: We don't set CC when entering data (WDP 94/06)
189                   profCtrC SLIT("TICK_RET_OLD") 
190                         [mkIntCLit (length arg_things)] `thenC`
191
192                   performReturn AbsCNop         -- Ptr to thing already in Node
193                                 (mkStaticAlgReturnCode con)
194         in
195         (closure_info, body_code)
196 \end{code}