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