Merge remote branch 'upstream/master'
[mono.git] / mono / mini / mini.c
index 734706d635acb1da98308d08f240c7c6db59af48..69e19cf74d148f54663305cd625120b96a4a601c 100644 (file)
 
 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt, MonoException **ex);
 
-#ifdef __native_client_codegen__
-/* Default alignment for Native Client is 32-byte. */
-guint8 nacl_align_byte = 0xe0;
-#endif
 
 static guint32 default_opt = 0;
 static gboolean default_opt_set = FALSE;
@@ -103,6 +99,10 @@ guint32 mono_jit_tls_id = -1;
 MINI_FAST_TLS_DECLARE(mono_jit_tls);
 #endif
 
+#ifndef MONO_ARCH_MONITOR_ENTER_ADJUSTMENT
+#define MONO_ARCH_MONITOR_ENTER_ADJUSTMENT 1
+#endif
+
 MonoTraceSpec *mono_jit_trace_calls = NULL;
 gboolean mono_break_on_exc = FALSE;
 gboolean mono_compile_aot = FALSE;
@@ -160,6 +160,38 @@ gboolean disable_vtypes_in_regs = FALSE;
 
 gboolean mono_dont_free_global_codeman;
 
+gpointer
+mono_realloc_native_code (MonoCompile *cfg)
+{
+#if defined(__default_codegen__)
+       return g_realloc (cfg->native_code, cfg->code_size);
+#elif defined(__native_client_codegen__)
+       guint old_padding;
+       gpointer native_code;
+       guint alignment_check;
+
+       /* Save the old alignment offset so we can re-align after the realloc. */
+       old_padding = (guint)(cfg->native_code - cfg->native_code_alloc);
+
+       cfg->native_code_alloc = g_realloc ( cfg->native_code_alloc,
+                                                                                cfg->code_size + kNaClAlignment );
+
+       /* Align native_code to next nearest kNaClAlignment byte. */
+       native_code = (guint)cfg->native_code_alloc + kNaClAlignment;
+       native_code = (guint)native_code & ~kNaClAlignmentMask;
+
+       /* Shift the data to be 32-byte aligned again. */
+       memmove (native_code, cfg->native_code_alloc + old_padding, cfg->code_size);
+
+       alignment_check = (guint)native_code & kNaClAlignmentMask;
+       g_assert (alignment_check == 0);
+       return native_code;
+#else
+       g_assert_not_reached ();
+       return cfg->native_code;
+#endif
+}
+
 #ifdef __native_client_codegen__
 
 /* Prevent instructions from straddling a 32-byte alignment boundary.   */
