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 CgTailCall ( performReturn, mkStaticAlgReturnCode )
16 import ClosureInfo ( layOutStaticClosure, layOutDynCon,
17 mkConLFInfo, ClosureInfo
19 import DataCon ( DataCon, dataConName, dataConRepArgTys, isNullaryDataCon )
20 import Name ( getOccName )
21 import OccName ( occNameUserString )
22 import TyCon ( tyConDataCons, isEnumerationTyCon, TyCon )
23 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 | tc <- gen_tycons ]
82 gen_for_tycon :: TyCon -> AbstractC
84 = mkAbstractCs (map (genConInfo comp_info tycon) (tyConDataCons tycon))
86 -- after the con decls, so we don't need to declare the constructor labels
87 if (isEnumerationTyCon tycon)
88 then CClosureTbl tycon
93 %************************************************************************
95 \subsection[CgConTbls-info-tables]{Generating info tables for constructors}
97 %************************************************************************
99 Generate the entry code, info tables, and (for niladic constructor) the
100 static closure, for a constructor.
103 genConInfo :: CompilationInfo -> TyCon -> DataCon -> AbstractC
105 genConInfo comp_info tycon data_con
110 -- Order of things is to reduce forward references
112 (closure_info, body_code) = mkConCodeAndInfo data_con
114 -- To allow the debuggers, interpreters, etc to cope with static
115 -- data structures (ie those built at compile time), we take care that
116 -- info-table contains the information we need.
117 (static_ci,_) = layOutStaticClosure con_name typePrimRep arg_tys
118 (mkConLFInfo data_con)
120 body = (initC comp_info (
121 profCtrC SLIT("TICK_ENT_CON") [CReg node] `thenC`
124 con_descr = occNameUserString (getOccName data_con)
126 -- Don't need any dynamic closure code for zero-arity constructors
127 closure_code = if zero_arity_con then
130 CClosureInfoAndCode closure_info body Nothing con_descr
132 static_code = CClosureInfoAndCode static_ci body Nothing con_descr
134 zero_arity_con = isNullaryDataCon data_con
135 -- We used to check that all the arg-sizes were zero, but we don't
136 -- really have any constructors with only zero-size args, and it's
137 -- just one more thing to go wrong.
139 arg_tys = dataConRepArgTys data_con
140 con_name = dataConName data_con
144 mkConCodeAndInfo :: DataCon -- Data constructor
145 -> (ClosureInfo, Code) -- The info table
149 arg_tys = dataConRepArgTys con
151 (closure_info, arg_things)
152 = layOutDynCon con typePrimRep arg_tys
155 = -- NB: We don't set CC when entering data (WDP 94/06)
156 profCtrC SLIT("TICK_RET_OLD")
157 [mkIntCLit (length arg_things)] `thenC`
159 performReturn AbsCNop -- Ptr to thing already in Node
160 (mkStaticAlgReturnCode con)
162 (closure_info, body_code)