
#15533: Access the number of bits in the target machine's Int type at compile time -------------------------------------+------------------------------------- Reporter: ChaiTRex | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: 8.6.1 Component: Compiler | Version: 8.4.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Description changed by ChaiTRex: Old description:
I'm not sure of the current progress on GHC cross-compiling user programs, but I think a small addition might need to be made somewhere in the modules included with `ghc`.
If I'm reading the documentation correctly, [https://hackage.haskell.org/package/base-4.11.1.0/docs/GHC- ByteOrder.html#v:targetByteOrder GHC.ByteOrder.targetByteOrder] will let us know the endianness of the target machine even if the compiling machine has a different endianness. I assume that this allows Template Haskell to, at compile time, generate proper code specifically for the target based on that fact.
I can't find any way of finding out the bit size of an `Int` (specifically the exact value of `(finiteBitSize (undefined :: Int)`) on the target platform, and it would be nice if that were added in a way that's convenient to access in general and by Template Haskell at compile time, similar to `GHC.ByteOrder.targetByteOrder`.
----
I've looked into it a little bit and found that [https://github.com/ghc/ghc/blob/master/compiler/utils/Platform.hs#L33 the GHC API will tell you the word size of the target in bytes with platformWordSize] (also on [https://hackage.haskell.org/package/ghc-8.4.3/docs/Platform.html#v:platformW... Hackage]):
{{{#!hs lineno=25 id=b marks=31-33 -- | Contains enough information for the native code generator to emit -- code for this platform. data Platform = Platform { platformArch :: Arch, platformOS :: OS, -- Word size in bytes (i.e. normally 4 or 8, -- for 32bit and 64bit platforms respectively) platformWordSize :: {-# UNPACK #-} !Int, platformUnregisterised :: Bool, platformHasGnuNonexecStack :: Bool, platformHasIdentDirective :: Bool, platformHasSubsectionsViaSymbols :: Bool, platformIsCrossCompiling :: Bool } deriving (Read, Show, Eq) }}}
The problem is that this doesn't account for possible tag bits reducing the size ''in bits'' of `Int` to something less than the given `platformWordSize`.
Recently, [https://ghc.haskell.org/trac/ghc/ticket/15486 support for WORD_SIZE_IN_BITS < 32 was dropped], so tag bits aren't a problem on 32-bit platforms anymore, but they might be on 64-bit platforms.
New description: I'm not sure of the current progress on GHC cross-compiling user programs, but I think a small addition might need to be made somewhere in the modules included with `ghc`. If I'm reading the documentation correctly, [https://hackage.haskell.org/package/base-4.11.1.0/docs/GHC- ByteOrder.html#v:targetByteOrder GHC.ByteOrder.targetByteOrder] will let us know the endianness of the target machine even if the compiling machine has a different endianness. I assume that this allows Template Haskell to, at compile time, generate proper code specifically for the target based on that fact. I can't find any way of finding out the bit size of an `Int` (specifically the exact value of `(finiteBitSize (undefined :: Int)`) on the target platform, and it would be nice if that were added in a way that's convenient to access in general and by Template Haskell at compile time, similar to `GHC.ByteOrder.targetByteOrder`. ---- I've looked into it a little bit and found that [https://github.com/ghc/ghc/blob/master/compiler/utils/Platform.hs#L31-L33 the GHC API will tell you the word size of the target in bytes with platformWordSize] (also on [https://hackage.haskell.org/package/ghc-8.4.3/docs/Platform.html#v:platformW... Hackage]): {{{#!hs lineno=25 id=b marks=31-33 -- | Contains enough information for the native code generator to emit -- code for this platform. data Platform = Platform { platformArch :: Arch, platformOS :: OS, -- Word size in bytes (i.e. normally 4 or 8, -- for 32bit and 64bit platforms respectively) platformWordSize :: {-# UNPACK #-} !Int, platformUnregisterised :: Bool, platformHasGnuNonexecStack :: Bool, platformHasIdentDirective :: Bool, platformHasSubsectionsViaSymbols :: Bool, platformIsCrossCompiling :: Bool } deriving (Read, Show, Eq) }}} The problem is that this doesn't account for possible tag bits reducing the size ''in bits'' of `Int` to something less than the given `platformWordSize`. Recently, [https://ghc.haskell.org/trac/ghc/ticket/15486 support for WORD_SIZE_IN_BITS < 32 was dropped], so tag bits aren't a problem on 32-bit platforms anymore, but they might be on 64-bit platforms. -- -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15533#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler