+ tramp_size = 64 + 8 * depth;
+
+ code = buf = mono_global_codeman_reserve (tramp_size);
+
+ rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
+
+ if (mrgctx) {
+ /* get mrgctx ptr */
+ amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
+ } else {
+ /* load rgctx ptr from vtable */
+ amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 8);
+ /* is the rgctx ptr null? */
+ amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
+ /* if yes, jump to actual trampoline */
+ rgctx_null_jumps [0] = code;
+ amd64_branch8 (code, X86_CC_Z, -1, 1);
+ }
+
+ for (i = 0; i < depth; ++i) {
+ /* load ptr to next array */
+ if (mrgctx && i == 0)
+ amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (MonoMethodRuntimeGenericContext), 8);
+ else
+ amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, 0, 8);
+ /* is the ptr null? */
+ amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
+ /* if yes, jump to actual trampoline */
+ rgctx_null_jumps [i + 1] = code;
+ amd64_branch8 (code, X86_CC_Z, -1, 1);
+ }
+
+ /* fetch slot */
+ amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (gpointer) * (index + 1), 8);
+ /* is the slot null? */
+ amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
+ /* if yes, jump to actual trampoline */
+ rgctx_null_jumps [depth + 1] = code;
+ amd64_branch8 (code, X86_CC_Z, -1, 1);
+ /* otherwise return */
+ amd64_ret (code);
+
+ for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
+ x86_patch (rgctx_null_jumps [i], code);
+
+ g_free (rgctx_null_jumps);
+
+ /* move the rgctx pointer to the VTABLE register */
+ amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, 8);
+
+ if (aot) {
+ *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
+ amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
+ amd64_jump_reg (code, AMD64_R11);
+ } else {
+ tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
+
+ /* jump to the actual trampoline */
+ amd64_jump_code (code, tramp);
+ }
+
+ mono_arch_flush_icache (buf, code - buf);
+
+ g_assert (code - buf <= tramp_size);
+
+ *code_size = code - buf;
+
+ return buf;
+}
+
+gpointer
+mono_arch_create_generic_class_init_trampoline (void)
+{
+ guint32 code_size;
+ MonoJumpInfo *ji;
+
+ return mono_arch_create_generic_class_init_trampoline_full (&code_size, &ji, FALSE);
+}
+
+gpointer
+mono_arch_create_generic_class_init_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+{
+ guint8 *tramp;
+ guint8 *code, *buf;
+ static int byte_offset = -1;
+ static guint8 bitmask;
+ guint8 *jump;
+ int tramp_size;
+
+ *ji = NULL;
+
+ tramp_size = 64;
+
+ code = buf = mono_global_codeman_reserve (tramp_size);
+
+ if (byte_offset < 0)
+ mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
+
+ amd64_test_membase_imm_size (code, MONO_AMD64_ARG_REG1, byte_offset, bitmask, 1);
+ jump = code;
+ amd64_branch8 (code, X86_CC_Z, -1, 1);
+
+ amd64_ret (code);
+
+ x86_patch (jump, code);
+
+ if (aot) {
+ *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
+ amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
+ amd64_jump_reg (code, AMD64_R11);
+ } else {
+ tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
+
+ /* jump to the actual trampoline */
+ amd64_jump_code (code, tramp);
+ }
+
+ mono_arch_flush_icache (buf, code - buf);
+
+ g_assert (code - buf <= tramp_size);
+
+ *code_size = code - buf;
+
+ return buf;
+}
+
+#ifdef MONO_ARCH_MONITOR_OBJECT_REG
+
+gpointer
+mono_arch_create_monitor_enter_trampoline (void)
+{
+ guint32 code_size;
+ MonoJumpInfo *ji;
+
+ return mono_arch_create_monitor_enter_trampoline_full (&code_size, &ji, FALSE);
+}
+
+gpointer
+mono_arch_create_monitor_enter_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+{
+
+ guint8 *tramp;
+ guint8 *code, *buf;
+ guint8 *jump_obj_null, *jump_sync_null, *jump_cmpxchg_failed, *jump_other_owner, *jump_tid;
+ int tramp_size;
+ int owner_offset, nest_offset, dummy;
+
+ *ji = NULL;
+
+ g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
+
+ 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 = 96;
+
+ code = buf = mono_global_codeman_reserve (tramp_size);
+
+ if (mono_thread_get_tls_offset () != -1) {
+ /* MonoObject* obj is in RDI */
+ /* is obj null? */
+ amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
+ /* if yes, jump to actual trampoline */
+ jump_obj_null = code;
+ amd64_branch8 (code, X86_CC_Z, -1, 1);
+
+ /* load obj->synchronization to RCX */
+ amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
+ /* is synchronization null? */
+ amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
+ /* if yes, jump to actual trampoline */
+ jump_sync_null = code;
+ amd64_branch8 (code, X86_CC_Z, -1, 1);
+
+ /* load MonoThread* into RDX */
+ code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
+ /* load TID into RDX */
+ amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoThread, tid), 8);
+
+ /* is synchronization->owner null? */
+ amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, owner_offset, 0, 8);
+ /* if not, jump to next case */
+ jump_tid = code;
+ amd64_branch8 (code, X86_CC_NZ, -1, 1);
+
+ /* if yes, try a compare-exchange with the TID */
+ /* zero RAX */
+ amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
+ /* compare and exchange */
+ amd64_prefix (code, X86_LOCK_PREFIX);
+ amd64_cmpxchg_membase_reg_size (code, AMD64_RCX, owner_offset, AMD64_RDX, 8);
+ /* if not successful, jump to actual trampoline */
+ jump_cmpxchg_failed = code;
+ amd64_branch8 (code, X86_CC_NZ, -1, 1);
+ /* if successful, return */
+ amd64_ret (code);
+
+ /* next case: synchronization->owner is not null */
+ x86_patch (jump_tid, code);
+ /* is synchronization->owner == TID? */
+ amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
+ /* if not, jump to actual trampoline */
+ jump_other_owner = code;
+ amd64_branch8 (code, X86_CC_NZ, -1, 1);
+ /* if yes, increment nest */
+ amd64_inc_membase_size (code, AMD64_RCX, nest_offset, 4);
+ /* return */
+ amd64_ret (code);
+
+ x86_patch (jump_obj_null, code);
+ x86_patch (jump_sync_null, code);
+ x86_patch (jump_cmpxchg_failed, code);
+ x86_patch (jump_other_owner, code);
+ }
+
+ /* jump to the actual trampoline */
+#if MONO_AMD64_ARG_REG1 != AMD64_RDI
+ amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
+#endif
+
+ if (aot) {
+ *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_enter");
+ amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
+ amd64_jump_reg (code, AMD64_R11);
+ } else {
+ tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
+
+ /* jump to the actual trampoline */
+ amd64_jump_code (code, tramp);
+ }
+
+ mono_arch_flush_icache (code, code - buf);
+ g_assert (code - buf <= tramp_size);
+
+ *code_size = code - buf;
+
+ return buf;
+}
+
+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;
+ guint8 *code, *buf;
+ guint8 *jump_obj_null, *jump_have_waiters;
+ guint8 *jump_next;
+ int tramp_size;
+ int owner_offset, nest_offset, entry_count_offset;
+
+ *ji = NULL;
+
+ g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
+
+ 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 = 94;
+
+ code = buf = mono_global_codeman_reserve (tramp_size);
+
+ if (mono_thread_get_tls_offset () != -1) {
+ /* MonoObject* obj is in RDI */
+ /* is obj null? */
+ amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
+ /* if yes, jump to actual trampoline */
+ jump_obj_null = code;
+ amd64_branch8 (code, X86_CC_Z, -1, 1);
+
+ /* load obj->synchronization to RCX */
+ amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
+ /* is synchronization null? */
+ amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
+ /* if not, jump to next case */
+ jump_next = code;
+ amd64_branch8 (code, X86_CC_NZ, -1, 1);
+ /* if yes, just return */
+ amd64_ret (code);
+
+ /* next case: synchronization is not null */
+ x86_patch (jump_next, code);
+ /* load MonoThread* into RDX */
+ code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
+ /* load TID into RDX */
+ amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoThread, tid), 8);
+ /* is synchronization->owner == TID */
+ amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
+ /* if yes, jump to next case */
+ jump_next = code;
+ amd64_branch8 (code, X86_CC_Z, -1, 1);
+ /* if not, just return */
+ amd64_ret (code);
+
+ /* next case: synchronization->owner == TID */
+ x86_patch (jump_next, code);
+ /* is synchronization->nest == 1 */
+ amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, nest_offset, 1, 4);
+ /* if not, jump to next case */
+ jump_next = code;
+ amd64_branch8 (code, X86_CC_NZ, -1, 1);
+ /* if yes, is synchronization->entry_count zero? */
+ amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, entry_count_offset, 0, 4);
+ /* if not, jump to actual trampoline */
+ jump_have_waiters = code;
+ amd64_branch8 (code, X86_CC_NZ, -1 , 1);
+ /* if yes, set synchronization->owner to null and return */
+ amd64_mov_membase_imm (code, AMD64_RCX, owner_offset, 0, 8);
+ amd64_ret (code);
+
+ /* next case: synchronization->nest is not 1 */
+ x86_patch (jump_next, code);
+ /* decrease synchronization->nest and return */
+ amd64_dec_membase_size (code, AMD64_RCX, nest_offset, 4);
+ amd64_ret (code);
+
+ x86_patch (jump_obj_null, code);
+ x86_patch (jump_have_waiters, code);
+ }
+
+ /* jump to the actual trampoline */
+#if MONO_AMD64_ARG_REG1 != AMD64_RDI
+ amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
+#endif
+
+ if (aot) {
+ *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_exit");
+ amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
+ amd64_jump_reg (code, AMD64_R11);
+ } else {
+ tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
+ amd64_jump_code (code, tramp);
+ }
+
+ mono_arch_flush_icache (code, code - buf);
+ g_assert (code - buf <= tramp_size);
+
+ *code_size = code - buf;
+
+ return buf;
+}
+#endif