+ guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
+ guint8 *code, *buf;
+ guint8 *jump_obj_null, *jump_sync_null, *jump_other_owner, *jump_cmpxchg_failed, *jump_tid;
+ int tramp_size;
+ int owner_offset, nest_offset, dummy;
+
+ g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
+
+ mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
+ g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
+ g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
+ owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
+ nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_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 yes, jump to actual trampoline */
+ jump_sync_null = buf;
+ x86_branch8 (buf, X86_CC_Z, -1, 1);
+
+ /* 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 null? */
+ x86_alu_membase_imm (buf, X86_CMP, X86_ECX, owner_offset, 0);
+ /* if not, jump to next case */
+ jump_tid = buf;
+ x86_branch8 (buf, X86_CC_NZ, -1, 1);
+
+ /* if yes, try a compare-exchange with the TID */
+ /* free up register EAX, needed for the zero */
+ x86_push_reg (buf, X86_EAX);
+ /* zero EAX */
+ x86_alu_reg_reg (buf, X86_XOR, X86_EAX, X86_EAX);
+ /* compare and exchange */
+ x86_prefix (buf, X86_LOCK_PREFIX);
+ x86_cmpxchg_membase_reg (buf, X86_ECX, owner_offset, X86_EDX);
+ /* if not successful, jump to actual trampoline */
+ jump_cmpxchg_failed = buf;
+ x86_branch8 (buf, X86_CC_NZ, -1, 1);
+ /* if successful, pop and return */
+ x86_pop_reg (buf, X86_EAX);
+ x86_ret (buf);
+
+ /* next case: synchronization->owner is not null */
+ x86_patch (jump_tid, buf);
+ /* is synchronization->owner == TID? */
+ x86_alu_membase_reg (buf, X86_CMP, X86_ECX, owner_offset, X86_EDX);
+ /* if not, jump to actual trampoline */
+ jump_other_owner = buf;
+ x86_branch8 (buf, X86_CC_NZ, -1, 1);
+ /* if yes, increment nest */
+ x86_inc_membase (buf, X86_ECX, nest_offset);
+ /* return */
+ x86_ret (buf);
+
+ /* push obj */
+ x86_patch (jump_obj_null, buf);
+ x86_patch (jump_sync_null, buf);
+ x86_patch (jump_other_owner, buf);
+ x86_push_reg (buf, X86_EAX);
+ /* jump to the actual trampoline */
+ x86_patch (jump_cmpxchg_failed, buf);
+ 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);
+ }