SMRep.lhs 13 KB
Newer Older
1
%
Simon Marlow's avatar
Simon Marlow committed
2
% (c) The University of Glasgow 2006
3
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
%
Simon Marlow's avatar
Simon Marlow committed
5 6

Storage manager representation of closures
7 8 9 10 11 12

This is here, rather than in ClosureInfo, just to keep nhc happy.
Other modules should access this info through ClosureInfo.

\begin{code}
module SMRep (
Simon Marlow's avatar
Simon Marlow committed
13
        -- * Words and bytes
14 15 16
        StgWord, StgHalfWord,
        hALF_WORD_SIZE, hALF_WORD_SIZE_IN_BITS,
        WordOff, ByteOff,
17
        roundUpToWords,
18

Simon Marlow's avatar
Simon Marlow committed
19
        -- * Closure repesentation
20 21
        SMRep(..), -- CmmInfo sees the rep; no one else does
        IsStatic,
22
        ClosureTypeInfo(..), ArgDescr(..), Liveness,
23
        ConstrDescription,
Simon Marlow's avatar
Simon Marlow committed
24 25

        -- ** Construction
26
        mkHeapRep, blackHoleRep, mkStackRep, mkRTSRep,
27

Simon Marlow's avatar
Simon Marlow committed
28
        -- ** Predicates
Simon Marlow's avatar
Simon Marlow committed
29
        isStaticRep, isConRep, isThunkRep, isFunRep, isStaticNoCafCon,
Simon Marlow's avatar
Simon Marlow committed
30 31

        -- ** Size-related things
32 33 34 35
        heapClosureSize,
        fixedHdrSize, arrWordsHdrSize, arrPtrsHdrSize,
        profHdrSize, thunkHdrSize, nonHdrSize,

Simon Marlow's avatar
Simon Marlow committed
36 37
        -- ** RTS closure types
        rtsClosureType, rET_SMALL, rET_BIG,
38 39
        aRG_GEN, aRG_GEN_BIG,

Simon Marlow's avatar
Simon Marlow committed
40
        -- * Operations over [Word8] strings that don't belong here
41
        pprWord8String, stringToWord8s
42 43
    ) where

44
#include "../HsVersions.h"
45
#include "../includes/MachDeps.h"
46

Simon Marlow's avatar
Simon Marlow committed
47
import StaticFlags
48
import Constants
sof's avatar
sof committed
49
import Outputable
50
import FastString
51

52
import Data.Char( ord )
Simon Marlow's avatar
Simon Marlow committed
53
import Data.Word
54
import Data.Bits
55 56
\end{code}

57 58

%************************************************************************
59 60 61
%*                                                                      *
                Words and bytes
%*                                                                      *
62 63 64
%************************************************************************

\begin{code}
65 66
type WordOff = Int -- Word offset, or word count
type ByteOff = Int -- Byte offset, or byte count
67 68 69

roundUpToWords :: ByteOff -> ByteOff
roundUpToWords n = (n + (wORD_SIZE - 1)) .&. (complement (wORD_SIZE - 1))
70 71 72 73 74 75 76 77
\end{code}

StgWord is a type representing an StgWord on the target platform.

\begin{code}
#if SIZEOF_HSWORD == 4
type StgWord     = Word32
type StgHalfWord = Word16
Ian Lynagh's avatar
Ian Lynagh committed
78 79 80 81
hALF_WORD_SIZE :: ByteOff
hALF_WORD_SIZE = 2
hALF_WORD_SIZE_IN_BITS :: Int
hALF_WORD_SIZE_IN_BITS = 16
82 83 84
#elif SIZEOF_HSWORD == 8
type StgWord     = Word64
type StgHalfWord = Word32
Ian Lynagh's avatar
Ian Lynagh committed
85 86 87 88
hALF_WORD_SIZE :: ByteOff
hALF_WORD_SIZE = 4
hALF_WORD_SIZE_IN_BITS :: Int
hALF_WORD_SIZE_IN_BITS = 32
89 90 91 92 93
#else
#error unknown SIZEOF_HSWORD
#endif
\end{code}

94

95
%************************************************************************
96
%*                                                                      *
97
\subsubsection[SMRep-datatype]{@SMRep@---storage manager representation}
98
%*                                                                      *
99 100 101
%************************************************************************

