6d8809df527e0760cf18bf7b011fd640f51dd97b
[ghc-hetmet.git] / compiler / nativeGen / RegAlloc / Linear / PPC / FreeRegs.hs
1
2 -- | Free regs map for PowerPC
3 module RegAlloc.Linear.PPC.FreeRegs
4 where
5
6 import Regs
7
8 import Outputable
9
10 import Data.Word
11 import Data.Bits
12 import Data.List
13
14 -- The PowerPC has 32 integer and 32 floating point registers.
15 -- This is 32bit PowerPC, so Word64 is inefficient - two Word32s are much
16 -- better.
17 -- Note that when getFreeRegs scans for free registers, it starts at register
18 -- 31 and counts down. This is a hack for the PowerPC - the higher-numbered
19 -- registers are callee-saves, while the lower regs are caller-saves, so it
20 -- makes sense to start at the high end.
21 -- Apart from that, the code does nothing PowerPC-specific, so feel free to
22 -- add your favourite platform to the #if (if you have 64 registers but only
23 -- 32-bit words).
24
25 data FreeRegs = FreeRegs !Word32 !Word32
26               deriving( Show )  -- The Show is used in an ASSERT
27
28 noFreeRegs :: FreeRegs
29 noFreeRegs = FreeRegs 0 0
30
31 releaseReg :: RegNo -> FreeRegs -> FreeRegs
32 releaseReg r (FreeRegs g f)
33     | r > 31    = FreeRegs g (f .|. (1 `shiftL` (fromIntegral r - 32)))
34     | otherwise = FreeRegs (g .|. (1 `shiftL` fromIntegral r)) f
35     
36 initFreeRegs :: FreeRegs
37 initFreeRegs = foldr releaseReg noFreeRegs allocatableRegs
38
39 getFreeRegs :: RegClass -> FreeRegs -> [RegNo]  -- lazilly
40 getFreeRegs cls (FreeRegs g f)
41     | RcDouble <- cls = go f (0x80000000) 63
42     | RcInteger <- cls = go g (0x80000000) 31
43     | otherwise = pprPanic "RegAllocLinear.getFreeRegs: Bad register class" (ppr cls)
44     where
45         go _ 0 _ = []
46         go x m i | x .&. m /= 0 = i : (go x (m `shiftR` 1) $! i-1)
47                  | otherwise    = go x (m `shiftR` 1) $! i-1
48
49 allocateReg :: RegNo -> FreeRegs -> FreeRegs
50 allocateReg r (FreeRegs g f) 
51     | r > 31    = FreeRegs g (f .&. complement (1 `shiftL` (fromIntegral r - 32)))
52     | otherwise = FreeRegs (g .&. complement (1 `shiftL` fromIntegral r)) f
53
54