[project @ 2004-08-13 10:45:16 by simonmar]
[ghc-hetmet.git] / ghc / compiler / codeGen / CgConTbls.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[CgConTbls]{Info tables and update bits for constructors}
5
6 \begin{code}
7 module CgConTbls ( genStaticConBits ) where
8
9 #include "HsVersions.h"
10
11 import AbsCSyn
12 import CgMonad
13
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 )
20 import CmdLineOpts
21 \end{code}
22
23 For every constructor we generate the following info tables:
24         A static info table, for static instances of the constructor,
25
26         Plus:
27
28 \begin{tabular}{lll}
29 Info tbls &      Macro  &            Kind of constructor \\
30 \hline
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@)\\
36 \end{tabular}
37
38 Possible info tables for constructor con:
39
40 \begin{description}
41 \item[@_con_info@:]
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.
46
47 \item[@_static_info@:]
48 Static occurrences of the constructor
49 macro: @STATIC_INFO_TABLE@.
50 \end{description}
51
52
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.
55
56 For charlike and intlike closures there is a fixed array of static
57 closures predeclared.
58
59 \begin{code}
60 genStaticConBits :: CompilationInfo     -- global info about the compilation
61                  -> [TyCon]             -- tycons to generate
62                  -> AbstractC           -- output
63
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
71
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
76
77     mkAbstractCs [ gen_for_tycon tc `mkAbsCStmts` enum_closure_table tc
78                  | tc <- gen_tycons ]
79   where
80     gen_for_tycon :: TyCon -> AbstractC
81     gen_for_tycon tycon = mkAbstractCs [ genConInfo comp_info data_con 
82                                        | data_con <- tyConDataCons tycon ] 
83
84     enum_closure_table tycon
85         | isEnumerationTyCon tycon = CClosureTbl tycon
86         | otherwise                = AbsCNop
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
90 \end{code}
91
92
93 %************************************************************************
94 %*                                                                      *
95 \subsection[CgConTbls-info-tables]{Generating info tables for constructors}
96 %*                                                                      *
97 %************************************************************************
98
99 Generate the entry code, info tables, and (for niladic constructor) the
100 static closure, for a constructor.
101
102 \begin{code}
103 genConInfo :: CompilationInfo -> DataCon -> AbstractC
104
105 genConInfo comp_info data_con
106   =     -- Order of things is to reduce forward references
107     mkAbstractCs [if opt_EnsureSplittableC then CSplitMarker else AbsCNop,
108                   closure_code,
109                   static_code]
110   where
111     (closure_info, body_code) = mkConCodeAndInfo data_con
112
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
117
118     static_body  = initC comp_info (
119                       profCtrC FSLIT("TICK_ENT_STATIC_CON") [CReg node] `thenC`
120                       ldv_enter_and_body_code)
121
122     closure_body = initC comp_info (
123                       profCtrC FSLIT("TICK_ENT_DYN_CON") [CReg node] `thenC`
124                       ldv_enter_and_body_code)
125
126     ldv_enter_and_body_code = ldvEnter `thenC` body_code
127
128     -- Don't need any dynamic closure code for zero-arity constructors
129     closure_code = if zero_arity_con then 
130                         AbsCNop 
131                    else 
132                         CClosureInfoAndCode closure_info closure_body
133
134     static_code  = CClosureInfoAndCode static_ci static_body
135
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.
140
141     arg_tys         = dataConRepArgTys  data_con
142 \end{code}
143
144 \begin{code}
145 mkConCodeAndInfo :: DataCon             -- Data constructor
146                  -> (ClosureInfo, Code) -- The info table
147
148 mkConCodeAndInfo con
149   = let
150         arg_tys = dataConRepArgTys con
151
152         (closure_info, arg_things) = layOutDynConstr con typePrimRep arg_tys
153
154         body_code
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`
158
159                   performReturn AbsCNop         -- Ptr to thing already in Node
160                                 (mkStaticAlgReturnCode con)
161         in
162         (closure_info, body_code)
163 \end{code}