\begin{code}
102 103
-- | A description of the layout of a closure.  Corresponds directly
-- to the closure types in includes/rts/storage/ClosureTypes.h.
104
data SMRep
105 106 107 108 109 110 111 112 113
  = HeapRep              -- GC routines consult sizes in info tbl
        IsStatic
        !WordOff         --  # ptr words
        !WordOff         --  # non-ptr words INCLUDING SLOP (see mkHeapRep below)
        ClosureTypeInfo  -- type-specific info

  | StackRep            -- Stack frame (RET_SMALL or RET_BIG)
        Liveness

114 115 116 117
  | RTSRep              -- The RTS needs to declare info tables with specific
        StgHalfWord     -- type tags, so this form lets us override the default
        SMRep           -- tag for an SMRep.

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
-- | True <=> This is a static closure.  Affects how we garbage-collect it.
-- Static closure have an extra static link field at the end.
type IsStatic = Bool

-- From an SMRep you can get to the closure type defined in
-- includes/rts/storage/ClosureTypes.h. Described by the function
-- rtsClosureType below.

data ClosureTypeInfo
  = Constr        ConstrTag ConstrDescription
  | Fun           FunArity ArgDescr
  | Thunk
  | ThunkSelector SelectorOffset
  | BlackHole

type ConstrTag         = StgHalfWord
type ConstrDescription = [Word8] -- result of dataConIdentity
type FunArity          = StgHalfWord
type SelectorOffset    = StgWord

-------------------------
-- We represent liveness bitmaps as a Bitmap (whose internal
-- representation really is a bitmap).  These are pinned onto case return
-- vectors to indicate the state of the stack for the garbage collector.
142
--
143 144
-- In the compiled program, liveness bitmaps that fit inside a single
-- word (StgWord) are stored as a single word, while larger bitmaps are
145
-- stored as a pointer to an array of words.
146 147 148 149 150 151 152 153

type Liveness = [Bool]   -- One Bool per word; True  <=> non-ptr or dead
                         --                    False <=> ptr

-------------------------
-- An ArgDescr describes the argument pattern of a function

data ArgDescr
154 155
  = ArgSpec             -- Fits one of the standard patterns
        !StgHalfWord    -- RTS type identifier ARG_P, ARG_N, ...
156

157 158
  | ArgGen              -- General case
        Liveness        -- Details about the arguments
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173


-----------------------------------------------------------------------------
-- Construction

mkHeapRep :: IsStatic -> WordOff -> WordOff -> ClosureTypeInfo -> SMRep
mkHeapRep is_static ptr_wds nonptr_wds cl_type_info
  = HeapRep is_static
            ptr_wds
            (nonptr_wds + slop_wds)
            cl_type_info
  where
     slop_wds
      | is_static = 0
      | otherwise = max 0 (minClosureSize - (hdr_size + payload_size))
174

175 176
     hdr_size     = closureTypeHdrSize cl_type_info
     payload_size = ptr_wds + nonptr_wds
177

178 179
mkRTSRep :: StgHalfWord -> SMRep -> SMRep
mkRTSRep = RTSRep
180 181

mkStackRep :: [Bool] -> SMRep
182
mkStackRep liveness = StackRep liveness
183 184 185 186

blackHoleRep :: SMRep
blackHoleRep = HeapRep False 0 0 BlackHole

Simon Marlow's avatar
Simon Marlow committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
-----------------------------------------------------------------------------
-- Predicates

isStaticRep :: SMRep -> IsStatic
isStaticRep (HeapRep is_static _ _ _) = is_static
isStaticRep (StackRep {})             = False
isStaticRep (RTSRep _ rep)            = isStaticRep rep

isConRep :: SMRep -> Bool
isConRep (HeapRep _ _ _ Constr{}) = True
isConRep _                        = False

isThunkRep :: SMRep -> Bool
isThunkRep (HeapRep _ _ _ Thunk{})         = True
isThunkRep (HeapRep _ _ _ ThunkSelector{}) = True
isThunkRep (HeapRep _ _ _ BlackHole{})     = True
isThunkRep _                               = False

Simon Marlow's avatar
Simon Marlow committed
205 206 207 208
isFunRep :: SMRep -> Bool
isFunRep (HeapRep _ _ _ Fun{}) = True
isFunRep _                     = False

Simon Marlow's avatar
Simon Marlow committed
209 210 211 212 213 214 215
isStaticNoCafCon :: SMRep -> Bool
-- This should line up exactly with CONSTR_NOCAF_STATIC above
-- See Note [Static NoCaf constructors]
isStaticNoCafCon (HeapRep True 0 _ Constr{}) = True
isStaticNoCafCon _                           = False


