Regs.hs 14.6 KB
Newer Older
1 2 3 4 5 6 7
-- -----------------------------------------------------------------------------
--
-- (c) The University of Glasgow 1994-2004
-- 
-- -----------------------------------------------------------------------------

module PPC.Regs (
8 9 10 11 12 13 14
	-- squeeze functions
	virtualRegSqueeze,
	realRegSqueeze,

	mkVirtualReg,
	regDotColor,

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
	-- immediates
	Imm(..),
	strImmLit,
	litToImm,

	-- addressing modes
	AddrMode(..),
	addrOffset,

	-- registers
	spRel,
	argRegs,
	allArgRegs,
	callClobberedRegs,
	allMachRegNos,
30
	classOfRealReg,
31 32 33 34 35 36 37 38 39 40 41
	showReg,
	
	-- machine specific
	allFPArgRegs,
	fits16Bits,
	makeImmediate,
	fReg,
	sp, r3, r4, r27, r28, f1, f20, f21,

	-- horrow show
	freeReg,
42 43 44 45
	globalRegMaybe,
	get_GlobalReg_reg_or_addr,
	allocatableRegs

46 47 48 49 50 51
)

where

#include "nativeGen/NCG.h"
#include "HsVersions.h"
Simon Marlow's avatar
Simon Marlow committed
52
#include "../includes/stg/MachRegs.h"
53

54 55
import Reg
import RegClass
56
import Size
57

58
import CgUtils          ( get_GlobalReg_addr )
59 60 61
import BlockId
import Cmm
import CLabel           ( CLabel )
62 63
import Unique

64
import Pretty
65
import Outputable       ( panic, SDoc )	
66 67 68
import qualified Outputable
import Constants
import FastBool
69
import FastTypes
70 71 72 73 74

import Data.Word	( Word8, Word16, Word32 )
import Data.Int 	( Int8, Int16, Int32 )


75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
-- squeese functions for the graph allocator -----------------------------------

