{-# LANGUAGE LambdaCase, ScopedTypeVariables #-}

-- | Platform architecture and OS
--
-- We need it in ghc-boot because ghc-pkg needs it.
module GHC.Platform.ArchOS
   ( ArchOS(..)
   , Arch(..)
   , OS(..)
   , ArmISA(..)
   , ArmISAExt(..)
   , ArmABI(..)
   , PPC_64ABI(..)
   , stringEncodeArch
   , stringEncodeOS
   )
where

import Prelude -- See Note [Why do we import Prelude here?]

-- | Platform architecture and OS.
data ArchOS
   = ArchOS
      { ArchOS -> Arch
archOS_arch :: Arch
      , ArchOS -> OS
archOS_OS   :: OS
      }
   deriving (ReadPrec [ArchOS]
ReadPrec ArchOS
Int -> ReadS ArchOS
ReadS [ArchOS]
(Int -> ReadS ArchOS)
-> ReadS [ArchOS]
-> ReadPrec ArchOS
-> ReadPrec [ArchOS]
-> Read ArchOS
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ArchOS]
$creadListPrec :: ReadPrec [ArchOS]
readPrec :: ReadPrec ArchOS
$creadPrec :: ReadPrec ArchOS
readList :: ReadS [ArchOS]
$creadList :: ReadS [ArchOS]
readsPrec :: Int -> ReadS ArchOS
$creadsPrec :: Int -> ReadS ArchOS
Read, Int -> ArchOS -> ShowS
[ArchOS] -> ShowS
ArchOS -> String
(Int -> ArchOS -> ShowS)
-> (ArchOS -> String) -> ([ArchOS] -> ShowS) -> Show ArchOS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ArchOS] -> ShowS
$cshowList :: [ArchOS] -> ShowS
show :: ArchOS -> String
$cshow :: ArchOS -> String
showsPrec :: Int -> ArchOS -> ShowS
$cshowsPrec :: Int -> ArchOS -> ShowS
Show, ArchOS -> ArchOS -> Bool
(ArchOS -> ArchOS -> Bool)
-> (ArchOS -> ArchOS -> Bool) -> Eq ArchOS
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ArchOS -> ArchOS -> Bool
$c/= :: ArchOS -> ArchOS -> Bool
== :: ArchOS -> ArchOS -> Bool
$c== :: ArchOS -> ArchOS -> Bool
Eq)

-- | Architectures
--
-- TODO: It might be nice to extend these constructors with information about
-- what instruction set extensions an architecture might support.
--
data Arch
   = ArchUnknown
   | ArchX86
   | ArchX86_64
   | ArchPPC
   | ArchPPC_64 PPC_64ABI
   | ArchS390X
   | ArchARM ArmISA [ArmISAExt] ArmABI
   | ArchAArch64
   | ArchAlpha
   | ArchMipseb
   | ArchMipsel
   | ArchRISCV64
   | ArchJavaScript
   deriving (ReadPrec [Arch]
ReadPrec Arch
Int -> ReadS Arch
ReadS [Arch]
(Int -> ReadS Arch)
-> ReadS [Arch] -> ReadPrec Arch -> ReadPrec [Arch] -> Read Arch
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Arch]
$creadListPrec :: ReadPrec [Arch]
readPrec :: ReadPrec Arch
$creadPrec :: ReadPrec Arch
readList :: ReadS [Arch]
$creadList :: ReadS [Arch]
readsPrec :: Int -> ReadS Arch
$creadsPrec :: Int -> ReadS Arch
Read, Int -> Arch -> ShowS
[Arch] -> ShowS
Arch -> String
(Int -> Arch -> ShowS)
-> (Arch -> String) -> ([Arch] -> ShowS) -> Show Arch
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Arch] -> ShowS
$cshowList :: [Arch] -> ShowS
show :: Arch -> String
$cshow :: Arch -> String
showsPrec :: Int -> Arch -> ShowS
$cshowsPrec :: Int -> Arch -> ShowS
Show, Arch -> Arch -> Bool
(Arch -> Arch -> Bool) -> (Arch -> Arch -> Bool) -> Eq Arch
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Arch -> Arch -> Bool
$c/= :: Arch -> Arch -> Bool
== :: Arch -> Arch -> Bool
$c== :: Arch -> Arch -> Bool
Eq)

