[project @ 2000-08-07 23:37:19 by qrczak]
[ghc-hetmet.git] / ghc / includes / StgTypes.h
1 /* -----------------------------------------------------------------------------
2  * $Id: StgTypes.h,v 1.13 2000/08/07 23:37:23 qrczak Exp $
3  *
4  * (c) The GHC Team, 1998-1999
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  * If a double fits in an StgWord, don't bother using floats.
96  */
97
98 #if SIZEOF_DOUBLE == SIZEOF_VOID_P
99 typedef double             StgFloat;
100 typedef double             StgDouble;
101 #define FLOATS_AS_DOUBLES  1
102 #else
103 typedef float              StgFloat;
104 typedef double             StgDouble;
105 #endif
106                            
107 typedef void               StgVoid;
108                            
109 typedef struct StgClosure_* StgClosurePtr;
110 typedef StgWord*           StgPtr;           /* pointer into closure       */
111 typedef StgWord            StgOffset;        /* byte offset within closure */
112                            
113 typedef struct StgTSO_*    StgTSOPtr;
114
115 typedef void*              StgForeignPtr;
116
117 typedef StgInt             StgStackOffset;   /* offset in words! */
118
119 typedef StgWord*           StgStackPtr;
120
121 typedef StgWord8           StgCode;         /* close enough */
122 typedef StgCode*           StgCodePtr;  
123
124 typedef StgPtr*            StgArray;        /* the goods of an Array# */
125 typedef char*              StgByteArray;    /* the goods of a ByteArray# */
126
127 typedef StgInt64               LI_;
128 typedef StgWord64              LW_;
129
130 /* Stable Pointers:  A stable pointer is represented as an index into
131  * the stable pointer table in the low 24 bits with a weight in the
132  * upper 8 bits.
133  * SUP: StgStablePtr used to be a synonym for StgWord, but stable pointers
134  * are guaranteed to be void* on the C-side, so we have to do some occasional
135  * casting. Size is not a matter, because StgWord is always the same size as
136  * a void*.
137  */
138 typedef void*              StgStablePtr;
139
140 #define STABLEPTR_WEIGHT_MASK   ((StgWord)0xff << ((sizeof(StgWord)-1) * BITS_PER_BYTE))
141 #define STABLEPTR_WEIGHT_SHIFT  (BITS_IN(StgWord) - 8)
142
143 /*
144   Types for the generated C functions
145   take no arguments
146   return a pointer to the next function to be called
147   use: Ptr to Fun that returns a Ptr to Fun which returns Ptr to void
148
149   Note: Neither StgFunPtr not StgFun is quite right (that is, 
150   StgFunPtr != StgFun*).  So, the functions we define all have type
151   StgFun but we always have to cast them to StgFunPtr when we assign
152   them to something.
153   The only way round this would be to write a recursive type but
154   C only allows that if you're defining a struct or union.
155 */
156
157 typedef void  *(*(*StgFunPtr)(void))(void);
158 typedef StgFunPtr StgFun(void);
159
160 typedef union {
161     StgWord        w;
162     StgAddr        a;
163     StgChar        c;
164     StgInt8        i8;
165     StgFloat       f;
166     StgInt         i;
167     StgPtr         p;
168     StgClosurePtr  cl;
169     StgStackOffset offset;      /* unused? */
170     StgByteArray   b;
171     StgTSOPtr      t;
172 } StgUnion;
173
174 /*
175  * Shorthand forms
176  */
177
178 typedef StgChar         C_;
179 typedef StgWord         W_;
180 typedef StgWord*        P_;
181 typedef P_*             PP_;
182 typedef StgInt          I_;
183 typedef StgAddr         A_;
184 typedef const StgWord*  D_;
185 typedef StgFunPtr       F_;
186 typedef StgByteArray    B_;
187 typedef StgClosurePtr   L_;
188
189 /*
190  * We often want to know the size of something in units of an
191  * StgWord... (rounded up, of course!)
192  */
193
194 #define sizeofW(t) ((sizeof(t)+sizeof(W_)-1)/sizeof(W_))
195
196 /* 
197  * It's nice to be able to grep for casts
198  */
199
200 #define stgCast(ty,e) ((ty)(e))
201
202 #endif /* STGTYPES_H */