216 217 218 219
-----------------------------------------------------------------------------
-- Size-related things

-- | Size of a closure header (StgHeader in includes/rts/storage/Closures.h)
220
fixedHdrSize :: WordOff
Simon Marlow's avatar
Simon Marlow committed
221
fixedHdrSize = sTD_HDR_SIZE + profHdrSize
222

223 224
-- | Size of the profiling part of a closure header
-- (StgProfHeader in includes/rts/storage/Closures.h)
225
profHdrSize  :: WordOff
226
profHdrSize  | opt_SccProfilingOn   = pROF_HDR_SIZE
227
             | otherwise            = 0
228

229 230 231 232
-- | The garbage collector requires that every closure is at least as big as this.
minClosureSize :: WordOff
minClosureSize = fixedHdrSize + mIN_PAYLOAD_SIZE

233 234
arrWordsHdrSize   :: ByteOff
arrWordsHdrSize   = fixedHdrSize*wORD_SIZE + sIZEOF_StgArrWords_NoHdr
235

236 237
arrPtrsHdrSize    :: ByteOff
arrPtrsHdrSize    = fixedHdrSize*wORD_SIZE + sIZEOF_StgMutArrPtrs_NoHdr
238

239
-- Thunks have an extra header word on SMP, so the update doesn't
240 241
-- splat the payload.
thunkHdrSize :: WordOff
242
thunkHdrSize = fixedHdrSize + smp_hdr
243
        where smp_hdr = sIZEOF_StgSMPThunkHeader `quot` wORD_SIZE
244

245

246 247 248
nonHdrSize :: SMRep -> WordOff
nonHdrSize (HeapRep _ p np _) = p + np
nonHdrSize (StackRep bs)      = length bs
249
nonHdrSize (RTSRep _ rep)     = nonHdrSize rep
250

251 252 253
heapClosureSize :: SMRep -> WordOff
heapClosureSize (HeapRep _ p np ty) = closureTypeHdrSize ty + p + np
heapClosureSize _ = panic "SMRep.heapClosureSize"
254

255 256 257 258 259 260
closureTypeHdrSize :: ClosureTypeInfo -> WordOff
closureTypeHdrSize ty = case ty of
                  Thunk{}         -> thunkHdrSize
                  ThunkSelector{} -> thunkHdrSize
                  BlackHole{}     -> thunkHdrSize
                  _               -> fixedHdrSize
261 262 263 264 265
        -- All thunks use thunkHdrSize, even if they are non-updatable.
        -- this is because we don't have separate closure types for
        -- updatable vs. non-updatable thunks, so the GC can't tell the
        -- difference.  If we ever have significant numbers of non-
        -- updatable thunks, it might be worth fixing this.
266

267 268
-----------------------------------------------------------------------------
-- deriving the RTS closure type from an SMRep
269

270 271 272
#include "../includes/rts/storage/ClosureTypes.h"
#include "../includes/rts/storage/FunTypes.h"
-- Defines CONSTR, CONSTR_1_0 etc
273

274 275
-- | Derives the RTS closure type from an 'SMRep'
rtsClosureType :: SMRep -> StgHalfWord
276 277
rtsClosureType (RTSRep ty _)  = ty

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
rtsClosureType (HeapRep False 1 0 Constr{}) = CONSTR_1_0
rtsClosureType (HeapRep False 0 1 Constr{}) = CONSTR_0_1
rtsClosureType (HeapRep False 2 0 Constr{}) = CONSTR_2_0
rtsClosureType (HeapRep False 1 1 Constr{}) = CONSTR_1_1
rtsClosureType (HeapRep False 0 2 Constr{}) = CONSTR_0_2
rtsClosureType (HeapRep False _ _ Constr{}) = CONSTR

rtsClosureType (HeapRep False 1 0 Fun{}) = FUN_1_0
rtsClosureType (HeapRep False 0 1 Fun{}) = FUN_0_1
rtsClosureType (HeapRep False 2 0 Fun{}) = FUN_2_0
rtsClosureType (HeapRep False 1 1 Fun{}) = FUN_1_1
rtsClosureType (HeapRep False 0 2 Fun{}) = FUN_0_2
rtsClosureType (HeapRep False _ _ Fun{}) = FUN

