f3d551daee1074fd6c8718501891a612af7721a1
[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 #ifdef mingw32_HOST_OS
23 #define SIZET_FMT "d"
24 #else
25 #define SIZET_FMT "zd"
26 #endif
27
28
29 /* struct_size(TYPE)
30 *
31 */
32 #define def_size(str, size) \
33 printf("#define SIZEOF_" str " %lu\n", (unsigned long)size);
34
35 #define struct_size(s_type) \
36 def_size(#s_type, sizeof(s_type));
37
38
39
40 /* struct_field(TYPE, FIELD)
41 *
42 */
43 #define def_offset(str, offset) \
44 printf("#define OFFSET_" str " %" SIZET_FMT "\n", offset);
45
46 #define field_offset_(str, s_type, field) \
47 def_offset(str, OFFSET(s_type,field));
48
49 #define field_offset(s_type, field) \
50 field_offset_(str(s_type,field),s_type,field);
51
52 #define field_type_(str, s_type, field) \
53 printf("#define REP_" str " b"); \
54 printf("%lu\n", (unsigned long)sizeof (__typeof__(((((s_type*)0)->field)))) * 8);
55
56 #define field_type(s_type, field) \
57 field_type_(str(s_type,field),s_type,field);
58
59 /* An access macro for use in C-- sources. */
60 #define struct_field_macro(str) \
61 printf("#define " str "(__ptr__) REP_" str "[__ptr__+OFFSET_" str "]\n");
62
63 /* Outputs the byte offset and MachRep for a field */
64 #define struct_field(s_type, field) \
65 field_offset(s_type, field); \
66 field_type(s_type, field); \
67 struct_field_macro(str(s_type,field))
68
69
70 int
71 main(int argc, char *argv[])
72 {
73 printf("/* This file is created automatically. Do not edit by hand.*/\n\n");
74
75 struct_size(MP_INT);
76 struct_field(MP_INT,_mp_alloc);
77 struct_field(MP_INT,_mp_size);
78 struct_field(MP_INT,_mp_d);
79
80 return 0;
81 }