12e0fd97758e71fe85d2534e59d4afc140a07a8b
[mono.git] / mono / mini / mini-trampolines.c
1 /*
2  * (C) 2003 Ximian, Inc.
3  * (C) 2003-2011 Novell, Inc.
4  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
5  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
6  */
7 #include <config.h>
8 #include <glib.h>
9
10 #include <mono/metadata/appdomain.h>
11 #include <mono/metadata/metadata-internals.h>
12 #include <mono/metadata/marshal.h>
13 #include <mono/metadata/tabledefs.h>
14 #include <mono/utils/mono-counters.h>
15 #include <mono/utils/mono-error-internals.h>
16 #include <mono/utils/mono-membar.h>
17 #include <mono/utils/mono-compiler.h>
18 #include <mono/utils/mono-threads-coop.h>
19
20 #include "mini.h"
21
22 /*
23  * Address of the trampoline code.  This is used by the debugger to check
24  * whether a method is a trampoline.
25  */
26 guint8* mono_trampoline_code [MONO_TRAMPOLINE_NUM];
27
28 static GHashTable *rgctx_lazy_fetch_trampoline_hash;
29 static GHashTable *rgctx_lazy_fetch_trampoline_hash_addr;
30 static guint32 trampoline_calls, jit_trampolines, unbox_trampolines, static_rgctx_trampolines;
31
32 #define mono_trampolines_lock() mono_os_mutex_lock (&trampolines_mutex)
33 #define mono_trampolines_unlock() mono_os_mutex_unlock (&trampolines_mutex)
34 static mono_mutex_t trampolines_mutex;
35
36 #ifdef MONO_ARCH_GSHARED_SUPPORTED
37
38 typedef struct {
39         MonoMethod *m;
40         gpointer addr;
41 } RgctxTrampInfo;
42
43 static gint
44 rgctx_tramp_info_equal (gconstpointer ka, gconstpointer kb)
45 {
46         const RgctxTrampInfo *i1 = (const RgctxTrampInfo *)ka;
47         const RgctxTrampInfo *i2 = (const RgctxTrampInfo *)kb;
48
49         if (i1->m == i2->m && i1->addr == i2->addr)
50                 return 1;
51         else
52                 return 0;
53 }
54
55 static guint
56 rgctx_tramp_info_hash (gconstpointer data)
57 {
58         const RgctxTrampInfo *info = (const RgctxTrampInfo *)data;
59
60         return GPOINTER_TO_UINT (info->m) ^ GPOINTER_TO_UINT (info->addr);
61 }
62
63 /**
64  * mono_create_static_rgctx_trampoline:
65  * @m: the mono method to create a trampoline for
66  * @addr: the address to jump to (where the compiled code for M lives)
67  *
68  * Creates a static rgctx trampoline for M which branches to ADDR which should
69  * point to the compiled code of M.
70  *
71  * Static rgctx trampolines are used when a shared generic method which doesn't
72  * have a this argument is called indirectly, ie. from code which can't pass in
73  * the rgctx argument. The trampoline sets the rgctx argument and jumps to the
74  * methods code. These trampolines are similar to the unbox trampolines, they
75  * perform the same task as the static rgctx wrappers, but they are smaller/faster,
76  * and can be made to work with full AOT.
77  *
78  * On PPC addr should be an ftnptr and the return value is an ftnptr too.
79  *
80  * Returns the generated static rgctx trampoline.
81  */
82 gpointer
83 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
84 {
85         gpointer ctx;
86         gpointer res;
87         MonoDomain *domain;
88         RgctxTrampInfo tmp_info;
89         RgctxTrampInfo *info;
90
91 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
92         g_assert (((gpointer*)addr) [2] == 0);
93 #endif
94
95         ctx = mini_method_get_rgctx (m);
96
97         domain = mono_domain_get ();
98
99         /* 
100          * In the AOT case, addr might point to either the method, or to an unbox trampoline,
101          * so make the hash keyed on the m+addr pair.
102          */
103         mono_domain_lock (domain);
104         if (!domain_jit_info (domain)->static_rgctx_trampoline_hash)
105                 domain_jit_info (domain)->static_rgctx_trampoline_hash = g_hash_table_new (rgctx_tramp_info_hash, rgctx_tramp_info_equal);
106         tmp_info.m = m;
107         tmp_info.addr = addr;
108         res = g_hash_table_lookup (domain_jit_info (domain)->static_rgctx_trampoline_hash,
109                                                            &tmp_info);
110         mono_domain_unlock (domain);
111         if (res)
112                 return res;
113
114         if (mono_aot_only)
115                 res = mono_aot_get_static_rgctx_trampoline (ctx, addr);
116         else
117                 res = mono_arch_get_static_rgctx_trampoline (m, (MonoMethodRuntimeGenericContext *)ctx, addr);
118
119         mono_domain_lock (domain);
120         /* Duplicates inserted while we didn't hold the lock are OK */
121         info = (RgctxTrampInfo *)mono_domain_alloc (domain, sizeof (RgctxTrampInfo));
122         info->m = m;
123         info->addr = addr;
124         g_hash_table_insert (domain_jit_info (domain)->static_rgctx_trampoline_hash, info, res);
125         mono_domain_unlock (domain);
126
127         static_rgctx_trampolines ++;
128
129         return res;
130 }
131 #else
132 gpointer
133 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
134 {
135        /* 
136         * This shouldn't happen as all arches which support generic sharing support
137         * static rgctx trampolines as well.
138         */
139        g_assert_not_reached ();
140 }
141 #endif
142
143 #if 0
144 #define DEBUG_IMT(stmt) do { stmt; } while (0)
145 #else
146 #define DEBUG_IMT(stmt) do { } while (0)
147 #endif
148
149 /*
150  * mini_resolve_imt_method:
151  *
152  *   Resolve the actual method called when making an IMT call through VTABLE_SLOT with IMT_METHOD as the interface method.
153  *
154  * Either IMPL_METHOD or OUT_AOT_ADDR will be set on return.
155  */
156 gpointer*
157 mini_resolve_imt_method (MonoVTable *vt, gpointer *vtable_slot, MonoMethod *imt_method, MonoMethod **impl_method, gpointer *out_aot_addr, gboolean *out_need_rgctx_tramp, MonoMethod **variant_iface)
158 {
159         MonoMethod *impl = NULL, *generic_virtual = NULL;
160         gboolean lookup_aot, variance_used = FALSE, need_rgctx_tramp = FALSE;
161         gpointer addr;
162         guint8 *aot_addr = NULL;
163         int displacement = vtable_slot - ((gpointer*)vt);
164         int interface_offset;
165         int imt_slot = MONO_IMT_SIZE + displacement;
166
167         g_assert (imt_slot < MONO_IMT_SIZE);
168
169         /* This has to be variance aware since imt_method can be from an interface that vt->klass doesn't directly implement */
170         interface_offset = mono_class_interface_offset_with_variance (vt->klass, imt_method->klass, &variance_used);
171         if (interface_offset < 0)
172                 g_error ("%s doesn't implement interface %s\n", mono_type_get_name_full (&vt->klass->byval_arg, MONO_TYPE_NAME_FORMAT_IL), mono_type_get_name_full (&imt_method->klass->byval_arg, MONO_TYPE_NAME_FORMAT_IL));
173
174         *variant_iface = NULL;
175         if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
176                 /* Generic virtual method */
177                 generic_virtual = imt_method;
178                 need_rgctx_tramp = TRUE;
179         } else if (variance_used && mono_class_has_variant_generic_params (imt_method->klass)) {
180                 *variant_iface = imt_method;
181         }
182
183         addr = NULL;
184         /* We can only use the AOT compiled code if we don't require further processing */
185         lookup_aot = !generic_virtual & !variant_iface;
186
187         if (!mono_llvm_only)
188                 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
189
190         if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
191                 MonoError error;
192                 MonoGenericContext context = { NULL, NULL };
193
194                 /*
195                  * Generic virtual method, imt_method contains the inflated interface
196                  * method, need to get the inflated impl method.
197                  */
198                 /* imt_method->slot might not be set */
199                 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_declaring_generic_method (imt_method)->slot);
200
201                 if (impl->klass->generic_class)
202                         context.class_inst = impl->klass->generic_class->context.class_inst;
203                 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
204                 impl = mono_class_inflate_generic_method_checked (impl, &context, &error);
205                 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
206         } else {
207                 MonoError error;
208
209                 /* Avoid loading metadata or creating a generic vtable if possible */
210                 if (lookup_aot && !vt->klass->valuetype) {
211                         aot_addr = (guint8 *)mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, interface_offset + mono_method_get_vtable_slot (imt_method), &error);
212                         mono_error_raise_exception (&error); // FIXME: Don't raise here
213                 } else {
214                         aot_addr = NULL;
215                 }
216                 if (aot_addr)
217                         impl = NULL;
218                 else
219                         impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_vtable_slot (imt_method));
220         }
221
222         if (impl && mono_method_needs_static_rgctx_invoke (impl, FALSE))
223                 need_rgctx_tramp = TRUE;
224         if (impl && impl->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
225                 WrapperInfo *info = mono_marshal_get_wrapper_info (impl);
226
227                 if (info && info->subtype == WRAPPER_SUBTYPE_GENERIC_ARRAY_HELPER)
228                         need_rgctx_tramp = TRUE;
229         }
230         *impl_method = impl;
231         *out_need_rgctx_tramp = need_rgctx_tramp;
232         *out_aot_addr = aot_addr;
233
234         DEBUG_IMT (printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
235                                            method->klass->name_space, method->klass->name, method->name,
236                                            imt_method->klass->name_space, imt_method->klass->name, imt_method->name));
237
238         if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
239                 int slot = mono_method_get_vtable_index (imt_method);
240                 int vtable_offset;
241
242                 g_assert (slot != -1);
243                 vtable_offset = interface_offset + slot;
244                 vtable_slot = & (vt->vtable [vtable_offset]);
245                 DEBUG_IMT (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));
246                 return vtable_slot;
247         } else {
248                 DEBUG_IMT (printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot));
249                 return vtable_slot;
250         }
251 }
252
253 /*
254  * This is a super-ugly hack to fix bug #616463.
255  *
256  * The problem is that we don't always set is_generic for generic
257  * method definitions.  See the comment at the end of
258  * mono_class_inflate_generic_method_full_checked() in class.c.
259  */
260 static gboolean
261 is_generic_method_definition (MonoMethod *m)
262 {
263         MonoGenericContext *context;
264         if (m->is_generic)
265                 return TRUE;
266         if (!m->is_inflated)
267                 return FALSE;
268
269         context = mono_method_get_context (m);
270         if (!context->method_inst)
271                 return FALSE;
272         if (context->method_inst == mono_method_get_generic_container (((MonoMethodInflated*)m)->declaring)->context.method_inst)
273                 return TRUE;
274         return FALSE;
275 }
276
277 gboolean
278 mini_jit_info_is_gsharedvt (MonoJitInfo *ji)
279 {
280         if (ji && ji->has_generic_jit_info && (mono_jit_info_get_generic_sharing_context (ji)->is_gsharedvt))
281                 return TRUE;
282         else
283                 return FALSE;
284 }
285
286 /**
287  * mini_add_method_trampoline:
288  * @m: 
289  * @compiled_method:
290  * @add_static_rgctx_tramp: adds a static rgctx trampoline
291  * @add_unbox_tramp: adds an unboxing trampoline
292  *
293  * Add static rgctx/gsharedvt_in/unbox trampolines to
294  * M/COMPILED_METHOD if needed.
295  *
296  * Returns the trampoline address, or COMPILED_METHOD if no trampoline
297  * is needed.
298  */
299 gpointer
300 mini_add_method_trampoline (MonoMethod *m, gpointer compiled_method, gboolean add_static_rgctx_tramp, gboolean add_unbox_tramp)
301 {
302         gpointer addr = compiled_method;
303         gboolean callee_gsharedvt, callee_array_helper;
304         MonoMethod *jmethod = NULL;
305         MonoJitInfo *ji;
306
307         // FIXME: This loads information from AOT (perf problem)
308         ji = mini_jit_info_table_find (mono_domain_get (), (char *)mono_get_addr_from_ftnptr (compiled_method), NULL);
309         callee_gsharedvt = mini_jit_info_is_gsharedvt (ji);
310
311         callee_array_helper = FALSE;
312         if (m->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
313                 WrapperInfo *info = mono_marshal_get_wrapper_info (m);
314
315                 /*
316                  * generic array helpers.
317                  * Have to replace the wrappers with the original generic instances.
318                  */
319                 if (info && info->subtype == WRAPPER_SUBTYPE_GENERIC_ARRAY_HELPER) {
320                         callee_array_helper = TRUE;
321                         m = info->d.generic_array_helper.method;
322                 }
323         } else if (m->wrapper_type == MONO_WRAPPER_UNKNOWN) {
324                 WrapperInfo *info = mono_marshal_get_wrapper_info (m);
325
326                 /* Same for synchronized inner wrappers */
327                 if (info && info->subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER) {
328                         m = info->d.synchronized_inner.method;
329                 }
330         }
331
332         if (callee_gsharedvt)
333                 g_assert (m->is_inflated);
334
335         addr = compiled_method;
336
337         if (add_unbox_tramp) {
338                 /*
339                  * The unbox trampolines call the method directly, so need to add
340                  * an rgctx tramp before them.
341                  */
342                 if (mono_aot_only) {
343                         addr = mono_aot_get_unbox_trampoline (m);
344                 } else {
345                         unbox_trampolines ++;
346                         addr = mono_arch_get_unbox_trampoline (m, addr);
347                 }
348         }
349
350         if (ji && !ji->is_trampoline)
351                 jmethod = jinfo_get_method (ji);
352         if (callee_gsharedvt && mini_is_gsharedvt_variable_signature (mono_method_signature (jmethod))) {
353                 MonoMethodSignature *sig, *gsig;
354
355                 /* Here m is a generic instance, while ji->method is the gsharedvt method implementing it */
356
357                 /* Call from normal/gshared code to gsharedvt code with variable signature */
358                 sig = mono_method_signature (m);
359                 gsig = mono_method_signature (jmethod);
360
361                 addr = mini_get_gsharedvt_wrapper (TRUE, addr, sig, gsig, -1, FALSE);
362
363                 if (mono_llvm_only)
364                         g_assert_not_reached ();
365                 //printf ("IN: %s\n", mono_method_full_name (m, TRUE));
366         }
367
368         if (callee_array_helper) {
369                 add_static_rgctx_tramp = FALSE;
370                 /* FIXME: ji->from_aot is not set for llvm methods */
371                 if (ji && (ji->from_aot || mono_aot_only)) {
372                         /* In AOT mode, compiled_method points to one of the InternalArray methods in Array. */
373                         if (!mono_llvm_only && mono_method_needs_static_rgctx_invoke (jinfo_get_method (ji), TRUE))
374                                 add_static_rgctx_tramp = TRUE;
375                 }
376         }
377
378         if (mono_llvm_only)
379                 add_static_rgctx_tramp = FALSE;
380
381         if (add_static_rgctx_tramp)
382                 addr = mono_create_static_rgctx_trampoline (m, addr);
383
384         return addr;
385 }
386
387 /*
388  * mini_create_llvmonly_ftndesc:
389  *
390  *   Create a function descriptor of the form <addr, arg>, which
391  * represents a callee ADDR with ARG as the last argument.
392  * This is used for:
393  * - generic sharing (ARG is the rgctx)
394  * - gsharedvt signature wrappers (ARG is a function descriptor)
395  */
396 MonoFtnDesc*
397 mini_create_llvmonly_ftndesc (MonoDomain *domain, gpointer addr, gpointer arg)
398 {
399         MonoFtnDesc *ftndesc = (MonoFtnDesc*)mono_domain_alloc0 (mono_domain_get (), 2 * sizeof (gpointer));
400         ftndesc->addr = addr;
401         ftndesc->arg = arg;
402
403         return ftndesc;
404 }
405
406 /**
407  * mini_add_method_wrappers_llvmonly:
408  *
409  *   Add unbox/gsharedvt wrappers around COMPILED_METHOD if needed. Return the wrapper address or COMPILED_METHOD
410  * if no wrapper is needed. Set OUT_ARG to the rgctx/extra argument needed to be passed to the returned method.
411  */
412 gpointer
413 mini_add_method_wrappers_llvmonly (MonoMethod *m, gpointer compiled_method, gboolean caller_gsharedvt, gboolean add_unbox_tramp, gpointer *out_arg)
414 {
415         gpointer addr;
416         gboolean callee_gsharedvt, callee_array_helper;
417         MonoMethod *jmethod = NULL;
418         MonoJitInfo *ji;
419
420         // FIXME: This loads information from AOT (perf problem)
421         ji = mini_jit_info_table_find (mono_domain_get (), (char *)mono_get_addr_from_ftnptr (compiled_method), NULL);
422         callee_gsharedvt = mini_jit_info_is_gsharedvt (ji);
423
424         callee_array_helper = FALSE;
425         if (m->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
426                 WrapperInfo *info = mono_marshal_get_wrapper_info (m);
427
428                 /*
429                  * generic array helpers.
430                  * Have to replace the wrappers with the original generic instances.
431                  */
432                 if (info && info->subtype == WRAPPER_SUBTYPE_GENERIC_ARRAY_HELPER) {
433                         callee_array_helper = TRUE;
434                         m = info->d.generic_array_helper.method;
435                 }
436         } else if (m->wrapper_type == MONO_WRAPPER_UNKNOWN) {
437                 WrapperInfo *info = mono_marshal_get_wrapper_info (m);
438
439                 /* Same for synchronized inner wrappers */
440                 if (info && info->subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER) {
441                         m = info->d.synchronized_inner.method;
442                 }
443         }
444
445         if (callee_gsharedvt)
446                 g_assert (m->is_inflated);
447
448         addr = compiled_method;
449
450         if (add_unbox_tramp) {
451                 /* 
452                  * The unbox trampolines call the method directly, so need to add
453                  * an rgctx tramp before them.
454                  */
455                 if (mono_aot_only) {
456                         addr = mono_aot_get_unbox_trampoline (m);
457                 } else {
458                         unbox_trampolines ++;
459                         addr = mono_arch_get_unbox_trampoline (m, addr);
460                 }
461         }
462
463         g_assert (mono_llvm_only);
464         g_assert (out_arg);
465
466         if (ji && !ji->is_trampoline)
467                 jmethod = jinfo_get_method (ji);
468
469         if (callee_gsharedvt)
470                 callee_gsharedvt = mini_is_gsharedvt_variable_signature (mono_method_signature (jmethod));
471
472         if (!caller_gsharedvt && callee_gsharedvt) {
473                 MonoMethodSignature *sig, *gsig;
474                 gpointer wrapper_addr;
475
476                 /* Here m is a generic instance, while ji->method is the gsharedvt method implementing it */
477
478                 /* Call from normal/gshared code to gsharedvt code with variable signature */
479                 sig = mono_method_signature (m);
480                 gsig = mono_method_signature (jmethod);
481
482                 wrapper_addr = mini_get_gsharedvt_wrapper (TRUE, addr, sig, gsig, -1, FALSE);
483
484                 /*
485                  * This is a gsharedvt in wrapper, it gets passed a ftndesc for the gsharedvt method as an argument.
486                  */
487                 *out_arg = mini_create_llvmonly_ftndesc (mono_domain_get (), addr, mini_method_get_rgctx (m));
488                 addr = wrapper_addr;
489                 //printf ("IN: %s\n", mono_method_full_name (m, TRUE));
490         }
491
492         if (!(*out_arg) && mono_method_needs_static_rgctx_invoke (m, FALSE))
493                 *out_arg = mini_method_get_rgctx (m);
494
495         if (caller_gsharedvt && !callee_gsharedvt) {
496                 /*
497                  * The callee uses the gsharedvt calling convention, have to add an out wrapper.
498                  */
499                 gpointer out_wrapper = mini_get_gsharedvt_wrapper (FALSE, NULL, mono_method_signature (m), NULL, -1, FALSE);
500                 MonoFtnDesc *out_wrapper_arg = mini_create_llvmonly_ftndesc (mono_domain_get (), addr, *out_arg);
501
502                 addr = out_wrapper;
503                 *out_arg = out_wrapper_arg;
504         }
505
506         return addr;
507 }
508
509 /**
510  * common_call_trampoline:
511  *
512  *   The code to handle normal, virtual, and interface method calls and jumps, both
513  * from JITted and LLVM compiled code.
514  */
515 static gpointer
516 common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, MonoVTable *vt, gpointer *vtable_slot, MonoError *error)
517 {
518         gpointer addr, compiled_method;
519         gboolean generic_shared = FALSE;
520         gboolean need_unbox_tramp = FALSE;
521         gboolean need_rgctx_tramp = FALSE;
522         MonoMethod *declaring = NULL;
523         MonoMethod *generic_virtual = NULL, *variant_iface = NULL;
524         int context_used;
525         gboolean imt_call, virtual_;
526         gpointer *orig_vtable_slot, *vtable_slot_to_patch = NULL;
527         MonoJitInfo *ji = NULL;
528
529         mono_error_init (error);
530
531         virtual_ = vt && (gpointer)vtable_slot > (gpointer)vt;
532         imt_call = vt && (gpointer)vtable_slot < (gpointer)vt;
533
534         /*
535          * rgctx trampolines are needed when the call is indirect so the caller can't pass
536          * the rgctx argument needed by the callee.
537          */
538         if (virtual_ && m)
539                 need_rgctx_tramp = mono_method_needs_static_rgctx_invoke (m, FALSE);
540
541         orig_vtable_slot = vtable_slot;
542         vtable_slot_to_patch = vtable_slot;
543
544         /* IMT call */
545         if (imt_call) {
546                 MonoMethod *imt_method = NULL, *impl_method = NULL;
547                 MonoObject *this_arg;
548
549                 g_assert (vtable_slot);
550
551                 imt_method = mono_arch_find_imt_method (regs, code);
552                 this_arg = (MonoObject *)mono_arch_get_this_arg_from_call (regs, code);
553
554                 if (mono_object_is_transparent_proxy (this_arg)) {
555                         /* Use the slow path for now */
556                     m = mono_object_get_virtual_method (this_arg, imt_method);
557                         vtable_slot_to_patch = NULL;
558                 } else {
559                         if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
560                                 /* Generic virtual method */
561                                 generic_virtual = imt_method;
562                                 need_rgctx_tramp = TRUE;
563                         }
564
565                         vtable_slot = mini_resolve_imt_method (vt, vtable_slot, imt_method, &impl_method, &addr, &need_rgctx_tramp, &variant_iface);
566                         /* This is the vcall slot which gets called through the IMT thunk */
567                         vtable_slot_to_patch = vtable_slot;
568
569                         if (addr) {
570                                 /*
571                                  * We found AOT compiled code for the method, skip the rest.
572                                  */
573                                 if (mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
574                                         *vtable_slot = addr;
575
576                                 return mono_create_ftnptr (mono_domain_get (), addr);
577                         }
578
579                         m = impl_method;
580                 }
581         }
582
583         /*
584          * The virtual check is needed because is_generic_method_definition (m) could
585          * return TRUE for methods used in IMT calls too.
586          */
587         if (virtual_ && is_generic_method_definition (m)) {
588                 MonoError error;
589                 MonoGenericContext context = { NULL, NULL };
590                 MonoMethod *declaring;
591
592                 if (m->is_inflated)
593                         declaring = mono_method_get_declaring_generic_method (m);
594                 else
595                         declaring = m;
596
597                 if (m->klass->generic_class)
598                         context.class_inst = m->klass->generic_class->context.class_inst;
599                 else
600                         g_assert (!m->klass->generic_container);
601
602                 generic_virtual = mono_arch_find_imt_method (regs, code);
603                 g_assert (generic_virtual);
604                 g_assert (generic_virtual->is_inflated);
605                 context.method_inst = ((MonoMethodInflated*)generic_virtual)->context.method_inst;
606
607                 m = mono_class_inflate_generic_method_checked (declaring, &context, &error);
608                 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
609                 /* FIXME: only do this if the method is sharable */
610                 need_rgctx_tramp = TRUE;
611         } else if ((context_used = mono_method_check_context_used (m))) {
612                 MonoClass *klass = NULL;
613                 MonoMethod *actual_method = NULL;
614                 MonoVTable *vt = NULL;
615                 MonoGenericInst *method_inst = NULL;
616
617                 vtable_slot = NULL;
618                 generic_shared = TRUE;
619
620                 g_assert (code);
621
622                 /*
623                  * The caller is gshared code, compute the actual method to call from M and this/rgctx.
624                  */
625                 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
626                         MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext*)mono_arch_find_static_call_vtable (regs, code);
627
628                         klass = mrgctx->class_vtable->klass;
629                         method_inst = mrgctx->method_inst;
630                 } else if ((m->flags & METHOD_ATTRIBUTE_STATIC) || m->klass->valuetype) {
631                         MonoVTable *vtable = mono_arch_find_static_call_vtable (regs, code);
632
633                         klass = vtable->klass;
634                 } else {
635                         MonoObject *this_argument = (MonoObject *)mono_arch_get_this_arg_from_call (regs, code);
636
637                         vt = this_argument->vtable;
638                         vtable_slot = orig_vtable_slot;
639
640                         g_assert (this_argument->vtable->klass->inited);
641
642                         if (!vtable_slot) {
643                                 mono_class_setup_supertypes (this_argument->vtable->klass);
644                                 klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
645                         }
646                 }
647
648                 g_assert (vtable_slot || klass);
649
650                 if (vtable_slot) {
651                         int displacement = vtable_slot - ((gpointer*)vt);
652
653                         g_assert_not_reached ();
654
655                         g_assert (displacement > 0);
656
657                         actual_method = vt->klass->vtable [displacement];
658                 }
659
660                 if (method_inst || m->wrapper_type) {
661                         MonoError error;
662                         MonoGenericContext context = { NULL, NULL };
663
664                         if (m->is_inflated)
665                                 declaring = mono_method_get_declaring_generic_method (m);
666                         else
667                                 declaring = m;
668
669                         if (klass->generic_class)
670                                 context.class_inst = klass->generic_class->context.class_inst;
671                         else if (klass->generic_container)
672                                 context.class_inst = klass->generic_container->context.class_inst;
673                         context.method_inst = method_inst;
674
675                         actual_method = mono_class_inflate_generic_method_checked (declaring, &context, &error);
676                         g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
677                 } else {
678                         actual_method = mono_class_get_method_generic (klass, m);
679                 }
680
681                 g_assert (klass);
682                 g_assert (actual_method);
683                 g_assert (actual_method->klass == klass);
684
685                 if (actual_method->is_inflated)
686                         declaring = mono_method_get_declaring_generic_method (actual_method);
687                 else
688                         declaring = NULL;
689
690                 m = actual_method;
691         }
692
693         if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
694                 m = mono_marshal_get_synchronized_wrapper (m);
695                 need_rgctx_tramp = FALSE;
696         }
697
698         /* Calls made through delegates on platforms without delegate trampolines */
699         if (!code && mono_method_needs_static_rgctx_invoke (m, FALSE))
700                 need_rgctx_tramp = TRUE;
701
702         addr = compiled_method = mono_jit_compile_method (m, error);
703         if (!addr)
704                 return NULL;
705
706         if (generic_virtual || variant_iface) {
707                 if (vt->klass->valuetype) /*FIXME is this required variant iface?*/
708                         need_unbox_tramp = TRUE;
709         } else if (orig_vtable_slot) {
710                 if (m->klass->valuetype)
711                         need_unbox_tramp = TRUE;
712         }
713
714         addr = mini_add_method_trampoline (m, compiled_method, need_rgctx_tramp, need_unbox_tramp);
715
716         if (generic_virtual || variant_iface) {
717                 MonoMethod *target = generic_virtual ? generic_virtual : variant_iface;
718
719                 vtable_slot = orig_vtable_slot;
720                 g_assert (vtable_slot);
721
722                 mono_method_add_generic_virtual_invocation (mono_domain_get (), 
723                                                                                                         vt, vtable_slot,
724                                                                                                         target, addr);
725
726                 return addr;
727         }
728
729         /* the method was jumped to */
730         if (!code) {
731                 MonoDomain *domain = mono_domain_get ();
732
733                 /* Patch the got entries pointing to this method */
734                 /* 
735                  * We do this here instead of in mono_codegen () to cover the case when m
736                  * was loaded from an aot image.
737                  */
738                 if (domain_jit_info (domain)->jump_target_got_slot_hash) {
739                         GSList *list, *tmp;
740
741                         mono_domain_lock (domain);
742                         list = (GSList *)g_hash_table_lookup (domain_jit_info (domain)->jump_target_got_slot_hash, m);
743                         if (list) {
744                                 for (tmp = list; tmp; tmp = tmp->next) {
745                                         gpointer *got_slot = (gpointer *)tmp->data;
746                                         *got_slot = addr;
747                                 }
748                                 g_hash_table_remove (domain_jit_info (domain)->jump_target_got_slot_hash, m);
749                                 g_slist_free (list);
750                         }
751                         mono_domain_unlock (domain);
752                 }
753
754                 return addr;
755         }
756
757         vtable_slot = orig_vtable_slot;
758
759         if (vtable_slot) {
760                 if (vtable_slot_to_patch && (mono_aot_is_got_entry (code, (guint8*)vtable_slot_to_patch) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot_to_patch))) {
761                         g_assert (*vtable_slot_to_patch);
762                         *vtable_slot_to_patch = mono_get_addr_from_ftnptr (addr);
763                 }
764         } else {
765                 guint8 *plt_entry = mono_aot_get_plt_entry (code);
766                 gboolean no_patch = FALSE;
767                 MonoJitInfo *target_ji;
768
769                 if (plt_entry) {
770                         if (generic_shared) {
771                                 target_ji =
772                                         mini_jit_info_table_find (mono_domain_get (), (char *)mono_get_addr_from_ftnptr (compiled_method), NULL);
773                                 if (!ji)
774                                         ji = mini_jit_info_table_find (mono_domain_get (), (char*)code, NULL);
775
776                                 if (ji && target_ji && generic_shared && ji->has_generic_jit_info && !target_ji->has_generic_jit_info) {
777                                         no_patch = TRUE;
778                                 }
779                         }
780                         if (!no_patch)
781                                 mono_aot_patch_plt_entry (code, plt_entry, NULL, regs, (guint8 *)addr);
782                 } else {
783                         if (generic_shared) {
784                                 if (m->wrapper_type != MONO_WRAPPER_NONE)
785                                         m = mono_marshal_method_from_wrapper (m);
786                                 //g_assert (mono_method_is_generic_sharable (m, FALSE));
787                         }
788
789                         /* Patch calling code */
790                         target_ji =
791                                 mini_jit_info_table_find (mono_domain_get (), (char *)mono_get_addr_from_ftnptr (compiled_method), NULL);
792                         if (!ji)
793                                 ji = mini_jit_info_table_find (mono_domain_get (), (char*)code, NULL);
794
795                         if (ji && target_ji && generic_shared && ji->has_generic_jit_info && !target_ji->has_generic_jit_info) {
796                                 /* 
797                                  * Can't patch the call as the caller is gshared, but the callee is not. Happens when
798                                  * generic sharing fails.
799                                  * FIXME: Performance problem.
800                                  */
801                                 no_patch = TRUE;
802                         }
803
804                         if (!no_patch && mono_method_same_domain (ji, target_ji))
805                                 mono_arch_patch_callsite ((guint8 *)ji->code_start, code, (guint8 *)addr);
806                 }
807         }
808
809         return addr;
810 }
811
812 /**
813  * mono_magic_trampoline:
814  *
815  * This trampoline handles normal calls from JITted code.
816  */
817 gpointer
818 mono_magic_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp)
819 {
820         gpointer res;
821
822         MONO_ENTER_GC_UNSAFE_UNBALANCED;
823
824         MonoError error;
825
826         trampoline_calls ++;
827
828         res = common_call_trampoline (regs, code, (MonoMethod *)arg, NULL, NULL, &error);
829         mono_error_set_pending_exception (&error);
830
831         mono_interruption_checkpoint_from_trampoline ();
832
833         MONO_EXIT_GC_UNSAFE_UNBALANCED;
834
835         return res;
836 }
837
838 /**
839  * mono_vcall_trampoline:
840  *
841  * This trampoline handles virtual calls.
842  */
843 static gpointer
844 mono_vcall_trampoline (mgreg_t *regs, guint8 *code, int slot, guint8 *tramp)
845 {
846         MONO_REQ_GC_UNSAFE_MODE;
847
848         MonoObject *this_arg;
849         MonoVTable *vt;
850         gpointer *vtable_slot;
851         MonoMethod *m;
852         MonoError error;
853         gpointer addr, res;
854
855         trampoline_calls ++;
856
857         /*
858          * We need to obtain the following pieces of information:
859          * - the method which needs to be compiled.
860          * - the vtable slot.
861          * We use one vtable trampoline per vtable slot index, so we need only the vtable,
862          * the other two can be computed from the vtable + the slot index.
863          */
864
865         /*
866          * Obtain the vtable from the 'this' arg.
867          */
868         this_arg = (MonoObject *)mono_arch_get_this_arg_from_call (regs, code);
869         g_assert (this_arg);
870
871         vt = this_arg->vtable;
872
873         if (slot >= 0) {
874                 /* Normal virtual call */
875                 vtable_slot = &(vt->vtable [slot]);
876
877                 /* Avoid loading metadata or creating a generic vtable if possible */
878                 addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, slot, &error);
879                 mono_error_raise_exception (&error); /* FIXME don't raise here */
880                 if (addr && !vt->klass->valuetype) {
881                         if (mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
882                                 *vtable_slot = addr;
883
884                         return mono_create_ftnptr (mono_domain_get (), addr);
885                 }
886
887                 /*
888                  * Bug #616463 (see
889                  * is_generic_method_definition() above) also
890                  * goes away if we do a
891                  * mono_class_setup_vtable (vt->klass) here,
892                  * because we then inflate the method
893                  * correctly, put it in the cache, and the
894                  * "wrong" inflation invocation still looks up
895                  * the correctly inflated method.
896                  *
897                  * The hack above seems more stable and
898                  * trustworthy.
899                  */
900                 m = mono_class_get_vtable_entry (vt->klass, slot);
901         } else {
902                 /* IMT call */
903                 vtable_slot = &(((gpointer*)vt) [slot]);
904
905                 m = NULL;
906         }
907
908         res = common_call_trampoline (regs, code, m, vt, vtable_slot, &error);
909         if (!mono_error_ok (&error)) {
910                 mono_error_set_pending_exception (&error);
911                 return NULL;
912         }
913         return res;
914 }
915
916 #ifndef DISABLE_REMOTING
917 gpointer
918 mono_generic_virtual_remoting_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
919 {
920         MONO_REQ_GC_UNSAFE_MODE;
921
922         MonoError error;
923         MonoGenericContext context = { NULL, NULL };
924         MonoMethod *imt_method, *declaring;
925         gpointer addr;
926
927         trampoline_calls ++;
928
929         g_assert (m->is_generic);
930
931         if (m->is_inflated)
932                 declaring = mono_method_get_declaring_generic_method (m);
933         else
934                 declaring = m;
935
936         if (m->klass->generic_class)
937                 context.class_inst = m->klass->generic_class->context.class_inst;
938         else
939                 g_assert (!m->klass->generic_container);
940
941         imt_method = mono_arch_find_imt_method (regs, code);
942         if (imt_method->is_inflated)
943                 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
944         m = mono_class_inflate_generic_method_checked (declaring, &context, &error);
945         g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */;
946         m = mono_marshal_get_remoting_invoke_with_check (m);
947
948         addr = mono_jit_compile_method (m, &error);
949         if (!mono_error_ok (&error)) {
950                 mono_error_set_pending_exception (&error);
951                 return NULL;
952         }
953         g_assert (addr);
954
955         return addr;
956 }
957 #endif
958
959 /**
960  * mono_aot_trampoline:
961  *
962  * This trampoline handles calls made from AOT code. We try to bypass the 
963  * normal JIT compilation logic to avoid loading the metadata for the method.
964  */
965 #ifdef MONO_ARCH_AOT_SUPPORTED
966 gpointer
967 mono_aot_trampoline (mgreg_t *regs, guint8 *code, guint8 *token_info, 
968                                          guint8* tramp)
969 {
970         MONO_REQ_GC_UNSAFE_MODE;
971
972         MonoImage *image;
973         guint32 token;
974         MonoMethod *method = NULL;
975         gpointer addr;
976         guint8 *plt_entry;
977         MonoError error;
978
979         trampoline_calls ++;
980
981         image = (MonoImage *)*(gpointer*)(gpointer)token_info;
982         token_info += sizeof (gpointer);
983         token = *(guint32*)(gpointer)token_info;
984
985         addr = mono_aot_get_method_from_token (mono_domain_get (), image, token, &error);
986         if (!is_ok (&error))
987                 mono_error_cleanup (&error);
988         if (!addr) {
989                 method = mono_get_method_checked (image, token, NULL, NULL, &error);
990                 if (!method)
991                         g_error ("Could not load AOT trampoline due to %s", mono_error_get_message (&error));
992
993                 /* Use the generic code */
994                 return mono_magic_trampoline (regs, code, method, tramp);
995         }
996
997         addr = mono_create_ftnptr (mono_domain_get (), addr);
998
999         /* This is a normal call through a PLT entry */
1000         plt_entry = mono_aot_get_plt_entry (code);
1001         g_assert (plt_entry);
1002
1003         mono_aot_patch_plt_entry (code, plt_entry, NULL, regs, (guint8 *)addr);
1004
1005         return addr;
1006 }
1007
1008 /*
1009  * mono_aot_plt_trampoline:
1010  *
1011  *   This trampoline handles calls made from AOT code through the PLT table.
1012  */
1013 gpointer
1014 mono_aot_plt_trampoline (mgreg_t *regs, guint8 *code, guint8 *aot_module, 
1015                                                  guint8* tramp)
1016 {
1017         MONO_REQ_GC_UNSAFE_MODE;
1018
1019         guint32 plt_info_offset = mono_aot_get_plt_info_offset (regs, code);
1020         gpointer res;
1021         MonoError error;
1022
1023         trampoline_calls ++;
1024
1025         res = mono_aot_plt_resolve (aot_module, plt_info_offset, code, &error);
1026         if (!res) {
1027                 if (!mono_error_ok (&error)) {
1028                         mono_error_set_pending_exception (&error);
1029                         return NULL;
1030                 }
1031                 // FIXME: Error handling (how ?)
1032                 g_assert (res);
1033         }
1034
1035         return res;
1036 }
1037 #endif
1038
1039 static gpointer
1040 mono_rgctx_lazy_fetch_trampoline (mgreg_t *regs, guint8 *code, gpointer data, guint8 *tramp)
1041 {
1042         MONO_REQ_GC_UNSAFE_MODE;
1043
1044         static gboolean inited = FALSE;
1045         static int num_lookups = 0;
1046         guint32 slot = GPOINTER_TO_UINT (data);
1047         mgreg_t *r = (mgreg_t*)regs;
1048         gpointer arg = (gpointer)(gssize)r [MONO_ARCH_VTABLE_REG];
1049         guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
1050         gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
1051         MonoError error;
1052         gpointer res;
1053
1054         trampoline_calls ++;
1055
1056         if (!inited) {
1057                 mono_counters_register ("RGCTX unmanaged lookups", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_lookups);
1058                 inited = TRUE;
1059         }
1060
1061         num_lookups++;
1062
1063         if (mrgctx)
1064                 res = mono_method_fill_runtime_generic_context ((MonoMethodRuntimeGenericContext *)arg, index, &error);
1065         else
1066                 res = mono_class_fill_runtime_generic_context ((MonoVTable *)arg, index, &error);
1067         if (!mono_error_ok (&error)) {
1068                 mono_error_set_pending_exception (&error);
1069                 return NULL;
1070         }
1071         return res;
1072 }
1073
1074 /**
1075  * mono_delegate_trampoline:
1076  *
1077  *   This trampoline handles calls made to Delegate:Invoke ().
1078  * This is called once the first time a delegate is invoked, so it must be fast.
1079  */
1080 gpointer
1081 mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *arg, guint8* tramp)
1082 {
1083         MONO_REQ_GC_UNSAFE_MODE;
1084
1085         MonoDomain *domain = mono_domain_get ();
1086         MonoDelegate *delegate;
1087         MonoJitInfo *ji;
1088         MonoMethod *m;
1089         MonoMethod *method = NULL;
1090         MonoError error;
1091         gboolean multicast, callvirt = FALSE, closed_over_null = FALSE;
1092         gboolean need_rgctx_tramp = FALSE;
1093         gboolean need_unbox_tramp = FALSE;
1094         gboolean enable_caching = TRUE;
1095         MonoDelegateTrampInfo *tramp_info = (MonoDelegateTrampInfo*)arg;
1096         MonoMethod *invoke = tramp_info->invoke;
1097         guint8 *impl_this = (guint8 *)tramp_info->impl_this;
1098         guint8 *impl_nothis = (guint8 *)tramp_info->impl_nothis;
1099         MonoError err;
1100         MonoMethodSignature *sig;
1101         gpointer addr, compiled_method;
1102         gboolean is_remote = FALSE;
1103
1104         trampoline_calls ++;
1105
1106         /* Obtain the delegate object according to the calling convention */
1107         delegate = (MonoDelegate *)mono_arch_get_this_arg_from_call (regs, code);
1108         g_assert (mono_class_has_parent (mono_object_class (delegate), mono_defaults.multicastdelegate_class));
1109
1110         if (delegate->method) {
1111                 method = delegate->method;
1112
1113                 /*
1114                  * delegate->method_ptr == NULL means the delegate was initialized by 
1115                  * mini_delegate_ctor, while != NULL means it is initialized by 
1116                  * mono_delegate_ctor_with_method (). In both cases, we need to add wrappers
1117                  * (ctor_with_method () does this, but it doesn't store the wrapper back into
1118                  * delegate->method).
1119                  */
1120 #ifndef DISABLE_REMOTING
1121                 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class) {
1122                         is_remote = TRUE;
1123 #ifndef DISABLE_COM
1124                         if (((MonoTransparentProxy *)delegate->target)->remote_class->proxy_class != mono_class_get_com_object_class () &&
1125                            !mono_class_is_com_object (((MonoTransparentProxy *)delegate->target)->remote_class->proxy_class))
1126 #endif
1127                                 method = mono_marshal_get_remoting_invoke (method);
1128                 }
1129 #endif
1130                 if (!is_remote) {
1131                         sig = tramp_info->sig;
1132                         if (!(sig && method == tramp_info->method)) {
1133                                 mono_error_init (&err);
1134                                 sig = mono_method_signature_checked (method, &err);
1135                                 if (!sig) {
1136                                         mono_error_set_pending_exception (&err);
1137                                         return NULL;
1138                                 }
1139                         }
1140
1141                         if (sig->hasthis && method->klass->valuetype) {
1142                                 gboolean need_unbox = TRUE;
1143
1144                                 if (tramp_info->invoke_sig->param_count > sig->param_count && tramp_info->invoke_sig->params [0]->byref)
1145                                         need_unbox = FALSE;
1146
1147                                 if (need_unbox) {
1148                                         if (mono_aot_only)
1149                                                 need_unbox_tramp = TRUE;
1150                                         else
1151                                                 method = mono_marshal_get_unbox_wrapper (method);
1152                                 }
1153                         }
1154                 }
1155         // If "delegate->method_ptr" is null mono_get_addr_from_ftnptr will fail if
1156         // ftnptrs are being used.  "method" would end up null on archtitectures without
1157         // ftnptrs so we can just skip this.
1158         } else if (delegate->method_ptr) {
1159                 ji = mono_jit_info_table_find (domain, (char *)mono_get_addr_from_ftnptr (delegate->method_ptr));
1160                 if (ji)
1161                         method = jinfo_get_method (ji);
1162         }
1163
1164         if (method) {
1165                 sig = tramp_info->sig;
1166                 if (!(sig && method == tramp_info->method)) {
1167                         mono_error_init (&err);
1168                         sig = mono_method_signature_checked (method, &err);
1169                         if (!sig) {
1170                                 mono_error_set_pending_exception (&err);
1171                                 return NULL;
1172                         }
1173                 }
1174
1175                 callvirt = !delegate->target && sig->hasthis;
1176                 if (callvirt)
1177                         closed_over_null = tramp_info->invoke_sig->param_count == sig->param_count;
1178
1179                 if (callvirt && !closed_over_null) {
1180                         /*
1181                          * The delegate needs to make a virtual call to the target method using its
1182                          * first argument as the receiver. This is hard to support in full-aot, so
1183                          * optimize it in some cases if possible.
1184                          * If the target method is not virtual or is in a sealed class,
1185                          * the vcall will call it directly.
1186                          * If the call doesn't return a valuetype, then the vcall uses the same calling
1187                          * convention as a normal call.
1188                          */
1189                         if (((method->klass->flags & TYPE_ATTRIBUTE_SEALED) || !(method->flags & METHOD_ATTRIBUTE_VIRTUAL)) && !MONO_TYPE_ISSTRUCT (sig->ret)) {
1190                                 callvirt = FALSE;
1191                                 enable_caching = FALSE;
1192                         }
1193                 }
1194
1195                 if (delegate->target && 
1196                         method->flags & METHOD_ATTRIBUTE_VIRTUAL && 
1197                         method->flags & METHOD_ATTRIBUTE_ABSTRACT &&
1198                         method->klass->flags & TYPE_ATTRIBUTE_ABSTRACT) {
1199                         method = mono_object_get_virtual_method (delegate->target, method);
1200                         enable_caching = FALSE;
1201                 }
1202
1203                 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
1204                         method = mono_marshal_get_synchronized_wrapper (method);
1205
1206                 if (method == tramp_info->method)
1207                         need_rgctx_tramp = tramp_info->need_rgctx_tramp;
1208                 else if (mono_method_needs_static_rgctx_invoke (method, FALSE))
1209                         need_rgctx_tramp = TRUE;
1210         }
1211
1212         /* 
1213          * If the called address is a trampoline, replace it with the compiled method so
1214          * further calls don't have to go through the trampoline.
1215          */
1216         if (method && !callvirt) {
1217                 /* Avoid the overhead of looking up an already compiled method if possible */
1218                 if (enable_caching && delegate->method_code && *delegate->method_code) {
1219                         delegate->method_ptr = *delegate->method_code;
1220                 } else {
1221                         compiled_method = addr = mono_jit_compile_method (method, &error);
1222                         if (!mono_error_ok (&error)) {
1223                                 mono_error_set_pending_exception (&error);
1224                                 return NULL;
1225                         }
1226                         addr = mini_add_method_trampoline (method, compiled_method, need_rgctx_tramp, need_unbox_tramp);
1227                         delegate->method_ptr = addr;
1228                         if (enable_caching && delegate->method_code)
1229                                 *delegate->method_code = (guint8 *)delegate->method_ptr;
1230                 }
1231         } else {
1232                 if (need_rgctx_tramp)
1233                         delegate->method_ptr = mono_create_static_rgctx_trampoline (method, delegate->method_ptr);
1234         }
1235
1236         /* Necessary for !code condition to fallback to slow path */
1237         code = NULL;
1238
1239         multicast = ((MonoMulticastDelegate*)delegate)->delegates != NULL;
1240         if (!multicast && !callvirt) {
1241                 if (method && (method->flags & METHOD_ATTRIBUTE_STATIC) && mono_method_signature (method)->param_count == mono_method_signature (invoke)->param_count + 1)
1242                         /* Closed static delegate */
1243                         code = impl_this;
1244                 else
1245                         code = delegate->target ? impl_this : impl_nothis;
1246         }
1247
1248         if (!code) {
1249                 /* The general, unoptimized case */
1250                 m = mono_marshal_get_delegate_invoke (invoke, delegate);
1251                 code = (guint8 *)mono_jit_compile_method (m, &error);
1252                 if (!mono_error_ok (&error)) {
1253                         mono_error_set_pending_exception (&error);
1254                         return NULL;
1255                 }
1256                 code = (guint8 *)mini_add_method_trampoline (m, code, mono_method_needs_static_rgctx_invoke (m, FALSE), FALSE);
1257         }
1258
1259         delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
1260         if (enable_caching && !callvirt && tramp_info->method) {
1261                 tramp_info->method_ptr = delegate->method_ptr;
1262                 tramp_info->invoke_impl = delegate->invoke_impl;
1263         }
1264
1265         return code;
1266 }
1267
1268 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
1269 static gpointer
1270 mono_handler_block_guard_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_info, guint8* tramp)
1271 {
1272         MONO_REQ_GC_UNSAFE_MODE;
1273
1274         MonoContext ctx;
1275         MonoException *exc;
1276         MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
1277         gpointer resume_ip = jit_tls->handler_block_return_address;
1278
1279         memcpy (&ctx, &jit_tls->handler_block_context, sizeof (MonoContext));
1280         MONO_CONTEXT_SET_IP (&ctx, jit_tls->handler_block_return_address);
1281
1282         jit_tls->handler_block_return_address = NULL;
1283         jit_tls->handler_block = NULL;
1284
1285         if (!resume_ip) /*this should not happen, but we should avoid crashing */
1286                 exc = mono_get_exception_execution_engine ("Invalid internal state, resuming abort after handler block but no resume ip found");
1287         else
1288                 exc = mono_thread_resume_interruption ();
1289
1290         if (exc) {
1291                 mono_handle_exception (&ctx, (MonoObject *)exc);
1292                 mono_restore_context (&ctx);
1293         }
1294
1295         return resume_ip;
1296 }
1297
1298 gpointer
1299 mono_create_handler_block_trampoline (void)
1300 {
1301         static gpointer code;
1302         if (code) {
1303                 mono_memory_barrier ();
1304                 return code;
1305         }
1306
1307         g_assert (!mono_aot_only);
1308
1309         mono_trampolines_lock ();
1310
1311         if (!code) {
1312                 MonoTrampInfo *info;
1313                 gpointer tmp;
1314
1315                 tmp = mono_arch_create_handler_block_trampoline (&info, FALSE);
1316                 mono_tramp_info_register (info, NULL);
1317                 mono_memory_barrier ();
1318                 code = tmp;
1319         }
1320         mono_trampolines_unlock ();
1321
1322         return code;
1323 }
1324 #endif
1325
1326 /*
1327  * mono_get_trampoline_func:
1328  *
1329  *   Return the C function which needs to be called by the generic trampoline of type
1330  * TRAMP_TYPE.
1331  */
1332 gconstpointer
1333 mono_get_trampoline_func (MonoTrampolineType tramp_type)
1334 {
1335         switch (tramp_type) {
1336         case MONO_TRAMPOLINE_JIT:
1337         case MONO_TRAMPOLINE_JUMP:
1338                 return mono_magic_trampoline;
1339         case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
1340                 return mono_rgctx_lazy_fetch_trampoline;
1341 #ifdef MONO_ARCH_AOT_SUPPORTED
1342         case MONO_TRAMPOLINE_AOT:
1343                 return mono_aot_trampoline;
1344         case MONO_TRAMPOLINE_AOT_PLT:
1345                 return mono_aot_plt_trampoline;
1346 #endif
1347         case MONO_TRAMPOLINE_DELEGATE:
1348                 return mono_delegate_trampoline;
1349         case MONO_TRAMPOLINE_RESTORE_STACK_PROT:
1350                 return mono_altstack_restore_prot;
1351 #ifndef DISABLE_REMOTING
1352         case MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING:
1353                 return mono_generic_virtual_remoting_trampoline;
1354 #endif
1355         case MONO_TRAMPOLINE_VCALL:
1356                 return mono_vcall_trampoline;
1357 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
1358         case MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD:
1359                 return mono_handler_block_guard_trampoline;
1360 #endif
1361         default:
1362                 g_assert_not_reached ();
1363                 return NULL;
1364         }
1365 }
1366
1367 static guchar*
1368 create_trampoline_code (MonoTrampolineType tramp_type)
1369 {
1370         MonoTrampInfo *info;
1371         guchar *code;
1372
1373         code = mono_arch_create_generic_trampoline (tramp_type, &info, FALSE);
1374         mono_tramp_info_register (info, NULL);
1375
1376         return code;
1377 }
1378
1379 void
1380 mono_trampolines_init (void)
1381 {
1382         mono_os_mutex_init_recursive (&trampolines_mutex);
1383
1384         if (mono_aot_only)
1385                 return;
1386
1387         mono_trampoline_code [MONO_TRAMPOLINE_JIT] = create_trampoline_code (MONO_TRAMPOLINE_JIT);
1388         mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = create_trampoline_code (MONO_TRAMPOLINE_JUMP);
1389         mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
1390 #ifdef MONO_ARCH_AOT_SUPPORTED
1391         mono_trampoline_code [MONO_TRAMPOLINE_AOT] = create_trampoline_code (MONO_TRAMPOLINE_AOT);
1392         mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
1393 #endif
1394         mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
1395         mono_trampoline_code [MONO_TRAMPOLINE_RESTORE_STACK_PROT] = create_trampoline_code (MONO_TRAMPOLINE_RESTORE_STACK_PROT);
1396 #ifndef DISABLE_REMOTING
1397         mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING] = create_trampoline_code (MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING);
1398 #endif
1399         mono_trampoline_code [MONO_TRAMPOLINE_VCALL] = create_trampoline_code (MONO_TRAMPOLINE_VCALL);
1400 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
1401         mono_trampoline_code [MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD] = create_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
1402         mono_create_handler_block_trampoline ();
1403 #endif
1404
1405         mono_counters_register ("Calls to trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &trampoline_calls);
1406         mono_counters_register ("JIT trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &jit_trampolines);
1407         mono_counters_register ("Unbox trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &unbox_trampolines);
1408         mono_counters_register ("Static rgctx trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &static_rgctx_trampolines);
1409 }
1410
1411 void
1412 mono_trampolines_cleanup (void)
1413 {
1414         if (rgctx_lazy_fetch_trampoline_hash)
1415                 g_hash_table_destroy (rgctx_lazy_fetch_trampoline_hash);
1416         if (rgctx_lazy_fetch_trampoline_hash_addr)
1417                 g_hash_table_destroy (rgctx_lazy_fetch_trampoline_hash_addr);
1418
1419         mono_os_mutex_destroy (&trampolines_mutex);
1420 }
1421
1422 guint8 *
1423 mono_get_trampoline_code (MonoTrampolineType tramp_type)
1424 {
1425         g_assert (mono_trampoline_code [tramp_type]);
1426
1427         return mono_trampoline_code [tramp_type];
1428 }
1429
1430 gpointer
1431 mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
1432 {
1433         if (mono_aot_only)
1434                 return mono_aot_create_specific_trampoline (mono_defaults.corlib, arg1, tramp_type, domain, code_len);
1435         else
1436                 return mono_arch_create_specific_trampoline (arg1, tramp_type, domain, code_len);
1437 }
1438
1439 gpointer
1440 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper, MonoError *error)
1441 {
1442         MonoJitInfo *ji;
1443         gpointer code;
1444         guint32 code_size = 0;
1445
1446         mono_error_init (error);
1447
1448         code = mono_jit_find_compiled_method_with_jit_info (domain, method, &ji);
1449         /*
1450          * We cannot recover the correct type of a shared generic
1451          * method from its native code address, so we use the
1452          * trampoline instead.
1453          * For synchronized methods, the trampoline adds the wrapper.
1454          */
1455         if (code && !ji->has_generic_jit_info && !(method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED))
1456                 return code;
1457
1458         if (mono_llvm_only) {
1459                 code = mono_jit_compile_method (method, error);
1460                 if (!mono_error_ok (error))
1461                         return NULL;
1462                 return code;
1463         }
1464
1465         mono_domain_lock (domain);
1466         code = g_hash_table_lookup (domain_jit_info (domain)->jump_trampoline_hash, method);
1467         mono_domain_unlock (domain);
1468         if (code)
1469                 return code;
1470
1471         code = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
1472         g_assert (code_size);
1473
1474         ji = (MonoJitInfo *)mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO);
1475         ji->code_start = code;
1476         ji->code_size = code_size;
1477         ji->d.method = method;
1478
1479         /*
1480          * mono_delegate_ctor needs to find the method metadata from the 
1481          * trampoline address, so we save it here.
1482          */
1483
1484         mono_jit_info_table_add (domain, ji);
1485
1486         mono_domain_lock (domain);
1487         g_hash_table_insert (domain_jit_info (domain)->jump_trampoline_hash, method, ji->code_start);
1488         mono_domain_unlock (domain);
1489
1490         return ji->code_start;
1491 }
1492
1493 static void
1494 method_not_found (void)
1495 {
1496         g_assert_not_reached ();
1497 }
1498
1499 gpointer
1500 mono_create_jit_trampoline (MonoDomain *domain, MonoMethod *method, MonoError *error)
1501 {
1502         gpointer tramp;
1503
1504         mono_error_init (error);
1505
1506         if (mono_aot_only) {
1507                 /* Avoid creating trampolines if possible */
1508                 gpointer code = mono_jit_find_compiled_method (domain, method);
1509                 
1510                 if (code)
1511                         return code;
1512                 if (mono_llvm_only) {
1513                         if (method->wrapper_type == MONO_WRAPPER_PROXY_ISINST || method->wrapper_type == MONO_WRAPPER_LDFLD_REMOTE ||
1514                                 method->wrapper_type == MONO_WRAPPER_STFLD_REMOTE)
1515                                 /* These wrappers are not generated */
1516                                 return method_not_found;
1517                         /* Methods are lazily initialized on first call, so this can't lead recursion */
1518                         code = mono_jit_compile_method (method, error);
1519                         if (!mono_error_ok (error))
1520                                 return NULL;
1521                         return code;
1522                 }
1523         }
1524
1525         mono_domain_lock (domain);
1526         tramp = g_hash_table_lookup (domain_jit_info (domain)->jit_trampoline_hash, method);
1527         mono_domain_unlock (domain);
1528         if (tramp)
1529                 return tramp;
1530
1531         tramp = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JIT, domain, NULL);
1532         
1533         mono_domain_lock (domain);
1534         g_hash_table_insert (domain_jit_info (domain)->jit_trampoline_hash, method, tramp);
1535         mono_domain_unlock (domain);
1536
1537         jit_trampolines++;
1538
1539         return tramp;
1540 }       
1541
1542 gpointer
1543 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
1544 {
1545         gpointer tramp;
1546
1547         MonoDomain *domain = mono_domain_get ();
1548         guint8 *buf, *start;
1549
1550         buf = start = (guint8 *)mono_domain_alloc0 (domain, 2 * sizeof (gpointer));
1551
1552         *(gpointer*)(gpointer)buf = image;
1553         buf += sizeof (gpointer);
1554         *(guint32*)(gpointer)buf = token;
1555
1556         tramp = mono_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
1557
1558         jit_trampolines++;
1559
1560         return tramp;
1561 }       
1562
1563
1564 /*
1565  * mono_create_delegate_trampoline_info:
1566  *
1567  *  Create a trampoline info structure for the KLASS+METHOD pair.
1568  */
1569 MonoDelegateTrampInfo*
1570 mono_create_delegate_trampoline_info (MonoDomain *domain, MonoClass *klass, MonoMethod *method)
1571 {
1572         MonoMethod *invoke;
1573         MonoError error;
1574         MonoDelegateTrampInfo *tramp_info;
1575         MonoClassMethodPair pair, *dpair;
1576         guint32 code_size = 0;
1577
1578         pair.klass = klass;
1579         pair.method = method;
1580         mono_domain_lock (domain);
1581         tramp_info = (MonoDelegateTrampInfo *)g_hash_table_lookup (domain_jit_info (domain)->delegate_trampoline_hash, &pair);
1582         mono_domain_unlock (domain);
1583         if (tramp_info)
1584                 return tramp_info;
1585
1586         invoke = mono_get_delegate_invoke (klass);
1587         g_assert (invoke);
1588
1589         tramp_info = (MonoDelegateTrampInfo *)mono_domain_alloc0 (domain, sizeof (MonoDelegateTrampInfo));
1590         tramp_info->invoke = invoke;
1591         tramp_info->invoke_sig = mono_method_signature (invoke);
1592         tramp_info->impl_this = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
1593         tramp_info->impl_nothis = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
1594         tramp_info->method = method;
1595         if (method) {
1596                 mono_error_init (&error);
1597                 tramp_info->sig = mono_method_signature_checked (method, &error);
1598                 tramp_info->need_rgctx_tramp = mono_method_needs_static_rgctx_invoke (method, FALSE);
1599         }
1600         tramp_info->invoke_impl = mono_create_specific_trampoline (tramp_info, MONO_TRAMPOLINE_DELEGATE, domain, &code_size);
1601         g_assert (code_size);
1602
1603         dpair = (MonoClassMethodPair *)mono_domain_alloc0 (domain, sizeof (MonoClassMethodPair));
1604         memcpy (dpair, &pair, sizeof (MonoClassMethodPair));
1605
1606         /* store trampoline address */
1607         mono_domain_lock (domain);
1608         g_hash_table_insert (domain_jit_info (domain)->delegate_trampoline_hash, dpair, tramp_info);
1609         mono_domain_unlock (domain);
1610
1611         return tramp_info;
1612 }
1613
1614 static void
1615 no_delegate_trampoline (void)
1616 {
1617         g_assert_not_reached ();
1618 }
1619
1620 gpointer
1621 mono_create_delegate_trampoline (MonoDomain *domain, MonoClass *klass)
1622 {
1623         if (mono_llvm_only)
1624                 return no_delegate_trampoline;
1625
1626         return mono_create_delegate_trampoline_info (domain, klass, NULL)->invoke_impl;
1627 }
1628
1629 gpointer
1630 mono_create_delegate_virtual_trampoline (MonoDomain *domain, MonoClass *klass, MonoMethod *method)
1631 {
1632         MonoMethod *invoke = mono_get_delegate_invoke (klass);
1633         g_assert (invoke);
1634
1635         return mono_get_delegate_virtual_invoke_impl (mono_method_signature (invoke), method);
1636 }
1637
1638 gpointer
1639 mono_create_rgctx_lazy_fetch_trampoline (guint32 offset)
1640 {
1641         static gboolean inited = FALSE;
1642         static int num_trampolines = 0;
1643         MonoTrampInfo *info;
1644
1645         gpointer tramp, ptr;
1646
1647         mono_trampolines_lock ();
1648         if (rgctx_lazy_fetch_trampoline_hash)
1649                 tramp = g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset));
1650         else
1651                 tramp = NULL;
1652         mono_trampolines_unlock ();
1653         if (tramp)
1654                 return tramp;
1655
1656         if (mono_aot_only) {
1657                 ptr = mono_aot_get_lazy_fetch_trampoline (offset);
1658         } else {
1659                 tramp = mono_arch_create_rgctx_lazy_fetch_trampoline (offset, &info, FALSE);
1660                 mono_tramp_info_register (info, NULL);
1661                 ptr = mono_create_ftnptr (mono_get_root_domain (), tramp);
1662         }
1663
1664         mono_trampolines_lock ();
1665         if (!rgctx_lazy_fetch_trampoline_hash) {
1666                 rgctx_lazy_fetch_trampoline_hash = g_hash_table_new (NULL, NULL);
1667                 rgctx_lazy_fetch_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
1668         }
1669         g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset), ptr);
1670         g_assert (offset != -1);
1671         g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash_addr, ptr, GUINT_TO_POINTER (offset + 1));
1672         mono_trampolines_unlock ();
1673
1674         if (!inited) {
1675                 mono_counters_register ("RGCTX num lazy fetch trampolines",
1676                                 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_trampolines);
1677                 inited = TRUE;
1678         }
1679         num_trampolines++;
1680
1681         return ptr;
1682 }
1683
1684 guint32
1685 mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr)
1686 {
1687         int offset;
1688
1689         mono_trampolines_lock ();
1690         if (rgctx_lazy_fetch_trampoline_hash_addr) {
1691                 /* We store the real offset + 1 so we can detect when the lookup fails */
1692                 offset = GPOINTER_TO_INT (g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash_addr, addr));
1693                 if (offset)
1694                         offset -= 1;
1695                 else
1696                         offset = -1;
1697         } else {
1698                 offset = -1;
1699         }
1700         mono_trampolines_unlock ();
1701         return offset;
1702 }
1703
1704 static const char*tramp_names [MONO_TRAMPOLINE_NUM] = {
1705         "jit",
1706         "jump",
1707         "rgctx_lazy_fetch",
1708         "aot",
1709         "aot_plt",
1710         "delegate",
1711         "restore_stack_prot",
1712         "generic_virtual_remoting",
1713         "vcall",
1714         "handler_block_guard"
1715 };
1716
1717 /*
1718  * mono_get_generic_trampoline_simple_name:
1719  *
1720  */
1721 const char*
1722 mono_get_generic_trampoline_simple_name (MonoTrampolineType tramp_type)
1723 {
1724         return tramp_names [tramp_type];
1725 }
1726
1727 /*
1728  * mono_get_generic_trampoline_name:
1729  *
1730  *   Returns a pointer to malloc-ed memory.
1731  */
1732 char*
1733 mono_get_generic_trampoline_name (MonoTrampolineType tramp_type)
1734 {
1735         return g_strdup_printf ("generic_trampoline_%s", tramp_names [tramp_type]);
1736 }
1737
1738 /*
1739  * mono_get_rgctx_fetch_trampoline_name:
1740  *
1741  *   Returns a pointer to malloc-ed memory.
1742  */
1743 char*
1744 mono_get_rgctx_fetch_trampoline_name (int slot)
1745 {
1746         gboolean mrgctx;
1747         int index;
1748
1749         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
1750         index = MONO_RGCTX_SLOT_INDEX (slot);
1751
1752         return g_strdup_printf ("rgctx_fetch_trampoline_%s_%d", mrgctx ? "mrgctx" : "rgctx", index);
1753 }
1754
1755 /*
1756  * mini_get_single_step_trampoline:
1757  *
1758  *   Return a trampoline which calls debugger_agent_single_step_from_context ().
1759  */
1760 gpointer
1761 mini_get_single_step_trampoline (void)
1762 {
1763         static gpointer trampoline;
1764
1765         if (!trampoline) {
1766                 gpointer tramp;
1767
1768                 if (mono_aot_only) {
1769                         tramp = mono_aot_get_trampoline ("sdb_single_step_trampoline");
1770                 } else {
1771 #ifdef MONO_ARCH_HAVE_SDB_TRAMPOLINES
1772                         MonoTrampInfo *info;
1773                         tramp = mono_arch_create_sdb_trampoline (TRUE, &info, FALSE);
1774                         mono_tramp_info_register (info, NULL);
1775 #else
1776                         tramp = NULL;
1777                         g_assert_not_reached ();
1778 #endif
1779                 }
1780                 mono_memory_barrier ();
1781                 trampoline = tramp;
1782         }
1783
1784         return trampoline;
1785 }
1786
1787 /*
1788  * mini_get_breakpoint_trampoline:
1789  *
1790  *   Return a trampoline which calls debugger_agent_breakpoint_from_context ().
1791  */
1792 gpointer
1793 mini_get_breakpoint_trampoline (void)
1794 {
1795         static gpointer trampoline;
1796
1797         if (!trampoline) {
1798                 gpointer tramp;
1799
1800                 if (mono_aot_only) {
1801                         tramp = mono_aot_get_trampoline ("sdb_breakpoint_trampoline");
1802                 } else {
1803 #ifdef MONO_ARCH_HAVE_SDB_TRAMPOLINES
1804                         MonoTrampInfo *info;
1805                         tramp = mono_arch_create_sdb_trampoline (FALSE, &info, FALSE);
1806                         mono_tramp_info_register (info, NULL);
1807 #else
1808                         tramp = NULL;
1809                         g_assert_not_reached ();
1810 #endif
1811                 }
1812                 mono_memory_barrier ();
1813                 trampoline = tramp;
1814         }
1815
1816         return trampoline;
1817 }