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/metadata-internals.h>
34 #include <mono/metadata/marshal.h>
35 #include <mono/os/gc_wrapper.h>
40 #define SHARED_EXT ".dll"
41 #elif defined(__ppc__) && defined(__MACH__)
42 #define SHARED_EXT ".dylib"
44 #define SHARED_EXT ".so"
47 #if defined(sparc) || defined(__ppc__)
48 #define AS_STRING_DIRECTIVE ".asciz"
51 #define AS_STRING_DIRECTIVE ".string"
54 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
56 typedef struct MonoAotMethod {
58 MonoJumpInfo *patch_info;
62 typedef struct MonoAotModule {
63 /* Optimization flags used to compile the module */
65 /* Maps MonoMethods to MonoAotMethodInfos */
66 MonoGHashTable *methods;
68 MonoImage **image_table;
69 guint32* methods_present_table;
72 typedef struct MonoAotCompile {
75 GHashTable *icall_hash;
76 GPtrArray *icall_table;
77 GHashTable *image_hash;
78 GPtrArray *image_table;
81 static MonoGHashTable *aot_modules;
83 static CRITICAL_SECTION aot_mutex;
85 static guint32 mono_aot_verbose = 0;
88 * Disabling this will make a copy of the loaded code and use the copy instead
89 * of the original. This will place the caller and the callee close to each
90 * other in memory, possibly improving cache behavior. Since the original
91 * code is in copy-on-write memory, this will not increase the memory usage
94 static gboolean use_loaded_code = FALSE;
97 static gint32 mono_last_aot_method = -1;
100 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info);
103 decode_class_info (MonoAotModule *module, guint32 *data)
108 image = module->image_table [data [1]];
112 return mono_class_get (image, data [0]);
114 /* the pointer is dword aligned so it is in data [4] */
115 klass = decode_class_info (module, *(guint32**)&(data [4]));
116 return mono_array_class_get (klass, data [2]);
123 load_aot_module (MonoAssembly *assembly, gpointer user_data)
127 gboolean usable = TRUE;
128 char *saved_guid = NULL;
129 char *aot_version = NULL;
130 char *opt_flags = NULL;
132 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
134 assembly->aot_module = g_module_open (aot_name, G_MODULE_BIND_LAZY);
136 if (!assembly->aot_module) {
137 if (mono_aot_verbose > 0)
138 printf ("Failed to load AOT module %s: %s\n", aot_name, g_module_error ());
143 g_module_symbol (assembly->aot_module, "mono_assembly_guid", (gpointer *) &saved_guid);
144 g_module_symbol (assembly->aot_module, "mono_aot_version", (gpointer *) &aot_version);
145 g_module_symbol (assembly->aot_module, "mono_aot_opt_flags", (gpointer *)&opt_flags);
147 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
148 if (mono_aot_verbose > 0)
149 printf ("AOT module %s has wrong file format version (expected %s got %s)\n", aot_name, MONO_AOT_FILE_VERSION, aot_version);
153 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
154 if (mono_aot_verbose > 0)
155 printf ("AOT module %s is out of date.\n", aot_name);
161 g_module_close (assembly->aot_module);
162 assembly->aot_module = NULL;
167 * It seems that MonoGHashTables are in the GC heap, so structures
168 * containing them must be in the GC heap as well :(
171 info = GC_MALLOC (sizeof (MonoAotModule));
173 info = g_new0 (MonoAotModule, 1);
175 info->methods = mono_g_hash_table_new (NULL, NULL);
176 sscanf (opt_flags, "%d", &info->opts);
178 /* Read image table */
180 guint32 table_len, i;
183 g_module_symbol (assembly->aot_module, "mono_image_table", (gpointer *)&table);
186 table_len = *(guint32*)table;
187 table += sizeof (guint32);
188 info->image_table = g_new0 (MonoImage*, table_len);
189 for (i = 0; i < table_len; ++i) {
190 info->image_table [i] = mono_image_loaded_by_guid (table);
191 if (!info->image_table [i]) {
192 if (mono_aot_verbose > 0)
193 printf ("AOT module %s is out of date.\n", aot_name);
194 mono_g_hash_table_destroy (info->methods);
195 g_free (info->image_table);
196 #ifndef HAVE_BOEHM_GC
200 g_module_close (assembly->aot_module);
201 assembly->aot_module = NULL;
204 table += strlen (table) + 1;
208 /* Read icall table */
210 guint32 table_len, i;
213 g_module_symbol (assembly->aot_module, "mono_icall_table", (gpointer *)&table);
216 table_len = *(guint32*)table;
217 table += sizeof (guint32);
218 info->icall_table = g_new0 (char*, table_len);
219 for (i = 0; i < table_len; ++i) {
220 info->icall_table [i] = table;
221 table += strlen (table) + 1;
225 /* Read methods present table */
226 g_module_symbol (assembly->aot_module, "mono_methods_present_table", (gpointer *)&info->methods_present_table);
227 g_assert (info->methods_present_table);
229 EnterCriticalSection (&aot_mutex);
230 mono_g_hash_table_insert (aot_modules, assembly, info);
231 LeaveCriticalSection (&aot_mutex);
233 if (mono_aot_verbose > 0)
234 printf ("Loaded AOT Module for %s.\n", assembly->image->name);
240 InitializeCriticalSection (&aot_mutex);
242 aot_modules = mono_g_hash_table_new (NULL, NULL);
244 mono_install_assembly_load_hook (load_aot_module, NULL);
246 if (getenv ("MONO_LASTAOT"))
247 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
251 mono_aot_get_method_inner (MonoDomain *domain, MonoMethod *method)
253 MonoClass *klass = method->klass;
254 MonoAssembly *ass = klass->image->assembly;
255 GModule *module = ass->aot_module;
256 char method_label [256];
257 char info_label [256];
260 MonoAotModule *aot_module;
261 MonoAotMethod *minfo;
263 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
272 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
275 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
276 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
277 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
278 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
281 aot_module = (MonoAotModule*)mono_g_hash_table_lookup (aot_modules, ass);
283 g_assert (klass->inited);
285 minfo = mono_g_hash_table_lookup (aot_module->methods, method);
286 /* Can't use code from non-root domains since they can be unloaded */
287 if (minfo && (minfo->domain == mono_get_root_domain ())) {
288 /* This method was already loaded in another appdomain */
290 /* Duplicate jinfo */
291 jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
292 memcpy (jinfo, minfo->info, sizeof (MonoJitInfo));
293 if (jinfo->clauses) {
295 mono_mempool_alloc0 (domain->mp, sizeof (MonoJitExceptionInfo) * header->num_clauses);
296 memcpy (jinfo->clauses, minfo->info->clauses, sizeof (MonoJitExceptionInfo) * header->num_clauses);
299 if (aot_module->opts & MONO_OPT_SHARED)
300 /* Use the same method in the new appdomain */
302 else if (!minfo->patch_info)
303 /* Use the same method in the new appdomain */
306 /* Create a copy of the original method and apply relocations */
308 code = mono_code_manager_reserve (domain->code_mp, minfo->info->code_size);
309 memcpy (code, minfo->info->code_start, minfo->info->code_size);
311 if (mono_aot_verbose > 1)
312 printf ("REUSE METHOD: %s %p - %p.\n", mono_method_full_name (method, TRUE), code, (char*)code + minfo->info->code_size);
314 /* Do this outside the lock to avoid deadlocks */
315 LeaveCriticalSection (&aot_mutex);
316 mono_arch_patch_code (method, domain, code, minfo->patch_info, TRUE);
317 EnterCriticalSection (&aot_mutex);
318 mono_arch_flush_icache (code, minfo->info->code_size);
321 jinfo->code_start = code;
322 if (jinfo->clauses) {
323 for (i = 0; i < header->num_clauses; ++i) {
324 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
325 gint32 offset = code - (guint8*)minfo->info->code_start;
327 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
328 ei->data.filter = (guint8*)ei->data.filter + offset;
329 ei->try_start = (guint8*)ei->try_start + offset;
330 ei->try_end = (guint8*)ei->try_end + offset;
331 ei->handler_start = (guint8*)ei->handler_start + offset;
339 /* Do a fast check to see whenever the method exists */
341 guint32 index = mono_metadata_token_index (method->token) - 1;
343 w = aot_module->methods_present_table [index / 32];
344 if (! (w & (1 << (index % 32)))) {
345 if (mono_aot_verbose > 1)
346 printf ("NOT FOUND: %s.\n", mono_method_full_name (method, TRUE));
351 sprintf (method_label, "m_%x", mono_metadata_token_index (method->token));
353 if (!g_module_symbol (module, method_label, (gpointer *)&code))
356 sprintf (info_label, "%s_p", method_label);
358 if (!g_module_symbol (module, info_label, (gpointer *)&info))
361 if (mono_last_aot_method != -1) {
362 if (mono_jit_stats.methods_aot > mono_last_aot_method)
365 if (mono_jit_stats.methods_aot == mono_last_aot_method)
366 printf ("LAST AOT METHOD: %s.%s.%s.\n", klass->name_space, klass->name, method->name);
369 return mono_aot_load_method (domain, aot_module, method, code, info);
373 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info)
375 MonoClass *klass = method->klass;
376 MonoJumpInfo *patch_info = NULL;
377 guint code_len, used_int_regs, used_strings;
378 MonoAotMethod *minfo;
380 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
385 minfo = GC_MALLOC (sizeof (MonoAotMethod));
387 minfo = g_new0 (MonoAotMethod, 1);
390 minfo->domain = domain;
391 jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
393 code_len = *(guint32*)info;
395 used_int_regs = *(guint32*)info;
398 if (!use_loaded_code) {
400 code2 = mono_code_manager_reserve (domain->code_mp, code_len);
401 memcpy (code2, code, code_len);
402 mono_arch_flush_icache (code2, code_len);
406 if (mono_aot_verbose > 1)
407 printf ("FOUND AOT compiled code for %s %p - %p %p\n", mono_method_full_name (method, TRUE), code, code + code_len, info);
409 /* Exception table */
410 if (header->num_clauses) {
412 mono_mempool_alloc0 (domain->mp, sizeof (MonoJitExceptionInfo) * header->num_clauses);
413 jinfo->num_clauses = header->num_clauses;
415 jinfo->exvar_offset = *(guint32*)info;
418 for (i = 0; i < header->num_clauses; ++i) {
419 MonoExceptionClause *ec = &header->clauses [i];
420 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
422 ei->flags = ec->flags;
423 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
424 ei->data.filter = code + *(guint32*)info;
426 ei->data.token = *(guint32*)info;
428 ei->try_start = code + *(guint32*)info;
430 ei->try_end = code + *(guint32*)info;
432 ei->handler_start = code + *(guint32*)info;
437 if (aot_module->opts & MONO_OPT_SHARED) {
438 used_strings = *(guint32*)info;
444 for (i = 0; i < used_strings; i++) {
445 guint token = *(guint32*)info;
447 mono_ldstr (mono_get_root_domain (), klass->image, mono_metadata_token_index (token));
457 guint32 last_offset, buf_len;
460 if (aot_module->opts & MONO_OPT_SHARED)
461 mp = mono_mempool_new ();
465 /* First load the type + offset table */
467 patches = g_ptr_array_new ();
469 MonoJumpInfo *ji = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
474 b2 = *((guint8*)info + 1);
480 if (((b1 & (1 + 2)) == 3) && (b2 == 255)) {
481 info = ALIGN_PTR_TO (info, 4);
482 ji->ip.i = *(guint32*)info;
486 ji->ip.i = (((guint32)(b1 & (1 + 2))) << 8) + b2;
488 ji->ip.i += last_offset;
489 last_offset = ji->ip.i;
490 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
492 ji->next = patch_info;
495 g_ptr_array_add (patches, ji);
499 info = ALIGN_PTR_TO (info, sizeof (gpointer));
501 /* Then load the other data */
502 for (pindex = 0; pindex < patches->len; ++pindex) {
503 MonoJumpInfo *ji = g_ptr_array_index (patches, pindex);
505 data = *((guint32 **)info);
506 info += sizeof (gpointer);
509 case MONO_PATCH_INFO_CLASS:
510 case MONO_PATCH_INFO_IID:
511 ji->data.klass = decode_class_info (aot_module, data);
512 g_assert (ji->data.klass);
513 mono_class_init (ji->data.klass);
515 case MONO_PATCH_INFO_VTABLE:
516 case MONO_PATCH_INFO_CLASS_INIT:
517 ji->data.klass = decode_class_info (aot_module, data);
518 g_assert (ji->data.klass);
519 mono_class_init (ji->data.klass);
521 case MONO_PATCH_INFO_IMAGE:
522 ji->data.image = aot_module->image_table [(guint32)data];
523 g_assert (ji->data.image);
525 case MONO_PATCH_INFO_METHOD:
526 case MONO_PATCH_INFO_METHODCONST:
527 case MONO_PATCH_INFO_METHOD_JUMP: {
528 guint32 image_index, token;
530 image_index = (guint32)data >> 24;
531 token = MONO_TOKEN_METHOD_DEF | ((guint32)data & 0xffffff);
533 image = aot_module->image_table [image_index];
534 ji->data.method = mono_get_method (image, token, NULL);
535 g_assert (ji->data.method);
536 mono_class_init (ji->data.method->klass);
540 case MONO_PATCH_INFO_WRAPPER: {
541 guint32 image_index, token;
542 guint32 wrapper_type;
544 wrapper_type = (guint32)data[0];
545 image_index = (guint32)data[1] >> 24;
546 token = MONO_TOKEN_METHOD_DEF | ((guint32)data[1] & 0xffffff);
548 image = aot_module->image_table [image_index];
549 ji->data.method = mono_get_method (image, token, NULL);
550 g_assert (ji->data.method);
551 mono_class_init (ji->data.method->klass);
553 g_assert (wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK);
554 ji->type = MONO_PATCH_INFO_METHOD;
555 ji->data.method = mono_marshal_get_remoting_invoke_with_check (ji->data.method);
558 case MONO_PATCH_INFO_FIELD:
559 case MONO_PATCH_INFO_SFLDA: {
560 /* The pointer is dword aligned so it is in data [2] */
561 gpointer class_ptr = *(gpointer*)(data + 2);
562 MonoClass *klass = decode_class_info (aot_module, class_ptr);
563 mono_class_init (klass);
564 ji->data.field = mono_class_get_field (klass, data [0]);
567 case MONO_PATCH_INFO_INTERNAL_METHOD:
568 ji->data.name = aot_module->icall_table [(guint32)data];
569 g_assert (ji->data.name);
570 //printf ("A: %s.\n", ji->data.name);
572 case MONO_PATCH_INFO_SWITCH:
573 ji->table_size = data [0];
574 table = g_new (gpointer, ji->table_size);
575 ji->data.target = table;
576 for (i = 0; i < ji->table_size; i++) {
577 table [i] = data [i + 1];
580 case MONO_PATCH_INFO_R4:
581 case MONO_PATCH_INFO_R8:
582 ji->data.target = data;
584 case MONO_PATCH_INFO_LDSTR:
585 case MONO_PATCH_INFO_LDTOKEN:
586 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
587 image = aot_module->image_table [data [0]];
588 ji->data.token = mono_jump_info_token_new (mp, image, data [1]);
590 case MONO_PATCH_INFO_EXC_NAME:
591 ji->data.klass = decode_class_info (aot_module, data);
592 g_assert (ji->data.klass);
593 mono_class_init (ji->data.klass);
594 ji->data.name = ji->data.klass->name;
596 case MONO_PATCH_INFO_METHOD_REL:
597 ji->data.offset = data [0];
600 g_warning ("unhandled type %d", ji->type);
601 g_assert_not_reached ();
605 g_ptr_array_free (patches, TRUE);
608 buf_len = *(guint32*)info;
610 mono_debug_add_aot_method (domain, method, code, info, buf_len);
612 if (use_loaded_code) {
613 /* disable write protection */
614 #ifndef PLATFORM_WIN32
615 page_start = (char *) (((int) (code)) & ~ (PAGESIZE - 1));
616 pages = (code + code_len - page_start + PAGESIZE - 1) / PAGESIZE;
617 err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
622 g_assert (VirtualProtect (code, code_len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
627 /* Do this outside the lock to avoid deadlocks */
628 LeaveCriticalSection (&aot_mutex);
629 mono_arch_patch_code (method, domain, code, patch_info, TRUE);
630 EnterCriticalSection (&aot_mutex);
632 if (aot_module->opts & MONO_OPT_SHARED)
633 /* No need to cache patches */
634 mono_mempool_destroy (mp);
636 minfo->patch_info = patch_info;
639 mono_jit_stats.methods_aot++;
642 jinfo->code_size = code_len;
643 jinfo->used_regs = used_int_regs;
644 jinfo->method = method;
645 jinfo->code_start = code;
646 jinfo->domain_neutral = (aot_module->opts & MONO_OPT_SHARED) != 0;
649 mono_g_hash_table_insert (aot_module->methods, method, minfo);
656 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
660 EnterCriticalSection (&aot_mutex);
661 info = mono_aot_get_method_inner (domain, method);
662 LeaveCriticalSection (&aot_mutex);
664 /* Do this outside the lock */
666 mono_jit_info_table_add (domain, info);
674 emit_section_change (FILE *fp, const char *section_name, int subsection_index)
677 /* For solaris as, GNU as should accept the same */
678 fprintf (fp, ".section \"%s\"\n", section_name);
679 #elif defined(__ppc__) && defined(__MACH__)
680 /* This needs to be made more precise on mach. */
681 fprintf (fp, "%s\n", subsection_index == 0 ? ".text" : ".data");
683 fprintf (fp, "%s %d\n", section_name, subsection_index);
688 emit_global (FILE *fp, const char *name)
690 #if defined(__ppc__) && defined(__MACH__)
691 // mach-o always uses a '_' prefix.
692 fprintf (fp, ".globl _%s\n", name);
694 fprintf (fp, ".globl %s\n", name);
699 emit_label (FILE *fp, const char *name)
701 #if defined(__ppc__) && defined(__MACH__)
702 // mach-o always uses a '_' prefix.
703 fprintf (fp, "_%s:\n", name);
705 fprintf (fp, "%s:\n", name);
711 write_data_symbol (FILE *fp, const char *name, guint8 *buf, int size, int align)
715 emit_section_change (fp, ".text", 1);
717 fprintf (fp, ".globl %s\n", name);
718 fprintf (fp, "\t.align %d\n", align);
719 fprintf (fp, "\t.type %s,#object\n", name);
720 fprintf (fp, "\t.size %s,%d\n", name, size);
721 fprintf (fp, "%s:\n", name);
722 for (i = 0; i < size; i++) {
723 fprintf (fp, ".byte %d\n", buf [i]);
730 write_string_symbol (FILE *fp, const char *name, const char *value)
732 emit_section_change (fp, ".text", 1);
733 emit_global(fp, name);
734 emit_label(fp, name);
735 fprintf (fp, "\t%s \"%s\"\n", AS_STRING_DIRECTIVE, value);
739 mono_get_field_token (MonoClassField *field)
741 MonoClass *klass = field->parent;
744 for (i = 0; i < klass->field.count; ++i) {
745 if (field == &klass->fields [i])
746 return MONO_TOKEN_FIELD_DEF | (klass->field.first + 1 + i);
749 g_assert_not_reached ();
754 get_image_index (MonoAotCompile *cfg, MonoImage *image)
758 index = GPOINTER_TO_UINT (g_hash_table_lookup (cfg->image_hash, image));
762 index = g_hash_table_size (cfg->image_hash);
763 g_hash_table_insert (cfg->image_hash, image, GUINT_TO_POINTER (index + 1));
764 g_ptr_array_add (cfg->image_table, image);
770 emit_image_index (MonoAotCompile *cfg, MonoImage *image)
774 image_index = get_image_index (cfg, image);
776 fprintf (cfg->fp, "\t.long %d\n", image_index);
779 #if defined(__ppc__) && defined(__MACH__)
781 ilog2(register int value)
784 while (value & ~0xf) count += 4, value >>= 4;
785 while (value) count++, value >>= 1;
790 static void emit_alignment(FILE *fp, int size)
792 #if defined(__ppc__) && defined(__MACH__)
793 // the mach-o assembler specifies alignments as powers of 2.
794 fprintf (fp, "\t.align %d\t; ilog2\n", ilog2(size));
795 #elif defined(__powerpc__)
796 /* ignore on linux/ppc */
798 fprintf (fp, "\t.align %d\n", size);
803 emit_pointer (FILE *fp, char *target)
805 #if defined(sparc) && SIZEOF_VOID_P == 8
806 emit_alignment (fp, 8);
807 fprintf (fp, "\t.xword %s\n", target);
809 fprintf (fp, "\t.long %s\n", target);
814 cond_emit_klass_label (MonoAotCompile *cfg, MonoClass *klass)
816 char *l1, *el = NULL;
818 if ((l1 = g_hash_table_lookup (cfg->ref_hash, klass)))
821 if (!klass->type_token) {
822 g_assert (klass->rank > 0);
823 el = cond_emit_klass_label (cfg, klass->element_class);
826 emit_alignment(cfg->fp, sizeof (gpointer));
828 l1 = g_strdup_printf ("klass_p_%08x_%p", klass->type_token, klass);
829 fprintf (cfg->fp, "%s:\n", l1);
830 fprintf (cfg->fp, "\t.long 0x%08x\n", klass->type_token);
831 emit_image_index (cfg, klass->image);
834 fprintf (cfg->fp, "\t.long %d\n", klass->rank);
835 emit_pointer (cfg->fp, el);
838 g_hash_table_insert (cfg->ref_hash, klass, l1);
844 cond_emit_field_label (MonoAotCompile *cfg, MonoJumpInfo *patch_info)
846 MonoClassField *field = patch_info->data.field;
850 if ((l1 = g_hash_table_lookup (cfg->ref_hash, field)))
853 l2 = cond_emit_klass_label (cfg, field->parent);
854 emit_alignment(cfg->fp, sizeof (gpointer));
855 token = mono_get_field_token (field);
857 l1 = g_strdup_printf ("klass_p_%08x_%p", token, field);
858 fprintf (cfg->fp, "%s:\n", l1);
859 fprintf (cfg->fp, "\t.long 0x%08x\n", token);
860 emit_pointer (cfg->fp, l2);
862 g_hash_table_insert (cfg->ref_hash, field, l1);
868 compare_patches (gconstpointer a, gconstpointer b)
872 i = (*(MonoJumpInfo**)a)->ip.i;
873 j = (*(MonoJumpInfo**)b)->ip.i;
885 emit_method (MonoAotCompile *acfg, MonoCompile *cfg)
891 guint8 *code, *mname, *mname_p;
892 int func_alignment = 16;
894 MonoJumpInfo *patch_info;
895 MonoMethodHeader *header;
898 method = cfg->method;
899 code = cfg->native_code;
900 header = ((MonoMethodNormal*)method)->header;
902 emit_section_change (tmpfp, ".text", 0);
903 mname = g_strdup_printf ("m_%x", mono_metadata_token_index (method->token));
904 mname_p = g_strdup_printf ("%s_p", mname);
905 emit_alignment(tmpfp, func_alignment);
906 emit_global(tmpfp, mname);
908 fprintf (tmpfp, "\t.type %s,#function\n", mname);
909 #elif !(defined(__ppc__) && defined(__MACH__))
910 fprintf (tmpfp, "\t.type %s,@function\n", mname);
912 emit_label(tmpfp, mname);
914 for (i = 0; i < cfg->code_len; i++)
915 fprintf (tmpfp, ".byte %d\n", (unsigned int) code [i]);
917 emit_section_change (tmpfp, ".text", 1);
919 /* Sort relocations */
920 patches = g_ptr_array_new ();
921 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next)
922 g_ptr_array_add (patches, patch_info);
923 g_ptr_array_sort (patches, compare_patches);
925 /* Emit out of line info for relocations */
927 for (pindex = 0; pindex < patches->len; ++pindex) {
928 patch_info = g_ptr_array_index (patches, pindex);
929 switch (patch_info->type) {
930 case MONO_PATCH_INFO_LABEL:
931 case MONO_PATCH_INFO_BB:
932 /* relative jumps are no problem, there is no need to handle then here */
934 case MONO_PATCH_INFO_SWITCH: {
935 gpointer *table = (gpointer *)patch_info->data.target;
938 emit_alignment(tmpfp, sizeof (gpointer));
939 fprintf (tmpfp, "%s_p_%d:\n", mname, j);
940 fprintf (tmpfp, "\t.long %d\n", patch_info->table_size);
942 for (k = 0; k < patch_info->table_size; k++) {
943 fprintf (tmpfp, "\t.long %d\n", (int)table [k]);
948 case MONO_PATCH_INFO_INTERNAL_METHOD: {
951 icall_index = (guint32)g_hash_table_lookup (acfg->icall_hash, patch_info->data.name);
953 icall_index = g_hash_table_size (acfg->icall_hash) + 1;
954 g_hash_table_insert (acfg->icall_hash, (gpointer)patch_info->data.name,
955 GUINT_TO_POINTER (icall_index));
956 g_ptr_array_add (acfg->icall_table, (gpointer)patch_info->data.name);
958 patch_info->data.name = g_strdup_printf ("%d", icall_index - 1);
962 case MONO_PATCH_INFO_METHODCONST:
963 case MONO_PATCH_INFO_METHOD:
964 case MONO_PATCH_INFO_METHOD_JUMP: {
966 * The majority of patches are for methods, so we emit
967 * them inline instead of defining a label for them to
968 * decrease the number of relocations.
970 guint32 image_index = get_image_index (acfg, patch_info->data.method->klass->image);
971 guint32 token = patch_info->data.method->token;
972 g_assert (image_index < 256);
973 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
975 patch_info->data.name =
976 g_strdup_printf ("%d", (image_index << 24) + (mono_metadata_token_index (token)));
980 case MONO_PATCH_INFO_WRAPPER: {
985 m = mono_marshal_method_from_wrapper (patch_info->data.method);
986 image_index = get_image_index (acfg, m->klass->image);
988 g_assert (image_index < 256);
989 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
991 emit_alignment(tmpfp, sizeof (gpointer));
992 fprintf (tmpfp, "%s_p_%d:\n", mname, j);
993 fprintf (tmpfp, "\t.long %d\n", patch_info->data.method->wrapper_type);
994 fprintf (tmpfp, "\t.long %d\n", (image_index << 24) + (mono_metadata_token_index (token)));
998 case MONO_PATCH_INFO_FIELD:
999 patch_info->data.name = cond_emit_field_label (acfg, patch_info);
1002 case MONO_PATCH_INFO_CLASS:
1003 case MONO_PATCH_INFO_IID:
1004 patch_info->data.name = cond_emit_klass_label (acfg, patch_info->data.klass);
1007 case MONO_PATCH_INFO_IMAGE:
1008 patch_info->data.name = g_strdup_printf ("%d", get_image_index (acfg, patch_info->data.image));
1011 case MONO_PATCH_INFO_EXC_NAME: {
1012 MonoClass *ex_class;
1015 mono_class_from_name (mono_defaults.exception_class->image,
1016 "System", patch_info->data.target);
1017 g_assert (ex_class);
1018 patch_info->data.name = cond_emit_klass_label (acfg, ex_class);
1022 case MONO_PATCH_INFO_R4:
1023 emit_alignment(tmpfp, 8);
1024 fprintf (tmpfp, "%s_p_%d:\n", mname, j);
1025 fprintf (tmpfp, "\t.long 0x%08x\n", *((guint32 *)patch_info->data.target));
1028 case MONO_PATCH_INFO_R8:
1029 emit_alignment(tmpfp, 8);
1030 fprintf (tmpfp, "%s_p_%d:\n", mname, j);
1031 fprintf (tmpfp, "\t.long 0x%08x\n", *((guint32 *)patch_info->data.target));
1032 fprintf (tmpfp, "\t.long 0x%08x\n", *((guint32 *)patch_info->data.target + 1));
1035 case MONO_PATCH_INFO_METHOD_REL:
1036 emit_alignment(tmpfp, sizeof (gpointer));
1037 fprintf (tmpfp, "%s_p_%d:\n", mname, j);
1038 fprintf (tmpfp, "\t.long 0x%08x\n", patch_info->data.offset);
1041 case MONO_PATCH_INFO_VTABLE:
1042 case MONO_PATCH_INFO_CLASS_INIT:
1043 patch_info->data.name = cond_emit_klass_label (acfg, patch_info->data.klass);
1046 case MONO_PATCH_INFO_SFLDA:
1047 patch_info->data.name = cond_emit_field_label (acfg, patch_info);
1050 case MONO_PATCH_INFO_LDSTR:
1051 case MONO_PATCH_INFO_LDTOKEN:
1052 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1053 emit_alignment(tmpfp, 8);
1054 fprintf (tmpfp, "%s_p_%d:\n", mname, j);
1055 fprintf (tmpfp, "\t.long 0x%08x\n", get_image_index (acfg, patch_info->data.token->image));
1056 fprintf (tmpfp, "\t.long 0x%08x\n", patch_info->data.token->token);
1060 g_warning ("unable to handle jump info %d", patch_info->type);
1061 g_assert_not_reached ();
1065 emit_global (tmpfp, mname_p);
1066 emit_alignment (tmpfp, sizeof (gpointer));
1067 emit_label (tmpfp, mname_p);
1069 fprintf (tmpfp, "\t.long %d\n", cfg->code_len);
1070 fprintf (tmpfp, "\t.long %ld\n", (long)cfg->used_int_regs);
1072 /* Exception table */
1073 if (header->num_clauses) {
1074 MonoJitInfo *jinfo = cfg->jit_info;
1076 fprintf (tmpfp, "\t.long %d\n", jinfo->exvar_offset);
1078 for (k = 0; k < header->num_clauses; ++k) {
1079 MonoJitExceptionInfo *ei = &jinfo->clauses [k];
1081 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1082 fprintf (tmpfp, "\t.long %d\n", (guint8*)ei->data.filter - code);
1084 /* fixme: tokens are not global */
1085 fprintf (tmpfp, "\t.long %d\n", ei->data.token);
1087 fprintf (tmpfp, "\t.long %d\n", (guint8*)ei->try_start - code);
1088 fprintf (tmpfp, "\t.long %d\n", (guint8*)ei->try_end - code);
1089 fprintf (tmpfp, "\t.long %d\n", (guint8*)ei->handler_start - code);
1094 if (cfg->opt & MONO_OPT_SHARED) {
1095 fprintf (tmpfp, "\t.long %d\n", g_list_length (cfg->ldstr_list));
1096 for (l = cfg->ldstr_list; l; l = l->next) {
1097 fprintf (tmpfp, "\t.long 0x%08lx\n", (long)l->data);
1101 /* Used only in shared mode */
1102 g_assert (!cfg->ldstr_list);
1104 //printf ("M: %s (%s).\n", mono_method_full_name (method, TRUE), mname);
1107 guint32 last_offset;
1112 /* First emit the type+position table */
1113 for (pindex = 0; pindex < patches->len; ++pindex) {
1115 patch_info = g_ptr_array_index (patches, pindex);
1117 if ((patch_info->type == MONO_PATCH_INFO_LABEL) ||
1118 (patch_info->type == MONO_PATCH_INFO_BB))
1122 //printf ("T: %d O: %d.\n", patch_info->type, patch_info->ip.i);
1123 offset = patch_info->ip.i - last_offset;
1124 last_offset = patch_info->ip.i;
1126 /* Encode type+position compactly */
1127 g_assert (patch_info->type < 64);
1128 if (offset < 1024 - 1) {
1129 fprintf (tmpfp, "\t.byte %d\n", (patch_info->type << 2) + (offset >> 8));
1130 fprintf (tmpfp, "\t.byte %d\n", offset & ((1 << 8) - 1));
1133 fprintf (tmpfp, "\t.byte %d\n", (patch_info->type << 2) + 3);
1134 fprintf (tmpfp, "\t.byte %d\n", 255);
1135 emit_alignment(tmpfp, 4);
1136 fprintf (tmpfp, "\t.long %d\n", offset);
1141 * 0 is PATCH_INFO_BB, which can't be in the file.
1143 /* NULL terminated array */
1144 fprintf (tmpfp, "\t.byte 0\n");
1146 emit_alignment (tmpfp, sizeof (gpointer));
1148 /* Then emit the other info */
1149 for (pindex = 0; pindex < patches->len; ++pindex) {
1150 patch_info = g_ptr_array_index (patches, pindex);
1152 if ((patch_info->type == MONO_PATCH_INFO_LABEL) ||
1153 (patch_info->type == MONO_PATCH_INFO_BB))
1157 switch (patch_info->type) {
1158 case MONO_PATCH_INFO_METHODCONST:
1159 case MONO_PATCH_INFO_METHOD:
1160 case MONO_PATCH_INFO_METHOD_JUMP:
1161 case MONO_PATCH_INFO_CLASS:
1162 case MONO_PATCH_INFO_IID:
1163 case MONO_PATCH_INFO_FIELD:
1164 case MONO_PATCH_INFO_INTERNAL_METHOD:
1165 case MONO_PATCH_INFO_IMAGE:
1166 case MONO_PATCH_INFO_VTABLE:
1167 case MONO_PATCH_INFO_CLASS_INIT:
1168 case MONO_PATCH_INFO_SFLDA:
1169 case MONO_PATCH_INFO_EXC_NAME:
1170 emit_pointer (tmpfp, patch_info->data.name);
1173 case MONO_PATCH_INFO_SWITCH:
1174 case MONO_PATCH_INFO_R4:
1175 case MONO_PATCH_INFO_R8:
1176 case MONO_PATCH_INFO_METHOD_REL:
1177 case MONO_PATCH_INFO_LDSTR:
1178 case MONO_PATCH_INFO_LDTOKEN:
1179 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1180 case MONO_PATCH_INFO_WRAPPER: {
1182 sprintf (buf, "%s_p_%d", mname, j);
1183 emit_pointer (tmpfp, buf);
1187 case MONO_PATCH_INFO_LABEL:
1188 case MONO_PATCH_INFO_BB:
1191 g_warning ("unable to handle jump info %d", patch_info->type);
1192 g_assert_not_reached ();
1202 mono_debug_serialize_debug_info (cfg, &buf, &buf_len);
1204 fprintf (tmpfp, "\t.long %d\n", buf_len);
1206 for (i = 0; i < buf_len; ++i)
1207 fprintf (tmpfp, ".byte %d\n", (unsigned int) buf [i]);
1213 /* fixme: save the rest of the required infos */
1220 mono_compile_assembly (MonoAssembly *ass, guint32 opts)
1223 MonoImage *image = ass->image;
1225 char *com, *tmpfname, *opts_str;
1229 int ccount = 0, mcount = 0, lmfcount = 0, abscount = 0, wrappercount = 0, ocount = 0;
1230 GHashTable *ref_hash;
1231 MonoAotCompile *acfg;
1234 printf ("Mono Ahead of Time compiler - compiling assembly %s\n", image->name);
1236 i = g_file_open_tmp ("mono_aot_XXXXXX", &tmpfname, NULL);
1237 tmpfp = fdopen (i, "w+");
1240 ref_hash = g_hash_table_new (NULL, NULL);
1242 acfg = g_new0 (MonoAotCompile, 1);
1244 acfg->ref_hash = ref_hash;
1245 acfg->icall_hash = g_hash_table_new (NULL, NULL);
1246 acfg->icall_table = g_ptr_array_new ();
1247 acfg->image_hash = g_hash_table_new (NULL, NULL);
1248 acfg->image_table = g_ptr_array_new ();
1250 write_string_symbol (tmpfp, "mono_assembly_guid" , image->guid);
1252 write_string_symbol (tmpfp, "mono_aot_version", MONO_AOT_FILE_VERSION);
1254 opts_str = g_strdup_printf ("%d", opts);
1255 write_string_symbol (tmpfp, "mono_aot_opt_flags", opts_str);
1258 emitted = g_new0 (gboolean, image->tables [MONO_TABLE_METHOD].rows);
1260 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
1261 MonoJumpInfo *patch_info;
1263 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
1264 method = mono_get_method (image, token, NULL);
1266 /* fixme: maybe we can also precompile wrapper methods */
1267 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1268 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1269 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1270 (method->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
1271 //printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
1277 /* fixme: we need to patch the IP for the LMF in that case */
1278 if (method->save_lmf) {
1279 //printf ("Skip (needs lmf): %s\n", mono_method_full_name (method, TRUE));
1284 //printf ("START: %s\n", mono_method_full_name (method, TRUE));
1285 //mono_compile_method (method);
1287 cfg = mini_method_compile (method, opts, mono_get_root_domain (), FALSE, 0);
1290 if (cfg->disable_aot) {
1291 printf ("Skip (other): %s\n", mono_method_full_name (method, TRUE));
1297 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
1298 if (patch_info->type == MONO_PATCH_INFO_ABS) {
1299 /* unable to handle this */
1300 //printf ("Skip (abs addr): %s %d\n", mono_method_full_name (method, TRUE), patch_info->type);
1311 /* remoting-invoke-with-check wrappers are very common */
1312 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
1313 if ((patch_info->type == MONO_PATCH_INFO_METHOD) &&
1314 ((patch_info->data.method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)))
1315 patch_info->type = MONO_PATCH_INFO_WRAPPER;
1319 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
1320 if ((patch_info->type == MONO_PATCH_INFO_METHOD ||
1321 patch_info->type == MONO_PATCH_INFO_METHODCONST) &&
1322 patch_info->data.method->wrapper_type) {
1323 /* unable to handle this */
1324 //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));
1335 //printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
1338 emit_method (acfg, cfg);
1340 mono_destroy_compile (cfg);
1346 * The icall and image tables are small but referenced in a lot of places.
1347 * So we emit them at once, and reference their elements by an index
1348 * instead of an assembly label to cut back on the number of relocations.
1351 /* Emit icall table */
1353 symbol = g_strdup_printf ("mono_icall_table");
1354 emit_section_change (tmpfp, ".text", 1);
1355 emit_global(tmpfp, symbol);
1356 emit_alignment(tmpfp, 8);
1357 emit_label(tmpfp, symbol);
1358 fprintf (tmpfp, ".long %d\n", acfg->icall_table->len);
1359 for (i = 0; i < acfg->icall_table->len; i++)
1360 fprintf (tmpfp, "%s \"%s\"\n", AS_STRING_DIRECTIVE, (char*)g_ptr_array_index (acfg->icall_table, i));
1362 /* Emit image table */
1364 symbol = g_strdup_printf ("mono_image_table");
1365 emit_section_change (tmpfp, ".text", 1);
1366 emit_global(tmpfp, symbol);
1367 emit_alignment(tmpfp, 8);
1368 emit_label(tmpfp, symbol);
1369 fprintf (tmpfp, ".long %d\n", acfg->image_table->len);
1370 for (i = 0; i < acfg->image_table->len; i++)
1371 fprintf (tmpfp, "%s \"%s\"\n", AS_STRING_DIRECTIVE, ((MonoImage*)g_ptr_array_index (acfg->image_table, i))->guid);
1374 * g_module_symbol takes a lot of time for failed lookups, so we emit
1375 * a table which contains one bit for each method. This bit specifies
1376 * whenever the method is emitted or not.
1379 symbol = g_strdup_printf ("mono_methods_present_table");
1380 emit_section_change (tmpfp, ".text", 1);
1381 emit_global(tmpfp, symbol);
1382 emit_alignment(tmpfp, 8);
1383 emit_label(tmpfp, symbol);
1388 nrows = image->tables [MONO_TABLE_METHOD].rows;
1389 for (i = 0; i < nrows / 32 + 1; ++i) {
1391 for (k = 0; k < 32; ++k) {
1392 if (emitted [(i * 32) + k])
1395 //printf ("EMITTED [%d] = %d.\n", i, b);
1396 fprintf (tmpfp, "\t.long %d\n", w);
1402 #if defined(sparc) && SIZEOF_VOID_P == 8
1403 com = g_strdup_printf ("as -xarch=v9 %s -o %s.o", tmpfname, tmpfname);
1405 com = g_strdup_printf ("as %s -o %s.o", tmpfname, tmpfname);
1407 printf ("Executing the native assembler: %s\n", com);
1408 if (system (com) != 0) {
1415 com = g_strdup_printf ("ld -shared -G -o %s%s %s.o", image->name, SHARED_EXT, tmpfname);
1416 #elif defined(__ppc__) && defined(__MACH__)
1417 com = g_strdup_printf ("gcc -dynamiclib -o %s%s %s.o", image->name, SHARED_EXT, tmpfname);
1419 com = g_strdup_printf ("ld -shared -o %s%s %s.o", image->name, SHARED_EXT, tmpfname);
1421 printf ("Executing the native linker: %s\n", com);
1422 if (system (com) != 0) {
1428 com = g_strdup_printf ("%s.o", tmpfname);
1431 /*com = g_strdup_printf ("strip --strip-unneeded %s%s", image->name, SHARED_EXT);
1432 printf ("Stripping the binary: %s\n", com);
1436 printf ("Compiled %d out of %d methods (%d%%)\n", ccount, mcount, mcount ? (ccount*100)/mcount : 100);
1437 printf ("%d methods contain absolute addresses (%d%%)\n", abscount, mcount ? (abscount*100)/mcount : 100);
1438 printf ("%d methods contain wrapper references (%d%%)\n", wrappercount, mcount ? (wrappercount*100)/mcount : 100);
1439 printf ("%d methods contain lmf pointers (%d%%)\n", lmfcount, mcount ? (lmfcount*100)/mcount : 100);
1440 printf ("%d methods have other problems (%d%%)\n", ocount, mcount ? (ocount*100)/mcount : 100);
1441 //printf ("Retained input file.\n");