a20e0ee097dd6d76ec3152e6aadae577a7f85ce5
[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, mkAbsCStmts )
16 import CgTailCall       ( performReturn, mkStaticAlgReturnCode )
17 import CLabel           ( mkConEntryLabel )
18 import ClosureInfo      ( layOutStaticClosure, layOutDynCon,
19                           mkConLFInfo, ClosureInfo
20                         )
21 import CostCentre       ( dontCareCCS )
22 import FiniteMap        ( fmToList, FiniteMap )
23 import DataCon          ( DataCon, dataConName, dataConRepArgTys, isNullaryDataCon )
24 import Name             ( getOccString )
25 import PrimRep          ( getPrimRepSize, PrimRep(..) )
26 import TyCon            ( tyConDataCons, isEnumerationTyCon, TyCon )
27 import Type             ( typePrimRep, Type )
28 import Outputable       
29 \end{code}
30
31 For every constructor we generate the following info tables:
32         A static info table, for static instances of the constructor,
33
34         Plus:
35
36 \begin{tabular}{lll}
37 Info tbls &      Macro  &            Kind of constructor \\
38 \hline
39 info & @CONST_INFO_TABLE@&    Zero arity (no info -- compiler uses static closure)\\
40 info & @CHARLIKE_INFO_TABLE@& Charlike   (no info -- compiler indexes fixed array)\\
41 info & @INTLIKE_INFO_TABLE@&  Intlike; the one macro generates both info tbls\\
42 info & @SPEC_INFO_TABLE@&     SPECish, and bigger than or equal to @MIN_UPD_SIZE@\\
43 info & @GEN_INFO_TABLE@&      GENish (hence bigger than or equal to @MIN_UPD_SIZE@)\\
44 \end{tabular}
45
46 Possible info tables for constructor con:
47
48 \begin{description}
49 \item[@_con_info@:]
50 Used for dynamically let(rec)-bound occurrences of
51 the constructor, and for updates.  For constructors
52 which are int-like, char-like or nullary, when GC occurs,
53 the closure tries to get rid of itself.
54
55 \item[@_static_info@:]
56 Static occurrences of the constructor
57 macro: @STATIC_INFO_TABLE@.
58 \end{description}
59
60
61 For zero-arity constructors, \tr{con}, we NO LONGER generate a static closure;
62 it's place is taken by the top level defn of the constructor.
63
64 For charlike and intlike closures there is a fixed array of static
65 closures predeclared.
66
67 \begin{code}
68 genStaticConBits :: CompilationInfo     -- global info about the compilation
69                  -> [TyCon]             -- tycons to generate
70                  -> AbstractC           -- output
71
72 genStaticConBits comp_info gen_tycons
73   = -- for each type constructor:
74     --   grab all its data constructors;
75     --      for each one, generate an info table
76     -- for each specialised type constructor
77     --   for each specialisation of the type constructor
78     --     grab data constructors, and generate info tables
79
80     -- ToDo: for tycons and specialisations which are not
81     --       declared in this module we must ensure that the
82     --       C labels are local to this module i.e. static
83     --       since they may be duplicated in other modules
84
85     mkAbstractCs [ gen_for_tycon tc | tc <- gen_tycons ]
86   where
87     gen_for_tycon :: TyCon -> AbstractC
88     gen_for_tycon tycon
89       = mkAbstractCs (map (genConInfo comp_info tycon) (tyConDataCons tycon)) 
90         `mkAbsCStmts` (
91           -- after the con decls, so we don't need to declare the constructor labels
92           if (isEnumerationTyCon tycon)
93             then CClosureTbl tycon
94             else AbsCNop
95         )
96 \end{code}
97
98 %************************************************************************
99 %*                                                                      *
100 \subsection[CgConTbls-info-tables]{Generating info tables for constructors}
101 %*                                                                      *
102 %************************************************************************
103
104 Generate the entry code, info tables, and (for niladic constructor) the
105 static closure, for a constructor.
106
107 \begin{code}
108 genConInfo :: CompilationInfo -> TyCon -> DataCon -> AbstractC
109
110 genConInfo comp_info tycon data_con
111   = mkAbstractCs [
112                   CSplitMarker,
113                   closure_code,
114                   static_code]
115         -- Order of things is to reduce forward references
116   where
117     (closure_info, body_code) = mkConCodeAndInfo data_con
118
119     -- To allow the debuggers, interpreters, etc to cope with static
120     -- data structures (ie those built at compile time), we take care that
121     -- info-table contains the information we need.
122     (static_ci,_) = layOutStaticClosure con_name typePrimRep arg_tys 
123                                 (mkConLFInfo data_con)
124
125     body       = (initC comp_info (
126                       profCtrC SLIT("TICK_ENT_CON") [CReg node] `thenC`
127                       body_code))
128
129     entry_addr = CLbl entry_label CodePtrRep
130     con_descr  = getOccString data_con
131
132     -- Don't need any dynamic closure code for zero-arity constructors
133     closure_code = if zero_arity_con then 
134                         AbsCNop 
135                    else 
136                         CClosureInfoAndCode closure_info body Nothing con_descr
137
138     static_code  = CClosureInfoAndCode static_ci body Nothing con_descr
139
140     cost_centre  = mkCCostCentreStack dontCareCCS -- not worried about static data costs
141
142     zero_size arg_ty = getPrimRepSize (typePrimRep arg_ty) == 0
143
144     zero_arity_con   = isNullaryDataCon data_con
145         -- We used to check that all the arg-sizes were zero, but we don't
146         -- really have any constructors with only zero-size args, and it's
147         -- just one more thing to go wrong.
148
149     arg_tys         = dataConRepArgTys  data_con
150     entry_label     = mkConEntryLabel      con_name
151     con_name        = dataConName data_con
152 \end{code}
153
154 \begin{code}
155 mkConCodeAndInfo :: DataCon             -- Data constructor
156                  -> (ClosureInfo, Code) -- The info table
157
158 mkConCodeAndInfo con
159   = let
160         arg_tys = dataConRepArgTys con
161
162         (closure_info, arg_things)
163                 = layOutDynCon con typePrimRep arg_tys
164
165         body_code
166                 = -- NB: We don't set CC when entering data (WDP 94/06)
167                   profCtrC SLIT("TICK_RET_OLD") 
168                         [mkIntCLit (length arg_things)] `thenC`
169
170                   performReturn AbsCNop         -- Ptr to thing already in Node
171                                 (mkStaticAlgReturnCode con)
172         in
173         (closure_info, body_code)
174 \end{code}