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