-- | Hard wired things related to registers.
--      This is module is preventing the native code generator being able to
--      emit code for non-host architectures.
--
--      TODO: Do a better job of the overloading, and eliminate this module.
--      We'd probably do better with a Register type class, and hook this to
--      Instruction somehow.
--
--      TODO: We should also make arch specific versions of RegAlloc.Graph.TrivColorable
module GHC.CmmToAsm.Reg.Target (
        targetVirtualRegSqueeze,
        targetRealRegSqueeze,
        targetClassOfRealReg,
        targetMkVirtualReg,
        targetRegDotColor,
        targetClassOfReg
)

where

import GHC.Prelude

import GHC.Platform.Reg
import GHC.Platform.Reg.Class
import GHC.CmmToAsm.Format

import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Types.Unique
import GHC.Platform

import qualified GHC.CmmToAsm.X86.Regs       as X86
import qualified GHC.CmmToAsm.X86.RegInfo    as X86
import qualified GHC.CmmToAsm.PPC.Regs       as PPC
import qualified GHC.CmmToAsm.AArch64.Regs   as AArch64


targetVirtualRegSqueeze :: Platform -> RegClass -> VirtualReg -> Int
targetVirtualRegSqueeze :: Platform -> RegClass -> VirtualReg -> Int
targetVirtualRegSqueeze Platform
platform
    = case Platform -> Arch
platformArch Platform
platform of
      Arch
ArchX86       -> RegClass -> VirtualReg -> Int
X86.virtualRegSqueeze
      Arch
ArchX86_64    -> RegClass -> VirtualReg -> Int
X86.virtualRegSqueeze
      Arch
ArchPPC       -> RegClass -> VirtualReg -> Int
PPC.virtualRegSqueeze
      Arch
ArchS390X     -> String -> RegClass -> VirtualReg -> Int
forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchS390X"
      ArchPPC_64 PPC_64ABI
_  -> RegClass -> VirtualReg -> Int
PPC.virtualRegSqueeze
      ArchARM ArmISA
_ [ArmISAExt]
_ ArmABI
_ -> String -> RegClass -> VirtualReg -> Int
forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchARM"
      Arch
ArchAArch64   -> RegClass -> VirtualReg -> Int
AArch64.virtualRegSqueeze
      Arch
ArchAlpha     -> String -> RegClass -> VirtualReg -> Int
forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchAlpha"
      Arch
ArchMipseb    -> String -> RegClass -> VirtualReg -> Int
forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchMipseb"
      Arch
ArchMipsel    -> String -> RegClass -> VirtualReg -> Int
forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchMipsel"
      Arch
ArchRISCV64   -> String -> RegClass -> VirtualReg -> Int
forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchRISCV64"
      Arch
ArchJavaScript-> String -> RegClass -> VirtualReg -> Int
forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchJavaScript"
      Arch
ArchUnknown   -> String -> RegClass -> VirtualReg -> Int
forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchUnknown"


targetRealRegSqueeze :: Platform -> RegClass -> RealReg -> Int
targetRealRegSqueeze :: Platform -> RegClass -> RealReg -> Int
targetRealRegSqueeze Platform
platform
    = case Platform -> Arch
platformArch Platform
platform of
      Arch
ArchX86       -> RegClass -> RealReg -> Int
X86.realRegSqueeze
      Arch
ArchX86_64    -> RegClass -> RealReg -> Int
X86.realRegSqueeze
      Arch
ArchPPC       -> RegClass -> RealReg -> Int
PPC.realRegSqueeze
      Arch
ArchS390X     -> String -> RegClass -> RealReg -> Int
forall a. String -> a
panic String
"targetRealRegSqueeze ArchS390X"
      ArchPPC_64 PPC_64ABI
_  -> RegClass -> RealReg -> Int
PPC.realRegSqueeze
      ArchARM ArmISA
_ [ArmISAExt]
_ ArmABI
_ -> String -> RegClass -> RealReg -> Int
forall a. String -> a
panic String
"targetRealRegSqueeze ArchARM"
      Arch
