Fix decomposition error on Windows
[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 #if defined(__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 #if !defined(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 #if defined(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 #if defined(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 #if !defined(_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 #if !defined(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 #if defined(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 #if defined(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/Profiling.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 void reportStackOverflow(StgTSO* tso);
216 void reportHeapOverflow(void);
217
218 void stg_exit(int n) GNU_ATTRIBUTE(__noreturn__);
219
220 #if !defined(mingw32_HOST_OS)
221 int stg_sig_install (int, int, void *);
222 #endif
223
224 /* -----------------------------------------------------------------------------
225 Ways
226 -------------------------------------------------------------------------- */
227
228 // Returns non-zero if the RTS is a profiling version
229 int rts_isProfiled(void);
230
231 // Returns non-zero if the RTS is a dynamically-linked version
232 int rts_isDynamic(void);
233
234 /* -----------------------------------------------------------------------------
235 RTS Exit codes
236 -------------------------------------------------------------------------- */
237
238 /* 255 is allegedly used by dynamic linkers to report linking failure */
239 #define EXIT_INTERNAL_ERROR 254
240 #define EXIT_DEADLOCK 253
241 #define EXIT_INTERRUPTED 252
242 #define EXIT_HEAPOVERFLOW 251
243 #define EXIT_KILLED 250
244
245 /* -----------------------------------------------------------------------------
246 Miscellaneous garbage
247 -------------------------------------------------------------------------- */
248
249 #if defined(DEBUG)
250 #define TICK_VAR(arity) \
251 extern StgInt SLOW_CALLS_##arity; \
252 extern StgInt RIGHT_ARITY_##arity; \
253 extern StgInt TAGGED_PTR_##arity;
254
255 extern StgInt TOTAL_CALLS;
256
257 TICK_VAR(1)
258 TICK_VAR(2)
259 #endif
260
261 /* -----------------------------------------------------------------------------
262 Assertions and Debuggery
263 -------------------------------------------------------------------------- */
264
265 #define IF_RTSFLAGS(c,s) if (RtsFlags.c) { s; } doNothing()
266
267 #if defined(DEBUG)
268 #if IN_STG_CODE
269 #define IF_DEBUG(c,s) if (RtsFlags[0].DebugFlags.c) { s; } doNothing()
270 #else
271 #define IF_DEBUG(c,s) if (RtsFlags.DebugFlags.c) { s; } doNothing()
272 #endif
273 #else
274 #define IF_DEBUG(c,s) doNothing()
275 #endif
276
277 #if defined(DEBUG)
278 #define DEBUG_ONLY(s) s
279 #else
280 #define DEBUG_ONLY(s) doNothing()
281 #endif
282
283 #if defined(DEBUG)
284 #define DEBUG_IS_ON 1
285 #else
286 #define DEBUG_IS_ON 0
287 #endif
288
289 /* -----------------------------------------------------------------------------
290 Useful macros and inline functions
291 -------------------------------------------------------------------------- */
292
293 #if defined(__GNUC__)
294 #define SUPPORTS_TYPEOF
295 #endif
296
297 #if defined(SUPPORTS_TYPEOF)
298 #define stg_min(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _a : _b; })
299 #define stg_max(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _b : _a; })
300 #else
301 #define stg_min(a,b) ((a) <= (b) ? (a) : (b))
302 #define stg_max(a,b) ((a) <= (b) ? (b) : (a))
303 #endif
304
305 /* -------------------------------------------------------------------------- */
306
307 #if defined(__cplusplus)
308 }
309 #endif