{-# LANGUAGE CPP #-}
-- | 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

#include "HsVersions.h"

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.SPARC.Regs     as SPARC
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     -> forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchS390X"
      Arch
ArchSPARC     -> RegClass -> VirtualReg -> Int
SPARC.virtualRegSqueeze
      Arch
ArchSPARC64   -> forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchSPARC64"
      ArchPPC_64 PPC_64ABI
_  -> RegClass -> VirtualReg -> Int
PPC.virtualRegSqueeze
      ArchARM ArmISA
_ [ArmISAExt]
_ ArmABI
_ -> forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchARM"
      Arch
ArchAArch64   -> RegClass -> VirtualReg -> Int
AArch64.virtualRegSqueeze
      Arch
ArchAlpha     -> forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchAlpha"
      Arch
ArchMipseb    -> forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchMipseb"
      Arch
ArchMipsel    -> forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchMipsel"
      Arch
ArchRISCV64   -> forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchRISCV64"
      Arch
ArchJavaScript-> forall a. String -> a
panic String
"targetVirtualRegSqueeze ArchJavaScript"
      Arch
ArchUnknown   -> 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     -> forall a. String -> a
panic String
"targetRealRegSqueeze ArchS390X"
      Arch
ArchSPARC     -> RegClass -> RealReg -> Int
SPARC.realRegSqueeze
      Arch
ArchSPARC64   -> forall a. String -> a
panic String
"targetRealRegSqueeze ArchSPARC64"
      ArchPPC_64 PPC_64ABI
_  -> RegClass -> RealReg -> Int
PPC.realRegSqueeze
      ArchARM ArmISA
_ [ArmISAExt]
_ ArmABI
_ -> forall a. String -> a
panic String
"targetRealRegSqueeze ArchARM"
      Arch
ArchAArch64   -> RegClass -> RealReg -> Int
AArch64.realRegSqueeze
      Arch
ArchAlpha     -> forall a. String -> a
panic String
"targetRealRegSqueeze ArchAlpha"
      Arch
ArchMipseb    -> forall a. String -> a
panic String
"targetRealRegSqueeze ArchMipseb"
      Arch
ArchMipsel    -> forall a. String -> a
panic String
"targetRealRegSqueeze ArchMipsel"
      Arch
ArchRISCV64   -> forall a. String -> a
panic String
"targetRealRegSqueeze ArchRISCV64"
      Arch
ArchJavaScript-> forall a. String -> a
panic String
"targetRealRegSqueeze ArchJavaScript"
      Arch
ArchUnknown   -> 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     -> forall a. String -> a
panic String
"targetClassOfRealReg ArchS390X"
      Arch
ArchSPARC     -> RealReg -> RegClass
SPARC.classOfRealReg
      Arch
ArchSPARC64   -> forall a. String -> a
panic String
"targetClassOfRealReg ArchSPARC64"
      ArchPPC_64 PPC_64ABI
_  -> RealReg -> RegClass
PPC.classOfRealReg
      ArchARM ArmISA
_ [ArmISAExt]
_ ArmABI
_ -> forall a. String -> a
panic String
"targetClassOfRealReg ArchARM"
      Arch
ArchAArch64   -> RealReg -> RegClass
AArch64.classOfRealReg
      Arch
ArchAlpha     -> forall a. String -> a
panic String
"targetClassOfRealReg ArchAlpha"
      Arch
ArchMipseb    -> forall a. String -> a
panic String
"targetClassOfRealReg ArchMipseb"
      Arch
ArchMipsel    -> forall a. String -> a
panic String
"targetClassOfRealReg ArchMipsel"
      Arch
ArchRISCV64   -> forall a. String -> a
panic String
"targetClassOfRealReg ArchRISCV64"
      Arch
ArchJavaScript-> forall a. String -> a
panic String
"targetClassOfRealReg ArchJavaScript"
      Arch
ArchUnknown   -> 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     -> forall a. String -> a
panic String
"targetMkVirtualReg ArchS390X"
      Arch
ArchSPARC     -> Unique -> Format -> VirtualReg
SPARC.mkVirtualReg
      Arch
ArchSPARC64   -> forall a. String -> a
panic String
"targetMkVirtualReg ArchSPARC64"
      ArchPPC_64 PPC_64ABI
_  -> Unique -> Format -> VirtualReg
PPC.mkVirtualReg
      ArchARM ArmISA
_ [ArmISAExt]
_ ArmABI
_ -> forall a. String -> a
panic String
"targetMkVirtualReg ArchARM"
      Arch
ArchAArch64   -> Unique -> Format -> VirtualReg
AArch64.mkVirtualReg
      Arch
ArchAlpha     -> forall a. String -> a
panic String
"targetMkVirtualReg ArchAlpha"
      Arch
ArchMipseb    -> forall a. String -> a
panic String
"targetMkVirtualReg ArchMipseb"
      Arch
ArchMipsel    -> forall a. String -> a
panic String
"targetMkVirtualReg ArchMipsel"
      Arch
ArchRISCV64   -> forall a. String -> a
panic String
"targetMkVirtualReg ArchRISCV64"
      Arch
ArchJavaScript-> forall a. String -> a
panic String
"targetMkVirtualReg ArchJavaScript"
      Arch
ArchUnknown   -> 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     -> forall a. String -> a
panic String
"targetRegDotColor ArchS390X"
      Arch
ArchSPARC     -> RealReg -> SDoc
SPARC.regDotColor
      Arch
ArchSPARC64   -> forall a. String -> a
panic String
"targetRegDotColor ArchSPARC64"
      ArchPPC_64 PPC_64ABI
_  -> RealReg -> SDoc
PPC.regDotColor
      ArchARM ArmISA
_ [ArmISAExt]
_ ArmABI
_ -> forall a. String -> a
panic String
"targetRegDotColor ArchARM"
      Arch
ArchAArch64   -> RealReg -> SDoc
AArch64.regDotColor
      Arch
ArchAlpha     -> forall a. String -> a
panic String
"targetRegDotColor ArchAlpha"
      Arch
ArchMipseb    -> forall a. String -> a
panic String
"targetRegDotColor ArchMipseb"
      Arch
ArchMipsel    -> forall a. String -> a
panic String
"targetRegDotColor ArchMipsel"
      Arch
ArchRISCV64   -> forall a. String -> a
panic String
"targetRegDotColor ArchRISCV64"
      Arch
ArchJavaScript-> forall a. String -> a
panic String
"targetRegDotColor ArchJavaScript"
      Arch
ArchUnknown   -> 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