01db0ed3ac441419ca51594612c6e631ac090cf1
[ghc.git] / compiler / nativeGen / SPARC / Regs.hs
1 -- -----------------------------------------------------------------------------
2 --
3 -- (c) The University of Glasgow 1994-2004
4 --
5 -- -----------------------------------------------------------------------------
6
7 {-# OPTIONS_GHC -fno-warn-tabs #-}
8 -- The above warning supression flag is a temporary kludge.
9 -- While working on this module you are encouraged to remove it and
10 -- detab the module (please do the detabbing in a separate patch). See
11 -- http://ghc.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
12 -- for details
13
14 module SPARC.Regs (
15 -- registers
16 showReg,
17 virtualRegSqueeze,
18 realRegSqueeze,
19 classOfRealReg,
20 allRealRegs,
21
22 -- machine specific info
23 gReg, iReg, lReg, oReg, fReg,
24 fp, sp, g0, g1, g2, o0, o1, f0, f1, f6, f8, f22, f26, f27,
25
26 -- allocatable
27 allocatableRegs,
28
29 -- args
30 argRegs,
31 allArgRegs,
32 callClobberedRegs,
33
34 --
35 mkVirtualReg,
36 regDotColor
37 )
38
39 where
40
41
42 import CodeGen.Platform.SPARC
43 import Reg
44 import RegClass
45 import Size
46
47 import Unique
48 import Outputable
49 import FastTypes
50 import FastBool
51
52 {-
53 The SPARC has 64 registers of interest; 32 integer registers and 32
54 floating point registers. The mapping of STG registers to SPARC
55 machine registers is defined in StgRegs.h. We are, of course,
56 prepared for any eventuality.
57
58 The whole fp-register pairing thing on sparcs is a huge nuisance. See
59 includes/stg/MachRegs.h for a description of what's going on
60 here.
61 -}
62
63
64 -- | Get the standard name for the register with this number.
65 showReg :: RegNo -> String
66 showReg n
67 | n >= 0 && n < 8 = "%g" ++ show n
68 | n >= 8 && n < 16 = "%o" ++ show (n-8)
69 | n >= 16 && n < 24 = "%l" ++ show (n-16)
70 | n >= 24 && n < 32 = "%i" ++ show (n-24)
71 | n >= 32 && n < 64 = "%f" ++ show (n-32)
72 | otherwise = panic "SPARC.Regs.showReg: unknown sparc register"
73
74
75 -- Get the register class of a certain real reg
76 classOfRealReg :: RealReg -> RegClass
77 classOfRealReg reg
78 = case reg of
79 RealRegSingle i
80 | i < 32 -> RcInteger
81 | otherwise -> RcFloat
82
83 RealRegPair{} -> RcDouble
84
85
86 -- | regSqueeze_class reg
87 -- Calculuate the maximum number of register colors that could be
88 -- denied to a node of this class due to having this reg
89 -- as a neighbour.
90 --
91 {-# INLINE virtualRegSqueeze #-}
92 virtualRegSqueeze :: RegClass -> VirtualReg -> FastInt
93
94 virtualRegSqueeze cls vr
95 = case cls of
96 RcInteger
97 -> case vr of
98 VirtualRegI{} -> _ILIT(1)
99 VirtualRegHi{} -> _ILIT(1)
100 _other -> _ILIT(0)
101
102 RcFloat
103 -> case vr of
104 VirtualRegF{} -> _ILIT(1)
105 VirtualRegD{} -> _ILIT(2)
106 _other -> _ILIT(0)
107
108 RcDouble
109 -> case vr of
110 VirtualRegF{} -> _ILIT(1)
111 VirtualRegD{} -> _ILIT(1)
112 _other -> _ILIT(0)
113
114 _other -> _ILIT(0)
115
116 {-# INLINE realRegSqueeze #-}
117 realRegSqueeze :: RegClass -> RealReg -> FastInt
118
119 realRegSqueeze cls rr
120 = case cls of
121 RcInteger
122 -> case rr of
123 RealRegSingle regNo
124 | regNo < 32 -> _ILIT(1)
125 | otherwise -> _ILIT(0)
126
127 RealRegPair{} -> _ILIT(0)
128
129 RcFloat
130 -> case rr of
131 RealRegSingle regNo
132 | regNo < 32 -> _ILIT(0)
133 | otherwise -> _ILIT(1)
134
135 RealRegPair{} -> _ILIT(2)
136
137 RcDouble
138 -> case rr of
139 RealRegSingle regNo
140 | regNo < 32 -> _ILIT(0)
141 | otherwise -> _ILIT(1)
142
143 RealRegPair{} -> _ILIT(1)
144
145 _other -> _ILIT(0)
146
147 -- | All the allocatable registers in the machine,
148 -- including register pairs.
149 allRealRegs :: [RealReg]
150 allRealRegs
151 = [ (RealRegSingle i) | i <- [0..63] ]
152 ++ [ (RealRegPair i (i+1)) | i <- [32, 34 .. 62 ] ]
153
154
155 -- | Get the regno for this sort of reg
156 gReg, lReg, iReg, oReg, fReg :: Int -> RegNo
157
158 gReg x = x -- global regs
159 oReg x = (8 + x) -- output regs
160 lReg x = (16 + x) -- local regs
161 iReg x = (24 + x) -- input regs
162 fReg x = (32 + x) -- float regs
163
164
165 -- | Some specific regs used by the code generator.
166 g0, g1, g2, fp, sp, o0, o1, f0, f1, f6, f8, f22, f26, f27 :: Reg
167
168 f6 = RegReal (RealRegSingle (fReg 6))
169 f8 = RegReal (RealRegSingle (fReg 8))
170 f22 = RegReal (RealRegSingle (fReg 22))
171 f26 = RegReal (RealRegSingle (fReg 26))
172 f27 = RegReal (RealRegSingle (fReg 27))
173
174 -- g0 is always zero, and writes to it vanish.
175 g0 = RegReal (RealRegSingle (gReg 0))
176 g1 = RegReal (RealRegSingle (gReg 1))
177 g2 = RegReal (RealRegSingle (gReg 2))
178
179 -- FP, SP, int and float return (from C) regs.
180 fp = RegReal (RealRegSingle (iReg 6))
181 sp = RegReal (RealRegSingle (oReg 6))
182 o0 = RegReal (RealRegSingle (oReg 0))
183 o1 = RegReal (RealRegSingle (oReg 1))
184 f0 = RegReal (RealRegSingle (fReg 0))
185 f1 = RegReal (RealRegSingle (fReg 1))
186
187 -- | Produce the second-half-of-a-double register given the first half.
188 {-
189 fPair :: Reg -> Maybe Reg
190 fPair (RealReg n)
191 | n >= 32 && n `mod` 2 == 0 = Just (RealReg (n+1))
192
193 fPair (VirtualRegD u)
194 = Just (VirtualRegHi u)
195
196 fPair reg
197 = trace ("MachInstrs.fPair: can't get high half of supposed double reg " ++ showPpr reg)
198 Nothing
199 -}
200
201
202 -- | All the regs that the register allocator can allocate to,
203 -- with the the fixed use regs removed.
204 --
205 allocatableRegs :: [RealReg]
206 allocatableRegs
207 = let isFree rr
208 = case rr of
209 RealRegSingle r
210 -> isFastTrue (freeReg r)
211
212 RealRegPair r1 r2
213 -> isFastTrue (freeReg r1)
214 && isFastTrue (freeReg r2)
215
216 in filter isFree allRealRegs
217
218
219 -- | The registers to place arguments for function calls,
220 -- for some number of arguments.
221 --
222 argRegs :: RegNo -> [Reg]
223 argRegs r
224 = case r of
225 0 -> []
226 1 -> map (RegReal . RealRegSingle . oReg) [0]
227 2 -> map (RegReal . RealRegSingle . oReg) [0,1]
228 3 -> map (RegReal . RealRegSingle . oReg) [0,1,2]
229 4 -> map (RegReal . RealRegSingle . oReg) [0,1,2,3]
230 5 -> map (RegReal . RealRegSingle . oReg) [0,1,2,3,4]
231 6 -> map (RegReal . RealRegSingle . oReg) [0,1,2,3,4,5]
232 _ -> panic "MachRegs.argRegs(sparc): don't know about >6 arguments!"
233
234
235 -- | All all the regs that could possibly be returned by argRegs
236 --
237 allArgRegs :: [Reg]
238 allArgRegs
239 = map (RegReal . RealRegSingle) [oReg i | i <- [0..5]]
240
241
242 -- These are the regs that we cannot assume stay alive over a C call.
243 -- TODO: Why can we assume that o6 isn't clobbered? -- BL 2009/02
244 --
245 callClobberedRegs :: [Reg]
246 callClobberedRegs
247 = map (RegReal . RealRegSingle)
248 ( oReg 7 :
249 [oReg i | i <- [0..5]] ++
250 [gReg i | i <- [1..7]] ++
251 [fReg i | i <- [0..31]] )
252
253
254
255 -- | Make a virtual reg with this size.
256 mkVirtualReg :: Unique -> Size -> VirtualReg
257 mkVirtualReg u size
258 | not (isFloatSize size)
259 = VirtualRegI u
260
261 | otherwise
262 = case size of
263 FF32 -> VirtualRegF u
264 FF64 -> VirtualRegD u
265 _ -> panic "mkVReg"
266
267
268 regDotColor :: RealReg -> SDoc
269 regDotColor reg
270 = case classOfRealReg reg of
271 RcInteger -> text "blue"
272 RcFloat -> text "red"
273 _other -> text "green"
274