+gpointer
+mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
+{
+ guint8 *tramp;
+ guint8 *code, *buf;
+ guint8 **rgctx_null_jumps;
+ int tramp_size;
+ int depth, index;
+ int i;
+ gboolean mrgctx;
+
+ mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
+ index = MONO_RGCTX_SLOT_INDEX (slot);
+ if (mrgctx)
+ index += sizeof (MonoMethodRuntimeGenericContext) / sizeof (gpointer);
+ for (depth = 0; ; ++depth) {
+ int size = mono_class_rgctx_get_array_size (depth, mrgctx);
+
+ if (index < size - 1)
+ break;
+ index -= size - 1;
+ }
+
+ tramp_size = 36 + 6 * depth;
+
+ code = buf = mono_global_codeman_reserve (tramp_size);
+
+ rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
+
+ /* load vtable/mrgctx ptr */
+ x86_mov_reg_membase (buf, X86_EAX, X86_ESP, 4, 4);
+ if (!mrgctx) {
+ /* load rgctx ptr from vtable */
+ x86_mov_reg_membase (buf, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
+ /* is the rgctx ptr null? */
+ x86_test_reg_reg (buf, X86_EAX, X86_EAX);
+ /* if yes, jump to actual trampoline */
+ rgctx_null_jumps [0] = buf;
+ x86_branch8 (buf, X86_CC_Z, -1, 1);
+ }
+
+ for (i = 0; i < depth; ++i) {
+ /* load ptr to next array */
+ if (mrgctx && i == 0)
+ x86_mov_reg_membase (buf, X86_EAX, X86_EAX, sizeof (MonoMethodRuntimeGenericContext), 4);
+ else
+ x86_mov_reg_membase (buf, X86_EAX, X86_EAX, 0, 4);
+ /* is the ptr null? */
+ x86_test_reg_reg (buf, X86_EAX, X86_EAX);
+ /* if yes, jump to actual trampoline */
+ rgctx_null_jumps [i + 1] = buf;
+ x86_branch8 (buf, X86_CC_Z, -1, 1);
+ }
+
+ /* fetch slot */
+ x86_mov_reg_membase (buf, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
+ /* is the slot null? */
+ x86_test_reg_reg (buf, X86_EAX, X86_EAX);
+ /* if yes, jump to actual trampoline */
+ rgctx_null_jumps [depth + 1] = buf;
+ x86_branch8 (buf, X86_CC_Z, -1, 1);
+ /* otherwise return */
+ x86_ret (buf);
+
+ for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
+ x86_patch (rgctx_null_jumps [i], buf);
+
+ g_free (rgctx_null_jumps);
+
+ x86_mov_reg_membase (buf, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
+
+ 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 */
+ x86_jump_code (buf, tramp);
+
+ mono_arch_flush_icache (code, buf - code);
+
+ g_assert (buf - code <= tramp_size);
+
+ return code;
+}
+
+gpointer
+mono_arch_create_generic_class_init_trampoline (void)
+{
+ guint8 *tramp;
+ guint8 *code, *buf;
+ static int byte_offset = -1;
+ static guint8 bitmask;
+ guint8 *jump;
+ int tramp_size;
+
+ 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);
+
+ x86_test_membase_imm (code, MONO_ARCH_VTABLE_REG, byte_offset, bitmask);
+ jump = code;
+ x86_branch8 (code, X86_CC_Z, -1, 1);
+
+ x86_ret (code);
+
+ x86_patch (jump, code);
+
+ /* Push the vtable so the stack is the same as in a specific trampoline */
+ x86_push_reg (code, MONO_ARCH_VTABLE_REG);
+
+ tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
+
+ /* jump to the actual trampoline */
+ x86_jump_code (code, tramp);
+
+ mono_arch_flush_icache (code, code - buf);
+
+ g_assert (code - buf <= tramp_size);
+
+ return buf;
+}
+