[project @ 2005-10-12 12:56:30 by simonmar]
[ghc-hetmet.git] / ghc / 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
22 #include "Rts.h"
23 #include "RtsFlags.h"
24 #include "Storage.h"
25
26 #include <stdio.h>
27
28 #define str(a,b) #a "_" #b
29
30 #define OFFSET(s_type, field) ((unsigned int)&(((s_type*)0)->field))
31
32 #if defined(GEN_HASKELL)
33 #define def_offset(str, offset) \
34     printf("oFFSET_" str " = %d::Int\n", offset);
35 #else
36 #define def_offset(str, offset) \
37     printf("#define OFFSET_" str " %d\n", offset);
38 #endif
39
40 #if defined(GEN_HASKELL)
41 #define ctype(type) /* nothing */
42 #else
43 #define ctype(type) \
44     printf("#define SIZEOF_" #type " %d\n", sizeof(type)); 
45 #endif
46
47 #if defined(GEN_HASKELL)
48 #define field_type_(str, s_type, field) /* nothing */
49 #else
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);
53 #endif
54
55 #define field_type(s_type, field) \
56     field_type_(str(s_type,field),s_type,field);
57
58 #define field_offset_(str, s_type, field) \
59     def_offset(str, OFFSET(s_type,field));
60
61 #define field_offset(s_type, field) \
62     field_offset_(str(s_type,field),s_type,field);
63
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");
67
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))
73
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)
78
79 #if defined(GEN_HASKELL)
80 #define def_size(str, size) \
81     printf("sIZEOF_" str " = %d::Int\n", size);
82 #else
83 #define def_size(str, size) \
84     printf("#define SIZEOF_" str " %d\n", size);
85 #endif
86
87 #if defined(GEN_HASKELL)
88 #define def_closure_size(str, size) /* nothing */
89 #else
90 #define def_closure_size(str, size) \
91     printf("#define SIZEOF_" str " (SIZEOF_StgHeader+%d)\n", size);
92 #endif
93
94 #if defined(GEN_HASKELL)
95 #define def_thunk_size(str, size) /* nothing */
96 #else
97 #define def_thunk_size(str, size) \
98     printf("#define SIZEOF_" str " (SIZEOF_StgThunkHeader+%d)\n", size);
99 #endif
100
101 #define struct_size(s_type) \
102     def_size(#s_type, sizeof(s_type));
103
104 /*
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.
107  */
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));
111
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));
115
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");
119
120 #define thunk_field_macro(str) \
121     printf("#define " str "(__ptr__)  REP_" str "[__ptr__+SIZEOF_StgThunkHeader+OFFSET_" str "]\n");
122
123 #define closure_field_offset_(str, s_type,field) \
124     def_offset(str, OFFSET(s_type,field) - sizeof(StgHeader));
125
126 #define thunk_field_offset_(str, s_type, field) \
127     closure_field_offset_(str, s_type, field)
128
129 #define closure_field_offset(s_type,field) \
130     closure_field_offset_(str(s_type,field),s_type,field)
131
132 #define thunk_field_offset(s_type,field) \
133     thunk_field_offset_(str(s_type,field),s_type,field)
134
135 #define closure_payload_macro(str) \
136     printf("#define " str "(__ptr__,__ix__)  W_[__ptr__+SIZEOF_StgHeader+OFFSET_" str " + WDS(__ix__)]\n");
137
138 #define thunk_payload_macro(str) \
139     printf("#define " str "(__ptr__,__ix__)  W_[__ptr__+SIZEOF_StgThunkHeader+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 #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));
148
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))
154
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))
159
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)
165
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)
170 /*
171  * Byte offset and MachRep for a TSO field, minus the header and
172  * variable prof bit.
173  */
174 #define tso_offset(s_type, field) \
175     def_offset(str(s_type,field), OFFSET(s_type,field) - sizeof(StgHeader) - sizeof(StgTSOProfInfo));
176
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");
179
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))
184
185 #define opt_struct_size(s_type, option)                                 \
186     printf("#ifdef " #option "\n");                                     \
187     printf("#define SIZEOF_OPT_" #s_type " SIZEOF_" #s_type "\n");      \
188     printf("#else\n");                                                  \
189     printf("#define SIZEOF_OPT_" #s_type " 0\n");                       \
190     printf("#endif\n\n");
191
192 #define FUN_OFFSET(sym) (OFFSET(Capability,f.sym) - OFFSET(Capability,r))
193
194
195 int
196 main(int argc, char *argv[])
197 {
198 #ifndef GEN_HASKELL
199     printf("/* This file is created automatically.  Do not edit by hand.*/\n\n");
200
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));
205
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));
209
210     printf("#define GRAN_ITBL_SIZE  %d\n", 0);
211     printf("#define TICKY_ITBL_SIZE %d\n", 0);
212
213     printf("#define BLOCK_SIZE   %d\n", BLOCK_SIZE);
214     printf("#define MBLOCK_SIZE   %d\n", MBLOCK_SIZE);  
215
216     printf("\n\n");
217 #endif
218
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);
243
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);
250
251     def_offset("stgGCEnter1", FUN_OFFSET(stgGCEnter1));
252     def_offset("stgGCFun", FUN_OFFSET(stgGCFun));
253
254     field_offset(Capability, r);
255
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);
261
262     struct_size(generation);
263     struct_field(generation, mut_list);
264
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);
270
271     struct_field(CostCentre, ccID);
272     struct_field(CostCentre, link);
273
274     struct_field(StgHeader, info);
275     struct_field_("StgHeader_ccs",  StgHeader, prof.ccs);
276     struct_field_("StgHeader_ldvw", StgHeader, prof.hp.ldvw);
277
278     struct_size(StgSMPThunkHeader);
279
280     closure_payload(StgClosure,payload);
281
282     struct_field(StgEntCounter, allocs);
283     struct_field(StgEntCounter, registeredp);
284     struct_field(StgEntCounter, link);
285     
286     closure_size(StgUpdateFrame);
287     closure_size(StgCatchFrame);
288     closure_size(StgStopFrame);
289
290     closure_size(StgMutArrPtrs);
291     closure_field(StgMutArrPtrs, ptrs);
292
293     closure_size(StgArrWords);
294     closure_field(StgArrWords, words);
295     closure_payload(StgArrWords, payload);
296
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);
310
311     struct_size(StgTSOProfInfo);
312     struct_size(StgTSOParInfo);
313     struct_size(StgTSOGranInfo);
314     struct_size(StgTSODistInfo);
315
316     opt_struct_size(StgTSOProfInfo,PROFILING);
317     opt_struct_size(StgTSOParInfo,PAR);
318     opt_struct_size(StgTSOGranInfo,GRAN);
319     opt_struct_size(StgTSODistInfo,DIST);
320
321     closure_field(StgUpdateFrame, updatee);
322
323     closure_field(StgCatchFrame, handler);
324     closure_field(StgCatchFrame, exceptions_blocked);
325
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);
331
332     closure_size(StgAP);
333     closure_field(StgAP, n_args);
334     closure_field(StgAP, fun);
335     closure_payload(StgAP, payload);
336
337     thunk_size(StgAP_STACK);
338     thunk_field(StgAP_STACK, size);
339     thunk_field(StgAP_STACK, fun);
340     thunk_payload(StgAP_STACK, payload);
341
342     closure_field(StgInd, indirectee);
343
344     closure_size(StgMutVar);
345     closure_field(StgMutVar, var);
346
347     closure_size(StgAtomicallyFrame);
348     closure_field(StgAtomicallyFrame, waiting);
349     closure_field(StgAtomicallyFrame, code);
350
351     closure_size(StgCatchSTMFrame);
352     closure_field(StgCatchSTMFrame, handler);
353
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);
359
360     closure_size(StgWeak);
361     closure_field(StgWeak,link);
362     closure_field(StgWeak,key);
363     closure_field(StgWeak,value);
364     closure_field(StgWeak,finalizer);
365
366     closure_size(StgDeadWeak);
367     closure_field(StgDeadWeak,link);
368
369     closure_size(StgMVar);
370     closure_field(StgMVar,head);
371     closure_field(StgMVar,tail);
372     closure_field(StgMVar,value);
373
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);
382
383     closure_size(StgStableName);
384     closure_field(StgStableName,sn);
385
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);
396
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);
402
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);
408
409     struct_field(StgLargeBitmap, size);
410     field_offset(StgLargeBitmap, bitmap);
411
412     struct_size(snEntry);
413     struct_field(snEntry,sn_obj);
414     struct_field(snEntry,addr);
415
416 #ifdef mingw32_HOST_OS
417     struct_size(StgAsyncIOResult);
418     struct_field(StgAsyncIOResult, reqID);
419     struct_field(StgAsyncIOResult, len);
420     struct_field(StgAsyncIOResult, errCode);
421 #endif
422
423     struct_size(MP_INT);
424     struct_field(MP_INT,_mp_alloc);
425     struct_field(MP_INT,_mp_size);
426     struct_field(MP_INT,_mp_d);
427
428     ctype(mp_limb_t);
429     return 0;
430 }