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