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 StgSyn ( SRT(..) )
15 import AbsCUtils ( mkAbstractCs, mkAbsCStmts )
16 import CgTailCall ( performReturn, mkStaticAlgReturnCode )
17 import CLabel ( mkConEntryLabel, mkStaticClosureLabel )
18 import ClosureInfo ( layOutStaticClosure, layOutDynCon,
19 mkConLFInfo, ClosureInfo
21 import CostCentre ( dontCareCCS )
22 import FiniteMap ( fmToList, FiniteMap )
23 import DataCon ( DataCon, dataConName, dataConAllRawArgTys )
24 import Const ( Con(..) )
25 import Name ( getOccString )
26 import PrimRep ( getPrimRepSize, PrimRep(..) )
27 import TyCon ( tyConDataCons, isEnumerationTyCon, TyCon )
28 import Type ( typePrimRep, Type )
32 For every constructor we generate the following info tables:
33 A static info table, for static instances of the constructor,
38 Info tbls & Macro & Kind of constructor \\
40 info & @CONST_INFO_TABLE@& Zero arity (no info -- compiler uses static closure)\\
41 info & @CHARLIKE_INFO_TABLE@& Charlike (no info -- compiler indexes fixed array)\\
42 info & @INTLIKE_INFO_TABLE@& Intlike; the one macro generates both info tbls\\
43 info & @SPEC_INFO_TABLE@& SPECish, and bigger than or equal to @MIN_UPD_SIZE@\\
44 info & @GEN_INFO_TABLE@& GENish (hence bigger than or equal to @MIN_UPD_SIZE@)\\
47 Possible info tables for constructor con:
51 Used for dynamically let(rec)-bound occurrences of
52 the constructor, and for updates. For constructors
53 which are int-like, char-like or nullary, when GC occurs,
54 the closure tries to get rid of itself.
56 \item[@_static_info@:]
57 Static occurrences of the constructor
58 macro: @STATIC_INFO_TABLE@.
61 For zero-arity constructors, \tr{con}, we also generate a static closure:
65 A single static copy of the (zero-arity) constructor itself.
68 For charlike and intlike closures there is a fixed array of static
72 genStaticConBits :: CompilationInfo -- global info about the compilation
73 -> [TyCon] -- tycons to generate
74 -> AbstractC -- output
76 genStaticConBits comp_info gen_tycons
77 = -- for each type constructor:
78 -- grab all its data constructors;
79 -- for each one, generate an info table
80 -- for each specialised type constructor
81 -- for each specialisation of the type constructor
82 -- grab data constructors, and generate info tables
84 -- ToDo: for tycons and specialisations which are not
85 -- declared in this module we must ensure that the
86 -- C labels are local to this module i.e. static
87 -- since they may be duplicated in other modules
89 mkAbstractCs [ gen_for_tycon tc | tc <- gen_tycons ]
91 gen_for_tycon :: TyCon -> AbstractC
93 = mkAbstractCs (map (genConInfo comp_info tycon) (tyConDataCons tycon))
95 -- after the con decls, so we don't need to declare the constructor labels
96 if (isEnumerationTyCon tycon)
97 then CClosureTbl tycon
102 %************************************************************************
104 \subsection[CgConTbls-info-tables]{Generating info tables for constructors}
106 %************************************************************************
108 Generate the entry code, info tables, and (for niladic constructor) the
109 static closure, for a constructor.
112 genConInfo :: CompilationInfo -> TyCon -> DataCon -> AbstractC
114 genConInfo comp_info tycon data_con
120 -- Order of things is to reduce forward references
122 (closure_info, body_code) = mkConCodeAndInfo data_con
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)
130 body = (initC comp_info (
131 profCtrC SLIT("TICK_ENT_CON") [CReg node] `thenC`
134 entry_addr = CLbl entry_label CodePtrRep
135 con_descr = getOccString data_con
137 -- Don't need any dynamic closure code for zero-arity constructors
138 closure_code = if zero_arity_con then
141 CClosureInfoAndCode closure_info body Nothing con_descr
143 static_code = CClosureInfoAndCode static_ci body Nothing con_descr
145 cost_centre = mkCCostCentreStack dontCareCCS -- not worried about static data costs
147 -- For zero-arity data constructors, or, more accurately,
148 -- those which only have VoidRep args (or none):
149 -- We make the closure too (not just info tbl), so that we can share
150 -- one copy throughout.
151 closure_maybe = if not zero_arity_con then
154 CStaticClosure closure_label -- Label for closure
155 static_ci -- Info table
157 [{-No args! A slight lie for constrs
160 zero_size arg_ty = getPrimRepSize (typePrimRep arg_ty) == 0
162 zero_arity_con = all zero_size arg_tys
164 arg_tys = dataConAllRawArgTys data_con
165 entry_label = mkConEntryLabel con_name
166 closure_label = mkStaticClosureLabel con_name
167 con_name = dataConName data_con
171 mkConCodeAndInfo :: DataCon -- Data constructor
172 -> (ClosureInfo, Code) -- The info table
176 arg_tys = dataConAllRawArgTys con
178 (closure_info, arg_things)
179 = layOutDynCon con typePrimRep arg_tys
182 = -- NB: We don't set CC when entering data (WDP 94/06)
183 profCtrC SLIT("TICK_RET_OLD")
184 [mkIntCLit (length arg_things)] `thenC`
186 performReturn AbsCNop -- Ptr to thing already in Node
187 (mkStaticAlgReturnCode con)
189 (closure_info, body_code)