2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[CgConTbls]{Info tables and update bits for constructors}
7 module CgConTbls ( genStaticConBits ) where
9 #include "HsVersions.h"
14 import AbsCUtils ( mkAbstractCs, mkAbsCStmts )
15 import CostCentre ( subsumedCCS )
16 import CgTailCall ( performReturn, mkStaticAlgReturnCode )
17 import ClosureInfo ( layOutStaticConstr, layOutDynConstr, ClosureInfo )
18 import DataCon ( DataCon, dataConName, dataConRepArgTys, isNullaryDataCon )
19 import Name ( getOccName )
20 import OccName ( occNameUserString )
21 import TyCon ( tyConDataCons, isEnumerationTyCon, TyCon )
22 import Type ( typePrimRep )
26 For every constructor we generate the following info tables:
27 A static info table, for static instances of the constructor,
32 Info tbls & Macro & Kind of constructor \\
34 info & @CONST_INFO_TABLE@& Zero arity (no info -- compiler uses static closure)\\
35 info & @CHARLIKE_INFO_TABLE@& Charlike (no info -- compiler indexes fixed array)\\
36 info & @INTLIKE_INFO_TABLE@& Intlike; the one macro generates both info tbls\\
37 info & @SPEC_INFO_TABLE@& SPECish, and bigger than or equal to @MIN_UPD_SIZE@\\
38 info & @GEN_INFO_TABLE@& GENish (hence bigger than or equal to @MIN_UPD_SIZE@)\\
41 Possible info tables for constructor con:
45 Used for dynamically let(rec)-bound occurrences of
46 the constructor, and for updates. For constructors
47 which are int-like, char-like or nullary, when GC occurs,
48 the closure tries to get rid of itself.
50 \item[@_static_info@:]
51 Static occurrences of the constructor
52 macro: @STATIC_INFO_TABLE@.
56 For zero-arity constructors, \tr{con}, we NO LONGER generate a static closure;
57 it's place is taken by the top level defn of the constructor.
59 For charlike and intlike closures there is a fixed array of static
63 genStaticConBits :: CompilationInfo -- global info about the compilation
64 -> [TyCon] -- tycons to generate
65 -> AbstractC -- output
67 genStaticConBits comp_info gen_tycons
68 = -- for each type constructor:
69 -- grab all its data constructors;
70 -- for each one, generate an info table
71 -- for each specialised type constructor
72 -- for each specialisation of the type constructor
73 -- grab data constructors, and generate info tables
75 -- ToDo: for tycons and specialisations which are not
76 -- declared in this module we must ensure that the
77 -- C labels are local to this module i.e. static
78 -- since they may be duplicated in other modules
80 mkAbstractCs [ gen_for_tycon tc `mkAbsCStmts` enum_closure_table tc
83 gen_for_tycon :: TyCon -> AbstractC
84 gen_for_tycon tycon = mkAbstractCs [ genConInfo comp_info data_con
85 | data_con <- tyConDataCons tycon ]
87 enum_closure_table tycon
88 | isEnumerationTyCon tycon = CClosureTbl tycon
90 -- Put the table after the data constructor decls, because the
91 -- datatype closure table (for enumeration types)
92 -- to (say) PrelBase_$wTrue_closure, which is defined in code_stuff
96 %************************************************************************
98 \subsection[CgConTbls-info-tables]{Generating info tables for constructors}
100 %************************************************************************
102 Generate the entry code, info tables, and (for niladic constructor) the
103 static closure, for a constructor.
106 genConInfo :: CompilationInfo -> DataCon -> AbstractC
108 genConInfo comp_info data_con
109 = -- Order of things is to reduce forward references
110 mkAbstractCs [CSplitMarker,
114 (closure_info, body_code) = mkConCodeAndInfo data_con
116 -- To allow the debuggers, interpreters, etc to cope with static
117 -- data structures (ie those built at compile time), we take care that
118 -- info-table contains the information we need.
119 (static_ci,_) = layOutStaticConstr con_name data_con typePrimRep arg_tys
121 body = initC comp_info (
122 profCtrC SLIT("TICK_ENT_CON") [CReg node] `thenC`
125 con_descr = occNameUserString (getOccName data_con)
127 -- Don't need any dynamic closure code for zero-arity constructors
128 closure_code = if zero_arity_con then
131 CClosureInfoAndCode closure_info body Nothing con_descr
133 static_code = CClosureInfoAndCode static_ci body Nothing con_descr
135 zero_arity_con = isNullaryDataCon data_con
136 -- We used to check that all the arg-sizes were zero, but we don't
137 -- really have any constructors with only zero-size args, and it's
138 -- just one more thing to go wrong.
140 arg_tys = dataConRepArgTys data_con
141 con_name = dataConName data_con
145 mkConCodeAndInfo :: DataCon -- Data constructor
146 -> (ClosureInfo, Code) -- The info table
150 arg_tys = dataConRepArgTys con
152 (closure_info, arg_things)
153 = layOutDynConstr (dataConName con) con typePrimRep arg_tys
156 = -- NB: We don't set CC when entering data (WDP 94/06)
157 profCtrC SLIT("TICK_RET_OLD")
158 [mkIntCLit (length arg_things)] `thenC`
160 performReturn AbsCNop -- Ptr to thing already in Node
161 (mkStaticAlgReturnCode con)
163 (closure_info, body_code)