Access the number of bits in the target machine's Int type at compile time
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, 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 the GHC API will tell you the word size of the target in bytes with platformWordSize (also on Hackage):
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, 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.