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