ArchAArch64   -> RegClass -> RealReg -> Int
AArch64.realRegSqueeze
      Arch
ArchAlpha     -> String -> RegClass -> RealReg -> Int
forall a. String -> a
panic String
"targetRealRegSqueeze ArchAlpha"
      Arch
ArchMipseb    -> String -> RegClass -> RealReg -> Int
forall a. String -> a
panic String
"targetRealRegSqueeze ArchMipseb"
      Arch
ArchMipsel    -> String -> RegClass -> RealReg -> Int
forall a. String -> a
panic String
"targetRealRegSqueeze ArchMipsel"
      Arch
ArchRISCV64   -> String -> RegClass -> RealReg -> Int
forall a. String -> a
panic String
"targetRealRegSqueeze ArchRISCV64"
      Arch
ArchJavaScript-> String -> RegClass -> RealReg -> Int
forall a. String -> a
panic String
"targetRealRegSqueeze ArchJavaScript"
      Arch
ArchUnknown   -> String -> RegClass -> RealReg -> Int
forall a. String -> a
panic String
"targetRealRegSqueeze ArchUnknown"

targetClassOfRealReg :: Platform -> RealReg -> RegClass
targetClassOfRealReg :: Platform -> RealReg -> RegClass
targetClassOfRealReg Platform
platform
    = case Platform -> Arch
platformArch Platform
platform of
      Arch
ArchX86       -> Platform -> RealReg -> RegClass
X86.classOfRealReg Platform
platform
      Arch
ArchX86_64    -> Platform -> RealReg -> RegClass
X86.classOfRealReg Platform
platform
      Arch
ArchPPC       -> RealReg -> RegClass
PPC.classOfRealReg
      Arch
ArchS390X     -> String -> RealReg -> RegClass
forall a. String -> a
panic String
"targetClassOfRealReg ArchS390X"
      ArchPPC_64 PPC_64ABI
_  -> RealReg -> RegClass
PPC.classOfRealReg
      ArchARM ArmISA
_ [ArmISAExt]
_ ArmABI
_ -> String -> RealReg -> RegClass
forall a. String -> a
panic String
"targetClassOfRealReg ArchARM"
      Arch
ArchAArch64   -> RealReg -> RegClass
AArch64.classOfRealReg
      Arch
ArchAlpha     -> String -> RealReg -> RegClass
forall a. String -> a
panic String
"targetClassOfRealReg ArchAlpha"
      Arch
ArchMipseb    -> String -> RealReg -> RegClass
forall a. String -> a
panic String
"targetClassOfRealReg ArchMipseb"
      Arch
ArchMipsel    -> String -> RealReg -> RegClass
forall a. String -> a
panic String
"targetClassOfRealReg ArchMipsel"
      Arch
ArchRISCV64   -> String -> RealReg -> RegClass
forall a. String -> a
panic String
"targetClassOfRealReg ArchRISCV64"
      Arch
ArchJavaScript-> String -> RealReg -> RegClass
forall a. String -> a
panic String
"targetClassOfRealReg ArchJavaScript"
      Arch
ArchUnknown   -> String -> RealReg -> RegClass
forall a. String -> a
panic String
"targetClassOfRealReg ArchUnknown"

targetMkVirtualReg :: Platform -> Unique -> Format -> VirtualReg
targetMkVirtualReg :: Platform -> Unique -> Format -> VirtualReg
targetMkVirtualReg Platform
platform
    = case Platform -> Arch
platformArch Platform
platform of
      Arch
ArchX86       -> Unique -> Format -> VirtualReg
X86.mkVirtualReg
      Arch
ArchX86_64    -> Unique -> Format -> VirtualReg
X86.mkVirtualReg
      Arch
ArchPPC       -> Unique -> Format -> VirtualReg
PPC.mkVirtualReg
      Arch
ArchS390X     -> String -> Unique -> Format -> VirtualReg
forall a. String -> a
panic String
"targetMkVirtualReg ArchS390X"
      ArchPPC_64 PPC_64ABI
_  -> Unique -> Format -> VirtualReg
PPC.mkVirtualReg
      ArchARM ArmISA
_ [ArmISAExt]
_ ArmABI
_ -> String -> Unique -> Format -> VirtualReg
forall a. String -> a
panic String
"targetMkVirtualReg ArchARM"
      Arch
ArchAArch64   -> Unique -> Format -> VirtualReg
AArch64.mkVirtualReg
      Arch
ArchAlpha     -> String -> Unique -> Format -> VirtualReg
forall a. String -> a
panic String
"targetMkVirtualReg ArchAlpha"
      Arch
ArchMipseb    -> String -> Unique -> Format -> VirtualReg
forall a. String -> a
panic String
"targetMkVirtualReg ArchMipseb"
      Arch
ArchMipsel    -> String -> Unique -> Format -> VirtualReg
forall a. String -> a
panic String
"targetMkVirtualReg ArchMipsel"
      Arch
ArchRISCV64   -> String -> Unique -> Format -> VirtualReg
forall a. String -> a
panic String
"targetMkVirtualReg ArchRISCV64"
      Arch
ArchJavaScript-> String -> Unique -> Format -> VirtualReg
forall a. String -> a
panic String
"targetMkVirtualReg ArchJavaScript"
      Arch
ArchUnknown   -> String -> Unique -> Format -> VirtualReg
forall a. String -> a
panic String
"targetMkVirtualReg ArchUnknown"

targetRegDotColor :: Platform -> RealReg -> SDoc
targetRegDotColor :: Platform -> RealReg -> SDoc
targetRegDotColor Platform
platform
    = case Platform -> Arch
platformArch Platform
platform of
      Arch
ArchX86       -> Platform -> RealReg -> SDoc
X86.regDotColor Platform
platform
      Arch
ArchX86_64    -> Platform -> RealReg -> SDoc
X86.regDotColor Platform
platform
      Arch
ArchPPC       -> RealReg -> SDoc
PPC.regDotColor
      Arch
ArchS390X     -> String -> RealReg -> SDoc
forall a. String -> a
panic String
"targetRegDotColor ArchS390X"
      ArchPPC_64 PPC_64ABI
_  -> RealReg -> SDoc
PPC.regDotColor
      ArchARM ArmISA
_ [ArmISAExt]
_ ArmABI
_ -> String -> RealReg -> SDoc
forall a. String -> a
panic String
"targetRegDotColor ArchARM"
      Arch
ArchAArch64   -> RealReg -> SDoc
AArch64.regDotColor
      Arch
ArchAlpha     -> String -> RealReg -> SDoc
forall a. String -> a
panic String
"targetRegDotColor ArchAlpha"
      Arch
ArchMipseb    -> String -> RealReg -> SDoc
forall a. String -> a
panic String
"targetRegDotColor ArchMipseb"
      Arch
ArchMipsel    -> String -> RealReg -> SDoc
forall a. String -> a
panic String
"targetRegDotColor ArchMipsel"
      Arch
ArchRISCV64   -> String -> RealReg -> SDoc
forall a. String -> a
panic String
"targetRegDotColor ArchRISCV64"
      Arch
ArchJavaScript-> String -> RealReg -> SDoc
forall a. String -> a
panic String
"targetRegDotColor ArchJavaScript"
      Arch
ArchUnknown   -> String -> RealReg -> SDoc
forall a. String -> a
panic String
"targetRegDotColor ArchUnknown"


targetClassOfReg :: Platform -> Reg -> RegClass
targetClassOfReg :: Platform -> Reg -> RegClass
targetClassOfReg Platform
platform Reg
reg
 = case Reg
reg of
   RegVirtual VirtualReg
vr -> VirtualReg -> RegClass
classOfVirtualReg VirtualReg
vr
   RegReal RealReg
rr -> Platform -> RealReg -> RegClass
targetClassOfRealReg Platform
platform RealReg
rr