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