RTS tidyup sweep, first phase
[ghc-hetmet.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  * ---------------------------------------------------------------------------*/
9
10 #ifndef RTS_H
11 #define RTS_H
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 #ifndef IN_STG_CODE
18 #define IN_STG_CODE 0
19 #endif
20 #include "Stg.h"
21
22 #include "HsFFI.h"
23 #include "RtsAPI.h"
24
25 // Turn off inlining when debugging - it obfuscates things
26 #ifdef DEBUG
27 # undef  STATIC_INLINE
28 # define STATIC_INLINE static
29 #endif
30
31 #include "rts/Types.h"
32
33 #if __GNUC__ >= 3
34 /* Assume that a flexible array member at the end of a struct
35  * can be defined thus: T arr[]; */
36 #define FLEXIBLE_ARRAY
37 #else
38 /* Assume that it must be defined thus: T arr[0]; */
39 #define FLEXIBLE_ARRAY 0
40 #endif
41
42 #if __GNUC__ >= 3
43 #define ATTRIBUTE_ALIGNED(n) __attribute__((aligned(n)))
44 #else
45 #define ATTRIBUTE_ALIGNED(n) /*nothing*/
46 #endif
47
48 /* Fix for mingw stat problem (done here so it's early enough) */
49 #ifdef mingw32_HOST_OS
50 #define __MSVCRT__ 1
51 #endif
52
53 /* Needed to get the macro version of errno on some OSs, and also to
54    get prototypes for the _r versions of C library functions. */
55 #ifndef _REENTRANT
56 #define _REENTRANT 1
57 #endif
58
59 /*
60  * We often want to know the size of something in units of an
61  * StgWord... (rounded up, of course!)
62  */
63 #define ROUNDUP_BYTES_TO_WDS(n) (((n) + sizeof(W_) - 1) / sizeof(W_))
64
65 #define sizeofW(t) ROUNDUP_BYTES_TO_WDS(sizeof(t))
66
67 /* -----------------------------------------------------------------------------
68    Assertions and Debuggery
69
70    CHECK(p)   evaluates p and terminates with an error if p is false
71    ASSERT(p)  like CHECK(p) if DEBUG is on, otherwise a no-op
72    -------------------------------------------------------------------------- */
73
74 void _assertFail(const char *filename, unsigned int linenum)
75    GNUC3_ATTRIBUTE(__noreturn__);
76
77 #define CHECK(predicate)                        \
78         if (predicate)                          \
79             /*null*/;                           \
80         else                                    \
81             _assertFail(__FILE__, __LINE__)
82
83 #ifndef DEBUG
84 #define ASSERT(predicate) /* nothing */
85 #else
86 #define ASSERT(predicate) CHECK(predicate)
87 #endif /* DEBUG */
88
89 /* 
90  * Use this on the RHS of macros which expand to nothing
91  * to make sure that the macro can be used in a context which
92  * demands a non-empty statement.
93  */
94
95 #define doNothing() do { } while (0)
96
97 #ifdef DEBUG
98 #define USED_IF_DEBUG
99 #define USED_IF_NOT_DEBUG STG_UNUSED
100 #else
101 #define USED_IF_DEBUG STG_UNUSED
102 #define USED_IF_NOT_DEBUG
103 #endif
104
105 #ifdef THREADED_RTS
106 #define USED_IF_THREADS
107 #define USED_IF_NOT_THREADS STG_UNUSED
108 #else
109 #define USED_IF_THREADS STG_UNUSED
110 #define USED_IF_NOT_THREADS
111 #endif
112
113 /*
114  * Getting printf formats right for platform-dependent typedefs
115  */
116 #if SIZEOF_LONG == 8
117 #define FMT_Word64 "lu"
118 #define FMT_Int64  "ld"
119 #else
120 #define FMT_Word64 "llu"
121 #define FMT_Int64  "lld"
122 #endif
123
124 /* -----------------------------------------------------------------------------
125    Include everything STG-ish
126    -------------------------------------------------------------------------- */
127
128 /* System headers: stdlib.h is eeded so that we can use NULL.  It must
129  * come after MachRegs.h, because stdlib.h might define some inline
130  * functions which may only be defined after register variables have
131  * been declared.
132  */
133 #include <stdlib.h>
134
135 #include "rts/Config.h"
136
137 /* Global constaints */
138 #include "rts/Constants.h"
139
140 /* Profiling information */
141 #include "rts/prof/CCS.h"
142 #include "rts/prof/LDV.h"
143
144 /* Parallel information */
145 #include "rts/OSThreads.h"
146 #include "rts/SpinLock.h"
147
148 #include "rts/Messages.h"
149
150 /* Storage format definitions */
151 #include "rts/storage/FunTypes.h"
152 #include "rts/storage/InfoTables.h"
153 #include "rts/storage/Closures.h"
154 #include "rts/storage/Liveness.h"
155 #include "rts/storage/ClosureTypes.h"
156 #include "rts/storage/TSO.h"
157 #include "stg/MiscClosures.h" /* InfoTables, closures etc. defined in the RTS */
158 #include "rts/storage/SMPClosureOps.h"
159 #include "rts/storage/Block.h"
160 #include "rts/storage/ClosureMacros.h"
161 #include "rts/storage/MBlock.h"
162 #include "rts/storage/GC.h"
163
164 /* Other RTS external APIs */
165 #include "rts/Parallel.h"
166 #include "rts/Hooks.h"
167 #include "rts/Signals.h"
168 #include "rts/Hpc.h"
169 #include "rts/Flags.h"
170 #include "rts/Adjustor.h"
171 #include "rts/FileLock.h"
172 #include "rts/Globals.h"
173 #include "rts/IOManager.h"
174 #include "rts/Linker.h"
175 #include "rts/Threads.h"
176 #include "rts/Timer.h"
177 #include "rts/Stable.h"
178
179 /* Misc stuff without a home */
180 DLL_IMPORT_RTS extern char **prog_argv; /* so we can get at these from Haskell */
181 DLL_IMPORT_RTS extern int    prog_argc;
182 DLL_IMPORT_RTS extern char  *prog_name;
183
184 void stackOverflow(void);
185
186 void stg_exit(int n) GNU_ATTRIBUTE(__noreturn__);
187
188 #ifndef mingw32_HOST_OS
189 int stg_sig_install (int, int, void *);
190 #endif
191
192 /* -----------------------------------------------------------------------------
193    RTS Exit codes
194    -------------------------------------------------------------------------- */
195
196 /* 255 is allegedly used by dynamic linkers to report linking failure */
197 #define EXIT_INTERNAL_ERROR 254
198 #define EXIT_DEADLOCK       253
199 #define EXIT_INTERRUPTED    252
200 #define EXIT_HEAPOVERFLOW   251
201 #define EXIT_KILLED         250
202
203 /* -----------------------------------------------------------------------------
204    Miscellaneous garbage
205    -------------------------------------------------------------------------- */
206
207 /* declarations for runtime flags/values */
208 #define MAX_RTS_ARGS 32
209
210 #ifdef DEBUG
211 #define TICK_VAR(arity) \
212   extern StgInt SLOW_CALLS_##arity; \
213   extern StgInt RIGHT_ARITY_##arity; \
214   extern StgInt TAGGED_PTR_##arity;
215
216 extern StgInt TOTAL_CALLS;
217
218 TICK_VAR(1)
219 TICK_VAR(2)
220 #endif
221
222 /* -----------------------------------------------------------------------------
223    Assertions and Debuggery
224    -------------------------------------------------------------------------- */
225
226 #define IF_RTSFLAGS(c,s)  if (RtsFlags.c) { s; }
227
228 #ifdef DEBUG
229 #if IN_STG_CODE
230 #define IF_DEBUG(c,s)  if (RtsFlags[0].DebugFlags.c) { s; }
231 #else
232 #define IF_DEBUG(c,s)  if (RtsFlags.DebugFlags.c) { s; }
233 #endif
234 #else
235 #define IF_DEBUG(c,s)  doNothing()
236 #endif
237
238 #ifdef DEBUG
239 #define DEBUG_ONLY(s) s
240 #else
241 #define DEBUG_ONLY(s) doNothing()
242 #endif
243
244 /* -----------------------------------------------------------------------------
245    Useful macros and inline functions
246    -------------------------------------------------------------------------- */
247
248 #if defined(__GNUC__)
249 #define SUPPORTS_TYPEOF
250 #endif
251
252 #if defined(SUPPORTS_TYPEOF)
253 #define stg_min(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _a : _b; })
254 #define stg_max(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _b : _a; })
255 #else
256 #define stg_min(a,b) ((a) <= (b) ? (a) : (b))
257 #define stg_max(a,b) ((a) <= (b) ? (b) : (a))
258 #endif
259
260 /* -------------------------------------------------------------------------- */
261
262 #ifdef __cplusplus
263 }
264 #endif
265
266
267 /* krc: I put this here because I don't think
268    it needs to be visible externally.
269    It used to be in StgTicky.h, but I got rid
270    of that. */
271
272 /* -----------------------------------------------------------------------------
273    The StgEntCounter type - needed regardless of TICKY_TICKY
274    -------------------------------------------------------------------------- */
275
276 typedef struct _StgEntCounter {
277   /* Using StgWord for everything, becuase both the C and asm code
278      generators make trouble if you try to pack things tighter */
279     StgWord     registeredp;    /* 0 == no, 1 == yes */
280     StgInt      arity;          /* arity (static info) */
281     StgInt      stk_args;       /* # of args off stack */
282                                 /* (rest of args are in registers) */
283     char        *str;           /* name of the thing */
284     char        *arg_kinds;     /* info about the args types */
285     StgInt      entry_count;    /* Trips to fast entry code */
286     StgInt      allocs;         /* number of allocations by this fun */
287     struct _StgEntCounter *link;/* link to chain them all together */
288 } StgEntCounter;
289
290
291 #endif /* RTS_H */