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