[project @ 2000-11-06 08:15:20 by simonpj]
[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 CLabel           ( mkConEntryLabel )
17 import ClosureInfo      ( layOutStaticClosure, layOutDynCon,
18                           mkConLFInfo, ClosureInfo
19                         )
20 import DataCon          ( DataCon, dataConName, dataConRepArgTys, isNullaryDataCon )
21 import Name             ( getOccName )
22 import OccName          ( occNameUserString )
23 import TyCon            ( tyConDataCons, isEnumerationTyCon, TyCon )
24 import Type             ( typePrimRep )
25 \end{code}
26
27 For every constructor we generate the following info tables:
28         A static info table, for static instances of the constructor,
29
30         Plus:
31
32 \begin{tabular}{lll}
33 Info tbls &      Macro  &            Kind of constructor \\
34 \hline
35 info & @CONST_INFO_TABLE@&    Zero arity (no info -- compiler uses static closure)\\
36 info & @CHARLIKE_INFO_TABLE@& Charlike   (no info -- compiler indexes fixed array)\\
37 info & @INTLIKE_INFO_TABLE@&  Intlike; the one macro generates both info tbls\\
38 info & @SPEC_INFO_TABLE@&     SPECish, and bigger than or equal to @MIN_UPD_SIZE@\\
39 info & @GEN_INFO_TABLE@&      GENish (hence bigger than or equal to @MIN_UPD_SIZE@)\\
40 \end{tabular}
41
42 Possible info tables for constructor con:
43
44 \begin{description}
45 \item[@_con_info@:]
46 Used for dynamically let(rec)-bound occurrences of
47 the constructor, and for updates.  For constructors
48 which are int-like, char-like or nullary, when GC occurs,
49 the closure tries to get rid of itself.
50
51 \item[@_static_info@:]
52 Static occurrences of the constructor
53 macro: @STATIC_INFO_TABLE@.
54 \end{description}
55
56
57 For zero-arity constructors, \tr{con}, we NO LONGER generate a static closure;
58 it's place is taken by the top level defn of the constructor.
59
60 For charlike and intlike closures there is a fixed array of static
61 closures predeclared.
62
63 \begin{code}
64 genStaticConBits :: CompilationInfo     -- global info about the compilation
65                  -> [TyCon]             -- tycons to generate
66                  -> AbstractC           -- output
67
68 genStaticConBits comp_info gen_tycons
69   = -- for each type constructor:
70     --   grab all its data constructors;
71     --      for each one, generate an info table
72     -- for each specialised type constructor
73     --   for each specialisation of the type constructor
74     --     grab data constructors, and generate info tables
75
76     -- ToDo: for tycons and specialisations which are not
77     --       declared in this module we must ensure that the
78     --       C labels are local to this module i.e. static
79     --       since they may be duplicated in other modules
80
81     mkAbstractCs [ gen_for_tycon tc | tc <- gen_tycons ]
82   where
83     gen_for_tycon :: TyCon -> AbstractC
84     gen_for_tycon tycon
85       = mkAbstractCs (map (genConInfo comp_info tycon) (tyConDataCons tycon)) 
86         `mkAbsCStmts` (
87           -- after the con decls, so we don't need to declare the constructor labels
88           if (isEnumerationTyCon tycon)
89             then CClosureTbl tycon
90             else AbsCNop
91         )
92 \end{code}
93
94 %************************************************************************
95 %*                                                                      *
96 \subsection[CgConTbls-info-tables]{Generating info tables for constructors}
97 %*                                                                      *
98 %************************************************************************
99
100 Generate the entry code, info tables, and (for niladic constructor) the
101 static closure, for a constructor.
102
103 \begin{code}
104 genConInfo :: CompilationInfo -> TyCon -> DataCon -> AbstractC
105
106 genConInfo comp_info tycon data_con
107   = mkAbstractCs [
108                   CSplitMarker,
109                   closure_code,
110                   static_code]
111         -- Order of things is to reduce forward references
112   where
113     (closure_info, body_code) = mkConCodeAndInfo data_con
114
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,_) = layOutStaticClosure con_name typePrimRep arg_tys 
119                                 (mkConLFInfo data_con)
120
121     body       = (initC comp_info (
122                       profCtrC SLIT("TICK_ENT_CON") [CReg node] `thenC`
123                       body_code))
124
125     con_descr  = occNameUserString (getOccName data_con)
126
127     -- Don't need any dynamic closure code for zero-arity constructors
128     closure_code = if zero_arity_con then 
129                         AbsCNop 
130                    else 
131                         CClosureInfoAndCode closure_info body Nothing con_descr
132
133     static_code  = CClosureInfoAndCode static_ci body Nothing con_descr
134
135     zero_arity_con   = isNullaryDataCon data_con
136         -- We used to check that all the arg-sizes were zero, but we don't
137         -- really have any constructors with only zero-size args, and it's
138         -- just one more thing to go wrong.
139
140     arg_tys         = dataConRepArgTys  data_con
141     con_name        = dataConName 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)
153                 = layOutDynCon con typePrimRep arg_tys
154
155         body_code
156                 = -- NB: We don't set CC when entering data (WDP 94/06)
157                   profCtrC SLIT("TICK_RET_OLD") 
158                         [mkIntCLit (length arg_things)] `thenC`
159
160                   performReturn AbsCNop         -- Ptr to thing already in Node
161                                 (mkStaticAlgReturnCode con)
162         in
163         (closure_info, body_code)
164 \end{code}