Fix build on OSX: Use the 'z' format specifier modifier when possible
[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://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 /* 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
43 #if __GNUC__ >= 3
44 /* Assume that a flexible array member at the end of a struct
45 * can be defined thus: T arr[]; */
46 #define FLEXIBLE_ARRAY
47 #else
48 /* Assume that it must be defined thus: T arr[0]; */
49 #define FLEXIBLE_ARRAY 0
50 #endif
51
52 #if __GNUC__ >= 3
53 #define ATTRIBUTE_ALIGNED(n) __attribute__((aligned(n)))
54 #else
55 #define ATTRIBUTE_ALIGNED(n) /*nothing*/
56 #endif
57
58 // Symbols that are extern, but private to the RTS, are declared
59 // with visibility "hidden" to hide them outside the RTS shared
60 // library.
61 #if defined(HAS_VISIBILITY_HIDDEN)
62 #define RTS_PRIVATE GNUC3_ATTRIBUTE(visibility("hidden"))
63 #else
64 #define RTS_PRIVATE /* disabled: RTS_PRIVATE */
65 #endif
66
67 #if __GNUC__ >= 4
68 #define RTS_UNLIKELY(p) __builtin_expect((p),0)
69 #else
70 #define RTS_UNLIKELY(p) p
71 #endif
72
73 /* Fix for mingw stat problem (done here so it's early enough) */
74 #ifdef mingw32_HOST_OS
75 #define __MSVCRT__ 1
76 #endif
77
78 /* Needed to get the macro version of errno on some OSs, and also to
79 get prototypes for the _r versions of C library functions. */
80 #ifndef _REENTRANT
81 #define _REENTRANT 1
82 #endif
83
84 /*
85 * We often want to know the size of something in units of an
86 * StgWord... (rounded up, of course!)
87 */
88 #define ROUNDUP_BYTES_TO_WDS(n) (((n) + sizeof(W_) - 1) / sizeof(W_))
89
90 #define sizeofW(t) ROUNDUP_BYTES_TO_WDS(sizeof(t))
91
92 /* -----------------------------------------------------------------------------
93 Assertions and Debuggery
94
95 CHECK(p) evaluates p and terminates with an error if p is false
96 ASSERT(p) like CHECK(p) if DEBUG is on, otherwise a no-op
97 -------------------------------------------------------------------------- */
98
99 void _assertFail(const char *filename, unsigned int linenum)
100 GNUC3_ATTRIBUTE(__noreturn__);
101
102 #define CHECK(predicate) \
103 if (predicate) \
104 /*null*/; \
105 else \
106 _assertFail(__FILE__, __LINE__)
107
108 #define CHECKM(predicate, msg, ...) \
109 if (predicate) \
110 /*null*/; \
111 else \
112 barf(msg, ##__VA_ARGS__)
113
114 #ifndef DEBUG
115 #define ASSERT(predicate) /* nothing */
116 #define ASSERTM(predicate,msg,...) /* nothing */
117 #else
118 #define ASSERT(predicate) CHECK(predicate)
119 #define ASSERTM(predicate,msg,...) CHECKM(predicate,msg,##__VA_ARGS__)
120 #endif /* DEBUG */
121
122 /*
123 * Use this on the RHS of macros which expand to nothing
124 * to make sure that the macro can be used in a context which
125 * demands a non-empty statement.
126 */
127
128 #define doNothing() do { } while (0)
129
130 #ifdef DEBUG
131 #define USED_IF_DEBUG
132 #define USED_IF_NOT_DEBUG STG_UNUSED
133 #else
134 #define USED_IF_DEBUG STG_UNUSED
135 #define USED_IF_NOT_DEBUG
136 #endif
137
138 #ifdef THREADED_RTS
139 #define USED_IF_THREADS
140 #define USED_IF_NOT_THREADS STG_UNUSED
141 #else
142 #define USED_IF_THREADS STG_UNUSED
143 #define USED_IF_NOT_THREADS
144 #endif
145
146 #if SIZEOF_VOID_P == 8
147 # define FMT_SizeT "zu"
148 # define FMT_HexSizeT "zx"
149 # define FMT_Word "zu"
150 # define FMT_Int "zd"
151 #elif SIZEOF_VOID_P == 4
152 # if defined(mingw32_HOST_OS)
153 # define FMT_SizeT "u"
154 # define FMT_HexSizeT "x"
155 # define FMT_Word "u"
156 # define FMT_Int "d"
157 # else
158 # define FMT_SizeT "zu"
159 # define FMT_HexSizeT "zx"
160 # define FMT_Word "zu"
161 # define FMT_Int "zd"
162 # endif
163 #else
164 # error Cannot handle this word size
165 #endif
166
167 /*
168 * Getting printf formats right for platform-dependent typedefs
169 */
170 #if SIZEOF_LONG == 8
171 #define FMT_Word64 "lu"
172 #define FMT_Int64 "ld"
173 #else
174 #if defined(mingw32_HOST_OS) && defined(i386_HOST_ARCH)
175 /* mingw doesn't understand %llu/%lld - it treats them as 32-bit
176 rather than 64-bit */
177 #define FMT_Word64 "I64u"
178 #define FMT_Int64 "I64d"
179 #else
180 #define FMT_Word64 "llu"
181 #define FMT_Int64 "lld"
182 #endif
183 #endif
184
185 /* -----------------------------------------------------------------------------
186 Time values in the RTS
187 -------------------------------------------------------------------------- */
188
189 // For most time values in the RTS we use a fixed resolution of nanoseconds,
190 // normalising the time we get from platform-dependent APIs to this
191 // resolution.
192 #define TIME_RESOLUTION 1000000000
193 typedef StgInt64 Time;
194
195 #if TIME_RESOLUTION == 1000000000
196 // I'm being lazy, but it's awkward to define fully general versions of these
197 #define TimeToUS(t) ((t) / 1000)
198 #define TimeToNS(t) (t)
199 #define USToTime(t) ((Time)(t) * 1000)
200 #define NSToTime(t) ((Time)(t))
201 #else
202 #error Fix TimeToNS(), TimeToUS() etc.
203 #endif
204
205 #define SecondsToTime(t) ((Time)(t) * TIME_RESOLUTION)
206 #define TimeToSeconds(t) ((t) / TIME_RESOLUTION)
207
208 // Use instead of SecondsToTime() when we have a floating-point
209 // seconds value, to avoid truncating it.
210 INLINE_HEADER Time fsecondsToTime (double t)
211 {
212 return (Time)(t * TIME_RESOLUTION);
213 }
214
215 /* -----------------------------------------------------------------------------
216 Include everything STG-ish
217 -------------------------------------------------------------------------- */
218
219 /* System headers: stdlib.h is eeded so that we can use NULL. It must
220 * come after MachRegs.h, because stdlib.h might define some inline
221 * functions which may only be defined after register variables have
222 * been declared.
223 */
224 #include <stdlib.h>
225
226 #include "rts/Config.h"
227
228 /* Global constaints */
229 #include "rts/Constants.h"
230
231 /* Profiling information */
232 #include "rts/prof/CCS.h"
233 #include "rts/prof/LDV.h"
234
235 /* Parallel information */
236 #include "rts/OSThreads.h"
237 #include "rts/SpinLock.h"
238
239 #include "rts/Messages.h"
240
241 /* Storage format definitions */
242 #include "rts/storage/FunTypes.h"
243 #include "rts/storage/InfoTables.h"
244 #include "rts/storage/Closures.h"
245 #include "rts/storage/Liveness.h"
246 #include "rts/storage/ClosureTypes.h"
247 #include "rts/storage/TSO.h"
248 #include "stg/MiscClosures.h" /* InfoTables, closures etc. defined in the RTS */
249 #include "rts/storage/SMPClosureOps.h"
250 #include "rts/storage/Block.h"
251 #include "rts/storage/ClosureMacros.h"
252 #include "rts/storage/MBlock.h"
253 #include "rts/storage/GC.h"
254
255 /* Other RTS external APIs */
256 #include "rts/Parallel.h"
257 #include "rts/Hooks.h"
258 #include "rts/Signals.h"
259 #include "rts/BlockSignals.h"
260 #include "rts/Hpc.h"
261 #include "rts/Flags.h"
262 #include "rts/Adjustor.h"
263 #include "rts/FileLock.h"
264 #include "rts/Globals.h"
265 #include "rts/IOManager.h"
266 #include "rts/Linker.h"
267 #include "rts/Threads.h"
268 #include "rts/Ticky.h"
269 #include "rts/Timer.h"
270 #include "rts/Stable.h"
271 #include "rts/TTY.h"
272 #include "rts/Utils.h"
273 #include "rts/PrimFloat.h"
274 #include "rts/Main.h"
275
276 /* Misc stuff without a home */
277 DLL_IMPORT_RTS extern char **prog_argv; /* so we can get at these from Haskell */
278 DLL_IMPORT_RTS extern int prog_argc;
279 DLL_IMPORT_RTS extern char *prog_name;
280
281 #ifdef mingw32_HOST_OS
282 // We need these two from Haskell too
283 void getWin32ProgArgv(int *argc, wchar_t **argv[]);
284 void setWin32ProgArgv(int argc, wchar_t *argv[]);
285 #endif
286
287 void stackOverflow(void);
288
289 void stg_exit(int n) GNU_ATTRIBUTE(__noreturn__);
290
291 #ifndef mingw32_HOST_OS
292 int stg_sig_install (int, int, void *);
293 #endif
294
295 /* -----------------------------------------------------------------------------
296 RTS Exit codes
297 -------------------------------------------------------------------------- */
298
299 /* 255 is allegedly used by dynamic linkers to report linking failure */
300 #define EXIT_INTERNAL_ERROR 254
301 #define EXIT_DEADLOCK 253
302 #define EXIT_INTERRUPTED 252
303 #define EXIT_HEAPOVERFLOW 251
304 #define EXIT_KILLED 250
305
306 /* -----------------------------------------------------------------------------
307 Miscellaneous garbage
308 -------------------------------------------------------------------------- */
309
310 #ifdef DEBUG
311 #define TICK_VAR(arity) \
312 extern StgInt SLOW_CALLS_##arity; \
313 extern StgInt RIGHT_ARITY_##arity; \
314 extern StgInt TAGGED_PTR_##arity;
315
316 extern StgInt TOTAL_CALLS;
317
318 TICK_VAR(1)
319 TICK_VAR(2)
320 #endif
321
322 /* -----------------------------------------------------------------------------
323 Assertions and Debuggery
324 -------------------------------------------------------------------------- */
325
326 #define IF_RTSFLAGS(c,s) if (RtsFlags.c) { s; }
327
328 #ifdef DEBUG
329 #if IN_STG_CODE
330 #define IF_DEBUG(c,s) if (RtsFlags[0].DebugFlags.c) { s; }
331 #else
332 #define IF_DEBUG(c,s) if (RtsFlags.DebugFlags.c) { s; }
333 #endif
334 #else
335 #define IF_DEBUG(c,s) doNothing()
336 #endif
337
338 #ifdef DEBUG
339 #define DEBUG_ONLY(s) s
340 #else
341 #define DEBUG_ONLY(s) doNothing()
342 #endif
343
344 /* -----------------------------------------------------------------------------
345 Useful macros and inline functions
346 -------------------------------------------------------------------------- */
347
348 #if defined(__GNUC__)
349 #define SUPPORTS_TYPEOF
350 #endif
351
352 #if defined(SUPPORTS_TYPEOF)
353 #define stg_min(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _a : _b; })
354 #define stg_max(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _b : _a; })
355 #else
356 #define stg_min(a,b) ((a) <= (b) ? (a) : (b))
357 #define stg_max(a,b) ((a) <= (b) ? (b) : (a))
358 #endif
359
360 /* -------------------------------------------------------------------------- */
361
362 #ifdef __cplusplus
363 }
364 #endif
365
366 #endif /* RTS_H */