{-# LANGUAGE CPP #-}

module RegAlloc.Linear.FreeRegs (
    FR(..),
    maxSpillSlots
)

#include "HsVersions.h"

where

import GhcPrelude

import Reg
import RegClass

import DynFlags
import Panic
import GHC.Platform

-- -----------------------------------------------------------------------------
-- The free register set
-- This needs to be *efficient*
-- Here's an inefficient 'executable specification' of the FreeRegs data type:
--
--      type FreeRegs = [RegNo]
--      noFreeRegs = 0
--      releaseReg n f = if n `elem` f then f else (n : f)
--      initFreeRegs = allocatableRegs
--      getFreeRegs cls f = filter ( (==cls) . regClass . RealReg ) f
--      allocateReg f r = filter (/= r) f

import qualified RegAlloc.Linear.PPC.FreeRegs    as PPC
import qualified RegAlloc.Linear.SPARC.FreeRegs  as SPARC
import qualified RegAlloc.Linear.X86.FreeRegs    as X86
import qualified RegAlloc.Linear.X86_64.FreeRegs as X86_64

import qualified PPC.Instr
import qualified SPARC.Instr
import qualified X86.Instr

class Show freeRegs => FR freeRegs where
    frAllocateReg :: Platform -> RealReg -> freeRegs -> freeRegs
    frGetFreeRegs :: Platform -> RegClass -> freeRegs -> [RealReg]
    frInitFreeRegs :: Platform -> freeRegs
    frReleaseReg :: Platform -> RealReg -> freeRegs -> freeRegs

instance FR X86.FreeRegs where
    frAllocateReg :: Platform -> RealReg -> FreeRegs -> FreeRegs
frAllocateReg  = \Platform
_ -> RealReg -> FreeRegs -> FreeRegs
X86.allocateReg
    frGetFreeRegs :: Platform -> RegClass -> FreeRegs -> [RealReg]
frGetFreeRegs  = Platform -> RegClass -> FreeRegs -> [RealReg]
X86.getFreeRegs
    frInitFreeRegs :: Platform -> FreeRegs
frInitFreeRegs = Platform -> FreeRegs
X86.initFreeRegs
    frReleaseReg :: Platform -> RealReg -> FreeRegs -> FreeRegs
frReleaseReg   = \Platform
_ -> RealReg -> FreeRegs -> FreeRegs
X86.releaseReg

instance FR X86_64.FreeRegs where
    frAllocateReg :: Platform -> RealReg -> FreeRegs -> FreeRegs
frAllocateReg  = \Platform
_ -> RealReg -> FreeRegs -> FreeRegs
X86_64.allocateReg
    frGetFreeRegs :: Platform -> RegClass -> FreeRegs -> [RealReg]
frGetFreeRegs  = Platform -> RegClass -> FreeRegs -> [RealReg]
X86_64.getFreeRegs
    frInitFreeRegs :: Platform -> FreeRegs
frInitFreeRegs = Platform -> FreeRegs
X86_64.initFreeRegs
    frReleaseReg :: Platform -> RealReg -> FreeRegs -> FreeRegs
frReleaseReg   = \Platform
_ -> RealReg -> FreeRegs -> FreeRegs
X86_64.releaseReg

instance FR PPC.FreeRegs where
    frAllocateReg :: Platform -> RealReg -> FreeRegs -> FreeRegs
frAllocateReg  = \Platform
_ -> RealReg -> FreeRegs -> FreeRegs
PPC.allocateReg
    frGetFreeRegs :: Platform -> RegClass -> FreeRegs -> [RealReg]
frGetFreeRegs  = \Platform
_ -> RegClass -> FreeRegs -> [RealReg]
PPC.getFreeRegs
    frInitFreeRegs :: Platform -> FreeRegs
frInitFreeRegs = Platform -> FreeRegs
PPC.initFreeRegs
    frReleaseReg :: Platform -> RealReg -> FreeRegs -> FreeRegs
frReleaseReg   = \Platform
_ -> RealReg -> FreeRegs -> FreeRegs
PPC.releaseReg

instance FR SPARC.FreeRegs where
    frAllocateReg :: Platform -> RealReg -> FreeRegs -> FreeRegs
frAllocateReg  = Platform -> RealReg -> FreeRegs -> FreeRegs
SPARC.allocateReg
    frGetFreeRegs :: Platform -> RegClass -> FreeRegs -> [RealReg]
frGetFreeRegs  = \Platform
_ -> RegClass -> FreeRegs -> [RealReg]
SPARC.getFreeRegs
    frInitFreeRegs :: Platform -> FreeRegs
frInitFreeRegs = Platform -> FreeRegs
SPARC.initFreeRegs
    frReleaseReg :: Platform -> RealReg -> FreeRegs -> FreeRegs
frReleaseReg   = Platform -> RealReg -> FreeRegs -> FreeRegs
SPARC.releaseReg

maxSpillSlots :: DynFlags -> Int
maxSpillSlots :: DynFlags -> Int
maxSpillSlots DynFlags
dflags
              = case Platform -> Arch
platformArch (DynFlags -> Platform
targetPlatform DynFlags
dflags) of
                Arch
ArchX86       -> DynFlags -> Int
X86.Instr.maxSpillSlots DynFlags
dflags
                Arch
ArchX86_64    -> DynFlags -> Int
X86.Instr.maxSpillSlots DynFlags
dflags
                Arch
ArchPPC       -> DynFlags -> Int
PPC.Instr.maxSpillSlots DynFlags
dflags
                Arch
ArchS390X     -> String -> Int
forall a. String -> a
panic String
"maxSpillSlots ArchS390X"
                Arch
ArchSPARC     -> DynFlags -> Int
SPARC.Instr.maxSpillSlots DynFlags
dflags
                Arch
ArchSPARC64   -> String -> Int
forall a. String -> a
panic String
"maxSpillSlots ArchSPARC64"
                ArchARM ArmISA
_ [ArmISAExt]
_ ArmABI
_ -> String -> Int
forall a. String -> a
panic String
"maxSpillSlots ArchARM"
                Arch
ArchARM64     -> String -> Int
forall a. String -> a
panic String
"maxSpillSlots ArchARM64"
                ArchPPC_64 PPC_64ABI
_  -> DynFlags -> Int
PPC.Instr.maxSpillSlots DynFlags
dflags
                Arch
ArchAlpha     -> String -> Int
forall a. String -> a
panic String
"maxSpillSlots ArchAlpha"
                Arch
ArchMipseb    -> String -> Int
forall a. String -> a
panic String
"maxSpillSlots ArchMipseb"
                Arch
ArchMipsel    -> String -> Int
forall a. String -> a
panic String
"maxSpillSlots ArchMipsel"
                Arch
ArchJavaScript-> String -> Int
forall a. String -> a
panic String
"maxSpillSlots ArchJavaScript"
                Arch
ArchUnknown   -> String -> Int
forall a. String -> a
panic String
"maxSpillSlots ArchUnknown"