8f1a00801d20225ce297e4adef06df0b4792b8d1
[mono.git] / mono / mini / aot.c
1 /*
2  * aot.c: mono Ahead of Time compiler
3  *
4  * Author:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *
7  * (C) 2002 Ximian, Inc.
8  */
9
10 #include "config.h"
11 #include <sys/types.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <string.h>
15 #ifndef PLATFORM_WIN32
16 #include <sys/mman.h>
17 #else
18 #include <windows.h>
19 #endif
20
21 #include <limits.h>    /* for PAGESIZE */
22 #ifndef PAGESIZE
23 #define PAGESIZE 4096
24 #endif
25
26 #include <mono/metadata/tabledefs.h>
27 #include <mono/metadata/class.h>
28 #include <mono/metadata/object.h>
29 #include <mono/metadata/tokentype.h>
30 #include <mono/metadata/appdomain.h>
31 #include <mono/metadata/debug-helpers.h>
32 #include <mono/metadata/assembly.h>
33 #include <mono/metadata/marshal.h>
34 #include <mono/os/gc_wrapper.h>
35
36 #include "mini.h"
37
38 #ifdef PLATFORM_WIN32
39 #define SHARED_EXT ".dll"
40 #else
41 #define SHARED_EXT ".so"
42 #endif
43
44 typedef struct MonoAotMethod {
45         MonoJitInfo *info;
46         MonoJumpInfo *patch_info;
47 } MonoAotMethod;
48
49 typedef struct MonoAotModule {
50         /* Optimization flags used to compile the module */
51         guint32 opts;
52         /* Maps MonoMethods to MonoAotMethodInfos */
53         MonoGHashTable *methods;
54         char **icall_table;
55         MonoImage **image_table;
56         guint32* methods_present_table;
57 } MonoAotModule;
58
59 typedef struct MonoAotCompile {
60         FILE *fp;
61         GHashTable *ref_hash;
62         GHashTable *icall_hash;
63         GPtrArray *icall_table;
64         GHashTable *image_hash;
65         GPtrArray *image_table;
66 } MonoAotCompile;
67
68 static MonoGHashTable *aot_modules;
69
70 static CRITICAL_SECTION aot_mutex;
71
72 static guint32 mono_aot_verbose = 0;
73
74 /*
75  * Disabling this will make a copy of the loaded code and use the copy instead 
76  * of the original. This will place the caller and the callee close to each 
77  * other in memory, possibly improving cache behavior. Since the original
78  * code is in copy-on-write memory, this will not increase the memory usage
79  * of the runtime.
80  */
81 static gboolean use_loaded_code = FALSE;
82
83 /* For debugging */
84 static gint32 mono_last_aot_method = -1;
85
86 static MonoClass * 
87 decode_class_info (MonoAotModule *module, gpointer *data)
88 {
89         MonoImage *image;
90         MonoClass *klass;
91         
92         image = module->image_table [(guint32)data [1]];
93         g_assert (image);
94
95         if (data [0]) {
96                 return mono_class_get (image, (guint32)data [0]);
97         } else {
98                 klass = decode_class_info (module, data [3]);
99                 return mono_array_class_get (klass, (guint32)data [2]);
100         }
101
102         return NULL;
103 }
104
105 static void
106 load_aot_module (MonoAssembly *assembly, gpointer user_data)
107 {
108         char *aot_name;
109         MonoAotModule *info;
110         gboolean usable = TRUE;
111         char *saved_guid = NULL;
112         char *aot_version = NULL;
113         char *opt_flags = NULL;
114
115         aot_name = g_strdup_printf ("%s.so", assembly->image->name);
116
117         assembly->aot_module = g_module_open (aot_name, G_MODULE_BIND_LAZY);
118
119         if (!assembly->aot_module)
120                 return;
121
122         g_module_symbol (assembly->aot_module, "mono_assembly_guid", (gpointer *) &saved_guid);
123         g_module_symbol (assembly->aot_module, "mono_aot_version", (gpointer *) &aot_version);
124         g_module_symbol (assembly->aot_module, "mono_aot_opt_flags", (gpointer *)&opt_flags);
125
126         if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
127                 if (mono_aot_verbose > 0)
128                         printf ("AOT module %s has wrong file format version (expected %s got %s)\n", aot_name, MONO_AOT_FILE_VERSION, aot_version);
129                 usable = FALSE;
130         }
131         else
132                 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
133                         if (mono_aot_verbose > 0)
134                                 printf ("AOT module %s is out of date.\n", aot_name);
135                         usable = FALSE;
136                 }
137
138         if (!usable) {
139                 g_free (aot_name);
140                 g_module_close (assembly->aot_module);
141                 assembly->aot_module = NULL;
142                 return;
143         }
144
145         /*
146          * It seems that MonoGHashTables are in the GC heap, so structures
147          * containing them must be in the GC heap as well :(
148          */
149 #ifdef HAVE_BOEHM_GC
150         info = GC_MALLOC (sizeof (MonoAotModule));
151 #else
152         info = g_new0 (MonoAotModule, 1);
153 #endif
154         info->methods = mono_g_hash_table_new (NULL, NULL);
155         sscanf (opt_flags, "%d", &info->opts);
156
157         /* Read image table */
158         {
159                 guint32 table_len, i;
160                 char *table = NULL;
161
162                 g_module_symbol (assembly->aot_module, "mono_image_table", (gpointer *)&table);
163                 g_assert (table);
164
165                 table_len = *(guint32*)table;
166                 table += sizeof (guint32);
167                 info->image_table = g_new0 (MonoImage*, table_len);
168                 for (i = 0; i < table_len; ++i) {
169                         info->image_table [i] = mono_image_loaded_by_guid (table);
170                         if (!info->image_table [i]) {
171                                 if (mono_aot_verbose > 0)
172                                         printf ("AOT module %s is out of date.\n", aot_name);
173                                 g_free (info->methods);
174                                 g_free (info->image_table);
175                                 g_free (info);
176                                 g_free (aot_name);
177                                 g_module_close (assembly->aot_module);
178                                 assembly->aot_module = NULL;
179                                 return;
180                         }
181                         table += strlen (table) + 1;
182                 }
183         }
184
185         /* Read icall table */
186         {
187                 guint32 table_len, i;
188                 char *table = NULL;
189
190                 g_module_symbol (assembly->aot_module, "mono_icall_table", (gpointer *)&table);
191                 g_assert (table);
192
193                 table_len = *(guint32*)table;
194                 table += sizeof (guint32);
195                 info->icall_table = g_new0 (char*, table_len);
196                 for (i = 0; i < table_len; ++i) {
197                         info->icall_table [i] = table;
198                         table += strlen (table) + 1;
199                 }
200         }
201
202         /* Read methods present table */
203         g_module_symbol (assembly->aot_module, "mono_methods_present_table", (gpointer *)&info->methods_present_table);
204         g_assert (info->methods_present_table);
205
206         EnterCriticalSection (&aot_mutex);
207         mono_g_hash_table_insert (aot_modules, assembly, info);
208         LeaveCriticalSection (&aot_mutex);
209
210         if (mono_aot_verbose > 0)
211                 printf ("Loaded AOT Module for %s.\n", assembly->image->name);
212 }
213
214 void
215 mono_aot_init (void)
216 {
217         InitializeCriticalSection (&aot_mutex);
218
219         aot_modules = mono_g_hash_table_new (NULL, NULL);
220
221         mono_install_assembly_load_hook (load_aot_module, NULL);
222
223         if (getenv ("MONO_LASTAOT"))
224                 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
225 }
226  
227 static MonoJitInfo *
228 mono_aot_get_method_inner (MonoDomain *domain, MonoMethod *method)
229 {
230         MonoClass *klass = method->klass;
231         MonoAssembly *ass = klass->image->assembly;
232         MonoJumpInfo *patch_info = NULL;
233         GModule *module = ass->aot_module;
234         char method_label [256];
235         char info_label [256];
236         guint8 *code = NULL;
237         gpointer *info;
238         guint code_len, used_int_regs, used_strings;
239         MonoAotModule *aot_module;
240         MonoAotMethod *minfo;
241         MonoJitInfo *jinfo;
242         MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
243         int i;
244
245         if (!module)
246                 return NULL;
247
248         if (!method->token)
249                 return NULL;
250
251         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
252                 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
253                 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
254                 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
255                 return NULL;
256
257         aot_module = (MonoAotModule*)mono_g_hash_table_lookup (aot_modules, ass);
258
259         g_assert (klass->inited);
260
261         minfo = mono_g_hash_table_lookup (aot_module->methods, method);
262         if (minfo) {
263                 /* Duplicate jinfo */
264                 jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
265                 memcpy (jinfo, minfo->info, sizeof (MonoJitInfo));
266                 if (jinfo->clauses) {
267                         jinfo->clauses = 
268                                 mono_mempool_alloc0 (domain->mp, sizeof (MonoJitExceptionInfo) * header->num_clauses);
269                         memcpy (jinfo->clauses, minfo->info->clauses, sizeof (MonoJitExceptionInfo) * header->num_clauses);
270                 }
271
272                 /* This method was already loaded in another appdomain */
273                 if (aot_module->opts & MONO_OPT_SHARED)
274                         /* Use the same method in the new appdomain */
275                         ;
276                 else if (!minfo->patch_info)
277                         /* Use the same method in the new appdomain */
278                         ;                       
279                 else {
280                         /* Create a copy of the original method and apply relocations */
281
282                         code = mono_mempool_alloc (domain->code_mp, minfo->info->code_size);
283                         memcpy (code, minfo->info->code_start, minfo->info->code_size);
284
285                         if (mono_aot_verbose > 1)
286                                 printf ("REUSE METHOD: %s %p - %p.\n", mono_method_full_name (method, TRUE), code, (char*)code + code_len);
287
288                         /* Do this outside the lock to avoid deadlocks */
289                         LeaveCriticalSection (&aot_mutex);
290                         mono_arch_patch_code (method, domain, code, minfo->patch_info);
291                         EnterCriticalSection (&aot_mutex);
292
293                         /* Relocate jinfo */
294                         jinfo->code_start = code;
295                         if (jinfo->clauses) {
296                                 for (i = 0; i < header->num_clauses; ++i) {
297                                         MonoJitExceptionInfo *ei = &jinfo->clauses [i];
298                                         gint32 offset = code - (guint8*)minfo->info->code_start;
299
300                                         if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
301                                                 ei->data.filter = (guint8*)ei->data.filter + offset;
302                                         ei->try_start = (guint8*)ei->try_start + offset;
303                                         ei->try_end = (guint8*)ei->try_end + offset;
304                                         ei->handler_start = (guint8*)ei->handler_start + offset;
305                                 }
306                         }
307                 }
308
309                 return jinfo;
310         }
311
312         /* Do a fast check to see whenever the method exists */
313         {
314                 guint32 index = mono_metadata_token_index (method->token) - 1;
315                 guint32 w;
316                 w = aot_module->methods_present_table [index / 32];
317                 if (! (w & (1 << (index % 32)))) {
318                         if (mono_aot_verbose > 1)
319                                 printf ("NOT FOUND: %s.\n", mono_method_full_name (method, TRUE));
320                         return NULL;
321                 }
322         }
323
324         sprintf (method_label, "m_%x", mono_metadata_token_index (method->token));
325
326         if (!g_module_symbol (module, method_label, (gpointer *)&code))
327                 return NULL;
328
329         sprintf (info_label, "%s_p", method_label);
330
331         if (!g_module_symbol (module, info_label, (gpointer *)&info))
332                 return NULL;
333
334         if (mono_last_aot_method != -1) {
335                 if (mono_jit_stats.methods_aot > mono_last_aot_method)
336                                 return NULL;
337                 else
338                         if (mono_jit_stats.methods_aot == mono_last_aot_method)
339                                 printf ("LAST AOT METHOD: %s.%s.%s.\n", klass->name_space, klass->name, method->name);
340         }
341
342 #ifdef HAVE_BOEHM_GC
343         minfo = GC_MALLOC (sizeof (MonoAotMethod));
344 #else
345         minfo = g_new0 (MonoAotMethod, 1);
346 #endif
347
348         jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
349
350         code_len = GPOINTER_TO_UINT (*((gpointer **)info));
351         info++;
352         used_int_regs = GPOINTER_TO_UINT (*((gpointer **)info));
353         info++;
354
355         if (!use_loaded_code) {
356                 guint8 *code2;
357                 code2 = mono_mempool_alloc (domain->code_mp, code_len);
358                 memcpy (code2, code, code_len);
359                 code = code2;
360         }
361
362         if (mono_aot_verbose > 1)
363                 printf ("FOUND AOT compiled code for %s %p - %p %p\n", mono_method_full_name (method, TRUE), code, code + code_len, info);
364
365         /* Exception table */
366         if (header->num_clauses) {
367                 jinfo->clauses = 
368                         mono_mempool_alloc0 (domain->mp, sizeof (MonoJitExceptionInfo) * header->num_clauses);
369                 jinfo->num_clauses = header->num_clauses;
370
371                 jinfo->exvar_offset = GPOINTER_TO_UINT (*((gpointer**)info));
372                 info ++;
373
374                 for (i = 0; i < header->num_clauses; ++i) {
375                         MonoExceptionClause *ec = &header->clauses [i];                         
376                         MonoJitExceptionInfo *ei = &jinfo->clauses [i];
377
378                         ei->flags = ec->flags;
379                         if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
380                                 ei->data.filter = code + GPOINTER_TO_UINT (*((gpointer**)info));
381                         else
382                                 ei->data.token = GPOINTER_TO_UINT (*((gpointer**)info));
383                         info ++;
384                         ei->try_start = code + GPOINTER_TO_UINT (*((gpointer**)info));
385                         info ++;
386                         ei->try_end = code + GPOINTER_TO_UINT (*((gpointer**)info));
387                         info ++;
388                         ei->handler_start = code + GPOINTER_TO_UINT (*((gpointer**)info));
389                         info ++;
390                 }
391         }
392
393         if (aot_module->opts & MONO_OPT_SHARED) {
394                 used_strings = GPOINTER_TO_UINT (*((gpointer **)info));
395                 info++;
396         }
397         else
398                 used_strings = 0;
399
400         for (i = 0; i < used_strings; i++) {
401                 guint token =  GPOINTER_TO_UINT (*((gpointer **)info));
402                 info++;
403                 mono_ldstr (mono_root_domain, klass->image, mono_metadata_token_index (token));
404         }
405
406         if (*info) {
407                 MonoMemPool *mp;
408                 MonoImage *image;
409                 guint8 *page_start;
410                 gpointer *table;
411                 int pages;
412                 int i, err;
413                 guint32 last_offset;
414
415                 if (aot_module->opts & MONO_OPT_SHARED)
416                         mp = mono_mempool_new ();
417                 else
418                         mp = domain->mp;
419
420                 last_offset = 0;
421                 while (*info) {
422                         MonoJumpInfo *ji = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
423                         gpointer *data;
424
425                         guint8 b1, b2;
426
427                         b1 = *(guint8*)info;
428                         b2 = *((guint8*)info + 1);
429
430                         info = (gpointer*)((guint8*)info + 2);
431
432                         ji->type = b1 >> 2;
433
434                         if (((b1 & (1 + 2)) == 3) && (b2 == 255)) {
435                                 ji->ip.i = GPOINTER_TO_UINT (*info);
436                                 info ++;
437                         }
438                         else
439                                 ji->ip.i = (((guint32)(b1 & (1 + 2))) << 8) + b2;
440
441                         ji->ip.i += last_offset;
442                         last_offset = ji->ip.i;
443                         //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
444
445                         data = *((gpointer **)info);
446
447                         switch (ji->type) {
448                         case MONO_PATCH_INFO_CLASS:
449                         case MONO_PATCH_INFO_IID:
450                                 ji->data.klass = decode_class_info (aot_module, data);
451                                 g_assert (ji->data.klass);
452                                 mono_class_init (ji->data.klass);
453                                 break;
454                         case MONO_PATCH_INFO_VTABLE:
455                         case MONO_PATCH_INFO_CLASS_INIT:
456                                 ji->data.klass = decode_class_info (aot_module, data);
457                                 g_assert (ji->data.klass);
458                                 mono_class_init (ji->data.klass);
459                                 break;
460                         case MONO_PATCH_INFO_IMAGE:
461                                 ji->data.image = aot_module->image_table [(guint32)data];
462                                 g_assert (ji->data.image);
463                                 break;
464                         case MONO_PATCH_INFO_METHOD:
465                         case MONO_PATCH_INFO_METHODCONST:
466                         case MONO_PATCH_INFO_METHOD_JUMP: {
467                                 guint32 image_index, token;
468
469                                 image_index = (guint32)data >> 24;
470                                 token = MONO_TOKEN_METHOD_DEF | ((guint32)data & 0xffffff);
471
472                                 image = aot_module->image_table [image_index];
473                                 ji->data.method = mono_get_method (image, token, NULL);
474                                 g_assert (ji->data.method);
475                                 mono_class_init (ji->data.method->klass);
476
477                                 break;
478                         }
479                         case MONO_PATCH_INFO_WRAPPER: {
480                                 guint32 image_index, token;
481                                 guint32 wrapper_type;
482
483                                 wrapper_type = (guint32)data[0];
484                                 image_index = (guint32)data[1] >> 24;
485                                 token = MONO_TOKEN_METHOD_DEF | ((guint32)data[1] & 0xffffff);
486
487                                 image = aot_module->image_table [image_index];
488                                 ji->data.method = mono_get_method (image, token, NULL);
489                                 g_assert (ji->data.method);
490                                 mono_class_init (ji->data.method->klass);
491
492                                 g_assert (wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK);
493                                 ji->type = MONO_PATCH_INFO_METHOD;
494                                 ji->data.method = mono_marshal_get_remoting_invoke_with_check (ji->data.method);
495                                 break;
496                         }
497                         case MONO_PATCH_INFO_FIELD:
498                         case MONO_PATCH_INFO_SFLDA: {
499                                 MonoClass *klass = decode_class_info (aot_module, data [1]);
500                                 mono_class_init (klass);
501                                 ji->data.field = mono_class_get_field (klass, (guint32)data [0]);
502                                 break;
503                         }
504                         case MONO_PATCH_INFO_INTERNAL_METHOD:
505                                 ji->data.name = aot_module->icall_table [(guint32)data];
506                                 g_assert (ji->data.name);
507                                 //printf ("A: %s.\n", ji->data.name);
508                                 break;
509                         case MONO_PATCH_INFO_SWITCH:
510                                 ji->table_size = (int)data [0];
511                                 table = g_new (gpointer, ji->table_size);
512                                 ji->data.target = table;
513                                 for (i = 0; i < ji->table_size; i++) {
514                                         table [i] = data [i + 1];
515                                 }
516                                 break;
517                         case MONO_PATCH_INFO_R4:
518                         case MONO_PATCH_INFO_R8:
519                                 ji->data.target = data;
520                                 break;
521                         case MONO_PATCH_INFO_LDSTR:
522                         case MONO_PATCH_INFO_LDTOKEN:
523                         case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
524                                 ji->data.target = *data;
525                                 break;
526                         case MONO_PATCH_INFO_EXC_NAME:
527                                 ji->data.klass = decode_class_info (aot_module, data);
528                                 g_assert (ji->data.klass);
529                                 mono_class_init (ji->data.klass);
530                                 ji->data.name = ji->data.klass->name;
531                                 break;
532                         case MONO_PATCH_INFO_METHOD_REL:
533                                 ji->data.offset = (int)data [0];
534                                 break;
535                         default:
536                                 g_warning ("unhandled type %d", ji->type);
537                                 g_assert_not_reached ();
538                         }
539
540                         info++;
541                         ji->next = patch_info;
542                         patch_info = ji;
543                 }
544
545                 if (use_loaded_code) {
546                 /* disable write protection */
547 #ifndef PLATFORM_WIN32
548                         page_start = (char *) (((int) (code)) & ~ (PAGESIZE - 1));
549                         pages = (code + code_len - page_start + PAGESIZE - 1) / PAGESIZE;
550                         err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
551                         g_assert (err == 0);
552 #else
553                         {
554                                 DWORD oldp;
555                                 g_assert (VirtualProtect (code, code_len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
556                         }
557 #endif
558                 }
559
560                 /* Do this outside the lock to avoid deadlocks */
561                 LeaveCriticalSection (&aot_mutex);
562                 mono_arch_patch_code (method, domain, code, patch_info);
563                 EnterCriticalSection (&aot_mutex);
564
565                 if (aot_module->opts & MONO_OPT_SHARED)
566                         /* No need to cache patches */
567                         mono_mempool_destroy (mp);
568                 else
569                         minfo->patch_info = patch_info;
570         }
571
572         mono_jit_stats.methods_aot++;
573
574         {
575                 jinfo->code_size = code_len;
576                 jinfo->used_regs = used_int_regs;
577                 jinfo->method = method;
578                 jinfo->code_start = code;
579                 jinfo->domain_neutral = (aot_module->opts & MONO_OPT_SHARED) != 0;
580
581                 minfo->info = jinfo;
582                 mono_g_hash_table_insert (aot_module->methods, method, minfo);
583
584                 return jinfo;
585         }
586 }
587
588 MonoJitInfo*
589 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
590 {
591         MonoJitInfo *info;
592
593         EnterCriticalSection (&aot_mutex);
594         info = mono_aot_get_method_inner (domain, method);
595         LeaveCriticalSection (&aot_mutex);
596
597         /* Do this outside the lock */
598         if (info) {
599                 mono_jit_info_table_add (domain, info);
600                 return info;
601         }
602         else
603                 return NULL;
604 }
605
606 #if 0
607 static void
608 write_data_symbol (FILE *fp, const char *name, guint8 *buf, int size, int align)
609 {
610         int i;
611
612         fprintf (fp, ".globl %s\n", name);
613         fprintf (fp, ".text 1 \n\t.align %d\n", align);
614         fprintf (fp, "\t.type %s,@object\n", name);
615         fprintf (fp, "\t.size %s,%d\n", name, size);
616         fprintf (fp, "%s:\n", name);
617         for (i = 0; i < size; i++) { 
618                 fprintf (fp, ".byte %d\n", buf [i]);
619         }
620         
621 }
622 #endif
623
624 static void
625 write_string_symbol (FILE *fp, const char *name, const char *value)
626 {
627         fprintf (fp, ".globl %s\n", name);
628         fprintf (fp, ".text 1\n");
629         fprintf (fp, "%s:\n", name);
630         fprintf (fp, "\t.string \"%s\"\n", value);
631 }
632
633 static guint32
634 mono_get_field_token (MonoClassField *field) 
635 {
636         MonoClass *klass = field->parent;
637         int i;
638
639         for (i = 0; i < klass->field.count; ++i) {
640                 if (field == &klass->fields [i])
641                         return MONO_TOKEN_FIELD_DEF | (klass->field.first + 1 + i);
642         }
643
644         g_assert_not_reached ();
645         return 0;
646 }
647
648 static guint32
649 get_image_index (MonoAotCompile *cfg, MonoImage *image)
650 {
651         guint32 index;
652
653         index = GPOINTER_TO_UINT (g_hash_table_lookup (cfg->image_hash, image));
654         if (index)
655                 return index - 1;
656         else {
657                 index = g_hash_table_size (cfg->image_hash);
658                 g_hash_table_insert (cfg->image_hash, image, GUINT_TO_POINTER (index + 1));
659                 g_ptr_array_add (cfg->image_table, image);
660                 return index;
661         }
662 }
663
664 static void
665 emit_image_index (MonoAotCompile *cfg, MonoImage *image)
666 {
667         guint32 image_index;
668
669         image_index = get_image_index (cfg, image);
670
671         fprintf (cfg->fp, "\t.long %d\n", image_index);
672 }
673
674 static char *
675 cond_emit_klass_label (MonoAotCompile *cfg, MonoClass *klass)
676 {
677         char *l1, *el = NULL;
678
679         if ((l1 = g_hash_table_lookup (cfg->ref_hash, klass))) 
680                 return l1;
681
682         if (!klass->type_token) {
683                 g_assert (klass->rank > 0);
684                 el = cond_emit_klass_label (cfg, klass->element_class);
685         }
686         
687         fprintf (cfg->fp, "\t.align %d\n", sizeof (gpointer));
688         l1 = g_strdup_printf ("klass_p_%08x_%p", klass->type_token, klass);
689         fprintf (cfg->fp, "%s:\n", l1);
690         fprintf (cfg->fp, "\t.long 0x%08x\n", klass->type_token);
691         emit_image_index (cfg, klass->image);
692
693         if (el) {
694                 fprintf (cfg->fp, "\t.long %d\n", klass->rank); 
695                 fprintf (cfg->fp, "\t.long %s\n", el);
696         }
697
698         g_hash_table_insert (cfg->ref_hash, klass, l1);
699
700         return l1;
701 }
702
703 static char *
704 cond_emit_field_label (MonoAotCompile *cfg, MonoJumpInfo *patch_info)
705 {
706         MonoClassField *field = patch_info->data.field;
707         char *l1, *l2;
708         guint token;
709
710         if ((l1 = g_hash_table_lookup (cfg->ref_hash, field))) 
711                 return l1;
712
713         l2 = cond_emit_klass_label (cfg, field->parent);
714         fprintf (cfg->fp, "\t.align %d\n", sizeof (gpointer));
715         token = mono_get_field_token (field);
716         g_assert (token);
717         l1 = g_strdup_printf ("klass_p_%08x_%p", token, field);
718         fprintf (cfg->fp, "%s:\n", l1);
719         fprintf (cfg->fp, "\t.long 0x%08x\n", token);
720         fprintf (cfg->fp, "\t.long %s\n", l2);
721                 
722         g_hash_table_insert (cfg->ref_hash, field, l1);
723
724         return l1;
725 }
726
727 static gint
728 compare_patches (gconstpointer a, gconstpointer b)
729 {
730         int i, j;
731
732         i = (*(MonoJumpInfo**)a)->ip.i;
733         j = (*(MonoJumpInfo**)b)->ip.i;
734
735         if (i < j)
736                 return -1;
737         else
738                 if (i > j)
739                         return 1;
740         else
741                 return 0;
742 }
743
744 static void
745 emit_method (MonoAotCompile *acfg, MonoCompile *cfg)
746 {
747         MonoMethod *method;
748         GList *l;
749         FILE *tmpfp;
750         int i, j, k, pindex;
751         guint8 *code, *mname;
752         int func_alignment = 16;
753         GPtrArray *patches;
754         MonoJumpInfo *patch_info;
755         MonoMethodHeader *header;
756
757         tmpfp = acfg->fp;
758         method = cfg->method;
759         code = cfg->native_code;
760         header = ((MonoMethodNormal*)method)->header;
761
762         fprintf (tmpfp, ".text 0\n");
763         mname = g_strdup_printf ("m_%x", mono_metadata_token_index (method->token));
764         fprintf (tmpfp, "\t.align %d\n", func_alignment);
765         fprintf (tmpfp, ".globl %s\n", mname);
766         fprintf (tmpfp, "\t.type %s,@function\n", mname);
767         fprintf (tmpfp, "%s:\n", mname);
768
769         for (i = 0; i < cfg->code_len; i++) 
770                 fprintf (tmpfp, ".byte %d\n", (unsigned int) code [i]);
771
772         fprintf (tmpfp, ".text 1\n");
773
774         /* Sort relocations */
775         patches = g_ptr_array_new ();
776         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next)
777                 g_ptr_array_add (patches, patch_info);
778         g_ptr_array_sort (patches, compare_patches);
779
780         j = 0;
781         for (pindex = 0; pindex < patches->len; ++pindex) {
782                 patch_info = g_ptr_array_index (patches, pindex);
783                 switch (patch_info->type) {
784                 case MONO_PATCH_INFO_LABEL:
785                 case MONO_PATCH_INFO_BB:
786                         /* relative jumps are no problem, there is no need to handle then here */
787                         break;
788                 case MONO_PATCH_INFO_SWITCH: {
789                         gpointer *table = (gpointer *)patch_info->data.target;
790                         int k;
791
792                         fprintf (tmpfp, "\t.align %d\n", sizeof (gpointer));
793                         fprintf (tmpfp, "%s_p_%d:\n", mname, j);
794                         fprintf (tmpfp, "\t.long %d\n", patch_info->table_size);
795                         
796                         for (k = 0; k < patch_info->table_size; k++) {
797                                 fprintf (tmpfp, "\t.long %d\n", (int)table [k]);
798                         }
799                         j++;
800                         break;
801                 }
802                 case MONO_PATCH_INFO_INTERNAL_METHOD: {
803                         guint32 icall_index;
804
805                         icall_index = (guint32)g_hash_table_lookup (acfg->icall_hash, patch_info->data.name);
806                         if (!icall_index) {
807                                 icall_index = g_hash_table_size (acfg->icall_hash) + 1;
808                                 g_hash_table_insert (acfg->icall_hash, (gpointer)patch_info->data.name,
809                                                                          GUINT_TO_POINTER (icall_index));
810                                 g_ptr_array_add (acfg->icall_table, (gpointer)patch_info->data.name);
811                         }
812                         patch_info->data.name = g_strdup_printf ("%d", icall_index - 1);
813                         j++;
814                         break;
815                 }
816                 case MONO_PATCH_INFO_METHODCONST:
817                 case MONO_PATCH_INFO_METHOD:
818                 case MONO_PATCH_INFO_METHOD_JUMP: {
819                         /*
820                          * The majority of patches are for methods, so we emit
821                          * them inline instead of defining a label for them to
822                          * decrease the number of relocations.
823                          */
824                         guint32 image_index = get_image_index (acfg, patch_info->data.method->klass->image);
825                         guint32 token = patch_info->data.method->token;
826                         g_assert (image_index < 256);
827                         g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
828
829                         patch_info->data.name = 
830                                 g_strdup_printf ("%d", (image_index << 24) + (mono_metadata_token_index (token)));
831                         j++;
832                         break;
833                 }
834                 case MONO_PATCH_INFO_WRAPPER: {
835                         MonoMethod *m;
836                         guint32 image_index;
837                         guint32 token;
838
839                         m = mono_marshal_method_from_wrapper (patch_info->data.method);
840                         image_index = get_image_index (acfg, m->klass->image);
841                         token = m->token;
842                         g_assert (image_index < 256);
843                         g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
844
845                         fprintf (tmpfp, "\t.align %d\n", sizeof (gpointer));
846                         fprintf (tmpfp, "%s_p_%d:\n", mname, j);
847                         fprintf (tmpfp, "\t.long %d\n", patch_info->data.method->wrapper_type);
848                         fprintf (tmpfp, "\t.long %d\n", (image_index << 24) + (mono_metadata_token_index (token)));
849                         j++;
850                         break;
851                 }
852                 case MONO_PATCH_INFO_FIELD:
853                         patch_info->data.name = cond_emit_field_label (acfg, patch_info);
854                         j++;
855                         break;
856                 case MONO_PATCH_INFO_CLASS:
857                 case MONO_PATCH_INFO_IID:
858                         patch_info->data.name = cond_emit_klass_label (acfg, patch_info->data.klass);
859                         j++;
860                         break;
861                 case MONO_PATCH_INFO_IMAGE:
862                         patch_info->data.name = g_strdup_printf ("%d", get_image_index (acfg, patch_info->data.image));
863                         j++;
864                         break;
865                 case MONO_PATCH_INFO_EXC_NAME: {
866                         MonoClass *ex_class;
867                         
868                         ex_class =
869                                 mono_class_from_name (mono_defaults.exception_class->image,
870                                                                           "System", patch_info->data.target);
871                         g_assert (ex_class);
872                         patch_info->data.name = cond_emit_klass_label (acfg, ex_class);
873                         j++;
874                         break;
875                 }
876                 case MONO_PATCH_INFO_R4:
877                         fprintf (tmpfp, "\t.align 8\n");
878                         fprintf (tmpfp, "%s_p_%d:\n", mname, j);
879                         fprintf (tmpfp, "\t.long 0x%08x\n", *((guint32 *)patch_info->data.target));     
880                         j++;
881                         break;
882                 case MONO_PATCH_INFO_R8:
883                         fprintf (tmpfp, "\t.align 8\n");
884                         fprintf (tmpfp, "%s_p_%d:\n", mname, j);
885                         fprintf (tmpfp, "\t.long 0x%08x\n", *((guint32 *)patch_info->data.target));
886                         fprintf (tmpfp, "\t.long 0x%08x\n", *((guint32 *)patch_info->data.target + 1));
887                         j++;
888                         break;
889                 case MONO_PATCH_INFO_METHOD_REL:
890                         fprintf (tmpfp, "\t.align %d\n", sizeof (gpointer));
891                         fprintf (tmpfp, "%s_p_%d:\n", mname, j);
892                         fprintf (tmpfp, "\t.long 0x%08x\n", patch_info->data.offset);
893                         j++;
894                         break;
895                 case MONO_PATCH_INFO_VTABLE:
896                 case MONO_PATCH_INFO_CLASS_INIT:
897                         patch_info->data.name = cond_emit_klass_label (acfg, patch_info->data.klass);
898                         j++;
899                         break;
900                 case MONO_PATCH_INFO_SFLDA:
901                         patch_info->data.name = cond_emit_field_label (acfg, patch_info);
902                         j++;
903                         break;
904                 case MONO_PATCH_INFO_LDSTR:
905                 case MONO_PATCH_INFO_LDTOKEN:
906                 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
907                         fprintf (tmpfp, "\t.align 8\n");
908                         fprintf (tmpfp, "%s_p_%d:\n", mname, j);
909                         fprintf (tmpfp, "\t.long 0x%08x\n", patch_info->data.token);
910                         j++;
911                         break;
912                 default:
913                         g_warning ("unable to handle jump info %d", patch_info->type);
914                         g_assert_not_reached ();
915                 }
916         }
917
918         fprintf (tmpfp, ".globl %s_p\n", mname);
919         fprintf (tmpfp, "\t.align %d\n", sizeof (gpointer));
920         fprintf (tmpfp, "%s_p:\n", mname);
921
922         fprintf (tmpfp, "\t.long %d\n", cfg->code_len);
923         fprintf (tmpfp, "\t.long %d\n", cfg->used_int_regs);
924
925         /* Exception table */
926         if (header->num_clauses) {
927                 MonoJitInfo *jinfo = cfg->jit_info;
928
929                 fprintf (tmpfp, "\t.long %d\n", jinfo->exvar_offset);
930
931                 for (k = 0; k < header->num_clauses; ++k) {
932                         MonoJitExceptionInfo *ei = &jinfo->clauses [k];
933
934                         if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
935                                 fprintf (tmpfp, "\t.long %d\n", (guint8*)ei->data.filter - code);
936                         else
937                                 /* fixme: tokens are not global */
938                                 fprintf (tmpfp, "\t.long %d\n", ei->data.token);
939
940                         fprintf (tmpfp, "\t.long %d\n", (guint8*)ei->try_start - code);
941                         fprintf (tmpfp, "\t.long %d\n", (guint8*)ei->try_end - code);
942                         fprintf (tmpfp, "\t.long %d\n", (guint8*)ei->handler_start - code);
943                 }
944         }
945
946         /* String table */
947         if (cfg->opt & MONO_OPT_SHARED) {
948                 fprintf (tmpfp, "\t.long %d\n", g_list_length (cfg->ldstr_list));
949                 for (l = cfg->ldstr_list; l; l = l->next) {
950                         fprintf (tmpfp, "\t.long 0x%08lx\n", (long)l->data);
951                 }
952         }
953         else
954                 /* Used only in shared mode */
955                 g_assert (!cfg->ldstr_list);
956
957         //printf ("M: %s (%s).\n", mono_method_full_name (method, TRUE), mname);
958
959         if (j) {
960                 guint32 last_offset;
961                 last_offset = 0;
962
963                 j = 0;
964                 for (pindex = 0; pindex < patches->len; ++pindex) {
965                         guint32 offset;
966                         patch_info = g_ptr_array_index (patches, pindex);
967
968                         if ((patch_info->type == MONO_PATCH_INFO_LABEL) ||
969                                 (patch_info->type == MONO_PATCH_INFO_BB))
970                                 /* Nothing to do */
971                                 continue;
972
973                         //printf ("T: %d O: %d.\n", patch_info->type, patch_info->ip.i);
974                         offset = patch_info->ip.i - last_offset;
975                         last_offset = patch_info->ip.i;
976
977                         /* Encode type+position compactly */
978                         g_assert (patch_info->type < 64);
979                         if (offset < 1024 - 1) {
980                                 fprintf (tmpfp, "\t.byte %d\n", (patch_info->type << 2) + (offset >> 8));
981                                 fprintf (tmpfp, "\t.byte %d\n", offset & ((1 << 8) - 1));
982                         }
983                         else {
984                                 fprintf (tmpfp, "\t.byte %d\n", (patch_info->type << 2) + 3);
985                                 fprintf (tmpfp, "\t.byte %d\n", 255);
986                                 fprintf (tmpfp, "\t.long %d\n", offset);
987                         }
988
989                         switch (patch_info->type) {
990                         case MONO_PATCH_INFO_METHODCONST:
991                         case MONO_PATCH_INFO_METHOD:
992                         case MONO_PATCH_INFO_METHOD_JUMP:
993                         case MONO_PATCH_INFO_CLASS:
994                         case MONO_PATCH_INFO_IID:
995                         case MONO_PATCH_INFO_FIELD:
996                         case MONO_PATCH_INFO_INTERNAL_METHOD:
997                         case MONO_PATCH_INFO_IMAGE:
998                         case MONO_PATCH_INFO_VTABLE:
999                         case MONO_PATCH_INFO_CLASS_INIT:
1000                         case MONO_PATCH_INFO_SFLDA:
1001                         case MONO_PATCH_INFO_EXC_NAME:
1002                                 fprintf (tmpfp, "\t.long %s\n", patch_info->data.name);
1003                                 j++;
1004                                 break;
1005                         case MONO_PATCH_INFO_SWITCH:
1006                         case MONO_PATCH_INFO_R4:
1007                         case MONO_PATCH_INFO_R8:
1008                         case MONO_PATCH_INFO_METHOD_REL:
1009                         case MONO_PATCH_INFO_LDSTR:
1010                         case MONO_PATCH_INFO_LDTOKEN:
1011                         case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1012                         case MONO_PATCH_INFO_WRAPPER:
1013                                 fprintf (tmpfp, "\t.long %s_p_%d\n", mname, j);
1014                                 j++;
1015                                 break;
1016                         case MONO_PATCH_INFO_LABEL:
1017                         case MONO_PATCH_INFO_BB:
1018                                 break;
1019                         default:
1020                                 g_warning ("unable to handle jump info %d", patch_info->type);
1021                                 g_assert_not_reached ();
1022                         }
1023
1024                 }
1025         }
1026
1027         /*
1028          * 0 is PATCH_INFO_BB, which can't be in the file.
1029          */
1030         /* NULL terminated array */
1031         fprintf (tmpfp, "\t.long 0\n");
1032
1033         /* fixme: save the rest of the required infos */
1034
1035         g_free (mname);
1036 }
1037
1038 int
1039 mono_compile_assembly (MonoAssembly *ass, guint32 opts)
1040 {
1041         MonoCompile *cfg;
1042         MonoImage *image = ass->image;
1043         MonoMethod *method;
1044         char *com, *tmpfname, *opts_str;
1045         FILE *tmpfp;
1046         int i;
1047         guint8 *symbol;
1048         int ccount = 0, mcount = 0, lmfcount = 0, abscount = 0, wrappercount = 0, ocount = 0;
1049         GHashTable *ref_hash;
1050         MonoAotCompile *acfg;
1051         gboolean *emitted;
1052
1053         printf ("Mono AOT compiler - compiling assembly %s\n", image->name);
1054
1055         i = g_file_open_tmp ("mono_aot_XXXXXX", &tmpfname, NULL);
1056         tmpfp = fdopen (i, "w+");
1057         g_assert (tmpfp);
1058
1059         ref_hash = g_hash_table_new (NULL, NULL);
1060
1061         acfg = g_new0 (MonoAotCompile, 1);
1062         acfg->fp = tmpfp;
1063         acfg->ref_hash = ref_hash;
1064         acfg->icall_hash = g_hash_table_new (NULL, NULL);
1065         acfg->icall_table = g_ptr_array_new ();
1066         acfg->image_hash = g_hash_table_new (NULL, NULL);
1067         acfg->image_table = g_ptr_array_new ();
1068
1069         write_string_symbol (tmpfp, "mono_assembly_guid" , image->guid);
1070
1071         write_string_symbol (tmpfp, "mono_aot_version", MONO_AOT_FILE_VERSION);
1072
1073         opts_str = g_strdup_printf ("%d", opts);
1074         write_string_symbol (tmpfp, "mono_aot_opt_flags", opts_str);
1075         g_free (opts_str);
1076
1077         emitted = g_new0 (gboolean, image->tables [MONO_TABLE_METHOD].rows);
1078
1079         for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
1080                 MonoJumpInfo *patch_info;
1081                 gboolean skip;
1082                 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
1083                 method = mono_get_method (image, token, NULL);
1084                 
1085                 /* fixme: maybe we can also precompile wrapper methods */
1086                 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1087                     (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1088                     (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1089                     (method->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
1090                         //printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
1091                         continue;
1092                 }
1093
1094                 mcount++;
1095
1096                 /* fixme: we need to patch the IP for the LMF in that case */
1097                 if (method->save_lmf) {
1098                         //printf ("Skip (needs lmf):  %s\n", mono_method_full_name (method, TRUE));
1099                         lmfcount++;
1100                         continue;
1101                 }
1102
1103                 //printf ("START:           %s\n", mono_method_full_name (method, TRUE));
1104                 //mono_compile_method (method);
1105
1106                 cfg = mini_method_compile (method, opts, mono_root_domain, 0);
1107                 g_assert (cfg);
1108
1109                 if (cfg->disable_aot) {
1110                         printf ("Skip (other): %s\n", mono_method_full_name (method, TRUE));
1111                         ocount++;
1112                         continue;
1113                 }
1114
1115                 skip = FALSE;
1116                 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
1117                         if (patch_info->type == MONO_PATCH_INFO_ABS) {
1118                                 /* unable to handle this */
1119                                 //printf ("Skip (abs addr):   %s %d\n", mono_method_full_name (method, TRUE), patch_info->type);
1120                                 skip = TRUE;    
1121                                 break;
1122                         }
1123                 }
1124
1125                 if (skip) {
1126                         abscount++;
1127                         continue;
1128                 }
1129
1130                 /* remoting-invoke-with-check wrappers are very common */
1131                 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
1132                         if ((patch_info->type == MONO_PATCH_INFO_METHOD) &&
1133                                 ((patch_info->data.method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)))
1134                                 patch_info->type = MONO_PATCH_INFO_WRAPPER;
1135                 }
1136
1137                 skip = FALSE;
1138                 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
1139                         if ((patch_info->type == MONO_PATCH_INFO_METHOD ||
1140                              patch_info->type == MONO_PATCH_INFO_METHODCONST) &&
1141                             patch_info->data.method->wrapper_type) {
1142                                 /* unable to handle this */
1143                                 //printf ("Skip (wrapper call):   %s %d -> %s\n", mono_method_full_name (method, TRUE), patch_info->type, mono_method_full_name (patch_info->data.method, TRUE));
1144                                 skip = TRUE;    
1145                                 break;
1146                         }
1147                 }
1148
1149                 if (skip) {
1150                         wrappercount++;
1151                         continue;
1152                 }
1153
1154                 //printf ("Compile:           %s\n", mono_method_full_name (method, TRUE));
1155
1156                 emitted [i] = TRUE;
1157                 emit_method (acfg, cfg);
1158
1159                 mono_destroy_compile (cfg);
1160
1161                 ccount++;
1162         }
1163
1164         /*
1165          * The icall and image tables are small but referenced in a lot of places.
1166          * So we emit them at once, and reference their elements by an index
1167          * instead of an assembly label to cut back on the number of relocations.
1168          */
1169
1170         /* Emit icall table */
1171
1172         symbol = g_strdup_printf ("mono_icall_table");
1173         fprintf (tmpfp, ".globl %s\n", symbol);
1174         fprintf (tmpfp, ".text 1 \n");
1175         fprintf (tmpfp, "\t.align 8\n");
1176         fprintf (tmpfp, "%s:\n", symbol);
1177         fprintf (tmpfp, ".long %d\n", acfg->icall_table->len);
1178         for (i = 0; i < acfg->icall_table->len; i++)
1179                 fprintf (tmpfp, ".string \"%s\"\n", (char*)g_ptr_array_index (acfg->icall_table, i));
1180
1181         /* Emit image table */
1182
1183         symbol = g_strdup_printf ("mono_image_table");
1184         fprintf (tmpfp, ".globl %s\n", symbol);
1185         fprintf (tmpfp, ".text 1 \n");
1186         fprintf (tmpfp, "\t.align 8\n");
1187         fprintf (tmpfp, "%s:\n", symbol);
1188         fprintf (tmpfp, ".long %d\n", acfg->image_table->len);
1189         for (i = 0; i < acfg->image_table->len; i++)
1190                 fprintf (tmpfp, ".string \"%s\"\n", ((MonoImage*)g_ptr_array_index (acfg->image_table, i))->guid);
1191
1192         /*
1193          * g_module_symbol takes a lot of time for failed lookups, so we emit
1194          * a table which contains one bit for each method. This bit specifies
1195          * whenever the method is emitted or not.
1196          */
1197
1198         symbol = g_strdup_printf ("mono_methods_present_table");
1199         fprintf (tmpfp, ".globl %s\n", symbol);
1200         fprintf (tmpfp, ".text 1 \n");
1201         fprintf (tmpfp, "\t.align 8\n");
1202         fprintf (tmpfp, "%s:\n", symbol);
1203         {
1204                 guint32 k, nrows;
1205                 guint32 w;
1206
1207                 nrows = image->tables [MONO_TABLE_METHOD].rows;
1208                 for (i = 0; i < nrows / 32 + 1; ++i) {
1209                         w = 0;
1210                         for (k = 0; k < 32; ++k) {
1211                                 if (emitted [(i * 32) + k])
1212                                         w += (1 << k);
1213                         }
1214                         //printf ("EMITTED [%d] = %d.\n", i, b);
1215                         fprintf (tmpfp, "\t.long %d\n", w);
1216                 }
1217         }
1218
1219         fclose (tmpfp);
1220
1221         com = g_strdup_printf ("as %s -o %s.o", tmpfname, tmpfname);
1222         printf ("Executing the native assembler: %s\n", com);
1223         system (com);
1224         g_free (com);
1225         com = g_strdup_printf ("ld -shared -o %s%s %s.o", image->name, SHARED_EXT, tmpfname);
1226         printf ("Executing the native linker: %s\n", com);
1227         system (com);
1228         g_free (com);
1229         com = g_strdup_printf ("%s.o", tmpfname);
1230         unlink (com);
1231         g_free (com);
1232         /*com = g_strdup_printf ("strip --strip-unneeded %s%s", image->name, SHARED_EXT);
1233         printf ("Stripping the binary: %s\n", com);
1234         system (com);
1235         g_free (com);*/
1236
1237         printf ("Compiled %d out of %d methods (%d%%)\n", ccount, mcount, (ccount*100)/mcount);
1238         printf ("%d methods contain absolute addresses (%d%%)\n", abscount, (abscount*100)/mcount);
1239         printf ("%d methods contain wrapper references (%d%%)\n", wrappercount, (wrappercount*100)/mcount);
1240         printf ("%d methods contain lmf pointers (%d%%)\n", lmfcount, (lmfcount*100)/mcount);
1241         printf ("%d methods have other problems (%d%%)\n", ocount, (ocount*100)/mcount);
1242         unlink (tmpfname);
1243
1244         return 0;
1245 }
1246
1247