rtsClosureType (HeapRep False 1 0 Thunk{}) = THUNK_1_0
rtsClosureType (HeapRep False 0 1 Thunk{}) = THUNK_0_1
rtsClosureType (HeapRep False 2 0 Thunk{}) = THUNK_2_0
rtsClosureType (HeapRep False 1 1 Thunk{}) = THUNK_1_1
rtsClosureType (HeapRep False 0 2 Thunk{}) = THUNK_0_2
rtsClosureType (HeapRep False _ _ Thunk{}) = THUNK

rtsClosureType (HeapRep False _ _ ThunkSelector{}) =  THUNK_SELECTOR

-- Approximation: we use the CONSTR_NOCAF_STATIC type for static constructors
-- that have no pointer words only.
rtsClosureType (HeapRep True 0 _ Constr{}) = CONSTR_NOCAF_STATIC  -- See isStaticNoCafCon below
rtsClosureType (HeapRep True _ _ Constr{}) = CONSTR_STATIC
rtsClosureType (HeapRep True _ _ Fun{})    = FUN_STATIC
rtsClosureType (HeapRep True _ _ Thunk{})  = THUNK_STATIC

rtsClosureType (HeapRep False _ _ BlackHole{}) =  BLACKHOLE

rtsClosureType _ = panic "rtsClosureType"

-- We export these ones
rET_SMALL, rET_BIG, aRG_GEN, aRG_GEN_BIG :: StgHalfWord
rET_SMALL   = RET_SMALL
rET_BIG     = RET_BIG
aRG_GEN     = ARG_GEN
aRG_GEN_BIG = ARG_GEN_BIG
\end{code}
319

320 321
Note [Static NoCaf constructors]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
322
If we know that a top-level binding 'x' is not Caffy (ie no CAFs are
323 324 325 326
reachable from 'x'), then a statically allocated constructor (Just x)
is also not Caffy, and the garbage collector need not follow its
argument fields.  Exploiting this would require two static info tables
for Just, for the two cases where the argument was Caffy or non-Caffy.
327

328
Currently we don't do this; instead we treat nullary constructors
329
as non-Caffy, and the others as potentially Caffy.
330

331

332
%************************************************************************
333
%*                                                                      *
334
             Pretty printing of SMRep and friends
335
%*                                                                      *
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
%************************************************************************

\begin{code}
instance Outputable ClosureTypeInfo where
   ppr = pprTypeInfo

instance Outputable SMRep where
   ppr (HeapRep static ps nps tyinfo)
     = hang (header <+> lbrace) 2 (ppr tyinfo <+> rbrace)
     where
       header = ptext (sLit "HeapRep")
                <+> if static then ptext (sLit "static") else empty
                <+> pp_n "ptrs" ps <+> pp_n "nonptrs" nps
       pp_n :: String -> Int -> SDoc
       pp_n _ 0 = empty
       pp_n s n = int n <+> text s

   ppr (StackRep bs) = ptext (sLit "StackRep") <+> ppr bs

355 356
   ppr (RTSRep ty rep) = ptext (sLit "tag:") <> ppr ty <+> ppr rep

357 358 359
instance Outputable ArgDescr where
  ppr (ArgSpec n) = ptext (sLit "ArgSpec") <+> integer (toInteger n)
  ppr (ArgGen ls) = ptext (sLit "ArgGen") <+> ppr ls
360

361 362
pprTypeInfo :: ClosureTypeInfo -> SDoc
pprTypeInfo (Constr tag descr)
363
  = ptext (sLit "Con") <+>
364 365 366 367
    braces (sep [ ptext (sLit "tag:") <+> integer (toInteger tag)
                , ptext (sLit "descr:") <> text (show descr) ])

pprTypeInfo (Fun arity args)
368
  = ptext (sLit "Fun") <+>
369 370 371
    braces (sep [ ptext (sLit "arity:") <+> integer (toInteger arity)
                , ptext (sLit ("fun_type:")) <+> ppr args ])

372
pprTypeInfo (ThunkSelector offset)
373 374 375 376 377
  = ptext (sLit "ThunkSel") <+> integer (toInteger offset)

pprTypeInfo Thunk     = ptext (sLit "Thunk")
pprTypeInfo BlackHole = ptext (sLit "BlackHole")

Simon Marlow's avatar
Simon Marlow committed
378
-- XXX Does not belong here!!
379 380 381 382 383 384 385
stringToWord8s :: String -> [Word8]
stringToWord8s s = map (fromIntegral . ord) s

pprWord8String :: [Word8] -> SDoc
-- Debug printing.  Not very clever right now.
pprWord8String ws = text (show ws)
\end{code}