2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[SMRep]{Storage manager representations of closure}
6 This is here, rather than in ClosureInfo, just to keep nhc happy.
7 Other modules should access this info through ClosureInfo.
13 hALF_WORD_SIZE, hALF_WORD_SIZE_IN_BITS,
16 -- Argument/return representations
17 CgRep(..), nonVoidArg,
18 argMachRep, primRepToCgRep, primRepHint,
19 isFollowableArg, isVoidArg,
20 isFloatingArg, isNonPtrArg, is64BitArg,
21 separateByPtrFollowness,
22 cgRepSizeW, cgRepSizeB,
25 typeCgRep, idCgRep, tyConCgRep, typeHint,
27 -- Closure repesentation
28 SMRep(..), ClosureType(..),
30 fixedHdrSize, arrWordsHdrSize, arrPtrsHdrSize,
33 smRepClosureType, smRepClosureTypeInt,
35 rET_SMALL, rET_VEC_SMALL, rET_BIG, rET_VEC_BIG
38 #include "HsVersions.h"
39 #include "../includes/MachDeps.h"
41 import Id ( Id, idType )
42 import Type ( Type, typePrimRep, PrimRep(..) )
43 import TyCon ( TyCon, tyConPrimRep )
44 import MachOp-- ( MachRep(..), MachHint(..), wordRep )
45 import CmdLineOpts ( opt_SccProfilingOn, opt_GranMacros, opt_Unregisterised )
53 %************************************************************************
57 %************************************************************************
60 type WordOff = Int -- Word offset, or word count
61 type ByteOff = Int -- Byte offset, or byte count
64 StgWord is a type representing an StgWord on the target platform.
67 #if SIZEOF_HSWORD == 4
69 type StgHalfWord = Word16
70 hALF_WORD_SIZE = 2 :: ByteOff
71 hALF_WORD_SIZE_IN_BITS = 16 :: Int
72 #elif SIZEOF_HSWORD == 8
74 type StgHalfWord = Word32
75 hALF_WORD_SIZE = 4 :: ByteOff
76 hALF_WORD_SIZE_IN_BITS = 32 :: Int
78 #error unknown SIZEOF_HSWORD
83 %************************************************************************
87 %************************************************************************
89 An CgRep is an abstraction of a Type which tells the code generator
90 all it needs to know about the calling convention for arguments (and
91 results) of that type. In particular, the ArgReps of a function's
92 arguments are used to decide which of the RTS's generic apply
93 functions to call when applying an unknown function.
95 It contains more information than the back-end data type MachRep,
96 so one can easily convert from CgRep -> MachRep. (Except that
97 there's no MachRep for a VoidRep.)
100 pointers from non-pointers (we sort the pointers together
101 when building closures)
103 void from other types: a void argument is different from no argument
105 All 64-bit types map to the same CgRep, because they're passed in the
106 same register, but a PtrArg is still different from an NonPtrArg
107 because the function's entry convention has to take into account the
108 pointer-hood of arguments for the purposes of describing the stack on
109 entry to the garbage collector.
114 | PtrArg -- Word-sized Ptr
115 | NonPtrArg -- Word-sized non-pointer
116 | LongArg -- 64-bit non-pointer
117 | FloatArg -- 32-bit float
118 | DoubleArg -- 64-bit float
121 instance Outputable CgRep where
122 ppr VoidArg = ptext SLIT("V_")
123 ppr PtrArg = ptext SLIT("P_")
124 ppr NonPtrArg = ptext SLIT("I_")
125 ppr LongArg = ptext SLIT("L_")
126 ppr FloatArg = ptext SLIT("F_")
127 ppr DoubleArg = ptext SLIT("D_")
129 argMachRep :: CgRep -> MachRep
130 argMachRep PtrArg = wordRep
131 argMachRep NonPtrArg = wordRep
132 argMachRep LongArg = I64
133 argMachRep FloatArg = F32
134 argMachRep DoubleArg = F64
135 argMachRep VoidArg = panic "argMachRep:VoidRep"
137 primRepToCgRep :: PrimRep -> CgRep
138 primRepToCgRep VoidRep = VoidArg
139 primRepToCgRep PtrRep = PtrArg
140 primRepToCgRep IntRep = NonPtrArg
141 primRepToCgRep WordRep = NonPtrArg
142 primRepToCgRep Int64Rep = LongArg
143 primRepToCgRep Word64Rep = LongArg
144 primRepToCgRep AddrRep = NonPtrArg
145 primRepToCgRep FloatRep = FloatArg
146 primRepToCgRep DoubleRep = DoubleArg
148 primRepHint :: PrimRep -> MachHint
149 primRepHint VoidRep = panic "primRepHint:VoidRep"
150 primRepHint PtrRep = PtrHint
151 primRepHint IntRep = SignedHint
152 primRepHint WordRep = NoHint
153 primRepHint Int64Rep = SignedHint
154 primRepHint Word64Rep = NoHint
155 primRepHint AddrRep = PtrHint -- NB! PtrHint, but NonPtrArg
156 primRepHint FloatRep = FloatHint
157 primRepHint DoubleRep = FloatHint
159 idCgRep :: Id -> CgRep
160 idCgRep = typeCgRep . idType
162 tyConCgRep :: TyCon -> CgRep
163 tyConCgRep = primRepToCgRep . tyConPrimRep
165 typeCgRep :: Type -> CgRep
166 typeCgRep = primRepToCgRep . typePrimRep
168 typeHint :: Type -> MachHint
169 typeHint = primRepHint . typePrimRep
172 Whether or not the thing is a pointer that the garbage-collector
173 should follow. Or, to put it another (less confusing) way, whether
174 the object in question is a heap object.
176 Depending on the outcome, this predicate determines what stack
177 the pointer/object possibly will have to be saved onto, and the
178 computation of GC liveness info.
181 isFollowableArg :: CgRep -> Bool -- True <=> points to a heap object
182 isFollowableArg PtrArg = True
183 isFollowableArg other = False
185 isVoidArg :: CgRep -> Bool
186 isVoidArg VoidArg = True
187 isVoidArg other = False
189 nonVoidArg :: CgRep -> Bool
190 nonVoidArg VoidArg = False
191 nonVoidArg other = True
193 -- isFloatingArg is used to distinguish @Double@ and @Float@ which
194 -- cause inadvertent numeric conversions if you aren't jolly careful.
195 -- See codeGen/CgCon:cgTopRhsCon.
197 isFloatingArg :: CgRep -> Bool
198 isFloatingArg DoubleArg = True
199 isFloatingArg FloatArg = True
200 isFloatingArg _ = False
202 isNonPtrArg :: CgRep -> Bool
203 -- Identify anything which is one word large and not a pointer.
204 isNonPtrArg NonPtrArg = True
205 isNonPtrArg other = False
207 is64BitArg :: CgRep -> Bool
208 is64BitArg LongArg = True
213 separateByPtrFollowness :: [(CgRep,a)] -> ([(CgRep,a)], [(CgRep,a)])
214 -- Returns (ptrs, non-ptrs)
215 separateByPtrFollowness things
216 = sep_things things [] []
217 -- accumulating params for follow-able and don't-follow things...
219 sep_things [] bs us = (reverse bs, reverse us)
220 sep_things ((PtrArg,a):ts) bs us = sep_things ts ((PtrArg,a):bs) us
221 sep_things (t :ts) bs us = sep_things ts bs (t:us)
225 cgRepSizeB :: CgRep -> ByteOff
226 cgRepSizeB DoubleArg = dOUBLE_SIZE
227 cgRepSizeB LongArg = wORD64_SIZE
228 cgRepSizeB VoidArg = 0
229 cgRepSizeB _ = wORD_SIZE
231 cgRepSizeW :: CgRep -> ByteOff
232 cgRepSizeW DoubleArg = dOUBLE_SIZE `quot` wORD_SIZE
233 cgRepSizeW LongArg = wORD64_SIZE `quot` wORD_SIZE
234 cgRepSizeW VoidArg = 0
237 retAddrSizeW :: WordOff
238 retAddrSizeW = 1 -- One word
241 %************************************************************************
243 \subsubsection[SMRep-datatype]{@SMRep@---storage manager representation}
245 %************************************************************************
249 -- static closure have an extra static link field at the end.
250 = GenericRep -- GC routines consult sizes in info tbl
251 Bool -- True <=> This is a static closure. Affects how
252 -- we garbage-collect it
254 !Int -- # non-ptr words
255 ClosureType -- closure type
259 data ClosureType -- Corresponds 1-1 with the varieties of closures
260 -- implemented by the RTS. Compare with ghc/includes/ClosureTypes.h
268 Size of a closure header.
271 fixedHdrSize :: WordOff
272 fixedHdrSize = sTD_HDR_SIZE + profHdrSize + granHdrSize
274 profHdrSize :: WordOff
275 profHdrSize | opt_SccProfilingOn = pROF_HDR_SIZE
278 granHdrSize :: WordOff
279 granHdrSize | opt_GranMacros = gRAN_HDR_SIZE
282 arrWordsHdrSize :: ByteOff
283 arrWordsHdrSize = fixedHdrSize*wORD_SIZE + sIZEOF_StgArrWords_NoHdr
285 arrPtrsHdrSize :: ByteOff
286 arrPtrsHdrSize = fixedHdrSize*wORD_SIZE + sIZEOF_StgMutArrPtrs_NoHdr
290 -- IA64 mangler doesn't place tables next to code
291 tablesNextToCode :: Bool
292 #if defined(ia64_TARGET_ARCH) || defined(powerpc64_TARGET_ARCH)
293 tablesNextToCode = False
295 tablesNextToCode = not opt_Unregisterised
300 isStaticRep :: SMRep -> Bool
301 isStaticRep (GenericRep is_static _ _ _) = is_static
302 isStaticRep BlackHoleRep = False
306 #include "../includes/ClosureTypes.h"
307 -- Defines CONSTR, CONSTR_1_0 etc
310 smRepClosureType :: SMRep -> ClosureType
311 smRepClosureType (GenericRep _ _ _ ty) = ty
312 smRepClosureType BlackHoleRep = panic "smRepClosureType: black hole"
314 smRepClosureTypeInt :: SMRep -> Int
315 smRepClosureTypeInt (GenericRep False 1 0 Constr) = CONSTR_1_0
316 smRepClosureTypeInt (GenericRep False 0 1 Constr) = CONSTR_0_1
317 smRepClosureTypeInt (GenericRep False 2 0 Constr) = CONSTR_2_0
318 smRepClosureTypeInt (GenericRep False 1 1 Constr) = CONSTR_1_1
319 smRepClosureTypeInt (GenericRep False 0 2 Constr) = CONSTR_0_2
320 smRepClosureTypeInt (GenericRep False _ _ Constr) = CONSTR
322 smRepClosureTypeInt (GenericRep False 1 0 Fun) = FUN_1_0
323 smRepClosureTypeInt (GenericRep False 0 1 Fun) = FUN_0_1
324 smRepClosureTypeInt (GenericRep False 2 0 Fun) = FUN_2_0
325 smRepClosureTypeInt (GenericRep False 1 1 Fun) = FUN_1_1
326 smRepClosureTypeInt (GenericRep False 0 2 Fun) = FUN_0_2
327 smRepClosureTypeInt (GenericRep False _ _ Fun) = FUN
329 smRepClosureTypeInt (GenericRep False 1 0 Thunk) = THUNK_1_0
330 smRepClosureTypeInt (GenericRep False 0 1 Thunk) = THUNK_0_1
331 smRepClosureTypeInt (GenericRep False 2 0 Thunk) = THUNK_2_0
332 smRepClosureTypeInt (GenericRep False 1 1 Thunk) = THUNK_1_1
333 smRepClosureTypeInt (GenericRep False 0 2 Thunk) = THUNK_0_2
334 smRepClosureTypeInt (GenericRep False _ _ Thunk) = THUNK
336 smRepClosureTypeInt (GenericRep False _ _ ThunkSelector) = THUNK_SELECTOR
338 smRepClosureTypeInt (GenericRep True _ _ Constr) = CONSTR_STATIC
339 smRepClosureTypeInt (GenericRep True _ _ ConstrNoCaf) = CONSTR_NOCAF_STATIC
340 smRepClosureTypeInt (GenericRep True _ _ Fun) = FUN_STATIC
341 smRepClosureTypeInt (GenericRep True _ _ Thunk) = THUNK_STATIC
343 smRepClosureTypeInt BlackHoleRep = BLACKHOLE
345 smRepClosureTypeInt rep = panic "smRepClosuretypeint"
348 -- We export these ones
349 rET_SMALL = (RET_SMALL :: Int)
350 rET_VEC_SMALL = (RET_VEC_SMALL :: Int)
351 rET_BIG = (RET_BIG :: Int)
352 rET_VEC_BIG = (RET_VEC_BIG :: Int)