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