@@ -426,6 +458,67 @@ void *mono_global_codeman_reserve (int size)
        }
 }
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+/* Given the temporary buffer (allocated by mono_global_codeman_reserve) into
+ * which we are generating code, return a pointer to the destination in the
+ * dynamic code segment into which the code will be copied when
+ * mono_global_codeman_commit is called.
+ * LOCKING: Acquires the jit lock.
+ */
+void*
+nacl_global_codeman_get_dest (void *data)
+{
+       void *dest;
+       mono_jit_lock ();
+       dest = nacl_code_manager_get_code_dest (global_codeman, data);
+       mono_jit_unlock ();
+       return dest;
+}
+
+void
+mono_global_codeman_commit (void *data, int size, int newsize)
+{
+       mono_jit_lock ();
+       mono_code_manager_commit (global_codeman, data, size, newsize);
+       mono_jit_unlock ();
+}
+
+/* 
+ * Convenience function which calls mono_global_codeman_commit to validate and
+ * copy the code. The caller sets *buf_base and *buf_size to the start and size
+ * of the buffer (allocated by mono_global_codeman_reserve), and *code_end to
+ * the byte after the last instruction byte. On return, *buf_base will point to
+ * the start of the copied in the code segment, and *code_end will point after
+ * the end of the copied code.
+ */
+void
+nacl_global_codeman_validate (guint8 **buf_base, int buf_size, guint8 **code_end)
+{
+       guint8 *tmp = nacl_global_codeman_get_dest (*buf_base);
+       mono_global_codeman_commit (*buf_base, buf_size, *code_end - *buf_base);
+       *code_end = tmp + (*code_end - *buf_base);
+       *buf_base = tmp;
+}
+#else
+/* no-op versions of Native Client functions */
+void*
+nacl_global_codeman_get_dest (void *data)
+{
+       return data;
+}
+
+void
+mono_global_codeman_commit (void *data, int size, int newsize)
+{
+}
+
+void
+nacl_global_codeman_validate (guint8 **buf_base, int buf_size, guint8 **code_end)
+{
+}
+
+#endif /* __native_client__ */
+
 /**
  * mono_create_unwind_op:
  *
@@ -1039,6 +1132,18 @@ mono_compile_create_var_for_vreg (MonoCompile *cfg, MonoType *type, int opcode,
        inst->backend.is_pinvoke = 0;
        inst->dreg = vreg;
 
+       if (cfg->compute_gc_maps) {
+               if (type->byref) {
+                       mono_mark_vreg_as_mp (cfg, vreg);
+               } else {
+                       MonoType *t = mini_type_get_underlying_type (NULL, type);
+                       if ((MONO_TYPE_ISSTRUCT (t) && inst->klass->has_references) || MONO_TYPE_IS_REFERENCE (t)) {
+                               inst->flags |= MONO_INST_GC_TRACK;
+                               mono_mark_vreg_as_ref (cfg, vreg);
+                       }
+               }
+       }
+       
        cfg->varinfo [num] = inst;
 
        MONO_INIT_VARINFO (&cfg->vars [num], num);
@@ -1129,6 +1234,38 @@ mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
        return mono_compile_create_var_for_vreg (cfg, type, opcode, dreg);
 }
 
+void
+mono_mark_vreg_as_ref (MonoCompile *cfg, int vreg)
+{
+       if (vreg >= cfg->vreg_is_ref_len) {
+               gboolean *tmp = cfg->vreg_is_ref;
+               int size = cfg->vreg_is_ref_len;
+
+               while (vreg >= cfg->vreg_is_ref_len)
+                       cfg->vreg_is_ref_len = cfg->vreg_is_ref_len ? cfg->vreg_is_ref_len * 2 : 32;
+               cfg->vreg_is_ref = mono_mempool_alloc0 (cfg->mempool, sizeof (gboolean) * cfg->vreg_is_ref_len);
+               if (size)
+                       memcpy (cfg->vreg_is_ref, tmp, size * sizeof (gboolean));
+       }
+       cfg->vreg_is_ref [vreg] = TRUE;
+}      
+
+void
+mono_mark_vreg_as_mp (MonoCompile *cfg, int vreg)
+{
+       if (vreg >= cfg->vreg_is_mp_len) {
+               gboolean *tmp = cfg->vreg_is_mp;
+               int size = cfg->vreg_is_mp_len;
+
+               while (vreg >= cfg->vreg_is_mp_len)
+                       cfg->vreg_is_mp_len = cfg->vreg_is_mp_len ? cfg->vreg_is_mp_len * 2 : 32;
+               cfg->vreg_is_mp = mono_mempool_alloc0 (cfg->mempool, sizeof (gboolean) * cfg->vreg_is_mp_len);
+               if (size)
+                       memcpy (cfg->vreg_is_mp, tmp, size * sizeof (gboolean));
+       }
+       cfg->vreg_is_mp [vreg] = TRUE;
+}      
+
 /*
  * Transform a MonoInst into a load from the variable of index var_index.
  */
