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 ( layOutStaticConstr, layOutDynConstr, ClosureInfo )
17 import DataCon ( DataCon, dataConRepArgTys, isNullaryDataCon )
18 import TyCon ( tyConDataCons, isEnumerationTyCon, TyCon )
19 import Type ( typePrimRep )
23 For every constructor we generate the following info tables:
24 A static info table, for static instances of the constructor,
29 Info tbls & Macro & Kind of constructor \\
31 info & @CONST_INFO_TABLE@& Zero arity (no info -- compiler uses static closure)\\
32 info & @CHARLIKE_INFO_TABLE@& Charlike (no info -- compiler indexes fixed array)\\
33 info & @INTLIKE_INFO_TABLE@& Intlike; the one macro generates both info tbls\\
34 info & @SPEC_INFO_TABLE@& SPECish, and bigger than or equal to @MIN_UPD_SIZE@\\
35 info & @GEN_INFO_TABLE@& GENish (hence bigger than or equal to @MIN_UPD_SIZE@)\\
38 Possible info tables for constructor con:
42 Used for dynamically let(rec)-bound occurrences of
43 the constructor, and for updates. For constructors
44 which are int-like, char-like or nullary, when GC occurs,
45 the closure tries to get rid of itself.
47 \item[@_static_info@:]
48 Static occurrences of the constructor
49 macro: @STATIC_INFO_TABLE@.
53 For zero-arity constructors, \tr{con}, we NO LONGER generate a static closure;
54 it's place is taken by the top level defn of the constructor.
56 For charlike and intlike closures there is a fixed array of static
60 genStaticConBits :: CompilationInfo -- global info about the compilation
61 -> [TyCon] -- tycons to generate
62 -> AbstractC -- output
64 genStaticConBits comp_info gen_tycons
65 = -- for each type constructor:
66 -- grab all its data constructors;
67 -- for each one, generate an info table
68 -- for each specialised type constructor
69 -- for each specialisation of the type constructor
70 -- grab data constructors, and generate info tables
72 -- ToDo: for tycons and specialisations which are not
73 -- declared in this module we must ensure that the
74 -- C labels are local to this module i.e. static
75 -- since they may be duplicated in other modules
77 mkAbstractCs [ gen_for_tycon tc `mkAbsCStmts` enum_closure_table tc
80 gen_for_tycon :: TyCon -> AbstractC
81 gen_for_tycon tycon = mkAbstractCs [ genConInfo comp_info data_con
82 | data_con <- tyConDataCons tycon ]
84 enum_closure_table tycon
85 | isEnumerationTyCon tycon = CClosureTbl tycon
87 -- Put the table after the data constructor decls, because the
88 -- datatype closure table (for enumeration types)
89 -- to (say) PrelBase_$wTrue_closure, which is defined in code_stuff
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 -> DataCon -> AbstractC
105 genConInfo comp_info data_con
106 = -- Order of things is to reduce forward references
107 mkAbstractCs [if opt_EnsureSplittableC then CSplitMarker else AbsCNop,
111 (closure_info, body_code) = mkConCodeAndInfo data_con
113 -- To allow the debuggers, interpreters, etc to cope with static
114 -- data structures (ie those built at compile time), we take care that
115 -- info-table contains the information we need.
116 (static_ci,_) = layOutStaticConstr data_con typePrimRep arg_tys
118 static_body = initC comp_info (
119 profCtrC FSLIT("TICK_ENT_STATIC_CON") [CReg node] `thenC`
120 ldv_enter_and_body_code)
122 closure_body = initC comp_info (
123 profCtrC FSLIT("TICK_ENT_DYN_CON") [CReg node] `thenC`
124 ldv_enter_and_body_code)
126 ldv_enter_and_body_code = ldvEnter `thenC` body_code
128 -- Don't need any dynamic closure code for zero-arity constructors
129 closure_code = if zero_arity_con then
132 CClosureInfoAndCode closure_info closure_body
134 static_code = CClosureInfoAndCode static_ci static_body
136 zero_arity_con = isNullaryDataCon data_con
137 -- We used to check that all the arg-sizes were zero, but we don't
138 -- really have any constructors with only zero-size args, and it's
139 -- just one more thing to go wrong.
141 arg_tys = dataConRepArgTys data_con
145 mkConCodeAndInfo :: DataCon -- Data constructor
146 -> (ClosureInfo, Code) -- The info table
150 arg_tys = dataConRepArgTys con
152 (closure_info, arg_things) = layOutDynConstr con typePrimRep arg_tys
155 = -- NB: We don't set CC when entering data (WDP 94/06)
156 profCtrC FSLIT("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)