Add several new record features
[ghc-hetmet.git] / compiler / codeGen / Bitmap.hs
1 --
2 -- (c) The University of Glasgow 2003-2006
3 -- 
4
5 -- Functions for constructing bitmaps, which are used in various
6 -- places in generated code (stack frame liveness masks, function
7 -- argument liveness masks, SRT bitmaps).
8
9 module Bitmap ( 
10         Bitmap, mkBitmap,
11         intsToBitmap, intsToReverseBitmap,
12         mAX_SMALL_BITMAP_SIZE
13   ) where
14
15 #include "HsVersions.h"
16 #include "../includes/MachDeps.h"
17
18 import SMRep
19 import Constants
20
21 import Data.Bits
22
23 {-|
24 A bitmap represented by a sequence of 'StgWord's on the /target/
25 architecture.  These are used for bitmaps in info tables and other
26 generated code which need to be emitted as sequences of StgWords.
27 -}
28 type Bitmap = [StgWord]
29
30 -- | Make a bitmap from a sequence of bits
31 mkBitmap :: [Bool] -> Bitmap
32 mkBitmap [] = []
33 mkBitmap stuff = chunkToBitmap chunk : mkBitmap rest
34   where (chunk, rest) = splitAt wORD_SIZE_IN_BITS stuff
35
36 chunkToBitmap :: [Bool] -> StgWord
37 chunkToBitmap chunk = 
38   foldr (.|.) 0 [ 1 `shiftL` n | (True,n) <- zip chunk [0..] ]
39
40 -- | Make a bitmap where the slots specified are the /ones/ in the bitmap.
41 -- eg. @[1,2,4], size 4 ==> 0xb@.
42 --
43 -- The list of @Int@s /must/ be already sorted.
44 intsToBitmap :: Int -> [Int] -> Bitmap
45 intsToBitmap size slots{- must be sorted -}
46   | size <= 0 = []
47   | otherwise = 
48     (foldr (.|.) 0 (map (1 `shiftL`) these)) : 
49         intsToBitmap (size - wORD_SIZE_IN_BITS) 
50              (map (\x -> x - wORD_SIZE_IN_BITS) rest)
51    where (these,rest) = span (<wORD_SIZE_IN_BITS) slots
52
53 -- | Make a bitmap where the slots specified are the /zeros/ in the bitmap.
54 -- eg. @[1,2,4], size 4 ==> 0x8@  (we leave any bits outside the size as zero,
55 -- just to make the bitmap easier to read).
56 --
57 -- The list of @Int@s /must/ be already sorted.
58 intsToReverseBitmap :: Int -> [Int] -> Bitmap
59 intsToReverseBitmap size slots{- must be sorted -}
60   | size <= 0 = []
61   | otherwise = 
62     (foldr xor init (map (1 `shiftL`) these)) : 
63         intsToReverseBitmap (size - wORD_SIZE_IN_BITS) 
64              (map (\x -> x - wORD_SIZE_IN_BITS) rest)
65    where (these,rest) = span (<wORD_SIZE_IN_BITS) slots
66          init
67            | size >= wORD_SIZE_IN_BITS = complement 0
68            | otherwise                 = (1 `shiftL` size) - 1
69
70 {- |
71 Magic number, must agree with @BITMAP_BITS_SHIFT@ in InfoTables.h.
72 Some kinds of bitmap pack a size\/bitmap into a single word if
73 possible, or fall back to an external pointer when the bitmap is too
74 large.  This value represents the largest size of bitmap that can be
75 packed into a single word.
76 -}
77 mAX_SMALL_BITMAP_SIZE :: Int
78 mAX_SMALL_BITMAP_SIZE  | wORD_SIZE == 4 = 27
79                        | otherwise      = 58
80