6e5f47da8fa36a86603f6bda913898de93c9aa3d
[ghc-hetmet.git] / ghc / includes / StgTypes.h
1 /* -----------------------------------------------------------------------------
2  * $Id: StgTypes.h,v 1.14 2000/11/07 13:30:40 simonmar Exp $
3  *
4  * (c) The GHC Team, 1998-2000
5  *
6  * Various C datatypes used in the run-time system.
7
8  * Specifically:
9
10         StgInt8,  16, 32, 64
11         StgWord8, 16, 32, 64
12         StgChar, StgFloat, StgDouble
13
14         ***** All the same size: *****
15         StgPtr                  Basic pointer type
16         StgWord                 Unit of heap allocation
17         StgInt                  Signed version of StgWord
18         StgAddr                 Generic address type
19         
20  * WARNING: Keep this file and HsFFI.h in synch!
21  *
22  * ---------------------------------------------------------------------------*/
23
24 #ifndef STGTYPES_H
25 #define STGTYPES_H
26
27 /*
28  * First, platform-dependent definitions of size-specific integers.
29  * Assume for now that the int type is 32 bits.
30  * NOTE: Synch the following definitions with MachDeps.h!
31  * ToDo: move these into a platform-dependent file.
32  */
33
34 typedef signed   char            StgInt8;
35 typedef unsigned char            StgWord8;
36
37 typedef signed   short           StgInt16;
38 typedef unsigned short           StgWord16;
39
40
41 #if SIZEOF_UNSIGNED_INT == 4
42 typedef signed   int             StgInt32;
43 typedef unsigned int             StgWord32;
44 #else
45 #error GHC untested on this architecture: sizeof(unsigned int) != 4
46 #endif
47
48 /* This #define controls whether we need to support long longs on a particular
49  * platform. 
50  *
51  * ToDo: find a proper home for (derived) configuration information like this.
52  */
53 #if HAVE_LONG_LONG && SIZEOF_VOID_P < 8
54 #define SUPPORT_LONG_LONGS
55 #endif
56
57 #ifdef SUPPORT_LONG_LONGS
58 /* assume long long is 64 bits */
59 typedef unsigned long long int StgWord64;
60 typedef signed long long int   StgInt64;
61 #elif SIZEOF_LONG == 8
62 typedef signed   long          StgInt64;
63 typedef unsigned long          StgWord64;
64 #else
65 #error GHC untested on this architecture: sizeof(void *) < 8 and no long longs.
66 #endif
67
68
69 /*
70  * Define the standard word size we'll use on this machine: make it
71  * big enough to hold a pointer.
72  */
73
74 #if SIZEOF_VOID_P == 8
75 typedef StgInt64           StgInt;
76 typedef StgWord64          StgWord;
77 #else
78 #if SIZEOF_VOID_P == 4
79 typedef StgInt32           StgInt; 
80 typedef StgWord32          StgWord;
81 #else
82 #error GHC untested on this architecture: sizeof(void *) != 4 or 8
83 #endif
84 #endif
85
86 typedef void*              StgAddr;
87
88 /*
89  * Other commonly-used STG datatypes.
90  */
91
92 typedef StgWord32          StgChar;
93 typedef int                StgBool;
94
95 typedef float              StgFloat;
96 typedef double             StgDouble;
97                            
98 typedef void               StgVoid;
99                            
100 typedef struct StgClosure_* StgClosurePtr;
101 typedef StgWord*           StgPtr;           /* pointer into closure       */
102 typedef StgWord            StgOffset;        /* byte offset within closure */
103                            
104 typedef struct StgTSO_*    StgTSOPtr;
105
106 typedef void*              StgForeignPtr;
107
108 typedef StgInt             StgStackOffset;   /* offset in words! */
109
110 typedef StgWord*           StgStackPtr;
111
112 typedef StgWord8           StgCode;         /* close enough */
113 typedef StgCode*           StgCodePtr;  
114
115 typedef StgPtr*            StgArray;        /* the goods of an Array# */
116 typedef char*              StgByteArray;    /* the goods of a ByteArray# */
117
118 typedef StgInt64               LI_;
119 typedef StgWord64              LW_;
120
121 /* Stable Pointers:  A stable pointer is represented as an index into
122  * the stable pointer table in the low 24 bits with a weight in the
123  * upper 8 bits.
124  * SUP: StgStablePtr used to be a synonym for StgWord, but stable pointers
125  * are guaranteed to be void* on the C-side, so we have to do some occasional
126  * casting. Size is not a matter, because StgWord is always the same size as
127  * a void*.
128  */
129 typedef void*              StgStablePtr;
130
131 #define STABLEPTR_WEIGHT_MASK   ((StgWord)0xff << ((sizeof(StgWord)-1) * BITS_PER_BYTE))
132 #define STABLEPTR_WEIGHT_SHIFT  (BITS_IN(StgWord) - 8)
133
134 /*
135   Types for the generated C functions
136   take no arguments
137   return a pointer to the next function to be called
138   use: Ptr to Fun that returns a Ptr to Fun which returns Ptr to void
139
140   Note: Neither StgFunPtr not StgFun is quite right (that is, 
141   StgFunPtr != StgFun*).  So, the functions we define all have type
142   StgFun but we always have to cast them to StgFunPtr when we assign
143   them to something.
144   The only way round this would be to write a recursive type but
145   C only allows that if you're defining a struct or union.
146 */
147
148 typedef void  *(*(*StgFunPtr)(void))(void);
149 typedef StgFunPtr StgFun(void);
150
151 typedef union {
152     StgWord        w;
153     StgAddr        a;
154     StgChar        c;
155     StgInt8        i8;
156     StgFloat       f;
157     StgInt         i;
158     StgPtr         p;
159     StgClosurePtr  cl;
160     StgStackOffset offset;      /* unused? */
161     StgByteArray   b;
162     StgTSOPtr      t;
163 } StgUnion;
164
165 /*
166  * Shorthand forms
167  */
168
169 typedef StgChar         C_;
170 typedef StgWord         W_;
171 typedef StgWord*        P_;
172 typedef P_*             PP_;
173 typedef StgInt          I_;
174 typedef StgAddr         A_;
175 typedef const StgWord*  D_;
176 typedef StgFunPtr       F_;
177 typedef StgByteArray    B_;
178 typedef StgClosurePtr   L_;
179
180 /*
181  * We often want to know the size of something in units of an
182  * StgWord... (rounded up, of course!)
183  */
184
185 #define sizeofW(t) ((sizeof(t)+sizeof(W_)-1)/sizeof(W_))
186
187 /* 
188  * It's nice to be able to grep for casts
189  */
190
191 #define stgCast(ty,e) ((ty)(e))
192
193 #endif /* STGTYPES_H */