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