Fix scoped type variables for expression type signatures
[ghc-hetmet.git] / compiler / codeGen / Bitmap.hs
1 --
2 -- (c) The University of Glasgow 2003
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 import DATA_BITS
21
22 {-|
23 A bitmap represented by a sequence of 'StgWord's on the /target/
24 architecture.  These are used for bitmaps in info tables and other
25 generated code which need to be emitted as sequences of StgWords.
26 -}
27 type Bitmap = [StgWord]
28
29 -- | Make a bitmap from a sequence of bits
30 mkBitmap :: [Bool] -> Bitmap
31 mkBitmap [] = []
32 mkBitmap stuff = chunkToBitmap chunk : mkBitmap rest
33   where (chunk, rest) = splitAt wORD_SIZE_IN_BITS stuff
34
35 chunkToBitmap :: [Bool] -> StgWord
36 chunkToBitmap chunk = 
37   foldr (.|.) 0 [ 1 `shiftL` n | (True,n) <- zip chunk [0..] ]
38
39 -- | Make a bitmap where the slots specified are the /ones/ in the bitmap.
40 -- eg. @[1,2,4], size 4 ==> 0xb@.
41 --
42 -- The list of @Int@s /must/ be already sorted.
43 intsToBitmap :: Int -> [Int] -> Bitmap
44 intsToBitmap size slots{- must be sorted -}
45   | size <= 0 = []
46   | otherwise = 
47     (foldr (.|.) 0 (map (1 `shiftL`) these)) : 
48         intsToBitmap (size - wORD_SIZE_IN_BITS) 
49              (map (\x -> x - wORD_SIZE_IN_BITS) rest)
50    where (these,rest) = span (<wORD_SIZE_IN_BITS) slots
51
52 -- | Make a bitmap where the slots specified are the /zeros/ in the bitmap.
53 -- eg. @[1,2,4], size 4 ==> 0x8@  (we leave any bits outside the size as zero,
54 -- just to make the bitmap easier to read).
55 --
56 -- The list of @Int@s /must/ be already sorted.
57 intsToReverseBitmap :: Int -> [Int] -> Bitmap
58 intsToReverseBitmap size slots{- must be sorted -}
59   | size <= 0 = []
60   | otherwise = 
61     (foldr xor init (map (1 `shiftL`) these)) : 
62         intsToReverseBitmap (size - wORD_SIZE_IN_BITS) 
63              (map (\x -> x - wORD_SIZE_IN_BITS) rest)
64    where (these,rest) = span (<wORD_SIZE_IN_BITS) slots
65          init
66            | size >= wORD_SIZE_IN_BITS = complement 0
67            | otherwise                 = (1 `shiftL` size) - 1
68
69 {- |
70 Magic number, must agree with @BITMAP_BITS_SHIFT@ in InfoTables.h.
71 Some kinds of bitmap pack a size\/bitmap into a single word if
72 possible, or fall back to an external pointer when the bitmap is too
73 large.  This value represents the largest size of bitmap that can be
74 packed into a single word.
75 -}
76 mAX_SMALL_BITMAP_SIZE :: Int
77 mAX_SMALL_BITMAP_SIZE  | wORD_SIZE == 4 = 27
78                        | otherwise      = 58
79