-- | regSqueeze_class reg
--	Calculuate the maximum number of register colors that could be
--	denied to a node of this class due to having this reg 
--	as a neighbour.
--
{-# INLINE virtualRegSqueeze #-}
virtualRegSqueeze :: RegClass -> VirtualReg -> FastInt
virtualRegSqueeze cls vr
 = case cls of
 	RcInteger
	 -> case vr of
	 	VirtualRegI{}		-> _ILIT(1)
		VirtualRegHi{}		-> _ILIT(1)
90
                _other                  -> _ILIT(0)
91 92 93 94 95

	RcDouble
	 -> case vr of
		VirtualRegD{}		-> _ILIT(1)
		VirtualRegF{}		-> _ILIT(0)
96
                _other                  -> _ILIT(0)
97

98
        _other -> _ILIT(0)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

{-# INLINE realRegSqueeze #-}
realRegSqueeze :: RegClass -> RealReg -> FastInt
realRegSqueeze cls rr
 = case cls of
 	RcInteger
	 -> case rr of
	 	RealRegSingle regNo
			| regNo	< 32	-> _ILIT(1)	-- first fp reg is 32 
			| otherwise	-> _ILIT(0)
			
		RealRegPair{}		-> _ILIT(0)

	RcDouble
	 -> case rr of
	 	RealRegSingle regNo
			| regNo	< 32	-> _ILIT(0)
			| otherwise	-> _ILIT(1)
			
		RealRegPair{}		-> _ILIT(0)

120 121
        _other -> _ILIT(0)

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
mkVirtualReg :: Unique -> Size -> VirtualReg
mkVirtualReg u size
   | not (isFloatSize size) = VirtualRegI u
   | otherwise
   = case size of
        FF32    -> VirtualRegD u
        FF64    -> VirtualRegD u
        _       -> panic "mkVirtualReg"

regDotColor :: RealReg -> SDoc
regDotColor reg
 = case classOfRealReg reg of
        RcInteger       -> Outputable.text "blue"
        RcFloat         -> Outputable.text "red"
        RcDouble        -> Outputable.text "green"
137
        RcDoubleSSE     -> Outputable.text "yellow"
138 139


140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
-- immediates ------------------------------------------------------------------
data Imm
	= ImmInt	Int
	| ImmInteger	Integer	    -- Sigh.
	| ImmCLbl	CLabel	    -- AbstractC Label (with baggage)
	| ImmLit	Doc	    -- Simple string
	| ImmIndex    CLabel Int
	| ImmFloat	Rational
	| ImmDouble	Rational
	| ImmConstantSum Imm Imm
	| ImmConstantDiff Imm Imm
	| LO Imm
	| HI Imm
	| HA Imm	{- high halfword adjusted -}


strImmLit :: String -> Imm
strImmLit s = ImmLit (text s)


litToImm :: CmmLit -> Imm
litToImm (CmmInt i w)        = ImmInteger (narrowS w i)
                -- narrow to the width: a CmmInt might be out of
                -- range, but we assume that ImmInteger only contains
                -- in-range values.  A signed value should be fine here.
litToImm (CmmFloat f W32)    = ImmFloat f
litToImm (CmmFloat f W64)    = ImmDouble f
litToImm (CmmLabel l)        = ImmCLbl l
litToImm (CmmLabelOff l off) = ImmIndex l off
litToImm (CmmLabelDiffOff l1 l2 off)
                             = ImmConstantSum
                               (ImmConstantDiff (ImmCLbl l1) (ImmCLbl l2))
                               (ImmInt off)
litToImm (CmmBlock id)       = ImmCLbl (infoTblLbl id)
litToImm _                   = panic "PPC.Regs.litToImm: no match"


-- addressing modes ------------------------------------------------------------

data AddrMode
	= AddrRegReg	Reg Reg
	| AddrRegImm	Reg Imm


addrOffset :: AddrMode -> Int -> Maybe AddrMode
addrOffset addr off
  = case addr of
      AddrRegImm r (ImmInt n)
       | fits16Bits n2 -> Just (AddrRegImm r (ImmInt n2))
       | otherwise     -> Nothing
       where n2 = n + off

      AddrRegImm r (ImmInteger n)
       | fits16Bits n2 -> Just (AddrRegImm r (ImmInt (fromInteger n2)))
       | otherwise     -> Nothing
       where n2 = n + toInteger off
       
      _ -> Nothing


-- registers -------------------------------------------------------------------
-- @spRel@ gives us a stack relative addressing mode for volatile
-- temporaries and for excess call arguments.  @fpRel@, where
-- applicable, is the same but for the frame pointer.

spRel :: Int	-- desired stack offset in words, positive or negative
      -> AddrMode

spRel n	= AddrRegImm sp (ImmInt (n * wORD_SIZE))


-- argRegs is the set of regs which are read for an n-argument call to C.
-- For archs which pass all args on the stack (x86), is empty.
-- Sparc passes up to the first 6 args in regs.
-- Dunno about Alpha.
argRegs :: RegNo -> [Reg]
argRegs 0 = []
217 218 219 220 221 222 223 224
argRegs 1 = map regSingle [3]
argRegs 2 = map regSingle [3,4]
argRegs 3 = map regSingle [3..5]
argRegs 4 = map regSingle [3..6]
argRegs 5 = map regSingle [3..7]
argRegs 6 = map regSingle [3..8]
argRegs 7 = map regSingle [3..9]
argRegs 8 = map regSingle [3..10]
225 226 227 228
argRegs _ = panic "MachRegs.argRegs(powerpc): don't know about >8 arguments!"


allArgRegs :: [Reg]
229
allArgRegs = map regSingle [3..10]
230 231 232 233 234 235


-- these are the regs which we cannot assume stay alive over a C call.  
callClobberedRegs :: [Reg]
#if   defined(darwin_TARGET_OS)
callClobberedRegs
236
  = map regSingle (0:[2..12] ++ map fReg [0..13])
237 238 239

#elif defined(linux_TARGET_OS)
callClobberedRegs
240
  = map regSingle (0:[2..13] ++ map fReg [0..13])
241 242 243 244 245 246 247 248 249 250 251

#else
callClobberedRegs
	= panic "PPC.Regs.callClobberedRegs: not defined for this architecture"
#endif


allMachRegNos 	:: [RegNo]
allMachRegNos	= [0..63]


252 253 254
{-# INLINE classOfRealReg      #-}
classOfRealReg :: RealReg -> RegClass
classOfRealReg (RealRegSingle i)
255 256 257
	| i < 32	= RcInteger 
	| otherwise	= RcDouble

258
classOfRealReg (RealRegPair{})
259
	= panic "regClass(ppr): no reg pairs on this architecture"
260 261 262 263 264 265 266 267 268 269 270 271 272

showReg :: RegNo -> String
showReg n
    | n >= 0 && n <= 31	  = "%r" ++ show n
    | n >= 32 && n <= 63  = "%f" ++ show (n - 32)
    | otherwise           = "%unknown_powerpc_real_reg_" ++ show n



-- machine specific ------------------------------------------------------------

allFPArgRegs :: [Reg]
#if    defined(darwin_TARGET_OS)
273
allFPArgRegs = map (regSingle . fReg) [1..13]
274 275

#elif  defined(linux_TARGET_OS)
276
allFPArgRegs = map (regSingle . fReg) [1..8]
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

#else
allFPArgRegs = panic "PPC.Regs.allFPArgRegs: not defined for this architecture"

#endif

fits16Bits :: Integral a => a -> Bool
fits16Bits x = x >= -32768 && x < 32768

makeImmediate :: Integral a => Width -> Bool -> a -> Maybe Imm
makeImmediate rep signed x = fmap ImmInt (toI16 rep signed)
    where
        narrow W32 False = fromIntegral (fromIntegral x :: Word32)
        narrow W16 False = fromIntegral (fromIntegral x :: Word16)
        narrow W8  False = fromIntegral (fromIntegral x :: Word8)
        narrow W32 True  = fromIntegral (fromIntegral x :: Int32)
        narrow W16 True  = fromIntegral (fromIntegral x :: Int16)
        narrow W8  True  = fromIntegral (fromIntegral x :: Int8)
	narrow _   _     = panic "PPC.Regs.narrow: no match"
        
        narrowed = narrow rep signed
        
        toI16 W32 True
            | narrowed >= -32768 && narrowed < 32768 = Just narrowed
            | otherwise = Nothing
        toI16 W32 False
            | narrowed >= 0 && narrowed < 65536 = Just narrowed
            | otherwise = Nothing
        toI16 _ _  = Just narrowed


{-
The PowerPC has 64 registers of interest; 32 integer registers and 32 floating
point registers.
-}

fReg :: Int -> RegNo
fReg x = (32 + x)

sp, r3, r4, r27, r28, f1, f20, f21 :: Reg
317 318 319 320 321 322 323 324
sp 	= regSingle 1
r3 	= regSingle 3
r4 	= regSingle 4
r27 	= regSingle 27
r28 	= regSingle 28
f1 	= regSingle $ fReg 1
f20 	= regSingle $ fReg 20
f21 	= regSingle $ fReg 21
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503



-- horror show -----------------------------------------------------------------
freeReg :: RegNo -> FastBool
globalRegMaybe :: GlobalReg -> Maybe Reg


#if powerpc_TARGET_ARCH
#define r0 0
#define r1 1
#define r2 2
#define r3 3
#define r4 4
#define r5 5
#define r6 6
#define r7 7
#define r8 8
#define r9 9
#define r10 10
#define r11 11
#define r12 12
#define r13 13
#define r14 14
#define r15 15
#define r16 16
#define r17 17
#define r18 18
#define r19 19
#define r20 20
#define r21 21
#define r22 22
#define r23 23
#define r24 24
#define r25 25
#define r26 26
#define r27 27
#define r28 28
#define r29 29
#define r30 30
#define r31 31

#ifdef darwin_TARGET_OS
#define f0  32
#define f1  33
#define f2  34
#define f3  35
#define f4  36
#define f5  37
#define f6  38
#define f7  39
#define f8  40
#define f9  41
#define f10 42
#define f11 43
#define f12 44
#define f13 45
#define f14 46
#define f15 47
#define f16 48
#define f17 49
#define f18 50
#define f19 51
#define f20 52
#define f21 53
#define f22 54
#define f23 55
#define f24 56
#define f25 57
#define f26 58
#define f27 59
#define f28 60
#define f29 61
#define f30 62
#define f31 63
#else
#define fr0  32
#define fr1  33
#define fr2  34
#define fr3  35
#define fr4  36
#define fr5  37
#define fr6  38
#define fr7  39
#define fr8  40
#define fr9  41
#define fr10 42
#define fr11 43
#define fr12 44
#define fr13 45
#define fr14 46
#define fr15 47
#define fr16 48
#define fr17 49
#define fr18 50
#define fr19 51
#define fr20 52
#define fr21 53
#define fr22 54
#define fr23 55
#define fr24 56
#define fr25 57
#define fr26 58
#define fr27 59
#define fr28 60
#define fr29 61
#define fr30 62
#define fr31 63
#endif



freeReg 0 = fastBool False -- Hack: r0 can't be used in all insns, but it's actually free
freeReg 1 = fastBool False -- The Stack Pointer
#if !darwin_TARGET_OS
 -- most non-darwin powerpc OSes use r2 as a TOC pointer or something like that
freeReg 2 = fastBool False
#endif

#ifdef REG_Base
freeReg REG_Base = fastBool False
#endif
#ifdef REG_R1
freeReg REG_R1   = fastBool False
#endif	
#ifdef REG_R2  
freeReg REG_R2   = fastBool False
#endif	
#ifdef REG_R3  
freeReg REG_R3   = fastBool False
#endif	
#ifdef REG_R4  
freeReg REG_R4   = fastBool False
#endif	
#ifdef REG_R5  
freeReg REG_R5   = fastBool False
#endif	
#ifdef REG_R6  
freeReg REG_R6   = fastBool False
#endif	
#ifdef REG_R7  
freeReg REG_R7   = fastBool False
#endif	
#ifdef REG_R8  
freeReg REG_R8   = fastBool False
#endif
#ifdef REG_F1
freeReg REG_F1 = fastBool False
#endif
#ifdef REG_F2
freeReg REG_F2 = fastBool False
#endif
#ifdef REG_F3
freeReg REG_F3 = fastBool False
#endif
#ifdef REG_F4
freeReg REG_F4 = fastBool False
#endif
#ifdef REG_D1
freeReg REG_D1 = fastBool False
#endif
#ifdef REG_D2
freeReg REG_D2 = fastBool False
#endif
#ifdef REG_Sp 
freeReg REG_Sp   = fastBool False
#endif 
#ifdef REG_Su
freeReg REG_Su   = fastBool False
#endif 
#ifdef REG_SpLim 
freeReg REG_SpLim = fastBool False
#endif 
#ifdef REG_Hp 
freeReg REG_Hp   = fastBool False
#endif
#ifdef REG_HpLim
freeReg REG_HpLim = fastBool False
#endif
504
freeReg _               = fastBool True
505 506 507 508 509 510 511 512


--  | Returns 'Nothing' if this global register is not stored
-- in a real machine register, otherwise returns @'Just' reg@, where
-- reg is the machine register it is stored in.


#ifdef REG_Base
513
globalRegMaybe BaseReg			= Just (regSingle REG_Base)
514 515
#endif
#ifdef REG_R1
516
globalRegMaybe (VanillaReg 1 _)		= Just (regSingle REG_R1)
517 518
#endif 
#ifdef REG_R2 
519
globalRegMaybe (VanillaReg 2 _)		= Just (regSingle REG_R2)
520 521
#endif 
#ifdef REG_R3 
522
globalRegMaybe (VanillaReg 3 _) 	= Just (regSingle REG_R3)
523 524
#endif 
#ifdef REG_R4 
525
globalRegMaybe (VanillaReg 4 _)		= Just (regSingle REG_R4)
526 527
#endif 
#ifdef REG_R5 
528
globalRegMaybe (VanillaReg 5 _)		= Just (regSingle REG_R5)
529 530
#endif 
#ifdef REG_R6 
531
globalRegMaybe (VanillaReg 6 _)		= Just (regSingle REG_R6)
532 533
#endif 
#ifdef REG_R7 
534
globalRegMaybe (VanillaReg 7 _)		= Just (regSingle REG_R7)
535 536
#endif 
#ifdef REG_R8 
537
globalRegMaybe (VanillaReg 8 _)		= Just (regSingle REG_R8)
538 539
#endif
#ifdef REG_R9 
540
globalRegMaybe (VanillaReg 9 _)		= Just (regSingle REG_R9)
541 542
#endif
#ifdef REG_R10 
543
globalRegMaybe (VanillaReg 10 _)	= Just (regSingle REG_R10)
544 545
#endif
#ifdef REG_F1
546
globalRegMaybe (FloatReg 1)		= Just (regSingle REG_F1)
547 548
#endif				 	
#ifdef REG_F2			 	
549
globalRegMaybe (FloatReg 2)		= Just (regSingle REG_F2)
550 551
#endif				 	
#ifdef REG_F3			 	
552
globalRegMaybe (FloatReg 3)		= Just (regSingle REG_F3)
553 554
#endif				 	
#ifdef REG_F4			 	
555
globalRegMaybe (FloatReg 4)		= Just (regSingle REG_F4)
556 557
#endif				 	
#ifdef REG_D1			 	
558
globalRegMaybe (DoubleReg 1)		= Just (regSingle REG_D1)
559 560
#endif				 	
#ifdef REG_D2			 	
561
globalRegMaybe (DoubleReg 2)		= Just (regSingle REG_D2)
562 563
#endif
#ifdef REG_Sp	    
564
globalRegMaybe Sp		   	= Just (regSingle REG_Sp)
565 566
#endif
#ifdef REG_Lng1			 	
567
globalRegMaybe (LongReg 1)		= Just (regSingle REG_Lng1)
568 569
#endif				 	
#ifdef REG_Lng2			 	
570
globalRegMaybe (LongReg 2)		= Just (regSingle REG_Lng2)
571 572
#endif
#ifdef REG_SpLim	    			
573
globalRegMaybe SpLim		   	= Just (regSingle REG_SpLim)
574 575
#endif	    				
#ifdef REG_Hp	   			
576
globalRegMaybe Hp		   	= Just (regSingle REG_Hp)
577 578
#endif	    				
#ifdef REG_HpLim      			
579
globalRegMaybe HpLim		   	= Just (regSingle REG_HpLim)
580 581
#endif	    				
#ifdef REG_CurrentTSO      			
582
globalRegMaybe CurrentTSO	   	= Just (regSingle REG_CurrentTSO)
583 584
#endif	    				
#ifdef REG_CurrentNursery      			
585
globalRegMaybe CurrentNursery	   	= Just (regSingle REG_CurrentNursery)
586 587 588 589 590 591 592 593 594 595
#endif	    				
globalRegMaybe _		   	= Nothing


#else  /* powerpc_TARGET_ARCH */

freeReg _		= 0#
globalRegMaybe _	= panic "PPC.Regs.globalRegMaybe: not defined"

#endif /* powerpc_TARGET_ARCH */
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614


-- We map STG registers onto appropriate CmmExprs.  Either they map
-- to real machine registers or stored as offsets from BaseReg.  Given
-- a GlobalReg, get_GlobalReg_reg_or_addr produces either the real
-- register it is in, on this platform, or a CmmExpr denoting the
-- address in the register table holding it.
-- (See also get_GlobalReg_addr in CgUtils.)

get_GlobalReg_reg_or_addr :: GlobalReg -> Either Reg CmmExpr
get_GlobalReg_reg_or_addr mid
   = case globalRegMaybe mid of
        Just rr -> Left rr
        Nothing -> Right (get_GlobalReg_addr mid)


-- allocatableRegs is allMachRegNos with the fixed-use regs removed.
-- i.e., these are the regs for which we are prepared to allow the
-- register allocator to attempt to map VRegs to.
615
allocatableRegs :: [RealReg]
616 617
allocatableRegs
   = let isFree i = isFastTrue (freeReg i)
618
     in  map RealRegSingle $ filter isFree allMachRegNos