{-# LANGUAGE GeneralizedNewtypeDeriving #-}

-- | Free regs map for PowerPC
module GHC.CmmToAsm.Reg.Linear.PPC where

import GHC.Prelude

import GHC.CmmToAsm.PPC.Regs
import GHC.Platform.Reg.Class
import GHC.Platform.Reg

import GHC.Utils.Outputable
import GHC.Platform

import Data.Word
import Data.Bits

-- The PowerPC has 32 integer and 32 floating point registers.
-- This is 32bit PowerPC, so Word64 is inefficient - two Word32s are much
-- better.
-- Note that when getFreeRegs scans for free registers, it starts at register
-- 31 and counts down. This is a hack for the PowerPC - the higher-numbered
-- registers are callee-saves, while the lower regs are caller-saves, so it
-- makes sense to start at the high end.
-- Apart from that, the code does nothing PowerPC-specific, so feel free to
-- add your favourite platform to the #if (if you have 64 registers but only
-- 32-bit words).

data FreeRegs = FreeRegs !Word32 !Word32
              deriving( RegNo -> FreeRegs -> ShowS
[FreeRegs] -> ShowS
FreeRegs -> String
(RegNo -> FreeRegs -> ShowS)
-> (FreeRegs -> String) -> ([FreeRegs] -> ShowS) -> Show FreeRegs
forall a.
(RegNo -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FreeRegs] -> ShowS
$cshowList :: [FreeRegs] -> ShowS
show :: FreeRegs -> String
$cshow :: FreeRegs -> String
showsPrec :: RegNo -> FreeRegs -> ShowS
$cshowsPrec :: RegNo -> FreeRegs -> ShowS
Show )  -- The Show is used in an ASSERT

instance Outputable FreeRegs where
    ppr :: FreeRegs -> SDoc
ppr = String -> SDoc
text (String -> SDoc) -> (FreeRegs -> String) -> FreeRegs -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FreeRegs -> String
forall a. Show a => a -> String
show

noFreeRegs :: FreeRegs
noFreeRegs :: FreeRegs
noFreeRegs = Word32 -> Word32 -> FreeRegs
FreeRegs Word32
0 Word32
0

releaseReg :: RealReg -> FreeRegs -> FreeRegs
releaseReg :: RealReg -> FreeRegs -> FreeRegs
releaseReg (RealRegSingle RegNo
r) (FreeRegs Word32
g Word32
f)
    | RegNo
r RegNo -> RegNo -> Bool
forall a. Ord a => a -> a -> Bool
> RegNo
31    = Word32 -> Word32 -> FreeRegs
FreeRegs Word32
g (Word32
f Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. (Word32
1 Word32 -> RegNo -> Word32
forall a. Bits a => a -> RegNo -> a
`shiftL` (RegNo
r RegNo -> RegNo -> RegNo
forall a. Num a => a -> a -> a
- RegNo
32)))
    | Bool
otherwise = Word32 -> Word32 -> FreeRegs
FreeRegs (Word32
g Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. (Word32
1 Word32 -> RegNo -> Word32
forall a. Bits a => a -> RegNo -> a
`shiftL` RegNo
r)) Word32
f

releaseReg RealReg
_ FreeRegs
_
        = String -> FreeRegs
forall a. String -> a
panic String
"RegAlloc.Linear.PPC.releaseReg: bad reg"

initFreeRegs :: Platform -> FreeRegs
initFreeRegs :: Platform -> FreeRegs
initFreeRegs Platform
platform = (FreeRegs -> RealReg -> FreeRegs)
-> FreeRegs -> [RealReg] -> FreeRegs
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' ((RealReg -> FreeRegs -> FreeRegs)
-> FreeRegs -> RealReg -> FreeRegs
forall a b c. (a -> b -> c) -> b -> a -> c
flip RealReg -> FreeRegs -> FreeRegs
releaseReg) FreeRegs
noFreeRegs (Platform -> [RealReg]
allocatableRegs Platform
platform)

getFreeRegs :: RegClass -> FreeRegs -> [RealReg]        -- lazily
getFreeRegs :: RegClass -> FreeRegs -> [RealReg]
getFreeRegs RegClass
cls (FreeRegs Word32
g Word32
f)
    | RegClass
RcDouble <- RegClass
cls = Word32 -> Word32 -> RegNo -> [RealReg]
forall {a}. (Num a, Bits a) => a -> a -> RegNo -> [RealReg]
go Word32
f (Word32
0x80000000) RegNo
63
    | RegClass
RcInteger <- RegClass
cls = Word32 -> Word32 -> RegNo -> [RealReg]
forall {a}. (Num a, Bits a) => a -> a -> RegNo -> [RealReg]
go Word32
g (Word32
0x80000000) RegNo
31
    | Bool
otherwise = String -> SDoc -> [RealReg]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"RegAllocLinear.getFreeRegs: Bad register class" (RegClass -> SDoc
forall a. Outputable a => a -> SDoc
ppr RegClass
cls)
    where
        go :: a -> a -> RegNo -> [RealReg]
go a
_ a
0 RegNo
_ = []
        go a
x a
m RegNo
i | a
x a -> a -> a
forall a. Bits a => a -> a -> a
.&. a
m a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
0 = RegNo -> RealReg
RealRegSingle RegNo
i RealReg -> [RealReg] -> [RealReg]
forall a. a -> [a] -> [a]
: (a -> a -> RegNo -> [RealReg]
go a
x (a
m a -> RegNo -> a
forall a. Bits a => a -> RegNo -> a
`shiftR` RegNo
1) (RegNo -> [RealReg]) -> RegNo -> [RealReg]
forall a b. (a -> b) -> a -> b
$! RegNo
iRegNo -> RegNo -> RegNo
forall a. Num a => a -> a -> a
-RegNo
1)
                 | Bool
otherwise    = a -> a -> RegNo -> [RealReg]
go a
x (a
m a -> RegNo -> a
forall a. Bits a => a -> RegNo -> a
`shiftR` RegNo
1) (RegNo -> [RealReg]) -> RegNo -> [RealReg]
forall a b. (a -> b) -> a -> b
$! RegNo
iRegNo -> RegNo -> RegNo
forall a. Num a => a -> a -> a
-RegNo
1

allocateReg :: RealReg -> FreeRegs -> FreeRegs
allocateReg :: RealReg -> FreeRegs -> FreeRegs
allocateReg (RealRegSingle RegNo
r) (FreeRegs Word32
g Word32
f)
    | RegNo
r RegNo -> RegNo -> Bool
forall a. Ord a => a -> a -> Bool
> RegNo
31    = Word32 -> Word32 -> FreeRegs
FreeRegs Word32
g (Word32
f Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. Word32 -> Word32
forall a. Bits a => a -> a
complement (Word32
1 Word32 -> RegNo -> Word32
forall a. Bits a => a -> RegNo -> a
`shiftL` (RegNo
r RegNo -> RegNo -> RegNo
forall a. Num a => a -> a -> a
- RegNo
32)))
    | Bool
otherwise = Word32 -> Word32 -> FreeRegs
FreeRegs (Word32
g Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. Word32 -> Word32
forall a. Bits a => a -> a
complement (Word32
1 Word32 -> RegNo -> Word32
forall a. Bits a => a -> RegNo -> a
`shiftL` RegNo
r)) Word32
f

allocateReg RealReg
_ FreeRegs
_
        = String -> FreeRegs
forall a. String -> a
panic String
"RegAlloc.Linear.PPC.allocateReg: bad reg"