+
+gpointer
+mono_arch_create_monitor_exit_trampoline (void)
+{
+ guint32 code_size;
+ MonoJumpInfo *ji;
+
+ return mono_arch_create_monitor_exit_trampoline_full (&code_size, &ji, FALSE);
+}
+
+gpointer
+mono_arch_create_monitor_exit_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+{
+ guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
+ guint8 *code, *buf;
+ guint8 *jump_obj_null, *jump_have_waiters;
+ guint8 *jump_next;
+ int tramp_size;
+ int owner_offset, nest_offset, entry_count_offset;
+
+ g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
+
+ mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
+ g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
+ g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
+ g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
+ owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
+ nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
+ entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
+
+ tramp_size = 64;
+
+ code = buf = mono_global_codeman_reserve (tramp_size);
+
+ if (mono_thread_get_tls_offset () != -1) {
+ /* MonoObject* obj is in EAX */
+ /* is obj null? */
+ x86_test_reg_reg (buf, X86_EAX, X86_EAX);
+ /* if yes, jump to actual trampoline */
+ jump_obj_null = buf;
+ x86_branch8 (buf, X86_CC_Z, -1, 1);
+
+ /* load obj->synchronization to ECX */
+ x86_mov_reg_membase (buf, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
+ /* is synchronization null? */
+ x86_test_reg_reg (buf, X86_ECX, X86_ECX);
+ /* if not, jump to next case */
+ jump_next = buf;
+ x86_branch8 (buf, X86_CC_NZ, -1, 1);
+ /* if yes, just return */
+ x86_ret (buf);
+
+ /* next case: synchronization is not null */
+ x86_patch (jump_next, buf);
+ /* load MonoThread* into EDX */
+ buf = mono_x86_emit_tls_get (buf, X86_EDX, mono_thread_get_tls_offset ());
+ /* load TID into EDX */
+ x86_mov_reg_membase (buf, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoThread, tid), 4);
+ /* is synchronization->owner == TID */
+ x86_alu_membase_reg (buf, X86_CMP, X86_ECX, owner_offset, X86_EDX);
+ /* if yes, jump to next case */
+ jump_next = buf;
+ x86_branch8 (buf, X86_CC_Z, -1, 1);
+ /* if not, just return */
+ x86_ret (buf);
+
+ /* next case: synchronization->owner == TID */
+ x86_patch (jump_next, buf);
+ /* is synchronization->nest == 1 */
+ x86_alu_membase_imm (buf, X86_CMP, X86_ECX, nest_offset, 1);
+ /* if not, jump to next case */
+ jump_next = buf;
+ x86_branch8 (buf, X86_CC_NZ, -1, 1);
+ /* if yes, is synchronization->entry_count zero? */
+ x86_alu_membase_imm (buf, X86_CMP, X86_ECX, entry_count_offset, 0);
+ /* if not, jump to actual trampoline */
+ jump_have_waiters = buf;
+ x86_branch8 (buf, X86_CC_NZ, -1 , 1);
+ /* if yes, set synchronization->owner to null and return */
+ x86_mov_membase_imm (buf, X86_ECX, owner_offset, 0, 4);
+ x86_ret (buf);
+
+ /* next case: synchronization->nest is not 1 */
+ x86_patch (jump_next, buf);
+ /* decrease synchronization->nest and return */
+ x86_dec_membase (buf, X86_ECX, nest_offset);
+ x86_ret (buf);
+
+ /* push obj and jump to the actual trampoline */
+ x86_patch (jump_obj_null, buf);
+ x86_patch (jump_have_waiters, buf);
+ x86_push_reg (buf, X86_EAX);
+ x86_jump_code (buf, tramp);
+ } else {
+ /* push obj and jump to the actual trampoline */
+ x86_push_reg (buf, X86_EAX);
+ x86_jump_code (buf, tramp);
+ }
+
+ mono_arch_flush_icache (buf, buf - code);
+ g_assert (buf - code <= tramp_size);
+
+ return code;
+}
+#else
+gpointer
+mono_arch_create_monitor_enter_trampoline (void)
+{
+ g_assert_not_reached ();
+ return NULL;
+}
+
+gpointer
+mono_arch_create_monitor_exit_trampoline (void)
+{
+ g_assert_not_reached ();
+ return NULL;
+}
+#endif
+
+void
+mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
+{
+ /* FIXME: This is not thread safe */
+ guint8 *code = ji->code_start;
+
+ x86_push_imm (code, func_arg);
+ x86_call_code (code, (guint8*)func);
+}