FreeRegs.hs 1.28 KB
Newer Older
1 2 3 4 5

-- | Free regs map for i386 and x86_64
module RegAlloc.Linear.X86.FreeRegs
where

6 7 8
import X86.Regs
import RegClass
import Reg
9
import Panic
10
import Platform
11 12 13 14

import Data.Word
import Data.Bits

15
type FreeRegs
16
#ifdef i386_TARGET_ARCH
17
        = Word32
18
#else
19
        = Word64
20
#endif
21 22 23 24

noFreeRegs :: FreeRegs
noFreeRegs = 0

25
releaseReg :: RealReg -> FreeRegs -> FreeRegs
26 27
releaseReg (RealRegSingle n) f
        = f .|. (1 `shiftL` n)
28

29 30
releaseReg _ _
        = panic "RegAlloc.Linear.X86.FreeRegs.realeaseReg: no reg"
31

32 33
initFreeRegs :: Platform -> FreeRegs
initFreeRegs platform
34
        = foldr releaseReg noFreeRegs (allocatableRegs platform)
35

36 37
getFreeRegs :: Platform -> RegClass -> FreeRegs -> [RealReg] -- lazilly
getFreeRegs platform cls f = go f 0
38 39

  where go 0 _ = []
40 41 42
        go n m
          | n .&. 1 /= 0 && classOfRealReg platform (RealRegSingle m) == cls
          = RealRegSingle m : (go (n `shiftR` 1) $! (m+1))
43

44 45 46 47
          | otherwise
          = go (n `shiftR` 1) $! (m+1)
        -- ToDo: there's no point looking through all the integer registers
        -- in order to find a floating-point one.
48

49
allocateReg :: RealReg -> FreeRegs -> FreeRegs
50
allocateReg (RealRegSingle r) f
51
        = f .&. complement (1 `shiftL` r)
52 53

allocateReg _ _
54
        = panic "RegAlloc.Linear.X86.FreeRegs.allocateReg: no reg"
55 56