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