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, dataConName, dataConRepArgTys, isNullaryDataCon )
18 import Name ( getOccName )
19 import OccName ( occNameUserString )
20 import TyCon ( tyConDataCons, isEnumerationTyCon, TyCon )
21 import Type ( typePrimRep )
22 import CmdLineOpts ( opt_EnsureSplittableC )
25 For every constructor we generate the following info tables:
26 A static info table, for static instances of the constructor,
31 Info tbls & Macro & Kind of constructor \\
33 info & @CONST_INFO_TABLE@& Zero arity (no info -- compiler uses static closure)\\
34 info & @CHARLIKE_INFO_TABLE@& Charlike (no info -- compiler indexes fixed array)\\
35 info & @INTLIKE_INFO_TABLE@& Intlike; the one macro generates both info tbls\\
36 info & @SPEC_INFO_TABLE@& SPECish, and bigger than or equal to @MIN_UPD_SIZE@\\
37 info & @GEN_INFO_TABLE@& GENish (hence bigger than or equal to @MIN_UPD_SIZE@)\\
40 Possible info tables for constructor con:
44 Used for dynamically let(rec)-bound occurrences of
45 the constructor, and for updates. For constructors
46 which are int-like, char-like or nullary, when GC occurs,
47 the closure tries to get rid of itself.
49 \item[@_static_info@:]
50 Static occurrences of the constructor
51 macro: @STATIC_INFO_TABLE@.
55 For zero-arity constructors, \tr{con}, we NO LONGER generate a static closure;
56 it's place is taken by the top level defn of the constructor.
58 For charlike and intlike closures there is a fixed array of static
62 genStaticConBits :: CompilationInfo -- global info about the compilation
63 -> [TyCon] -- tycons to generate
64 -> AbstractC -- output
66 genStaticConBits comp_info gen_tycons
67 = -- for each type constructor:
68 -- grab all its data constructors;
69 -- for each one, generate an info table
70 -- for each specialised type constructor
71 -- for each specialisation of the type constructor
72 -- grab data constructors, and generate info tables
74 -- ToDo: for tycons and specialisations which are not
75 -- declared in this module we must ensure that the
76 -- C labels are local to this module i.e. static
77 -- since they may be duplicated in other modules
79 mkAbstractCs [ gen_for_tycon tc `mkAbsCStmts` enum_closure_table tc
82 gen_for_tycon :: TyCon -> AbstractC
83 gen_for_tycon tycon = mkAbstractCs [ genConInfo comp_info data_con
84 | data_con <- tyConDataCons tycon ]
86 enum_closure_table tycon
87 | isEnumerationTyCon tycon = CClosureTbl tycon
89 -- Put the table after the data constructor decls, because the
90 -- datatype closure table (for enumeration types)
91 -- to (say) PrelBase_$wTrue_closure, which is defined in code_stuff
95 %************************************************************************
97 \subsection[CgConTbls-info-tables]{Generating info tables for constructors}
99 %************************************************************************
101 Generate the entry code, info tables, and (for niladic constructor) the
102 static closure, for a constructor.
105 genConInfo :: CompilationInfo -> DataCon -> AbstractC
107 genConInfo comp_info data_con
108 = -- Order of things is to reduce forward references
109 mkAbstractCs [if opt_EnsureSplittableC then CSplitMarker else AbsCNop,
113 (closure_info, body_code) = mkConCodeAndInfo data_con
115 -- To allow the debuggers, interpreters, etc to cope with static
116 -- data structures (ie those built at compile time), we take care that
117 -- info-table contains the information we need.
118 (static_ci,_) = layOutStaticConstr con_name data_con typePrimRep arg_tys
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 = layOutDynConstr (dataConName con) 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)