2 -- | A description of the register set of the X86.
3 -- This isn't used directly in GHC proper.
5 -- See RegArchBase.hs for the reference.
6 -- See MachRegs.hs for the actual trivColorable function used in GHC.
18 import RegArchBase (Reg(..), RegSub(..), RegClass(..))
22 -- | Determine the class of a register
23 classOfReg :: Reg -> RegClass
28 RegSub SubL16 _ -> ClassG16
29 RegSub SubL8 _ -> ClassG8
30 RegSub SubL8H _ -> ClassG8
33 -- | Determine all the regs that make up a certain class.
35 regsOfClass :: RegClass -> UniqSet Reg
39 -> mkUniqSet [ Reg ClassG32 i | i <- [0..7] ]
42 -> mkUniqSet [ RegSub SubL16 (Reg ClassG32 i) | i <- [0..7] ]
46 (mkUniqSet [ RegSub SubL8 (Reg ClassG32 i) | i <- [0..3] ])
47 (mkUniqSet [ RegSub SubL8H (Reg ClassG32 i) | i <- [0..3] ])
50 -> mkUniqSet [ Reg ClassF64 i | i <- [0..5] ]
53 -- | Determine the common name of a reg
54 -- returns Nothing if this reg is not part of the machine.
56 regName :: Reg -> Maybe String
60 | i <= 7 -> Just ([ "eax", "ebx", "ecx", "edx", "ebp", "esi", "edi", "esp" ] !! i)
62 RegSub SubL16 (Reg ClassG32 i)
63 | i <= 7 -> Just ([ "ax", "bx", "cx", "dx", "bp", "si", "di", "sp"] !! i)
65 RegSub SubL8 (Reg ClassG32 i)
66 | i <= 3 -> Just ([ "al", "bl", "cl", "dl"] !! i)
68 RegSub SubL8H (Reg ClassG32 i)
69 | i <= 3 -> Just ([ "ah", "bh", "ch", "dh"] !! i)
74 -- | Which regs alias what other regs
75 regAlias :: Reg -> UniqSet Reg
79 -- 32 bit regs alias all of the subregs
82 -- for eax, ebx, ecx, eds
84 -> mkUniqSet $ [ Reg ClassG32 i, RegSub SubL16 reg, RegSub SubL8 reg, RegSub SubL8H reg ]
86 -- for esi, edi, esp, ebp
88 -> mkUniqSet $ [ Reg ClassG32 i, RegSub SubL16 reg ]
91 -- 16 bit subregs alias the whole reg
92 RegSub SubL16 r@(Reg ClassG32 _)
95 -- 8 bit subregs alias the 32 and 16, but not the other 8 bit subreg
96 RegSub SubL8 r@(Reg ClassG32 _)
97 -> mkUniqSet $ [ r, RegSub SubL16 r, RegSub SubL8 r ]
99 RegSub SubL8H r@(Reg ClassG32 _)
100 -> mkUniqSet $ [ r, RegSub SubL16 r, RegSub SubL8H r ]
106 _ -> error "regAlias: invalid register"
109 -- | Optimised versions of RegColorBase.{worst, squeese} specific to x86
111 worst :: Int -> RegClass -> RegClass -> Int
112 worst n classN classC
130 ClassG32 -> min (n*2) 8
131 ClassG16 -> min (n*2) 8
140 squeese :: RegClass -> [(Int, RegClass)] -> Int
141 squeese classN countCs
142 = sum (map (\(i, classC) -> worst i classN classC) countCs)