be49b326f27afedbf1205026af5d4ae028830297
[ghc.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://ghc.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 /* We include windows.h very early, as on Win64 the CONTEXT type has
21 fields "R8", "R9" and "R10", which goes bad if we've already
22 #define'd those names for our own purposes (in stg/Regs.h) */
23 #if defined(HAVE_WINDOWS_H)
24 #include <windows.h>
25 #endif
26
27 #ifndef IN_STG_CODE
28 #define IN_STG_CODE 0
29 #endif
30 #include "Stg.h"
31
32 #include "HsFFI.h"
33 #include "RtsAPI.h"
34
35 // Turn off inlining when debugging - it obfuscates things
36 #ifdef DEBUG
37 # undef STATIC_INLINE
38 # define STATIC_INLINE static
39 #endif
40
41 #include "rts/Types.h"
42 #include "rts/Time.h"
43
44 #if __GNUC__ >= 3
45 #define ATTRIBUTE_ALIGNED(n) __attribute__((aligned(n)))
46 #else
47 #define ATTRIBUTE_ALIGNED(n) /*nothing*/
48 #endif
49
50 // Symbols that are extern, but private to the RTS, are declared
51 // with visibility "hidden" to hide them outside the RTS shared
52 // library.
53 #if defined(HAS_VISIBILITY_HIDDEN)
54 #define RTS_PRIVATE GNUC3_ATTRIBUTE(visibility("hidden"))
55 #else
56 #define RTS_PRIVATE /* disabled: RTS_PRIVATE */
57 #endif
58
59 #if __GNUC__ >= 4
60 #define RTS_UNLIKELY(p) __builtin_expect((p),0)
61 #else
62 #define RTS_UNLIKELY(p) p
63 #endif
64
65 /* __builtin_unreachable is supported since GNU C 4.5 */
66 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
67 #define RTS_UNREACHABLE __builtin_unreachable()
68 #else
69 #define RTS_UNREACHABLE abort()
70 #endif
71
72 /* Fix for mingw stat problem (done here so it's early enough) */
73 #ifdef mingw32_HOST_OS
74 #define __MSVCRT__ 1
75 #endif
76
77 /* Needed to get the macro version of errno on some OSs, and also to
78 get prototypes for the _r versions of C library functions. */
79 #ifndef _REENTRANT
80 #define _REENTRANT 1
81 #endif
82
83 /*
84 * We often want to know the size of something in units of an
85 * StgWord... (rounded up, of course!)
86 */
87 #define ROUNDUP_BYTES_TO_WDS(n) (((n) + sizeof(W_) - 1) / sizeof(W_))
88
89 #define sizeofW(t) ROUNDUP_BYTES_TO_WDS(sizeof(t))
90
91 /* -----------------------------------------------------------------------------
92 Assertions and Debuggery
93
94 CHECK(p) evaluates p and terminates with an error if p is false
95 ASSERT(p) like CHECK(p) if DEBUG is on, otherwise a no-op
96 -------------------------------------------------------------------------- */
97
98 void _assertFail(const char *filename, unsigned int linenum)
99 GNUC3_ATTRIBUTE(__noreturn__);
100
101 #define CHECK(predicate) \
102 if (predicate) \
103 /*null*/; \
104 else \
105 _assertFail(__FILE__, __LINE__)
106
107 #define CHECKM(predicate, msg, ...) \
108 if (predicate) \
109 /*null*/; \
110 else \
111 barf(msg, ##__VA_ARGS__)
112
113 #ifndef DEBUG
114 #define ASSERT(predicate) /* nothing */
115 #define ASSERTM(predicate,msg,...) /* nothing */
116 #else
117 #define ASSERT(predicate) CHECK(predicate)
118 #define ASSERTM(predicate,msg,...) CHECKM(predicate,msg,##__VA_ARGS__)
119 #endif /* DEBUG */
120
121 /*
122 * Use this on the RHS of macros which expand to nothing
123 * to make sure that the macro can be used in a context which
124 * demands a non-empty statement.
125 */
126
127 #define doNothing() do { } while (0)
128
129 #ifdef DEBUG
130 #define USED_IF_DEBUG
131 #define USED_IF_NOT_DEBUG STG_UNUSED
132 #else
133 #define USED_IF_DEBUG STG_UNUSED
134 #define USED_IF_NOT_DEBUG
135 #endif
136
137 #ifdef THREADED_RTS
138 #define USED_IF_THREADS
139 #define USED_IF_NOT_THREADS STG_UNUSED
140 #else
141 #define USED_IF_THREADS STG_UNUSED
142 #define USED_IF_NOT_THREADS
143 #endif
144
145 #define FMT_SizeT "zu"
146 #define FMT_HexSizeT "zx"
147
148 /* -----------------------------------------------------------------------------
149 Include everything STG-ish
150 -------------------------------------------------------------------------- */
151
152 /* System headers: stdlib.h is needed so that we can use NULL. It must
153 * come after MachRegs.h, because stdlib.h might define some inline
154 * functions which may only be defined after register variables have
155 * been declared.
156 */
157 #include <stdlib.h>
158
159 #include "rts/Config.h"
160
161 /* Global constraints */
162 #include "rts/Constants.h"
163
164 /* Profiling information */
165 #include "rts/prof/CCS.h"
166 #include "rts/prof/LDV.h"
167
168 /* Parallel information */
169 #include "rts/OSThreads.h"
170 #include "rts/SpinLock.h"
171
172 #include "rts/Messages.h"
173 #include "rts/Threads.h"
174
175 /* Storage format definitions */
176 #include "rts/storage/FunTypes.h"
177 #include "rts/storage/InfoTables.h"
178 #include "rts/storage/Closures.h"
179 #include "rts/storage/ClosureTypes.h"
180 #include "rts/storage/TSO.h"
181 #include "stg/MiscClosures.h" /* InfoTables, closures etc. defined in the RTS */
182 #include "rts/storage/Block.h"
183 #include "rts/storage/ClosureMacros.h"
184 #include "rts/storage/MBlock.h"
185 #include "rts/storage/GC.h"
186
187 /* Other RTS external APIs */
188 #include "rts/Parallel.h"
189 #include "rts/Signals.h"
190 #include "rts/BlockSignals.h"
191 #include "rts/Hpc.h"
192 #include "rts/Flags.h"
193 #include "rts/Adjustor.h"
194 #include "rts/FileLock.h"
195 #include "rts/GetTime.h"
196 #include "rts/Globals.h"
197 #include "rts/IOManager.h"
198 #include "rts/Linker.h"
199 #include "rts/Ticky.h"
200 #include "rts/Timer.h"
201 #include "rts/Stable.h"
202 #include "rts/TTY.h"
203 #include "rts/Utils.h"
204 #include "rts/PrimFloat.h"
205 #include "rts/Main.h"
206 #include "rts/StaticPtrTable.h"
207 #include "rts/Libdw.h"
208 #include "rts/LibdwPool.h"
209
210 /* Misc stuff without a home */
211 DLL_IMPORT_RTS extern char **prog_argv; /* so we can get at these from Haskell */
212 DLL_IMPORT_RTS extern int prog_argc;
213 DLL_IMPORT_RTS extern char *prog_name;
214
215 #ifdef mingw32_HOST_OS
216 // We need these two from Haskell too
217 void getWin32ProgArgv(int *argc, wchar_t **argv[]);
218 void setWin32ProgArgv(int argc, wchar_t *argv[]);
219 #endif
220
221 void reportStackOverflow(StgTSO* tso);
222 void reportHeapOverflow(void);
223
224 void stg_exit(int n) GNU_ATTRIBUTE(__noreturn__);
225
226 #ifndef mingw32_HOST_OS
227 int stg_sig_install (int, int, void *);
228 #endif
229
230 /* -----------------------------------------------------------------------------
231 Ways
232 -------------------------------------------------------------------------- */
233
234 // Returns non-zero if the RTS is a profiling version
235 int rts_isProfiled(void);
236
237 // Returns non-zero if the RTS is a dynamically-linked version
238 int rts_isDynamic(void);
239
240 /* -----------------------------------------------------------------------------
241 RTS Exit codes
242 -------------------------------------------------------------------------- */
243
244 /* 255 is allegedly used by dynamic linkers to report linking failure */
245 #define EXIT_INTERNAL_ERROR 254
246 #define EXIT_DEADLOCK 253
247 #define EXIT_INTERRUPTED 252
248 #define EXIT_HEAPOVERFLOW 251
249 #define EXIT_KILLED 250
250
251 /* -----------------------------------------------------------------------------
252 Miscellaneous garbage
253 -------------------------------------------------------------------------- */
254
255 #ifdef DEBUG
256 #define TICK_VAR(arity) \
257 extern StgInt SLOW_CALLS_##arity; \
258 extern StgInt RIGHT_ARITY_##arity; \
259 extern StgInt TAGGED_PTR_##arity;
260
261 extern StgInt TOTAL_CALLS;
262
263 TICK_VAR(1)
264 TICK_VAR(2)
265 #endif
266
267 /* -----------------------------------------------------------------------------
268 Assertions and Debuggery
269 -------------------------------------------------------------------------- */
270
271 #define IF_RTSFLAGS(c,s) if (RtsFlags.c) { s; } doNothing()
272
273 #ifdef DEBUG
274 #if IN_STG_CODE
275 #define IF_DEBUG(c,s) if (RtsFlags[0].DebugFlags.c) { s; } doNothing()
276 #else
277 #define IF_DEBUG(c,s) if (RtsFlags.DebugFlags.c) { s; } doNothing()
278 #endif
279 #else
280 #define IF_DEBUG(c,s) doNothing()
281 #endif
282
283 #ifdef DEBUG
284 #define DEBUG_ONLY(s) s
285 #else
286 #define DEBUG_ONLY(s) doNothing()
287 #endif
288
289 #ifdef DEBUG
290 #define DEBUG_IS_ON 1
291 #else
292 #define DEBUG_IS_ON 0
293 #endif
294
295 /* -----------------------------------------------------------------------------
296 Useful macros and inline functions
297 -------------------------------------------------------------------------- */
298
299 #if defined(__GNUC__)
300 #define SUPPORTS_TYPEOF
301 #endif
302
303 #if defined(SUPPORTS_TYPEOF)
304 #define stg_min(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _a : _b; })
305 #define stg_max(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _b : _a; })
306 #else
307 #define stg_min(a,b) ((a) <= (b) ? (a) : (b))
308 #define stg_max(a,b) ((a) <= (b) ? (b) : (a))
309 #endif
310
311 /* -------------------------------------------------------------------------- */
312
313 #ifdef __cplusplus
314 }
315 #endif
316
317 #endif /* RTS_H */