Fix some holes in the SPARC native code generator.
[ghc-hetmet.git] / compiler / nativeGen / MachRegs.lhs
1 -- -----------------------------------------------------------------------------
2 --
3 -- (c) The University of Glasgow 1994-2004
4 -- 
5 -- Machine-specific info about registers.
6 -- 
7 -- Also includes stuff about immediate operands, which are
8 -- often/usually quite entangled with registers.
9 -- 
10 -- (Immediates could be untangled from registers at some cost in tangled
11 -- modules --- the pleasure has been foregone.)
12 -- 
13 -- -----------------------------------------------------------------------------
14
15 \begin{code}
16 {-# OPTIONS -w #-}
17 -- The above warning supression flag is a temporary kludge.
18 -- While working on this module you are encouraged to remove it and fix
19 -- any warnings in the module. See
20 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
21 -- for details
22
23 #include "nativeGen/NCG.h"
24
25 module MachRegs (
26
27         -- * Sizes
28         Size(..), intSize, floatSize, isFloatSize, 
29                   wordSize, cmmTypeSize, sizeToWidth,
30
31         -- * Immediate values
32         Imm(..), strImmLit, litToImm,
33
34         -- * Addressing modes
35         AddrMode(..),
36         addrOffset,
37
38         -- * The 'Reg' type
39         RegNo,
40         Reg(..), isRealReg, isVirtualReg, renameVirtualReg,
41         RegClass(..), regClass,
42         trivColorable,
43         getHiVRegFromLo, 
44         mkVReg,
45
46         -- * Global registers
47         get_GlobalReg_reg_or_addr,
48
49         -- * Machine-dependent register-related stuff
50         allocatableRegs, argRegs, allArgRegs, callClobberedRegs,
51         allocatableRegsInClass,
52         freeReg,
53         spRel,
54
55 #if alpha_TARGET_ARCH
56         fits8Bits,
57         fReg,
58         gp, pv, ra, sp, t9, t10, t11, t12, v0, f0, zeroh,
59 #endif
60 #if i386_TARGET_ARCH
61         EABase(..), EAIndex(..),
62         eax, ebx, ecx, edx, esi, edi, ebp, esp,
63         fake0, fake1, fake2, fake3, fake4, fake5,
64         addrModeRegs,
65 #endif
66 #if x86_64_TARGET_ARCH
67         EABase(..), EAIndex(..), ripRel,
68         rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp,
69         eax, ebx, ecx, edx, esi, edi, ebp, esp,
70         r8, r9, r10, r11, r12, r13, r14, r15,
71         xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7,
72         xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15,
73         xmm,
74         addrModeRegs, allFPArgRegs,
75 #endif
76 #if sparc_TARGET_ARCH
77         fits13Bits, 
78         fpRel, gReg, iReg, lReg, oReg, largeOffsetError,
79         fp, sp, g0, g1, g2, o0, o1, f0, f6, f8, f26, f27,
80 #endif
81 #if powerpc_TARGET_ARCH
82         allFPArgRegs,
83         makeImmediate,
84         sp,
85         r3, r4, r27, r28,
86         f1, f20, f21,
87 #endif
88     ) where
89
90 #include "HsVersions.h"
91
92 #if i386_TARGET_ARCH
93 # define STOLEN_X86_REGS 4
94 -- HACK: go for the max
95 #endif
96
97 #include "../includes/MachRegs.h"
98
99 import BlockId
100 import Cmm
101 import CgUtils          ( get_GlobalReg_addr )
102 import CLabel           ( CLabel, mkMainCapabilityLabel )
103 import Pretty
104 import Outputable       ( Outputable(..), pprPanic, panic )
105 import qualified Outputable
106 import Unique
107 import UniqSet
108 import Constants
109 import FastTypes
110 import FastBool
111 import UniqFM
112
113 #if powerpc_TARGET_ARCH
114 import Data.Word        ( Word8, Word16, Word32 )
115 import Data.Int         ( Int8, Int16, Int32 )
116 #endif
117
118 -- -----------------------------------------------------------------------------
119 -- Sizes on this architecture
120 -- 
121 -- A Size is usually a combination of width and class
122
123 -- It looks very like the old MachRep, but it's now of purely local
124 -- significance, here in the native code generator.  You can change it
125 -- without global consequences.
126 --
127 -- A major use is as an opcode qualifier; thus the opcode 
128 --      mov.l a b
129 -- might be encoded 
130 --      MOV II32 a b
131 -- where the Size field encodes the ".l" part.
132
133 -- ToDo: it's not clear to me that we need separate signed-vs-unsigned sizes
134 -- here.  I've removed them from the x86 version, we'll see what happens --SDM
135
136 -- ToDo: quite a few occurrences of Size could usefully be replaced by Width
137
138 #if powerpc_TARGET_ARCH || i386_TARGET_ARCH || x86_64_TARGET_ARCH
139 data Size       -- For these three, the "size" also gives the int/float
140                 -- distinction, because the instructions for int/float
141                 -- differ only in their suffices
142   = II8 | II16 | II32 | II64 | FF32 | FF64 | FF80
143   deriving Eq
144
145 intSize, floatSize :: Width -> Size
146 intSize W8 = II8
147 intSize W16 = II16
148 intSize W32 = II32
149 intSize W64 = II64
150 intSize other = pprPanic "MachInstrs.intSize" (ppr other)
151
152 floatSize W32 = FF32
153 floatSize W64 = FF64
154 floatSize other = pprPanic "MachInstrs.intSize" (ppr other)
155
156 wordSize :: Size
157 wordSize = intSize wordWidth
158
159 sizeToWidth :: Size -> Width
160 sizeToWidth II8  = W8
161 sizeToWidth II16 = W16
162 sizeToWidth II32 = W32
163 sizeToWidth II64 = W64
164 sizeToWidth FF32 = W32
165 sizeToWidth FF64 = W64
166 sizeToWidth _ = panic "MachInstrs.sizeToWidth"
167
168 cmmTypeSize :: CmmType -> Size
169 cmmTypeSize ty | isFloatType ty = floatSize (typeWidth ty)
170                | otherwise      = intSize (typeWidth ty)
171
172 isFloatSize :: Size -> Bool
173 isFloatSize FF32 = True
174 isFloatSize FF64 = True
175 isFloatSize FF80 = True
176 isFloatSize other = False
177 #endif
178
179 #if alpha_TARGET_ARCH
180 data Size
181     = B     -- byte
182     | Bu
183 --  | W     -- word (2 bytes): UNUSED
184 --  | Wu    -- : UNUSED
185     | L     -- longword (4 bytes)
186     | Q     -- quadword (8 bytes)
187 --  | FF    -- VAX F-style floating pt: UNUSED
188 --  | GF    -- VAX G-style floating pt: UNUSED
189 --  | DF    -- VAX D-style floating pt: UNUSED
190 --  | SF    -- IEEE single-precision floating pt: UNUSED
191     | TF    -- IEEE double-precision floating pt
192   deriving Eq
193 #endif
194
195 #if sparc_TARGET_ARCH /* || powerpc_TARGET_ARCH */
196 data Size
197     = II8     -- byte (signed)
198 --    | II8u    -- byte (unsigned)
199     | II16    -- halfword (signed, 2 bytes)
200 --   | II16u   -- halfword (unsigned, 2 bytes)
201     | II32    -- word (4 bytes)
202     | II64    -- word (8 bytes)
203     | FF32    -- IEEE single-precision floating pt
204     | FF64    -- IEEE single-precision floating pt
205   deriving Eq
206
207
208 intSize, floatSize :: Width -> Size
209 intSize W8  = II8
210 --intSize W16 = II16u
211 intSize W16 = II16
212 intSize W32 = II32
213 intSize W64 = II64
214 intSize other = pprPanic "MachInstrs.intSize" (ppr other)
215
216 floatSize W32 = FF32
217 floatSize W64 = FF64
218 floatSize other = pprPanic "MachInstrs.intSize" (ppr other)
219
220 wordSize :: Size
221 wordSize = intSize wordWidth
222
223 isFloatSize :: Size -> Bool
224 isFloatSize FF32        = True
225 isFloatSize FF64        = True
226 isFloatSize _           = False
227
228 cmmTypeSize :: CmmType -> Size
229 cmmTypeSize ty | isFloatType ty = floatSize (typeWidth ty)
230                | otherwise      = intSize (typeWidth ty)
231
232 sizeToWidth :: Size -> Width
233 sizeToWidth size
234  = case size of
235         II8             -> W8
236 --      II8u            -> W8
237         II16            -> W16
238 --      II16u           -> W16
239         II32            -> W32
240         II64            -> W64
241         FF32            -> W32
242         FF64            -> W64
243
244
245 #endif
246
247 -- -----------------------------------------------------------------------------
248 -- Immediates
249
250 data Imm
251   = ImmInt      Int
252   | ImmInteger  Integer     -- Sigh.
253   | ImmCLbl     CLabel      -- AbstractC Label (with baggage)
254   | ImmLit      Doc         -- Simple string
255   | ImmIndex    CLabel Int
256   | ImmFloat    Rational
257   | ImmDouble   Rational
258   | ImmConstantSum Imm Imm
259   | ImmConstantDiff Imm Imm
260 #if sparc_TARGET_ARCH
261   | LO Imm                  {- Possible restrictions... -}
262   | HI Imm
263 #endif
264 #if powerpc_TARGET_ARCH
265   | LO Imm
266   | HI Imm
267   | HA Imm      {- high halfword adjusted -}
268 #endif
269 strImmLit s = ImmLit (text s)
270
271 litToImm :: CmmLit -> Imm
272 litToImm (CmmInt i w)        = ImmInteger (narrowS w i)
273                 -- narrow to the width: a CmmInt might be out of
274                 -- range, but we assume that ImmInteger only contains
275                 -- in-range values.  A signed value should be fine here.
276 litToImm (CmmFloat f W32)    = ImmFloat f
277 litToImm (CmmFloat f W64)    = ImmDouble f
278 litToImm (CmmLabel l)        = ImmCLbl l
279 litToImm (CmmLabelOff l off) = ImmIndex l off
280 litToImm (CmmLabelDiffOff l1 l2 off)
281                              = ImmConstantSum
282                                (ImmConstantDiff (ImmCLbl l1) (ImmCLbl l2))
283                                (ImmInt off)
284 litToImm (CmmBlock id)       = ImmCLbl (infoTblLbl id)
285
286 -- -----------------------------------------------------------------------------
287 -- Addressing modes
288
289 data AddrMode
290 #if alpha_TARGET_ARCH
291   = AddrImm     Imm
292   | AddrReg     Reg
293   | AddrRegImm  Reg Imm
294 #endif
295
296 #if i386_TARGET_ARCH || x86_64_TARGET_ARCH
297   = AddrBaseIndex       EABase EAIndex Displacement
298   | ImmAddr             Imm Int
299
300 data EABase       = EABaseNone  | EABaseReg Reg | EABaseRip
301 data EAIndex      = EAIndexNone | EAIndex Reg Int
302 type Displacement = Imm
303 #endif
304
305 #if sparc_TARGET_ARCH
306   = AddrRegReg  Reg Reg
307   | AddrRegImm  Reg Imm
308 #endif
309
310 #if powerpc_TARGET_ARCH
311   = AddrRegReg  Reg Reg
312   | AddrRegImm  Reg Imm
313 #endif
314
315 #if i386_TARGET_ARCH || x86_64_TARGET_ARCH
316 addrModeRegs :: AddrMode -> [Reg]
317 addrModeRegs (AddrBaseIndex b i _) =  b_regs ++ i_regs
318   where
319    b_regs = case b of { EABaseReg r -> [r]; _ -> [] }
320    i_regs = case i of { EAIndex r _ -> [r]; _ -> [] }
321 addrModeRegs _ = []
322 #endif
323
324
325 addrOffset :: AddrMode -> Int -> Maybe AddrMode
326
327 addrOffset addr off
328   = case addr of
329 #if alpha_TARGET_ARCH
330       _ -> panic "MachMisc.addrOffset not defined for Alpha"
331 #endif
332 #if i386_TARGET_ARCH || x86_64_TARGET_ARCH
333       ImmAddr i off0      -> Just (ImmAddr i (off0 + off))
334
335       AddrBaseIndex r i (ImmInt n) -> Just (AddrBaseIndex r i (ImmInt (n + off)))
336       AddrBaseIndex r i (ImmInteger n)
337         -> Just (AddrBaseIndex r i (ImmInt (fromInteger (n + toInteger off))))
338
339       AddrBaseIndex r i (ImmCLbl lbl)
340         -> Just (AddrBaseIndex r i (ImmIndex lbl off))
341
342       AddrBaseIndex r i (ImmIndex lbl ix)
343         -> Just (AddrBaseIndex r i (ImmIndex lbl (ix+off)))
344
345       _ -> Nothing  -- in theory, shouldn't happen
346 #endif
347 #if sparc_TARGET_ARCH
348       AddrRegImm r (ImmInt n)
349        | fits13Bits n2 -> Just (AddrRegImm r (ImmInt n2))
350        | otherwise     -> Nothing
351        where n2 = n + off
352
353       AddrRegImm r (ImmInteger n)
354        | fits13Bits n2 -> Just (AddrRegImm r (ImmInt (fromInteger n2)))
355        | otherwise     -> Nothing
356        where n2 = n + toInteger off
357
358       AddrRegReg r (RealReg 0)
359        | fits13Bits off -> Just (AddrRegImm r (ImmInt off))
360        | otherwise     -> Nothing
361        
362       _ -> Nothing
363 #endif /* sparc */
364 #if powerpc_TARGET_ARCH
365       AddrRegImm r (ImmInt n)
366        | fits16Bits n2 -> Just (AddrRegImm r (ImmInt n2))
367        | otherwise     -> Nothing
368        where n2 = n + off
369
370       AddrRegImm r (ImmInteger n)
371        | fits16Bits n2 -> Just (AddrRegImm r (ImmInt (fromInteger n2)))
372        | otherwise     -> Nothing
373        where n2 = n + toInteger off
374        
375       _ -> Nothing
376 #endif /* powerpc */
377
378 -----------------
379 #if alpha_TARGET_ARCH
380
381 fits8Bits :: Integer -> Bool
382 fits8Bits i = i >= -256 && i < 256
383
384 #endif
385
386 #if sparc_TARGET_ARCH
387
388 {-# SPECIALIZE fits13Bits :: Int -> Bool, Integer -> Bool #-}
389 fits13Bits :: Integral a => a -> Bool
390 fits13Bits x = x >= -4096 && x < 4096
391
392 -----------------
393 largeOffsetError i
394   = error ("ERROR: SPARC native-code generator cannot handle large offset ("
395            ++show i++");\nprobably because of large constant data structures;" ++ 
396            "\nworkaround: use -fvia-C on this module.\n")
397
398 #endif /* sparc */
399
400 #if powerpc_TARGET_ARCH
401 fits16Bits :: Integral a => a -> Bool
402 fits16Bits x = x >= -32768 && x < 32768
403
404 makeImmediate :: Integral a => Width -> Bool -> a -> Maybe Imm
405 makeImmediate rep signed x = fmap ImmInt (toI16 rep signed)
406     where
407         narrow W32 False = fromIntegral (fromIntegral x :: Word32)
408         narrow W16 False = fromIntegral (fromIntegral x :: Word16)
409         narrow W8  False = fromIntegral (fromIntegral x :: Word8)
410         narrow W32 True  = fromIntegral (fromIntegral x :: Int32)
411         narrow W16 True  = fromIntegral (fromIntegral x :: Int16)
412         narrow W8  True  = fromIntegral (fromIntegral x :: Int8)
413         
414         narrowed = narrow rep signed
415         
416         toI16 W32 True
417             | narrowed >= -32768 && narrowed < 32768 = Just narrowed
418             | otherwise = Nothing
419         toI16 W32 False
420             | narrowed >= 0 && narrowed < 65536 = Just narrowed
421             | otherwise = Nothing
422         toI16 _ _  = Just narrowed
423 #endif
424
425
426 -- @spRel@ gives us a stack relative addressing mode for volatile
427 -- temporaries and for excess call arguments.  @fpRel@, where
428 -- applicable, is the same but for the frame pointer.
429
430 spRel :: Int    -- desired stack offset in words, positive or negative
431       -> AddrMode
432
433 spRel n
434 #if defined(i386_TARGET_ARCH)
435   = AddrBaseIndex (EABaseReg esp) EAIndexNone (ImmInt (n * wORD_SIZE))
436 #elif defined(x86_64_TARGET_ARCH)
437   = AddrBaseIndex (EABaseReg rsp) EAIndexNone (ImmInt (n * wORD_SIZE))
438 #else
439   = AddrRegImm sp (ImmInt (n * wORD_SIZE))
440 #endif
441
442 #if sparc_TARGET_ARCH
443 fpRel :: Int -> AddrMode
444     -- Duznae work for offsets greater than 13 bits; we just hope for
445     -- the best
446 fpRel n
447   = AddrRegImm fp (ImmInt (n * wORD_SIZE))
448 #endif
449
450 #if x86_64_TARGET_ARCH
451 ripRel imm = AddrBaseIndex EABaseRip EAIndexNone imm
452 #endif
453
454 -- -----------------------------------------------------------------------------
455 -- Global registers
456
457 -- We map STG registers onto appropriate CmmExprs.  Either they map
458 -- to real machine registers or stored as offsets from BaseReg.  Given
459 -- a GlobalReg, get_GlobalReg_reg_or_addr produces either the real
460 -- register it is in, on this platform, or a CmmExpr denoting the
461 -- address in the register table holding it.
462 -- (See also get_GlobalReg_addr in CgUtils.)
463
464 get_GlobalReg_reg_or_addr       :: GlobalReg -> Either Reg CmmExpr
465 get_GlobalReg_reg_or_addr mid
466    = case globalRegMaybe mid of
467         Just rr -> Left rr
468         Nothing -> Right (get_GlobalReg_addr mid)
469
470 -- ---------------------------------------------------------------------------
471 -- Registers
472
473 -- RealRegs are machine regs which are available for allocation, in
474 -- the usual way.  We know what class they are, because that's part of
475 -- the processor's architecture.
476
477 -- VirtualRegs are virtual registers.  The register allocator will
478 -- eventually have to map them into RealRegs, or into spill slots.
479 -- VirtualRegs are allocated on the fly, usually to represent a single
480 -- value in the abstract assembly code (i.e. dynamic registers are
481 -- usually single assignment).  With the new register allocator, the
482 -- single assignment restriction isn't necessary to get correct code,
483 -- although a better register allocation will result if single
484 -- assignment is used -- because the allocator maps a VirtualReg into
485 -- a single RealReg, even if the VirtualReg has multiple live ranges.
486
487 -- Virtual regs can be of either class, so that info is attached.
488
489 -- Determine the upper-half vreg for a 64-bit quantity on a 32-bit platform
490 -- when supplied with the vreg for the lower-half of the quantity.
491 -- (NB. Not reversible).
492 getHiVRegFromLo (VirtualRegI u) 
493    = VirtualRegHi (newTagUnique u 'H') -- makes a pseudo-unique with tag 'H'
494 getHiVRegFromLo other 
495    = pprPanic "getHiVRegFromLo" (ppr other)
496
497 data RegClass 
498    = RcInteger 
499    | RcFloat
500    | RcDouble
501      deriving Eq
502
503 instance Uniquable RegClass where
504     getUnique RcInteger = mkUnique 'L' 0
505     getUnique RcFloat   = mkUnique 'L' 1
506     getUnique RcDouble  = mkUnique 'L' 2
507
508 type RegNo = Int
509
510 data Reg
511    = RealReg      {-# UNPACK #-} !RegNo
512    | VirtualRegI  {-# UNPACK #-} !Unique
513    | VirtualRegHi {-# UNPACK #-} !Unique  -- High part of 2-word register
514    | VirtualRegF  {-# UNPACK #-} !Unique
515    | VirtualRegD  {-# UNPACK #-} !Unique
516    deriving (Eq,Ord)
517
518 -- We like to have Uniques for Reg so that we can make UniqFM and UniqSets 
519 -- in the register allocator.
520 instance Uniquable Reg where
521    getUnique (RealReg i)      = mkUnique 'C' i
522    getUnique (VirtualRegI u)  = u
523    getUnique (VirtualRegHi u) = u
524    getUnique (VirtualRegF u)  = u
525    getUnique (VirtualRegD u)  = u
526
527 unRealReg (RealReg i) = i
528 unRealReg vreg        = pprPanic "unRealReg on VirtualReg" (ppr vreg)
529
530 mkVReg :: Unique -> Size -> Reg
531 mkVReg u size
532    | not (isFloatSize size) = VirtualRegI u
533    | otherwise
534    = case size of
535 #if sparc_TARGET_ARCH
536         FF32    -> VirtualRegF u
537         FF64    -> VirtualRegD u
538 #else
539         FF32    -> VirtualRegD u
540         FF64    -> VirtualRegD u
541 #endif
542         _other -> panic "mkVReg"
543
544 isVirtualReg :: Reg -> Bool
545 isVirtualReg (RealReg _)      = False
546 isVirtualReg (VirtualRegI _)  = True
547 isVirtualReg (VirtualRegHi _) = True
548 isVirtualReg (VirtualRegF _)  = True
549 isVirtualReg (VirtualRegD _)  = True
550
551 isRealReg :: Reg -> Bool
552 isRealReg = not . isVirtualReg
553
554 renameVirtualReg :: Unique -> Reg -> Reg
555 renameVirtualReg u r
556  = case r of
557         RealReg _       -> error "renameVirtualReg: can't change unique on a real reg"
558         VirtualRegI _   -> VirtualRegI  u
559         VirtualRegHi _  -> VirtualRegHi u
560         VirtualRegF _   -> VirtualRegF  u
561         VirtualRegD _   -> VirtualRegD  u
562
563 instance Show Reg where
564     show (RealReg i)      = showReg i
565     show (VirtualRegI u)  = "%vI_" ++ show u
566     show (VirtualRegHi u) = "%vHi_" ++ show u
567     show (VirtualRegF u)  = "%vF_" ++ show u
568     show (VirtualRegD u)  = "%vD_" ++ show u
569
570 instance Outputable RegClass where
571     ppr RcInteger       = Outputable.text "I"
572     ppr RcFloat         = Outputable.text "F"
573     ppr RcDouble        = Outputable.text "D"
574
575 instance Outputable Reg where
576     ppr r = Outputable.text (show r)
577
578
579
580
581 -- trivColorable function for the graph coloring allocator
582 --      This gets hammered by scanGraph during register allocation,
583 --      so needs to be fairly efficient.
584 --
585 --      NOTE:   This only works for arcitectures with just RcInteger and RcDouble
586 --              (which are disjoint) ie. x86, x86_64 and ppc
587 --
588
589 --      BL 2007/09
590 --      Doing a nice fold over the UniqSet makes trivColorable use
591 --      32% of total compile time and 42% of total alloc when compiling SHA1.lhs from darcs.
592 {-
593 trivColorable :: RegClass -> UniqSet Reg -> UniqSet Reg -> Bool
594 trivColorable classN conflicts exclusions
595  = let
596
597         acc :: Reg -> (Int, Int) -> (Int, Int)
598         acc r (cd, cf)  
599          = case regClass r of
600                 RcInteger       -> (cd+1, cf)
601                 RcDouble        -> (cd,   cf+1)
602                 _               -> panic "MachRegs.trivColorable: reg class not handled"
603
604         tmp                     = foldUniqSet acc (0, 0) conflicts
605         (countInt,  countFloat) = foldUniqSet acc tmp    exclusions
606
607         squeese         = worst countInt   classN RcInteger
608                         + worst countFloat classN RcDouble
609
610    in   squeese < allocatableRegsInClass classN
611
612 -- | Worst case displacement
613 --      node N of classN has n neighbors of class C.
614 --
615 --      We currently only have RcInteger and RcDouble, which don't conflict at all.
616 --      This is a bit boring compared to what's in RegArchX86.
617 --
618 worst :: Int -> RegClass -> RegClass -> Int
619 worst n classN classC
620  = case classN of
621         RcInteger
622          -> case classC of
623                 RcInteger       -> min n (allocatableRegsInClass RcInteger)
624                 RcDouble        -> 0
625                 
626         RcDouble
627          -> case classC of
628                 RcDouble        -> min n (allocatableRegsInClass RcDouble)
629                 RcInteger       -> 0
630 -}
631
632
633 -- The number of allocatable regs is hard coded here so we can do a fast comparision
634 -- in trivColorable. It's ok if these numbers are _less_ than the actual number of
635 -- free regs, but they can't be more or the register conflict graph won't color.
636 --
637 -- There is an allocatableRegsInClass :: RegClass -> Int, but doing the unboxing
638 -- is too slow for us here.
639 --
640 -- Compare MachRegs.freeRegs  and MachRegs.h to get these numbers.
641 --
642 #if i386_TARGET_ARCH
643 #define ALLOCATABLE_REGS_INTEGER (_ILIT(3))
644 #define ALLOCATABLE_REGS_DOUBLE  (_ILIT(6))
645
646 #elif x86_64_TARGET_ARCH
647 #define ALLOCATABLE_REGS_INTEGER (_ILIT(5))
648 #define ALLOCATABLE_REGS_DOUBLE  (_ILIT(2))
649
650 #elif powerpc_TARGET_ARCH
651 #define ALLOCATABLE_REGS_INTEGER (_ILIT(16))
652 #define ALLOCATABLE_REGS_DOUBLE  (_ILIT(26))
653
654 #elif sparc_TARGET_ARCH
655 #define ALLOCATABLE_REGS_INTEGER (_ILIT(3))
656 #define ALLOCATABLE_REGS_DOUBLE  (_ILIT(6))
657
658 #else
659 #error ToDo: define ALLOCATABLE_REGS_INTEGER and ALLOCATABLE_REGS_DOUBLE
660 #endif
661
662 {-# INLINE regClass      #-}
663 trivColorable :: RegClass -> UniqSet Reg -> UniqSet Reg -> Bool
664 trivColorable classN conflicts exclusions
665  = {-# SCC "trivColorable" #-}
666    let
667         isSqueesed cI cF ufm
668           = case ufm of
669                 NodeUFM _ _ left right
670                  -> case isSqueesed cI cF right of
671                         (# s, cI', cF' #)
672                          -> case s of
673                                 False   -> isSqueesed cI' cF' left
674                                 True    -> (# True, cI', cF' #)
675
676                 LeafUFM _ reg
677                  -> case regClass reg of
678                         RcInteger
679                          -> case cI +# _ILIT(1) of
680                                 cI' -> (# cI' >=# ALLOCATABLE_REGS_INTEGER, cI', cF #)
681
682                         RcDouble
683                          -> case cF +# _ILIT(1) of
684                                 cF' -> (# cF' >=# ALLOCATABLE_REGS_DOUBLE,  cI, cF' #)
685
686                 EmptyUFM
687                  ->     (# False, cI, cF #)
688
689    in case isSqueesed (_ILIT(0)) (_ILIT(0)) conflicts of
690         (# False, cI', cF' #)
691          -> case isSqueesed cI' cF' exclusions of
692                 (# s, _, _ #)   -> not s
693
694         (# True, _, _ #)
695          -> False
696
697
698
699 -- -----------------------------------------------------------------------------
700 -- Machine-specific register stuff
701
702 -- The Alpha has 64 registers of interest; 32 integer registers and 32 floating
703 -- point registers.  The mapping of STG registers to alpha machine registers
704 -- is defined in StgRegs.h.  We are, of course, prepared for any eventuality.
705
706 #if alpha_TARGET_ARCH
707 fReg :: Int -> RegNo
708 fReg x = (32 + x)
709
710 v0, f0, ra, pv, gp, sp, zeroh :: Reg
711 v0    = realReg 0
712 f0    = realReg (fReg 0)
713 ra    = FixedReg ILIT(26)
714 pv    = t12
715 gp    = FixedReg ILIT(29)
716 sp    = FixedReg ILIT(30)
717 zeroh = FixedReg ILIT(31) -- "zero" is used in 1.3 (MonadZero method)
718
719 t9, t10, t11, t12 :: Reg
720 t9  = realReg 23
721 t10 = realReg 24
722 t11 = realReg 25
723 t12 = realReg 27
724 #endif
725
726 {-
727 Intel x86 architecture:
728 - All registers except 7 (esp) are available for use.
729 - Only ebx, esi, edi and esp are available across a C call (they are callee-saves).
730 - Registers 0-7 have 16-bit counterparts (ax, bx etc.)
731 - Registers 0-3 have 8 bit counterparts (ah, bh etc.)
732 - Registers 8-13 are fakes; we pretend x86 has 6 conventionally-addressable
733   fp registers, and 3-operand insns for them, and we translate this into
734   real stack-based x86 fp code after register allocation.
735
736 The fp registers are all Double registers; we don't have any RcFloat class
737 regs.  @regClass@ barfs if you give it a VirtualRegF, and mkVReg above should
738 never generate them.
739 -}
740
741 #if i386_TARGET_ARCH
742
743 fake0, fake1, fake2, fake3, fake4, fake5, 
744        eax, ebx, ecx, edx, esp, ebp, esi, edi :: Reg
745 eax   = RealReg 0
746 ebx   = RealReg 1
747 ecx   = RealReg 2
748 edx   = RealReg 3
749 esi   = RealReg 4
750 edi   = RealReg 5
751 ebp   = RealReg 6
752 esp   = RealReg 7
753 fake0 = RealReg 8
754 fake1 = RealReg 9
755 fake2 = RealReg 10
756 fake3 = RealReg 11
757 fake4 = RealReg 12
758 fake5 = RealReg 13
759
760
761 -- On x86, we might want to have an 8-bit RegClass, which would
762 -- contain just regs 1-4 (the others don't have 8-bit versions).
763 -- However, we can get away without this at the moment because the
764 -- only allocatable integer regs are also 8-bit compatible (1, 3, 4).
765 regClass (RealReg i)     = if i < 8 then RcInteger else RcDouble
766 regClass (VirtualRegI  u) = RcInteger
767 regClass (VirtualRegHi u) = RcInteger
768 regClass (VirtualRegD  u) = RcDouble
769 regClass (VirtualRegF  u) = pprPanic "regClass(x86):VirtualRegF" 
770                                     (ppr (VirtualRegF u))
771
772 regNames 
773    = ["%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp", 
774       "%fake0", "%fake1", "%fake2", "%fake3", "%fake4", "%fake5", "%fake6"]
775
776 showReg :: RegNo -> String
777 showReg n
778    = if   n >= 0 && n < 14
779      then regNames !! n
780      else "%unknown_x86_real_reg_" ++ show n
781
782
783 #endif
784
785 {-
786 AMD x86_64 architecture:
787 - Registers 0-16 have 32-bit counterparts (eax, ebx etc.)
788 - Registers 0-7 have 16-bit counterparts (ax, bx etc.)
789 - Registers 0-3 have 8 bit counterparts (ah, bh etc.)
790
791 -}
792
793 #if x86_64_TARGET_ARCH
794
795 rax, rbx, rcx, rdx, rsp, rbp, rsi, rdi, 
796   r8, r9, r10, r11, r12, r13, r14, r15,
797   xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7,
798   xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15 :: Reg
799
800 rax   = RealReg 0
801 rbx   = RealReg 1
802 rcx   = RealReg 2
803 rdx   = RealReg 3
804 rsi   = RealReg 4
805 rdi   = RealReg 5
806 rbp   = RealReg 6
807 rsp   = RealReg 7
808 r8    = RealReg 8
809 r9    = RealReg 9
810 r10   = RealReg 10
811 r11   = RealReg 11
812 r12   = RealReg 12
813 r13   = RealReg 13
814 r14   = RealReg 14
815 r15   = RealReg 15
816 xmm0  = RealReg 16
817 xmm1  = RealReg 17
818 xmm2  = RealReg 18
819 xmm3  = RealReg 19
820 xmm4  = RealReg 20
821 xmm5  = RealReg 21
822 xmm6  = RealReg 22
823 xmm7  = RealReg 23
824 xmm8  = RealReg 24
825 xmm9  = RealReg 25
826 xmm10 = RealReg 26
827 xmm11 = RealReg 27
828 xmm12 = RealReg 28
829 xmm13 = RealReg 29
830 xmm14 = RealReg 30
831 xmm15 = RealReg 31
832
833  -- so we can re-use some x86 code:
834 eax = rax
835 ebx = rbx
836 ecx = rcx
837 edx = rdx
838 esi = rsi
839 edi = rdi
840 ebp = rbp
841 esp = rsp
842
843 xmm n = RealReg (16+n)
844
845 -- On x86, we might want to have an 8-bit RegClass, which would
846 -- contain just regs 1-4 (the others don't have 8-bit versions).
847 -- However, we can get away without this at the moment because the
848 -- only allocatable integer regs are also 8-bit compatible (1, 3, 4).
849 regClass (RealReg i)     = if i < 16 then RcInteger else RcDouble
850 regClass (VirtualRegI  u) = RcInteger
851 regClass (VirtualRegHi u) = RcInteger
852 regClass (VirtualRegD  u) = RcDouble
853 regClass (VirtualRegF  u) = pprPanic "regClass(x86_64):VirtualRegF" 
854                                     (ppr (VirtualRegF u))
855
856 regNames 
857  = ["%rax", "%rbx", "%rcx", "%rdx", "%rsi", "%rdi", "%rbp", "%rsp" ]
858
859 showReg :: RegNo -> String
860 showReg n
861   | n >= 16 = "%xmm" ++ show (n-16)
862   | n >= 8  = "%r" ++ show n
863   | otherwise = regNames !! n
864
865 #endif
866
867 {-
868 The SPARC has 64 registers of interest; 32 integer registers and 32
869 floating point registers.  The mapping of STG registers to SPARC
870 machine registers is defined in StgRegs.h.  We are, of course,
871 prepared for any eventuality.
872
873 The whole fp-register pairing thing on sparcs is a huge nuisance.  See
874 fptools/ghc/includes/MachRegs.h for a description of what's going on
875 here.
876 -}
877
878 #if sparc_TARGET_ARCH
879
880 gReg,lReg,iReg,oReg,fReg :: Int -> RegNo
881 gReg x = x
882 oReg x = (8 + x)
883 lReg x = (16 + x)
884 iReg x = (24 + x)
885 fReg x = (32 + x)
886
887 nCG_FirstFloatReg :: RegNo
888 nCG_FirstFloatReg = unRealReg NCG_FirstFloatReg
889
890 regClass (VirtualRegI u) = RcInteger
891 regClass (VirtualRegF u) = RcFloat
892 regClass (VirtualRegD u) = RcDouble
893 regClass (RealReg i) | i < 32                = RcInteger 
894                      | i < nCG_FirstFloatReg = RcDouble
895                      | otherwise             = RcFloat
896
897 showReg :: RegNo -> String
898 showReg n
899    | n >= 0  && n < 8   = "%g" ++ show n
900    | n >= 8  && n < 16  = "%o" ++ show (n-8)
901    | n >= 16 && n < 24  = "%l" ++ show (n-16)
902    | n >= 24 && n < 32  = "%i" ++ show (n-24)
903    | n >= 32 && n < 64  = "%f" ++ show (n-32)
904    | otherwise          = "%unknown_sparc_real_reg_" ++ show n
905
906 g0, g1, g2, fp, sp, o0, o1, f0, f1, f6, f8, f22, f26, f27 :: Reg
907
908 f6  = RealReg (fReg 6)
909 f8  = RealReg (fReg 8)
910 f22 = RealReg (fReg 22)
911 f26 = RealReg (fReg 26)
912 f27 = RealReg (fReg 27)
913
914
915 -- g0 is useful for codegen; is always zero, and writes to it vanish.
916 g0  = RealReg (gReg 0)
917 g1  = RealReg (gReg 1)
918 g2  = RealReg (gReg 2)
919
920 -- FP, SP, int and float return (from C) regs.
921 fp  = RealReg (iReg 6)
922 sp  = RealReg (oReg 6)
923 o0  = RealReg (oReg 0)
924 o1  = RealReg (oReg 1)
925 f0  = RealReg (fReg 0)
926 f1  = RealReg (fReg 1)
927
928 #endif
929
930 {-
931 The PowerPC has 64 registers of interest; 32 integer registers and 32 floating
932 point registers.
933 -}
934
935 #if powerpc_TARGET_ARCH
936 fReg :: Int -> RegNo
937 fReg x = (32 + x)
938
939 regClass (VirtualRegI  u) = RcInteger
940 regClass (VirtualRegHi u) = RcInteger
941 regClass (VirtualRegF  u) = pprPanic "regClass(ppc):VirtualRegF" 
942                                     (ppr (VirtualRegF u))
943 regClass (VirtualRegD u) = RcDouble
944 regClass (RealReg i) | i < 32                = RcInteger 
945                      | otherwise             = RcDouble
946
947 showReg :: RegNo -> String
948 showReg n
949     | n >= 0 && n <= 31   = "%r" ++ show n
950     | n >= 32 && n <= 63  = "%f" ++ show (n - 32)
951     | otherwise           = "%unknown_powerpc_real_reg_" ++ show n
952
953 sp = RealReg 1
954 r3 = RealReg 3
955 r4 = RealReg 4
956 r27 = RealReg 27
957 r28 = RealReg 28
958 f1 = RealReg $ fReg 1
959 f20 = RealReg $ fReg 20
960 f21 = RealReg $ fReg 21
961 #endif
962
963 {-
964 Redefine the literals used for machine-registers with non-numeric
965 names in the header files.  Gag me with a spoon, eh?
966 -}
967
968 #if alpha_TARGET_ARCH
969 #define f0 32
970 #define f1 33
971 #define f2 34
972 #define f3 35
973 #define f4 36
974 #define f5 37
975 #define f6 38
976 #define f7 39
977 #define f8 40
978 #define f9 41
979 #define f10 42
980 #define f11 43
981 #define f12 44
982 #define f13 45
983 #define f14 46
984 #define f15 47
985 #define f16 48
986 #define f17 49
987 #define f18 50
988 #define f19 51
989 #define f20 52
990 #define f21 53
991 #define f22 54
992 #define f23 55
993 #define f24 56
994 #define f25 57
995 #define f26 58
996 #define f27 59
997 #define f28 60
998 #define f29 61
999 #define f30 62
1000 #define f31 63
1001 #endif
1002 #if i386_TARGET_ARCH
1003 #define eax 0
1004 #define ebx 1
1005 #define ecx 2
1006 #define edx 3
1007 #define esi 4
1008 #define edi 5
1009 #define ebp 6
1010 #define esp 7
1011 #define fake0 8
1012 #define fake1 9
1013 #define fake2 10
1014 #define fake3 11
1015 #define fake4 12
1016 #define fake5 13
1017 #endif
1018
1019 #if x86_64_TARGET_ARCH
1020 #define rax   0
1021 #define rbx   1
1022 #define rcx   2
1023 #define rdx   3
1024 #define rsi   4
1025 #define rdi   5
1026 #define rbp   6
1027 #define rsp   7
1028 #define r8    8
1029 #define r9    9
1030 #define r10   10
1031 #define r11   11
1032 #define r12   12
1033 #define r13   13
1034 #define r14   14
1035 #define r15   15
1036 #define xmm0  16
1037 #define xmm1  17
1038 #define xmm2  18
1039 #define xmm3  19
1040 #define xmm4  20
1041 #define xmm5  21
1042 #define xmm6  22
1043 #define xmm7  23
1044 #define xmm8  24
1045 #define xmm9  25
1046 #define xmm10 26
1047 #define xmm11 27
1048 #define xmm12 28
1049 #define xmm13 29
1050 #define xmm14 30
1051 #define xmm15 31
1052 #endif
1053
1054 #if sparc_TARGET_ARCH
1055 #define g0 0
1056 #define g1 1
1057 #define g2 2
1058 #define g3 3
1059 #define g4 4
1060 #define g5 5
1061 #define g6 6
1062 #define g7 7
1063 #define o0 8
1064 #define o1 9
1065 #define o2 10
1066 #define o3 11
1067 #define o4 12
1068 #define o5 13
1069 #define o6 14
1070 #define o7 15
1071 #define l0 16
1072 #define l1 17
1073 #define l2 18
1074 #define l3 19
1075 #define l4 20
1076 #define l5 21
1077 #define l6 22
1078 #define l7 23
1079 #define i0 24
1080 #define i1 25
1081 #define i2 26
1082 #define i3 27
1083 #define i4 28
1084 #define i5 29
1085 #define i6 30
1086 #define i7 31
1087
1088 #define f0  32
1089 #define f1  33
1090 #define f2  34
1091 #define f3  35
1092 #define f4  36
1093 #define f5  37
1094 #define f6  38
1095 #define f7  39
1096 #define f8  40
1097 #define f9  41
1098 #define f10 42
1099 #define f11 43
1100 #define f12 44
1101 #define f13 45
1102 #define f14 46
1103 #define f15 47
1104 #define f16 48
1105 #define f17 49
1106 #define f18 50
1107 #define f19 51
1108 #define f20 52
1109 #define f21 53
1110 #define f22 54
1111 #define f23 55
1112 #define f24 56
1113 #define f25 57
1114 #define f26 58
1115 #define f27 59
1116 #define f28 60
1117 #define f29 61
1118 #define f30 62
1119 #define f31 63
1120 #endif
1121
1122 #if powerpc_TARGET_ARCH
1123 #define r0 0
1124 #define r1 1
1125 #define r2 2
1126 #define r3 3
1127 #define r4 4
1128 #define r5 5
1129 #define r6 6
1130 #define r7 7
1131 #define r8 8
1132 #define r9 9
1133 #define r10 10
1134 #define r11 11
1135 #define r12 12
1136 #define r13 13
1137 #define r14 14
1138 #define r15 15
1139 #define r16 16
1140 #define r17 17
1141 #define r18 18
1142 #define r19 19
1143 #define r20 20
1144 #define r21 21
1145 #define r22 22
1146 #define r23 23
1147 #define r24 24
1148 #define r25 25
1149 #define r26 26
1150 #define r27 27
1151 #define r28 28
1152 #define r29 29
1153 #define r30 30
1154 #define r31 31
1155
1156 #ifdef darwin_TARGET_OS
1157 #define f0  32
1158 #define f1  33
1159 #define f2  34
1160 #define f3  35
1161 #define f4  36
1162 #define f5  37
1163 #define f6  38
1164 #define f7  39
1165 #define f8  40
1166 #define f9  41
1167 #define f10 42
1168 #define f11 43
1169 #define f12 44
1170 #define f13 45
1171 #define f14 46
1172 #define f15 47
1173 #define f16 48
1174 #define f17 49
1175 #define f18 50
1176 #define f19 51
1177 #define f20 52
1178 #define f21 53
1179 #define f22 54
1180 #define f23 55
1181 #define f24 56
1182 #define f25 57
1183 #define f26 58
1184 #define f27 59
1185 #define f28 60
1186 #define f29 61
1187 #define f30 62
1188 #define f31 63
1189 #else
1190 #define fr0  32
1191 #define fr1  33
1192 #define fr2  34
1193 #define fr3  35
1194 #define fr4  36
1195 #define fr5  37
1196 #define fr6  38
1197 #define fr7  39
1198 #define fr8  40
1199 #define fr9  41
1200 #define fr10 42
1201 #define fr11 43
1202 #define fr12 44
1203 #define fr13 45
1204 #define fr14 46
1205 #define fr15 47
1206 #define fr16 48
1207 #define fr17 49
1208 #define fr18 50
1209 #define fr19 51
1210 #define fr20 52
1211 #define fr21 53
1212 #define fr22 54
1213 #define fr23 55
1214 #define fr24 56
1215 #define fr25 57
1216 #define fr26 58
1217 #define fr27 59
1218 #define fr28 60
1219 #define fr29 61
1220 #define fr30 62
1221 #define fr31 63
1222 #endif
1223 #endif
1224
1225
1226 -- allMachRegs is the complete set of machine regs.
1227 allMachRegNos :: [RegNo]
1228 allMachRegNos
1229    = IF_ARCH_alpha( [0..63],
1230      IF_ARCH_i386(  [0..13],
1231      IF_ARCH_x86_64( [0..31],
1232      IF_ARCH_sparc( ([0..31]
1233                      ++ [f0,f2 .. nCG_FirstFloatReg-1]
1234                      ++ [nCG_FirstFloatReg .. f31]),
1235      IF_ARCH_powerpc([0..63],
1236                    )))))
1237
1238 -- allocatableRegs is allMachRegNos with the fixed-use regs removed.
1239 -- i.e., these are the regs for which we are prepared to allow the
1240 -- register allocator to attempt to map VRegs to.
1241 allocatableRegs :: [RegNo]
1242 allocatableRegs
1243    = let isFree i = isFastTrue (freeReg i)
1244      in  filter isFree allMachRegNos
1245
1246
1247 -- | The number of regs in each class.
1248 --      We go via top level CAFs to ensure that we're not recomputing
1249 --      the length of these lists each time the fn is called.
1250 allocatableRegsInClass :: RegClass -> Int
1251 allocatableRegsInClass cls
1252  = case cls of
1253         RcInteger       -> allocatableRegsInteger
1254         RcDouble        -> allocatableRegsDouble
1255
1256 allocatableRegsInteger  
1257         = length $ filter (\r -> regClass r == RcInteger) 
1258                  $ map RealReg allocatableRegs
1259
1260 allocatableRegsDouble
1261         = length $ filter (\r -> regClass r == RcDouble) 
1262                  $ map RealReg allocatableRegs
1263
1264
1265 -- these are the regs which we cannot assume stay alive over a
1266 -- C call.  
1267 callClobberedRegs :: [Reg]
1268 callClobberedRegs
1269   =
1270 #if alpha_TARGET_ARCH
1271     [0, 1, 2, 3, 4, 5, 6, 7, 8,
1272      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
1273      fReg 0, fReg 1, fReg 10, fReg 11, fReg 12, fReg 13, fReg 14, fReg 15,
1274      fReg 16, fReg 17, fReg 18, fReg 19, fReg 20, fReg 21, fReg 22, fReg 23,
1275      fReg 24, fReg 25, fReg 26, fReg 27, fReg 28, fReg 29, fReg 30]
1276 #endif /* alpha_TARGET_ARCH */
1277 #if i386_TARGET_ARCH
1278     -- caller-saves registers
1279     map RealReg [eax,ecx,edx,fake0,fake1,fake2,fake3,fake4,fake5]
1280 #endif /* i386_TARGET_ARCH */
1281 #if x86_64_TARGET_ARCH
1282     -- caller-saves registers
1283     map RealReg ([rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11] ++ [16..31])
1284        -- all xmm regs are caller-saves
1285 #endif /* x86_64_TARGET_ARCH */
1286 #if sparc_TARGET_ARCH
1287     map RealReg 
1288         ( oReg 7 :
1289           [oReg i | i <- [0..5]] ++
1290           [gReg i | i <- [1..7]] ++
1291           [fReg i | i <- [0..31]] )
1292 #endif /* sparc_TARGET_ARCH */
1293 #if powerpc_TARGET_ARCH
1294 #if darwin_TARGET_OS
1295     map RealReg (0:[2..12] ++ map fReg [0..13])
1296 #elif linux_TARGET_OS
1297     map RealReg (0:[2..13] ++ map fReg [0..13])
1298 #endif
1299 #endif /* powerpc_TARGET_ARCH */
1300
1301
1302 -- argRegs is the set of regs which are read for an n-argument call to C.
1303 -- For archs which pass all args on the stack (x86), is empty.
1304 -- Sparc passes up to the first 6 args in regs.
1305 -- Dunno about Alpha.
1306 argRegs :: RegNo -> [Reg]
1307
1308 #if i386_TARGET_ARCH
1309 argRegs _ = panic "MachRegs.argRegs(x86): should not be used!"
1310 #endif
1311
1312 #if x86_64_TARGET_ARCH
1313 argRegs _ = panic "MachRegs.argRegs(x86_64): should not be used!"
1314 #endif
1315
1316 #if alpha_TARGET_ARCH
1317 argRegs 0 = []
1318 argRegs 1 = freeMappedRegs [16, fReg 16]
1319 argRegs 2 = freeMappedRegs [16, 17, fReg 16, fReg 17]
1320 argRegs 3 = freeMappedRegs [16, 17, 18, fReg 16, fReg 17, fReg 18]
1321 argRegs 4 = freeMappedRegs [16, 17, 18, 19, fReg 16, fReg 17, fReg 18, fReg 19]
1322 argRegs 5 = freeMappedRegs [16, 17, 18, 19, 20, fReg 16, fReg 17, fReg 18, fReg 19, fReg 20]
1323 argRegs 6 = freeMappedRegs [16, 17, 18, 19, 20, 21, fReg 16, fReg 17, fReg 18, fReg 19, fReg 20, fReg 21]
1324 argRegs _ = panic "MachRegs.argRegs(alpha): don't know about >6 arguments!"
1325 #endif /* alpha_TARGET_ARCH */
1326
1327 #if sparc_TARGET_ARCH
1328 argRegs 0 = []
1329 argRegs 1 = map (RealReg . oReg) [0]
1330 argRegs 2 = map (RealReg . oReg) [0,1]
1331 argRegs 3 = map (RealReg . oReg) [0,1,2]
1332 argRegs 4 = map (RealReg . oReg) [0,1,2,3]
1333 argRegs 5 = map (RealReg . oReg) [0,1,2,3,4]
1334 argRegs 6 = map (RealReg . oReg) [0,1,2,3,4,5]
1335 argRegs _ = panic "MachRegs.argRegs(sparc): don't know about >6 arguments!"
1336 #endif /* sparc_TARGET_ARCH */
1337
1338 #if powerpc_TARGET_ARCH
1339 argRegs 0 = []
1340 argRegs 1 = map RealReg [3]
1341 argRegs 2 = map RealReg [3,4]
1342 argRegs 3 = map RealReg [3..5]
1343 argRegs 4 = map RealReg [3..6]
1344 argRegs 5 = map RealReg [3..7]
1345 argRegs 6 = map RealReg [3..8]
1346 argRegs 7 = map RealReg [3..9]
1347 argRegs 8 = map RealReg [3..10]
1348 argRegs _ = panic "MachRegs.argRegs(powerpc): don't know about >8 arguments!"
1349 #endif /* powerpc_TARGET_ARCH */
1350
1351
1352 -- all of the arg regs ??
1353 #if alpha_TARGET_ARCH
1354 allArgRegs :: [(Reg, Reg)]
1355 allArgRegs = [(realReg i, realReg (fReg i)) | i <- [16..21]]
1356 #endif /* alpha_TARGET_ARCH */
1357
1358 #if sparc_TARGET_ARCH
1359 allArgRegs :: [Reg]
1360 allArgRegs = map RealReg [oReg i | i <- [0..5]]
1361 #endif /* sparc_TARGET_ARCH */
1362
1363 #if i386_TARGET_ARCH
1364 allArgRegs :: [Reg]
1365 allArgRegs = panic "MachRegs.allArgRegs(x86): should not be used!"
1366 #endif
1367
1368 #if x86_64_TARGET_ARCH
1369 allArgRegs :: [Reg]
1370 allArgRegs = map RealReg [rdi,rsi,rdx,rcx,r8,r9]
1371 allFPArgRegs :: [Reg]
1372 allFPArgRegs = map RealReg [xmm0 .. xmm7]
1373 #endif
1374
1375 #if powerpc_TARGET_ARCH
1376 allArgRegs :: [Reg]
1377 allArgRegs = map RealReg [3..10]
1378 allFPArgRegs :: [Reg]
1379 #if darwin_TARGET_OS
1380 allFPArgRegs = map (RealReg . fReg) [1..13]
1381 #elif linux_TARGET_OS
1382 allFPArgRegs = map (RealReg . fReg) [1..8]
1383 #endif
1384 #endif /* powerpc_TARGET_ARCH */
1385 \end{code}
1386
1387 \begin{code}
1388 freeReg :: RegNo -> FastBool
1389
1390 #if alpha_TARGET_ARCH
1391 freeReg 26 = fastBool False  -- return address (ra)
1392 freeReg 28 = fastBool False  -- reserved for the assembler (at)
1393 freeReg 29 = fastBool False  -- global pointer (gp)
1394 freeReg 30 = fastBool False  -- stack pointer (sp)
1395 freeReg 31 = fastBool False  -- always zero (zeroh)
1396 freeReg 63 = fastBool False  -- always zero (f31)
1397 #endif
1398
1399 #if i386_TARGET_ARCH
1400 freeReg esp = fastBool False  --        %esp is the C stack pointer
1401 #endif
1402
1403 #if x86_64_TARGET_ARCH
1404 freeReg rsp = fastBool False  --        %rsp is the C stack pointer
1405 #endif
1406
1407 #if sparc_TARGET_ARCH
1408 freeReg g0 = fastBool False  -- %g0 is always 0.
1409
1410 freeReg g5 = fastBool False  -- %g5 is reserved (ABI).
1411 freeReg g6 = fastBool False  -- %g6 is reserved (ABI).
1412 freeReg g7 = fastBool False  -- %g7 is reserved (ABI).
1413 freeReg i6 = fastBool False  -- %i6 is our frame pointer.
1414 freeReg i7 = fastBool False  -- %i7 tends to have ret-addr-ish things
1415 freeReg o6 = fastBool False  -- %o6 is our stack pointer.
1416 freeReg o7 = fastBool False  -- %o7 holds ret addrs (???)
1417 freeReg f0 = fastBool False  --  %f0/%f1 are the C fp return registers.
1418 freeReg f1 = fastBool False
1419
1420 -- TODO: Not sure about these BL 2009/01/10
1421 --      Used for NCG spill tmps? what is this?
1422
1423 {-
1424 freeReg g1  = fastBool False  -- %g1 is used for NCG spill tmp
1425 freeReg g2  = fastBool False 
1426 freeReg f6  = fastBool False
1427 freeReg f8  = fastBool False
1428 freeReg f26 = fastBool False
1429 freeReg f27 = fastBool False
1430 -}
1431
1432 #endif
1433
1434 #if powerpc_TARGET_ARCH
1435 freeReg 0 = fastBool False -- Hack: r0 can't be used in all insns, but it's actually free
1436 freeReg 1 = fastBool False -- The Stack Pointer
1437 #if !darwin_TARGET_OS
1438  -- most non-darwin powerpc OSes use r2 as a TOC pointer or something like that
1439 freeReg 2 = fastBool False
1440 #endif
1441 #endif
1442
1443 #ifdef REG_Base
1444 freeReg REG_Base = fastBool False
1445 #endif
1446 #ifdef REG_R1
1447 freeReg REG_R1   = fastBool False
1448 #endif  
1449 #ifdef REG_R2  
1450 freeReg REG_R2   = fastBool False
1451 #endif  
1452 #ifdef REG_R3  
1453 freeReg REG_R3   = fastBool False
1454 #endif  
1455 #ifdef REG_R4  
1456 freeReg REG_R4   = fastBool False
1457 #endif  
1458 #ifdef REG_R5  
1459 freeReg REG_R5   = fastBool False
1460 #endif  
1461 #ifdef REG_R6  
1462 freeReg REG_R6   = fastBool False
1463 #endif  
1464 #ifdef REG_R7  
1465 freeReg REG_R7   = fastBool False
1466 #endif  
1467 #ifdef REG_R8  
1468 freeReg REG_R8   = fastBool False
1469 #endif
1470 #ifdef REG_F1
1471 freeReg REG_F1 = fastBool False
1472 #endif
1473 #ifdef REG_F2
1474 freeReg REG_F2 = fastBool False
1475 #endif
1476 #ifdef REG_F3
1477 freeReg REG_F3 = fastBool False
1478 #endif
1479 #ifdef REG_F4
1480 freeReg REG_F4 = fastBool False
1481 #endif
1482 #ifdef REG_D1
1483 freeReg REG_D1 = fastBool False
1484 #endif
1485 #ifdef REG_D2
1486 freeReg REG_D2 = fastBool False
1487 #endif
1488 #ifdef REG_Sp 
1489 freeReg REG_Sp   = fastBool False
1490 #endif 
1491 #ifdef REG_Su
1492 freeReg REG_Su   = fastBool False
1493 #endif 
1494 #ifdef REG_SpLim 
1495 freeReg REG_SpLim = fastBool False
1496 #endif 
1497 #ifdef REG_Hp 
1498 freeReg REG_Hp   = fastBool False
1499 #endif
1500 #ifdef REG_HpLim
1501 freeReg REG_HpLim = fastBool False
1502 #endif
1503 freeReg n               = fastBool True
1504
1505
1506 --  | Returns 'Nothing' if this global register is not stored
1507 -- in a real machine register, otherwise returns @'Just' reg@, where
1508 -- reg is the machine register it is stored in.
1509
1510 globalRegMaybe :: GlobalReg -> Maybe Reg
1511
1512 #ifdef REG_Base
1513 globalRegMaybe BaseReg                  = Just (RealReg REG_Base)
1514 #endif
1515 #ifdef REG_R1
1516 globalRegMaybe (VanillaReg 1 _)         = Just (RealReg REG_R1)
1517 #endif 
1518 #ifdef REG_R2 
1519 globalRegMaybe (VanillaReg 2 _)         = Just (RealReg REG_R2)
1520 #endif 
1521 #ifdef REG_R3 
1522 globalRegMaybe (VanillaReg 3 _)         = Just (RealReg REG_R3)
1523 #endif 
1524 #ifdef REG_R4 
1525 globalRegMaybe (VanillaReg 4 _)         = Just (RealReg REG_R4)
1526 #endif 
1527 #ifdef REG_R5 
1528 globalRegMaybe (VanillaReg 5 _)         = Just (RealReg REG_R5)
1529 #endif 
1530 #ifdef REG_R6 
1531 globalRegMaybe (VanillaReg 6 _)         = Just (RealReg REG_R6)
1532 #endif 
1533 #ifdef REG_R7 
1534 globalRegMaybe (VanillaReg 7 _)         = Just (RealReg REG_R7)
1535 #endif 
1536 #ifdef REG_R8 
1537 globalRegMaybe (VanillaReg 8 _)         = Just (RealReg REG_R8)
1538 #endif
1539 #ifdef REG_R9 
1540 globalRegMaybe (VanillaReg 9 _)         = Just (RealReg REG_R9)
1541 #endif
1542 #ifdef REG_R10 
1543 globalRegMaybe (VanillaReg 10 _)        = Just (RealReg REG_R10)
1544 #endif
1545 #ifdef REG_F1
1546 globalRegMaybe (FloatReg 1)             = Just (RealReg REG_F1)
1547 #endif                                  
1548 #ifdef REG_F2                           
1549 globalRegMaybe (FloatReg 2)             = Just (RealReg REG_F2)
1550 #endif                                  
1551 #ifdef REG_F3                           
1552 globalRegMaybe (FloatReg 3)             = Just (RealReg REG_F3)
1553 #endif                                  
1554 #ifdef REG_F4                           
1555 globalRegMaybe (FloatReg 4)             = Just (RealReg REG_F4)
1556 #endif                                  
1557 #ifdef REG_D1                           
1558 globalRegMaybe (DoubleReg 1)            = Just (RealReg REG_D1)
1559 #endif                                  
1560 #ifdef REG_D2                           
1561 globalRegMaybe (DoubleReg 2)            = Just (RealReg REG_D2)
1562 #endif
1563 #ifdef REG_Sp       
1564 globalRegMaybe Sp                       = Just (RealReg REG_Sp)
1565 #endif
1566 #ifdef REG_Lng1                         
1567 globalRegMaybe (LongReg 1)              = Just (RealReg REG_Lng1)
1568 #endif                                  
1569 #ifdef REG_Lng2                         
1570 globalRegMaybe (LongReg 2)              = Just (RealReg REG_Lng2)
1571 #endif
1572 #ifdef REG_SpLim                                
1573 globalRegMaybe SpLim                    = Just (RealReg REG_SpLim)
1574 #endif                                  
1575 #ifdef REG_Hp                           
1576 globalRegMaybe Hp                       = Just (RealReg REG_Hp)
1577 #endif                                  
1578 #ifdef REG_HpLim                        
1579 globalRegMaybe HpLim                    = Just (RealReg REG_HpLim)
1580 #endif                                  
1581 #ifdef REG_CurrentTSO                           
1582 globalRegMaybe CurrentTSO               = Just (RealReg REG_CurrentTSO)
1583 #endif                                  
1584 #ifdef REG_CurrentNursery                       
1585 globalRegMaybe CurrentNursery           = Just (RealReg REG_CurrentNursery)
1586 #endif                                  
1587 globalRegMaybe _                        = Nothing
1588
1589
1590 \end{code}