Opened 13 months ago

Last modified 13 months ago

#15533 new feature request

Access the number of bits in the target machine's Int type at compile time

Reported by: ChaiTRex Owned by:
Priority: normal Milestone: 8.6.1
Component: Template Haskell Version: 8.4.3
Keywords: Cc:
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 (last modified by ChaiTRex)

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):

25-- | Contains enough information for the native code generator to emit
26--      code for this platform.
27data Platform
28        = Platform {
29              platformArch                     :: Arch,
30              platformOS                       :: OS,
31              -- Word size in bytes (i.e. normally 4 or 8,
32              -- for 32bit and 64bit platforms respectively)
33              platformWordSize                 :: {-# UNPACK #-} !Int,
34              platformUnregisterised           :: Bool,
35              platformHasGnuNonexecStack       :: Bool,
36              platformHasIdentDirective        :: Bool,
37              platformHasSubsectionsViaSymbols :: Bool,
38              platformIsCrossCompiling         :: Bool
39          }
40        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.

Change History (2)

comment:1 Changed 13 months ago by ChaiTRex

Description: modified (diff)

comment:2 Changed 13 months ago by bgamari

Component: CompilerTemplate Haskell

It sounds like this is really asking for a Template Haskell interface.

Note: See TracTickets for help on using tickets.