2 * aot.c: mono Ahead of Time compiler
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2002 Ximian, Inc.
11 #include <sys/types.h>
15 #ifndef PLATFORM_WIN32
21 #include <limits.h> /* for PAGESIZE */
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>
39 #define SHARED_EXT ".dll"
41 #define SHARED_EXT ".so"
44 typedef struct MonoAotMethod {
46 MonoJumpInfo *patch_info;
49 typedef struct MonoAotModule {
50 /* Optimization flags used to compile the module */
52 /* Maps MonoMethods to MonoAotMethodInfos */
53 MonoGHashTable *methods;
55 MonoImage **image_table;
56 guint32* methods_present_table;
59 typedef struct MonoAotCompile {
62 GHashTable *icall_hash;
63 GPtrArray *icall_table;
64 GHashTable *image_hash;
65 GPtrArray *image_table;
68 static MonoGHashTable *aot_modules;
70 static CRITICAL_SECTION aot_mutex;
72 static guint32 mono_aot_verbose = 0;
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
81 static gboolean use_loaded_code = FALSE;
84 static gint32 mono_last_aot_method = -1;
87 decode_class_info (MonoAotModule *module, gpointer *data)
92 image = module->image_table [(guint32)data [1]];
96 return mono_class_get (image, (guint32)data [0]);
98 klass = decode_class_info (module, data [3]);
99 return mono_array_class_get (klass, (guint32)data [2]);
106 load_aot_module (MonoAssembly *assembly, gpointer user_data)
110 gboolean usable = TRUE;
111 char *saved_guid = NULL;
112 char *aot_version = NULL;
113 char *opt_flags = NULL;
115 aot_name = g_strdup_printf ("%s.so", assembly->image->name);
117 assembly->aot_module = g_module_open (aot_name, G_MODULE_BIND_LAZY);
119 if (!assembly->aot_module)
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);
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);
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);
140 g_module_close (assembly->aot_module);
141 assembly->aot_module = NULL;
146 * It seems that MonoGHashTables are in the GC heap, so structures
147 * containing them must be in the GC heap as well :(
150 info = GC_MALLOC (sizeof (MonoAotModule));
152 info = g_new0 (MonoAotModule, 1);
154 info->methods = mono_g_hash_table_new (NULL, NULL);
155 sscanf (opt_flags, "%d", &info->opts);
157 /* Read image table */
159 guint32 table_len, i;
162 g_module_symbol (assembly->aot_module, "mono_image_table", (gpointer *)&table);
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
179 g_module_close (assembly->aot_module);
180 assembly->aot_module = NULL;
183 table += strlen (table) + 1;
187 /* Read icall table */
189 guint32 table_len, i;
192 g_module_symbol (assembly->aot_module, "mono_icall_table", (gpointer *)&table);
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;
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);
208 EnterCriticalSection (&aot_mutex);
209 mono_g_hash_table_insert (aot_modules, assembly, info);
210 LeaveCriticalSection (&aot_mutex);
212 if (mono_aot_verbose > 0)
213 printf ("Loaded AOT Module for %s.\n", assembly->image->name);
219 InitializeCriticalSection (&aot_mutex);
221 aot_modules = mono_g_hash_table_new (NULL, NULL);
223 mono_install_assembly_load_hook (load_aot_module, NULL);
225 if (getenv ("MONO_LASTAOT"))
226 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
230 mono_aot_get_method_inner (MonoDomain *domain, MonoMethod *method)
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];
240 guint code_len, used_int_regs, used_strings;
241 MonoAotModule *aot_module;
242 MonoAotMethod *minfo;
244 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
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))
259 aot_module = (MonoAotModule*)mono_g_hash_table_lookup (aot_modules, ass);
261 g_assert (klass->inited);
263 minfo = mono_g_hash_table_lookup (aot_module->methods, method);
265 /* Duplicate jinfo */
266 jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
267 memcpy (jinfo, minfo->info, sizeof (MonoJitInfo));
268 if (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);
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 */
278 else if (!minfo->patch_info)
279 /* Use the same method in the new appdomain */
282 /* Create a copy of the original method and apply relocations */
284 code = mono_mempool_alloc (domain->code_mp, minfo->info->code_size);
285 memcpy (code, minfo->info->code_start, minfo->info->code_size);
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);
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);
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;
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;
314 /* Do a fast check to see whenever the method exists */
316 guint32 index = mono_metadata_token_index (method->token) - 1;
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));
326 sprintf (method_label, "m_%x", mono_metadata_token_index (method->token));
328 if (!g_module_symbol (module, method_label, (gpointer *)&code))
331 sprintf (info_label, "%s_p", method_label);
333 if (!g_module_symbol (module, info_label, (gpointer *)&info))
336 if (mono_last_aot_method != -1) {
337 if (mono_jit_stats.methods_aot > mono_last_aot_method)
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);
345 minfo = GC_MALLOC (sizeof (MonoAotMethod));
347 minfo = g_new0 (MonoAotMethod, 1);
350 jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
352 code_len = GPOINTER_TO_UINT (*((gpointer **)info));
354 used_int_regs = GPOINTER_TO_UINT (*((gpointer **)info));
357 if (!use_loaded_code) {
359 code2 = mono_mempool_alloc (domain->code_mp, code_len);
360 memcpy (code2, code, code_len);
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);
367 /* Exception table */
368 if (header->num_clauses) {
370 mono_mempool_alloc0 (domain->mp, sizeof (MonoJitExceptionInfo) * header->num_clauses);
371 jinfo->num_clauses = header->num_clauses;
373 jinfo->exvar_offset = GPOINTER_TO_UINT (*((gpointer**)info));
376 for (i = 0; i < header->num_clauses; ++i) {
377 MonoExceptionClause *ec = &header->clauses [i];
378 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
380 ei->flags = ec->flags;
381 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
382 ei->data.filter = code + GPOINTER_TO_UINT (*((gpointer**)info));
384 ei->data.token = GPOINTER_TO_UINT (*((gpointer**)info));
386 ei->try_start = code + GPOINTER_TO_UINT (*((gpointer**)info));
388 ei->try_end = code + GPOINTER_TO_UINT (*((gpointer**)info));
390 ei->handler_start = code + GPOINTER_TO_UINT (*((gpointer**)info));
395 if (aot_module->opts & MONO_OPT_SHARED) {
396 used_strings = GPOINTER_TO_UINT (*((gpointer **)info));
402 for (i = 0; i < used_strings; i++) {
403 guint token = GPOINTER_TO_UINT (*((gpointer **)info));
405 mono_ldstr (mono_root_domain, klass->image, mono_metadata_token_index (token));
415 guint32 last_offset, buf_len;
417 if (aot_module->opts & MONO_OPT_SHARED)
418 mp = mono_mempool_new ();
424 MonoJumpInfo *ji = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
430 b2 = *((guint8*)info + 1);
432 info = (gpointer*)((guint8*)info + 2);
436 if (((b1 & (1 + 2)) == 3) && (b2 == 255)) {
437 ji->ip.i = GPOINTER_TO_UINT (*info);
441 ji->ip.i = (((guint32)(b1 & (1 + 2))) << 8) + b2;
443 ji->ip.i += last_offset;
444 last_offset = ji->ip.i;
445 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
447 data = *((gpointer **)info);
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);
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);
462 case MONO_PATCH_INFO_IMAGE:
463 ji->data.image = aot_module->image_table [(guint32)data];
464 g_assert (ji->data.image);
466 case MONO_PATCH_INFO_METHOD:
467 case MONO_PATCH_INFO_METHODCONST:
468 case MONO_PATCH_INFO_METHOD_JUMP: {
469 guint32 image_index, token;
471 image_index = (guint32)data >> 24;
472 token = MONO_TOKEN_METHOD_DEF | ((guint32)data & 0xffffff);
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);
481 case MONO_PATCH_INFO_WRAPPER: {
482 guint32 image_index, token;
483 guint32 wrapper_type;
485 wrapper_type = (guint32)data[0];
486 image_index = (guint32)data[1] >> 24;
487 token = MONO_TOKEN_METHOD_DEF | ((guint32)data[1] & 0xffffff);
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);
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);
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]);
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);
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];
519 case MONO_PATCH_INFO_R4:
520 case MONO_PATCH_INFO_R8:
521 ji->data.target = data;
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]);
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;
535 case MONO_PATCH_INFO_METHOD_REL:
536 ji->data.offset = (int)data [0];
539 g_warning ("unhandled type %d", ji->type);
540 g_assert_not_reached ();
544 ji->next = patch_info;
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);
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);
563 g_assert (VirtualProtect (code, code_len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
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);
573 if (aot_module->opts & MONO_OPT_SHARED)
574 /* No need to cache patches */
575 mono_mempool_destroy (mp);
577 minfo->patch_info = patch_info;
580 mono_jit_stats.methods_aot++;
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;
590 mono_g_hash_table_insert (aot_module->methods, method, minfo);
597 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
601 EnterCriticalSection (&aot_mutex);
602 info = mono_aot_get_method_inner (domain, method);
603 LeaveCriticalSection (&aot_mutex);
605 /* Do this outside the lock */
607 mono_jit_info_table_add (domain, info);
616 write_data_symbol (FILE *fp, const char *name, guint8 *buf, int size, int align)
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]);
633 write_string_symbol (FILE *fp, const char *name, const char *value)
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);
642 mono_get_field_token (MonoClassField *field)
644 MonoClass *klass = field->parent;
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);
652 g_assert_not_reached ();
657 get_image_index (MonoAotCompile *cfg, MonoImage *image)
661 index = GPOINTER_TO_UINT (g_hash_table_lookup (cfg->image_hash, image));
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);
673 emit_image_index (MonoAotCompile *cfg, MonoImage *image)
677 image_index = get_image_index (cfg, image);
679 fprintf (cfg->fp, "\t.long %d\n", image_index);
683 cond_emit_klass_label (MonoAotCompile *cfg, MonoClass *klass)
685 char *l1, *el = NULL;
687 if ((l1 = g_hash_table_lookup (cfg->ref_hash, klass)))
690 if (!klass->type_token) {
691 g_assert (klass->rank > 0);
692 el = cond_emit_klass_label (cfg, klass->element_class);
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);
702 fprintf (cfg->fp, "\t.long %d\n", klass->rank);
703 fprintf (cfg->fp, "\t.long %s\n", el);
706 g_hash_table_insert (cfg->ref_hash, klass, l1);
712 cond_emit_field_label (MonoAotCompile *cfg, MonoJumpInfo *patch_info)
714 MonoClassField *field = patch_info->data.field;
718 if ((l1 = g_hash_table_lookup (cfg->ref_hash, field)))
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);
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);
730 g_hash_table_insert (cfg->ref_hash, field, l1);
736 compare_patches (gconstpointer a, gconstpointer b)
740 i = (*(MonoJumpInfo**)a)->ip.i;
741 j = (*(MonoJumpInfo**)b)->ip.i;
753 emit_method (MonoAotCompile *acfg, MonoCompile *cfg)
759 guint8 *code, *mname;
760 int func_alignment = 16;
762 MonoJumpInfo *patch_info;
763 MonoMethodHeader *header;
766 method = cfg->method;
767 code = cfg->native_code;
768 header = ((MonoMethodNormal*)method)->header;
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);
777 for (i = 0; i < cfg->code_len; i++)
778 fprintf (tmpfp, ".byte %d\n", (unsigned int) code [i]);
780 fprintf (tmpfp, ".text 1\n");
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);
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 */
796 case MONO_PATCH_INFO_SWITCH: {
797 gpointer *table = (gpointer *)patch_info->data.target;
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);
804 for (k = 0; k < patch_info->table_size; k++) {
805 fprintf (tmpfp, "\t.long %d\n", (int)table [k]);
810 case MONO_PATCH_INFO_INTERNAL_METHOD: {
813 icall_index = (guint32)g_hash_table_lookup (acfg->icall_hash, patch_info->data.name);
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);
820 patch_info->data.name = g_strdup_printf ("%d", icall_index - 1);
824 case MONO_PATCH_INFO_METHODCONST:
825 case MONO_PATCH_INFO_METHOD:
826 case MONO_PATCH_INFO_METHOD_JUMP: {
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.
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);
837 patch_info->data.name =
838 g_strdup_printf ("%d", (image_index << 24) + (mono_metadata_token_index (token)));
842 case MONO_PATCH_INFO_WRAPPER: {
847 m = mono_marshal_method_from_wrapper (patch_info->data.method);
848 image_index = get_image_index (acfg, m->klass->image);
850 g_assert (image_index < 256);
851 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
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)));
860 case MONO_PATCH_INFO_FIELD:
861 patch_info->data.name = cond_emit_field_label (acfg, patch_info);
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);
869 case MONO_PATCH_INFO_IMAGE:
870 patch_info->data.name = g_strdup_printf ("%d", get_image_index (acfg, patch_info->data.image));
873 case MONO_PATCH_INFO_EXC_NAME: {
877 mono_class_from_name (mono_defaults.exception_class->image,
878 "System", patch_info->data.target);
880 patch_info->data.name = cond_emit_klass_label (acfg, ex_class);
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));
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));
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);
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);
908 case MONO_PATCH_INFO_SFLDA:
909 patch_info->data.name = cond_emit_field_label (acfg, patch_info);
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);
922 g_warning ("unable to handle jump info %d", patch_info->type);
923 g_assert_not_reached ();
927 fprintf (tmpfp, ".globl %s_p\n", mname);
928 fprintf (tmpfp, "\t.align %d\n", sizeof (gpointer));
929 fprintf (tmpfp, "%s_p:\n", mname);
931 fprintf (tmpfp, "\t.long %d\n", cfg->code_len);
932 fprintf (tmpfp, "\t.long %d\n", cfg->used_int_regs);
934 /* Exception table */
935 if (header->num_clauses) {
936 MonoJitInfo *jinfo = cfg->jit_info;
938 fprintf (tmpfp, "\t.long %d\n", jinfo->exvar_offset);
940 for (k = 0; k < header->num_clauses; ++k) {
941 MonoJitExceptionInfo *ei = &jinfo->clauses [k];
943 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
944 fprintf (tmpfp, "\t.long %d\n", (guint8*)ei->data.filter - code);
946 /* fixme: tokens are not global */
947 fprintf (tmpfp, "\t.long %d\n", ei->data.token);
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);
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);
963 /* Used only in shared mode */
964 g_assert (!cfg->ldstr_list);
966 //printf ("M: %s (%s).\n", mono_method_full_name (method, TRUE), mname);
973 for (pindex = 0; pindex < patches->len; ++pindex) {
975 patch_info = g_ptr_array_index (patches, pindex);
977 if ((patch_info->type == MONO_PATCH_INFO_LABEL) ||
978 (patch_info->type == MONO_PATCH_INFO_BB))
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;
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));
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);
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);
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);
1025 case MONO_PATCH_INFO_LABEL:
1026 case MONO_PATCH_INFO_BB:
1029 g_warning ("unable to handle jump info %d", patch_info->type);
1030 g_assert_not_reached ();
1037 * 0 is PATCH_INFO_BB, which can't be in the file.
1039 /* NULL terminated array */
1040 fprintf (tmpfp, "\t.long 0\n");
1046 mono_debug_serialize_debug_info (cfg, &buf, &buf_len);
1048 fprintf (tmpfp, "\t.long %d\n", buf_len);
1050 for (i = 0; i < buf_len; ++i)
1051 fprintf (tmpfp, ".byte %d\n", (unsigned int) buf [i]);
1057 /* fixme: save the rest of the required infos */
1063 mono_compile_assembly (MonoAssembly *ass, guint32 opts)
1066 MonoImage *image = ass->image;
1068 char *com, *tmpfname, *opts_str;
1072 int ccount = 0, mcount = 0, lmfcount = 0, abscount = 0, wrappercount = 0, ocount = 0;
1073 GHashTable *ref_hash;
1074 MonoAotCompile *acfg;
1077 printf ("Mono Ahead of Time compiler - compiling assembly %s\n", image->name);
1079 i = g_file_open_tmp ("mono_aot_XXXXXX", &tmpfname, NULL);
1080 tmpfp = fdopen (i, "w+");
1083 ref_hash = g_hash_table_new (NULL, NULL);
1085 acfg = g_new0 (MonoAotCompile, 1);
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 ();
1093 write_string_symbol (tmpfp, "mono_assembly_guid" , image->guid);
1095 write_string_symbol (tmpfp, "mono_aot_version", MONO_AOT_FILE_VERSION);
1097 opts_str = g_strdup_printf ("%d", opts);
1098 write_string_symbol (tmpfp, "mono_aot_opt_flags", opts_str);
1101 emitted = g_new0 (gboolean, image->tables [MONO_TABLE_METHOD].rows);
1103 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
1104 MonoJumpInfo *patch_info;
1106 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
1107 method = mono_get_method (image, token, NULL);
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));
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));
1127 //printf ("START: %s\n", mono_method_full_name (method, TRUE));
1128 //mono_compile_method (method);
1130 cfg = mini_method_compile (method, opts, mono_root_domain, FALSE, 0);
1133 if (cfg->disable_aot) {
1134 printf ("Skip (other): %s\n", mono_method_full_name (method, TRUE));
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);
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;
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));
1178 //printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
1181 emit_method (acfg, cfg);
1183 mono_destroy_compile (cfg);
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.
1194 /* Emit icall table */
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));
1205 /* Emit image table */
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);
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.
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);
1231 nrows = image->tables [MONO_TABLE_METHOD].rows;
1232 for (i = 0; i < nrows / 32 + 1; ++i) {
1234 for (k = 0; k < 32; ++k) {
1235 if (emitted [(i * 32) + k])
1238 //printf ("EMITTED [%d] = %d.\n", i, b);
1239 fprintf (tmpfp, "\t.long %d\n", w);
1245 com = g_strdup_printf ("as %s -o %s.o", tmpfname, tmpfname);
1246 printf ("Executing the native assembler: %s\n", 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);
1253 com = g_strdup_printf ("%s.o", tmpfname);
1256 /*com = g_strdup_printf ("strip --strip-unneeded %s%s", image->name, SHARED_EXT);
1257 printf ("Stripping the binary: %s\n", com);
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);