-- | ARM Instruction Set Architecture
data ArmISA
   = ARMv5
   | ARMv6
   | ARMv7
   deriving (ReadPrec [ArmISA]
ReadPrec ArmISA
Int -> ReadS ArmISA
ReadS [ArmISA]
(Int -> ReadS ArmISA)
-> ReadS [ArmISA]
-> ReadPrec ArmISA
-> ReadPrec [ArmISA]
-> Read ArmISA
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ArmISA]
$creadListPrec :: ReadPrec [ArmISA]
readPrec :: ReadPrec ArmISA
$creadPrec :: ReadPrec ArmISA
readList :: ReadS [ArmISA]
$creadList :: ReadS [ArmISA]
readsPrec :: Int -> ReadS ArmISA
$creadsPrec :: Int -> ReadS ArmISA
Read, Int -> ArmISA -> ShowS
[ArmISA] -> ShowS
ArmISA -> String
(Int -> ArmISA -> ShowS)
-> (ArmISA -> String) -> ([ArmISA] -> ShowS) -> Show ArmISA
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ArmISA] -> ShowS
$cshowList :: [ArmISA] -> ShowS
show :: ArmISA -> String
$cshow :: ArmISA -> String
showsPrec :: Int -> ArmISA -> ShowS
$cshowsPrec :: Int -> ArmISA -> ShowS
Show, ArmISA -> ArmISA -> Bool
(ArmISA -> ArmISA -> Bool)
-> (ArmISA -> ArmISA -> Bool) -> Eq ArmISA
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ArmISA -> ArmISA -> Bool
$c/= :: ArmISA -> ArmISA -> Bool
== :: ArmISA -> ArmISA -> Bool
$c== :: ArmISA -> ArmISA -> Bool
Eq)

-- | ARM extensions
data ArmISAExt
   = VFPv2
   | VFPv3
   | VFPv3D16
   | NEON
   | IWMMX2
   deriving (ReadPrec [ArmISAExt]
ReadPrec ArmISAExt
Int -> ReadS ArmISAExt
ReadS [ArmISAExt]
(Int -> ReadS ArmISAExt)
-> ReadS [ArmISAExt]
-> ReadPrec ArmISAExt
-> ReadPrec [ArmISAExt]
-> Read ArmISAExt
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ArmISAExt]
$creadListPrec :: ReadPrec [ArmISAExt]
readPrec :: ReadPrec ArmISAExt
$creadPrec :: ReadPrec ArmISAExt
readList :: ReadS [ArmISAExt]
$creadList :: ReadS [ArmISAExt]
readsPrec :: Int -> ReadS ArmISAExt
$creadsPrec :: Int -> ReadS ArmISAExt
Read, Int -> ArmISAExt -> ShowS
[ArmISAExt] -> ShowS
ArmISAExt -> String
(Int -> ArmISAExt -> ShowS)
-> (ArmISAExt -> String)
-> ([ArmISAExt] -> ShowS)
-> Show ArmISAExt
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ArmISAExt] -> ShowS
$cshowList :: [ArmISAExt] -> ShowS
show :: ArmISAExt -> String
$cshow :: ArmISAExt -> String
showsPrec :: Int -> ArmISAExt -> ShowS
$cshowsPrec :: Int -> ArmISAExt -> ShowS
Show, ArmISAExt -> ArmISAExt -> Bool
(ArmISAExt -> ArmISAExt -> Bool)
-> (ArmISAExt -> ArmISAExt -> Bool) -> Eq ArmISAExt
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ArmISAExt -> ArmISAExt -> Bool
$c/= :: ArmISAExt -> ArmISAExt -> Bool
== :: ArmISAExt -> ArmISAExt -> Bool
$c== :: ArmISAExt -> ArmISAExt -> Bool
Eq)

