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.
28 #define str(a,b) #a "_" #b
30 #define OFFSET(s_type, field) ((unsigned int)&(((s_type*)0)->field))
32 #if defined(GEN_HASKELL)
33 #define def_offset(str, offset) \
34 printf("oFFSET_" str " = %d::Int\n", offset);
36 #define def_offset(str, offset) \
37 printf("#define OFFSET_" str " %d\n", offset);
40 #if defined(GEN_HASKELL)
41 #define ctype(type) /* nothing */
44 printf("#define SIZEOF_" #type " %d\n", sizeof(type));
47 #if defined(GEN_HASKELL)
48 #define field_type_(str, s_type, field) /* nothing */
50 #define field_type_(str, s_type, field) \
51 printf("#define REP_" str " I"); \
52 printf("%d\n", sizeof (__typeof__(((((s_type*)0)->field)))) * 8);
55 #define field_type(s_type, field) \
56 field_type_(str(s_type,field),s_type,field);
58 #define field_offset_(str, s_type, field) \
59 def_offset(str, OFFSET(s_type,field));
61 #define field_offset(s_type, field) \
62 field_offset_(str(s_type,field),s_type,field);
64 /* An access macro for use in C-- sources. */
65 #define struct_field_macro(str) \
66 printf("#define " str "(__ptr__) REP_" str "[__ptr__+OFFSET_" str "]\n");
68 /* Outputs the byte offset and MachRep for a field */
69 #define struct_field(s_type, field) \
70 field_offset(s_type, field); \
71 field_type(s_type, field); \
72 struct_field_macro(str(s_type,field))
74 #define struct_field_(str, s_type, field) \
75 field_offset_(str, s_type, field); \
76 field_type_(str, s_type, field); \
77 struct_field_macro(str)
79 #if defined(GEN_HASKELL)
80 #define def_size(str, size) \
81 printf("sIZEOF_" str " = %d::Int\n", size);
83 #define def_size(str, size) \
84 printf("#define SIZEOF_" str " %d\n", size);
87 #if defined(GEN_HASKELL)
88 #define def_closure_size(str, size) /* nothing */
90 #define def_closure_size(str, size) \
91 printf("#define SIZEOF_" str " (SIZEOF_StgHeader+%d)\n", size);
94 #if defined(GEN_HASKELL)
95 #define def_thunk_size(str, size) /* nothing */
97 #define def_thunk_size(str, size) \
98 printf("#define SIZEOF_" str " (SIZEOF_StgThunkHeader+%d)\n", size);
101 #define struct_size(s_type) \
102 def_size(#s_type, sizeof(s_type));
105 * Size of a closure type, minus the header, named SIZEOF_<type>_NoHdr
106 * Also, we #define SIZEOF_<type> to be the size of the whole closure for .cmm.
108 #define closure_size(s_type) \
109 def_size(#s_type "_NoHdr", sizeof(s_type) - sizeof(StgHeader)); \
110 def_closure_size(#s_type, sizeof(s_type) - sizeof(StgHeader));
112 #define thunk_size(s_type) \
113 def_size(#s_type "_NoHdr", sizeof(s_type) - sizeof(StgHeader)); \
114 def_thunk_size(#s_type, sizeof(s_type) - sizeof(StgHeader));
116 /* An access macro for use in C-- sources. */
117 #define closure_field_macro(str) \
118 printf("#define " str "(__ptr__) REP_" str "[__ptr__+SIZEOF_StgHeader+OFFSET_" str "]\n");
120 #define thunk_field_macro(str) \
121 printf("#define " str "(__ptr__) REP_" str "[__ptr__+SIZEOF_StgThunkHeader+OFFSET_" str "]\n");
123 #define closure_field_offset_(str, s_type,field) \
124 def_offset(str, OFFSET(s_type,field) - sizeof(StgHeader));
126 #define thunk_field_offset_(str, s_type, field) \
127 closure_field_offset_(str, s_type, field)
129 #define closure_field_offset(s_type,field) \
130 closure_field_offset_(str(s_type,field),s_type,field)
132 #define thunk_field_offset(s_type,field) \
133 thunk_field_offset_(str(s_type,field),s_type,field)
135 #define closure_payload_macro(str) \
136 printf("#define " str "(__ptr__,__ix__) W_[__ptr__+SIZEOF_StgHeader+OFFSET_" str " + WDS(__ix__)]\n");
138 #define thunk_payload_macro(str) \
139 printf("#define " str "(__ptr__,__ix__) W_[__ptr__+SIZEOF_StgThunkHeader+OFFSET_" str " + WDS(__ix__)]\n");
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));
145 #define thunk_payload(s_type,field) \
146 thunk_field_offset_(str(s_type,field),s_type,field); \
147 thunk_payload_macro(str(s_type,field));
149 /* Byte offset and MachRep for a closure field, minus the header */
150 #define closure_field(s_type, field) \
151 closure_field_offset(s_type,field) \
152 field_type(s_type, field); \
153 closure_field_macro(str(s_type,field))
155 #define thunk_field(s_type, field) \
156 thunk_field_offset(s_type,field) \
157 field_type(s_type, field); \
158 thunk_field_macro(str(s_type,field))
160 /* Byte offset and MachRep for a closure field, minus the header */
161 #define closure_field_(str, s_type, field) \
162 closure_field_offset_(str,s_type,field) \
163 field_type_(str, s_type, field); \
164 closure_field_macro(str)
166 #define thunk_field_(str, s_type, field) \
167 thunk_field_offset_(str,s_type,field) \
168 field_type_(str, s_type, field); \
169 thunk_field_macro(str)
171 * Byte offset and MachRep for a TSO field, minus the header and
174 #define tso_offset(s_type, field) \
175 def_offset(str(s_type,field), OFFSET(s_type,field) - sizeof(StgHeader) - sizeof(StgTSOProfInfo));
177 #define tso_field_macro(str) \
178 printf("#define " str "(__ptr__) REP_" str "[__ptr__+SIZEOF_StgHeader+SIZEOF_OPT_StgTSOProfInfo+SIZEOF_OPT_StgTSOParInfo+SIZEOF_OPT_StgTSOGranInfo+SIZEOF_OPT_StgTSODistInfo+OFFSET_" str "]\n");
180 #define tso_field(s_type, field) \
181 tso_offset(s_type, field); \
182 field_type(s_type, field); \
183 tso_field_macro(str(s_type,field))
185 #define opt_struct_size(s_type, option) \
186 printf("#ifdef " #option "\n"); \
187 printf("#define SIZEOF_OPT_" #s_type " SIZEOF_" #s_type "\n"); \
189 printf("#define SIZEOF_OPT_" #s_type " 0\n"); \
190 printf("#endif\n\n");
192 #define FUN_OFFSET(sym) (OFFSET(Capability,f.sym) - OFFSET(Capability,r))
196 main(int argc, char *argv[])
199 printf("/* This file is created automatically. Do not edit by hand.*/\n\n");
201 printf("#define STD_HDR_SIZE %d\n", sizeofW(StgHeader) - sizeofW(StgProfHeader));
202 /* grrr.. PROFILING is on so we need to subtract sizeofW(StgProfHeader) */
203 printf("#define PROF_HDR_SIZE %d\n", sizeofW(StgProfHeader));
204 printf("#define GRAN_HDR_SIZE %d\n", sizeofW(StgGranHeader));
206 printf("#define STD_ITBL_SIZE %d\n", sizeofW(StgInfoTable));
207 printf("#define RET_ITBL_SIZE %d\n", sizeofW(StgRetInfoTable) - sizeofW(StgInfoTable));
208 printf("#define PROF_ITBL_SIZE %d\n", sizeofW(StgProfInfo));
210 printf("#define GRAN_ITBL_SIZE %d\n", 0);
211 printf("#define TICKY_ITBL_SIZE %d\n", 0);
213 printf("#define BLOCK_SIZE %d\n", BLOCK_SIZE);
214 printf("#define MBLOCK_SIZE %d\n", MBLOCK_SIZE);
219 field_offset(StgRegTable, rR1);
220 field_offset(StgRegTable, rR2);
221 field_offset(StgRegTable, rR3);
222 field_offset(StgRegTable, rR4);
223 field_offset(StgRegTable, rR5);
224 field_offset(StgRegTable, rR6);
225 field_offset(StgRegTable, rR7);
226 field_offset(StgRegTable, rR8);
227 field_offset(StgRegTable, rR9);
228 field_offset(StgRegTable, rR10);
229 field_offset(StgRegTable, rF1);
230 field_offset(StgRegTable, rF2);
231 field_offset(StgRegTable, rF3);
232 field_offset(StgRegTable, rF4);
233 field_offset(StgRegTable, rD1);
234 field_offset(StgRegTable, rD2);
235 field_offset(StgRegTable, rL1);
236 field_offset(StgRegTable, rSp);
237 field_offset(StgRegTable, rSpLim);
238 field_offset(StgRegTable, rHp);
239 field_offset(StgRegTable, rHpLim);
240 field_offset(StgRegTable, rCurrentTSO);
241 field_offset(StgRegTable, rCurrentNursery);
242 field_offset(StgRegTable, rHpAlloc);
244 // Needed for SMP builds
245 field_offset(StgRegTable, rmp_tmp_w);
246 field_offset(StgRegTable, rmp_tmp1);
247 field_offset(StgRegTable, rmp_tmp2);
248 field_offset(StgRegTable, rmp_result1);
249 field_offset(StgRegTable, rmp_result2);
251 def_offset("stgGCEnter1", FUN_OFFSET(stgGCEnter1));
252 def_offset("stgGCFun", FUN_OFFSET(stgGCFun));
254 field_offset(Capability, r);
256 struct_field(bdescr, start);
257 struct_field(bdescr, free);
258 struct_field(bdescr, blocks);
259 struct_field(bdescr, gen_no);
260 struct_field(bdescr, link);
262 struct_size(generation);
263 struct_field(generation, mut_list);
265 struct_size(CostCentreStack);
266 struct_field(CostCentreStack, ccsID);
267 struct_field(CostCentreStack, mem_alloc);
268 struct_field(CostCentreStack, scc_count);
269 struct_field(CostCentreStack, prevStack);
271 struct_field(CostCentre, ccID);
272 struct_field(CostCentre, link);
274 struct_field(StgHeader, info);
275 struct_field_("StgHeader_ccs", StgHeader, prof.ccs);
276 struct_field_("StgHeader_ldvw", StgHeader, prof.hp.ldvw);
278 struct_size(StgSMPThunkHeader);
280 closure_payload(StgClosure,payload);
282 struct_field(StgEntCounter, allocs);
283 struct_field(StgEntCounter, registeredp);
284 struct_field(StgEntCounter, link);
286 closure_size(StgUpdateFrame);
287 closure_size(StgCatchFrame);
288 closure_size(StgStopFrame);
290 closure_size(StgMutArrPtrs);
291 closure_field(StgMutArrPtrs, ptrs);
293 closure_size(StgArrWords);
294 closure_field(StgArrWords, words);
295 closure_payload(StgArrWords, payload);
297 closure_field(StgTSO, link);
298 closure_field(StgTSO, global_link);
299 closure_field(StgTSO, what_next);
300 closure_field(StgTSO, why_blocked);
301 closure_field(StgTSO, block_info);
302 closure_field(StgTSO, blocked_exceptions);
303 closure_field(StgTSO, id);
304 closure_field(StgTSO, saved_errno);
305 closure_field(StgTSO, trec);
306 closure_field_("StgTSO_CCCS", StgTSO, prof.CCCS);
307 tso_field(StgTSO, sp);
308 tso_offset(StgTSO, stack);
309 tso_field(StgTSO, stack_size);
311 struct_size(StgTSOProfInfo);
312 struct_size(StgTSOParInfo);
313 struct_size(StgTSOGranInfo);
314 struct_size(StgTSODistInfo);
316 opt_struct_size(StgTSOProfInfo,PROFILING);
317 opt_struct_size(StgTSOParInfo,PAR);
318 opt_struct_size(StgTSOGranInfo,GRAN);
319 opt_struct_size(StgTSODistInfo,DIST);
321 closure_field(StgUpdateFrame, updatee);
323 closure_field(StgCatchFrame, handler);
324 closure_field(StgCatchFrame, exceptions_blocked);
326 closure_size(StgPAP);
327 closure_field(StgPAP, n_args);
328 closure_field(StgPAP, fun);
329 closure_field(StgPAP, arity);
330 closure_payload(StgPAP, payload);
333 closure_field(StgAP, n_args);
334 closure_field(StgAP, fun);
335 closure_payload(StgAP, payload);
337 thunk_size(StgAP_STACK);
338 thunk_field(StgAP_STACK, size);
339 thunk_field(StgAP_STACK, fun);
340 thunk_payload(StgAP_STACK, payload);
342 closure_field(StgInd, indirectee);
344 closure_size(StgMutVar);
345 closure_field(StgMutVar, var);
347 closure_size(StgAtomicallyFrame);
348 closure_field(StgAtomicallyFrame, waiting);
349 closure_field(StgAtomicallyFrame, code);
351 closure_size(StgCatchSTMFrame);
352 closure_field(StgCatchSTMFrame, handler);
354 closure_size(StgCatchRetryFrame);
355 closure_field(StgCatchRetryFrame, running_alt_code);
356 closure_field(StgCatchRetryFrame, first_code);
357 closure_field(StgCatchRetryFrame, alt_code);
358 closure_field(StgCatchRetryFrame, first_code_trec);
360 closure_size(StgWeak);
361 closure_field(StgWeak,link);
362 closure_field(StgWeak,key);
363 closure_field(StgWeak,value);
364 closure_field(StgWeak,finalizer);
366 closure_size(StgDeadWeak);
367 closure_field(StgDeadWeak,link);
369 closure_size(StgMVar);
370 closure_field(StgMVar,head);
371 closure_field(StgMVar,tail);
372 closure_field(StgMVar,value);
374 closure_size(StgBCO);
375 closure_field(StgBCO, instrs);
376 closure_field(StgBCO, literals);
377 closure_field(StgBCO, ptrs);
378 closure_field(StgBCO, itbls);
379 closure_field(StgBCO, arity);
380 closure_field(StgBCO, size);
381 closure_payload(StgBCO, bitmap);
383 closure_size(StgStableName);
384 closure_field(StgStableName,sn);
386 struct_field_("RtsFlags_ProfFlags_showCCSOnException",
387 RTS_FLAGS, ProfFlags.showCCSOnException);
388 struct_field_("RtsFlags_DebugFlags_apply",
389 RTS_FLAGS, DebugFlags.apply);
390 struct_field_("RtsFlags_DebugFlags_sanity",
391 RTS_FLAGS, DebugFlags.sanity);
392 struct_field_("RtsFlags_DebugFlags_weak",
393 RTS_FLAGS, DebugFlags.weak);
394 struct_field_("RtsFlags_GcFlags_initialStkSize",
395 RTS_FLAGS, GcFlags.initialStkSize);
397 struct_size(StgFunInfoExtraFwd);
398 struct_field(StgFunInfoExtraFwd, slow_apply);
399 struct_field(StgFunInfoExtraFwd, fun_type);
400 struct_field(StgFunInfoExtraFwd, arity);
401 struct_field_("StgFunInfoExtraFwd_bitmap", StgFunInfoExtraFwd, b.bitmap);
403 struct_size(StgFunInfoExtraRev);
404 struct_field(StgFunInfoExtraRev, slow_apply_offset);
405 struct_field(StgFunInfoExtraRev, fun_type);
406 struct_field(StgFunInfoExtraRev, arity);
407 struct_field_("StgFunInfoExtraRev_bitmap", StgFunInfoExtraRev, b.bitmap);
409 struct_field(StgLargeBitmap, size);
410 field_offset(StgLargeBitmap, bitmap);
412 struct_size(snEntry);
413 struct_field(snEntry,sn_obj);
414 struct_field(snEntry,addr);
416 #ifdef mingw32_HOST_OS
417 struct_size(StgAsyncIOResult);
418 struct_field(StgAsyncIOResult, reqID);
419 struct_field(StgAsyncIOResult, len);
420 struct_field(StgAsyncIOResult, errCode);
424 struct_field(MP_INT,_mp_alloc);
425 struct_field(MP_INT,_mp_size);
426 struct_field(MP_INT,_mp_d);