2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 Storage manager representation of closures
8 This is here, rather than in ClosureInfo, just to keep nhc happy.
9 Other modules should access this info through ClosureInfo.
13 -- The above warning supression flag is a temporary kludge.
14 -- While working on this module you are encouraged to remove it and fix
15 -- any warnings in the module. See
16 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
22 hALF_WORD_SIZE, hALF_WORD_SIZE_IN_BITS,
25 -- Argument/return representations
26 CgRep(..), nonVoidArg,
27 argMachRep, primRepToCgRep, primRepHint,
28 isFollowableArg, isVoidArg,
29 isFloatingArg, is64BitArg,
30 separateByPtrFollowness,
31 cgRepSizeW, cgRepSizeB,
34 typeCgRep, idCgRep, tyConCgRep, typeHint,
36 -- Closure repesentation
37 SMRep(..), ClosureType(..),
39 fixedHdrSize, arrWordsHdrSize, arrPtrsHdrSize,
40 profHdrSize, thunkHdrSize,
41 smRepClosureType, smRepClosureTypeInt,
46 #include "HsVersions.h"
47 #include "../includes/MachDeps.h"
61 %************************************************************************
65 %************************************************************************
68 type WordOff = Int -- Word offset, or word count
69 type ByteOff = Int -- Byte offset, or byte count
72 StgWord is a type representing an StgWord on the target platform.
75 #if SIZEOF_HSWORD == 4
77 type StgHalfWord = Word16
78 hALF_WORD_SIZE = 2 :: ByteOff
79 hALF_WORD_SIZE_IN_BITS = 16 :: Int
80 #elif SIZEOF_HSWORD == 8
82 type StgHalfWord = Word32
83 hALF_WORD_SIZE = 4 :: ByteOff
84 hALF_WORD_SIZE_IN_BITS = 32 :: Int
86 #error unknown SIZEOF_HSWORD
91 %************************************************************************
95 %************************************************************************
97 An CgRep is an abstraction of a Type which tells the code generator
98 all it needs to know about the calling convention for arguments (and
99 results) of that type. In particular, the ArgReps of a function's
100 arguments are used to decide which of the RTS's generic apply
101 functions to call when applying an unknown function.
103 It contains more information than the back-end data type MachRep,
104 so one can easily convert from CgRep -> MachRep. (Except that
105 there's no MachRep for a VoidRep.)
108 pointers from non-pointers (we sort the pointers together
109 when building closures)
111 void from other types: a void argument is different from no argument
113 All 64-bit types map to the same CgRep, because they're passed in the
114 same register, but a PtrArg is still different from an NonPtrArg
115 because the function's entry convention has to take into account the
116 pointer-hood of arguments for the purposes of describing the stack on
117 entry to the garbage collector.
122 | PtrArg -- Word-sized heap pointer, followed
123 -- by the garbage collector
124 | NonPtrArg -- Word-sized non-pointer
125 -- (including addresses not followed by GC)
126 | LongArg -- 64-bit non-pointer
127 | FloatArg -- 32-bit float
128 | DoubleArg -- 64-bit float
131 instance Outputable CgRep where
132 ppr VoidArg = ptext SLIT("V_")
133 ppr PtrArg = ptext SLIT("P_")
134 ppr NonPtrArg = ptext SLIT("I_")
135 ppr LongArg = ptext SLIT("L_")
136 ppr FloatArg = ptext SLIT("F_")
137 ppr DoubleArg = ptext SLIT("D_")
139 argMachRep :: CgRep -> MachRep
140 argMachRep PtrArg = wordRep
141 argMachRep NonPtrArg = wordRep
142 argMachRep LongArg = I64
143 argMachRep FloatArg = F32
144 argMachRep DoubleArg = F64
145 argMachRep VoidArg = panic "argMachRep:VoidRep"
147 primRepToCgRep :: PrimRep -> CgRep
148 primRepToCgRep VoidRep = VoidArg
149 primRepToCgRep PtrRep = PtrArg
150 primRepToCgRep IntRep = NonPtrArg
151 primRepToCgRep WordRep = NonPtrArg
152 primRepToCgRep Int64Rep = LongArg
153 primRepToCgRep Word64Rep = LongArg
154 primRepToCgRep AddrRep = NonPtrArg
155 primRepToCgRep FloatRep = FloatArg
156 primRepToCgRep DoubleRep = DoubleArg
158 primRepHint :: PrimRep -> MachHint
159 primRepHint VoidRep = panic "primRepHint:VoidRep"
160 primRepHint PtrRep = PtrHint
161 primRepHint IntRep = SignedHint
162 primRepHint WordRep = NoHint
163 primRepHint Int64Rep = SignedHint
164 primRepHint Word64Rep = NoHint
165 primRepHint AddrRep = PtrHint -- NB! PtrHint, but NonPtrArg
166 primRepHint FloatRep = FloatHint
167 primRepHint DoubleRep = FloatHint
169 idCgRep :: Id -> CgRep
170 idCgRep x = typeCgRep . idType $ x
172 tyConCgRep :: TyCon -> CgRep
173 tyConCgRep = primRepToCgRep . tyConPrimRep
175 typeCgRep :: Type -> CgRep
176 typeCgRep = primRepToCgRep . typePrimRep
178 typeHint :: Type -> MachHint
179 typeHint = primRepHint . typePrimRep
182 Whether or not the thing is a pointer that the garbage-collector
183 should follow. Or, to put it another (less confusing) way, whether
184 the object in question is a heap object.
186 Depending on the outcome, this predicate determines what stack
187 the pointer/object possibly will have to be saved onto, and the
188 computation of GC liveness info.
191 isFollowableArg :: CgRep -> Bool -- True <=> points to a heap object
192 isFollowableArg PtrArg = True
193 isFollowableArg other = False
195 isVoidArg :: CgRep -> Bool
196 isVoidArg VoidArg = True
197 isVoidArg other = False
199 nonVoidArg :: CgRep -> Bool
200 nonVoidArg VoidArg = False
201 nonVoidArg other = True
203 -- isFloatingArg is used to distinguish @Double@ and @Float@ which
204 -- cause inadvertent numeric conversions if you aren't jolly careful.
205 -- See codeGen/CgCon:cgTopRhsCon.
207 isFloatingArg :: CgRep -> Bool
208 isFloatingArg DoubleArg = True
209 isFloatingArg FloatArg = True
210 isFloatingArg _ = False
212 is64BitArg :: CgRep -> Bool
213 is64BitArg LongArg = True
218 separateByPtrFollowness :: [(CgRep,a)] -> ([(CgRep,a)], [(CgRep,a)])
219 -- Returns (ptrs, non-ptrs)
220 separateByPtrFollowness things
221 = sep_things things [] []
222 -- accumulating params for follow-able and don't-follow things...
224 sep_things [] bs us = (reverse bs, reverse us)
225 sep_things ((PtrArg,a):ts) bs us = sep_things ts ((PtrArg,a):bs) us
226 sep_things (t :ts) bs us = sep_things ts bs (t:us)
230 cgRepSizeB :: CgRep -> ByteOff
231 cgRepSizeB DoubleArg = dOUBLE_SIZE
232 cgRepSizeB LongArg = wORD64_SIZE
233 cgRepSizeB VoidArg = 0
234 cgRepSizeB _ = wORD_SIZE
236 cgRepSizeW :: CgRep -> ByteOff
237 cgRepSizeW DoubleArg = dOUBLE_SIZE `quot` wORD_SIZE
238 cgRepSizeW LongArg = wORD64_SIZE `quot` wORD_SIZE
239 cgRepSizeW VoidArg = 0
242 retAddrSizeW :: WordOff
243 retAddrSizeW = 1 -- One word
246 %************************************************************************
248 \subsubsection[SMRep-datatype]{@SMRep@---storage manager representation}
250 %************************************************************************
254 -- static closure have an extra static link field at the end.
255 = GenericRep -- GC routines consult sizes in info tbl
256 Bool -- True <=> This is a static closure. Affects how
257 -- we garbage-collect it
259 !Int -- # non-ptr words
260 ClosureType -- closure type
264 data ClosureType -- Corresponds 1-1 with the varieties of closures
265 -- implemented by the RTS. Compare with ghc/includes/ClosureTypes.h
273 Size of a closure header.
276 fixedHdrSize :: WordOff
277 fixedHdrSize = sTD_HDR_SIZE + profHdrSize + granHdrSize
279 profHdrSize :: WordOff
280 profHdrSize | opt_SccProfilingOn = pROF_HDR_SIZE
283 granHdrSize :: WordOff
284 granHdrSize | opt_GranMacros = gRAN_HDR_SIZE
287 arrWordsHdrSize :: ByteOff
288 arrWordsHdrSize = fixedHdrSize*wORD_SIZE + sIZEOF_StgArrWords_NoHdr
290 arrPtrsHdrSize :: ByteOff
291 arrPtrsHdrSize = fixedHdrSize*wORD_SIZE + sIZEOF_StgMutArrPtrs_NoHdr
293 -- Thunks have an extra header word on SMP, so the update doesn't
294 -- splat the payload.
295 thunkHdrSize :: WordOff
296 thunkHdrSize = fixedHdrSize + smp_hdr
297 where smp_hdr = sIZEOF_StgSMPThunkHeader `quot` wORD_SIZE
301 isStaticRep :: SMRep -> Bool
302 isStaticRep (GenericRep is_static _ _ _) = is_static
303 isStaticRep BlackHoleRep = False
307 #include "../includes/ClosureTypes.h"
308 -- Defines CONSTR, CONSTR_1_0 etc
310 -- krc: only called by tickyDynAlloc in CgTicky; return
311 -- Nothing for a black hole so we can at least make something work.
312 smRepClosureType :: SMRep -> Maybe ClosureType
313 smRepClosureType (GenericRep _ _ _ ty) = Just ty
314 smRepClosureType BlackHoleRep = Nothing
316 smRepClosureTypeInt :: SMRep -> StgHalfWord
317 smRepClosureTypeInt (GenericRep False 1 0 Constr) = CONSTR_1_0
318 smRepClosureTypeInt (GenericRep False 0 1 Constr) = CONSTR_0_1
319 smRepClosureTypeInt (GenericRep False 2 0 Constr) = CONSTR_2_0
320 smRepClosureTypeInt (GenericRep False 1 1 Constr) = CONSTR_1_1
321 smRepClosureTypeInt (GenericRep False 0 2 Constr) = CONSTR_0_2
322 smRepClosureTypeInt (GenericRep False _ _ Constr) = CONSTR
324 smRepClosureTypeInt (GenericRep False 1 0 Fun) = FUN_1_0
325 smRepClosureTypeInt (GenericRep False 0 1 Fun) = FUN_0_1
326 smRepClosureTypeInt (GenericRep False 2 0 Fun) = FUN_2_0
327 smRepClosureTypeInt (GenericRep False 1 1 Fun) = FUN_1_1
328 smRepClosureTypeInt (GenericRep False 0 2 Fun) = FUN_0_2
329 smRepClosureTypeInt (GenericRep False _ _ Fun) = FUN
331 smRepClosureTypeInt (GenericRep False 1 0 Thunk) = THUNK_1_0
332 smRepClosureTypeInt (GenericRep False 0 1 Thunk) = THUNK_0_1
333 smRepClosureTypeInt (GenericRep False 2 0 Thunk) = THUNK_2_0
334 smRepClosureTypeInt (GenericRep False 1 1 Thunk) = THUNK_1_1
335 smRepClosureTypeInt (GenericRep False 0 2 Thunk) = THUNK_0_2
336 smRepClosureTypeInt (GenericRep False _ _ Thunk) = THUNK
338 smRepClosureTypeInt (GenericRep False _ _ ThunkSelector) = THUNK_SELECTOR
340 smRepClosureTypeInt (GenericRep True _ _ Constr) = CONSTR_STATIC
341 smRepClosureTypeInt (GenericRep True _ _ ConstrNoCaf) = CONSTR_NOCAF_STATIC
342 smRepClosureTypeInt (GenericRep True _ _ Fun) = FUN_STATIC
343 smRepClosureTypeInt (GenericRep True _ _ Thunk) = THUNK_STATIC
345 smRepClosureTypeInt BlackHoleRep = BLACKHOLE
347 smRepClosureTypeInt rep = panic "smRepClosuretypeint"
350 -- We export these ones
351 rET_SMALL = (RET_SMALL :: StgHalfWord)
352 rET_BIG = (RET_BIG :: StgHalfWord)