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