{-# 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]
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
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
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, Eq ArchOS
ArchOS -> ArchOS -> Bool
ArchOS -> ArchOS -> Ordering
ArchOS -> ArchOS -> ArchOS
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ArchOS -> ArchOS -> ArchOS
$cmin :: ArchOS -> ArchOS -> ArchOS
max :: ArchOS -> ArchOS -> ArchOS
$cmax :: ArchOS -> ArchOS -> ArchOS
>= :: ArchOS -> ArchOS -> Bool
$c>= :: ArchOS -> ArchOS -> Bool
> :: ArchOS -> ArchOS -> Bool
$c> :: ArchOS -> ArchOS -> Bool
<= :: ArchOS -> ArchOS -> Bool
$c<= :: ArchOS -> ArchOS -> Bool
< :: ArchOS -> ArchOS -> Bool
$c< :: ArchOS -> ArchOS -> Bool
compare :: ArchOS -> ArchOS -> Ordering
$ccompare :: ArchOS -> ArchOS -> Ordering
Ord)

-- | 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
   | ArchLoongArch64
   | ArchJavaScript
   | ArchWasm32
   deriving (ReadPrec [Arch]
ReadPrec Arch
Int -> ReadS Arch
ReadS [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
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
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, Eq Arch
Arch -> Arch -> Bool
Arch -> Arch -> Ordering
Arch -> Arch -> Arch
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Arch -> Arch -> Arch
$cmin :: Arch -> Arch -> Arch
max :: Arch -> Arch -> Arch
$cmax :: Arch -> Arch -> Arch
>= :: Arch -> Arch -> Bool
$c>= :: Arch -> Arch -> Bool
> :: Arch -> Arch -> Bool
$c> :: Arch -> Arch -> Bool
<= :: Arch -> Arch -> Bool
$c<= :: Arch -> Arch -> Bool
< :: Arch -> Arch -> Bool
$c< :: Arch -> Arch -> Bool
compare :: Arch -> Arch -> Ordering
$ccompare :: Arch -> Arch -> Ordering
Ord)

-- | ARM Instruction Set Architecture
data ArmISA
   = ARMv5
   | ARMv6
   | ARMv7
   deriving (ReadPrec [ArmISA]
ReadPrec ArmISA
Int -> ReadS ArmISA
ReadS [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
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
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, Eq ArmISA
ArmISA -> ArmISA -> Bool
ArmISA -> ArmISA -> Ordering
ArmISA -> ArmISA -> ArmISA
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ArmISA -> ArmISA -> ArmISA
$cmin :: ArmISA -> ArmISA -> ArmISA
max :: ArmISA -> ArmISA -> ArmISA
$cmax :: ArmISA -> ArmISA -> ArmISA
>= :: ArmISA -> ArmISA -> Bool
$c>= :: ArmISA -> ArmISA -> Bool
> :: ArmISA -> ArmISA -> Bool
$c> :: ArmISA -> ArmISA -> Bool
<= :: ArmISA -> ArmISA -> Bool
$c<= :: ArmISA -> ArmISA -> Bool
< :: ArmISA -> ArmISA -> Bool
$c< :: ArmISA -> ArmISA -> Bool
compare :: ArmISA -> ArmISA -> Ordering
$ccompare :: ArmISA -> ArmISA -> Ordering
Ord)

-- | ARM extensions
data ArmISAExt
   = VFPv2
   | VFPv3
   | VFPv3D16
   | NEON
   | IWMMX2
   deriving (ReadPrec [ArmISAExt]
ReadPrec ArmISAExt
Int -> ReadS ArmISAExt
ReadS [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
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
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, Eq ArmISAExt
ArmISAExt -> ArmISAExt -> Bool
ArmISAExt -> ArmISAExt -> Ordering
ArmISAExt -> ArmISAExt -> ArmISAExt
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ArmISAExt -> ArmISAExt -> ArmISAExt
$cmin :: ArmISAExt -> ArmISAExt -> ArmISAExt
max :: ArmISAExt -> ArmISAExt -> ArmISAExt
$cmax :: ArmISAExt -> ArmISAExt -> ArmISAExt
>= :: ArmISAExt -> ArmISAExt -> Bool
$c>= :: ArmISAExt -> ArmISAExt -> Bool
> :: ArmISAExt -> ArmISAExt -> Bool
$c> :: ArmISAExt -> ArmISAExt -> Bool
<= :: ArmISAExt -> ArmISAExt -> Bool
$c<= :: ArmISAExt -> ArmISAExt -> Bool
< :: ArmISAExt -> ArmISAExt -> Bool
$c< :: ArmISAExt -> ArmISAExt -> Bool
compare :: ArmISAExt -> ArmISAExt -> Ordering
$ccompare :: ArmISAExt -> ArmISAExt -> Ordering
Ord)

-- | ARM ABI
data ArmABI
   = SOFT
   | SOFTFP
   | HARD
   deriving (ReadPrec [ArmABI]
ReadPrec ArmABI
Int -> ReadS ArmABI
ReadS [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
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
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, Eq ArmABI
ArmABI -> ArmABI -> Bool
ArmABI -> ArmABI -> Ordering
ArmABI -> ArmABI -> ArmABI
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ArmABI -> ArmABI -> ArmABI
$cmin :: ArmABI -> ArmABI -> ArmABI
max :: ArmABI -> ArmABI -> ArmABI
$cmax :: ArmABI -> ArmABI -> ArmABI
>= :: ArmABI -> ArmABI -> Bool
$c>= :: ArmABI -> ArmABI -> Bool
> :: ArmABI -> ArmABI -> Bool
$c> :: ArmABI -> ArmABI -> Bool
<= :: ArmABI -> ArmABI -> Bool
$c<= :: ArmABI -> ArmABI -> Bool
< :: ArmABI -> ArmABI -> Bool
$c< :: ArmABI -> ArmABI -> Bool
compare :: ArmABI -> ArmABI -> Ordering
$ccompare :: ArmABI -> ArmABI -> Ordering
Ord)

-- | 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]
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
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
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, Eq PPC_64ABI
PPC_64ABI -> PPC_64ABI -> Bool
PPC_64ABI -> PPC_64ABI -> Ordering
PPC_64ABI -> PPC_64ABI -> PPC_64ABI
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: PPC_64ABI -> PPC_64ABI -> PPC_64ABI
$cmin :: PPC_64ABI -> PPC_64ABI -> PPC_64ABI
max :: PPC_64ABI -> PPC_64ABI -> PPC_64ABI
$cmax :: PPC_64ABI -> PPC_64ABI -> PPC_64ABI
>= :: PPC_64ABI -> PPC_64ABI -> Bool
$c>= :: PPC_64ABI -> PPC_64ABI -> Bool
> :: PPC_64ABI -> PPC_64ABI -> Bool
$c> :: PPC_64ABI -> PPC_64ABI -> Bool
<= :: PPC_64ABI -> PPC_64ABI -> Bool
$c<= :: PPC_64ABI -> PPC_64ABI -> Bool
< :: PPC_64ABI -> PPC_64ABI -> Bool
$c< :: PPC_64ABI -> PPC_64ABI -> Bool
compare :: PPC_64ABI -> PPC_64ABI -> Ordering
$ccompare :: PPC_64ABI -> PPC_64ABI -> Ordering
Ord)

-- | 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
   | OSWasi
   | OSGhcjs
   deriving (ReadPrec [OS]
ReadPrec OS
Int -> ReadS OS
ReadS [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
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
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, Eq OS
OS -> OS -> Bool
OS -> OS -> Ordering
OS -> OS -> OS
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: OS -> OS -> OS
$cmin :: OS -> OS -> OS
max :: OS -> OS -> OS
$cmax :: OS -> OS -> OS
>= :: OS -> OS -> Bool
$c>= :: OS -> OS -> Bool
> :: OS -> OS -> Bool
$c> :: OS -> OS -> Bool
<= :: OS -> OS -> Bool
$c<= :: OS -> OS -> Bool
< :: OS -> OS -> Bool
$c< :: OS -> OS -> Bool
compare :: OS -> OS -> Ordering
$ccompare :: OS -> OS -> Ordering
Ord)


-- 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
ArchLoongArch64   -> String
"loongarch64"
  Arch
ArchJavaScript    -> String
"javascript"
  Arch
ArchWasm32        -> String
"wasm32"

-- | 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"
  OS
OSWasi      -> String
"wasi"
  OS
OSGhcjs     -> String
"ghcjs"