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