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