[runtime] Rename mono_arch_find_jit_info () -> mono_arch_unwind_frame ().
[mono.git] / mono / mini / tramp-x86.c
1 /*
2  * tramp-x86.c: JIT trampoline code for x86
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9
10 #include <config.h>
11 #include <glib.h>
12
13 #include <mono/metadata/abi-details.h>
14 #include <mono/metadata/appdomain.h>
15 #include <mono/metadata/metadata-internals.h>
16 #include <mono/metadata/marshal.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/mono-debug.h>
19 #include <mono/metadata/mono-debug-debugger.h>
20 #include <mono/metadata/monitor.h>
21 #include <mono/metadata/profiler-private.h>
22 #include <mono/metadata/gc-internal.h>
23 #include <mono/arch/x86/x86-codegen.h>
24
25 #include <mono/utils/memcheck.h>
26
27 #include "mini.h"
28 #include "mini-x86.h"
29
30 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
31
32 /*
33  * mono_arch_get_unbox_trampoline:
34  * @m: method pointer
35  * @addr: pointer to native code for @m
36  *
37  * when value type methods are called through the vtable we need to unbox the
38  * this argument. This method returns a pointer to a trampoline which does
39  * unboxing before calling the method
40  */
41 gpointer
42 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
43 {
44         guint8 *code, *start;
45         int this_pos = 4, size = NACL_SIZE(16, 32);
46         MonoDomain *domain = mono_domain_get ();
47
48         start = code = mono_domain_code_reserve (domain, size);
49
50         x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
51         x86_jump_code (code, addr);
52         g_assert ((code - start) < size);
53
54         nacl_domain_code_validate (domain, &start, size, &code);
55         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m);
56
57         return start;
58 }
59
60 gpointer
61 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
62 {
63         guint8 *code, *start;
64         int buf_len;
65
66         MonoDomain *domain = mono_domain_get ();
67
68         buf_len = NACL_SIZE (10, 32);
69
70         start = code = mono_domain_code_reserve (domain, buf_len);
71
72         x86_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
73         x86_jump_code (code, addr);
74         g_assert ((code - start) <= buf_len);
75
76         nacl_domain_code_validate (domain, &start, buf_len, &code);
77         mono_arch_flush_icache (start, code - start);
78         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
79
80         return start;
81 }
82
83 gpointer
84 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
85 {
86         guint8 *code, *start;
87         int buf_len;
88         int this_offset;
89
90         buf_len = 32;
91
92         start = code = mono_domain_code_reserve (domain, buf_len);
93
94         this_offset = mono_x86_get_this_arg_offset (mono_method_signature (m));
95
96         /* Set imt arg */
97         x86_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
98         /* Load this */
99         x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_offset + 4, 4);
100         /* Load vtable address */
101         x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
102         x86_jump_membase (code, X86_EAX, vt_offset);
103
104         g_assert ((code - start) < buf_len);
105
106         nacl_domain_code_validate (domain, &start, buf_len, &code);
107
108         mono_arch_flush_icache (start, code - start);
109         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
110
111         return start;
112 }
113
114 void
115 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
116 {
117 #if defined(__default_codegen__)
118         guint8 *code;
119         guint8 buf [8];
120         gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
121
122         code = buf + 8;
123
124         /* go to the start of the call instruction
125          *
126          * address_byte = (m << 6) | (o << 3) | reg
127          * call opcode: 0xff address_byte displacement
128          * 0xff m=1,o=2 imm8
129          * 0xff m=2,o=2 imm32
130          */
131         code -= 6;
132         orig_code -= 6;
133         if (code [1] == 0xe8) {
134                 if (can_write) {
135                         InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
136
137                         /* Tell valgrind to recompile the patched code */
138                         VALGRIND_DISCARD_TRANSLATIONS (orig_code + 2, 4);
139                 }
140         } else if (code [1] == 0xe9) {
141                 /* A PLT entry: jmp <DISP> */
142                 if (can_write)
143                         InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
144         } else {
145                 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
146                                 code [4], code [5], code [6]);
147                 g_assert_not_reached ();
148         }
149 #elif defined(__native_client__)
150         /* Target must be bundle-aligned */
151         g_assert (((guint32)addr & kNaClAlignmentMask) == 0);
152
153         /* 0xe8 = call <DISP>, 0xe9 = jump <DISP> */
154         if ((orig_code [-5] == 0xe8) || orig_code [-6] == 0xe9) {
155                 int ret;
156                 gint32 offset = (gint32)addr - (gint32)orig_code;
157                 guint8 buf[sizeof(gint32)];
158                 *((gint32*)(buf)) = offset;
159                 ret = nacl_dyncode_modify (orig_code - sizeof(gint32), buf, sizeof(gint32));
160                 g_assert (ret == 0);
161         } else {
162                 printf ("Invalid trampoline sequence %p: %02x %02x %02x %02x %02x\n", orig_code, orig_code [-5], orig_code [-4], orig_code [-3], orig_code [-2], orig_code[-1]);
163                 g_assert_not_reached ();
164         }
165 #endif
166 }
167
168 void
169 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
170 {
171         guint32 offset;
172
173         /* Patch the jump table entry used by the plt entry */
174
175 #if defined(__native_client_codegen__) || defined(__native_client__)
176         /* for both compiler and runtime      */
177         /* A PLT entry:                       */
178         /*        mov <DISP>(%ebx), %ecx      */
179         /*        and 0xffffffe0, %ecx        */
180         /*        jmp *%ecx                   */
181         g_assert (code [0] == 0x8b);
182         g_assert (code [1] == 0x8b);
183
184         offset = *(guint32*)(code + 2);
185 #elif defined(__default_codegen__)
186         /* A PLT entry: jmp *<DISP>(%ebx) */
187         g_assert (code [0] == 0xff);
188         g_assert (code [1] == 0xa3);
189
190         offset = *(guint32*)(code + 2);
191 #endif  /* __native_client_codegen__ */
192         if (!got)
193                 got = (gpointer*)(gsize) regs [MONO_ARCH_GOT_REG];
194         *(guint8**)((guint8*)got + offset) = addr;
195 }
196
197 static gpointer
198 get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
199 {
200         const int kBufSize = NACL_SIZE (8, 16);
201         guint8 buf [64];
202         guint8 reg = 0;
203         gint32 disp = 0;
204
205         mono_breakpoint_clean_code (NULL, code, kBufSize, buf, kBufSize);
206         code = buf + 8;
207
208         *displacement = 0;
209
210         if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
211                 reg = code [1] & 0x07;
212                 disp = *((gint32*)(code + 2));
213 #if defined(__native_client_codegen__) || defined(__native_client__)
214         } else if ((code[1] == 0x83) && (code[2] == 0xe1) && (code[4] == 0xff) &&
215                            (code[5] == 0xd1) && (code[-5] == 0x8b)) {
216                 disp = *((gint32*)(code - 3));
217                 reg = code[-4] & 0x07;
218         } else if ((code[-2] == 0x8b) && (code[1] == 0x83) && (code[4] == 0xff)) {
219                 reg = code[-1] & 0x07;
220                 disp = (signed char)code[0];
221 #endif
222         } else {
223                 g_assert_not_reached ();
224                 return NULL;
225         }
226
227         *displacement = disp;
228         return (gpointer)regs [reg];
229 }
230
231 static gpointer*
232 get_vcall_slot_addr (guint8* code, mgreg_t *regs)
233 {
234         gpointer vt;
235         int displacement;
236         vt = get_vcall_slot (code, regs, &displacement);
237         if (!vt)
238                 return NULL;
239         return (gpointer*)((char*)vt + displacement);
240 }
241
242 guchar*
243 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
244 {
245         char *tramp_name;
246         guint8 *buf, *code, *tramp;
247         GSList *unwind_ops = NULL;
248         MonoJumpInfo *ji = NULL;
249         int i, offset, frame_size, regarray_offset, lmf_offset, caller_ip_offset, arg_offset;
250
251         unwind_ops = mono_arch_get_cie_program ();
252
253         code = buf = mono_global_codeman_reserve (256);
254
255         /* Note that there is a single argument to the trampoline
256          * and it is stored at: esp + pushed_args * sizeof (gpointer)
257          * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
258          */
259
260         // FIXME: Unwind info
261
262         /* Compute frame offsets relative to the frame pointer %ebp */
263         arg_offset = sizeof (mgreg_t);
264         caller_ip_offset = 2 * sizeof (mgreg_t);
265         offset = 0;
266         offset += sizeof (MonoLMF);
267         lmf_offset = -offset;
268         offset += X86_NREG * sizeof (mgreg_t);
269         regarray_offset = -offset;
270         /* Argument area */
271         offset += 4 * sizeof (mgreg_t);
272         frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
273
274         /* Allocate frame */
275         x86_push_reg (code, X86_EBP);
276         x86_mov_reg_reg (code, X86_EBP, X86_ESP, sizeof (mgreg_t));
277         /* There are three words on the stack, adding + 4 aligns the stack to 16, which is needed on osx */
278         x86_alu_reg_imm (code, X86_SUB, X86_ESP, frame_size + sizeof (mgreg_t));
279
280         /* Save all registers */
281         for (i = X86_EAX; i <= X86_EDI; ++i) {
282                 int reg = i;
283
284                 if (i == X86_EBP) {
285                         /* Save original ebp */
286                         /* EAX is already saved */
287                         x86_mov_reg_membase (code, X86_EAX, X86_EBP, 0, sizeof (mgreg_t));
288                         reg = X86_EAX;
289                 } else if (i == X86_ESP) {
290                         /* Save original esp */
291                         /* EAX is already saved */
292                         x86_mov_reg_reg (code, X86_EAX, X86_EBP, sizeof (mgreg_t));
293                         /* Saved ebp + trampoline arg + return addr */
294                         x86_alu_reg_imm (code, X86_ADD, X86_EAX, 3 * sizeof (mgreg_t));
295                         reg = X86_EAX;
296                 }
297                 x86_mov_membase_reg (code, X86_EBP, regarray_offset + (i * sizeof (mgreg_t)), reg, sizeof (mgreg_t));
298         }
299
300         /* Setup LMF */
301         /* eip */
302         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
303                 x86_mov_membase_imm (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), 0, sizeof (mgreg_t));
304         } else {
305                 x86_mov_reg_membase (code, X86_EAX, X86_EBP, caller_ip_offset, sizeof (mgreg_t));
306                 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), X86_EAX, sizeof (mgreg_t));
307         }
308         /* method */
309         if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP)) {
310                 x86_mov_reg_membase (code, X86_EAX, X86_EBP, arg_offset, sizeof (mgreg_t));
311                 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), X86_EAX, sizeof (mgreg_t));
312         } else {
313                 x86_mov_membase_imm (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, sizeof (mgreg_t));
314         }
315         /* esp */
316         x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_ESP * sizeof (mgreg_t)), sizeof (mgreg_t));
317         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esp), X86_EAX, sizeof (mgreg_t));
318         /* callee save registers */
319         x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EBX * sizeof (mgreg_t)), sizeof (mgreg_t));
320         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), X86_EAX, sizeof (mgreg_t));
321         x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EDI * sizeof (mgreg_t)), sizeof (mgreg_t));
322         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), X86_EAX, sizeof (mgreg_t));
323         x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_ESI * sizeof (mgreg_t)), sizeof (mgreg_t));
324         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), X86_EAX, sizeof (mgreg_t));
325         x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EBP * sizeof (mgreg_t)), sizeof (mgreg_t));
326         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp), X86_EAX, sizeof (mgreg_t));
327
328         /* Push LMF */
329         /* get the address of lmf for the current thread */
330         if (aot) {
331                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
332                 x86_call_reg (code, X86_EAX);
333         } else {
334                 x86_call_code (code, mono_get_lmf_addr);
335         }
336         /* lmf->lmf_addr = lmf_addr (%eax) */
337         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), X86_EAX, sizeof (mgreg_t));
338         /* lmf->previous_lmf = *(lmf_addr) */
339         x86_mov_reg_membase (code, X86_ECX, X86_EAX, 0, sizeof (mgreg_t));
340         /* Signal to mono_arch_unwind_frame () that this is a trampoline frame */
341         x86_alu_reg_imm (code, X86_ADD, X86_ECX, 1);
342         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), X86_ECX, sizeof (mgreg_t));
343         /* *lmf_addr = lmf */
344         x86_lea_membase (code, X86_ECX, X86_EBP, lmf_offset);
345         x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
346
347         /* Call trampoline function */
348         /* Arg 1 - registers */
349         x86_lea_membase (code, X86_EAX, X86_EBP, regarray_offset);
350         x86_mov_membase_reg (code, X86_ESP, (0 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
351         /* Arg2 - calling code */
352         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
353                 x86_mov_membase_imm (code, X86_ESP, (1 * sizeof (mgreg_t)), 0, sizeof (mgreg_t));
354         } else {
355                 x86_mov_reg_membase (code, X86_EAX, X86_EBP, caller_ip_offset, sizeof (mgreg_t));
356                 x86_mov_membase_reg (code, X86_ESP, (1 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
357         }
358         /* Arg3 - trampoline argument */
359         x86_mov_reg_membase (code, X86_EAX, X86_EBP, arg_offset, sizeof (mgreg_t));
360         x86_mov_membase_reg (code, X86_ESP, (2 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
361         /* Arg4 - trampoline address */
362         // FIXME:
363         x86_mov_membase_imm (code, X86_ESP, (3 * sizeof (mgreg_t)), 0, sizeof (mgreg_t));
364
365 #ifdef __APPLE__
366         /* check the stack is aligned after the ret ip is pushed */
367         /*
368         x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
369         x86_alu_reg_imm (code, X86_AND, X86_EDX, 15);
370         x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
371         x86_branch_disp (code, X86_CC_Z, 3, FALSE);
372         x86_breakpoint (code);
373         */
374 #endif
375
376         if (aot) {
377                 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
378                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
379                 x86_call_reg (code, X86_EAX);
380         } else {
381                 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
382                 x86_call_code (code, tramp);
383         }
384
385         /*
386          * Overwrite the trampoline argument with the address we need to jump to,
387          * to free %eax.
388          */
389         x86_mov_membase_reg (code, X86_EBP, arg_offset, X86_EAX, 4);
390
391         /* Check for interruptions */
392         if (aot) {
393                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
394                 x86_call_reg (code, X86_EAX);
395         } else {
396                 x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint);
397         }
398
399         /* Restore LMF */
400         x86_mov_reg_membase (code, X86_EAX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sizeof (mgreg_t));
401         x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof (mgreg_t));
402         x86_alu_reg_imm (code, X86_SUB, X86_ECX, 1);
403         x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
404
405         /* Restore registers */
406         for (i = X86_EAX; i <= X86_EDI; ++i) {
407                 if (i == X86_ESP || i == X86_EBP)
408                         continue;
409                 if (i == X86_EAX && !((tramp_type == MONO_TRAMPOLINE_RESTORE_STACK_PROT) || (tramp_type == MONO_TRAMPOLINE_AOT_PLT)))
410                         continue;
411                 x86_mov_reg_membase (code, i, X86_EBP, regarray_offset + (i * 4), 4);
412         }
413
414         /* Restore frame */
415         x86_leave (code);
416
417         if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
418                 /* Load the value returned by the trampoline */
419                 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 0, 4);
420                 /* The trampoline returns normally, pop the trampoline argument */
421                 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
422                 x86_ret (code);
423         } else {
424                 /* The trampoline argument is at the top of the stack, and it contains the address we need to branch to */
425                 if (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD) {
426                         x86_pop_reg (code, X86_EAX);
427                         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 0x8);
428                         x86_jump_reg (code, X86_EAX);
429                 } else {
430                         x86_ret (code);
431                 }
432         }
433
434         nacl_global_codeman_validate (&buf, 256, &code);
435         g_assert ((code - buf) <= 256);
436         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
437
438         tramp_name = mono_get_generic_trampoline_name (tramp_type);
439         *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
440         g_free (tramp_name);
441
442         return buf;
443 }
444
445 #define TRAMPOLINE_SIZE 10
446
447 gpointer
448 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
449 {
450         guint8 *code, *buf, *tramp;
451         
452         tramp = mono_get_trampoline_code (tramp_type);
453
454         code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, NACL_SIZE (4, kNaClAlignment));
455
456         x86_push_imm (buf, arg1);
457         x86_jump_code (buf, tramp);
458         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
459
460         nacl_domain_code_validate (domain, &code, NACL_SIZE (4, kNaClAlignment), &buf);
461
462         mono_arch_flush_icache (code, buf - code);
463         mono_profiler_code_buffer_new (code, buf - code, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type));
464
465         if (code_len)
466                 *code_len = buf - code;
467
468         return code;
469 }
470
471 gpointer
472 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
473 {
474         guint8 *tramp;
475         guint8 *code, *buf;
476         guint8 **rgctx_null_jumps;
477         int tramp_size;
478         int depth, index;
479         int i;
480         gboolean mrgctx;
481         MonoJumpInfo *ji = NULL;
482         GSList *unwind_ops = NULL;
483
484         unwind_ops = mono_arch_get_cie_program ();
485
486         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
487         index = MONO_RGCTX_SLOT_INDEX (slot);
488         if (mrgctx)
489                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
490         for (depth = 0; ; ++depth) {
491                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
492
493                 if (index < size - 1)
494                         break;
495                 index -= size - 1;
496         }
497
498 #if defined(__default_codegen__)
499         tramp_size = (aot ? 64 : 36) + 6 * depth;
500 #elif defined(__native_client_codegen__)
501         tramp_size = (aot ? 64 : 36) + 2 * kNaClAlignment +
502           6 * (depth + kNaClAlignment);
503 #endif
504
505         code = buf = mono_global_codeman_reserve (tramp_size);
506
507         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
508
509         /* load vtable/mrgctx ptr */
510         x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
511         if (!mrgctx) {
512                 /* load rgctx ptr from vtable */
513                 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
514                 /* is the rgctx ptr null? */
515                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
516                 /* if yes, jump to actual trampoline */
517                 rgctx_null_jumps [0] = code;
518                 x86_branch8 (code, X86_CC_Z, -1, 1);
519         }
520
521         for (i = 0; i < depth; ++i) {
522                 /* load ptr to next array */
523                 if (mrgctx && i == 0)
524                         x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
525                 else
526                         x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
527                 /* is the ptr null? */
528                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
529                 /* if yes, jump to actual trampoline */
530                 rgctx_null_jumps [i + 1] = code;
531                 x86_branch8 (code, X86_CC_Z, -1, 1);
532         }
533
534         /* fetch slot */
535         x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
536         /* is the slot null? */
537         x86_test_reg_reg (code, X86_EAX, X86_EAX);
538         /* if yes, jump to actual trampoline */
539         rgctx_null_jumps [depth + 1] = code;
540         x86_branch8 (code, X86_CC_Z, -1, 1);
541         /* otherwise return */
542         x86_ret (code);
543
544         for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
545                 x86_patch (rgctx_null_jumps [i], code);
546
547         g_free (rgctx_null_jumps);
548
549         x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
550
551         if (aot) {
552                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
553                 x86_jump_reg (code, X86_EAX);
554         } else {
555                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
556
557                 /* jump to the actual trampoline */
558                 x86_jump_code (code, tramp);
559         }
560
561         nacl_global_codeman_validate (&buf, tramp_size, &code);
562         mono_arch_flush_icache (buf, code - buf);
563         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
564
565         g_assert (code - buf <= tramp_size);
566
567         char *name = mono_get_rgctx_fetch_trampoline_name (slot);
568         *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
569         g_free (name);
570
571         return buf;
572 }
573
574 /*
575  * mono_arch_create_general_rgctx_lazy_fetch_trampoline:
576  *
577  *   This is a general variant of the rgctx fetch trampolines. It receives a pointer to gpointer[2] in the rgctx reg. The first entry contains the slot, the second
578  * the trampoline to call if the slot is not filled.
579  */
580 gpointer
581 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
582 {
583         guint8 *code, *buf;
584         int tramp_size;
585         MonoJumpInfo *ji = NULL;
586         GSList *unwind_ops = NULL;
587
588         g_assert (aot);
589
590         unwind_ops = mono_arch_get_cie_program ();
591
592         tramp_size = 64;
593
594         code = buf = mono_global_codeman_reserve (tramp_size);
595
596         // FIXME: Currently, we always go to the slow path.
597         
598         /* Load trampoline addr */
599         x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_RGCTX_REG, 4, 4);
600         /* Load mrgctx/vtable */
601         x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
602
603         x86_jump_reg (code, X86_EAX);
604
605         nacl_global_codeman_validate (&buf, tramp_size, &code);
606         mono_arch_flush_icache (buf, code - buf);
607         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
608
609         g_assert (code - buf <= tramp_size);
610
611         *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
612
613         return buf;
614 }
615
616 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
617 /*
618  * The code produced by this trampoline is equivalent to this:
619  *
620  * if (obj) {
621  *      if (obj->synchronisation) {
622  *              if (obj->synchronisation->owner == 0) {
623  *                      if (cmpxch (&obj->synchronisation->owner, TID, 0) == 0)
624  *                              return;
625  *              }
626  *              if (obj->synchronisation->owner == TID) {
627  *                      ++obj->synchronisation->nest;
628  *                      return;
629  *              }
630  *      }
631  * }
632  * return full_monitor_enter ();
633  *
634  */
635 gpointer
636 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
637 {
638         guint8 *code, *buf;
639         guint8 *jump_obj_null, *jump_sync_null, *jump_other_owner, *jump_cmpxchg_failed, *jump_tid, *jump_sync_thin_hash = NULL;
640         guint8 *jump_lock_taken_true = NULL;
641         int tramp_size;
642         int status_offset, nest_offset;
643         MonoJumpInfo *ji = NULL;
644         GSList *unwind_ops = NULL;
645
646         g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
647 #ifdef MONO_ARCH_MONITOR_LOCK_TAKEN_REG
648         g_assert (MONO_ARCH_MONITOR_LOCK_TAKEN_REG == X86_EDX);
649 #else
650         g_assert (!is_v4);
651 #endif
652
653         mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
654         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
655         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
656         status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
657         nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
658
659         tramp_size = NACL_SIZE (128, 192);
660
661         code = buf = mono_global_codeman_reserve (tramp_size);
662
663         x86_push_reg (code, X86_EAX);
664         if (mono_thread_get_tls_offset () != -1) {
665                 if (is_v4) {
666                         x86_test_membase_imm (code, X86_EDX, 0, 1);
667                         /* if *lock_taken is 1, jump to actual trampoline */
668                         jump_lock_taken_true = code;
669                         x86_branch8 (code, X86_CC_NZ, -1, 1);
670                         x86_push_reg (code, X86_EDX);
671                 }
672                 /* MonoObject* obj is in EAX */
673                 /* is obj null? */
674                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
675                 /* if yes, jump to actual trampoline */
676                 jump_obj_null = code;
677                 x86_branch8 (code, X86_CC_Z, -1, 1);
678
679                 /* load obj->synchronization to ECX */
680                 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoObject, synchronisation), 4);
681
682                 if (mono_gc_is_moving ()) {
683                         /*if bit zero is set it's a thin hash*/
684                         /*FIXME use testb encoding*/
685                         x86_test_reg_imm (code, X86_ECX, 0x01);
686                         jump_sync_thin_hash = code;
687                         x86_branch8 (code, X86_CC_NE, -1, 1);
688
689                         /*clear bits used by the gc*/
690                         x86_alu_reg_imm (code, X86_AND, X86_ECX, ~0x3);
691                 }
692
693                 /* is synchronization null? */
694                 x86_test_reg_reg (code, X86_ECX, X86_ECX);
695
696                 /* if yes, jump to actual trampoline */
697                 jump_sync_null = code;
698                 x86_branch8 (code, X86_CC_Z, -1, 1);
699
700                 /* load MonoInternalThread* into EDX */
701                 if (aot) {
702                         /* load_aotconst () puts the result into EAX */
703                         x86_mov_reg_reg (code, X86_EDX, X86_EAX, sizeof (mgreg_t));
704                         code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (TLS_KEY_THREAD));
705                         code = mono_x86_emit_tls_get_reg (code, X86_EAX, X86_EAX);
706                         x86_xchg_reg_reg (code, X86_EAX, X86_EDX, sizeof (mgreg_t));
707                 } else {
708                         code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
709                 }
710                 /* load TID into EDX */
711                 x86_mov_reg_membase (code, X86_EDX, X86_EDX, MONO_STRUCT_OFFSET (MonoInternalThread, small_id), 4);
712
713                 /* is synchronization->owner free */
714                 x86_mov_reg_membase (code, X86_EAX, X86_ECX, status_offset, 4);
715                 x86_test_reg_imm (code, X86_EAX, OWNER_MASK);
716                 /* if not, jump to next case */
717                 jump_tid = code;
718                 x86_branch8 (code, X86_CC_NZ, -1, 1);
719
720                 /* if yes, try a compare-exchange with the TID */
721                 /* Form new status */
722                 x86_alu_reg_reg (code, X86_OR, X86_EDX, X86_EAX);
723                 /* compare and exchange */
724                 x86_prefix (code, X86_LOCK_PREFIX);
725                 x86_cmpxchg_membase_reg (code, X86_ECX, status_offset, X86_EDX);
726                 /* if not successful, jump to actual trampoline */
727                 jump_cmpxchg_failed = code;
728                 x86_branch8 (code, X86_CC_NZ, -1, 1);
729                 /* if successful, pop and return */
730                 if (is_v4) {
731                         x86_pop_reg (code, X86_EDX);
732                         x86_mov_membase_imm (code, X86_EDX, 0, 1, 1);
733                 }
734                 x86_pop_reg (code, X86_EAX);
735                 x86_ret (code);
736
737                 /* next case: synchronization->owner is not null */
738                 x86_patch (jump_tid, code);
739                 /* is synchronization->owner == TID? */
740                 x86_alu_reg_imm (code, X86_AND, X86_EAX, OWNER_MASK);
741                 x86_alu_reg_reg (code, X86_CMP, X86_EAX, X86_EDX);
742                 /* if not, jump to actual trampoline */
743                 jump_other_owner = code;
744                 x86_branch8 (code, X86_CC_NZ, -1, 1);
745                 /* if yes, increment nest */
746                 x86_inc_membase (code, X86_ECX, nest_offset);
747                 if (is_v4) {
748                         x86_pop_reg (code, X86_EDX);
749                         x86_mov_membase_imm (code, X86_EDX, 0, 1, 1);
750                 }
751                 x86_pop_reg (code, X86_EAX);
752                 /* return */
753                 x86_ret (code);
754
755                 /* obj is pushed, jump to the actual trampoline */
756                 x86_patch (jump_obj_null, code);
757                 if (jump_sync_thin_hash)
758                         x86_patch (jump_sync_thin_hash, code);
759                 x86_patch (jump_sync_null, code);
760                 x86_patch (jump_other_owner, code);
761                 x86_patch (jump_cmpxchg_failed, code);
762
763                 if (is_v4) {
764                         x86_pop_reg (code, X86_EDX);
765                         x86_patch (jump_lock_taken_true, code);
766                 }
767         }
768
769         if (aot) {
770                 /* We are calling the generic trampoline directly, the argument is pushed
771                  * on the stack just like a specific trampoline.
772                  */
773                 if (is_v4)
774                         code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter_v4");
775                 else
776                         code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter");
777                 x86_jump_reg (code, X86_EAX);
778         } else {
779                 if (is_v4)
780                         x86_jump_code (code, mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER_V4));
781                 else
782                         x86_jump_code (code, mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER));
783         }
784
785         mono_arch_flush_icache (buf, code - buf);
786         g_assert (code - buf <= tramp_size);
787
788         nacl_global_codeman_validate (&buf, tramp_size, &code);
789         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
790
791         if (is_v4)
792                 *info = mono_tramp_info_create ("monitor_enter_v4_trampoline", buf, code - buf, ji, unwind_ops);
793         else
794                 *info = mono_tramp_info_create ("monitor_enter_trampoline", buf, code - buf, ji, unwind_ops);
795
796         return buf;
797 }
798
799 gpointer
800 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
801 {
802         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
803         guint8 *code, *buf;
804         guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned, *jump_sync_thin_hash = NULL;
805         guint8 *jump_next, *jump_cmpxchg_failed;
806         int tramp_size;
807         int status_offset, nest_offset;
808         MonoJumpInfo *ji = NULL;
809         GSList *unwind_ops = NULL;
810
811         g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
812
813         mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
814         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
815         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
816         status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
817         nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
818
819         tramp_size = NACL_SIZE (128, 192);
820
821         code = buf = mono_global_codeman_reserve (tramp_size);
822
823         x86_push_reg (code, X86_EAX);
824         if (mono_thread_get_tls_offset () != -1) {
825                 /* MonoObject* obj is in EAX */
826                 /* is obj null? */
827                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
828                 /* if yes, jump to actual trampoline */
829                 jump_obj_null = code;
830                 x86_branch8 (code, X86_CC_Z, -1, 1);
831
832                 /* load obj->synchronization to ECX */
833                 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoObject, synchronisation), 4);
834
835                 if (mono_gc_is_moving ()) {
836                         /*if bit zero is set it's a thin hash*/
837                         /*FIXME use testb encoding*/
838                         x86_test_reg_imm (code, X86_ECX, 0x01);
839                         jump_sync_thin_hash = code;
840                         x86_branch8 (code, X86_CC_NE, -1, 1);
841
842                         /*clear bits used by the gc*/
843                         x86_alu_reg_imm (code, X86_AND, X86_ECX, ~0x3);
844                 }
845
846                 /* is synchronization null? */
847                 x86_test_reg_reg (code, X86_ECX, X86_ECX);
848                 /* if yes, jump to actual trampoline */
849                 jump_sync_null = code;
850                 x86_branch8 (code, X86_CC_Z, -1, 1);
851
852                 /* next case: synchronization is not null */
853                 /* load MonoInternalThread* into EDX */
854                 if (aot) {
855                         /* load_aotconst () puts the result into EAX */
856                         x86_mov_reg_reg (code, X86_EDX, X86_EAX, sizeof (mgreg_t));
857                         code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (TLS_KEY_THREAD));
858                         code = mono_x86_emit_tls_get_reg (code, X86_EAX, X86_EAX);
859                         x86_xchg_reg_reg (code, X86_EAX, X86_EDX, sizeof (mgreg_t));
860                 } else {
861                         code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
862                 }
863                 /* load TID into EDX */
864                 x86_mov_reg_membase (code, X86_EDX, X86_EDX, MONO_STRUCT_OFFSET (MonoInternalThread, small_id), 4);
865                 /* is synchronization->owner == TID */
866                 x86_mov_reg_membase (code, X86_EAX, X86_ECX, status_offset, 4);
867                 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EAX);
868                 x86_test_reg_imm (code, X86_EDX, OWNER_MASK);
869                 /* if no, jump to actual trampoline */
870                 jump_not_owned = code;
871                 x86_branch8 (code, X86_CC_NZ, -1, 1);
872
873                 /* next case: synchronization->owner == TID */
874                 /* is synchronization->nest == 1 */
875                 x86_alu_membase_imm (code, X86_CMP, X86_ECX, nest_offset, 1);
876                 /* if not, jump to next case */
877                 jump_next = code;
878                 x86_branch8 (code, X86_CC_NZ, -1, 1);
879                 /* if yes, is synchronization->entry_count greater than zero? */
880                 x86_test_reg_imm (code, X86_EAX, ENTRY_COUNT_WAITERS);
881                 /* if yes, jump to actual trampoline */
882                 jump_have_waiters = code;
883                 x86_branch8 (code, X86_CC_NZ, -1 , 1);
884                 /* if not, try to set synchronization->owner to null and return */
885                 x86_mov_reg_reg (code, X86_EDX, X86_EAX, 4);
886                 x86_alu_reg_imm (code, X86_AND, X86_EDX, ENTRY_COUNT_MASK); 
887                 /* compare and exchange */
888                 x86_prefix (code, X86_LOCK_PREFIX);
889                 /* EAX contains the previous status */
890                 x86_cmpxchg_membase_reg (code, X86_ECX, status_offset, X86_EDX);
891                 /* if not successful, jump to actual trampoline */
892                 jump_cmpxchg_failed = code;
893                 x86_branch8 (code, X86_CC_NZ, -1, 1);
894
895                 x86_pop_reg (code, X86_EAX);
896                 x86_ret (code);
897
898                 /* next case: synchronization->nest is not 1 */
899                 x86_patch (jump_next, code);
900                 /* decrease synchronization->nest and return */
901                 x86_dec_membase (code, X86_ECX, nest_offset);
902                 x86_pop_reg (code, X86_EAX);
903                 x86_ret (code);
904
905                 /* push obj and jump to the actual trampoline */
906                 x86_patch (jump_obj_null, code);
907                 if (jump_sync_thin_hash)
908                         x86_patch (jump_sync_thin_hash, code);
909                 x86_patch (jump_have_waiters, code);
910                 x86_patch (jump_cmpxchg_failed, code);
911                 x86_patch (jump_not_owned, code);
912                 x86_patch (jump_sync_null, code);
913         }
914
915         /* obj is pushed, jump to the actual trampoline */
916         if (aot) {
917                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_exit");
918                 x86_jump_reg (code, X86_EAX);
919         } else {
920                 x86_jump_code (code, tramp);
921         }
922
923         nacl_global_codeman_validate (&buf, tramp_size, &code);
924
925         mono_arch_flush_icache (buf, code - buf);
926         g_assert (code - buf <= tramp_size);
927         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
928
929         *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
930
931         return buf;
932 }
933
934 #else
935
936 gpointer
937 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
938 {
939         g_assert_not_reached ();
940         return NULL;
941 }
942
943 gpointer
944 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
945 {
946         g_assert_not_reached ();
947         return NULL;
948 }
949
950 #endif
951
952 void
953 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
954 {
955         /* FIXME: This is not thread safe */
956         guint8 *code = ji->code_start;
957
958         x86_push_imm (code, func_arg);
959         x86_call_code (code, (guint8*)func);
960 }
961
962 static gpointer
963 handler_block_trampoline_helper (void)
964 {
965         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
966         return jit_tls->handler_block_return_address;
967 }
968
969 gpointer
970 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
971 {
972         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
973         guint8 *code, *buf;
974         int tramp_size = 64;
975         MonoJumpInfo *ji = NULL;
976         GSList *unwind_ops = NULL;
977
978         g_assert (!aot);
979
980         code = buf = mono_global_codeman_reserve (tramp_size);
981
982         /*
983         This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
984         */
985
986         /*
987          * We are in a method frame after the call emitted by OP_CALL_HANDLER.
988          */
989
990         if (mono_get_jit_tls_offset () != -1) {
991                 code = mono_x86_emit_tls_get (code, X86_EAX, mono_get_jit_tls_offset ());
992                 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 4);
993         } else {
994                 /*Slow path uses a c helper*/
995                 x86_call_code (code, handler_block_trampoline_helper);
996         }
997         /* Simulate a call */
998         /*Fix stack alignment*/
999         x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x4);
1000         /* This is the address the trampoline will return to */
1001         x86_push_reg (code, X86_EAX);
1002         /* Dummy trampoline argument, since we call the generic trampoline directly */
1003         x86_push_imm (code, 0);
1004         x86_jump_code (code, tramp);
1005
1006         nacl_global_codeman_validate (&buf, tramp_size, &code);
1007
1008         mono_arch_flush_icache (buf, code - buf);
1009         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
1010         g_assert (code - buf <= tramp_size);
1011
1012         *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
1013
1014         return buf;
1015 }
1016
1017 guint8*
1018 mono_arch_get_call_target (guint8 *code)
1019 {
1020         if (code [-5] == 0xe8) {
1021                 gint32 disp = *(gint32*)(code - 4);
1022                 guint8 *target = code + disp;
1023
1024                 return target;
1025         } else {
1026                 return NULL;
1027         }
1028 }
1029
1030 guint32
1031 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
1032 {
1033         return *(guint32*)(plt_entry + NACL_SIZE (6, 12));
1034 }
1035
1036 /*
1037  * mono_arch_get_gsharedvt_arg_trampoline:
1038  *
1039  *   Return a trampoline which passes ARG to the gsharedvt in/out trampoline ADDR.
1040  */
1041 gpointer
1042 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1043 {
1044         guint8 *code, *start;
1045         int buf_len;
1046
1047         buf_len = 10;
1048
1049         start = code = mono_domain_code_reserve (domain, buf_len);
1050
1051         x86_mov_reg_imm (code, X86_EAX, arg);
1052         x86_jump_code (code, addr);
1053         g_assert ((code - start) <= buf_len);
1054
1055         nacl_domain_code_validate (domain, &start, buf_len, &code);
1056         mono_arch_flush_icache (start, code - start);
1057         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
1058
1059         return start;
1060 }
1061
1062 #if defined(ENABLE_GSHAREDVT)
1063
1064 #include "../../../mono-extensions/mono/mini/tramp-x86-gsharedvt.c"
1065
1066 #else
1067
1068 gpointer
1069 mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
1070 {
1071         *info = NULL;
1072         return NULL;
1073 }
1074
1075 #endif /* !MONOTOUCH */