@@ -1636,7 +1773,7 @@ mono_allocate_stack_slots_full2 (MonoCompile *cfg, gboolean backward, guint32 *s
                case MONO_TYPE_PTR:
                case MONO_TYPE_I:
                case MONO_TYPE_U:
-#if SIZEOF_REGISTER == 4
+#if SIZEOF_VOID_P == 4
                case MONO_TYPE_I4:
 #else
                case MONO_TYPE_I8:
@@ -1870,7 +2007,7 @@ mono_allocate_stack_slots_full (MonoCompile *cfg, gboolean backward, guint32 *st
 
        vars = mono_varlist_sort (cfg, vars, 0);
        offset = 0;
-       *stack_align = sizeof (gpointer);
+       *stack_align = sizeof(mgreg_t);
        for (l = vars; l; l = l->next) {
                vmv = l->data;
                inst = cfg->varinfo [vmv->idx];
@@ -1925,7 +2062,7 @@ mono_allocate_stack_slots_full (MonoCompile *cfg, gboolean backward, guint32 *st
                        case MONO_TYPE_PTR:
                        case MONO_TYPE_I:
                        case MONO_TYPE_U:
-#if SIZEOF_REGISTER == 4
+#if SIZEOF_VOID_P == 4
                        case MONO_TYPE_I4:
 #else
                        case MONO_TYPE_I8:
@@ -2139,7 +2276,11 @@ print_dfn (MonoCompile *cfg) {
        MonoBasicBlock *bb;
        MonoInst *c;
 
-       g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
+       {
+               char *method_name = mono_method_full_name (cfg->method, TRUE);
+               g_print ("IR code for method %s\n", method_name);
+               g_free (method_name);
+       }
 
        for (i = 0; i < cfg->num_bblocks; ++i) {
                bb = cfg->bblocks [i];
@@ -2225,6 +2366,8 @@ mono_bblock_insert_before_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_
 {
        if (ins == NULL) {
                ins = bb->code;
+               if (ins)
+                       ins->prev = ins_to_insert;
                bb->code = ins_to_insert;
                ins_to_insert->next = ins;
                if (bb->last_ins == NULL)
@@ -2300,6 +2443,8 @@ mono_destroy_compile (MonoCompile *cfg)
                g_hash_table_destroy (cfg->abs_patches);
        mono_mempool_destroy (cfg->mempool);
 
+       mono_debug_free_method (cfg);
+
        g_free (cfg->varinfo);
        g_free (cfg->vars);
        g_free (cfg->exception_message);
@@ -2365,8 +2510,11 @@ mono_get_lmf (void)
 
        if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
                return jit_tls->lmf;
-
-       g_assert_not_reached ();
+       /*
+        * We do not assert here because this function can be called from
+        * mini-gc.c on a thread that has not executed any managed code, yet
+        * (the thread object allocation can trigger a collection).
+        */
        return NULL;
 #endif
 }
@@ -2802,7 +2950,13 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
                target = patch_info->data.inst->inst_c0 + code;
                break;
        case MONO_PATCH_INFO_IP:
+#if defined(__native_client__) && defined(__native_client_codegen__)
+               /* Need to transform to the destination address, it's */
+               /* emitted as an immediate in the code. */
+               target = nacl_inverse_modify_patch_target(ip);
+#else
                target = ip;
+#endif
                break;
        case MONO_PATCH_INFO_METHOD_REL:
                target = code + patch_info->data.offset;
@@ -2818,6 +2972,13 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
        }
        case MONO_PATCH_INFO_METHOD_JUMP:
                target = mono_create_jump_trampoline (domain, patch_info->data.method, FALSE);
+#if defined(__native_client__) && defined(__native_client_codegen__)
+#if defined(TARGET_AMD64)
+               /* This target is an absolute address, not relative to the */
+               /* current code being emitted on AMD64. */
+               target = nacl_inverse_modify_patch_target(target);
+#endif
+#endif
                break;
        case MONO_PATCH_INFO_METHOD:
                if (patch_info->data.method == method) {
@@ -2831,6 +2992,11 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
                gpointer *jump_table;
                int i;
 
+#if defined(__native_client__) && defined(__native_client_codegen__)
+               /* This memory will leak, but we don't care if we're */
+               /* not deleting JIT'd methods anyway                 */
+               jump_table = g_malloc0 (sizeof(gpointer) * patch_info->data.table->table_size);
+#else
                if (method && method->dynamic) {
                        jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
                } else {
@@ -2840,10 +3006,27 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
                                jump_table = mono_domain_code_reserve (domain, sizeof (gpointer) * patch_info->data.table->table_size);
                        }
                }
+#endif
 
-               for (i = 0; i < patch_info->data.table->table_size; i++)
+               for (i = 0; i < patch_info->data.table->table_size; i++) {
+#if defined(__native_client__) && defined(__native_client_codegen__)
+                       /* 'code' is relative to the current code blob, we */
+                       /* need to do this transform on it to make the     */
+                       /* pointers in this table absolute                 */
+                       jump_table [i] = nacl_inverse_modify_patch_target (code) + GPOINTER_TO_INT (patch_info->data.table->table [i]);
+#else
                        jump_table [i] = code + GPOINTER_TO_INT (patch_info->data.table->table [i]);
+#endif
+               }
+
+#if defined(__native_client__) && defined(__native_client_codegen__)
+               /* jump_table is in the data section, we need to transform */
+               /* it here so when it gets modified in amd64_patch it will */
+               /* then point back to the absolute data address            */
+               target = nacl_inverse_modify_patch_target (jump_table);
+#else
                target = jump_table;
+#endif
                break;
        }
        case MONO_PATCH_INFO_METHODCONST:
@@ -3189,11 +3372,18 @@ mono_postprocess_patches (MonoCompile *cfg)
                }
                case MONO_PATCH_INFO_SWITCH: {
                        gpointer *table;
+#if defined(__native_client__) && defined(__native_client_codegen__)
+                       /* This memory will leak.  */
+                       /* TODO: can we free this when  */
+                       /* making the final jump table? */
+                       table = g_malloc0 (sizeof(gpointer) * patch_info->data.table->table_size);
+#else
                        if (cfg->method->dynamic) {
                                table = mono_code_manager_reserve (cfg->dynamic_info->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
                        } else {
                                table = mono_domain_code_reserve (cfg->domain, sizeof (gpointer) * patch_info->data.table->table_size);
                        }
+#endif
 
                        for (i = 0; i < patch_info->data.table->table_size; i++) {
                                /* Might be NULL if the switch is eliminated */
@@ -3211,6 +3401,12 @@ mono_postprocess_patches (MonoCompile *cfg)
                        GSList *list;
                        MonoDomain *domain = cfg->domain;
                        unsigned char *ip = cfg->native_code + patch_info->ip.i;
+#if defined(__native_client__) && defined(__native_client_codegen__)
+                       /* When this jump target gets evaluated, the method */
+                       /* will be installed in the dynamic code section,   */
+                       /* not at the location of cfg->native_code.         */
+                       ip = nacl_inverse_modify_patch_target (cfg->native_code) + patch_info->ip.i;
+#endif
 
                        mono_domain_lock (domain);
                        if (!domain_jit_info (domain)->jump_target_hash)
@@ -3331,9 +3527,13 @@ mono_save_seq_point_info (MonoCompile *cfg)
        cfg->seq_point_info = info;
 
        // FIXME: dynamic methods
-       mono_domain_lock (domain);
-       g_hash_table_insert (domain_jit_info (domain)->seq_points, cfg->method_to_register, info);
-       mono_domain_unlock (domain);
+       if (!cfg->compile_aot) {
+               mono_domain_lock (domain);
+               // FIXME: How can the lookup succeed ?
+               if (!g_hash_table_lookup (domain_jit_info (domain)->seq_points, cfg->method_to_register))
+                       g_hash_table_insert (domain_jit_info (domain)->seq_points, cfg->method_to_register, info);
+               mono_domain_unlock (domain);
+       }
 
        g_ptr_array_free (cfg->seq_points, TRUE);
        cfg->seq_points = NULL;
@@ -3346,6 +3546,15 @@ mono_codegen (MonoCompile *cfg)
        int max_epilog_size;
        guint8 *code;
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       void *code_dest;
+
+       /* This keeps patch targets from being transformed during
+        * ordinary method compilation, for local branches and jumps.
+        */
+       nacl_allow_target_modification (FALSE);
+#endif
+
        for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
                cfg->spill_count = 0;
                /* we reuse dfn here */
@@ -3379,6 +3588,7 @@ mono_codegen (MonoCompile *cfg)
        /* emit code all basic blocks */
        for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
                bb->native_offset = cfg->code_len;
+               bb->real_native_offset = cfg->code_len;
                //if ((bb == cfg->bb_entry) || !(bb->region == -1 && !bb->dfn))
                        mono_arch_output_basic_block (cfg, bb);
                bb->native_length = cfg->code_len - bb->native_offset;
@@ -3397,6 +3607,9 @@ mono_codegen (MonoCompile *cfg)
                }
        }
 
+#ifdef __native_client_codegen__
+       mono_nacl_fix_patches (cfg->native_code, cfg->patch_info);
+#endif
        mono_arch_emit_exceptions (cfg);
 
        max_epilog_size = 0;
@@ -3427,9 +3640,14 @@ mono_codegen (MonoCompile *cfg)
 #endif
                code = mono_domain_code_reserve (cfg->domain, cfg->code_size + unwindlen);
        }
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_allow_target_modification (TRUE);
+#endif
 
        memcpy (code, cfg->native_code, cfg->code_len);
-#ifdef __native_client_codegen__
+#if defined(__default_codegen__)
+       g_free (cfg->native_code);
+#elif defined(__native_client_codegen__)
        if (cfg->native_code_alloc) {
                g_free (cfg->native_code_alloc);
                cfg->native_code_alloc = 0;
@@ -3437,9 +3655,7 @@ mono_codegen (MonoCompile *cfg)
        else if (cfg->native_code) {
                g_free (cfg->native_code);
        }
-#else
-       g_free (cfg->native_code);
-#endif
+#endif /* __native_client_codegen__ */
        cfg->native_code = code;
        code = cfg->native_code + cfg->code_len;
   
@@ -3477,8 +3693,18 @@ if (valgrind_register){
 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
        mono_arch_save_unwind_info (cfg);
 #endif
-       
-#ifdef __native_client_codegen__
+
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       if (!cfg->compile_aot) {
+               if (cfg->method->dynamic) {
+                       code_dest = nacl_code_manager_get_code_dest(cfg->dynamic_info->code_mp, cfg->native_code);
+               } else {
+                       code_dest = nacl_domain_get_code_dest(cfg->domain, cfg->native_code);
+               }
+       }
+#endif
+
+#if defined(__native_client_codegen__)
        mono_nacl_fix_patches (cfg->native_code, cfg->patch_info);
 #endif
 
@@ -3489,6 +3715,9 @@ if (valgrind_register){
        } else {
                mono_domain_code_commit (cfg->domain, cfg->native_code, cfg->code_size, cfg->code_len);
        }
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       cfg->native_code = code_dest;
+#endif
        mono_profiler_code_buffer_new (cfg->native_code, cfg->code_len, MONO_PROFILER_CODE_BUFFER_METHOD, cfg->method);
        
        mono_arch_flush_icache (cfg->native_code, cfg->code_len);
@@ -3512,6 +3741,20 @@ compute_reachable (MonoBasicBlock *bb)
        }
 }
 
+static void
+mono_handle_out_of_line_bblock (MonoCompile *cfg)
+{
+       MonoBasicBlock *bb;
+       for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
+               if (bb->next_bb && bb->next_bb->out_of_line && bb->last_ins && !MONO_IS_BRANCH_OP (bb->last_ins)) {
+                       MonoInst *ins;
+                       MONO_INST_NEW (cfg, ins, OP_BR);
+                       MONO_ADD_INS (bb, ins);
+                       ins->inst_target_bb = bb->next_bb;
+               }
+       }
+}
+
 static MonoJitInfo*
 create_jit_info (MonoCompile *cfg, MonoMethod *method_to_compile)
 {
@@ -3690,9 +3933,8 @@ create_jit_info (MonoCompile *cfg, MonoMethod *method_to_compile)
                                /*
                                 * Extend the try block backwards to include parts of the previous call
                                 * instruction.
-                                * FIXME: This is arch specific.
                                 */
-                               ei->try_start = (guint8*)ei->try_start - 1;
+                               ei->try_start = (guint8*)ei->try_start - MONO_ARCH_MONITOR_ENTER_ADJUSTMENT;
                        }
                        tblock = cfg->cil_offset_to_bb [ec->try_offset + ec->try_len];
                        g_assert (tblock);
@@ -4068,12 +4310,14 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
        cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX * header->max_stack);
 
        if (cfg->verbose_level > 0) {
+               char *method_name;
                if (COMPILE_LLVM (cfg))
-                       g_print ("converting llvm method %s\n", mono_method_full_name (method, TRUE));
+                       g_print ("converting llvm method %s\n", method_name = mono_method_full_name (method, TRUE));
                else if (cfg->generic_sharing_context)
-                       g_print ("converting shared method %s\n", mono_method_full_name (method_to_compile, TRUE));
+                       g_print ("converting shared method %s\n", method_name = mono_method_full_name (method_to_compile, TRUE));
                else
-                       g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
+                       g_print ("converting method %s\n", method_name = mono_method_full_name (method, TRUE));
+               g_free (method_name);
        }
 
        if (cfg->opt & (MONO_OPT_ABCREM | MONO_OPT_SSAPRE))
@@ -4220,6 +4464,8 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
         */
        mono_liveness_handle_exception_clauses (cfg);
 
+       mono_handle_out_of_line_bblock (cfg);
+
        /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
 
        if (!COMPILE_LLVM (cfg))
@@ -4586,12 +4832,6 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
        else
                InterlockedIncrement (&mono_jit_stats.methods_without_llvm);
 
-       if (cfg->verbose_level >= 2) {
-               char *id =  mono_method_full_name (cfg->method, FALSE);
-               mono_disassemble_code (cfg, cfg->native_code, cfg->code_len, id + 3);
-               g_free (id);
-       }
-
        cfg->jit_info = create_jit_info (cfg, method_to_compile);
 
 #ifdef MONO_ARCH_HAVE_LIVERANGE_OPS
@@ -4609,6 +4849,12 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
  
        mono_save_seq_point_info (cfg);
 
+       if (cfg->verbose_level >= 2) {
+               char *id =  mono_method_full_name (cfg->method, FALSE);
+               mono_disassemble_code (cfg, cfg->native_code, cfg->code_len, id + 3);
+               g_free (id);
+       }
+
        if (!cfg->compile_aot) {
                mono_domain_lock (cfg->domain);
                mono_jit_info_table_add (cfg->domain, cfg->jit_info);
@@ -5910,6 +6156,9 @@ mini_init (const char *filename, const char *runtime_version)
 #endif
 
        mono_install_callbacks (&callbacks);
+
+       if (getenv ("MONO_DEBUG") != NULL)
+               mini_parse_debug_options ();
        
        mono_arch_cpu_init ();
 
@@ -5919,9 +6168,6 @@ mini_init (const char *filename, const char *runtime_version)
 
        mini_gc_init ();
 
-       if (getenv ("MONO_DEBUG") != NULL)
-               mini_parse_debug_options ();
-
        if (getenv ("MONO_XDEBUG")) {
                char *xdebug_opts = getenv ("MONO_XDEBUG");
                mono_xdebug_init (xdebug_opts);
@@ -6026,6 +6272,9 @@ mini_init (const char *filename, const char *runtime_version)
 
        mono_icall_init ();
 
+       /* This should come after mono_init () too */
+       mini_gc_init ();
+
        mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", 
                                ves_icall_get_frame_info);
        mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", 
@@ -6067,6 +6316,9 @@ mini_init (const char *filename, const char *runtime_version)
        register_icall (mono_load_remote_field_new, "mono_load_remote_field_new", "object object ptr ptr", FALSE);
        register_icall (mono_store_remote_field_new, "mono_store_remote_field_new", "void object ptr ptr object", FALSE);
 
+#if defined(__native_client__) || defined(__native_client_codegen__)
+       register_icall (mono_nacl_gc, "mono_nacl_gc", "void", TRUE);
+#endif
        /* 
         * NOTE, NOTE, NOTE, NOTE:
         * when adding emulation for some opcodes, remember to also add a dummy
@@ -6137,7 +6389,11 @@ mini_init (const char *filename, const char *runtime_version)
        mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un, FALSE);
 #endif
 #ifdef MONO_ARCH_EMULATE_FREM
+#if defined(__default_codegen__)
        mono_register_opcode_emulation (OP_FREM, "__emul_frem", "double double double", fmod, FALSE);
+#elif defined(__native_client_codegen__)
+       mono_register_opcode_emulation (OP_FREM, "__emul_frem", "double double double", mono_fmod, FALSE);
+#endif
 #endif
 
 #ifdef MONO_ARCH_SOFT_FLOAT