FreeRegs.hs 1.09 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 11 12 13 14 15 16 17 18 19

import Data.Word
import Data.Bits

type FreeRegs 
	= Word32

noFreeRegs :: FreeRegs
noFreeRegs = 0

20 21 22 23 24 25
releaseReg :: RealReg -> FreeRegs -> FreeRegs
releaseReg (RealRegSingle n) f 
	= f .|. (1 `shiftL` n)

releaseReg _ _	
	= panic "RegAlloc.Linear.X86.FreeRegs.realeaseReg: no reg"
26 27

initFreeRegs :: FreeRegs
28 29
initFreeRegs 
	= foldr releaseReg noFreeRegs allocatableRegs
30

31
getFreeRegs :: RegClass -> FreeRegs -> [RealReg]	-- lazilly
32 33 34 35
getFreeRegs cls f = go f 0

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

	  | 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.

44 45 46 47 48 49
allocateReg :: RealReg -> FreeRegs -> FreeRegs
allocateReg (RealRegSingle r) f 
	= f .&. complement (1 `shiftL` fromIntegral r)

allocateReg _ _
	= panic "RegAlloc.Linear.X86.FreeRegs.allocateReg: no reg"
50 51