add ROUNDUP_BYTES_TO_WDS
[ghc-hetmet.git] / includes / Rts.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2004
4  *
5  * Top-level include file for the RTS itself
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #ifndef RTS_H
10 #define RTS_H
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 #ifndef IN_STG_CODE
17 #define IN_STG_CODE 0
18 #endif
19 #include "Stg.h"
20
21 // ToDo: move RtsExternal stuff elsewhere
22 #include "RtsExternal.h"
23
24 // Turn off inlining when debugging - it obfuscates things
25 #ifdef DEBUG
26 # undef  STATIC_INLINE
27 # define STATIC_INLINE static
28 #endif
29
30 #include "RtsTypes.h"
31
32 #if __GNUC__ >= 3
33 /* Assume that a flexible array member at the end of a struct
34  * can be defined thus: T arr[]; */
35 #define FLEXIBLE_ARRAY
36 #else
37 /* Assume that it must be defined thus: T arr[0]; */
38 #define FLEXIBLE_ARRAY 0
39 #endif
40
41 /* Fix for mingw stat problem (done here so it's early enough) */
42 #ifdef mingw32_HOST_OS
43 #define __MSVCRT__ 1
44 #endif
45
46 /* Needed to get the macro version of errno on some OSs, and also to
47    get prototypes for the _r versions of C library functions. */
48 #define _REENTRANT 1
49
50 /*
51  * We often want to know the size of something in units of an
52  * StgWord... (rounded up, of course!)
53  */
54 #define ROUNDUP_BYTES_TO_WDS(n) (((n) + sizeof(W_) - 1) / sizeof(W_))
55
56 #define sizeofW(t) ROUNDUP_BYTES_TO_WDS(sizeof(t))
57
58 /* 
59  * It's nice to be able to grep for casts
60  */
61 #define stgCast(ty,e) ((ty)(e))
62
63 /* -----------------------------------------------------------------------------
64    Assertions and Debuggery
65    -------------------------------------------------------------------------- */
66
67 #ifndef DEBUG
68 #define ASSERT(predicate) /* nothing */
69 #else
70
71 extern void _assertFail (const char *, unsigned int);
72
73 #define ASSERT(predicate)                       \
74         if (predicate)                          \
75             /*null*/;                           \
76         else                                    \
77             _assertFail(__FILE__, __LINE__)
78 #endif /* DEBUG */
79
80 /* 
81  * Use this on the RHS of macros which expand to nothing
82  * to make sure that the macro can be used in a context which
83  * demands a non-empty statement.
84  */
85
86 #define doNothing() do { } while (0)
87
88 #ifdef DEBUG
89 #define USED_IF_DEBUG
90 #define USED_IF_NOT_DEBUG STG_UNUSED
91 #else
92 #define USED_IF_DEBUG STG_UNUSED
93 #define USED_IF_NOT_DEBUG
94 #endif
95
96 #ifdef THREADED_RTS
97 #define USED_IF_THREADS
98 #define USED_IF_NOT_THREADS STG_UNUSED
99 #else
100 #define USED_IF_THREADS STG_UNUSED
101 #define USED_IF_NOT_THREADS
102 #endif
103
104 /*
105  * Getting printf formats right for platform-dependent typedefs
106  */
107 #if SIZEOF_LONG == 8
108 #define FMT_Word64 "lu"
109 #define FMT_Int64  "ld"
110 #else
111 #define FMT_Word64 "llu"
112 #define FMT_Int64  "lld"
113 #endif
114
115 /*
116  * Macros for untagging and retagging closure pointers
117  * For more information look at the comments in Cmm.h
118  */
119
120 static inline StgWord
121 GET_CLOSURE_TAG(StgClosure * p)
122 {
123     return (StgWord)p & TAG_MASK;
124 }
125
126 static inline StgClosure *
127 UNTAG_CLOSURE(StgClosure * p)
128 {
129     return (StgClosure*)((StgWord)p & ~TAG_MASK);
130 }
131
132 static inline StgClosure *
133 TAG_CLOSURE(StgWord tag,StgClosure * p)
134 {
135     return (StgClosure*)((StgWord)p | tag);
136 }
137
138 /* -----------------------------------------------------------------------------
139    Include everything STG-ish
140    -------------------------------------------------------------------------- */
141
142 /* System headers: stdlib.h is eeded so that we can use NULL.  It must
143  * come after MachRegs.h, because stdlib.h might define some inline
144  * functions which may only be defined after register variables have
145  * been declared.
146  */
147 #include <stdlib.h>
148
149 /* Global constaints */
150 #include "Constants.h"
151
152 /* Profiling information */
153 #include "StgProf.h"
154 #include "StgLdvProf.h"
155
156 /* Storage format definitions */
157 #include "StgFun.h"
158 #include "Closures.h"
159 #include "Liveness.h"
160 #include "ClosureTypes.h"
161 #include "InfoTables.h"
162 #include "TSO.h"
163
164 /* Info tables, closures & code fragments defined in the RTS */
165 #include "StgMiscClosures.h"
166
167 /* Simulated-parallel information */
168 #include "GranSim.h"
169
170 /* Parallel information */
171 #include "Parallel.h"
172 #include "OSThreads.h"
173 #include "SMPClosureOps.h"
174 #include "SpinLock.h"
175
176 /* GNU mp library */
177 #if defined(HAVE_FRAMEWORK_GMP)
178 #include <GMP/gmp.h>
179 #else
180 #include "gmp.h"
181 #endif
182
183 /* Macros for STG/C code */
184 #include "Block.h"
185 #include "ClosureMacros.h"
186
187   /* Ticky-ticky counters */
188 #include "TickyCounters.h"
189
190 /* Runtime-system hooks */
191 #include "Hooks.h"
192 #include "RtsMessages.h"
193
194 /* for StablePtr/getStablePtr/deRefStablePtr */
195 #include "Storage.h"
196 #include "Stable.h"
197
198 #include "ieee-flpt.h"
199
200 #include "Signals.h"
201
202 /* Misc stuff without a home */
203 DLL_IMPORT_RTS extern char **prog_argv; /* so we can get at these from Haskell */
204 DLL_IMPORT_RTS extern int    prog_argc;
205 DLL_IMPORT_RTS extern char  *prog_name;
206
207 extern void stackOverflow(void);
208
209 extern void      __decodeDouble (MP_INT *man, I_ *_exp, StgDouble dbl);
210 extern void      __decodeFloat  (MP_INT *man, I_ *_exp, StgFloat flt);
211 extern void      __decodeDouble_2Int (I_ *man_sign, W_ *man_high, W_ *man_low, I_ *exp, StgDouble dbl);
212 extern void      __decodeFloat_Int (I_ *man, I_ *exp, StgFloat flt);
213
214 #if defined(WANT_DOTNET_SUPPORT)
215 #include "DNInvoke.h"
216 #endif
217
218 /* Initialising the whole adjustor thunk machinery. */
219 extern void initAdjustor(void);
220
221 extern void stg_exit(int n) GNU_ATTRIBUTE(__noreturn__);
222
223 /* -----------------------------------------------------------------------------
224    RTS Exit codes
225    -------------------------------------------------------------------------- */
226
227 /* 255 is allegedly used by dynamic linkers to report linking failure */
228 #define EXIT_INTERNAL_ERROR 254
229 #define EXIT_DEADLOCK       253
230 #define EXIT_INTERRUPTED    252
231 #define EXIT_HEAPOVERFLOW   251
232 #define EXIT_KILLED         250
233
234 /* -----------------------------------------------------------------------------
235    Miscellaneous garbage
236    -------------------------------------------------------------------------- */
237
238 /* declarations for runtime flags/values */
239 #define MAX_RTS_ARGS 32
240
241 #ifdef DEBUG
242 #define TICK_VAR(arity) \
243   extern StgInt SLOW_CALLS_##arity; \
244   extern StgInt RIGHT_ARITY_##arity; \
245   extern StgInt TAGGED_PTR_##arity;
246
247 #define TICK_VAR_INI(arity) \
248   StgInt SLOW_CALLS_##arity = 1; \
249   StgInt RIGHT_ARITY_##arity = 1; \
250   StgInt TAGGED_PTR_##arity = 0;
251
252 extern StgInt TOTAL_CALLS;
253
254 TICK_VAR(1)
255 TICK_VAR(2)
256 #endif
257
258 /* -----------------------------------------------------------------------------
259    Assertions and Debuggery
260    -------------------------------------------------------------------------- */
261
262 #define IF_RTSFLAGS(c,s)  if (RtsFlags.c) { s; }
263
264 /* -----------------------------------------------------------------------------
265    Assertions and Debuggery
266    -------------------------------------------------------------------------- */
267
268 #ifdef DEBUG
269 #if IN_STG_CODE
270 #define IF_DEBUG(c,s)  if (RtsFlags[0].DebugFlags.c) { s; }
271 #else
272 #define IF_DEBUG(c,s)  if (RtsFlags.DebugFlags.c) { s; }
273 #endif
274 #else
275 #define IF_DEBUG(c,s)  doNothing()
276 #endif
277
278 #ifdef DEBUG
279 #define DEBUG_ONLY(s) s
280 #else
281 #define DEBUG_ONLY(s) doNothing()
282 #endif
283
284 #if defined(GRAN) && defined(DEBUG)
285 #define IF_GRAN_DEBUG(c,s)  if (RtsFlags.GranFlags.Debug.c) { s; }
286 #else
287 #define IF_GRAN_DEBUG(c,s)  doNothing()
288 #endif
289
290 #if defined(PAR) && defined(DEBUG)
291 #define IF_PAR_DEBUG(c,s)  if (RtsFlags.ParFlags.Debug.c) { s; }
292 #else
293 #define IF_PAR_DEBUG(c,s)  doNothing()
294 #endif
295
296 /* -----------------------------------------------------------------------------
297    Useful macros and inline functions
298    -------------------------------------------------------------------------- */
299
300 #if defined(__GNUC__)
301 #define SUPPORTS_TYPEOF
302 #endif
303
304 #if defined(SUPPORTS_TYPEOF)
305 #define stg_min(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _a : _b; })
306 #define stg_max(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _b : _a; })
307 #else
308 #define stg_min(a,b) ((a) <= (b) ? (a) : (b))
309 #define stg_max(a,b) ((a) <= (b) ? (b) : (a))
310 #endif
311
312 /* -------------------------------------------------------------------------- */
313
314 #ifdef __cplusplus
315 }
316 #endif
317
318
319 /* krc: I put this here because I don't think
320    it needs to be visible externally.
321    It used to be in StgTicky.h, but I got rid
322    of that. */
323
324 /* -----------------------------------------------------------------------------
325    The StgEntCounter type - needed regardless of TICKY_TICKY
326    -------------------------------------------------------------------------- */
327
328 typedef struct _StgEntCounter {
329   /* Using StgWord for everything, becuase both the C and asm code
330      generators make trouble if you try to pack things tighter */
331     StgWord     registeredp;    /* 0 == no, 1 == yes */
332     StgInt      arity;          /* arity (static info) */
333     StgInt      stk_args;       /* # of args off stack */
334                                 /* (rest of args are in registers) */
335     char        *str;           /* name of the thing */
336     char        *arg_kinds;     /* info about the args types */
337     StgInt      entry_count;    /* Trips to fast entry code */
338     StgInt      allocs;         /* number of allocations by this fun */
339     struct _StgEntCounter *link;/* link to chain them all together */
340 } StgEntCounter;
341
342
343 #endif /* RTS_H */