1 -----------------------------------------------------------------------------
3 -- Stg to C--: code generation for constructors
5 -- This module provides the support code for StgCmm to deal with with
6 -- constructors on the RHSs of let(rec)s.
8 -- (c) The University of Glasgow 2004-2006
10 -----------------------------------------------------------------------------
13 cgTopRhsCon, buildDynCon, bindConArgs
16 #include "HsVersions.h"
19 import CoreSyn ( AltCon(..) )
30 import MkZipCfgCmm (CmmAGraph, mkNop)
41 import Util ( lengthIs )
42 import StaticFlags ( opt_PIC )
46 ---------------------------------------------------------------
47 -- Top-level constructors
48 ---------------------------------------------------------------
50 cgTopRhsCon :: Id -- Name of thing bound to this RHS
54 cgTopRhsCon id con args
57 -- Windows DLLs have a problem with static cross-DLL refs.
58 ; this_pkg <- getThisPackage
59 ; ASSERT( not (isDllConApp this_pkg con args) ) return ()
61 ; ASSERT( args `lengthIs` dataConRepArity con ) return ()
66 lf_info = mkConLFInfo con
67 closure_label = mkClosureLabel name $ idCafInfo id
68 caffy = any stgArgHasCafRefs args
69 (closure_info, nv_args_w_offsets)
70 = layOutStaticConstr con (addArgReps args)
72 get_lit (arg, _offset) = do { CmmLit lit <- getArgAmode arg
75 ; payload <- mapM get_lit nv_args_w_offsets
76 -- NB1: nv_args_w_offsets is sorted into ptrs then non-ptrs
77 -- NB2: all the amodes should be Lits!
79 ; let closure_rep = mkStaticClosureFields
81 dontCareCCS -- Because it's static data
86 ; emitDataLits closure_label closure_rep
89 ; return $ litIdInfo id lf_info (CmmLabel closure_label) }
92 ---------------------------------------------------------------
93 -- Lay out and allocate non-top-level constructors
94 ---------------------------------------------------------------
96 buildDynCon :: Id -- Name of the thing to which this constr will
98 -> CostCentreStack -- Where to grab cost centre from;
99 -- current CCS if currentOrSubsumedCCS
100 -> DataCon -- The data constructor
101 -> [StgArg] -- Its args
102 -> FCode (CgIdInfo, CmmAGraph)
103 -- Return details about how to find it and initialization code
105 {- We used to pass a boolean indicating whether all the
106 args were of size zero, so we could use a static
107 construtor; but I concluded that it just isn't worth it.
108 Now I/O uses unboxed tuples there just aren't any constructors
109 with all size-zero args.
111 The reason for having a separate argument, rather than looking at
112 the addr modes of the args is that we may be in a "knot", and
113 premature looking at the args will cause the compiler to black-hole!
117 -------- buildDynCon: Nullary constructors --------------
118 -- First we deal with the case of zero-arity constructors. They
119 -- will probably be unfolded, so we don't expect to see this case much,
120 -- if at all, but it does no harm, and sets the scene for characters.
122 -- In the case of zero-arity constructors, or, more accurately, those
123 -- which have exclusively size-zero (VoidRep) args, we generate no code
126 buildDynCon binder _cc con []
127 = return (litIdInfo binder (mkConLFInfo con)
128 (CmmLabel (mkClosureLabel (dataConName con) (idCafInfo binder))),
131 -------- buildDynCon: Charlike and Intlike constructors -----------
132 {- The following three paragraphs about @Char@-like and @Int@-like
133 closures are obsolete, but I don't understand the details well enough
134 to properly word them, sorry. I've changed the treatment of @Char@s to
135 be analogous to @Int@s: only a subset is preallocated, because @Char@
136 has now 31 bits. Only literals are handled here. -- Qrczak
138 Now for @Char@-like closures. We generate an assignment of the
139 address of the closure to a temporary. It would be possible simply to
140 generate no code, and record the addressing mode in the environment,
141 but we'd have to be careful if the argument wasn't a constant --- so
142 for simplicity we just always asssign to a temporary.
144 Last special case: @Int@-like closures. We only special-case the
145 situation in which the argument is a literal in the range
146 @mIN_INTLIKE@..@mAX_INTLILKE@. NB: for @Char@-like closures we can
147 work with any old argument, but for @Int@-like ones the argument has
148 to be a literal. Reason: @Char@ like closures have an argument type
149 which is guaranteed in range.
151 Because of this, we use can safely return an addressing mode.
153 We don't support this optimisation when compiling into Windows DLLs yet
154 because they don't support cross package data references well.
157 buildDynCon binder _cc con [arg]
158 | maybeIntLikeCon con
159 #if defined(mingw32_TARGET_OS)
162 , StgLitArg (MachInt val) <- arg
163 , val <= fromIntegral mAX_INTLIKE -- Comparisons at type Integer!
164 , val >= fromIntegral mIN_INTLIKE -- ...ditto...
165 = do { let intlike_lbl = mkCmmGcPtrLabel rtsPackageId (fsLit "stg_INTLIKE_closure")
166 val_int = fromIntegral val :: Int
167 offsetW = (val_int - mIN_INTLIKE) * (fixedHdrSize + 1)
168 -- INTLIKE closures consist of a header and one word payload
169 intlike_amode = cmmLabelOffW intlike_lbl offsetW
170 ; return (litIdInfo binder (mkConLFInfo con) intlike_amode, mkNop) }
172 buildDynCon binder _cc con [arg]
173 | maybeCharLikeCon con
174 #if defined(mingw32_TARGET_OS)
177 , StgLitArg (MachChar val) <- arg
178 , let val_int = ord val :: Int
179 , val_int <= mAX_CHARLIKE
180 , val_int >= mIN_CHARLIKE
181 = do { let charlike_lbl = mkCmmGcPtrLabel rtsPackageId (fsLit "stg_CHARLIKE_closure")
182 offsetW = (val_int - mIN_CHARLIKE) * (fixedHdrSize + 1)
183 -- CHARLIKE closures consist of a header and one word payload
184 charlike_amode = cmmLabelOffW charlike_lbl offsetW
185 ; return (litIdInfo binder (mkConLFInfo con) charlike_amode, mkNop) }
187 -------- buildDynCon: the general case -----------
188 buildDynCon binder ccs con args
189 = do { let (cl_info, args_w_offsets) = layOutDynConstr con (addArgReps args)
190 -- No void args in args_w_offsets
191 ; (tmp, init) <- allocDynClosure cl_info use_cc blame_cc args_w_offsets
192 ; return (regIdInfo binder lf_info tmp, init) }
194 lf_info = mkConLFInfo con
196 use_cc -- cost-centre to stick in the object
197 | currentOrSubsumedCCS ccs = curCCS
198 | otherwise = CmmLit (mkCCostCentreStack ccs)
200 blame_cc = use_cc -- cost-centre on which to blame the alloc (same)
203 ---------------------------------------------------------------
204 -- Binding constructor arguments
205 ---------------------------------------------------------------
207 bindConArgs :: AltCon -> LocalReg -> [Id] -> FCode [LocalReg]
208 -- bindConArgs is called from cgAlt of a case
209 -- (bindConArgs con args) augments the environment with bindings for the
210 -- binders args, assuming that we have just returned from a 'case' which
212 bindConArgs (DataAlt con) base args
213 = ASSERT(not (isUnboxedTupleCon con))
214 mapM bind_arg args_w_offsets
216 (_, args_w_offsets) = layOutDynConstr con (addIdReps args)
220 -- The binding below forces the masking out of the tag bits
221 -- when accessing the constructor field.
222 bind_arg :: (NonVoid Id, VirtualHpOffset) -> FCode LocalReg
223 bind_arg (arg, offset)
224 = do { emit $ mkTaggedObjectLoad (idToReg arg) base offset tag
227 bindConArgs _other_con _base args
228 = ASSERT( null args ) return []