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