2 -- | An architecture independent description of a register.
3 -- This needs to stay architecture independent because it is used
4 -- by NCGMonad and the register allocators, which are shared
5 -- by all architectures.
12 isRealReg, takeRealReg,
13 isVirtualReg, takeVirtualReg,
18 getHiVirtualRegFromLo,
35 -- | An identifier for a primitive real machine register.
39 -- VirtualRegs are virtual registers. The register allocator will
40 -- eventually have to map them into RealRegs, or into spill slots.
42 -- VirtualRegs are allocated on the fly, usually to represent a single
43 -- value in the abstract assembly code (i.e. dynamic registers are
44 -- usually single assignment).
46 -- The single assignment restriction isn't necessary to get correct code,
47 -- although a better register allocation will result if single
48 -- assignment is used -- because the allocator maps a VirtualReg into
49 -- a single RealReg, even if the VirtualReg has multiple live ranges.
51 -- Virtual regs can be of either class, so that info is attached.
54 = VirtualRegI {-# UNPACK #-} !Unique
55 | VirtualRegHi {-# UNPACK #-} !Unique -- High part of 2-word register
56 | VirtualRegF {-# UNPACK #-} !Unique
57 | VirtualRegD {-# UNPACK #-} !Unique
58 | VirtualRegSSE {-# UNPACK #-} !Unique
59 deriving (Eq, Show, Ord)
61 instance Uniquable VirtualReg where
70 instance Outputable VirtualReg where
73 VirtualRegI u -> text "%vI_" <> pprUnique u
74 VirtualRegHi u -> text "%vHi_" <> pprUnique u
75 VirtualRegF u -> text "%vF_" <> pprUnique u
76 VirtualRegD u -> text "%vD_" <> pprUnique u
77 VirtualRegSSE u -> text "%vSSE_" <> pprUnique u
80 renameVirtualReg :: Unique -> VirtualReg -> VirtualReg
83 VirtualRegI _ -> VirtualRegI u
84 VirtualRegHi _ -> VirtualRegHi u
85 VirtualRegF _ -> VirtualRegF u
86 VirtualRegD _ -> VirtualRegD u
87 VirtualRegSSE _ -> VirtualRegSSE u
90 classOfVirtualReg :: VirtualReg -> RegClass
93 VirtualRegI{} -> RcInteger
94 VirtualRegHi{} -> RcInteger
95 VirtualRegF{} -> RcFloat
96 VirtualRegD{} -> RcDouble
97 VirtualRegSSE{} -> RcDoubleSSE
100 -- Determine the upper-half vreg for a 64-bit quantity on a 32-bit platform
101 -- when supplied with the vreg for the lower-half of the quantity.
102 -- (NB. Not reversible).
103 getHiVirtualRegFromLo :: VirtualReg -> VirtualReg
104 getHiVirtualRegFromLo reg
106 -- makes a pseudo-unique with tag 'H'
107 VirtualRegI u -> VirtualRegHi (newTagUnique u 'H')
108 _ -> panic "Reg.getHiVirtualRegFromLo"
110 getHiVRegFromLo :: Reg -> Reg
113 RegVirtual vr -> RegVirtual (getHiVirtualRegFromLo vr)
114 RegReal _ -> panic "Reg.getHiVRegFromLo"
117 ------------------------------------------------------------------------------------
118 -- | RealRegs are machine regs which are available for allocation, in
119 -- the usual way. We know what class they are, because that's part of
120 -- the processor's architecture.
122 -- RealRegPairs are pairs of real registers that are allocated together
123 -- to hold a larger value, such as with Double regs on SPARC.
126 = RealRegSingle {-# UNPACK #-} !RegNo
127 | RealRegPair {-# UNPACK #-} !RegNo {-# UNPACK #-} !RegNo
128 deriving (Eq, Show, Ord)
130 instance Uniquable RealReg where
133 RealRegSingle i -> mkRegSingleUnique i
134 RealRegPair r1 r2 -> mkRegPairUnique (r1 * 65536 + r2)
136 instance Outputable RealReg where
139 RealRegSingle i -> text "%r" <> int i
140 RealRegPair r1 r2 -> text "%r(" <> int r1 <> text "|" <> int r2 <> text ")"
142 regNosOfRealReg :: RealReg -> [RegNo]
145 RealRegSingle r1 -> [r1]
146 RealRegPair r1 r2 -> [r1, r2]
149 realRegsAlias :: RealReg -> RealReg -> Bool
150 realRegsAlias rr1 rr2
151 = not $ null $ intersect (regNosOfRealReg rr1) (regNosOfRealReg rr2)
153 --------------------------------------------------------------------------------
154 -- | A register, either virtual or real
156 = RegVirtual !VirtualReg
160 regSingle :: RegNo -> Reg
161 regSingle regNo = RegReal $ RealRegSingle regNo
163 regPair :: RegNo -> RegNo -> Reg
164 regPair regNo1 regNo2 = RegReal $ RealRegPair regNo1 regNo2
167 -- We like to have Uniques for Reg so that we can make UniqFM and UniqSets
168 -- in the register allocator.
169 instance Uniquable Reg where
172 RegVirtual vr -> getUnique vr
173 RegReal rr -> getUnique rr
175 -- | Print a reg in a generic manner
176 -- If you want the architecture specific names, then use the pprReg
177 -- function from the appropriate Ppr module.
178 instance Outputable Reg where
181 RegVirtual vr -> ppr vr
185 isRealReg :: Reg -> Bool
189 RegVirtual _ -> False
191 takeRealReg :: Reg -> Maybe RealReg
194 RegReal rr -> Just rr
198 isVirtualReg :: Reg -> Bool
204 takeVirtualReg :: Reg -> Maybe VirtualReg
208 RegVirtual vr -> Just vr
211 -- | The patch function supplied by the allocator maps VirtualReg to RealReg
212 -- regs, but sometimes we want to apply it to plain old Reg.
214 liftPatchFnToRegReg :: (VirtualReg -> RealReg) -> (Reg -> Reg)
215 liftPatchFnToRegReg patchF reg
217 RegVirtual vr -> RegReal (patchF vr)