1 -- -----------------------------------------------------------------------------
3 -- (c) The University of Glasgow 1994-2004
5 -- -----------------------------------------------------------------------------
15 -- machine specific info
16 gReg, iReg, lReg, oReg, fReg,
17 fp, sp, g0, g1, g2, o0, o1, f0, f1, f6, f8, f22, f26, f27,
48 The SPARC has 64 registers of interest; 32 integer registers and 32
49 floating point registers. The mapping of STG registers to SPARC
50 machine registers is defined in StgRegs.h. We are, of course,
51 prepared for any eventuality.
53 The whole fp-register pairing thing on sparcs is a huge nuisance. See
54 includes/stg/MachRegs.h for a description of what's going on
59 -- | Get the standard name for the register with this number.
60 showReg :: RegNo -> String
62 | n >= 0 && n < 8 = "%g" ++ show n
63 | n >= 8 && n < 16 = "%o" ++ show (n-8)
64 | n >= 16 && n < 24 = "%l" ++ show (n-16)
65 | n >= 24 && n < 32 = "%i" ++ show (n-24)
66 | n >= 32 && n < 64 = "%f" ++ show (n-32)
67 | otherwise = panic "SPARC.Regs.showReg: unknown sparc register"
70 -- Get the register class of a certain real reg
71 classOfRealReg :: RealReg -> RegClass
76 | otherwise -> RcFloat
78 RealRegPair{} -> RcDouble
81 -- | regSqueeze_class reg
82 -- Calculuate the maximum number of register colors that could be
83 -- denied to a node of this class due to having this reg
86 {-# INLINE virtualRegSqueeze #-}
87 virtualRegSqueeze :: RegClass -> VirtualReg -> FastInt
89 virtualRegSqueeze cls vr
93 VirtualRegI{} -> _ILIT(1)
94 VirtualRegHi{} -> _ILIT(1)
99 VirtualRegF{} -> _ILIT(1)
100 VirtualRegD{} -> _ILIT(2)
105 VirtualRegF{} -> _ILIT(1)
106 VirtualRegD{} -> _ILIT(1)
111 {-# INLINE realRegSqueeze #-}
112 realRegSqueeze :: RegClass -> RealReg -> FastInt
114 realRegSqueeze cls rr
119 | regNo < 32 -> _ILIT(1)
120 | otherwise -> _ILIT(0)
122 RealRegPair{} -> _ILIT(0)
127 | regNo < 32 -> _ILIT(0)
128 | otherwise -> _ILIT(1)
130 RealRegPair{} -> _ILIT(2)
135 | regNo < 32 -> _ILIT(0)
136 | otherwise -> _ILIT(1)
138 RealRegPair{} -> _ILIT(1)
142 -- | All the allocatable registers in the machine,
143 -- including register pairs.
144 allRealRegs :: [RealReg]
146 = [ (RealRegSingle i) | i <- [0..63] ]
147 ++ [ (RealRegPair i (i+1)) | i <- [32, 34 .. 62 ] ]
150 -- | Get the regno for this sort of reg
151 gReg, lReg, iReg, oReg, fReg :: Int -> RegNo
153 gReg x = x -- global regs
154 oReg x = (8 + x) -- output regs
155 lReg x = (16 + x) -- local regs
156 iReg x = (24 + x) -- input regs
157 fReg x = (32 + x) -- float regs
160 -- | Some specific regs used by the code generator.
161 g0, g1, g2, fp, sp, o0, o1, f0, f1, f6, f8, f22, f26, f27 :: Reg
163 f6 = RegReal (RealRegSingle (fReg 6))
164 f8 = RegReal (RealRegSingle (fReg 8))
165 f22 = RegReal (RealRegSingle (fReg 22))
166 f26 = RegReal (RealRegSingle (fReg 26))
167 f27 = RegReal (RealRegSingle (fReg 27))
169 -- g0 is always zero, and writes to it vanish.
170 g0 = RegReal (RealRegSingle (gReg 0))
171 g1 = RegReal (RealRegSingle (gReg 1))
172 g2 = RegReal (RealRegSingle (gReg 2))
174 -- FP, SP, int and float return (from C) regs.
175 fp = RegReal (RealRegSingle (iReg 6))
176 sp = RegReal (RealRegSingle (oReg 6))
177 o0 = RegReal (RealRegSingle (oReg 0))
178 o1 = RegReal (RealRegSingle (oReg 1))
179 f0 = RegReal (RealRegSingle (fReg 0))
180 f1 = RegReal (RealRegSingle (fReg 1))
182 -- | Produce the second-half-of-a-double register given the first half.
184 fPair :: Reg -> Maybe Reg
186 | n >= 32 && n `mod` 2 == 0 = Just (RealReg (n+1))
188 fPair (VirtualRegD u)
189 = Just (VirtualRegHi u)
192 = trace ("MachInstrs.fPair: can't get high half of supposed double reg " ++ showPpr reg)
197 -- | All the regs that the register allocator can allocate to,
198 -- with the the fixed use regs removed.
200 allocatableRegs :: [RealReg]
205 -> isFastTrue (freeReg r)
208 -> isFastTrue (freeReg r1)
209 && isFastTrue (freeReg r2)
211 in filter isFree allRealRegs
214 -- | The registers to place arguments for function calls,
215 -- for some number of arguments.
217 argRegs :: RegNo -> [Reg]
221 1 -> map (RegReal . RealRegSingle . oReg) [0]
222 2 -> map (RegReal . RealRegSingle . oReg) [0,1]
223 3 -> map (RegReal . RealRegSingle . oReg) [0,1,2]
224 4 -> map (RegReal . RealRegSingle . oReg) [0,1,2,3]
225 5 -> map (RegReal . RealRegSingle . oReg) [0,1,2,3,4]
226 6 -> map (RegReal . RealRegSingle . oReg) [0,1,2,3,4,5]
227 _ -> panic "MachRegs.argRegs(sparc): don't know about >6 arguments!"
230 -- | All all the regs that could possibly be returned by argRegs
234 = map (RegReal . RealRegSingle) [oReg i | i <- [0..5]]
237 -- These are the regs that we cannot assume stay alive over a C call.
238 -- TODO: Why can we assume that o6 isn't clobbered? -- BL 2009/02
240 callClobberedRegs :: [Reg]
242 = map (RegReal . RealRegSingle)
244 [oReg i | i <- [0..5]] ++
245 [gReg i | i <- [1..7]] ++
246 [fReg i | i <- [0..31]] )
250 -- | Make a virtual reg with this size.
251 mkVirtualReg :: Unique -> Size -> VirtualReg
253 | not (isFloatSize size)
258 FF32 -> VirtualRegF u
259 FF64 -> VirtualRegD u
263 regDotColor :: RealReg -> SDoc
265 = case classOfRealReg reg of
266 RcInteger -> text "blue"
267 RcFloat -> text "red"
268 _other -> text "green"
273 -- Hard coded freeReg / globalRegMaybe -----------------------------------------
274 -- This isn't being used at the moment because we're generating
275 -- these functions from the information in
276 -- includes/stg/MachRegs.hs via RegPlate.hs
278 -- | Check whether a machine register is free for allocation.
279 -- This needs to match the info in includes/stg/MachRegs.h
280 -- otherwise modules compiled with the NCG won't be compatible
284 freeReg :: RegNo -> FastBool
287 -- %g0(r0) is always 0.
290 -- %g1(r1) - %g4(r4) are allocable -----------------
293 -- are reserved for the OS
298 -- %o0(r8) - %o5(r13) are allocable ----------------
301 -- is the C stack pointer
305 -- holds C return addresses (???)
308 -- %l0(r16) is allocable ---------------------------
310 -- %l1(r17) - %l5(r21)
311 -- are STG regs R1 - R5
318 -- %l6(r22) - %l7(r23) are allocable --------------
320 -- %i0(r24) - %i5(r29)
321 -- are STG regs Sp, Base, SpLim, Hp, R6
327 -- %i5(r28) is allocable --------------------------
332 -- is the C frame pointer
336 -- is used for C return addresses
339 -- %f0(r32) - %f1(r33)
340 -- are C fp return registers
344 -- %f2(r34) - %f5(r37)
345 -- are STG regs D1 - D2
351 -- %f22(r54) - %f25(r57)
352 -- are STG regs F1 - F4
358 -- regs not matched above are allocable.
363 -- | Returns Just the real register that a global register is stored in.
364 -- Returns Nothing if the global has no real register, and is stored
365 -- in the in-memory register table instead.
368 globalRegMaybe :: GlobalReg -> Maybe Reg
371 -- Argument and return regs
372 VanillaReg 1 _ -> Just (RealReg 17) -- %l1
373 VanillaReg 2 _ -> Just (RealReg 18) -- %l2
374 VanillaReg 3 _ -> Just (RealReg 19) -- %l3
375 VanillaReg 4 _ -> Just (RealReg 20) -- %l4
376 VanillaReg 5 _ -> Just (RealReg 21) -- %l5
377 VanillaReg 6 _ -> Just (RealReg 29) -- %i5
379 FloatReg 1 -> Just (RealReg 54) -- %f22
380 FloatReg 2 -> Just (RealReg 55) -- %f23
381 FloatReg 3 -> Just (RealReg 56) -- %f24
382 FloatReg 4 -> Just (RealReg 57) -- %f25
384 DoubleReg 1 -> Just (RealReg 34) -- %f2
385 DoubleReg 2 -> Just (RealReg 36) -- %f4
388 Sp -> Just (RealReg 24) -- %i0
389 SpLim -> Just (RealReg 26) -- %i2
390 Hp -> Just (RealReg 27) -- %i3
392 BaseReg -> Just (RealReg 25) -- %i1