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 g_free (info->methods);
174 g_free (info->image_table);
177 g_module_close (assembly->aot_module);
178 assembly->aot_module = NULL;
181 table += strlen (table) + 1;
185 /* Read icall table */
187 guint32 table_len, i;
190 g_module_symbol (assembly->aot_module, "mono_icall_table", (gpointer *)&table);
193 table_len = *(guint32*)table;
194 table += sizeof (guint32);
195 info->icall_table = g_new0 (char*, table_len);
196 for (i = 0; i < table_len; ++i) {
197 info->icall_table [i] = table;
198 table += strlen (table) + 1;
202 /* Read methods present table */
203 g_module_symbol (assembly->aot_module, "mono_methods_present_table", (gpointer *)&info->methods_present_table);
204 g_assert (info->methods_present_table);
206 EnterCriticalSection (&aot_mutex);
207 mono_g_hash_table_insert (aot_modules, assembly, info);
208 LeaveCriticalSection (&aot_mutex);
210 if (mono_aot_verbose > 0)
211 printf ("Loaded AOT Module for %s.\n", assembly->image->name);
217 InitializeCriticalSection (&aot_mutex);
219 aot_modules = mono_g_hash_table_new (NULL, NULL);
221 mono_install_assembly_load_hook (load_aot_module, NULL);
223 if (getenv ("MONO_LASTAOT"))
224 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
228 mono_aot_get_method_inner (MonoDomain *domain, MonoMethod *method)
230 MonoClass *klass = method->klass;
231 MonoAssembly *ass = klass->image->assembly;
232 MonoJumpInfo *patch_info = NULL;
233 GModule *module = ass->aot_module;
234 char method_label [256];
235 char info_label [256];
238 guint code_len, used_int_regs, used_strings;
239 MonoAotModule *aot_module;
240 MonoAotMethod *minfo;
242 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
251 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
252 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
253 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
254 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
257 aot_module = (MonoAotModule*)mono_g_hash_table_lookup (aot_modules, ass);
259 g_assert (klass->inited);
261 minfo = mono_g_hash_table_lookup (aot_module->methods, method);
263 /* Duplicate jinfo */
264 jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
265 memcpy (jinfo, minfo->info, sizeof (MonoJitInfo));
266 if (jinfo->clauses) {
268 mono_mempool_alloc0 (domain->mp, sizeof (MonoJitExceptionInfo) * header->num_clauses);
269 memcpy (jinfo->clauses, minfo->info->clauses, sizeof (MonoJitExceptionInfo) * header->num_clauses);
272 /* This method was already loaded in another appdomain */
273 if (aot_module->opts & MONO_OPT_SHARED)
274 /* Use the same method in the new appdomain */
276 else if (!minfo->patch_info)
277 /* Use the same method in the new appdomain */
280 /* Create a copy of the original method and apply relocations */
282 code = mono_mempool_alloc (domain->code_mp, minfo->info->code_size);
283 memcpy (code, minfo->info->code_start, minfo->info->code_size);
285 if (mono_aot_verbose > 1)
286 printf ("REUSE METHOD: %s %p - %p.\n", mono_method_full_name (method, TRUE), code, (char*)code + code_len);
288 /* Do this outside the lock to avoid deadlocks */
289 LeaveCriticalSection (&aot_mutex);
290 mono_arch_patch_code (method, domain, code, minfo->patch_info);
291 EnterCriticalSection (&aot_mutex);
294 jinfo->code_start = code;
295 if (jinfo->clauses) {
296 for (i = 0; i < header->num_clauses; ++i) {
297 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
298 gint32 offset = code - (guint8*)minfo->info->code_start;
300 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
301 ei->data.filter = (guint8*)ei->data.filter + offset;
302 ei->try_start = (guint8*)ei->try_start + offset;
303 ei->try_end = (guint8*)ei->try_end + offset;
304 ei->handler_start = (guint8*)ei->handler_start + offset;
312 /* Do a fast check to see whenever the method exists */
314 guint32 index = mono_metadata_token_index (method->token) - 1;
316 w = aot_module->methods_present_table [index / 32];
317 if (! (w & (1 << (index % 32)))) {
318 if (mono_aot_verbose > 1)
319 printf ("NOT FOUND: %s.\n", mono_method_full_name (method, TRUE));
324 sprintf (method_label, "m_%x", mono_metadata_token_index (method->token));
326 if (!g_module_symbol (module, method_label, (gpointer *)&code))
329 sprintf (info_label, "%s_p", method_label);
331 if (!g_module_symbol (module, info_label, (gpointer *)&info))
334 if (mono_last_aot_method != -1) {
335 if (mono_jit_stats.methods_aot > mono_last_aot_method)
338 if (mono_jit_stats.methods_aot == mono_last_aot_method)
339 printf ("LAST AOT METHOD: %s.%s.%s.\n", klass->name_space, klass->name, method->name);
343 minfo = GC_MALLOC (sizeof (MonoAotMethod));
345 minfo = g_new0 (MonoAotMethod, 1);
348 jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
350 code_len = GPOINTER_TO_UINT (*((gpointer **)info));
352 used_int_regs = GPOINTER_TO_UINT (*((gpointer **)info));
355 if (!use_loaded_code) {
357 code2 = mono_mempool_alloc (domain->code_mp, code_len);
358 memcpy (code2, code, code_len);
362 if (mono_aot_verbose > 1)
363 printf ("FOUND AOT compiled code for %s %p - %p %p\n", mono_method_full_name (method, TRUE), code, code + code_len, info);
365 /* Exception table */
366 if (header->num_clauses) {
368 mono_mempool_alloc0 (domain->mp, sizeof (MonoJitExceptionInfo) * header->num_clauses);
369 jinfo->num_clauses = header->num_clauses;
371 jinfo->exvar_offset = GPOINTER_TO_UINT (*((gpointer**)info));
374 for (i = 0; i < header->num_clauses; ++i) {
375 MonoExceptionClause *ec = &header->clauses [i];
376 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
378 ei->flags = ec->flags;
379 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
380 ei->data.filter = code + GPOINTER_TO_UINT (*((gpointer**)info));
382 ei->data.token = GPOINTER_TO_UINT (*((gpointer**)info));
384 ei->try_start = code + GPOINTER_TO_UINT (*((gpointer**)info));
386 ei->try_end = code + GPOINTER_TO_UINT (*((gpointer**)info));
388 ei->handler_start = code + GPOINTER_TO_UINT (*((gpointer**)info));
393 if (aot_module->opts & MONO_OPT_SHARED) {
394 used_strings = GPOINTER_TO_UINT (*((gpointer **)info));
400 for (i = 0; i < used_strings; i++) {
401 guint token = GPOINTER_TO_UINT (*((gpointer **)info));
403 mono_ldstr (mono_root_domain, klass->image, mono_metadata_token_index (token));
415 if (aot_module->opts & MONO_OPT_SHARED)
416 mp = mono_mempool_new ();
422 MonoJumpInfo *ji = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
428 b2 = *((guint8*)info + 1);
430 info = (gpointer*)((guint8*)info + 2);
434 if (((b1 & (1 + 2)) == 3) && (b2 == 255)) {
435 ji->ip.i = GPOINTER_TO_UINT (*info);
439 ji->ip.i = (((guint32)(b1 & (1 + 2))) << 8) + b2;
441 ji->ip.i += last_offset;
442 last_offset = ji->ip.i;
443 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
445 data = *((gpointer **)info);
448 case MONO_PATCH_INFO_CLASS:
449 case MONO_PATCH_INFO_IID:
450 ji->data.klass = decode_class_info (aot_module, data);
451 g_assert (ji->data.klass);
452 mono_class_init (ji->data.klass);
454 case MONO_PATCH_INFO_VTABLE:
455 case MONO_PATCH_INFO_CLASS_INIT:
456 ji->data.klass = decode_class_info (aot_module, data);
457 g_assert (ji->data.klass);
458 mono_class_init (ji->data.klass);
460 case MONO_PATCH_INFO_IMAGE:
461 ji->data.image = aot_module->image_table [(guint32)data];
462 g_assert (ji->data.image);
464 case MONO_PATCH_INFO_METHOD:
465 case MONO_PATCH_INFO_METHODCONST:
466 case MONO_PATCH_INFO_METHOD_JUMP: {
467 guint32 image_index, token;
469 image_index = (guint32)data >> 24;
470 token = MONO_TOKEN_METHOD_DEF | ((guint32)data & 0xffffff);
472 image = aot_module->image_table [image_index];
473 ji->data.method = mono_get_method (image, token, NULL);
474 g_assert (ji->data.method);
475 mono_class_init (ji->data.method->klass);
479 case MONO_PATCH_INFO_WRAPPER: {
480 guint32 image_index, token;
481 guint32 wrapper_type;
483 wrapper_type = (guint32)data[0];
484 image_index = (guint32)data[1] >> 24;
485 token = MONO_TOKEN_METHOD_DEF | ((guint32)data[1] & 0xffffff);
487 image = aot_module->image_table [image_index];
488 ji->data.method = mono_get_method (image, token, NULL);
489 g_assert (ji->data.method);
490 mono_class_init (ji->data.method->klass);
492 g_assert (wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK);
493 ji->type = MONO_PATCH_INFO_METHOD;
494 ji->data.method = mono_marshal_get_remoting_invoke_with_check (ji->data.method);
497 case MONO_PATCH_INFO_FIELD:
498 case MONO_PATCH_INFO_SFLDA: {
499 MonoClass *klass = decode_class_info (aot_module, data [1]);
500 mono_class_init (klass);
501 ji->data.field = mono_class_get_field (klass, (guint32)data [0]);
504 case MONO_PATCH_INFO_INTERNAL_METHOD:
505 ji->data.name = aot_module->icall_table [(guint32)data];
506 g_assert (ji->data.name);
507 //printf ("A: %s.\n", ji->data.name);
509 case MONO_PATCH_INFO_SWITCH:
510 ji->table_size = (int)data [0];
511 table = g_new (gpointer, ji->table_size);
512 ji->data.target = table;
513 for (i = 0; i < ji->table_size; i++) {
514 table [i] = data [i + 1];
517 case MONO_PATCH_INFO_R4:
518 case MONO_PATCH_INFO_R8:
519 ji->data.target = data;
521 case MONO_PATCH_INFO_LDSTR:
522 case MONO_PATCH_INFO_LDTOKEN:
523 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
524 ji->data.target = *data;
526 case MONO_PATCH_INFO_EXC_NAME:
527 ji->data.klass = decode_class_info (aot_module, data);
528 g_assert (ji->data.klass);
529 mono_class_init (ji->data.klass);
530 ji->data.name = ji->data.klass->name;
532 case MONO_PATCH_INFO_METHOD_REL:
533 ji->data.offset = (int)data [0];
536 g_warning ("unhandled type %d", ji->type);
537 g_assert_not_reached ();
541 ji->next = patch_info;
545 if (use_loaded_code) {
546 /* disable write protection */
547 #ifndef PLATFORM_WIN32
548 page_start = (char *) (((int) (code)) & ~ (PAGESIZE - 1));
549 pages = (code + code_len - page_start + PAGESIZE - 1) / PAGESIZE;
550 err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
555 g_assert (VirtualProtect (code, code_len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
560 /* Do this outside the lock to avoid deadlocks */
561 LeaveCriticalSection (&aot_mutex);
562 mono_arch_patch_code (method, domain, code, patch_info);
563 EnterCriticalSection (&aot_mutex);
565 if (aot_module->opts & MONO_OPT_SHARED)
566 /* No need to cache patches */
567 mono_mempool_destroy (mp);
569 minfo->patch_info = patch_info;
572 mono_jit_stats.methods_aot++;
575 jinfo->code_size = code_len;
576 jinfo->used_regs = used_int_regs;
577 jinfo->method = method;
578 jinfo->code_start = code;
579 jinfo->domain_neutral = (aot_module->opts & MONO_OPT_SHARED) != 0;
582 mono_g_hash_table_insert (aot_module->methods, method, minfo);
589 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
593 EnterCriticalSection (&aot_mutex);
594 info = mono_aot_get_method_inner (domain, method);
595 LeaveCriticalSection (&aot_mutex);
597 /* Do this outside the lock */
599 mono_jit_info_table_add (domain, info);
608 write_data_symbol (FILE *fp, const char *name, guint8 *buf, int size, int align)
612 fprintf (fp, ".globl %s\n", name);
613 fprintf (fp, ".text 1 \n\t.align %d\n", align);
614 fprintf (fp, "\t.type %s,@object\n", name);
615 fprintf (fp, "\t.size %s,%d\n", name, size);
616 fprintf (fp, "%s:\n", name);
617 for (i = 0; i < size; i++) {
618 fprintf (fp, ".byte %d\n", buf [i]);
625 write_string_symbol (FILE *fp, const char *name, const char *value)
627 fprintf (fp, ".globl %s\n", name);
628 fprintf (fp, ".text 1\n");
629 fprintf (fp, "%s:\n", name);
630 fprintf (fp, "\t.string \"%s\"\n", value);
634 mono_get_field_token (MonoClassField *field)
636 MonoClass *klass = field->parent;
639 for (i = 0; i < klass->field.count; ++i) {
640 if (field == &klass->fields [i])
641 return MONO_TOKEN_FIELD_DEF | (klass->field.first + 1 + i);
644 g_assert_not_reached ();
649 get_image_index (MonoAotCompile *cfg, MonoImage *image)
653 index = GPOINTER_TO_UINT (g_hash_table_lookup (cfg->image_hash, image));
657 index = g_hash_table_size (cfg->image_hash);
658 g_hash_table_insert (cfg->image_hash, image, GUINT_TO_POINTER (index + 1));
659 g_ptr_array_add (cfg->image_table, image);
665 emit_image_index (MonoAotCompile *cfg, MonoImage *image)
669 image_index = get_image_index (cfg, image);
671 fprintf (cfg->fp, "\t.long %d\n", image_index);
675 cond_emit_klass_label (MonoAotCompile *cfg, MonoClass *klass)
677 char *l1, *el = NULL;
679 if ((l1 = g_hash_table_lookup (cfg->ref_hash, klass)))
682 if (!klass->type_token) {
683 g_assert (klass->rank > 0);
684 el = cond_emit_klass_label (cfg, klass->element_class);
687 fprintf (cfg->fp, "\t.align %d\n", sizeof (gpointer));
688 l1 = g_strdup_printf ("klass_p_%08x_%p", klass->type_token, klass);
689 fprintf (cfg->fp, "%s:\n", l1);
690 fprintf (cfg->fp, "\t.long 0x%08x\n", klass->type_token);
691 emit_image_index (cfg, klass->image);
694 fprintf (cfg->fp, "\t.long %d\n", klass->rank);
695 fprintf (cfg->fp, "\t.long %s\n", el);
698 g_hash_table_insert (cfg->ref_hash, klass, l1);
704 cond_emit_field_label (MonoAotCompile *cfg, MonoJumpInfo *patch_info)
706 MonoClassField *field = patch_info->data.field;
710 if ((l1 = g_hash_table_lookup (cfg->ref_hash, field)))
713 l2 = cond_emit_klass_label (cfg, field->parent);
714 fprintf (cfg->fp, "\t.align %d\n", sizeof (gpointer));
715 token = mono_get_field_token (field);
717 l1 = g_strdup_printf ("klass_p_%08x_%p", token, field);
718 fprintf (cfg->fp, "%s:\n", l1);
719 fprintf (cfg->fp, "\t.long 0x%08x\n", token);
720 fprintf (cfg->fp, "\t.long %s\n", l2);
722 g_hash_table_insert (cfg->ref_hash, field, l1);
728 compare_patches (gconstpointer a, gconstpointer b)
732 i = (*(MonoJumpInfo**)a)->ip.i;
733 j = (*(MonoJumpInfo**)b)->ip.i;
745 emit_method (MonoAotCompile *acfg, MonoCompile *cfg)
751 guint8 *code, *mname;
752 int func_alignment = 16;
754 MonoJumpInfo *patch_info;
755 MonoMethodHeader *header;
758 method = cfg->method;
759 code = cfg->native_code;
760 header = ((MonoMethodNormal*)method)->header;
762 fprintf (tmpfp, ".text 0\n");
763 mname = g_strdup_printf ("m_%x", mono_metadata_token_index (method->token));
764 fprintf (tmpfp, "\t.align %d\n", func_alignment);
765 fprintf (tmpfp, ".globl %s\n", mname);
766 fprintf (tmpfp, "\t.type %s,@function\n", mname);
767 fprintf (tmpfp, "%s:\n", mname);
769 for (i = 0; i < cfg->code_len; i++)
770 fprintf (tmpfp, ".byte %d\n", (unsigned int) code [i]);
772 fprintf (tmpfp, ".text 1\n");
774 /* Sort relocations */
775 patches = g_ptr_array_new ();
776 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next)
777 g_ptr_array_add (patches, patch_info);
778 g_ptr_array_sort (patches, compare_patches);
781 for (pindex = 0; pindex < patches->len; ++pindex) {
782 patch_info = g_ptr_array_index (patches, pindex);
783 switch (patch_info->type) {
784 case MONO_PATCH_INFO_LABEL:
785 case MONO_PATCH_INFO_BB:
786 /* relative jumps are no problem, there is no need to handle then here */
788 case MONO_PATCH_INFO_SWITCH: {
789 gpointer *table = (gpointer *)patch_info->data.target;
792 fprintf (tmpfp, "\t.align %d\n", sizeof (gpointer));
793 fprintf (tmpfp, "%s_p_%d:\n", mname, j);
794 fprintf (tmpfp, "\t.long %d\n", patch_info->table_size);
796 for (k = 0; k < patch_info->table_size; k++) {
797 fprintf (tmpfp, "\t.long %d\n", (int)table [k]);
802 case MONO_PATCH_INFO_INTERNAL_METHOD: {
805 icall_index = (guint32)g_hash_table_lookup (acfg->icall_hash, patch_info->data.name);
807 icall_index = g_hash_table_size (acfg->icall_hash) + 1;
808 g_hash_table_insert (acfg->icall_hash, (gpointer)patch_info->data.name,
809 GUINT_TO_POINTER (icall_index));
810 g_ptr_array_add (acfg->icall_table, (gpointer)patch_info->data.name);
812 patch_info->data.name = g_strdup_printf ("%d", icall_index - 1);
816 case MONO_PATCH_INFO_METHODCONST:
817 case MONO_PATCH_INFO_METHOD:
818 case MONO_PATCH_INFO_METHOD_JUMP: {
820 * The majority of patches are for methods, so we emit
821 * them inline instead of defining a label for them to
822 * decrease the number of relocations.
824 guint32 image_index = get_image_index (acfg, patch_info->data.method->klass->image);
825 guint32 token = patch_info->data.method->token;
826 g_assert (image_index < 256);
827 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
829 patch_info->data.name =
830 g_strdup_printf ("%d", (image_index << 24) + (mono_metadata_token_index (token)));
834 case MONO_PATCH_INFO_WRAPPER: {
839 m = mono_marshal_method_from_wrapper (patch_info->data.method);
840 image_index = get_image_index (acfg, m->klass->image);
842 g_assert (image_index < 256);
843 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
845 fprintf (tmpfp, "\t.align %d\n", sizeof (gpointer));
846 fprintf (tmpfp, "%s_p_%d:\n", mname, j);
847 fprintf (tmpfp, "\t.long %d\n", patch_info->data.method->wrapper_type);
848 fprintf (tmpfp, "\t.long %d\n", (image_index << 24) + (mono_metadata_token_index (token)));
852 case MONO_PATCH_INFO_FIELD:
853 patch_info->data.name = cond_emit_field_label (acfg, patch_info);
856 case MONO_PATCH_INFO_CLASS:
857 case MONO_PATCH_INFO_IID:
858 patch_info->data.name = cond_emit_klass_label (acfg, patch_info->data.klass);
861 case MONO_PATCH_INFO_IMAGE:
862 patch_info->data.name = g_strdup_printf ("%d", get_image_index (acfg, patch_info->data.image));
865 case MONO_PATCH_INFO_EXC_NAME: {
869 mono_class_from_name (mono_defaults.exception_class->image,
870 "System", patch_info->data.target);
872 patch_info->data.name = cond_emit_klass_label (acfg, ex_class);
876 case MONO_PATCH_INFO_R4:
877 fprintf (tmpfp, "\t.align 8\n");
878 fprintf (tmpfp, "%s_p_%d:\n", mname, j);
879 fprintf (tmpfp, "\t.long 0x%08x\n", *((guint32 *)patch_info->data.target));
882 case MONO_PATCH_INFO_R8:
883 fprintf (tmpfp, "\t.align 8\n");
884 fprintf (tmpfp, "%s_p_%d:\n", mname, j);
885 fprintf (tmpfp, "\t.long 0x%08x\n", *((guint32 *)patch_info->data.target));
886 fprintf (tmpfp, "\t.long 0x%08x\n", *((guint32 *)patch_info->data.target + 1));
889 case MONO_PATCH_INFO_METHOD_REL:
890 fprintf (tmpfp, "\t.align %d\n", sizeof (gpointer));
891 fprintf (tmpfp, "%s_p_%d:\n", mname, j);
892 fprintf (tmpfp, "\t.long 0x%08x\n", patch_info->data.offset);
895 case MONO_PATCH_INFO_VTABLE:
896 case MONO_PATCH_INFO_CLASS_INIT:
897 patch_info->data.name = cond_emit_klass_label (acfg, patch_info->data.klass);
900 case MONO_PATCH_INFO_SFLDA:
901 patch_info->data.name = cond_emit_field_label (acfg, patch_info);
904 case MONO_PATCH_INFO_LDSTR:
905 case MONO_PATCH_INFO_LDTOKEN:
906 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
907 fprintf (tmpfp, "\t.align 8\n");
908 fprintf (tmpfp, "%s_p_%d:\n", mname, j);
909 fprintf (tmpfp, "\t.long 0x%08x\n", patch_info->data.token);
913 g_warning ("unable to handle jump info %d", patch_info->type);
914 g_assert_not_reached ();
918 fprintf (tmpfp, ".globl %s_p\n", mname);
919 fprintf (tmpfp, "\t.align %d\n", sizeof (gpointer));
920 fprintf (tmpfp, "%s_p:\n", mname);
922 fprintf (tmpfp, "\t.long %d\n", cfg->code_len);
923 fprintf (tmpfp, "\t.long %d\n", cfg->used_int_regs);
925 /* Exception table */
926 if (header->num_clauses) {
927 MonoJitInfo *jinfo = cfg->jit_info;
929 fprintf (tmpfp, "\t.long %d\n", jinfo->exvar_offset);
931 for (k = 0; k < header->num_clauses; ++k) {
932 MonoJitExceptionInfo *ei = &jinfo->clauses [k];
934 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
935 fprintf (tmpfp, "\t.long %d\n", (guint8*)ei->data.filter - code);
937 /* fixme: tokens are not global */
938 fprintf (tmpfp, "\t.long %d\n", ei->data.token);
940 fprintf (tmpfp, "\t.long %d\n", (guint8*)ei->try_start - code);
941 fprintf (tmpfp, "\t.long %d\n", (guint8*)ei->try_end - code);
942 fprintf (tmpfp, "\t.long %d\n", (guint8*)ei->handler_start - code);
947 if (cfg->opt & MONO_OPT_SHARED) {
948 fprintf (tmpfp, "\t.long %d\n", g_list_length (cfg->ldstr_list));
949 for (l = cfg->ldstr_list; l; l = l->next) {
950 fprintf (tmpfp, "\t.long 0x%08lx\n", (long)l->data);
954 /* Used only in shared mode */
955 g_assert (!cfg->ldstr_list);
957 //printf ("M: %s (%s).\n", mono_method_full_name (method, TRUE), mname);
964 for (pindex = 0; pindex < patches->len; ++pindex) {
966 patch_info = g_ptr_array_index (patches, pindex);
968 if ((patch_info->type == MONO_PATCH_INFO_LABEL) ||
969 (patch_info->type == MONO_PATCH_INFO_BB))
973 //printf ("T: %d O: %d.\n", patch_info->type, patch_info->ip.i);
974 offset = patch_info->ip.i - last_offset;
975 last_offset = patch_info->ip.i;
977 /* Encode type+position compactly */
978 g_assert (patch_info->type < 64);
979 if (offset < 1024 - 1) {
980 fprintf (tmpfp, "\t.byte %d\n", (patch_info->type << 2) + (offset >> 8));
981 fprintf (tmpfp, "\t.byte %d\n", offset & ((1 << 8) - 1));
984 fprintf (tmpfp, "\t.byte %d\n", (patch_info->type << 2) + 3);
985 fprintf (tmpfp, "\t.byte %d\n", 255);
986 fprintf (tmpfp, "\t.long %d\n", offset);
989 switch (patch_info->type) {
990 case MONO_PATCH_INFO_METHODCONST:
991 case MONO_PATCH_INFO_METHOD:
992 case MONO_PATCH_INFO_METHOD_JUMP:
993 case MONO_PATCH_INFO_CLASS:
994 case MONO_PATCH_INFO_IID:
995 case MONO_PATCH_INFO_FIELD:
996 case MONO_PATCH_INFO_INTERNAL_METHOD:
997 case MONO_PATCH_INFO_IMAGE:
998 case MONO_PATCH_INFO_VTABLE:
999 case MONO_PATCH_INFO_CLASS_INIT:
1000 case MONO_PATCH_INFO_SFLDA:
1001 case MONO_PATCH_INFO_EXC_NAME:
1002 fprintf (tmpfp, "\t.long %s\n", patch_info->data.name);
1005 case MONO_PATCH_INFO_SWITCH:
1006 case MONO_PATCH_INFO_R4:
1007 case MONO_PATCH_INFO_R8:
1008 case MONO_PATCH_INFO_METHOD_REL:
1009 case MONO_PATCH_INFO_LDSTR:
1010 case MONO_PATCH_INFO_LDTOKEN:
1011 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1012 case MONO_PATCH_INFO_WRAPPER:
1013 fprintf (tmpfp, "\t.long %s_p_%d\n", mname, j);
1016 case MONO_PATCH_INFO_LABEL:
1017 case MONO_PATCH_INFO_BB:
1020 g_warning ("unable to handle jump info %d", patch_info->type);
1021 g_assert_not_reached ();
1028 * 0 is PATCH_INFO_BB, which can't be in the file.
1030 /* NULL terminated array */
1031 fprintf (tmpfp, "\t.long 0\n");
1033 /* fixme: save the rest of the required infos */
1039 mono_compile_assembly (MonoAssembly *ass, guint32 opts)
1042 MonoImage *image = ass->image;
1044 char *com, *tmpfname, *opts_str;
1048 int ccount = 0, mcount = 0, lmfcount = 0, abscount = 0, wrappercount = 0, ocount = 0;
1049 GHashTable *ref_hash;
1050 MonoAotCompile *acfg;
1053 printf ("Mono AOT compiler - compiling assembly %s\n", image->name);
1055 i = g_file_open_tmp ("mono_aot_XXXXXX", &tmpfname, NULL);
1056 tmpfp = fdopen (i, "w+");
1059 ref_hash = g_hash_table_new (NULL, NULL);
1061 acfg = g_new0 (MonoAotCompile, 1);
1063 acfg->ref_hash = ref_hash;
1064 acfg->icall_hash = g_hash_table_new (NULL, NULL);
1065 acfg->icall_table = g_ptr_array_new ();
1066 acfg->image_hash = g_hash_table_new (NULL, NULL);
1067 acfg->image_table = g_ptr_array_new ();
1069 write_string_symbol (tmpfp, "mono_assembly_guid" , image->guid);
1071 write_string_symbol (tmpfp, "mono_aot_version", MONO_AOT_FILE_VERSION);
1073 opts_str = g_strdup_printf ("%d", opts);
1074 write_string_symbol (tmpfp, "mono_aot_opt_flags", opts_str);
1077 emitted = g_new0 (gboolean, image->tables [MONO_TABLE_METHOD].rows);
1079 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
1080 MonoJumpInfo *patch_info;
1082 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
1083 method = mono_get_method (image, token, NULL);
1085 /* fixme: maybe we can also precompile wrapper methods */
1086 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1087 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1088 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1089 (method->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
1090 //printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
1096 /* fixme: we need to patch the IP for the LMF in that case */
1097 if (method->save_lmf) {
1098 //printf ("Skip (needs lmf): %s\n", mono_method_full_name (method, TRUE));
1103 //printf ("START: %s\n", mono_method_full_name (method, TRUE));
1104 //mono_compile_method (method);
1106 cfg = mini_method_compile (method, opts, mono_root_domain, 0);
1109 if (cfg->disable_aot) {
1110 printf ("Skip (other): %s\n", mono_method_full_name (method, TRUE));
1116 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
1117 if (patch_info->type == MONO_PATCH_INFO_ABS) {
1118 /* unable to handle this */
1119 //printf ("Skip (abs addr): %s %d\n", mono_method_full_name (method, TRUE), patch_info->type);
1130 /* remoting-invoke-with-check wrappers are very common */
1131 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
1132 if ((patch_info->type == MONO_PATCH_INFO_METHOD) &&
1133 ((patch_info->data.method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)))
1134 patch_info->type = MONO_PATCH_INFO_WRAPPER;
1138 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
1139 if ((patch_info->type == MONO_PATCH_INFO_METHOD ||
1140 patch_info->type == MONO_PATCH_INFO_METHODCONST) &&
1141 patch_info->data.method->wrapper_type) {
1142 /* unable to handle this */
1143 //printf ("Skip (wrapper call): %s %d -> %s\n", mono_method_full_name (method, TRUE), patch_info->type, mono_method_full_name (patch_info->data.method, TRUE));
1154 //printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
1157 emit_method (acfg, cfg);
1159 mono_destroy_compile (cfg);
1165 * The icall and image tables are small but referenced in a lot of places.
1166 * So we emit them at once, and reference their elements by an index
1167 * instead of an assembly label to cut back on the number of relocations.
1170 /* Emit icall table */
1172 symbol = g_strdup_printf ("mono_icall_table");
1173 fprintf (tmpfp, ".globl %s\n", symbol);
1174 fprintf (tmpfp, ".text 1 \n");
1175 fprintf (tmpfp, "\t.align 8\n");
1176 fprintf (tmpfp, "%s:\n", symbol);
1177 fprintf (tmpfp, ".long %d\n", acfg->icall_table->len);
1178 for (i = 0; i < acfg->icall_table->len; i++)
1179 fprintf (tmpfp, ".string \"%s\"\n", (char*)g_ptr_array_index (acfg->icall_table, i));
1181 /* Emit image table */
1183 symbol = g_strdup_printf ("mono_image_table");
1184 fprintf (tmpfp, ".globl %s\n", symbol);
1185 fprintf (tmpfp, ".text 1 \n");
1186 fprintf (tmpfp, "\t.align 8\n");
1187 fprintf (tmpfp, "%s:\n", symbol);
1188 fprintf (tmpfp, ".long %d\n", acfg->image_table->len);
1189 for (i = 0; i < acfg->image_table->len; i++)
1190 fprintf (tmpfp, ".string \"%s\"\n", ((MonoImage*)g_ptr_array_index (acfg->image_table, i))->guid);
1193 * g_module_symbol takes a lot of time for failed lookups, so we emit
1194 * a table which contains one bit for each method. This bit specifies
1195 * whenever the method is emitted or not.
1198 symbol = g_strdup_printf ("mono_methods_present_table");
1199 fprintf (tmpfp, ".globl %s\n", symbol);
1200 fprintf (tmpfp, ".text 1 \n");
1201 fprintf (tmpfp, "\t.align 8\n");
1202 fprintf (tmpfp, "%s:\n", symbol);
1207 nrows = image->tables [MONO_TABLE_METHOD].rows;
1208 for (i = 0; i < nrows / 32 + 1; ++i) {
1210 for (k = 0; k < 32; ++k) {
1211 if (emitted [(i * 32) + k])
1214 //printf ("EMITTED [%d] = %d.\n", i, b);
1215 fprintf (tmpfp, "\t.long %d\n", w);
1221 com = g_strdup_printf ("as %s -o %s.o", tmpfname, tmpfname);
1222 printf ("Executing the native assembler: %s\n", com);
1225 com = g_strdup_printf ("ld -shared -o %s%s %s.o", image->name, SHARED_EXT, tmpfname);
1226 printf ("Executing the native linker: %s\n", com);
1229 com = g_strdup_printf ("%s.o", tmpfname);
1232 /*com = g_strdup_printf ("strip --strip-unneeded %s%s", image->name, SHARED_EXT);
1233 printf ("Stripping the binary: %s\n", com);
1237 printf ("Compiled %d out of %d methods (%d%%)\n", ccount, mcount, (ccount*100)/mcount);
1238 printf ("%d methods contain absolute addresses (%d%%)\n", abscount, (abscount*100)/mcount);
1239 printf ("%d methods contain wrapper references (%d%%)\n", wrappercount, (wrappercount*100)/mcount);
1240 printf ("%d methods contain lmf pointers (%d%%)\n", lmfcount, (lmfcount*100)/mcount);
1241 printf ("%d methods have other problems (%d%%)\n", ocount, (ocount*100)/mcount);