Tag pointers in interpreted constructors
[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
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 Time values in the RTS
149 -------------------------------------------------------------------------- */
150
151 // For most time values in the RTS we use a fixed resolution of nanoseconds,
152 // normalising the time we get from platform-dependent APIs to this
153 // resolution.
154 #define TIME_RESOLUTION 1000000000
155 typedef StgInt64 Time;
156
157 #define TIME_MAX HS_INT64_MAX
158
159 #if TIME_RESOLUTION == 1000000000
160 // I'm being lazy, but it's awkward to define fully general versions of these
161 #define TimeToUS(t) ((t) / 1000)
162 #define TimeToNS(t) (t)
163 #define USToTime(t) ((Time)(t) * 1000)
164 #define NSToTime(t) ((Time)(t))
165 #else
166 #error Fix TimeToNS(), TimeToUS() etc.
167 #endif
168
169 #define SecondsToTime(t) ((Time)(t) * TIME_RESOLUTION)
170 #define TimeToSeconds(t) ((t) / TIME_RESOLUTION)
171
172 // Use instead of SecondsToTime() when we have a floating-point
173 // seconds value, to avoid truncating it.
174 INLINE_HEADER Time fsecondsToTime (double t)
175 {
176 return (Time)(t * TIME_RESOLUTION);
177 }
178
179 /* -----------------------------------------------------------------------------
180 Include everything STG-ish
181 -------------------------------------------------------------------------- */
182
183 /* System headers: stdlib.h is needed so that we can use NULL. It must
184 * come after MachRegs.h, because stdlib.h might define some inline
185 * functions which may only be defined after register variables have
186 * been declared.
187 */
188 #include <stdlib.h>
189
190 #include "rts/Config.h"
191
192 /* Global constraints */
193 #include "rts/Constants.h"
194
195 /* Profiling information */
196 #include "rts/prof/CCS.h"
197 #include "rts/prof/LDV.h"
198
199 /* Parallel information */
200 #include "rts/OSThreads.h"
201 #include "rts/SpinLock.h"
202
203 #include "rts/Messages.h"
204 #include "rts/Threads.h"
205
206 /* Storage format definitions */
207 #include "rts/storage/FunTypes.h"
208 #include "rts/storage/InfoTables.h"
209 #include "rts/storage/Closures.h"
210 #include "rts/storage/ClosureTypes.h"
211 #include "rts/storage/TSO.h"
212 #include "stg/MiscClosures.h" /* InfoTables, closures etc. defined in the RTS */
213 #include "rts/storage/Block.h"
214 #include "rts/storage/ClosureMacros.h"
215 #include "rts/storage/MBlock.h"
216 #include "rts/storage/GC.h"
217
218 /* Other RTS external APIs */
219 #include "rts/Parallel.h"
220 #include "rts/Signals.h"
221 #include "rts/BlockSignals.h"
222 #include "rts/Hpc.h"
223 #include "rts/Flags.h"
224 #include "rts/Adjustor.h"
225 #include "rts/FileLock.h"
226 #include "rts/GetTime.h"
227 #include "rts/Globals.h"
228 #include "rts/IOManager.h"
229 #include "rts/Linker.h"
230 #include "rts/Ticky.h"
231 #include "rts/Timer.h"
232 #include "rts/Stable.h"
233 #include "rts/TTY.h"
234 #include "rts/Utils.h"
235 #include "rts/PrimFloat.h"
236 #include "rts/Main.h"
237 #include "rts/StaticPtrTable.h"
238 #include "rts/Libdw.h"
239 #include "rts/LibdwPool.h"
240
241 /* Misc stuff without a home */
242 DLL_IMPORT_RTS extern char **prog_argv; /* so we can get at these from Haskell */
243 DLL_IMPORT_RTS extern int prog_argc;
244 DLL_IMPORT_RTS extern char *prog_name;
245
246 #ifdef mingw32_HOST_OS
247 // We need these two from Haskell too
248 void getWin32ProgArgv(int *argc, wchar_t **argv[]);
249 void setWin32ProgArgv(int argc, wchar_t *argv[]);
250 #endif
251
252 void stackOverflow(StgTSO* tso);
253
254 void stg_exit(int n) GNU_ATTRIBUTE(__noreturn__);
255
256 #ifndef mingw32_HOST_OS
257 int stg_sig_install (int, int, void *);
258 #endif
259
260 /* -----------------------------------------------------------------------------
261 Ways
262 -------------------------------------------------------------------------- */
263
264 // Returns non-zero if the RTS is a profiling version
265 int rts_isProfiled(void);
266
267 // Returns non-zero if the RTS is a dynamically-linked version
268 int rts_isDynamic(void);
269
270 /* -----------------------------------------------------------------------------
271 RTS Exit codes
272 -------------------------------------------------------------------------- */
273
274 /* 255 is allegedly used by dynamic linkers to report linking failure */
275 #define EXIT_INTERNAL_ERROR 254
276 #define EXIT_DEADLOCK 253
277 #define EXIT_INTERRUPTED 252
278 #define EXIT_HEAPOVERFLOW 251
279 #define EXIT_KILLED 250
280
281 /* -----------------------------------------------------------------------------
282 Miscellaneous garbage
283 -------------------------------------------------------------------------- */
284
285 #ifdef DEBUG
286 #define TICK_VAR(arity) \
287 extern StgInt SLOW_CALLS_##arity; \
288 extern StgInt RIGHT_ARITY_##arity; \
289 extern StgInt TAGGED_PTR_##arity;
290
291 extern StgInt TOTAL_CALLS;
292
293 TICK_VAR(1)
294 TICK_VAR(2)
295 #endif
296
297 /* -----------------------------------------------------------------------------
298 Assertions and Debuggery
299 -------------------------------------------------------------------------- */
300
301 #define IF_RTSFLAGS(c,s) if (RtsFlags.c) { s; }
302
303 #ifdef DEBUG
304 #if IN_STG_CODE
305 #define IF_DEBUG(c,s) if (RtsFlags[0].DebugFlags.c) { s; }
306 #else
307 #define IF_DEBUG(c,s) if (RtsFlags.DebugFlags.c) { s; }
308 #endif
309 #else
310 #define IF_DEBUG(c,s) doNothing()
311 #endif
312
313 #ifdef DEBUG
314 #define DEBUG_ONLY(s) s
315 #else
316 #define DEBUG_ONLY(s) doNothing()
317 #endif
318
319 #ifdef DEBUG
320 #define DEBUG_IS_ON 1
321 #else
322 #define DEBUG_IS_ON 0
323 #endif
324
325 /* -----------------------------------------------------------------------------
326 Useful macros and inline functions
327 -------------------------------------------------------------------------- */
328
329 #if defined(__GNUC__)
330 #define SUPPORTS_TYPEOF
331 #endif
332
333 #if defined(SUPPORTS_TYPEOF)
334 #define stg_min(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _a : _b; })
335 #define stg_max(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _b : _a; })
336 #else
337 #define stg_min(a,b) ((a) <= (b) ? (a) : (b))
338 #define stg_max(a,b) ((a) <= (b) ? (b) : (a))
339 #endif
340
341 /* -------------------------------------------------------------------------- */
342
343 #ifdef __cplusplus
344 }
345 #endif
346
347 #endif /* RTS_H */