Platform.hs 10.2 KB
Newer Older
1
{-# LANGUAGE LambdaCase, ScopedTypeVariables #-}
2 3 4

-- | A description of the platform we're compiling for.
--
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
module GHC.Platform
   ( PlatformMini(..)
   , PlatformWordSize(..)
   , Platform(..)
   , platformArch
   , platformOS
   , Arch(..)
   , OS(..)
   , ArmISA(..)
   , ArmISAExt(..)
   , ArmABI(..)
   , PPC_64ABI(..)
   , ByteOrder(..)
   , target32Bit
   , isARM
   , osElfTarget
   , osMachOTarget
   , osSubsectionsViaSymbols
   , platformUsesFrameworks
   , platformWordSizeInBytes
   , platformWordSizeInBits
   , platformMinInt
   , platformMaxInt
   , platformMaxWord
   , platformInIntRange
   , platformInWordRange
   , PlatformMisc(..)
   , IntegerLibrary(..)
   , stringEncodeArch
   , stringEncodeOS
   , SseVersion (..)
   , BmiVersion (..)
37 38 39 40
)

where

John Ericson's avatar
John Ericson committed
41
import Prelude -- See Note [Why do we import Prelude here?]
42
import GHC.Read
43
import GHC.ByteOrder (ByteOrder(..))
Sylvain Henry's avatar
Sylvain Henry committed
44 45
import Data.Word
import Data.Int
46

47 48 49 50 51 52 53 54 55 56
-- | Contains the bare-bones arch and os information. This isn't enough for
-- code gen, but useful for tasks where we can fall back upon the host
-- platform, as this is all we know about the host platform.
data PlatformMini
  = PlatformMini
    { platformMini_arch :: Arch
    , platformMini_os :: OS
    }
    deriving (Read, Show, Eq)

57
-- | Contains enough information for the native code generator to emit
58
-- code for this platform.
59
data Platform = Platform
60 61 62 63 64 65 66 67 68
   { platformMini                     :: !PlatformMini
   , platformWordSize                 :: !PlatformWordSize -- ^ Word size
   , platformByteOrder                :: !ByteOrder        -- ^ Byte order (endianness)
   , platformUnregisterised           :: !Bool
   , platformHasGnuNonexecStack       :: !Bool
   , platformHasIdentDirective        :: !Bool
   , platformHasSubsectionsViaSymbols :: !Bool
   , platformIsCrossCompiling         :: !Bool
   , platformLeadingUnderscore        :: !Bool             -- ^ Symbols need underscore prefix
69 70
   }
   deriving (Read, Show, Eq)
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
data PlatformWordSize
  = PW4 -- ^ A 32-bit platform
  | PW8 -- ^ A 64-bit platform
  deriving (Eq)

instance Show PlatformWordSize where
  show PW4 = "4"
  show PW8 = "8"

instance Read PlatformWordSize where
  readPrec = do
    i :: Int <- readPrec
    case i of
      4 -> return PW4
      8 -> return PW8
      other -> fail ("Invalid PlatformWordSize: " ++ show other)

platformWordSizeInBytes :: Platform -> Int
platformWordSizeInBytes p =
    case platformWordSize p of
      PW4 -> 4
      PW8 -> 8

platformWordSizeInBits :: Platform -> Int
platformWordSizeInBits p = platformWordSizeInBytes p * 8
97

98 99 100 101 102 103 104 105
-- | Legacy accessor
platformArch :: Platform -> Arch
platformArch = platformMini_arch . platformMini

-- | Legacy accessor
platformOS :: Platform -> OS
platformOS = platformMini_os . platformMini

106
-- | Architectures that the native code generator knows about.
107 108
--      TODO: It might be nice to extend these constructors with information
--      about what instruction set extensions an architecture might support.
109 110
--
data Arch
Ian Lynagh's avatar
Ian Lynagh committed
111 112
        = ArchUnknown
        | ArchX86
113 114 115
        | ArchX86_64
        | ArchPPC
        | ArchPPC_64
116 117
          { ppc_64ABI :: PPC_64ABI
          }
118
        | ArchS390X
119
        | ArchSPARC
120
        | ArchSPARC64
121
        | ArchARM
122
          { armISA    :: ArmISA
123 124 125
          , armISAExt :: [ArmISAExt]
          , armABI    :: ArmABI
          }
Colin Watson's avatar
Colin Watson committed
126
        | ArchARM64
127 128 129
        | ArchAlpha
        | ArchMipseb
        | ArchMipsel
thoughtpolice's avatar
thoughtpolice committed
130
        | ArchJavaScript
131
        deriving (Read, Show, Eq)
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
-- 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 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 = \case
  ArchUnknown -> "unknown"
  ArchX86 -> "i386"
  ArchX86_64 -> "x86_64"
  ArchPPC -> "powerpc"
  ArchPPC_64 { ppc_64ABI = abi } -> case abi of
    ELF_V1 -> "powerpc64"
    ELF_V2 -> "powerpc64le"
158
  ArchS390X -> "s390x"
159 160 161 162 163 164 165 166 167 168 169 170 171 172
  ArchSPARC -> "sparc"
  ArchSPARC64 -> "sparc64"
  ArchARM { armISA = isa, armISAExt = _, armABI = _ } -> "arm" ++ vsuf
    where
      vsuf = case isa of
        ARMv5 -> "v5"
        ARMv6 -> "v6"
        ARMv7 -> "v7"
  ArchARM64 -> "aarch64"
  ArchAlpha -> "alpha"
  ArchMipseb -> "mipseb"
  ArchMipsel -> "mipsel"
  ArchJavaScript -> "js"

mad.one@gmail.com's avatar
mad.one@gmail.com committed
173 174
isARM :: Arch -> Bool
isARM (ArchARM {}) = True
175
isARM ArchARM64    = True
mad.one@gmail.com's avatar
mad.one@gmail.com committed
176
isARM _ = False
177

Ian Lynagh's avatar
Ian Lynagh committed
178 179
-- | Operating systems that the native code generator knows about.
--      Having OSUnknown should produce a sensible default, but no promises.
180
data OS
Ian Lynagh's avatar
Ian Lynagh committed
181 182
        = OSUnknown
        | OSLinux
183 184 185 186
        | OSDarwin
        | OSSolaris2
        | OSMinGW32
        | OSFreeBSD
187
        | OSDragonFly
188
        | OSOpenBSD
Ian Lynagh's avatar
Ian Lynagh committed
189
        | OSNetBSD
190
        | OSKFreeBSD
pcapriotti's avatar
pcapriotti committed
191
        | OSHaiku
192
        | OSQNXNTO
193
        | OSAIX
194
        | OSHurd
195
        deriving (Read, Show, Eq)
196

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
-- | See Note [Platform Syntax].
stringEncodeOS :: OS -> String
stringEncodeOS = \case
  OSUnknown -> "unknown"
  OSLinux -> "linux"
  OSDarwin -> "darwin"
  OSSolaris2 -> "solaris2"
  OSMinGW32 -> "mingw32"
  OSFreeBSD -> "freebsd"
  OSDragonFly -> "dragonfly"
  OSOpenBSD -> "openbsd"
  OSNetBSD -> "netbsd"
  OSKFreeBSD -> "kfreebsdgnu"
  OSHaiku -> "haiku"
  OSQNXNTO -> "nto-qnx"
  OSAIX -> "aix"
  OSHurd -> "hurd"

215
-- | ARM Instruction Set Architecture, Extensions and ABI
216 217 218 219 220
--
data ArmISA
    = ARMv5
    | ARMv6
    | ARMv7
221
    deriving (Read, Show, Eq)
222 223 224 225 226 227 228

data ArmISAExt
    = VFPv2
    | VFPv3
    | VFPv3D16
    | NEON
    | IWMMX2
229
    deriving (Read, Show, Eq)
230

231 232 233 234 235
data ArmABI
    = SOFT
    | SOFTFP
    | HARD
    deriving (Read, Show, Eq)
236

237 238 239 240 241 242 243 244
-- | PowerPC 64-bit ABI
--
data PPC_64ABI
    = ELF_V1
    | ELF_V2
    deriving (Read, Show, Eq)

-- | This predicate tells us whether the platform is 32-bit.
245
target32Bit :: Platform -> Bool
246 247 248 249
target32Bit p =
    case platformWordSize p of
      PW4 -> True
      PW8 -> False
250

251
-- | This predicate tells us whether the OS supports ELF-like shared libraries.
252
osElfTarget :: OS -> Bool
253 254 255 256 257 258 259 260 261
osElfTarget OSLinux     = True
osElfTarget OSFreeBSD   = True
osElfTarget OSDragonFly = True
osElfTarget OSOpenBSD   = True
osElfTarget OSNetBSD    = True
osElfTarget OSSolaris2  = True
osElfTarget OSDarwin    = False
osElfTarget OSMinGW32   = False
osElfTarget OSKFreeBSD  = True
pcapriotti's avatar
pcapriotti committed
262
osElfTarget OSHaiku     = True
263
osElfTarget OSQNXNTO    = False
264
osElfTarget OSAIX       = False
265
osElfTarget OSHurd      = True
266
osElfTarget OSUnknown   = False
267 268 269 270
 -- Defaulting to False is safe; it means don't rely on any
 -- ELF-specific functionality.  It is important to have a default for
 -- portability, otherwise we have to answer this question for every
 -- new platform we compile on (even unreg).
ian@well-typed.com's avatar
ian@well-typed.com committed
271

272 273 274 275 276
-- | This predicate tells us whether the OS support Mach-O shared libraries.
osMachOTarget :: OS -> Bool
osMachOTarget OSDarwin = True
osMachOTarget _ = False

277 278 279 280 281 282 283
osUsesFrameworks :: OS -> Bool
osUsesFrameworks OSDarwin = True
osUsesFrameworks _        = False

platformUsesFrameworks :: Platform -> Bool
platformUsesFrameworks = osUsesFrameworks . platformOS

284 285 286 287
osSubsectionsViaSymbols :: OS -> Bool
osSubsectionsViaSymbols OSDarwin = True
osSubsectionsViaSymbols _        = False

288 289 290 291 292 293 294 295 296 297 298 299 300
-- | Platform-specific settings formerly hard-coded in Config.hs.
--
-- These should probably be all be triaged whether they can be computed from
-- other settings or belong in another another place (like 'Platform' above).
data PlatformMisc = PlatformMisc
  { -- TODO Recalculate string from richer info?
    platformMisc_targetPlatformString :: String
  , platformMisc_integerLibrary       :: String
  , platformMisc_integerLibraryType   :: IntegerLibrary
  , platformMisc_ghcWithInterpreter   :: Bool
  , platformMisc_ghcWithNativeCodeGen :: Bool
  , platformMisc_ghcWithSMP           :: Bool
  , platformMisc_ghcRTSWays           :: String
301 302 303
  -- | Determines whether we will be compiling info tables that reside just
  --   before the entry code, or with an indirection to the entry code. See
  --   TABLES_NEXT_TO_CODE in includes/rts/storage/InfoTables.h.
304 305 306 307 308
  , platformMisc_tablesNextToCode     :: Bool
  , platformMisc_libFFI               :: Bool
  , platformMisc_ghcThreaded          :: Bool
  , platformMisc_ghcDebugged          :: Bool
  , platformMisc_ghcRtsWithLibdw      :: Bool
309
  , platformMisc_llvmTarget           :: String
310 311 312 313 314 315
  }

data IntegerLibrary
    = IntegerGMP
    | IntegerSimple
    deriving (Read, Show, Eq)
Sylvain Henry's avatar
Sylvain Henry committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

-- | Minimum representable Int value for the given platform
platformMinInt :: Platform -> Integer
platformMinInt p = case platformWordSize p of
   PW4 -> toInteger (minBound :: Int32)
   PW8 -> toInteger (minBound :: Int64)

-- | Maximum representable Int value for the given platform
platformMaxInt :: Platform -> Integer
platformMaxInt p = case platformWordSize p of
   PW4 -> toInteger (maxBound :: Int32)
   PW8 -> toInteger (maxBound :: Int64)

-- | Maximum representable Word value for the given platform
platformMaxWord :: Platform -> Integer
platformMaxWord p = case platformWordSize p of
   PW4 -> toInteger (maxBound :: Word32)
   PW8 -> toInteger (maxBound :: Word64)

-- | Test if the given Integer is representable with a platform Int
platformInIntRange :: Platform -> Integer -> Bool
platformInIntRange platform x = x >= platformMinInt platform && x <= platformMaxInt platform

-- | Test if the given Integer is representable with a platform Word
platformInWordRange :: Platform -> Integer -> Bool
platformInWordRange platform x = x >= 0 && x <= platformMaxWord platform
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362


--------------------------------------------------
-- Instruction sets
--------------------------------------------------

-- | x86 SSE instructions
data SseVersion
   = SSE1
   | SSE2
   | SSE3
   | SSE4
   | SSE42
   deriving (Eq, Ord)

-- | x86 BMI (bit manipulation) instructions
data BmiVersion
   = BMI1
   | BMI2
   deriving (Eq, Ord)