Merge pull request #3381 from krytarowski/netbsd-support-20
[mono.git] / mono / metadata / metadata-cross-helpers.c
1 #include <stdio.h>
2
3 #include "config.h"
4 #include <mono/metadata/abi-details.h>
5
6 #include <mono/metadata/class-internals.h>
7 #include <mono/metadata/object-internals.h>
8 #include <mono/metadata/monitor.h>
9 #include <mono/metadata/handle.h>
10 #ifdef HAVE_SGEN_GC
11 #include <mono/sgen/sgen-gc.h>
12 #endif
13
14 static int
15 dump_arch (void)
16 {
17 #if defined (TARGET_X86)
18         g_print ("#ifdef TARGET_X86\n");
19 #elif defined (TARGET_AMD64)
20         g_print ("#ifdef TARGET_AMD64\n");
21 #elif defined (TARGET_ARM)
22         g_print ("#ifdef TARGET_ARM\n");
23 #elif defined (TARGET_ARM64)
24         g_print ("#ifdef TARGET_ARM64\n");
25 #else
26         return 0;
27 #endif
28         return 1;
29 }
30
31 static int
32 dump_os (void)
33 {
34 #if defined (PLATFORM_WIN32)
35         g_print ("#ifdef TARGET_WIN32\n");
36 #elif defined (PLATFORM_ANDROID)
37         g_print ("#ifdef TARGET_ANDROID\n");
38 #elif defined (PLATFORM_MACOSX)
39         g_print ("#ifdef TARGET_OSX\n");
40 #elif defined (PLATFORM_IOS)
41         g_print ("#ifdef TARGET_IOS\n");
42 #else
43         return 0;
44 #endif
45         return 1;
46 }
47
48 void
49 mono_dump_metadata_offsets (void);
50
51 void
52 mono_dump_metadata_offsets (void)
53 {
54 #ifdef USED_CROSS_COMPILER_OFFSETS
55         g_print ("not using native offsets\n");
56 #else
57         g_print ("#ifndef USED_CROSS_COMPILER_OFFSETS\n");
58
59         if (!dump_arch ()) {
60                 g_print ("#error failed to figure out the current arch\n");
61                 return;
62         }
63
64         if (!dump_os ()) {
65                 g_print ("#error failed to figure out the current OS\n");
66                 return;
67         }
68
69 #ifdef HAVE_SGEN_GC
70         g_print ("#ifndef HAVE_BOEHM_GC\n");
71 #elif HAVE_BOEHM_GC
72         g_print ("#ifndef HAVE_SGEN_GC\n");
73 #else
74         g_print ("#error no gc conf not supported\n");
75         return;
76 #endif
77
78         g_print ("#define HAS_CROSS_COMPILER_OFFSETS\n");
79         g_print ("#if defined (USE_CROSS_COMPILE_OFFSETS) || defined (MONO_CROSS_COMPILE)\n");
80         g_print ("#if !defined (DISABLE_METADATA_OFFSETS)\n");
81         g_print ("#define USED_CROSS_COMPILER_OFFSETS\n");
82
83 #define DISABLE_JIT_OFFSETS
84 #define DECL_OFFSET2(struct,field,offset) this_should_not_happen
85 #define DECL_ALIGN2(type,size) this_should_not_happen
86
87 #define DECL_OFFSET(struct,field) g_print ("DECL_OFFSET2(%s,%s,%d)\n", #struct, #field, (int)MONO_STRUCT_OFFSET (struct, field));
88 #define DECL_ALIGN(type) g_print ("DECL_ALIGN2(%s,%d)\n", #type, (int)MONO_ABI_ALIGNOF (type));
89 #define DECL_SIZE(type) g_print ("DECL_SIZE2(%s,%d)\n", #type, (int)MONO_ABI_SIZEOF (type));
90 #include <mono/metadata/object-offsets.h>
91
92         g_print ("#endif //disable metadata check\n");
93         g_print ("#endif //gc check\n");
94 #endif
95 }
96
97 void
98 mono_metadata_cross_helpers_run (void);
99
100 void
101 mono_metadata_cross_helpers_run (void)
102 {
103 #if defined (HAS_CROSS_COMPILER_OFFSETS) && !defined (MONO_CROSS_COMPILE)
104         gboolean is_broken = FALSE;
105
106 #define DISABLE_JIT_OFFSETS
107 #define USE_CROSS_COMPILE_OFFSETS
108 #define DECL_OFFSET(struct,field) this_should_not_happen_for_cross_fields
109 #define DECL_OFFSET2(struct,field,offset) \
110          if ((int)G_STRUCT_OFFSET (struct, field) != offset) { \
111                 g_print (#struct ":" #field " invalid struct offset %d (expected %d)\n",        \
112                         offset, \
113                         (int)G_STRUCT_OFFSET (struct, field));  \
114                 is_broken = TRUE;       \
115         }
116 #define DECL_ALIGN(type) this_should_not_happen_for_cross_align
117 #define DECL_ALIGN2(name,size) \
118          if (MONO_ALIGN_ ## name != size) { \
119                 g_print (#name ": invalid alignment %d (expected %d)\n",        \
120                 size,   \
121                 MONO_ALIGN_ ## name);   \
122                 is_broken = TRUE;       \
123         }
124 #define DECL_SIZE(type) this_should_not_happen_for_cross_size
125 #define DECL_SIZE2(name,size) \
126          if (MONO_SIZEOF_ ## name != size) { \
127                 g_print (#name ": invalid size %d (expected %d)\n",     \
128                 size,   \
129                 MONO_SIZEOF_ ## name);  \
130                 is_broken = TRUE;       \
131         }
132
133 #include <mono/metadata/object-offsets.h>
134
135         g_assert (!is_broken);
136 #endif
137 }
138