Commit 260a5648 authored by wereHamster's avatar wereHamster Committed by Ben Gamari

Use stdint types for Stg{Word,Int}{8,16,32,64}

We can't define Stg{Int,Word} in terms of {,u}intptr_t because STG
depends on them being the exact same size as void*, and {,u}intptr_t
does not make that guarantee. Furthermore, we also need to define
StgHalf{Int,Word}, so the preprocessor if needs to stay. But we can at
least keep it in a single place instead of repeating it in various
files.

Also define STG_{INT,WORD}{8,16,32,64}_{MIN,MAX} and use it in HsFFI.h,
further reducing the need for CPP in other files.

Reviewers: austin, bgamari, simonmar, hvr, erikd

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D2182
parent dd3e8470
......@@ -20,50 +20,6 @@ extern "C" {
#include "ghcconfig.h"
#include "stg/Types.h"
/* get limits for integral types */
#if defined HAVE_STDINT_H && !defined USE_INTTYPES_H_FOR_RTS_PROBES_D
/* ISO C 99 says:
* "C++ implementations should define these macros only when
* __STDC_LIMIT_MACROS is defined before <stdint.h> is included."
*/
#define __STDC_LIMIT_MACROS
#include <stdint.h>
#elif defined(HAVE_INTTYPES_H)
#include <inttypes.h>
#else
/* second best guess (e.g. on Solaris) */
#include <limits.h>
#endif
#ifdef INT8_MIN
#define __INT8_MIN INT8_MIN
#define __INT16_MIN INT16_MIN
#define __INT32_MIN INT32_MIN
#define __INT64_MIN INT64_MIN
#define __INT8_MAX INT8_MAX
#define __INT16_MAX INT16_MAX
#define __INT32_MAX INT32_MAX
#define __INT64_MAX INT64_MAX
#define __UINT8_MAX UINT8_MAX
#define __UINT16_MAX UINT16_MAX
#define __UINT32_MAX UINT32_MAX
#define __UINT64_MAX UINT64_MAX
#else
/* if we had no luck, let's do it for ourselves (assuming 64bit long longs) */
#define __INT8_MIN (-128)
#define __INT16_MIN (-32767-1)
#define __INT32_MIN (-2147483647-1)
#define __INT64_MIN (-9223372036854775807LL-1)
#define __INT8_MAX (127)
#define __INT16_MAX (32767)
#define __INT32_MAX (2147483647)
#define __INT64_MAX (9223372036854775807LL)
#define __UINT8_MAX (255U)
#define __UINT16_MAX (65535U)
#define __UINT32_MAX (4294967295U)
#define __UINT64_MAX (18446744073709551615ULL)
#endif
/* get limits for floating point types */
#include <float.h>
......@@ -96,31 +52,23 @@ typedef void* HsStablePtr;
#define HS_BOOL_MIN HS_BOOL_FALSE
#define HS_BOOL_MAX HS_BOOL_TRUE
/* this mirrors the distinction of cases in StgTypes.h */
#if SIZEOF_VOID_P == 8
#define HS_INT_MIN __INT64_MIN
#define HS_INT_MAX __INT64_MAX
#define HS_WORD_MAX __UINT64_MAX
#elif SIZEOF_VOID_P == 4
#define HS_INT_MIN __INT32_MIN
#define HS_INT_MAX __INT32_MAX
#define HS_WORD_MAX __UINT32_MAX
#else
#error GHC untested on this architecture: sizeof(void *) != 4 or 8
#endif
#define HS_INT8_MIN __INT8_MIN
#define HS_INT8_MAX __INT8_MAX
#define HS_INT16_MIN __INT16_MIN
#define HS_INT16_MAX __INT16_MAX
#define HS_INT32_MIN __INT32_MIN
#define HS_INT32_MAX __INT32_MAX
#define HS_INT64_MIN __INT64_MIN
#define HS_INT64_MAX __INT64_MAX
#define HS_WORD8_MAX __UINT8_MAX
#define HS_WORD16_MAX __UINT16_MAX
#define HS_WORD32_MAX __UINT32_MAX
#define HS_WORD64_MAX __UINT64_MAX
#define HS_INT_MIN STG_INT_MIN
#define HS_INT_MAX STG_INT_MAX
#define HS_WORD_MAX STG_WORD_MAX
#define HS_INT8_MIN STG_INT8_MIN
#define HS_INT8_MAX STG_INT8_MAX
#define HS_INT16_MIN STG_INT16_MIN
#define HS_INT16_MAX STG_INT16_MAX
#define HS_INT32_MIN STG_INT32_MIN
#define HS_INT32_MAX STG_INT32_MAX
#define HS_INT64_MIN STG_INT64_MIN
#define HS_INT64_MAX STG_INT64_MAX
#define HS_WORD8_MAX STG_WORD8_MAX
#define HS_WORD16_MAX STG_WORD16_MAX
#define HS_WORD32_MAX STG_WORD32_MAX
#define HS_WORD64_MAX STG_WORD64_MAX
#define HS_FLOAT_RADIX FLT_RADIX
#define HS_FLOAT_ROUNDS FLT_ROUNDS
......
......@@ -21,6 +21,16 @@
#ifndef STGTYPES_H
#define STGTYPES_H
/* ISO C 99 says:
* "C++ implementations should define these macros only when
* __STDC_LIMIT_MACROS is defined before <stdint.h> is included."
*
* C++11 does not require it anymore. Remove once we upgrade to C++11 or newer.
*/
#define __STDC_LIMIT_MACROS
#import <inttypes.h>
/*
* This module should define types *only*, all beginning with "Stg".
*
......@@ -45,43 +55,41 @@
* First, platform-dependent definitions of size-specific integers.
*/
typedef signed char StgInt8;
typedef unsigned char StgWord8;
typedef signed short StgInt16;
typedef unsigned short StgWord16;
#if SIZEOF_INT == 4
typedef signed int StgInt32;
typedef unsigned int StgWord32;
#define FMT_Word32 "u"
#define FMT_HexWord32 "x"
#define FMT_Int32 "d"
#elif SIZEOF_LONG == 4
typedef signed long StgInt32;
typedef unsigned long StgWord32;
#define FMT_Word32 "lu"
#define FMT_HexWord32 "lx"
#define FMT_Int32 "ld"
#else
#error GHC untested on this architecture: sizeof(int) != 4
#endif
typedef int8_t StgInt8;
typedef uint8_t StgWord8;
#if SIZEOF_LONG == 8
typedef signed long StgInt64;
typedef unsigned long StgWord64;
#define FMT_Word64 "lu"
#define FMT_HexWord64 "lx"
#define FMT_Int64 "ld"
#elif SIZEOF_LONG_LONG == 8
typedef signed long long int StgInt64;
typedef unsigned long long int StgWord64;
#define FMT_Word64 "llu"
#define FMT_HexWord64 "llx"
#define FMT_Int64 "lld"
#else
#error cannot find a way to define StgInt64
#endif
#define STG_INT8_MIN INT8_MIN
#define STG_INT8_MAX INT8_MAX
#define STG_WORD8_MAX UINT8_MAX
typedef int16_t StgInt16;
typedef uint16_t StgWord16;
#define STG_INT16_MIN INT16_MIN
#define STG_INT16_MAX INT16_MAX
#define STG_WORD16_MAX UINT16_MAX
typedef int32_t StgInt32;
typedef uint32_t StgWord32;
#define STG_INT32_MIN INT32_MIN
#define STG_INT32_MAX INT32_MAX
#define STG_WORD32_MAX UINT32_MAX
#define FMT_Word32 PRIu32
#define FMT_HexWord32 PRIx32
#define FMT_Int32 PRId32
typedef int64_t StgInt64;
typedef uint64_t StgWord64;
#define STG_INT64_MIN INT64_MIN
#define STG_INT64_MAX INT64_MAX
#define STG_WORD64_MAX UINT64_MAX
#define FMT_Word64 PRIu64
#define FMT_HexWord64 PRIx64
#define FMT_Int64 PRId64
typedef struct { StgWord64 h; StgWord64 l; } StgWord128;
......@@ -90,31 +98,43 @@ typedef struct { StgWord128 h; StgWord128 l; } StgWord256;
typedef struct { StgWord256 h; StgWord256 l; } StgWord512;
/*
* Define the standard word size we'll use on this machine: make it
* big enough to hold a pointer.
* Stg{Int,Word} are defined such that they have the exact same size as a
* void pointer.
*/
#if SIZEOF_VOID_P == 8
typedef StgInt64 StgInt;
typedef StgWord64 StgWord;
typedef StgInt32 StgHalfInt;
typedef StgWord32 StgHalfWord;
#define FMT_Word FMT_Word64
#define FMT_HexWord FMT_HexWord64
#define FMT_Int FMT_Int64
#else
#if SIZEOF_VOID_P == 4
typedef StgInt32 StgInt;
typedef StgWord32 StgWord;
typedef StgInt16 StgHalfInt;
typedef StgWord16 StgHalfWord;
#define FMT_Word FMT_Word32
#define FMT_HexWord FMT_HexWord32
#define FMT_Int FMT_Int32
typedef int64_t StgInt;
typedef uint64_t StgWord;
typedef int32_t StgHalfInt;
typedef uint32_t StgHalfWord;
#define STG_INT_MIN INT64_MIN
#define STG_INT_MAX INT64_MAX
#define STG_WORD_MAX UINT64_MAX
#define FMT_Word FMT_Word64
#define FMT_HexWord FMT_HexWord64
#define FMT_Int FMT_Int64
#elif SIZEOF_VOID_P == 4
typedef int32_t StgInt;
typedef uint32_t StgWord;
typedef int16_t StgHalfInt;
typedef uint16_s StgHalfWord;
#define STG_INT_MIN INT32_MIN
#define STG_INT_MAX INT32_MAX
#define STG_WORD_MAX UINT32_MAX
#define FMT_Word FMT_Word32
#define FMT_HexWord FMT_HexWord32
#define FMT_Int FMT_Int32
#else
#error GHC untested on this architecture: sizeof(void *) != 4 or 8
#endif
#endif
#define W_MASK (sizeof(W_)-1)
......
......@@ -829,7 +829,7 @@ dumpCensus( Census *census )
}
#endif
fprintf(hp_file, "\t%" FMT_SizeT "\n", (W_)count * sizeof(W_));
fprintf(hp_file, "\t%" FMT_Word "\n", (W_)count * sizeof(W_));
}
printSample(rtsFalse, census->time);
......
......@@ -331,7 +331,7 @@ stat_endGC (Capability *cap, gc_thread *gct,
if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS) {
W_ faults = getPageFaults();
statsPrintf("%9" FMT_SizeT " %9" FMT_SizeT " %9" FMT_SizeT,
statsPrintf("%9" FMT_Word " %9" FMT_Word " %9" FMT_Word,
alloc*sizeof(W_), copied*sizeof(W_),
live*sizeof(W_));
statsPrintf(" %6.3f %6.3f %8.3f %8.3f %4" FMT_Word " %4" FMT_Word " (Gen: %2d)\n",
......@@ -560,7 +560,7 @@ stat_exit (void)
if (tot_elapsed == 0.0) tot_elapsed = 1;
if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
statsPrintf("%9" FMT_SizeT " %9.9s %9.9s", (W_)alloc*sizeof(W_), "", "");
statsPrintf("%9" FMT_Word " %9.9s %9.9s", (W_)alloc*sizeof(W_), "", "");
statsPrintf(" %6.3f %6.3f\n\n", 0.0, 0.0);
}
......@@ -836,7 +836,7 @@ statDescribeGens(void)
tot_slop += gen_slop;
}
debugBelch("----------------------------------------------------------\n");
debugBelch("%41s%8" FMT_SizeT " %8" FMT_SizeT "\n",
debugBelch("%41s%8" FMT_Word " %8" FMT_Word "\n",
"",tot_live*sizeof(W_),tot_slop*sizeof(W_));
debugBelch("----------------------------------------------------------\n");
debugBelch("\n");
......
......@@ -821,7 +821,7 @@ void findSlop(bdescr *bd)
for (; bd != NULL; bd = bd->link) {
slop = (bd->blocks * BLOCK_SIZE_W) - (bd->free - bd->start);
if (slop > (1024/sizeof(W_))) {
debugBelch("block at %p (bdescr %p) has %" FMT_SizeT "KB slop\n",
debugBelch("block at %p (bdescr %p) has %" FMT_Word "KB slop\n",
bd->start, bd, slop / (1024/sizeof(W_)));
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment