FreeRegs.hs 1.13 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

import Data.Word
import Data.Bits

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

noFreeRegs :: FreeRegs
noFreeRegs = 0

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

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

initFreeRegs :: FreeRegs
32 33
initFreeRegs 
	= foldr releaseReg noFreeRegs allocatableRegs
34

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

  where go 0 _ = []
        go n m 
40 41
	  | n .&. 1 /= 0 && classOfRealReg (RealRegSingle m) == cls
	  = RealRegSingle m : (go (n `shiftR` 1) $! (m+1))
42 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
allocateReg (RealRegSingle r) f 
50
        = f .&. complement (1 `shiftL` r)
51 52 53

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