1 /* --------------------------------------------------------------------------
3 * (c) The GHC Team, 1992-2004
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.
12 * ------------------------------------------------------------------------*/
17 * We need offsets of profiled things... better be careful that this
18 * doesn't affect the offsets of anything else.
25 #include "OSThreads.h"
26 #include "Capability.h"
30 #define str(a,b) #a "_" #b
32 #define OFFSET(s_type, field) ((unsigned int)&(((s_type*)0)->field))
34 #if defined(GEN_HASKELL)
35 #define def_offset(str, offset) \
36 printf("oFFSET_" str " = %d::Int\n", offset);
38 #define def_offset(str, offset) \
39 printf("#define OFFSET_" str " %d\n", offset);
42 #if defined(GEN_HASKELL)
43 #define ctype(type) /* nothing */
46 printf("#define SIZEOF_" #type " %d\n", sizeof(type));
49 #if defined(GEN_HASKELL)
50 #define field_type_(str, s_type, field) /* nothing */
52 #define field_type_(str, s_type, field) \
53 printf("#define REP_" str " I"); \
54 printf("%d\n", sizeof (__typeof__(((((s_type*)0)->field)))) * 8);
57 #define field_type(s_type, field) \
58 field_type_(str(s_type,field),s_type,field);
60 #define field_offset_(str, s_type, field) \
61 def_offset(str, OFFSET(s_type,field));
63 #define field_offset(s_type, field) \
64 field_offset_(str(s_type,field),s_type,field);
66 /* An access macro for use in C-- sources. */
67 #define struct_field_macro(str) \
68 printf("#define " str "(__ptr__) REP_" str "[__ptr__+OFFSET_" str "]\n");
70 /* Outputs the byte offset and MachRep for a field */
71 #define struct_field(s_type, field) \
72 field_offset(s_type, field); \
73 field_type(s_type, field); \
74 struct_field_macro(str(s_type,field))
76 #define struct_field_(str, s_type, field) \
77 field_offset_(str, s_type, field); \
78 field_type_(str, s_type, field); \
79 struct_field_macro(str)
81 #if defined(GEN_HASKELL)
82 #define def_size(str, size) \
83 printf("sIZEOF_" str " = %d::Int\n", size);
85 #define def_size(str, size) \
86 printf("#define SIZEOF_" str " %d\n", size);
89 #if defined(GEN_HASKELL)
90 #define def_closure_size(str, size) /* nothing */
92 #define def_closure_size(str, size) \
93 printf("#define SIZEOF_" str " (SIZEOF_StgHeader+%d)\n", size);
96 #if defined(GEN_HASKELL)
97 #define def_thunk_size(str, size) /* nothing */
99 #define def_thunk_size(str, size) \
100 printf("#define SIZEOF_" str " (SIZEOF_StgThunkHeader+%d)\n", size);
103 #define struct_size(s_type) \
104 def_size(#s_type, sizeof(s_type));
107 * Size of a closure type, minus the header, named SIZEOF_<type>_NoHdr
108 * Also, we #define SIZEOF_<type> to be the size of the whole closure for .cmm.
110 #define closure_size(s_type) \
111 def_size(#s_type "_NoHdr", sizeof(s_type) - sizeof(StgHeader)); \
112 def_closure_size(#s_type, sizeof(s_type) - sizeof(StgHeader));
114 #define thunk_size(s_type) \
115 def_size(#s_type "_NoHdr", sizeof(s_type) - sizeof(StgHeader)); \
116 def_thunk_size(#s_type, sizeof(s_type) - sizeof(StgHeader));
118 /* An access macro for use in C-- sources. */
119 #define closure_field_macro(str) \
120 printf("#define " str "(__ptr__) REP_" str "[__ptr__+SIZEOF_StgHeader+OFFSET_" str "]\n");
122 #define thunk_field_macro(str) \
123 printf("#define " str "(__ptr__) REP_" str "[__ptr__+SIZEOF_StgThunkHeader+OFFSET_" str "]\n");
125 #define closure_field_offset_(str, s_type,field) \
126 def_offset(str, OFFSET(s_type,field) - sizeof(StgHeader));
128 #define thunk_field_offset_(str, s_type, field) \
129 closure_field_offset_(str, s_type, field)
131 #define closure_field_offset(s_type,field) \
132 closure_field_offset_(str(s_type,field),s_type,field)
134 #define thunk_field_offset(s_type,field) \
135 thunk_field_offset_(str(s_type,field),s_type,field)
137 #define closure_payload_macro(str) \
138 printf("#define " str "(__ptr__,__ix__) W_[__ptr__+SIZEOF_StgHeader+OFFSET_" str " + WDS(__ix__)]\n");
140 #define thunk_payload_macro(str) \
141 printf("#define " str "(__ptr__,__ix__) W_[__ptr__+SIZEOF_StgThunkHeader+OFFSET_" str " + WDS(__ix__)]\n");
143 #define closure_payload(s_type,field) \
144 closure_field_offset_(str(s_type,field),s_type,field); \
145 closure_payload_macro(str(s_type,field));
147 #define thunk_payload(s_type,field) \
148 thunk_field_offset_(str(s_type,field),s_type,field); \
149 thunk_payload_macro(str(s_type,field));
151 /* Byte offset and MachRep for a closure field, minus the header */
152 #define closure_field(s_type, field) \
153 closure_field_offset(s_type,field) \
154 field_type(s_type, field); \
155 closure_field_macro(str(s_type,field))
157 #define thunk_field(s_type, field) \
158 thunk_field_offset(s_type,field) \
159 field_type(s_type, field); \
160 thunk_field_macro(str(s_type,field))
162 /* Byte offset and MachRep for a closure field, minus the header */
163 #define closure_field_(str, s_type, field) \
164 closure_field_offset_(str,s_type,field) \
165 field_type_(str, s_type, field); \
166 closure_field_macro(str)
168 #define thunk_field_(str, s_type, field) \
169 thunk_field_offset_(str,s_type,field) \
170 field_type_(str, s_type, field); \
171 thunk_field_macro(str)
173 /* Byte offset for a TSO field, minus the header and variable prof bit. */
174 #define tso_payload_offset(s_type, field) \
175 def_offset(str(s_type,field), OFFSET(s_type,field) - sizeof(StgHeader) - sizeof(StgTSOProfInfo));
177 /* Full byte offset for a TSO field, for use from Cmm */
178 #define tso_field_offset_macro(str) \
179 printf("#define TSO_OFFSET_" str " (SIZEOF_StgHeader+SIZEOF_OPT_StgTSOProfInfo+SIZEOF_OPT_StgTSOParInfo+SIZEOF_OPT_StgTSOGranInfo+SIZEOF_OPT_StgTSODistInfo+OFFSET_" str ")\n");
181 #define tso_field_offset(s_type, field) \
182 tso_payload_offset(s_type, field); \
183 tso_field_offset_macro(str(s_type,field));
185 #define tso_field_macro(str) \
186 printf("#define " str "(__ptr__) REP_" str "[__ptr__+TSO_OFFSET_" str "]\n")
187 #define tso_field(s_type, field) \
188 tso_payload_offset(s_type, field); \
189 field_type(s_type, field); \
190 tso_field_offset(s_type,field); \
191 tso_field_macro(str(s_type,field))
193 #define opt_struct_size(s_type, option) \
194 printf("#ifdef " #option "\n"); \
195 printf("#define SIZEOF_OPT_" #s_type " SIZEOF_" #s_type "\n"); \
197 printf("#define SIZEOF_OPT_" #s_type " 0\n"); \
198 printf("#endif\n\n");
200 #define FUN_OFFSET(sym) (OFFSET(Capability,f.sym) - OFFSET(Capability,r))
204 main(int argc, char *argv[])
207 printf("/* This file is created automatically. Do not edit by hand.*/\n\n");
209 printf("#define STD_HDR_SIZE %d\n", sizeofW(StgHeader) - sizeofW(StgProfHeader));
210 /* grrr.. PROFILING is on so we need to subtract sizeofW(StgProfHeader) */
211 printf("#define PROF_HDR_SIZE %d\n", sizeofW(StgProfHeader));
212 printf("#define GRAN_HDR_SIZE %d\n", sizeofW(StgGranHeader));
214 printf("#define STD_ITBL_SIZE %d\n", sizeofW(StgInfoTable));
215 printf("#define RET_ITBL_SIZE %d\n", sizeofW(StgRetInfoTable) - sizeofW(StgInfoTable));
216 printf("#define PROF_ITBL_SIZE %d\n", sizeofW(StgProfInfo));
218 printf("#define GRAN_ITBL_SIZE %d\n", 0);
219 printf("#define TICKY_ITBL_SIZE %d\n", 0);
221 printf("#define BLOCK_SIZE %d\n", BLOCK_SIZE);
222 printf("#define MBLOCK_SIZE %d\n", MBLOCK_SIZE);
227 field_offset(StgRegTable, rR1);
228 field_offset(StgRegTable, rR2);
229 field_offset(StgRegTable, rR3);
230 field_offset(StgRegTable, rR4);
231 field_offset(StgRegTable, rR5);
232 field_offset(StgRegTable, rR6);
233 field_offset(StgRegTable, rR7);
234 field_offset(StgRegTable, rR8);
235 field_offset(StgRegTable, rR9);
236 field_offset(StgRegTable, rR10);
237 field_offset(StgRegTable, rF1);
238 field_offset(StgRegTable, rF2);
239 field_offset(StgRegTable, rF3);
240 field_offset(StgRegTable, rF4);
241 field_offset(StgRegTable, rD1);
242 field_offset(StgRegTable, rD2);
243 field_offset(StgRegTable, rL1);
244 field_offset(StgRegTable, rSp);
245 field_offset(StgRegTable, rSpLim);
246 field_offset(StgRegTable, rHp);
247 field_offset(StgRegTable, rHpLim);
248 field_offset(StgRegTable, rCurrentTSO);
249 field_offset(StgRegTable, rCurrentNursery);
250 field_offset(StgRegTable, rHpAlloc);
251 struct_field(StgRegTable, rRet);
253 // Needed for SMP builds
254 field_offset(StgRegTable, rmp_tmp_w);
255 field_offset(StgRegTable, rmp_tmp1);
256 field_offset(StgRegTable, rmp_tmp2);
257 field_offset(StgRegTable, rmp_result1);
258 field_offset(StgRegTable, rmp_result2);
260 def_offset("stgGCEnter1", FUN_OFFSET(stgGCEnter1));
261 def_offset("stgGCFun", FUN_OFFSET(stgGCFun));
263 field_offset(Capability, r);
265 struct_field(bdescr, start);
266 struct_field(bdescr, free);
267 struct_field(bdescr, blocks);
268 struct_field(bdescr, gen_no);
269 struct_field(bdescr, link);
271 struct_size(generation);
272 struct_field(generation, mut_list);
274 struct_size(CostCentreStack);
275 struct_field(CostCentreStack, ccsID);
276 struct_field(CostCentreStack, mem_alloc);
277 struct_field(CostCentreStack, scc_count);
278 struct_field(CostCentreStack, prevStack);
280 struct_field(CostCentre, ccID);
281 struct_field(CostCentre, link);
283 struct_field(StgHeader, info);
284 struct_field_("StgHeader_ccs", StgHeader, prof.ccs);
285 struct_field_("StgHeader_ldvw", StgHeader, prof.hp.ldvw);
287 struct_size(StgSMPThunkHeader);
289 closure_payload(StgClosure,payload);
291 struct_field(StgEntCounter, allocs);
292 struct_field(StgEntCounter, registeredp);
293 struct_field(StgEntCounter, link);
295 closure_size(StgUpdateFrame);
296 closure_size(StgCatchFrame);
297 closure_size(StgStopFrame);
299 closure_size(StgMutArrPtrs);
300 closure_field(StgMutArrPtrs, ptrs);
302 closure_size(StgArrWords);
303 closure_field(StgArrWords, words);
304 closure_payload(StgArrWords, payload);
306 closure_field(StgTSO, link);
307 closure_field(StgTSO, global_link);
308 closure_field(StgTSO, what_next);
309 closure_field(StgTSO, why_blocked);
310 closure_field(StgTSO, block_info);
311 closure_field(StgTSO, blocked_exceptions);
312 closure_field(StgTSO, id);
313 closure_field(StgTSO, saved_errno);
314 closure_field(StgTSO, trec);
315 closure_field_("StgTSO_CCCS", StgTSO, prof.CCCS);
316 tso_field(StgTSO, sp);
317 tso_field_offset(StgTSO, stack);
318 tso_field(StgTSO, stack_size);
320 struct_size(StgTSOProfInfo);
321 struct_size(StgTSOParInfo);
322 struct_size(StgTSOGranInfo);
323 struct_size(StgTSODistInfo);
325 opt_struct_size(StgTSOProfInfo,PROFILING);
326 opt_struct_size(StgTSOParInfo,PAR);
327 opt_struct_size(StgTSOGranInfo,GRAN);
328 opt_struct_size(StgTSODistInfo,DIST);
330 closure_field(StgUpdateFrame, updatee);
332 closure_field(StgCatchFrame, handler);
333 closure_field(StgCatchFrame, exceptions_blocked);
335 closure_size(StgPAP);
336 closure_field(StgPAP, n_args);
337 closure_field(StgPAP, fun);
338 closure_field(StgPAP, arity);
339 closure_payload(StgPAP, payload);
342 closure_field(StgAP, n_args);
343 closure_field(StgAP, fun);
344 closure_payload(StgAP, payload);
346 thunk_size(StgAP_STACK);
347 thunk_field(StgAP_STACK, size);
348 thunk_field(StgAP_STACK, fun);
349 thunk_payload(StgAP_STACK, payload);
351 closure_field(StgInd, indirectee);
353 closure_size(StgMutVar);
354 closure_field(StgMutVar, var);
356 closure_size(StgAtomicallyFrame);
357 closure_field(StgAtomicallyFrame, code);
359 closure_size(StgCatchSTMFrame);
360 closure_field(StgCatchSTMFrame, handler);
362 closure_size(StgCatchRetryFrame);
363 closure_field(StgCatchRetryFrame, running_alt_code);
364 closure_field(StgCatchRetryFrame, first_code);
365 closure_field(StgCatchRetryFrame, alt_code);
366 closure_field(StgCatchRetryFrame, first_code_trec);
368 closure_size(StgWeak);
369 closure_field(StgWeak,link);
370 closure_field(StgWeak,key);
371 closure_field(StgWeak,value);
372 closure_field(StgWeak,finalizer);
374 closure_size(StgDeadWeak);
375 closure_field(StgDeadWeak,link);
377 closure_size(StgMVar);
378 closure_field(StgMVar,head);
379 closure_field(StgMVar,tail);
380 closure_field(StgMVar,value);
382 closure_size(StgBCO);
383 closure_field(StgBCO, instrs);
384 closure_field(StgBCO, literals);
385 closure_field(StgBCO, ptrs);
386 closure_field(StgBCO, itbls);
387 closure_field(StgBCO, arity);
388 closure_field(StgBCO, size);
389 closure_payload(StgBCO, bitmap);
391 closure_size(StgStableName);
392 closure_field(StgStableName,sn);
394 struct_field_("RtsFlags_ProfFlags_showCCSOnException",
395 RTS_FLAGS, ProfFlags.showCCSOnException);
396 struct_field_("RtsFlags_DebugFlags_apply",
397 RTS_FLAGS, DebugFlags.apply);
398 struct_field_("RtsFlags_DebugFlags_sanity",
399 RTS_FLAGS, DebugFlags.sanity);
400 struct_field_("RtsFlags_DebugFlags_weak",
401 RTS_FLAGS, DebugFlags.weak);
402 struct_field_("RtsFlags_GcFlags_initialStkSize",
403 RTS_FLAGS, GcFlags.initialStkSize);
405 struct_size(StgFunInfoExtraFwd);
406 struct_field(StgFunInfoExtraFwd, slow_apply);
407 struct_field(StgFunInfoExtraFwd, fun_type);
408 struct_field(StgFunInfoExtraFwd, arity);
409 struct_field_("StgFunInfoExtraFwd_bitmap", StgFunInfoExtraFwd, b.bitmap);
411 struct_size(StgFunInfoExtraRev);
412 struct_field(StgFunInfoExtraRev, slow_apply_offset);
413 struct_field(StgFunInfoExtraRev, fun_type);
414 struct_field(StgFunInfoExtraRev, arity);
415 struct_field_("StgFunInfoExtraRev_bitmap", StgFunInfoExtraRev, b.bitmap);
417 struct_field(StgLargeBitmap, size);
418 field_offset(StgLargeBitmap, bitmap);
420 struct_size(snEntry);
421 struct_field(snEntry,sn_obj);
422 struct_field(snEntry,addr);
424 #ifdef mingw32_HOST_OS
425 struct_size(StgAsyncIOResult);
426 struct_field(StgAsyncIOResult, reqID);
427 struct_field(StgAsyncIOResult, len);
428 struct_field(StgAsyncIOResult, errCode);
432 struct_field(MP_INT,_mp_alloc);
433 struct_field(MP_INT,_mp_size);
434 struct_field(MP_INT,_mp_d);