New implementation of BLACKHOLEs
[ghc-hetmet.git] / includes / Rts.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2009
4  *
5  * RTS external APIs.  This file declares everything that the GHC RTS
6  * exposes externally.
7  *
8  * To understand the structure of the RTS headers, see the wiki:
9  *   http://hackage.haskell.org/trac/ghc/wiki/Commentary/SourceTree/Includes
10  *
11  * ---------------------------------------------------------------------------*/
12
13 #ifndef RTS_H
14 #define RTS_H
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #ifndef IN_STG_CODE
21 #define IN_STG_CODE 0
22 #endif
23 #include "Stg.h"
24
25 #include "HsFFI.h"
26 #include "RtsAPI.h"
27
28 // Turn off inlining when debugging - it obfuscates things
29 #ifdef DEBUG
30 # undef  STATIC_INLINE
31 # define STATIC_INLINE static
32 #endif
33
34 #include "rts/Types.h"
35
36 #if __GNUC__ >= 3
37 /* Assume that a flexible array member at the end of a struct
38  * can be defined thus: T arr[]; */
39 #define FLEXIBLE_ARRAY
40 #else
41 /* Assume that it must be defined thus: T arr[0]; */
42 #define FLEXIBLE_ARRAY 0
43 #endif
44
45 #if __GNUC__ >= 3
46 #define ATTRIBUTE_ALIGNED(n) __attribute__((aligned(n)))
47 #else
48 #define ATTRIBUTE_ALIGNED(n) /*nothing*/
49 #endif
50
51 // Symbols that are extern, but private to the RTS, are declared
52 // with visibility "hidden" to hide them outside the RTS shared
53 // library.
54 #if !defined(mingw32_HOST_OS)
55 #define RTS_PRIVATE  GNUC3_ATTRIBUTE(visibility("hidden"))
56 #else
57 #define RTS_PRIVATE  /* disabled: RTS_PRIVATE */
58 #endif
59
60 #if __GNUC__ > 4
61 #define BEGIN_RTS_PRIVATE #pragma GCC visibility push(hidden)
62 #define END_RTS_PRIVATE   #pragma GCC visibility pop
63 #else
64 #define BEGIN_RTS_PRIVATE /* disabled: BEGIN_RTS_PRIVATE */
65 #define END_RTS_PRIVATE   /* disabled: END_RTS_PRIVATE */
66 #endif
67
68 #if __GNUC__ > 4
69 #define RTS_UNLIKELY(p) __builtin_expect((p),0)
70 #else
71 #define RTS_UNLIKELY(p) p
72 #endif
73
74 /* Fix for mingw stat problem (done here so it's early enough) */
75 #ifdef mingw32_HOST_OS
76 #define __MSVCRT__ 1
77 #endif
78
79 /* Needed to get the macro version of errno on some OSs, and also to
80    get prototypes for the _r versions of C library functions. */
81 #ifndef _REENTRANT
82 #define _REENTRANT 1
83 #endif
84
85 /*
86  * We often want to know the size of something in units of an
87  * StgWord... (rounded up, of course!)
88  */
89 #define ROUNDUP_BYTES_TO_WDS(n) (((n) + sizeof(W_) - 1) / sizeof(W_))
90
91 #define sizeofW(t) ROUNDUP_BYTES_TO_WDS(sizeof(t))
92
93 /* -----------------------------------------------------------------------------
94    Assertions and Debuggery
95
96    CHECK(p)   evaluates p and terminates with an error if p is false
97    ASSERT(p)  like CHECK(p) if DEBUG is on, otherwise a no-op
98    -------------------------------------------------------------------------- */
99
100 void _assertFail(const char *filename, unsigned int linenum)
101    GNUC3_ATTRIBUTE(__noreturn__);
102
103 #define CHECK(predicate)                        \
104         if (predicate)                          \
105             /*null*/;                           \
106         else                                    \
107             _assertFail(__FILE__, __LINE__)
108
109 #define CHECKM(predicate, msg, ...)             \
110         if (predicate)                          \
111             /*null*/;                           \
112         else                                    \
113             barf(msg, ##__VA_ARGS__)
114
115 #ifndef DEBUG
116 #define ASSERT(predicate) /* nothing */
117 #define ASSERTM(predicate,msg,...) /* nothing */
118 #else
119 #define ASSERT(predicate) CHECK(predicate)
120 #define ASSERTM(predicate,msg,...) CHECKM(predicate,msg,##__VA_ARGS__)
121 #endif /* DEBUG */
122
123 /* 
124  * Use this on the RHS of macros which expand to nothing
125  * to make sure that the macro can be used in a context which
126  * demands a non-empty statement.
127  */
128
129 #define doNothing() do { } while (0)
130
131 #ifdef DEBUG
132 #define USED_IF_DEBUG
133 #define USED_IF_NOT_DEBUG STG_UNUSED
134 #else
135 #define USED_IF_DEBUG STG_UNUSED
136 #define USED_IF_NOT_DEBUG
137 #endif
138
139 #ifdef THREADED_RTS
140 #define USED_IF_THREADS
141 #define USED_IF_NOT_THREADS STG_UNUSED
142 #else
143 #define USED_IF_THREADS STG_UNUSED
144 #define USED_IF_NOT_THREADS
145 #endif
146
147 /*
148  * Getting printf formats right for platform-dependent typedefs
149  */
150 #if SIZEOF_LONG == 8
151 #define FMT_Word64 "lu"
152 #define FMT_Int64  "ld"
153 #else
154 #if defined(mingw32_HOST_OS)
155 /* mingw doesn't understand %llu/%lld - it treats them as 32-bit
156    rather than 64-bit */
157 #define FMT_Word64 "I64u"
158 #define FMT_Int64  "I64d"
159 #else
160 #define FMT_Word64 "llu"
161 #define FMT_Int64  "lld"
162 #endif
163 #endif
164
165 /* -----------------------------------------------------------------------------
166    Include everything STG-ish
167    -------------------------------------------------------------------------- */
168
169 /* System headers: stdlib.h is eeded so that we can use NULL.  It must
170  * come after MachRegs.h, because stdlib.h might define some inline
171  * functions which may only be defined after register variables have
172  * been declared.
173  */
174 #include <stdlib.h>
175
176 #include "rts/Config.h"
177
178 /* Global constaints */
179 #include "rts/Constants.h"
180
181 /* Profiling information */
182 #include "rts/prof/CCS.h"
183 #include "rts/prof/LDV.h"
184
185 /* Parallel information */
186 #include "rts/OSThreads.h"
187 #include "rts/SpinLock.h"
188
189 #include "rts/Messages.h"
190
191 /* Storage format definitions */
192 #include "rts/storage/FunTypes.h"
193 #include "rts/storage/InfoTables.h"
194 #include "rts/storage/Closures.h"
195 #include "rts/storage/Liveness.h"
196 #include "rts/storage/ClosureTypes.h"
197 #include "rts/storage/TSO.h"
198 #include "stg/MiscClosures.h" /* InfoTables, closures etc. defined in the RTS */
199 #include "rts/storage/SMPClosureOps.h"
200 #include "rts/storage/Block.h"
201 #include "rts/storage/ClosureMacros.h"
202 #include "rts/storage/MBlock.h"
203 #include "rts/storage/GC.h"
204
205 /* Other RTS external APIs */
206 #include "rts/Parallel.h"
207 #include "rts/Hooks.h"
208 #include "rts/Signals.h"
209 #include "rts/BlockSignals.h"
210 #include "rts/Hpc.h"
211 #include "rts/Flags.h"
212 #include "rts/Adjustor.h"
213 #include "rts/FileLock.h"
214 #include "rts/Globals.h"
215 #include "rts/IOManager.h"
216 #include "rts/Linker.h"
217 #include "rts/Threads.h"
218 #include "rts/Ticky.h"
219 #include "rts/Timer.h"
220 #include "rts/Stable.h"
221 #include "rts/TTY.h"
222 #include "rts/Utils.h"
223 #include "rts/PrimFloat.h"
224
225 /* Misc stuff without a home */
226 DLL_IMPORT_RTS extern char **prog_argv; /* so we can get at these from Haskell */
227 DLL_IMPORT_RTS extern int    prog_argc;
228 DLL_IMPORT_RTS extern char  *prog_name;
229
230 void stackOverflow(void);
231
232 void stg_exit(int n) GNU_ATTRIBUTE(__noreturn__);
233
234 #ifndef mingw32_HOST_OS
235 int stg_sig_install (int, int, void *);
236 #endif
237
238 /* -----------------------------------------------------------------------------
239    RTS Exit codes
240    -------------------------------------------------------------------------- */
241
242 /* 255 is allegedly used by dynamic linkers to report linking failure */
243 #define EXIT_INTERNAL_ERROR 254
244 #define EXIT_DEADLOCK       253
245 #define EXIT_INTERRUPTED    252
246 #define EXIT_HEAPOVERFLOW   251
247 #define EXIT_KILLED         250
248
249 /* -----------------------------------------------------------------------------
250    Miscellaneous garbage
251    -------------------------------------------------------------------------- */
252
253 /* declarations for runtime flags/values */
254 #define MAX_RTS_ARGS 32
255
256 #ifdef DEBUG
257 #define TICK_VAR(arity) \
258   extern StgInt SLOW_CALLS_##arity; \
259   extern StgInt RIGHT_ARITY_##arity; \
260   extern StgInt TAGGED_PTR_##arity;
261
262 extern StgInt TOTAL_CALLS;
263
264 TICK_VAR(1)
265 TICK_VAR(2)
266 #endif
267
268 /* -----------------------------------------------------------------------------
269    Assertions and Debuggery
270    -------------------------------------------------------------------------- */
271
272 #define IF_RTSFLAGS(c,s)  if (RtsFlags.c) { s; }
273
274 #ifdef DEBUG
275 #if IN_STG_CODE
276 #define IF_DEBUG(c,s)  if (RtsFlags[0].DebugFlags.c) { s; }
277 #else
278 #define IF_DEBUG(c,s)  if (RtsFlags.DebugFlags.c) { s; }
279 #endif
280 #else
281 #define IF_DEBUG(c,s)  doNothing()
282 #endif
283
284 #ifdef DEBUG
285 #define DEBUG_ONLY(s) s
286 #else
287 #define DEBUG_ONLY(s) doNothing()
288 #endif
289
290 /* -----------------------------------------------------------------------------
291    Useful macros and inline functions
292    -------------------------------------------------------------------------- */
293
294 #if defined(__GNUC__)
295 #define SUPPORTS_TYPEOF
296 #endif
297
298 #if defined(SUPPORTS_TYPEOF)
299 #define stg_min(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _a : _b; })
300 #define stg_max(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _b : _a; })
301 #else
302 #define stg_min(a,b) ((a) <= (b) ? (a) : (b))
303 #define stg_max(a,b) ((a) <= (b) ? (b) : (a))
304 #endif
305
306 /* -------------------------------------------------------------------------- */
307
308 #ifdef __cplusplus
309 }
310 #endif
311
312 #endif /* RTS_H */