1 -- -----------------------------------------------------------------------------
3 -- (c) The University of Glasgow 1994-2004
5 -- -----------------------------------------------------------------------------
26 -- machine specific info
30 gReg, iReg, lReg, oReg, fReg,
31 fp, sp, g0, g1, g2, o0, o1, f0, f6, f8, f22, f26, f27,
39 get_GlobalReg_reg_or_addr
48 import CgUtils ( get_GlobalReg_addr )
51 import CLabel ( CLabel )
53 import Outputable ( panic )
54 import qualified Outputable
59 -- immediates ------------------------------------------------------------------
61 -- | An immediate value.
62 -- Not all of these are directly representable by the machine.
63 -- Things like ImmLit are slurped out and put in a data segment instead.
71 -- AbstractC Label (with baggage)
80 | ImmConstantSum Imm Imm
81 | ImmConstantDiff Imm Imm
87 -- | Create a ImmLit containing this string.
88 strImmLit :: String -> Imm
89 strImmLit s = ImmLit (text s)
92 -- | Convert a CmmLit to an Imm.
93 -- Narrow to the width: a CmmInt might be out of
94 -- range, but we assume that ImmInteger only contains
95 -- in-range values. A signed value should be fine here.
97 litToImm :: CmmLit -> Imm
100 CmmInt i w -> ImmInteger (narrowS w i)
101 CmmFloat f W32 -> ImmFloat f
102 CmmFloat f W64 -> ImmDouble f
103 CmmLabel l -> ImmCLbl l
104 CmmLabelOff l off -> ImmIndex l off
106 CmmLabelDiffOff l1 l2 off
108 (ImmConstantDiff (ImmCLbl l1) (ImmCLbl l2))
111 CmmBlock id -> ImmCLbl (infoTblLbl id)
112 _ -> panic "SPARC.Regs.litToImm: no match"
116 -- addressing modes ------------------------------------------------------------
118 -- | Represents a memory address in an instruction.
119 -- Being a RISC machine, the SPARC addressing modes are very regular.
122 = AddrRegReg Reg Reg -- addr = r1 + r2
123 | AddrRegImm Reg Imm -- addr = r1 + imm
126 -- | Add an integer offset to the address in an AddrMode.
128 addrOffset :: AddrMode -> Int -> Maybe AddrMode
131 AddrRegImm r (ImmInt n)
132 | fits13Bits n2 -> Just (AddrRegImm r (ImmInt n2))
133 | otherwise -> Nothing
136 AddrRegImm r (ImmInteger n)
137 | fits13Bits n2 -> Just (AddrRegImm r (ImmInt (fromInteger n2)))
138 | otherwise -> Nothing
139 where n2 = n + toInteger off
141 AddrRegReg r (RealReg 0)
142 | fits13Bits off -> Just (AddrRegImm r (ImmInt off))
143 | otherwise -> Nothing
149 -- registers -------------------------------------------------------------------
151 -- | Get an AddrMode relative to the address in sp.
152 -- This gives us a stack relative addressing mode for volatile
153 -- temporaries and for excess call arguments.
155 spRel :: Int -- ^ stack offset in words, positive or negative
158 spRel n = AddrRegImm sp (ImmInt (n * wORD_SIZE))
161 -- | The registers to place arguments for function calls,
162 -- for some number of arguments.
164 argRegs :: RegNo -> [Reg]
168 1 -> map (RealReg . oReg) [0]
169 2 -> map (RealReg . oReg) [0,1]
170 3 -> map (RealReg . oReg) [0,1,2]
171 4 -> map (RealReg . oReg) [0,1,2,3]
172 5 -> map (RealReg . oReg) [0,1,2,3,4]
173 6 -> map (RealReg . oReg) [0,1,2,3,4,5]
174 _ -> panic "MachRegs.argRegs(sparc): don't know about >6 arguments!"
177 -- | All all the regs that could possibly be returned by argRegs
181 = map RealReg [oReg i | i <- [0..5]]
184 -- These are the regs that we cannot assume stay alive over a C call.
185 -- TODO: Why can we assume that o6 isn't clobbered? -- BL 2009/02
187 callClobberedRegs :: [Reg]
191 [oReg i | i <- [0..5]] ++
192 [gReg i | i <- [1..7]] ++
193 [fReg i | i <- [0..31]] )
196 -- | The RegNos corresponding to all the registers in the machine.
197 -- For SPARC we use f0-f22 as doubles, so pretend that the high halves
198 -- of these, ie f23, f25 .. don't exist.
200 allMachRegNos :: [RegNo]
203 ++ [32,34 .. nCG_FirstFloatReg-1]
204 ++ [nCG_FirstFloatReg .. 63])
207 -- | Get the class of a register.
208 {-# INLINE regClass #-}
209 regClass :: Reg -> RegClass
212 VirtualRegI _ -> RcInteger
213 VirtualRegHi _ -> RcInteger
214 VirtualRegF _ -> RcFloat
215 VirtualRegD _ -> RcDouble
217 | i < 32 -> RcInteger
218 | i < nCG_FirstFloatReg -> RcDouble
219 | otherwise -> RcFloat
222 -- | Get the standard name for the register with this number.
223 showReg :: RegNo -> String
225 | n >= 0 && n < 8 = "%g" ++ show n
226 | n >= 8 && n < 16 = "%o" ++ show (n-8)
227 | n >= 16 && n < 24 = "%l" ++ show (n-16)
228 | n >= 24 && n < 32 = "%i" ++ show (n-24)
229 | n >= 32 && n < 64 = "%f" ++ show (n-32)
230 | otherwise = panic "SPARC.Regs.showReg: unknown sparc register"
233 -- machine specific ------------------------------------------------------------
235 -- | Get an address relative to the frame pointer.
236 -- This doesn't work work for offsets greater than 13 bits; we just hope for the best
238 fpRel :: Int -> AddrMode
240 = AddrRegImm fp (ImmInt (n * wORD_SIZE))
243 -- | Check whether an offset is representable with 13 bits.
244 fits13Bits :: Integral a => a -> Bool
245 fits13Bits x = x >= -4096 && x < 4096
247 {-# SPECIALIZE fits13Bits :: Int -> Bool, Integer -> Bool #-}
251 largeOffsetError :: Integral a => a -> b
253 = panic ("ERROR: SPARC native-code generator cannot handle large offset ("
254 ++ show i ++ ");\nprobably because of large constant data structures;" ++
255 "\nworkaround: use -fvia-C on this module.\n")
260 The SPARC has 64 registers of interest; 32 integer registers and 32
261 floating point registers. The mapping of STG registers to SPARC
262 machine registers is defined in StgRegs.h. We are, of course,
263 prepared for any eventuality.
265 The whole fp-register pairing thing on sparcs is a huge nuisance. See
266 fptools/ghc/includes/MachRegs.h for a description of what's going on
271 -- | Get the regno for this sort of reg
272 gReg, lReg, iReg, oReg, fReg :: Int -> RegNo
274 gReg x = x -- global regs
275 oReg x = (8 + x) -- output regs
276 lReg x = (16 + x) -- local regs
277 iReg x = (24 + x) -- input regs
278 fReg x = (32 + x) -- float regs
281 -- | Some specific regs used by the code generator.
282 g0, g1, g2, fp, sp, o0, o1, f0, f6, f8, f22, f26, f27 :: Reg
284 f6 = RealReg (fReg 6)
285 f8 = RealReg (fReg 8)
286 f22 = RealReg (fReg 22)
287 f26 = RealReg (fReg 26)
288 f27 = RealReg (fReg 27)
290 g0 = RealReg (gReg 0) -- g0 is always zero, and writes to it vanish.
291 g1 = RealReg (gReg 1)
292 g2 = RealReg (gReg 2)
294 -- FP, SP, int and float return (from C) regs.
295 fp = RealReg (iReg 6)
296 sp = RealReg (oReg 6)
297 o0 = RealReg (oReg 0)
298 o1 = RealReg (oReg 1)
299 f0 = RealReg (fReg 0)
302 -- | We use he first few float regs as double precision.
303 -- This is the RegNo of the first float regs we use as single precision.
305 nCG_FirstFloatReg :: RegNo
306 nCG_FirstFloatReg = 54
310 -- | Check whether a machine register is free for allocation.
311 -- This needs to match the info in includes/MachRegs.h otherwise modules
312 -- compiled with the NCG won't be compatible with via-C ones.
314 freeReg :: RegNo -> FastBool
317 -- %g0(r0) is always 0.
320 -- %g1(r1) - %g4(r4) are allocable -----------------
323 -- are reserved for the OS
328 -- %o0(r8) - %o5(r13) are allocable ----------------
331 -- is the C stack pointer
335 -- holds C return addresses (???)
338 -- %l0(r16) is allocable ---------------------------
340 -- %l1(r17) - %l5(r21)
341 -- are STG regs R1 - R5
348 -- %l6(r22) - %l7(r23) are allocable --------------
350 -- %i0(r24) - %i5(r29)
351 -- are STG regs Sp, Base, SpLim, Hp, HpLim, R6
360 -- is the C frame pointer
364 -- is used for C return addresses
367 -- %f0(r32) - %f1(r33)
368 -- are C fp return registers
372 -- %f2(r34) - %f5(r37)
373 -- are STG regs D1 - D2
379 -- %f22(r54) - %f25(r57)
380 -- are STG regs F1 - F4
386 -- regs not matched above are allocable.
390 -- allocatableRegs is allMachRegNos with the fixed-use regs removed.
391 -- i.e., these are the regs for which we are prepared to allow the
392 -- register allocator to attempt to map VRegs to.
393 allocatableRegs :: [RegNo]
395 = let isFree i = isFastTrue (freeReg i)
396 in filter isFree allMachRegNos
399 -- | Returns Just the real register that a global register is stored in.
400 -- Returns Nothing if the global has no real register, and is stored
401 -- in the in-memory register table instead.
403 globalRegMaybe :: GlobalReg -> Maybe Reg
406 -- Argument and return regs
407 VanillaReg 1 _ -> Just (RealReg 17) -- %l1
408 VanillaReg 2 _ -> Just (RealReg 18) -- %l2
409 VanillaReg 3 _ -> Just (RealReg 19) -- %l3
410 VanillaReg 4 _ -> Just (RealReg 20) -- %l4
411 VanillaReg 5 _ -> Just (RealReg 21) -- %l5
412 VanillaReg 6 _ -> Just (RealReg 29) -- %i5
414 FloatReg 1 -> Just (RealReg 54) -- %f22
415 FloatReg 2 -> Just (RealReg 55) -- %f23
416 FloatReg 3 -> Just (RealReg 56) -- %f24
417 FloatReg 4 -> Just (RealReg 57) -- %f25
419 DoubleReg 1 -> Just (RealReg 34) -- %f2
420 DoubleReg 2 -> Just (RealReg 36) -- %f4
423 Sp -> Just (RealReg 24) -- %i0
424 SpLim -> Just (RealReg 26) -- %i2
425 Hp -> Just (RealReg 27) -- %i3
426 HpLim -> Just (RealReg 28) -- %i4
428 BaseReg -> Just (RealReg 25) -- %i1
433 -- We map STG registers onto appropriate CmmExprs. Either they map
434 -- to real machine registers or stored as offsets from BaseReg. Given
435 -- a GlobalReg, get_GlobalReg_reg_or_addr produces either the real
436 -- register it is in, on this platform, or a CmmExpr denoting the
437 -- address in the register table holding it.
438 -- (See also get_GlobalReg_addr in CgUtils.)
440 get_GlobalReg_reg_or_addr :: GlobalReg -> Either Reg CmmExpr
441 get_GlobalReg_reg_or_addr mid
442 = case globalRegMaybe mid of
444 Nothing -> Right (get_GlobalReg_addr mid)