-- | ARM ABI
data ArmABI
   = SOFT
   | SOFTFP
   | HARD
   deriving (ReadPrec [ArmABI]
ReadPrec ArmABI
Int -> ReadS ArmABI
ReadS [ArmABI]
(Int -> ReadS ArmABI)
-> ReadS [ArmABI]
-> ReadPrec ArmABI
-> ReadPrec [ArmABI]
-> Read ArmABI
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ArmABI]
$creadListPrec :: ReadPrec [ArmABI]
readPrec :: ReadPrec ArmABI
$creadPrec :: ReadPrec ArmABI
readList :: ReadS [ArmABI]
$creadList :: ReadS [ArmABI]
readsPrec :: Int -> ReadS ArmABI
$creadsPrec :: Int -> ReadS ArmABI
Read, Int -> ArmABI -> ShowS
[ArmABI] -> ShowS
ArmABI -> String
(Int -> ArmABI -> ShowS)
-> (ArmABI -> String) -> ([ArmABI] -> ShowS) -> Show ArmABI
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ArmABI] -> ShowS
$cshowList :: [ArmABI] -> ShowS
show :: ArmABI -> String
$cshow :: ArmABI -> String
showsPrec :: Int -> ArmABI -> ShowS
$cshowsPrec :: Int -> ArmABI -> ShowS
Show, ArmABI -> ArmABI -> Bool
(ArmABI -> ArmABI -> Bool)
-> (ArmABI -> ArmABI -> Bool) -> Eq ArmABI
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ArmABI -> ArmABI -> Bool
$c/= :: ArmABI -> ArmABI -> Bool
== :: ArmABI -> ArmABI -> Bool
$c== :: ArmABI -> ArmABI -> Bool
Eq)

-- | PowerPC 64-bit ABI
data PPC_64ABI
   = ELF_V1 -- ^ PowerPC64
   | ELF_V2 -- ^ PowerPC64 LE
   deriving (ReadPrec [PPC_64ABI]
ReadPrec PPC_64ABI
Int -> ReadS PPC_64ABI
ReadS [PPC_64ABI]
(Int -> ReadS PPC_64ABI)
-> ReadS [PPC_64ABI]
-> ReadPrec PPC_64ABI
-> ReadPrec [PPC_64ABI]
-> Read PPC_64ABI
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PPC_64ABI]
$creadListPrec :: ReadPrec [PPC_64ABI]
readPrec :: ReadPrec PPC_64ABI
$creadPrec :: ReadPrec PPC_64ABI
readList :: ReadS [PPC_64ABI]
$creadList :: ReadS [PPC_64ABI]
readsPrec :: Int -> ReadS PPC_64ABI
$creadsPrec :: Int -> ReadS PPC_64ABI
Read, Int -> PPC_64ABI -> ShowS
[PPC_64ABI] -> ShowS
PPC_64ABI -> String
(Int -> PPC_64ABI -> ShowS)
-> (PPC_64ABI -> String)
-> ([PPC_64ABI] -> ShowS)
-> Show PPC_64ABI
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PPC_64ABI] -> ShowS
$cshowList :: [PPC_64ABI] -> ShowS
show :: PPC_64ABI -> String
$cshow :: PPC_64ABI -> String
showsPrec :: Int -> PPC_64ABI -> ShowS
$cshowsPrec :: Int -> PPC_64ABI -> ShowS
Show, PPC_64ABI -> PPC_64ABI -> Bool
(PPC_64ABI -> PPC_64ABI -> Bool)
-> (PPC_64ABI -> PPC_64ABI -> Bool) -> Eq PPC_64ABI
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PPC_64ABI -> PPC_64ABI -> Bool
$c/= :: PPC_64ABI -> PPC_64ABI -> Bool
== :: PPC_64ABI -> PPC_64ABI -> Bool
$c== :: PPC_64ABI -> PPC_64ABI -> Bool
Eq)

