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 and MachRep for a TSO field, minus the header and
176 #define tso_offset(s_type, field) \
177 def_offset(str(s_type,field), OFFSET(s_type,field) - sizeof(StgHeader) - sizeof(StgTSOProfInfo));
179 #define tso_field_macro(str) \
180 printf("#define " str "(__ptr__) REP_" str "[__ptr__+SIZEOF_StgHeader+SIZEOF_OPT_StgTSOProfInfo+SIZEOF_OPT_StgTSOParInfo+SIZEOF_OPT_StgTSOGranInfo+SIZEOF_OPT_StgTSODistInfo+OFFSET_" str "]\n");
182 #define tso_field(s_type, field) \
183 tso_offset(s_type, field); \
184 field_type(s_type, field); \
185 tso_field_macro(str(s_type,field))
187 #define opt_struct_size(s_type, option) \
188 printf("#ifdef " #option "\n"); \
189 printf("#define SIZEOF_OPT_" #s_type " SIZEOF_" #s_type "\n"); \
191 printf("#define SIZEOF_OPT_" #s_type " 0\n"); \
192 printf("#endif\n\n");
194 #define FUN_OFFSET(sym) (OFFSET(Capability,f.sym) - OFFSET(Capability,r))
198 main(int argc, char *argv[])
201 printf("/* This file is created automatically. Do not edit by hand.*/\n\n");
203 printf("#define STD_HDR_SIZE %d\n", sizeofW(StgHeader) - sizeofW(StgProfHeader));
204 /* grrr.. PROFILING is on so we need to subtract sizeofW(StgProfHeader) */
205 printf("#define PROF_HDR_SIZE %d\n", sizeofW(StgProfHeader));
206 printf("#define GRAN_HDR_SIZE %d\n", sizeofW(StgGranHeader));
208 printf("#define STD_ITBL_SIZE %d\n", sizeofW(StgInfoTable));
209 printf("#define RET_ITBL_SIZE %d\n", sizeofW(StgRetInfoTable) - sizeofW(StgInfoTable));
210 printf("#define PROF_ITBL_SIZE %d\n", sizeofW(StgProfInfo));
212 printf("#define GRAN_ITBL_SIZE %d\n", 0);
213 printf("#define TICKY_ITBL_SIZE %d\n", 0);
215 printf("#define BLOCK_SIZE %d\n", BLOCK_SIZE);
216 printf("#define MBLOCK_SIZE %d\n", MBLOCK_SIZE);
221 field_offset(StgRegTable, rR1);
222 field_offset(StgRegTable, rR2);
223 field_offset(StgRegTable, rR3);
224 field_offset(StgRegTable, rR4);
225 field_offset(StgRegTable, rR5);
226 field_offset(StgRegTable, rR6);
227 field_offset(StgRegTable, rR7);
228 field_offset(StgRegTable, rR8);
229 field_offset(StgRegTable, rR9);
230 field_offset(StgRegTable, rR10);
231 field_offset(StgRegTable, rF1);
232 field_offset(StgRegTable, rF2);
233 field_offset(StgRegTable, rF3);
234 field_offset(StgRegTable, rF4);
235 field_offset(StgRegTable, rD1);
236 field_offset(StgRegTable, rD2);
237 field_offset(StgRegTable, rL1);
238 field_offset(StgRegTable, rSp);
239 field_offset(StgRegTable, rSpLim);
240 field_offset(StgRegTable, rHp);
241 field_offset(StgRegTable, rHpLim);
242 field_offset(StgRegTable, rCurrentTSO);
243 field_offset(StgRegTable, rCurrentNursery);
244 field_offset(StgRegTable, rHpAlloc);
245 struct_field(StgRegTable, rRet);
247 // Needed for SMP builds
248 field_offset(StgRegTable, rmp_tmp_w);
249 field_offset(StgRegTable, rmp_tmp1);
250 field_offset(StgRegTable, rmp_tmp2);
251 field_offset(StgRegTable, rmp_result1);
252 field_offset(StgRegTable, rmp_result2);
254 def_offset("stgGCEnter1", FUN_OFFSET(stgGCEnter1));
255 def_offset("stgGCFun", FUN_OFFSET(stgGCFun));
257 field_offset(Capability, r);
259 struct_field(bdescr, start);
260 struct_field(bdescr, free);
261 struct_field(bdescr, blocks);
262 struct_field(bdescr, gen_no);
263 struct_field(bdescr, link);
265 struct_size(generation);
266 struct_field(generation, mut_list);
268 struct_size(CostCentreStack);
269 struct_field(CostCentreStack, ccsID);
270 struct_field(CostCentreStack, mem_alloc);
271 struct_field(CostCentreStack, scc_count);
272 struct_field(CostCentreStack, prevStack);
274 struct_field(CostCentre, ccID);
275 struct_field(CostCentre, link);
277 struct_field(StgHeader, info);
278 struct_field_("StgHeader_ccs", StgHeader, prof.ccs);
279 struct_field_("StgHeader_ldvw", StgHeader, prof.hp.ldvw);
281 struct_size(StgSMPThunkHeader);
283 closure_payload(StgClosure,payload);
285 struct_field(StgEntCounter, allocs);
286 struct_field(StgEntCounter, registeredp);
287 struct_field(StgEntCounter, link);
289 closure_size(StgUpdateFrame);
290 closure_size(StgCatchFrame);
291 closure_size(StgStopFrame);
293 closure_size(StgMutArrPtrs);
294 closure_field(StgMutArrPtrs, ptrs);
296 closure_size(StgArrWords);
297 closure_field(StgArrWords, words);
298 closure_payload(StgArrWords, payload);
300 closure_field(StgTSO, link);
301 closure_field(StgTSO, global_link);
302 closure_field(StgTSO, what_next);
303 closure_field(StgTSO, why_blocked);
304 closure_field(StgTSO, block_info);
305 closure_field(StgTSO, blocked_exceptions);
306 closure_field(StgTSO, id);
307 closure_field(StgTSO, saved_errno);
308 closure_field(StgTSO, trec);
309 closure_field_("StgTSO_CCCS", StgTSO, prof.CCCS);
310 tso_field(StgTSO, sp);
311 tso_offset(StgTSO, stack);
312 tso_field(StgTSO, stack_size);
314 struct_size(StgTSOProfInfo);
315 struct_size(StgTSOParInfo);
316 struct_size(StgTSOGranInfo);
317 struct_size(StgTSODistInfo);
319 opt_struct_size(StgTSOProfInfo,PROFILING);
320 opt_struct_size(StgTSOParInfo,PAR);
321 opt_struct_size(StgTSOGranInfo,GRAN);
322 opt_struct_size(StgTSODistInfo,DIST);
324 closure_field(StgUpdateFrame, updatee);
326 closure_field(StgCatchFrame, handler);
327 closure_field(StgCatchFrame, exceptions_blocked);
329 closure_size(StgPAP);
330 closure_field(StgPAP, n_args);
331 closure_field(StgPAP, fun);
332 closure_field(StgPAP, arity);
333 closure_payload(StgPAP, payload);
336 closure_field(StgAP, n_args);
337 closure_field(StgAP, fun);
338 closure_payload(StgAP, payload);
340 thunk_size(StgAP_STACK);
341 thunk_field(StgAP_STACK, size);
342 thunk_field(StgAP_STACK, fun);
343 thunk_payload(StgAP_STACK, payload);
345 closure_field(StgInd, indirectee);
347 closure_size(StgMutVar);
348 closure_field(StgMutVar, var);
350 closure_size(StgAtomicallyFrame);
351 closure_field(StgAtomicallyFrame, code);
353 closure_size(StgCatchSTMFrame);
354 closure_field(StgCatchSTMFrame, handler);
356 closure_size(StgCatchRetryFrame);
357 closure_field(StgCatchRetryFrame, running_alt_code);
358 closure_field(StgCatchRetryFrame, first_code);
359 closure_field(StgCatchRetryFrame, alt_code);
360 closure_field(StgCatchRetryFrame, first_code_trec);
362 closure_size(StgWeak);
363 closure_field(StgWeak,link);
364 closure_field(StgWeak,key);
365 closure_field(StgWeak,value);
366 closure_field(StgWeak,finalizer);
368 closure_size(StgDeadWeak);
369 closure_field(StgDeadWeak,link);
371 closure_size(StgMVar);
372 closure_field(StgMVar,head);
373 closure_field(StgMVar,tail);
374 closure_field(StgMVar,value);
376 closure_size(StgBCO);
377 closure_field(StgBCO, instrs);
378 closure_field(StgBCO, literals);
379 closure_field(StgBCO, ptrs);
380 closure_field(StgBCO, itbls);
381 closure_field(StgBCO, arity);
382 closure_field(StgBCO, size);
383 closure_payload(StgBCO, bitmap);
385 closure_size(StgStableName);
386 closure_field(StgStableName,sn);
388 struct_field_("RtsFlags_ProfFlags_showCCSOnException",
389 RTS_FLAGS, ProfFlags.showCCSOnException);
390 struct_field_("RtsFlags_DebugFlags_apply",
391 RTS_FLAGS, DebugFlags.apply);
392 struct_field_("RtsFlags_DebugFlags_sanity",
393 RTS_FLAGS, DebugFlags.sanity);
394 struct_field_("RtsFlags_DebugFlags_weak",
395 RTS_FLAGS, DebugFlags.weak);
396 struct_field_("RtsFlags_GcFlags_initialStkSize",
397 RTS_FLAGS, GcFlags.initialStkSize);
399 struct_size(StgFunInfoExtraFwd);
400 struct_field(StgFunInfoExtraFwd, slow_apply);
401 struct_field(StgFunInfoExtraFwd, fun_type);
402 struct_field(StgFunInfoExtraFwd, arity);
403 struct_field_("StgFunInfoExtraFwd_bitmap", StgFunInfoExtraFwd, b.bitmap);
405 struct_size(StgFunInfoExtraRev);
406 struct_field(StgFunInfoExtraRev, slow_apply_offset);
407 struct_field(StgFunInfoExtraRev, fun_type);
408 struct_field(StgFunInfoExtraRev, arity);
409 struct_field_("StgFunInfoExtraRev_bitmap", StgFunInfoExtraRev, b.bitmap);
411 struct_field(StgLargeBitmap, size);
412 field_offset(StgLargeBitmap, bitmap);
414 struct_size(snEntry);
415 struct_field(snEntry,sn_obj);
416 struct_field(snEntry,addr);
418 #ifdef mingw32_HOST_OS
419 struct_size(StgAsyncIOResult);
420 struct_field(StgAsyncIOResult, reqID);
421 struct_field(StgAsyncIOResult, len);
422 struct_field(StgAsyncIOResult, errCode);
426 struct_field(MP_INT,_mp_alloc);
427 struct_field(MP_INT,_mp_size);
428 struct_field(MP_INT,_mp_d);