allocateLocal was renamed to allocate
[packages/integer-gmp.git] / cbits / mkGmpDerivedConstants.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 #include <stdio.h>
15 #include "gmp.h"
16
17
18 #define str(a,b) #a "_" #b
19
20 #define OFFSET(s_type, field) ((size_t)&(((s_type*)0)->field))
21
22 /* struct_size(TYPE)
23 *
24 */
25 #define def_size(str, size) \
26 printf("#define SIZEOF_" str " %lu\n", (unsigned long)size);
27
28 #define struct_size(s_type) \
29 def_size(#s_type, sizeof(s_type));
30
31
32
33 /* struct_field(TYPE, FIELD)
34 *
35 */
36 #define def_offset(str, offset) \
37 printf("#define OFFSET_" str " %d\n", (int)(offset));
38
39 #define field_offset_(str, s_type, field) \
40 def_offset(str, OFFSET(s_type,field));
41
42 #define field_offset(s_type, field) \
43 field_offset_(str(s_type,field),s_type,field);
44
45 #define field_type_(str, s_type, field) \
46 printf("#define REP_" str " b"); \
47 printf("%lu\n", (unsigned long)sizeof (__typeof__(((((s_type*)0)->field)))) * 8);
48
49 #define field_type(s_type, field) \
50 field_type_(str(s_type,field),s_type,field);
51
52 /* An access macro for use in C-- sources. */
53 #define struct_field_macro(str) \
54 printf("#define " str "(__ptr__) REP_" str "[__ptr__+OFFSET_" str "]\n");
55
56 /* Outputs the byte offset and MachRep for a field */
57 #define struct_field(s_type, field) \
58 field_offset(s_type, field); \
59 field_type(s_type, field); \
60 struct_field_macro(str(s_type,field))
61
62
63 int
64 main(int argc, char *argv[])
65 {
66 printf("/* This file is created automatically. Do not edit by hand.*/\n\n");
67
68 struct_size(MP_INT);
69 struct_field(MP_INT,_mp_alloc);
70 struct_field(MP_INT,_mp_size);
71 struct_field(MP_INT,_mp_d);
72
73 return 0;
74 }