2009-07-13 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini-trampolines.c
1
2 #include <config.h>
3 #include <glib.h>
4
5 #include <mono/metadata/appdomain.h>
6 #include <mono/metadata/metadata-internals.h>
7 #include <mono/metadata/marshal.h>
8 #include <mono/metadata/tabledefs.h>
9 #include <mono/utils/mono-counters.h>
10
11 #ifdef HAVE_VALGRIND_MEMCHECK_H
12 #include <valgrind/memcheck.h>
13 #endif
14
15 #include "mini.h"
16 #include "debug-mini.h"
17
18 /*
19  * Address of the trampoline code.  This is used by the debugger to check
20  * whether a method is a trampoline.
21  */
22 guint8* mono_trampoline_code [MONO_TRAMPOLINE_NUM];
23
24 static GHashTable *class_init_hash_addr = NULL;
25 static GHashTable *rgctx_lazy_fetch_trampoline_hash = NULL;
26 static GHashTable *rgctx_lazy_fetch_trampoline_hash_addr = NULL;
27
28 #define mono_trampolines_lock() EnterCriticalSection (&trampolines_mutex)
29 #define mono_trampolines_unlock() LeaveCriticalSection (&trampolines_mutex)
30 static CRITICAL_SECTION trampolines_mutex;
31
32 static gpointer
33 get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
34 {
35         if (mono_aot_only)
36                 return mono_aot_get_unbox_trampoline (m);
37         else
38                 return mono_arch_get_unbox_trampoline (gsctx, m, addr);
39 }
40
41 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
42 /*
43  * mono_create_static_rgctx_trampoline:
44  *
45  *   Return a static rgctx trampoline for M which branches to ADDR which should
46  * point to the compiled code of M.
47  *
48  *   Static rgctx trampolines are used when a shared generic method which doesn't
49  * have a this argument is called indirectly, ie. from code which can't pass in
50  * the rgctx argument. The trampoline sets the rgctx argument and jumps to the
51  * methods code. These trampolines are similar to the unbox trampolines, they
52  * perform the same task as the static rgctx wrappers, but they are smaller/faster,
53  * and can be made to work with full AOT.
54  */
55 gpointer
56 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
57 {
58         gpointer ctx;
59         gpointer res;
60         MonoDomain *domain;
61
62         if (mini_method_get_context (m)->method_inst)
63                 ctx = mono_method_lookup_rgctx (mono_class_vtable (mono_domain_get (), m->klass), mini_method_get_context (m)->method_inst);
64         else
65                 ctx = mono_class_vtable (mono_domain_get (), m->klass);
66
67         if (mono_aot_only)
68                 return mono_aot_get_static_rgctx_trampoline (ctx, addr);
69
70         domain = mono_domain_get ();
71
72         mono_domain_lock (domain);
73         res = g_hash_table_lookup (domain_jit_info (domain)->static_rgctx_trampoline_hash,
74                                                            m);
75         mono_domain_unlock (domain);
76         if (res)
77                 return res;
78
79         res = mono_arch_get_static_rgctx_trampoline (m, ctx, addr);
80
81         mono_domain_lock (domain);
82         /* Duplicates inserted while we didn't hold the lock are OK */
83         g_hash_table_insert (domain_jit_info (domain)->static_rgctx_trampoline_hash, m, res);
84         mono_domain_unlock (domain);
85
86         return res;
87 }
88 #else
89 gpointer
90 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
91 {
92         /* 
93          * This shouldn't happen as all arches which support generic sharing support
94          * static rgctx trampolines as well.
95          */
96         g_assert_not_reached ();
97 }
98 #endif
99
100 gpointer*
101 mono_get_vcall_slot_addr (guint8* code, gpointer *regs)
102 {
103         gpointer vt;
104         int displacement;
105         vt = mono_arch_get_vcall_slot (code, regs, &displacement);
106         if (!vt)
107                 return NULL;
108         return (gpointer*)((char*)vt + displacement);
109 }
110
111 #ifdef MONO_ARCH_HAVE_IMT
112
113 static gpointer*
114 mono_convert_imt_slot_to_vtable_slot (gpointer* slot, gpointer *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method, gboolean *need_rgctx_tramp)
115 {
116         MonoGenericSharingContext *gsctx = mono_get_generic_context_from_code (code);
117         MonoObject *this_argument = mono_arch_find_this_argument (regs, method, gsctx);
118         MonoVTable *vt = this_argument->vtable;
119         int displacement = slot - ((gpointer*)vt);
120
121         if (displacement > 0) {
122                 /* slot is in the vtable, not in the IMT */
123 #if DEBUG_IMT
124                 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
125 #endif
126                 return slot;
127         } else {
128                 MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
129                 int interface_offset;
130                 int imt_slot = MONO_IMT_SIZE + displacement;
131
132                 interface_offset = mono_class_interface_offset (vt->klass, imt_method->klass);
133
134                 if (interface_offset < 0) {
135                         g_print ("%s doesn't implement interface %s\n", mono_type_get_name_full (&vt->klass->byval_arg, 0), mono_type_get_name_full (&imt_method->klass->byval_arg, 0));
136                         g_assert_not_reached ();
137                 }
138                 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
139
140                 if (impl_method) {
141                         MonoMethod *impl;
142
143                         if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
144                                 MonoGenericContext context = { NULL, NULL };
145
146                                 /* 
147                                  * Generic virtual method, imt_method contains the inflated interface 
148                                  * method, need to get the inflated impl method.
149                                  */
150                                 /* imt_method->slot might not be set */
151                                 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_declaring_generic_method (imt_method)->slot);
152
153                                 if (impl->klass->generic_class)
154                                         context.class_inst = impl->klass->generic_class->context.class_inst;
155                                 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
156                                 impl = mono_class_inflate_generic_method (impl, &context);
157                         } else {
158                                 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + imt_method->slot);
159                         }
160
161                         if (mono_method_needs_static_rgctx_invoke (impl, FALSE))
162                                 *need_rgctx_tramp = TRUE;
163
164                         *impl_method = impl;
165 #if DEBUG_IMT
166                 printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
167                                 method->klass->name_space, method->klass->name, method->name, 
168                                 imt_method->klass->name_space, imt_method->klass->name, imt_method->name);
169 #endif
170                 }
171                 g_assert (imt_slot < MONO_IMT_SIZE);
172                 if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
173                         int vtable_offset = interface_offset + mono_method_get_vtable_index (imt_method);
174                         gpointer *vtable_slot = & (vt->vtable [vtable_offset]);
175 #if DEBUG_IMT
176                         printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, and colliding becomes %p[%d] (interface_offset = %d, method->slot = %d)\n", slot, imt_slot, vtable_slot, vtable_offset, interface_offset, imt_method->slot);
177 #endif
178                         return vtable_slot;
179                 } else {
180 #if DEBUG_IMT
181                         printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
182 #endif
183                         return slot;
184                 }
185         }
186 }
187 #endif
188
189 /**
190  * mono_magic_trampoline:
191  *
192  *   This trampoline handles calls from JITted code.
193  */
194 gpointer
195 mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
196 {
197         gpointer addr;
198         gpointer *vtable_slot;
199         gboolean generic_shared = FALSE;
200         MonoMethod *declaring = NULL;
201         MonoMethod *generic_virtual = NULL;
202         int context_used;
203         gboolean proxy = FALSE;
204         gboolean need_rgctx_tramp = FALSE;
205
206         if (m == MONO_FAKE_VTABLE_METHOD) {
207                 int displacement;
208                 MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
209                 if (!vt) {
210                         int i;
211                         MonoJitInfo *ji;
212
213                         ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
214                         if (ji)
215                                 printf ("Caller: %s\n", mono_method_full_name (ji->method, TRUE));
216                         /* Print some debug info */
217                         for (i = 0; i < 32; ++i)
218                                 printf ("0x%x ", code [-32 + i]);
219                         printf ("\n");
220                         g_assert (vt);
221                 }
222                 if (displacement > 0) {
223                         displacement -= G_STRUCT_OFFSET (MonoVTable, vtable);
224                         g_assert (displacement >= 0);
225                         displacement /= sizeof (gpointer);
226
227                         /* Avoid loading metadata or creating a generic vtable if possible */
228                         addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, displacement);
229                         if (addr)
230                                 addr = mono_create_ftnptr (mono_domain_get (), addr);
231                         if (addr && !vt->klass->valuetype) {
232                                 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
233                                 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
234                                         *vtable_slot = mono_get_addr_from_ftnptr (addr);
235                                 }
236
237                                 return addr;
238                         }
239
240                         m = mono_class_get_vtable_entry (vt->klass, displacement);
241                         if (mono_method_needs_static_rgctx_invoke (m, FALSE))
242                                 need_rgctx_tramp = TRUE;
243
244                         /*g_print ("%s with disp %d: %s at %p\n", vt->klass->name, displacement, m->name, code);*/
245                 } else {
246                         /* We got here from an interface method: redirect to IMT handling */
247                         m = MONO_FAKE_IMT_METHOD;
248                         /*g_print ("vtable with disp %d at %p\n", displacement, code);*/
249                 }
250         }
251
252         /* this is the IMT trampoline */
253 #ifdef MONO_ARCH_HAVE_IMT
254         if (m == MONO_FAKE_IMT_METHOD) {
255                 MonoMethod *impl_method;
256                 MonoGenericSharingContext *gsctx;
257                 MonoObject *this_arg;
258
259                 /* we get the interface method because mono_convert_imt_slot_to_vtable_slot ()
260                  * needs the signature to be able to find the this argument
261                  */
262                 m = mono_arch_find_imt_method ((gpointer*)regs, code);
263                 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
264                 g_assert (vtable_slot);
265
266                 gsctx = mono_get_generic_context_from_code (code);
267                 this_arg = mono_arch_find_this_argument ((gpointer*)regs, m, gsctx);
268
269                 if (this_arg->vtable->klass == mono_defaults.transparent_proxy_class) {
270                         /* Use the slow path for now */
271                         proxy = TRUE;
272                     m = mono_object_get_virtual_method (this_arg, m);
273                 } else {
274                         vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, &impl_method, &need_rgctx_tramp);
275                         /* mono_convert_imt_slot_to_vtable_slot () also gives us the method that is supposed
276                          * to be called, so we compile it and go ahead as usual.
277                          */
278                         /*g_print ("imt found method %p (%s) at %p\n", impl_method, impl_method->name, code);*/
279                         if (m->is_inflated && ((MonoMethodInflated*)m)->context.method_inst) {
280                                 /* Generic virtual method */
281                                 generic_virtual = m;
282                                 m = impl_method;
283                                 need_rgctx_tramp = TRUE;
284                         } else {
285                                 m = impl_method;
286                         }
287                 }
288         }
289 #endif
290
291         if (m->is_generic) {
292                 MonoGenericContext context = { NULL, NULL };
293                 MonoMethod *declaring;
294
295                 if (m->is_inflated)
296                         declaring = mono_method_get_declaring_generic_method (m);
297                 else
298                         declaring = m;
299
300                 if (m->klass->generic_class)
301                         context.class_inst = m->klass->generic_class->context.class_inst;
302                 else
303                         g_assert (!m->klass->generic_container);
304
305 #ifdef MONO_ARCH_HAVE_IMT
306                 generic_virtual = mono_arch_find_imt_method ((gpointer*)regs, code);
307 #endif
308                 if (generic_virtual) {
309                         g_assert (generic_virtual->is_inflated);
310                         context.method_inst = ((MonoMethodInflated*)generic_virtual)->context.method_inst;
311                 }
312
313                 m = mono_class_inflate_generic_method (declaring, &context);
314                 /* FIXME: only do this if the method is sharable */
315                 need_rgctx_tramp = TRUE;
316         } else if ((context_used = mono_method_check_context_used (m))) {
317                 MonoClass *klass = NULL;
318                 MonoMethod *actual_method = NULL;
319                 MonoVTable *vt = NULL;
320                 MonoGenericInst *method_inst = NULL;
321
322                 vtable_slot = NULL;
323                 generic_shared = TRUE;
324
325                 g_assert (code);
326
327                 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
328 #ifdef MONO_ARCH_RGCTX_REG
329                         MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext*)mono_arch_find_static_call_vtable ((gpointer*)regs, code);
330
331                         klass = mrgctx->class_vtable->klass;
332                         method_inst = mrgctx->method_inst;
333 #else
334                         g_assert_not_reached ();
335 #endif
336                 } else if ((m->flags & METHOD_ATTRIBUTE_STATIC) || m->klass->valuetype) {
337 #ifdef MONO_ARCH_RGCTX_REG
338                         MonoVTable *vtable = mono_arch_find_static_call_vtable ((gpointer*)regs, code);
339
340                         klass = vtable->klass;
341 #else
342                         g_assert_not_reached ();
343 #endif
344                 } else {
345 #ifdef MONO_ARCH_HAVE_IMT
346                         MonoObject *this_argument = mono_arch_find_this_argument ((gpointer*)regs, m,
347                                 mono_get_generic_context_from_code (code));
348
349                         vt = this_argument->vtable;
350                         vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
351
352                         g_assert (this_argument->vtable->klass->inited);
353                         //mono_class_init (this_argument->vtable->klass);
354
355                         if (!vtable_slot)
356                                 klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
357 #else
358                         NOT_IMPLEMENTED;
359 #endif
360                 }
361
362                 g_assert (vtable_slot || klass);
363
364                 if (vtable_slot) {
365                         int displacement = vtable_slot - ((gpointer*)vt);
366
367                         g_assert_not_reached ();
368
369                         g_assert (displacement > 0);
370
371                         actual_method = vt->klass->vtable [displacement];
372                 }
373
374                 if (method_inst) {
375                         MonoGenericContext context = { NULL, NULL };
376
377                         if (m->is_inflated)
378                                 declaring = mono_method_get_declaring_generic_method (m);
379                         else
380                                 declaring = m;
381
382                         if (klass->generic_class)
383                                 context.class_inst = klass->generic_class->context.class_inst;
384                         else if (klass->generic_container)
385                                 context.class_inst = klass->generic_container->context.class_inst;
386                         context.method_inst = method_inst;
387
388                         actual_method = mono_class_inflate_generic_method (declaring, &context);
389                 } else {
390                         actual_method = mono_class_get_method_generic (klass, m);
391                 }
392
393                 g_assert (klass);
394                 g_assert (actual_method->klass == klass);
395
396                 if (actual_method->is_inflated)
397                         declaring = mono_method_get_declaring_generic_method (actual_method);
398                 else
399                         declaring = NULL;
400
401                 m = actual_method;
402         }
403
404         if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
405                 MonoJitInfo *ji;
406
407                 if (code)
408                         ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
409                 else
410                         ji = NULL;
411
412                 /* Avoid recursion */
413                 if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
414                         m = mono_marshal_get_synchronized_wrapper (m);
415         }
416
417         /* Calls made through delegates on platforms without delegate trampolines */
418         if (!code && mono_method_needs_static_rgctx_invoke (m, FALSE))
419                 need_rgctx_tramp = TRUE;
420
421         addr = mono_compile_method (m);
422         g_assert (addr);
423
424         mono_debugger_trampoline_compiled (m, addr);
425
426         if (need_rgctx_tramp)
427                 addr = mono_create_static_rgctx_trampoline (m, addr);
428
429         if (generic_virtual) {
430                 int displacement;
431                 MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
432
433                 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
434                 g_assert (vtable_slot);
435
436                 if (vt->klass->valuetype)
437                         addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr);                       
438
439                 mono_method_add_generic_virtual_invocation (mono_domain_get (), 
440                                                                                                         vt, vtable_slot,
441                                                                                                         generic_virtual, addr);
442
443                 return addr;
444         }
445
446         /* the method was jumped to */
447         if (!code) {
448                 MonoDomain *domain = mono_domain_get ();
449
450                 /* Patch the got entries pointing to this method */
451                 /* 
452                  * We do this here instead of in mono_codegen () to cover the case when m
453                  * was loaded from an aot image.
454                  */
455                 if (domain_jit_info (domain)->jump_target_got_slot_hash) {
456                         GSList *list, *tmp;
457
458                         mono_domain_lock (domain);
459                         list = g_hash_table_lookup (domain_jit_info (domain)->jump_target_got_slot_hash, m);
460                         if (list) {
461                                 for (tmp = list; tmp; tmp = tmp->next) {
462                                         gpointer *got_slot = tmp->data;
463                                         *got_slot = addr;
464                                 }
465                                 g_hash_table_remove (domain_jit_info (domain)->jump_target_got_slot_hash, m);
466                                 g_slist_free (list);
467                         }
468                         mono_domain_unlock (domain);
469                 }
470
471                 return addr;
472         }
473
474         vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
475
476         if (vtable_slot) {
477                 if (m->klass->valuetype)
478                         addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr);
479                 g_assert (*vtable_slot);
480
481                 if (!proxy && (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))) {
482 #ifdef MONO_ARCH_HAVE_IMT
483                         vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, NULL, &need_rgctx_tramp);
484 #endif
485                         *vtable_slot = mono_get_addr_from_ftnptr (addr);
486                 }
487         }
488         else {
489                 guint8 *plt_entry = mono_aot_get_plt_entry (code);
490
491                 if (plt_entry) {
492                         mono_arch_patch_plt_entry (plt_entry, NULL, regs, addr);
493                 } else if (!generic_shared || (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
494                         mono_domain_lookup_shared_generic (mono_domain_get (), declaring)) {
495                         if (generic_shared) {
496                                 if (m->wrapper_type != MONO_WRAPPER_NONE)
497                                         m = mono_marshal_method_from_wrapper (m);
498                                 g_assert (mono_method_is_generic_sharable_impl (m, FALSE));
499                         }
500
501                         /* Patch calling code */
502                         if (plt_entry) {
503
504                         } else {
505                                 MonoJitInfo *ji = 
506                                         mono_jit_info_table_find (mono_domain_get (), (char*)code);
507                                 MonoJitInfo *target_ji = 
508                                         mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
509
510                                 if (mono_method_same_domain (ji, target_ji))
511                                         mono_arch_patch_callsite (ji->code_start, code, addr);
512                         }
513                 }
514         }
515
516         return addr;
517 }
518  
519 #ifdef ENABLE_LLVM
520 /*
521  * mono_llvm_vcall_trampoline:
522  *
523  *   This trampoline handles virtual calls when using LLVM.
524  */
525 static gpointer
526 mono_llvm_vcall_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
527 {
528         MonoObject *this;
529         gpointer addr;
530         MonoVTable *vt;
531         gpointer *vtable_slot;
532         gboolean proxy = FALSE;
533         gboolean need_rgctx_tramp = FALSE;
534
535         /* 
536          * We have the method which is called, we need to obtain the vtable slot without
537          * disassembly which is impossible with LLVM.
538          * So we use the this argument.
539          */
540         this = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (m), regs, code);
541         g_assert (this);
542
543         vt = this->vtable;
544
545         g_assert (!m->is_generic);
546
547         /* This is a simplified version of mono_magic_trampoline () */
548         /* FIXME: Avoid code duplication */
549
550         if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
551                 MonoJitInfo *ji;
552
553                 if (code)
554                         ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
555                 else
556                         ji = NULL;
557
558                 /* Avoid recursion */
559                 if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
560                         m = mono_marshal_get_synchronized_wrapper (m);
561         }
562
563         addr = mono_compile_method (m);
564         g_assert (addr);
565
566         if (m->klass->valuetype)
567                 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr);
568
569         vtable_slot = &(vt->vtable [mono_method_get_vtable_slot (m)]);
570         g_assert (*vtable_slot);
571
572         if (!proxy && (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))) {
573 #ifdef MONO_ARCH_HAVE_IMT
574                 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, NULL, &need_rgctx_tramp);
575 #endif
576                 *vtable_slot = mono_get_addr_from_ftnptr (addr);
577           }
578
579         mono_debugger_trampoline_compiled (m, addr);
580
581         return addr;
582 }
583 #endif
584
585 gpointer
586 mono_generic_virtual_remoting_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
587 {
588         MonoGenericContext context = { NULL, NULL };
589         MonoMethod *imt_method, *declaring;
590         gpointer addr;
591
592         g_assert (m->is_generic);
593
594         if (m->is_inflated)
595                 declaring = mono_method_get_declaring_generic_method (m);
596         else
597                 declaring = m;
598
599         if (m->klass->generic_class)
600                 context.class_inst = m->klass->generic_class->context.class_inst;
601         else
602                 g_assert (!m->klass->generic_container);
603
604 #ifdef MONO_ARCH_HAVE_IMT
605         imt_method = mono_arch_find_imt_method ((gpointer*)regs, code);
606         if (imt_method->is_inflated)
607                 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
608 #endif
609         m = mono_class_inflate_generic_method (declaring, &context);
610         m = mono_marshal_get_remoting_invoke_with_check (m);
611
612         addr = mono_compile_method (m);
613         g_assert (addr);
614
615         mono_debugger_trampoline_compiled (m, addr);
616
617         return addr;
618 }
619
620 /*
621  * mono_aot_trampoline:
622  *
623  *   This trampoline handles calls made from AOT code. We try to bypass the 
624  * normal JIT compilation logic to avoid loading the metadata for the method.
625  */
626 #ifdef MONO_ARCH_AOT_SUPPORTED
627 gpointer
628 mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info, 
629                                          guint8* tramp)
630 {
631         MonoImage *image;
632         guint32 token;
633         MonoMethod *method = NULL;
634         gpointer addr;
635         gpointer *vtable_slot;
636         gboolean is_got_entry;
637         guint8 *plt_entry;
638         gboolean need_rgctx_tramp = FALSE;
639
640         image = *(gpointer*)(gpointer)token_info;
641         token_info += sizeof (gpointer);
642         token = *(guint32*)(gpointer)token_info;
643
644         addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
645         if (!addr) {
646                 method = mono_get_method (image, token, NULL);
647                 g_assert (method);
648
649                 /* Use the generic code */
650                 return mono_magic_trampoline (regs, code, method, tramp);
651         }
652
653         addr = mono_create_ftnptr (mono_domain_get (), addr);
654
655         vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
656         g_assert (!vtable_slot);
657
658         /* This is a normal call through a PLT entry */
659         plt_entry = mono_aot_get_plt_entry (code);
660         g_assert (plt_entry);
661
662         mono_arch_patch_plt_entry (plt_entry, NULL, regs, addr);
663
664         is_got_entry = FALSE;
665
666         /*
667          * Since AOT code is only used in the root domain, 
668          * mono_domain_get () != mono_get_root_domain () means the calling method
669          * is AppDomain:InvokeInDomain, so this is the same check as in 
670          * mono_method_same_domain () but without loading the metadata for the method.
671          */
672         if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
673 #ifdef MONO_ARCH_HAVE_IMT
674                 if (!method)
675                         method = mono_get_method (image, token, NULL);
676                 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, method, NULL, &need_rgctx_tramp);
677 #endif
678                 *vtable_slot = addr;
679         }
680
681         return addr;
682 }
683
684 /*
685  * mono_aot_plt_trampoline:
686  *
687  *   This trampoline handles calls made from AOT code through the PLT table.
688  */
689 gpointer
690 mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module, 
691                                                  guint8* tramp)
692 {
693         guint32 plt_info_offset = mono_aot_get_plt_info_offset (regs, code);
694
695         return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
696 }
697 #endif
698
699 /**
700  * mono_class_init_trampoline:
701  *
702  * This method calls mono_runtime_class_init () to run the static constructor
703  * for the type, then patches the caller code so it is not called again.
704  */
705 void
706 mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
707 {
708         guint8 *plt_entry = mono_aot_get_plt_entry (code);
709
710         mono_runtime_class_init (vtable);
711
712         if (plt_entry) {
713                 mono_arch_nullify_plt_entry (plt_entry, regs);
714         } else {
715                 mono_arch_nullify_class_init_trampoline (code, regs);
716         }
717 }
718
719 /**
720  * mono_generic_class_init_trampoline:
721  *
722  * This method calls mono_runtime_class_init () to run the static constructor
723  * for the type.
724  */
725 void
726 mono_generic_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
727 {
728         mono_runtime_class_init (vtable);
729 }
730
731 static gpointer
732 mono_rgctx_lazy_fetch_trampoline (gssize *regs, guint8 *code, gpointer data, guint8 *tramp)
733 {
734 #ifdef MONO_ARCH_VTABLE_REG
735         static gboolean inited = FALSE;
736         static int num_lookups = 0;
737         guint32 slot = GPOINTER_TO_UINT (data);
738         gpointer arg = (gpointer)(gssize)regs [MONO_ARCH_VTABLE_REG];
739         guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
740         gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
741
742         if (!inited) {
743                 mono_counters_register ("RGCTX unmanaged lookups", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_lookups);
744                 inited = TRUE;
745         }
746
747         num_lookups++;
748
749         if (mrgctx)
750                 return mono_method_fill_runtime_generic_context (arg, index);
751         else
752                 return mono_class_fill_runtime_generic_context (arg, index);
753 #else
754         g_assert_not_reached ();
755 #endif
756 }
757
758 void
759 mono_monitor_enter_trampoline (gssize *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
760 {
761         mono_monitor_enter (obj);
762 }
763
764 void
765 mono_monitor_exit_trampoline (gssize *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
766 {
767         mono_monitor_exit (obj);
768 }
769
770 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
771
772 /**
773  * mono_delegate_trampoline:
774  *
775  *   This trampoline handles calls made to Delegate:Invoke ().
776  * This is called once the first time a delegate is invoked, so it must be fast.
777  */
778 gpointer
779 mono_delegate_trampoline (gssize *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
780 {
781         MonoDomain *domain = mono_domain_get ();
782         MonoDelegate *delegate;
783         MonoJitInfo *ji;
784         MonoMethod *m;
785         MonoMethod *method = NULL;
786         gboolean multicast, callvirt;
787         gboolean need_rgctx_tramp = FALSE;
788         MonoMethod *invoke = tramp_data [0];
789         guint8 *impl_this = tramp_data [1];
790         guint8 *impl_nothis = tramp_data [2];
791
792         /* Obtain the delegate object according to the calling convention */
793
794         /* 
795          * Avoid calling mono_get_generic_context_from_code () now since it is expensive, 
796          * get_this_arg_from_call will call it if needed.
797          */
798         delegate = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (invoke), regs, code);
799
800         if (delegate->method) {
801                 method = delegate->method;
802
803                 /*
804                  * delegate->method_ptr == NULL means the delegate was initialized by 
805                  * mini_delegate_ctor, while != NULL means it is initialized by 
806                  * mono_delegate_ctor_with_method (). In both cases, we need to add wrappers
807                  * (ctor_with_method () does this, but it doesn't store the wrapper back into
808                  * delegate->method).
809                  */
810                 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class)
811                         method = mono_marshal_get_remoting_invoke (method);
812                 else if (mono_method_signature (method)->hasthis && method->klass->valuetype)
813                         method = mono_marshal_get_unbox_wrapper (method);
814         } else {
815                 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
816                 if (ji)
817                         method = ji->method;
818         }
819         callvirt = !delegate->target && method && mono_method_signature (method)->hasthis;
820
821         if (method && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
822                 method = mono_marshal_get_synchronized_wrapper (method);
823
824         if (method && mono_method_needs_static_rgctx_invoke (method, FALSE))
825                 need_rgctx_tramp = TRUE;
826
827         /* 
828          * If the called address is a trampoline, replace it with the compiled method so
829          * further calls don't have to go through the trampoline.
830          */
831         if (method && !callvirt) {
832                 /* Avoid the overhead of looking up an already compiled method if possible */
833                 if (delegate->method_code && *delegate->method_code) {
834                         delegate->method_ptr = *delegate->method_code;
835                 } else {
836                         delegate->method_ptr = mono_compile_method (method);
837                         if (delegate->method_code)
838                                 *delegate->method_code = delegate->method_ptr;
839                         mono_debugger_trampoline_compiled (method, delegate->method_ptr);
840                 }
841         }
842
843         if (need_rgctx_tramp)
844                 delegate->method_ptr = mono_create_static_rgctx_trampoline (method, delegate->method_ptr);
845
846         multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
847         if (!multicast && !callvirt) {
848                 if (method && (method->flags & METHOD_ATTRIBUTE_STATIC) && mono_method_signature (method)->param_count == mono_method_signature (invoke)->param_count + 1)
849                         /* Closed static delegate */
850                         code = impl_this;
851                 else
852                         code = delegate->target ? impl_this : impl_nothis;
853
854                 if (code) {
855                         delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
856                         return code;
857                 }
858         }
859
860         /* The general, unoptimized case */
861         m = mono_marshal_get_delegate_invoke (invoke, delegate);
862         code = mono_compile_method (m);
863         delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
864         mono_debugger_trampoline_compiled (m, delegate->invoke_impl);
865
866         return code;
867 }
868
869 #endif
870
871 /*
872  * mono_get_trampoline_func:
873  *
874  *   Return the C function which needs to be called by the generic trampoline of type
875  * TRAMP_TYPE.
876  */
877 gconstpointer
878 mono_get_trampoline_func (MonoTrampolineType tramp_type)
879 {
880         switch (tramp_type) {
881         case MONO_TRAMPOLINE_JIT:
882         case MONO_TRAMPOLINE_JUMP:
883                 return mono_magic_trampoline;
884         case MONO_TRAMPOLINE_CLASS_INIT:
885                 return mono_class_init_trampoline;
886         case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
887                 return mono_generic_class_init_trampoline;
888         case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
889                 return mono_rgctx_lazy_fetch_trampoline;
890 #ifdef MONO_ARCH_AOT_SUPPORTED
891         case MONO_TRAMPOLINE_AOT:
892                 return mono_aot_trampoline;
893         case MONO_TRAMPOLINE_AOT_PLT:
894                 return mono_aot_plt_trampoline;
895 #endif
896 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
897         case MONO_TRAMPOLINE_DELEGATE:
898                 return mono_delegate_trampoline;
899 #endif
900         case MONO_TRAMPOLINE_RESTORE_STACK_PROT:
901                 return mono_altstack_restore_prot;
902         case MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING:
903                 return mono_generic_virtual_remoting_trampoline;
904         case MONO_TRAMPOLINE_MONITOR_ENTER:
905                 return mono_monitor_enter_trampoline;
906         case MONO_TRAMPOLINE_MONITOR_EXIT:
907                 return mono_monitor_exit_trampoline;
908 #ifdef ENABLE_LLVM
909         case MONO_TRAMPOLINE_LLVM_VCALL:
910                 return mono_llvm_vcall_trampoline;
911 #endif
912         default:
913                 g_assert_not_reached ();
914                 return NULL;
915         }
916 }
917
918 void
919 mono_trampolines_init (void)
920 {
921         InitializeCriticalSection (&trampolines_mutex);
922
923         if (mono_aot_only)
924                 return;
925
926         mono_trampoline_code [MONO_TRAMPOLINE_JIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JIT);
927         mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
928         mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
929         mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
930         mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
931 #ifdef MONO_ARCH_AOT_SUPPORTED
932         mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
933         mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
934 #endif
935 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
936         mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
937 #endif
938         mono_trampoline_code [MONO_TRAMPOLINE_RESTORE_STACK_PROT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RESTORE_STACK_PROT);
939         mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING);
940         mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_ENTER] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
941         mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_EXIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
942 #ifdef ENABLE_LLVM
943         mono_trampoline_code [MONO_TRAMPOLINE_LLVM_VCALL] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_LLVM_VCALL);
944 #endif
945 }
946
947 void
948 mono_trampolines_cleanup (void)
949 {
950         if (class_init_hash_addr)
951                 g_hash_table_destroy (class_init_hash_addr);
952
953         DeleteCriticalSection (&trampolines_mutex);
954 }
955
956 guint8 *
957 mono_get_trampoline_code (MonoTrampolineType tramp_type)
958 {
959         g_assert (mono_trampoline_code [tramp_type]);
960
961         return mono_trampoline_code [tramp_type];
962 }
963
964 gpointer
965 mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
966 {
967         if (mono_aot_only)
968                 return mono_aot_create_specific_trampoline (mono_defaults.corlib, arg1, tramp_type, domain, code_len);
969         else
970                 return mono_arch_create_specific_trampoline (arg1, tramp_type, domain, code_len);
971 }
972
973 gpointer
974 mono_create_class_init_trampoline (MonoVTable *vtable)
975 {
976         gpointer code, ptr;
977         MonoDomain *domain = vtable->domain;
978
979         g_assert (!vtable->klass->generic_container);
980
981         /* previously created trampoline code */
982         mono_domain_lock (domain);
983         ptr = 
984                 g_hash_table_lookup (domain_jit_info (domain)->class_init_trampoline_hash,
985                                                                   vtable);
986         mono_domain_unlock (domain);
987         if (ptr)
988                 return ptr;
989
990         code = mono_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, domain, NULL);
991
992         ptr = mono_create_ftnptr (domain, code);
993
994         /* store trampoline address */
995         mono_domain_lock (domain);
996         g_hash_table_insert (domain_jit_info (domain)->class_init_trampoline_hash,
997                                                           vtable, ptr);
998         mono_domain_unlock (domain);
999
1000         mono_trampolines_lock ();
1001         if (!class_init_hash_addr)
1002                 class_init_hash_addr = g_hash_table_new (NULL, NULL);
1003         g_hash_table_insert (class_init_hash_addr, ptr, vtable);
1004         mono_trampolines_unlock ();
1005
1006         return ptr;
1007 }
1008
1009 gpointer
1010 mono_create_generic_class_init_trampoline (void)
1011 {
1012 #ifdef MONO_ARCH_VTABLE_REG
1013         static gpointer code;
1014
1015         mono_trampolines_lock ();
1016
1017         if (!code) {
1018                 if (mono_aot_only)
1019                         code = mono_aot_get_named_code ("generic_class_init_trampoline");
1020                 else
1021                         code = mono_arch_create_generic_class_init_trampoline ();
1022         }
1023
1024         mono_trampolines_unlock ();
1025
1026         return code;
1027 #else
1028         g_assert_not_reached ();
1029 #endif
1030 }
1031
1032 gpointer
1033 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper)
1034 {
1035         MonoJitInfo *ji;
1036         gpointer code;
1037         guint32 code_size = 0;
1038
1039         code = mono_jit_find_compiled_method_with_jit_info (domain, method, &ji);
1040         /*
1041          * We cannot recover the correct type of a shared generic
1042          * method from its native code address, so we use the
1043          * trampoline instead.
1044          */
1045         if (code && !ji->has_generic_jit_info)
1046                 return code;
1047
1048         mono_domain_lock (domain);
1049         code = g_hash_table_lookup (domain_jit_info (domain)->jump_trampoline_hash, method);
1050         mono_domain_unlock (domain);
1051         if (code)
1052                 return code;
1053
1054         code = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
1055         g_assert (code_size);
1056
1057         ji = mono_domain_alloc0 (domain, sizeof (MonoJitInfo));
1058         ji->code_start = code;
1059         ji->code_size = code_size;
1060         ji->method = method;
1061
1062         /*
1063          * mono_delegate_ctor needs to find the method metadata from the 
1064          * trampoline address, so we save it here.
1065          */
1066
1067         mono_jit_info_table_add (domain, ji);
1068
1069         mono_domain_lock (domain);
1070         g_hash_table_insert (domain_jit_info (domain)->jump_trampoline_hash, method, ji->code_start);
1071         mono_domain_unlock (domain);
1072
1073         return ji->code_start;
1074 }
1075
1076 gpointer
1077 mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method)
1078 {
1079         gpointer tramp;
1080
1081         if (mono_aot_only) {
1082                 /* Avoid creating trampolines if possible */
1083                 gpointer code = mono_jit_find_compiled_method (domain, method);
1084                 
1085                 if (code)
1086                         return code;
1087         }
1088
1089         mono_domain_lock (domain);
1090         tramp = g_hash_table_lookup (domain_jit_info (domain)->jit_trampoline_hash, method);
1091         mono_domain_unlock (domain);
1092         if (tramp)
1093                 return tramp;
1094
1095         tramp = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JIT, domain, NULL);
1096         
1097         mono_domain_lock (domain);
1098         g_hash_table_insert (domain_jit_info (domain)->jit_trampoline_hash, method, tramp);
1099         mono_domain_unlock (domain);
1100
1101         mono_jit_stats.method_trampolines++;
1102
1103         return tramp;
1104 }       
1105
1106 gpointer
1107 mono_create_jit_trampoline (MonoMethod *method)
1108 {
1109         return mono_create_jit_trampoline_in_domain (mono_domain_get (), method);
1110 }
1111
1112 gpointer
1113 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
1114 {
1115         gpointer tramp;
1116
1117         MonoDomain *domain = mono_domain_get ();
1118         guint8 *buf, *start;
1119
1120         buf = start = mono_domain_code_reserve (domain, 2 * sizeof (gpointer));
1121
1122         *(gpointer*)(gpointer)buf = image;
1123         buf += sizeof (gpointer);
1124         *(guint32*)(gpointer)buf = token;
1125
1126         tramp = mono_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
1127
1128         mono_jit_stats.method_trampolines++;
1129
1130         return tramp;
1131 }       
1132
1133 gpointer
1134 mono_create_delegate_trampoline (MonoClass *klass)
1135 {
1136 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
1137         MonoDomain *domain = mono_domain_get ();
1138         gpointer ptr;
1139         guint32 code_size = 0;
1140         gpointer *tramp_data;
1141         MonoMethod *invoke;
1142
1143         mono_domain_lock (domain);
1144         ptr = g_hash_table_lookup (domain_jit_info (domain)->delegate_trampoline_hash, klass);
1145         mono_domain_unlock (domain);
1146         if (ptr)
1147                 return ptr;
1148
1149         // Precompute the delegate invoke impl and pass it to the delegate trampoline
1150         invoke = mono_get_delegate_invoke (klass);
1151         g_assert (invoke);
1152
1153         tramp_data = mono_domain_alloc (domain, sizeof (gpointer) * 3);
1154         tramp_data [0] = invoke;
1155         tramp_data [1] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
1156         tramp_data [2] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
1157
1158         ptr = mono_create_specific_trampoline (tramp_data, MONO_TRAMPOLINE_DELEGATE, mono_domain_get (), &code_size);
1159         g_assert (code_size);
1160
1161         /* store trampoline address */
1162         mono_domain_lock (domain);
1163         g_hash_table_insert (domain_jit_info (domain)->delegate_trampoline_hash,
1164                                                           klass, ptr);
1165         mono_domain_unlock (domain);
1166
1167         return ptr;
1168 #else
1169         return NULL;
1170 #endif
1171 }
1172
1173 gpointer
1174 mono_create_rgctx_lazy_fetch_trampoline (guint32 offset)
1175 {
1176         static gboolean inited = FALSE;
1177         static int num_trampolines = 0;
1178
1179         gpointer tramp, ptr;
1180
1181         if (mono_aot_only)
1182                 return mono_aot_get_lazy_fetch_trampoline (offset);
1183
1184         mono_trampolines_lock ();
1185         if (rgctx_lazy_fetch_trampoline_hash)
1186                 tramp = g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset));
1187         else
1188                 tramp = NULL;
1189         mono_trampolines_unlock ();
1190         if (tramp)
1191                 return tramp;
1192
1193         tramp = mono_arch_create_rgctx_lazy_fetch_trampoline (offset);
1194         ptr = mono_create_ftnptr (mono_get_root_domain (), tramp);
1195
1196         mono_trampolines_lock ();
1197         if (!rgctx_lazy_fetch_trampoline_hash) {
1198                 rgctx_lazy_fetch_trampoline_hash = g_hash_table_new (NULL, NULL);
1199                 rgctx_lazy_fetch_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
1200         }
1201         g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset), ptr);
1202         g_assert (offset != -1);
1203         g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash_addr, ptr, GUINT_TO_POINTER (offset + 1));
1204         mono_trampolines_unlock ();
1205
1206         if (!inited) {
1207                 mono_counters_register ("RGCTX num lazy fetch trampolines",
1208                                 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_trampolines);
1209                 inited = TRUE;
1210         }
1211         num_trampolines++;
1212
1213         return ptr;
1214 }
1215
1216 gpointer
1217 mono_create_monitor_enter_trampoline (void)
1218 {
1219         static gpointer code;
1220
1221         if (mono_aot_only) {
1222                 if (!code)
1223                         code = mono_aot_get_named_code ("monitor_enter_trampoline");
1224                 return code;
1225         }
1226
1227 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1228         mono_trampolines_lock ();
1229
1230         if (!code)
1231                 code = mono_arch_create_monitor_enter_trampoline ();
1232
1233         mono_trampolines_unlock ();
1234 #else
1235         code = NULL;
1236         g_assert_not_reached ();
1237 #endif
1238
1239         return code;
1240 }
1241
1242 gpointer
1243 mono_create_monitor_exit_trampoline (void)
1244 {
1245         static gpointer code;
1246
1247         if (mono_aot_only) {
1248                 if (!code)
1249                         code = mono_aot_get_named_code ("monitor_exit_trampoline");
1250                 return code;
1251         }
1252
1253 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1254         mono_trampolines_lock ();
1255
1256         if (!code)
1257                 code = mono_arch_create_monitor_exit_trampoline ();
1258
1259         mono_trampolines_unlock ();
1260 #else
1261         code = NULL;
1262         g_assert_not_reached ();
1263 #endif
1264         return code;
1265 }
1266  
1267 #ifdef ENABLE_LLVM
1268 /*
1269  * mono_create_llvm_vcall_trampoline:
1270  *
1271  *  LLVM emits code for virtual calls which mono_get_vcall_slot is unable to
1272  * decode, i.e. only the final branch address is available:
1273  * mov <offset>(%rax), %rax
1274  * <random code inserted by instruction scheduling>
1275  * call *%rax
1276  *
1277  * To work around this problem, we don't use the common vtable trampoline when
1278  * llvm is enabled. Instead, we use one trampoline per method.
1279  */
1280 gpointer
1281 mono_create_llvm_vcall_trampoline (MonoMethod *method)
1282 {
1283         MonoDomain *domain;
1284         gpointer res;
1285
1286         domain = mono_domain_get ();
1287
1288         mono_domain_lock (domain);
1289         res = g_hash_table_lookup (domain_jit_info (domain)->llvm_vcall_trampoline_hash, method);
1290         mono_domain_unlock (domain);
1291         if (res)
1292                 return res;
1293
1294         res = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_LLVM_VCALL, domain, NULL);
1295
1296         mono_domain_lock (domain);
1297         g_hash_table_insert (domain_jit_info (domain)->llvm_vcall_trampoline_hash, method, res);
1298         mono_domain_unlock (domain);
1299
1300         return res;
1301 }
1302 #endif
1303
1304 MonoVTable*
1305 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
1306 {
1307         MonoVTable *res;
1308
1309         mono_trampolines_lock ();
1310         if (class_init_hash_addr)
1311                 res = g_hash_table_lookup (class_init_hash_addr, addr);
1312         else
1313                 res = NULL;
1314         mono_trampolines_unlock ();
1315         return res;
1316 }
1317
1318 guint32
1319 mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr)
1320 {
1321         int offset;
1322
1323         mono_trampolines_lock ();
1324         if (rgctx_lazy_fetch_trampoline_hash_addr) {
1325                 /* We store the real offset + 1 so we can detect when the lookup fails */
1326                 offset = GPOINTER_TO_INT (g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash_addr, addr));
1327                 if (offset)
1328                         offset -= 1;
1329                 else
1330                         offset = -1;
1331         } else {
1332                 offset = -1;
1333         }
1334         mono_trampolines_unlock ();
1335         return offset;
1336 }