-- | Operating systems.
--
-- Using OSUnknown to generate code should produce a sensible default, but no
-- promises.
data OS
   = OSUnknown
   | OSLinux
   | OSDarwin
   | OSSolaris2
   | OSMinGW32
   | OSFreeBSD
   | OSDragonFly
   | OSOpenBSD
   | OSNetBSD
   | OSKFreeBSD
   | OSHaiku
   | OSQNXNTO
   | OSAIX
   | OSHurd
   deriving (ReadPrec [OS]
ReadPrec OS
Int -> ReadS OS
ReadS [OS]
(Int -> ReadS OS)
-> ReadS [OS] -> ReadPrec OS -> ReadPrec [OS] -> Read OS
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [OS]
$creadListPrec :: ReadPrec [OS]
readPrec :: ReadPrec OS
$creadPrec :: ReadPrec OS
readList :: ReadS [OS]
$creadList :: ReadS [OS]
readsPrec :: Int -> ReadS OS
$creadsPrec :: Int -> ReadS OS
Read, Int -> OS -> ShowS
[OS] -> ShowS
OS -> String
(Int -> OS -> ShowS)
-> (OS -> String) -> ([OS] -> ShowS) -> Show OS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [OS] -> ShowS
$cshowList :: [OS] -> ShowS
show :: OS -> String
$cshow :: OS -> String
showsPrec :: Int -> OS -> ShowS
$cshowsPrec :: Int -> OS -> ShowS
Show, OS -> OS -> Bool
(OS -> OS -> Bool) -> (OS -> OS -> Bool) -> Eq OS
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OS -> OS -> Bool
$c/= :: OS -> OS -> Bool
== :: OS -> OS -> Bool
$c== :: OS -> OS -> Bool
Eq)


-- Note [Platform Syntax]
-- ~~~~~~~~~~~~~~~~~~~~~~
--
-- There is a very loose encoding of platforms shared by many tools we are
-- encoding to here. GNU Config (http://git.savannah.gnu.org/cgit/config.git),
-- and LLVM's http://llvm.org/doxygen/classllvm_1_1Triple.html are perhaps the
-- most definitional parsers. The basic syntax is a list of '-'-separated
-- components. The Unix 'uname' command syntax is related but briefer.
--
-- Those two parsers are quite forgiving, and even the 'config.sub'
-- normalization is forgiving too. The "best" way to encode a platform is
-- therefore somewhat a matter of taste.
--
-- The 'stringEncode*' functions here convert each part of GHC's structured
-- notion of a platform into one dash-separated component.

-- | See Note [Platform Syntax].
stringEncodeArch :: Arch -> String
stringEncodeArch :: Arch -> String
stringEncodeArch = \case
  Arch
ArchUnknown       -> String
"unknown"
  Arch
ArchX86           -> String
"i386"
  Arch
ArchX86_64        -> String
"x86_64"
  Arch
ArchPPC           -> String
"powerpc"
  ArchPPC_64 PPC_64ABI
ELF_V1 -> String
"powerpc64"
  ArchPPC_64 PPC_64ABI
ELF_V2 -> String
"powerpc64le"
  Arch
ArchS390X         -> String
"s390x"
  ArchARM ArmISA
ARMv5 [ArmISAExt]
_ ArmABI
_ -> String
"armv5"
  ArchARM ArmISA
ARMv6 [ArmISAExt]
_ ArmABI
_ -> String
"armv6"
  ArchARM ArmISA
ARMv7 [ArmISAExt]
_ ArmABI
_ -> String
"armv7"
  Arch
ArchAArch64       -> String
"aarch64"
  Arch
ArchAlpha         -> String
"alpha"
  Arch
ArchMipseb        -> String
"mipseb"
  Arch
ArchMipsel        -> String
"mipsel"
  Arch
ArchRISCV64       -> String
"riscv64"
  Arch
ArchJavaScript    -> String
"js"

-- | See Note [Platform Syntax].
stringEncodeOS :: OS -> String
stringEncodeOS :: OS -> String
stringEncodeOS = \case
  OS
OSUnknown   -> String
"unknown"
  OS
OSLinux     -> String
"linux"
  OS
OSDarwin    -> String
"darwin"
  OS
OSSolaris2  -> String
"solaris2"
  OS
OSMinGW32   -> String
"mingw32"
  OS
OSFreeBSD   -> String
"freebsd"
  OS
OSDragonFly -> String
"dragonfly"
  OS
OSOpenBSD   -> String
"openbsd"
  OS
OSNetBSD    -> String
"netbsd"
  OS
OSKFreeBSD  -> String
"kfreebsdgnu"
  OS
OSHaiku     -> String
"haiku"
  OS
OSQNXNTO    -> String
"nto-qnx"
  OS
OSAIX       -> String
"aix"
  OS
OSHurd      -> String
"hurd"