add numSparks# primop (#4167)
[ghc.git] / includes / mkDerivedConstants.c
1 /* --------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1992-2004
4 *
5 * mkDerivedConstants.c
6 *
7 * Basically this is a C program that extracts information from the C
8 * declarations in the header files (primarily struct field offsets)
9 * and generates a header file that can be #included into non-C source
10 * containing this information.
11 *
12 * ------------------------------------------------------------------------*/
13
14 #define IN_STG_CODE 0
15
16 /*
17 * We need offsets of profiled things... better be careful that this
18 * doesn't affect the offsets of anything else.
19 */
20 #define PROFILING
21 #define THREADED_RTS
22
23 #include "Rts.h"
24
25 #include "Stable.h"
26 #include "Capability.h"
27
28 #include <stdio.h>
29
30 #define str(a,b) #a "_" #b
31
32 #define OFFSET(s_type, field) ((size_t)&(((s_type*)0)->field))
33
34 #if defined(GEN_HASKELL)
35 #define def_offset(str, offset) \
36 printf("oFFSET_" str " :: Int\n"); \
37 printf("oFFSET_" str " = %lu\n", (unsigned long)offset);
38 #else
39 #define def_offset(str, offset) \
40 printf("#define OFFSET_" str " %lu\n", (unsigned long)offset);
41 #endif
42
43 #if defined(GEN_HASKELL)
44 #define ctype(type) /* nothing */
45 #else
46 #define ctype(type) \
47 printf("#define SIZEOF_" #type " %lu\n", (unsigned long)sizeof(type));
48 #endif
49
50 #if defined(GEN_HASKELL)
51 #define field_type_(str, s_type, field) /* nothing */
52 #define field_type_gcptr_(str, s_type, field) /* nothing */
53 #else
54 /* Defining REP_x to be b32 etc
55 These are both the C-- types used in a load
56 e.g. b32[addr]
57 and the names of the CmmTypes in the compiler
58 b32 :: CmmType
59 */
60 #define field_type_(str, s_type, field) \
61 printf("#define REP_" str " b"); \
62 printf("%lu\n", (unsigned long)sizeof (__typeof__(((((s_type*)0)->field)))) * 8);
63 #define field_type_gcptr_(str, s_type, field) \
64 printf("#define REP_" str " gcptr\n");
65 #endif
66
67 #define field_type(s_type, field) \
68 field_type_(str(s_type,field),s_type,field);
69
70 #define field_offset_(str, s_type, field) \
71 def_offset(str, OFFSET(s_type,field));
72
73 #define field_offset(s_type, field) \
74 field_offset_(str(s_type,field),s_type,field);
75
76 /* An access macro for use in C-- sources. */
77 #define struct_field_macro(str) \
78 printf("#define " str "(__ptr__) REP_" str "[__ptr__+OFFSET_" str "]\n");
79
80 /* Outputs the byte offset and MachRep for a field */
81 #define struct_field(s_type, field) \
82 field_offset(s_type, field); \
83 field_type(s_type, field); \
84 struct_field_macro(str(s_type,field))
85
86 #define struct_field_(str, s_type, field) \
87 field_offset_(str, s_type, field); \
88 field_type_(str, s_type, field); \
89 struct_field_macro(str)
90
91 #if defined(GEN_HASKELL)
92 #define def_size(str, size) \
93 printf("sIZEOF_" str " :: Int\n"); \
94 printf("sIZEOF_" str " = %lu\n", (unsigned long)size);
95 #else
96 #define def_size(str, size) \
97 printf("#define SIZEOF_" str " %lu\n", (unsigned long)size);
98 #endif
99
100 #if defined(GEN_HASKELL)
101 #define def_closure_size(str, size) /* nothing */
102 #else
103 #define def_closure_size(str, size) \
104 printf("#define SIZEOF_" str " (SIZEOF_StgHeader+%lu)\n", (unsigned long)size);
105 #endif
106
107 #define struct_size(s_type) \
108 def_size(#s_type, sizeof(s_type));
109
110 /*
111 * Size of a closure type, minus the header, named SIZEOF_<type>_NoHdr
112 * Also, we #define SIZEOF_<type> to be the size of the whole closure for .cmm.
113 */
114 #define closure_size(s_type) \
115 def_size(#s_type "_NoHdr", sizeof(s_type) - sizeof(StgHeader)); \
116 def_closure_size(#s_type, sizeof(s_type) - sizeof(StgHeader));
117
118 #define thunk_size(s_type) \
119 def_size(#s_type "_NoThunkHdr", sizeof(s_type) - sizeof(StgThunkHeader)); \
120 closure_size(s_type)
121
122 /* An access macro for use in C-- sources. */
123 #define closure_field_macro(str) \
124 printf("#define " str "(__ptr__) REP_" str "[__ptr__+SIZEOF_StgHeader+OFFSET_" str "]\n");
125
126 #define closure_field_offset_(str, s_type,field) \
127 def_offset(str, OFFSET(s_type,field) - sizeof(StgHeader));
128
129 #define closure_field_offset(s_type,field) \
130 closure_field_offset_(str(s_type,field),s_type,field)
131
132 #define closure_payload_macro(str) \
133 printf("#define " str "(__ptr__,__ix__) W_[__ptr__+SIZEOF_StgHeader+OFFSET_" str " + WDS(__ix__)]\n");
134
135 #define closure_payload(s_type,field) \
136 closure_field_offset_(str(s_type,field),s_type,field); \
137 closure_payload_macro(str(s_type,field));
138
139 /* Byte offset and MachRep for a closure field, minus the header */
140 #define closure_field_(str, s_type, field) \
141 closure_field_offset_(str,s_type,field) \
142 field_type_(str, s_type, field); \
143 closure_field_macro(str)
144
145 #define closure_field(s_type, field) \
146 closure_field_(str(s_type,field),s_type,field)
147
148 /* Byte offset and MachRep for a closure field, minus the header */
149 #define closure_field_gcptr_(str, s_type, field) \
150 closure_field_offset_(str,s_type,field) \
151 field_type_gcptr_(str, s_type, field); \
152 closure_field_macro(str)
153
154 #define closure_field_gcptr(s_type, field) \
155 closure_field_gcptr_(str(s_type,field),s_type,field)
156
157 /* Byte offset for a TSO field, minus the header and variable prof bit. */
158 #define tso_payload_offset(s_type, field) \
159 def_offset(str(s_type,field), OFFSET(s_type,field) - sizeof(StgHeader) - sizeof(StgTSOProfInfo));
160
161 /* Full byte offset for a TSO field, for use from Cmm */
162 #define tso_field_offset_macro(str) \
163 printf("#define TSO_OFFSET_" str " (SIZEOF_StgHeader+SIZEOF_OPT_StgTSOProfInfo+OFFSET_" str ")\n");
164
165 #define tso_field_offset(s_type, field) \
166 tso_payload_offset(s_type, field); \
167 tso_field_offset_macro(str(s_type,field));
168
169 #define tso_field_macro(str) \
170 printf("#define " str "(__ptr__) REP_" str "[__ptr__+TSO_OFFSET_" str "]\n")
171 #define tso_field(s_type, field) \
172 field_type(s_type, field); \
173 tso_field_offset(s_type,field); \
174 tso_field_macro(str(s_type,field))
175
176 #define opt_struct_size(s_type, option) \
177 printf("#ifdef " #option "\n"); \
178 printf("#define SIZEOF_OPT_" #s_type " SIZEOF_" #s_type "\n"); \
179 printf("#else\n"); \
180 printf("#define SIZEOF_OPT_" #s_type " 0\n"); \
181 printf("#endif\n\n");
182
183 #define FUN_OFFSET(sym) (OFFSET(Capability,f.sym) - OFFSET(Capability,r))
184
185
186 int
187 main(int argc, char *argv[])
188 {
189 #ifndef GEN_HASKELL
190 printf("/* This file is created automatically. Do not edit by hand.*/\n\n");
191
192 printf("#define STD_HDR_SIZE %lu\n", (unsigned long)sizeofW(StgHeader) - sizeofW(StgProfHeader));
193 /* grrr.. PROFILING is on so we need to subtract sizeofW(StgProfHeader) */
194 printf("#define PROF_HDR_SIZE %lu\n", (unsigned long)sizeofW(StgProfHeader));
195
196 printf("#define BLOCK_SIZE %u\n", BLOCK_SIZE);
197 printf("#define MBLOCK_SIZE %u\n", MBLOCK_SIZE);
198
199 printf("\n\n");
200 #endif
201
202 field_offset(StgRegTable, rR1);
203 field_offset(StgRegTable, rR2);
204 field_offset(StgRegTable, rR3);
205 field_offset(StgRegTable, rR4);
206 field_offset(StgRegTable, rR5);
207 field_offset(StgRegTable, rR6);
208 field_offset(StgRegTable, rR7);
209 field_offset(StgRegTable, rR8);
210 field_offset(StgRegTable, rR9);
211 field_offset(StgRegTable, rR10);
212 field_offset(StgRegTable, rF1);
213 field_offset(StgRegTable, rF2);
214 field_offset(StgRegTable, rF3);
215 field_offset(StgRegTable, rF4);
216 field_offset(StgRegTable, rD1);
217 field_offset(StgRegTable, rD2);
218 field_offset(StgRegTable, rL1);
219 field_offset(StgRegTable, rSp);
220 field_offset(StgRegTable, rSpLim);
221 field_offset(StgRegTable, rHp);
222 field_offset(StgRegTable, rHpLim);
223 field_offset(StgRegTable, rCurrentTSO);
224 field_offset(StgRegTable, rCurrentNursery);
225 field_offset(StgRegTable, rHpAlloc);
226 struct_field(StgRegTable, rRet);
227 struct_field(StgRegTable, rNursery);
228
229 def_offset("stgEagerBlackholeInfo", FUN_OFFSET(stgEagerBlackholeInfo));
230 def_offset("stgGCEnter1", FUN_OFFSET(stgGCEnter1));
231 def_offset("stgGCFun", FUN_OFFSET(stgGCFun));
232
233 field_offset(Capability, r);
234 field_offset(Capability, lock);
235 struct_field(Capability, mut_lists);
236 struct_field(Capability, context_switch);
237 struct_field(Capability, sparks);
238
239 struct_field(bdescr, start);
240 struct_field(bdescr, free);
241 struct_field(bdescr, blocks);
242 struct_field(bdescr, gen_no);
243 struct_field(bdescr, link);
244
245 struct_size(generation);
246 struct_field(generation, mut_list);
247 struct_field(generation, n_new_large_blocks);
248
249 struct_size(CostCentreStack);
250 struct_field(CostCentreStack, ccsID);
251 struct_field(CostCentreStack, mem_alloc);
252 struct_field(CostCentreStack, scc_count);
253 struct_field(CostCentreStack, prevStack);
254
255 struct_field(CostCentre, ccID);
256 struct_field(CostCentre, link);
257
258 struct_field(StgHeader, info);
259 struct_field_("StgHeader_ccs", StgHeader, prof.ccs);
260 struct_field_("StgHeader_ldvw", StgHeader, prof.hp.ldvw);
261
262 struct_size(StgSMPThunkHeader);
263
264 closure_payload(StgClosure,payload);
265
266 struct_field(StgEntCounter, allocs);
267 struct_field(StgEntCounter, registeredp);
268 struct_field(StgEntCounter, link);
269 struct_field(StgEntCounter, entry_count);
270
271 closure_size(StgUpdateFrame);
272 closure_size(StgCatchFrame);
273 closure_size(StgStopFrame);
274
275 closure_size(StgMutArrPtrs);
276 closure_field(StgMutArrPtrs, ptrs);
277 closure_field(StgMutArrPtrs, size);
278
279 closure_size(StgArrWords);
280 closure_field(StgArrWords, bytes);
281 closure_payload(StgArrWords, payload);
282
283 closure_field(StgTSO, _link);
284 closure_field(StgTSO, global_link);
285 closure_field(StgTSO, what_next);
286 closure_field(StgTSO, why_blocked);
287 closure_field(StgTSO, block_info);
288 closure_field(StgTSO, blocked_exceptions);
289 closure_field(StgTSO, id);
290 closure_field(StgTSO, cap);
291 closure_field(StgTSO, saved_errno);
292 closure_field(StgTSO, trec);
293 closure_field(StgTSO, flags);
294 closure_field(StgTSO, dirty);
295 closure_field(StgTSO, bq);
296 closure_field_("StgTSO_CCCS", StgTSO, prof.CCCS);
297 tso_field(StgTSO, sp);
298 tso_field_offset(StgTSO, stack);
299 tso_field(StgTSO, stack_size);
300
301 struct_size(StgTSOProfInfo);
302
303 opt_struct_size(StgTSOProfInfo,PROFILING);
304
305 closure_field(StgUpdateFrame, updatee);
306
307 closure_field(StgCatchFrame, handler);
308 closure_field(StgCatchFrame, exceptions_blocked);
309
310 closure_size(StgPAP);
311 closure_field(StgPAP, n_args);
312 closure_field_gcptr(StgPAP, fun);
313 closure_field(StgPAP, arity);
314 closure_payload(StgPAP, payload);
315
316 thunk_size(StgAP);
317 closure_field(StgAP, n_args);
318 closure_field_gcptr(StgAP, fun);
319 closure_payload(StgAP, payload);
320
321 thunk_size(StgAP_STACK);
322 closure_field(StgAP_STACK, size);
323 closure_field_gcptr(StgAP_STACK, fun);
324 closure_payload(StgAP_STACK, payload);
325
326 thunk_size(StgSelector);
327
328 closure_field_gcptr(StgInd, indirectee);
329
330 closure_size(StgMutVar);
331 closure_field(StgMutVar, var);
332
333 closure_size(StgAtomicallyFrame);
334 closure_field(StgAtomicallyFrame, code);
335 closure_field(StgAtomicallyFrame, next_invariant_to_check);
336 closure_field(StgAtomicallyFrame, result);
337
338 closure_field(StgInvariantCheckQueue, invariant);
339 closure_field(StgInvariantCheckQueue, my_execution);
340 closure_field(StgInvariantCheckQueue, next_queue_entry);
341
342 closure_field(StgAtomicInvariant, code);
343
344 closure_field(StgTRecHeader, enclosing_trec);
345
346 closure_size(StgCatchSTMFrame);
347 closure_field(StgCatchSTMFrame, handler);
348 closure_field(StgCatchSTMFrame, code);
349
350 closure_size(StgCatchRetryFrame);
351 closure_field(StgCatchRetryFrame, running_alt_code);
352 closure_field(StgCatchRetryFrame, first_code);
353 closure_field(StgCatchRetryFrame, alt_code);
354
355 closure_field(StgTVarWatchQueue, closure);
356 closure_field(StgTVarWatchQueue, next_queue_entry);
357 closure_field(StgTVarWatchQueue, prev_queue_entry);
358
359 closure_field(StgTVar, current_value);
360
361 closure_size(StgWeak);
362 closure_field(StgWeak,link);
363 closure_field(StgWeak,key);
364 closure_field(StgWeak,value);
365 closure_field(StgWeak,finalizer);
366 closure_field(StgWeak,cfinalizer);
367
368 closure_size(StgDeadWeak);
369 closure_field(StgDeadWeak,link);
370
371 closure_size(StgMVar);
372 closure_field(StgMVar,head);
373 closure_field(StgMVar,tail);
374 closure_field(StgMVar,value);
375
376 closure_size(StgMVarTSOQueue);
377 closure_field(StgMVarTSOQueue, link);
378 closure_field(StgMVarTSOQueue, tso);
379
380 closure_size(StgBCO);
381 closure_field(StgBCO, instrs);
382 closure_field(StgBCO, literals);
383 closure_field(StgBCO, ptrs);
384 closure_field(StgBCO, arity);
385 closure_field(StgBCO, size);
386 closure_payload(StgBCO, bitmap);
387
388 closure_size(StgStableName);
389 closure_field(StgStableName,sn);
390
391 closure_size(StgBlockingQueue);
392 closure_field(StgBlockingQueue, bh);
393 closure_field(StgBlockingQueue, owner);
394 closure_field(StgBlockingQueue, queue);
395 closure_field(StgBlockingQueue, link);
396
397 closure_size(MessageBlackHole);
398 closure_field(MessageBlackHole, link);
399 closure_field(MessageBlackHole, tso);
400 closure_field(MessageBlackHole, bh);
401
402 struct_field_("RtsFlags_ProfFlags_showCCSOnException",
403 RTS_FLAGS, ProfFlags.showCCSOnException);
404 struct_field_("RtsFlags_DebugFlags_apply",
405 RTS_FLAGS, DebugFlags.apply);
406 struct_field_("RtsFlags_DebugFlags_sanity",
407 RTS_FLAGS, DebugFlags.sanity);
408 struct_field_("RtsFlags_DebugFlags_weak",
409 RTS_FLAGS, DebugFlags.weak);
410 struct_field_("RtsFlags_GcFlags_initialStkSize",
411 RTS_FLAGS, GcFlags.initialStkSize);
412 struct_field_("RtsFlags_MiscFlags_tickInterval",
413 RTS_FLAGS, MiscFlags.tickInterval);
414
415 struct_size(StgFunInfoExtraFwd);
416 struct_field(StgFunInfoExtraFwd, slow_apply);
417 struct_field(StgFunInfoExtraFwd, fun_type);
418 struct_field(StgFunInfoExtraFwd, arity);
419 struct_field_("StgFunInfoExtraFwd_bitmap", StgFunInfoExtraFwd, b.bitmap);
420
421 struct_size(StgFunInfoExtraRev);
422 struct_field(StgFunInfoExtraRev, slow_apply_offset);
423 struct_field(StgFunInfoExtraRev, fun_type);
424 struct_field(StgFunInfoExtraRev, arity);
425 struct_field_("StgFunInfoExtraRev_bitmap", StgFunInfoExtraRev, b.bitmap);
426
427 struct_field(StgLargeBitmap, size);
428 field_offset(StgLargeBitmap, bitmap);
429
430 struct_size(snEntry);
431 struct_field(snEntry,sn_obj);
432 struct_field(snEntry,addr);
433
434 #ifdef mingw32_HOST_OS
435 struct_size(StgAsyncIOResult);
436 struct_field(StgAsyncIOResult, reqID);
437 struct_field(StgAsyncIOResult, len);
438 struct_field(StgAsyncIOResult, errCode);
439 #endif
440
441 return 0;
442 }