+ else
+ memcpy (&new_ctx, &tls->ctx, sizeof (MonoContext));
+
+ memset (reg_locations, 0, sizeof (reg_locations));
+ memset (new_reg_locations, 0, sizeof (new_reg_locations));
+
+ while (TRUE) {
+ memcpy (&ctx, &new_ctx, sizeof (ctx));
+
+ for (i = 0; i < MONO_MAX_IREGS; ++i) {
+ if (new_reg_locations [i]) {
+ /*
+ * If the current frame saves the register, it means it might modify its
+ * value, thus the old location might not contain the same value, so
+ * we have to mark it conservatively.
+ */
+ if (reg_locations [i]) {
+ DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
+ mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
+ scanned_registers += sizeof (mgreg_t);
+ }
+
+ reg_locations [i] = new_reg_locations [i];
+
+ DEBUG (fprintf (logfile, "\treg %s is now at location %p.\n", mono_arch_regname (i), reg_locations [i]));
+ }
+ }
+
+ g_assert ((mgreg_t)stack_limit % sizeof (mgreg_t) == 0);
+
+ res = mono_find_jit_info_ext (frame.domain ? frame.domain : mono_domain_get (), tls->jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, new_reg_locations, &frame);
+ if (!res)
+ break;
+
+ ji = frame.ji;
+
+ if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE) {
+ /*
+ * These frames are problematic for several reasons:
+ * - they are unwound through an LMF, and we have no precise register tracking for those.
+ * - the LMF might not contain a precise ip, so we can't compute the call site.
+ * - the LMF only unwinds to the wrapper frame, so we get these methods twice.
+ */
+ DEBUG (fprintf (logfile, "Mark(0): <Managed-to-native transition>\n"));
+ for (i = 0; i < MONO_MAX_IREGS; ++i) {
+ if (reg_locations [i]) {
+ DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
+ mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
+ scanned_registers += sizeof (mgreg_t);
+ }
+ reg_locations [i] = NULL;
+ new_reg_locations [i] = NULL;
+ }
+ ctx = new_ctx;
+ continue;
+ }
+
+ /* The last frame can be in any state so mark conservatively */
+ if (last) {
+ if (ji) {
+ DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); fprintf (logfile, "Mark(0): %s+0x%x (%p)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx)); g_free (fname));
+ }
+ DEBUG (fprintf (logfile, "\t <Last frame>\n"));
+ last = FALSE;
+ continue;
+ }
+
+ pc_offset = (guint8*)MONO_CONTEXT_GET_IP (&ctx) - (guint8*)ji->code_start;
+
+ /* These frames are very problematic */
+ if (ji->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
+ DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); fprintf (logfile, "Mark(0): %s+0x%x (%p)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx)); g_free (fname));
+ DEBUG (fprintf (logfile, "\tSkip.\n"));
+ continue;
+ }
+
+ /* All the other frames are at a call site */
+
+ if (tls->nframes == MAX_FRAMES) {
+ /*
+ * Can't save information since the array is full. So scan the rest of the
+ * stack conservatively.
+ */
+ DEBUG (fprintf (logfile, "Mark (0): Frame stack full.\n"));
+ break;
+ }
+
+ /* Scan the frame of this method */
+
+ /*
+ * A frame contains the following:
+ * - saved registers
+ * - saved args
+ * - locals
+ * - spill area
+ * - localloc-ed memory
+ */
+ g_assert (pc_offset >= 0);
+
+ emap = ji->gc_info;
+
+ if (!emap) {
+ DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); fprintf (logfile, "Mark(0): %s+0x%x (%p)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx)); g_free (fname));
+ DEBUG (fprintf (logfile, "\tNo GC Map.\n"));
+ continue;
+ }
+
+ /* The embedded callsite table requires this */
+ g_assert (((mgreg_t)emap % 4) == 0);
+
+ /*
+ * Debugging aid to control the number of frames scanned precisely
+ */
+ if (!precise_frame_limit_inited) {
+ if (getenv ("MONO_PRECISE_COUNT"))
+ precise_frame_limit = atoi (getenv ("MONO_PRECISE_COUNT"));
+ precise_frame_limit_inited = TRUE;
+ }
+
+ if (precise_frame_limit != -1) {
+ if (precise_frame_count [FALSE] == precise_frame_limit)
+ printf ("LAST PRECISE FRAME: %s\n", mono_method_full_name (ji->method, TRUE));
+ if (precise_frame_count [FALSE] > precise_frame_limit)
+ continue;
+ }
+ precise_frame_count [FALSE] ++;
+
+ /* Decode the encoded GC map */
+ map = &map_tmp;
+ memset (map, 0, sizeof (GCMap));
+ decode_gc_map (&emap->encoded [0], map, &p);
+ p = (guint8*)ALIGN_TO (p, map->callsite_entry_size);
+ map->callsites.offsets8 = p;
+ p += map->callsite_entry_size * map->ncallsites;
+ bitmaps = p;
+
+ fp = (guint8*)get_frame_pointer (&ctx, map->frame_reg);
+
+ real_frame_start = fp + map->start_offset;
+ frame_start = fp + map->start_offset + map->map_offset;
+ frame_end = fp + map->end_offset;
+
+ DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); fprintf (logfile, "Mark(0): %s+0x%x (%p) limit=%p fp=%p frame=%p-%p (%d)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx), stack_limit, fp, frame_start, frame_end, (int)(frame_end - frame_start)); g_free (fname));
+
+ /* Find the callsite index */
+ if (map->callsite_entry_size == 1) {
+ for (i = 0; i < map->ncallsites; ++i)
+ /* ip points inside the call instruction */
+ if (map->callsites.offsets8 [i] == pc_offset + 1)
+ break;
+ } else if (map->callsite_entry_size == 2) {
+ // FIXME: Use a binary search
+ for (i = 0; i < map->ncallsites; ++i)
+ /* ip points inside the call instruction */
+ if (map->callsites.offsets16 [i] == pc_offset + 1)
+ break;
+ } else {
+ // FIXME: Use a binary search
+ for (i = 0; i < map->ncallsites; ++i)
+ /* ip points inside the call instruction */
+ if (map->callsites.offsets32 [i] == pc_offset + 1)
+ break;
+ }
+ if (i == map->ncallsites) {
+ printf ("Unable to find ip offset 0x%x in callsite list of %s.\n", pc_offset + 1, mono_method_full_name (ji->method, TRUE));
+ g_assert_not_reached ();
+ }
+ cindex = i;
+
+ g_assert (real_frame_start >= stack_limit);
+
+ if (real_frame_start > stack_limit) {
+ /* This scans the previously skipped frames as well */
+ DEBUG (fprintf (logfile, "\tscan area %p-%p (%d).\n", stack_limit, real_frame_start, (int)(real_frame_start - stack_limit)));
+ mono_gc_conservatively_scan_area (stack_limit, real_frame_start);
+ stats.scanned_other += real_frame_start - stack_limit;
+ }
+
+ /* Mark stack slots */
+ if (map->has_pin_slots) {
+ int bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
+ guint8 *pin_bitmap = &bitmaps [map->stack_pin_bitmap_offset + (bitmap_width * cindex)];
+ guint8 *p;
+ gboolean pinned;
+
+ p = frame_start;
+ for (i = 0; i < map->nslots; ++i) {
+ pinned = pin_bitmap [i / 8] & (1 << (i % 8));
+ if (pinned) {
+ DEBUG (fprintf (logfile, "\tscan slot %s0x%x(fp)=%p.\n", (guint8*)p > (guint8*)fp ? "" : "-", ABS ((int)((gssize)p - (gssize)fp)), p));
+ mono_gc_conservatively_scan_area (p, p + sizeof (mgreg_t));
+ scanned_conservatively += sizeof (mgreg_t);
+ } else {
+ scanned_precisely += sizeof (mgreg_t);
+ }
+ p += sizeof (mgreg_t);
+ }
+ } else {
+ scanned_precisely += (map->nslots * sizeof (mgreg_t));
+ }
+
+ /* The area outside of start-end is NOREF */
+ scanned_precisely += (map->end_offset - map->start_offset) - (map->nslots * sizeof (mgreg_t));
+
+ /* Mark registers */
+ precise_regmask = map->used_int_regs | (1 << map->frame_reg);
+ if (map->has_pin_regs) {
+ int bitmap_width = ALIGN_TO (map->npin_regs, 8) / 8;
+ guint8 *pin_bitmap = &bitmaps [map->reg_pin_bitmap_offset + (bitmap_width * cindex)];
+ int bindex = 0;
+ for (i = 0; i < NREGS; ++i) {
+ if (!(map->used_int_regs & (1 << i)))
+ continue;
+
+ if (!(map->reg_pin_mask & (1 << i)))
+ continue;
+
+ if (pin_bitmap [bindex / 8] & (1 << (bindex % 8))) {
+ DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is pinning.\n", mono_arch_regname (i), reg_locations [i]));
+ precise_regmask &= ~(1 << i);
+ }
+ bindex ++;
+ }
+ }
+
+ scanned += map->end_offset - map->start_offset;
+
+ g_assert (scanned == scanned_precisely + scanned_conservatively);
+
+ stack_limit = frame_end;
+
+ /* Save information for the precise pass */
+ fi = &tls->frames [tls->nframes];
+ fi->nslots = map->nslots;
+ bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
+ if (map->has_ref_slots)
+ fi->bitmap = &bitmaps [map->stack_ref_bitmap_offset + (bitmap_width * cindex)];
+ else
+ fi->bitmap = NULL;
+ fi->frame_start_offset = frame_start - stack_start;
+ fi->nreg_locations = 0;
+ DEBUG_PRECISE (fi->ji = ji);
+ DEBUG_PRECISE (fi->fp = fp);
+
+ if (map->has_ref_regs) {
+ int bitmap_width = ALIGN_TO (map->nref_regs, 8) / 8;
+ guint8 *ref_bitmap = &bitmaps [map->reg_ref_bitmap_offset + (bitmap_width * cindex)];
+ int bindex = 0;
+ for (i = 0; i < NREGS; ++i) {
+ if (!(map->reg_ref_mask & (1 << i)))
+ continue;
+
+ if (reg_locations [i] && (ref_bitmap [bindex / 8] & (1 << (bindex % 8)))) {
+ DEBUG_PRECISE (fi->regs [fi->nreg_locations] = i);
+ DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is ref.\n", mono_arch_regname (i), reg_locations [i]));
+ fi->reg_locations [fi->nreg_locations] = (guint8*)reg_locations [i] - stack_start;
+ fi->nreg_locations ++;
+ }
+ bindex ++;
+ }
+ }
+
+ /*
+ * Clear locations of precisely stacked registers.
+ */
+ if (precise_regmask) {
+ for (i = 0; i < NREGS; ++i) {
+ if (precise_regmask & (1 << i)) {
+ /*
+ * The method uses this register, and we have precise info for it.
+ * This means the location will be scanned precisely.
+ * Tell the code at the beginning of the loop that this location is
+ * processed.
+ */
+ if (reg_locations [i])
+ DEBUG (fprintf (logfile, "\treg %s at location %p (==%p) is precise.\n", mono_arch_regname (i), reg_locations [i], (gpointer)*reg_locations [i]));
+ reg_locations [i] = NULL;
+ }
+ }
+ }
+
+ tls->nframes ++;
+ }
+
+ /* Scan the remaining register save locations */
+ for (i = 0; i < MONO_MAX_IREGS; ++i) {
+ if (reg_locations [i]) {
+ DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", reg_locations [i]));
+ mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
+ scanned_registers += sizeof (mgreg_t);
+ }
+ if (new_reg_locations [i]) {
+ DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", new_reg_locations [i]));
+ mono_gc_conservatively_scan_area (new_reg_locations [i], new_reg_locations [i] + sizeof (mgreg_t));
+ scanned_registers += sizeof (mgreg_t);
+ }
+ }
+
+ if (stack_limit < stack_end) {
+ DEBUG (fprintf (logfile, "\tscan remaining stack %p-%p (%d).\n", stack_limit, stack_end, (int)(stack_end - stack_limit)));
+ mono_gc_conservatively_scan_area (stack_limit, stack_end);
+ stats.scanned_native += stack_end - stack_limit;
+ }
+
+ DEBUG (fprintf (logfile, "Marked %d bytes, p=%d,c=%d out of %d.\n", scanned, scanned_precisely, scanned_conservatively, (int)(stack_end - stack_start)));
+
+ stats.scanned_stacks += stack_end - stack_start;
+ stats.scanned += scanned;
+ stats.scanned_precisely += scanned_precisely;
+ stats.scanned_conservatively += scanned_conservatively;
+ stats.scanned_registers += scanned_registers;
+
+ //mono_gc_conservatively_scan_area (stack_start, stack_end);
+}
+
+/*
+ * precise_pass:
+ *
+ * Mark a thread stack precisely based on information saved during the conservative
+ * pass.
+ */
+static void
+precise_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
+{
+ int findex, i;
+ FrameInfo *fi;
+ guint8 *frame_start;
+
+ if (!tls)
+ return;
+
+ for (findex = 0; findex < tls->nframes; findex ++) {
+ /* Load information saved by the !precise pass */
+ fi = &tls->frames [findex];
+ frame_start = stack_start + fi->frame_start_offset;
+
+ DEBUG (char *fname = mono_method_full_name (fi->ji->method, TRUE); fprintf (logfile, "Mark(1): %s\n", fname); g_free (fname));
+
+ /*
+ * FIXME: Add a function to mark using a bitmap, to avoid doing a
+ * call for each object.
+ */
+
+ /* Mark stack slots */
+ if (fi->bitmap) {
+ guint8 *ref_bitmap = fi->bitmap;
+ gboolean live;
+
+ for (i = 0; i < fi->nslots; ++i) {
+ MonoObject **ptr = (MonoObject**)(frame_start + (i * sizeof (mgreg_t)));
+
+ live = ref_bitmap [i / 8] & (1 << (i % 8));
+
+ if (live) {
+ MonoObject *obj = *ptr;
+ if (obj) {
+ DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p ->", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
+ *ptr = mono_gc_scan_object (obj);
+ DEBUG (fprintf (logfile, " %p.\n", *ptr));
+ } else {
+ DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p.\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
+ }
+ } else {
+#if 0
+ /*
+ * This is disabled because the pointer takes up a lot of space.
+ * Stack slots might be shared between ref and non-ref variables ?
+ */
+ if (map->ref_slots [i / 8] & (1 << (i % 8))) {
+ DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: dead (%p)\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, *ptr));
+ /*
+ * Fail fast if the live range is incorrect, and
+ * the JITted code tries to access this object
+ */
+ *ptr = DEAD_REF;
+ }
+#endif
+ }
+ }
+ }
+
+ /* Mark registers */
+
+ /*
+ * Registers are different from stack slots, they have no address where they
+ * are stored. Instead, some frame below this frame in the stack saves them
+ * in its prolog to the stack. We can mark this location precisely.
+ */
+ for (i = 0; i < fi->nreg_locations; ++i) {
+ /*
+ * reg_locations [i] contains the address of the stack slot where
+ * a reg was last saved, so mark that slot.
+ */
+ MonoObject **ptr = (MonoObject**)((guint8*)stack_start + fi->reg_locations [i]);
+ MonoObject *obj = *ptr;
+
+ if (obj) {
+ DEBUG (fprintf (logfile, "\treg %s saved at %p: %p ->", mono_arch_regname (fi->regs [i]), ptr, obj));
+ *ptr = mono_gc_scan_object (obj);
+ DEBUG (fprintf (logfile, " %p.\n", *ptr));
+ } else {
+ DEBUG (fprintf (logfile, "\treg %s saved at %p: %p\n", mono_arch_regname (fi->regs [i]), ptr, obj));
+ }
+ }
+ }
+
+ /*
+ * Debugging aid to check for missed refs.
+ */
+ if (tls->ref_to_track) {
+ mgreg_t *p;
+
+ for (p = (mgreg_t*)stack_start; p < (mgreg_t*)stack_end; ++p)
+ if (*p == (mgreg_t)tls->ref_to_track)
+ printf ("REF AT %p.\n", p);
+ }
+}
+
+/*
+ * thread_mark_func:
+ *
+ * This is called by the GC twice to mark a thread stack. PRECISE is FALSE at the first
+ * call, and TRUE at the second. USER_DATA points to a TlsData
+ * structure filled up by thread_suspend_func.
+ */
+static void
+thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise)
+{
+ TlsData *tls = user_data;
+
+ DEBUG (fprintf (logfile, "****************************************\n"));
+ DEBUG (fprintf (logfile, "*** %s stack marking for thread %p (%p-%p) ***\n", precise ? "Precise" : "Conservative", tls ? GUINT_TO_POINTER (tls->tid) : NULL, stack_start, stack_end));
+ DEBUG (fprintf (logfile, "****************************************\n"));
+
+ if (!precise)
+ conservative_pass (tls, stack_start, stack_end);
+ else
+ precise_pass (tls, stack_start, stack_end);
+}
+
+static void
+mini_gc_init_gc_map (MonoCompile *cfg)
+{
+ if (COMPILE_LLVM (cfg))
+ return;
+
+ if (!mono_gc_is_moving ())
+ return;
+
+ if (!cfg->compile_aot && !mono_gc_precise_stack_mark_enabled ())
+ return;
+
+#if 1
+ /* Debugging support */
+ {
+ static int precise_count;
+
+ precise_count ++;
+ if (getenv ("MONO_GCMAP_COUNT")) {
+ if (precise_count == atoi (getenv ("MONO_GCMAP_COUNT")))
+ printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
+ if (precise_count > atoi (getenv ("MONO_GCMAP_COUNT")))
+ return;
+ }
+ }
+#endif
+
+ cfg->compute_gc_maps = TRUE;
+
+ cfg->gc_info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoCompileGC));
+}
+
+/*
+ * mini_gc_set_slot_type_from_fp:
+ *
+ * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
+ * relative to the frame pointer. By default, all stack slots are type PIN, so there is no
+ * need to call this function for those slots.
+ */
+void
+mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
+{
+ MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
+
+ if (!cfg->compute_gc_maps)
+ return;
+
+ g_assert (slot_offset % sizeof (mgreg_t) == 0);
+
+ gcfg->stack_slots_from_fp = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_fp, GINT_TO_POINTER (((slot_offset) << 16) | type));
+}
+
+/*
+ * mini_gc_set_slot_type_from_cfa:
+ *
+ * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
+ * relative to the DWARF CFA value. This should be called from mono_arch_emit_prolog ().
+ * If type is STACK_REF, the slot is assumed to be live from the end of the prolog until
+ * the end of the method. By default, all stack slots are type PIN, so there is no need to
+ * call this function for those slots.
+ */
+void
+mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
+{
+ MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
+ int slot = - (slot_offset / sizeof (mgreg_t));
+
+ if (!cfg->compute_gc_maps)
+ return;
+
+ g_assert (slot_offset <= 0);
+ g_assert (slot_offset % sizeof (mgreg_t) == 0);
+
+ gcfg->stack_slots_from_cfa = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_cfa, GUINT_TO_POINTER (((slot) << 16) | type));
+}
+
+static inline int
+fp_offset_to_slot (MonoCompile *cfg, int offset)
+{
+ MonoCompileGC *gcfg = cfg->gc_info;
+
+ return (offset - gcfg->min_offset) / sizeof (mgreg_t);
+}
+
+static inline int
+slot_to_fp_offset (MonoCompile *cfg, int slot)
+{
+ MonoCompileGC *gcfg = cfg->gc_info;
+
+ return (slot * sizeof (mgreg_t)) + gcfg->min_offset;
+}
+
+static inline void
+set_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
+{
+ g_assert (slot >= 0 && slot < gcfg->nslots);
+
+ if (type == SLOT_PIN) {
+ clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
+ set_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
+ } else if (type == SLOT_REF) {
+ set_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
+ clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
+ } else if (type == SLOT_NOREF) {
+ clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
+ clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
+ }
+}
+
+static inline void
+set_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
+{
+ int cindex;
+
+ for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
+ set_slot (gcfg, slot, cindex, type);
+}
+
+static inline void
+set_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
+{
+ int cindex;
+
+ for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
+ int callsite_offset = gcfg->callsites [cindex]->pc_offset;
+ if (callsite_offset >= from && callsite_offset < to)
+ set_slot (gcfg, slot, cindex, type);
+ }
+}
+
+static inline void
+set_reg_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
+{
+ g_assert (slot >= 0 && slot < gcfg->nregs);
+
+ if (type == SLOT_PIN) {
+ clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
+ set_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
+ } else if (type == SLOT_REF) {
+ set_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
+ clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
+ } else if (type == SLOT_NOREF) {
+ clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
+ clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
+ }
+}
+
+static inline void
+set_reg_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
+{
+ int cindex;
+
+ for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
+ set_reg_slot (gcfg, slot, cindex, type);
+}
+
+static inline void
+set_reg_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
+{
+ int cindex;
+
+ for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
+ int callsite_offset = gcfg->callsites [cindex]->pc_offset;
+ if (callsite_offset >= from && callsite_offset < to)
+ set_reg_slot (gcfg, slot, cindex, type);
+ }
+}
+
+static void
+process_spill_slots (MonoCompile *cfg)
+{
+ MonoCompileGC *gcfg = cfg->gc_info;
+ MonoBasicBlock *bb;
+ GSList *l;
+ int i;
+
+ /* Mark all ref/pin spill slots as NOREF by default outside of their live range */
+ for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
+ for (l = bb->spill_slot_defs; l; l = l->next) {
+ MonoInst *def = l->data;
+ int spill_slot = def->inst_c0;
+ int bank = def->inst_c1;
+ int offset = cfg->spill_info [bank][spill_slot].offset;
+ int slot = fp_offset_to_slot (cfg, offset);
+
+ if (bank == MONO_REG_INT_MP || bank == MONO_REG_INT_REF)
+ set_slot_everywhere (gcfg, slot, SLOT_NOREF);
+ }
+ }
+
+ for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
+ for (l = bb->spill_slot_defs; l; l = l->next) {
+ MonoInst *def = l->data;
+ int spill_slot = def->inst_c0;
+ int bank = def->inst_c1;
+ int offset = cfg->spill_info [bank][spill_slot].offset;
+ int slot = fp_offset_to_slot (cfg, offset);
+ GCSlotType type;
+
+ if (bank == MONO_REG_INT_MP)
+ type = SLOT_PIN;
+ else
+ type = SLOT_REF;
+
+ /*
+ * Extend the live interval for the GC tracked spill slots
+ * defined in this bblock.
+ * FIXME: This is not needed.
+ */
+ set_slot_in_range (gcfg, slot, def->backend.pc_offset, bb->native_offset + bb->native_length, type);
+
+ if (cfg->verbose_level > 1)
+ printf ("\t%s spill slot at %s0x%x(fp) (slot = %d)\n", slot_type_to_string (type), offset >= 0 ? "" : "-", ABS (offset), slot);
+ }
+ }
+
+ /* Set fp spill slots to NOREF */
+ for (i = 0; i < cfg->spill_info_len [MONO_REG_DOUBLE]; ++i) {
+ int offset = cfg->spill_info [MONO_REG_DOUBLE][i].offset;
+ int slot;
+
+ if (offset == -1)
+ continue;
+
+ slot = fp_offset_to_slot (cfg, offset);
+
+ set_slot_everywhere (gcfg, slot, SLOT_NOREF);
+ /* FIXME: 32 bit */
+ if (cfg->verbose_level > 1)
+ printf ("\tfp spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
+ }
+
+ /* Set int spill slots to NOREF */
+ for (i = 0; i < cfg->spill_info_len [MONO_REG_INT]; ++i) {
+ int offset = cfg->spill_info [MONO_REG_INT][i].offset;
+ int slot;
+
+ if (offset == -1)
+ continue;
+
+ slot = fp_offset_to_slot (cfg, offset);
+
+ set_slot_everywhere (gcfg, slot, SLOT_NOREF);
+ if (cfg->verbose_level > 1)
+ printf ("\tint spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
+ }
+}
+
+/*
+ * process_other_slots:
+ *
+ * Process stack slots registered using mini_gc_set_slot_type_... ().
+ */
+static void
+process_other_slots (MonoCompile *cfg)
+{
+ MonoCompileGC *gcfg = cfg->gc_info;
+ GSList *l;
+
+ /* Relative to the CFA */
+ for (l = gcfg->stack_slots_from_cfa; l; l = l->next) {
+ guint data = GPOINTER_TO_UINT (l->data);
+ int cfa_slot = data >> 16;
+ GCSlotType type = data & 0xff;
+ int slot;
+
+ /*
+ * Map the cfa relative slot to an fp relative slot.
+ * slot_addr == cfa - <cfa_slot>*4/8
+ * fp + cfa_offset == cfa
+ * -> slot_addr == fp + (cfa_offset - <cfa_slot>*4/8)
+ */
+ slot = (cfg->cfa_offset / sizeof (mgreg_t)) - cfa_slot - (gcfg->min_offset / sizeof (mgreg_t));
+
+ set_slot_everywhere (gcfg, slot, type);
+
+ if (cfg->verbose_level > 1) {
+ int fp_offset = slot_to_fp_offset (cfg, slot);
+ if (type == SLOT_NOREF)
+ printf ("\tnoref slot at %s0x%x(fp) (slot = %d) (cfa - 0x%x)\n", fp_offset >= 0 ? "" : "-", ABS (fp_offset), slot, (int)(cfa_slot * sizeof (mgreg_t)));
+ }
+ }
+
+ /* Relative to the FP */
+ for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
+ gint data = GPOINTER_TO_INT (l->data);
+ int offset = data >> 16;
+ GCSlotType type = data & 0xff;
+ int slot;
+
+ slot = fp_offset_to_slot (cfg, offset);
+
+ set_slot_everywhere (gcfg, slot, type);
+
+ /* Liveness for these slots is handled by process_spill_slots () */
+
+ if (cfg->verbose_level > 1) {
+ if (type == SLOT_REF)
+ printf ("\tref slot at fp+0x%x (slot = %d)\n", offset, slot);
+ }
+ }
+}
+
+static void
+process_variables (MonoCompile *cfg)
+{
+ MonoCompileGC *gcfg = cfg->gc_info;
+ MonoMethodSignature *sig = mono_method_signature (cfg->method);
+ int i, locals_min_slot, locals_max_slot, cindex;
+ MonoBasicBlock *bb;
+ MonoInst *tmp;
+ int *pc_offsets;
+ int locals_min_offset = gcfg->locals_min_offset;
+ int locals_max_offset = gcfg->locals_max_offset;
+
+ /* Slots for locals are NOREF by default */
+ locals_min_slot = (locals_min_offset - gcfg->min_offset) / sizeof (mgreg_t);
+ locals_max_slot = (locals_max_offset - gcfg->min_offset) / sizeof (mgreg_t);
+ for (i = locals_min_slot; i < locals_max_slot; ++i) {
+ set_slot_everywhere (gcfg, i, SLOT_NOREF);
+ }
+
+ /*
+ * Compute the offset where variables are initialized in the first bblock, if any.
+ */
+ pc_offsets = g_new0 (int, cfg->next_vreg);
+
+ bb = cfg->bb_entry->next_bb;
+ MONO_BB_FOR_EACH_INS (bb, tmp) {
+ if (tmp->opcode == OP_GC_LIVENESS_DEF) {
+ int vreg = tmp->inst_c1;
+ if (pc_offsets [vreg] == 0) {
+ g_assert (tmp->backend.pc_offset > 0);
+ pc_offsets [vreg] = tmp->backend.pc_offset;
+ }
+ }
+ }
+
+ /*
+ * Stack slots holding arguments are initialized in the prolog.
+ * This means we can treat them alive for the whole method.
+ */
+ for (i = 0; i < cfg->num_varinfo; i++) {
+ MonoInst *ins = cfg->varinfo [i];
+ MonoType *t = ins->inst_vtype;
+ MonoMethodVar *vmv;
+ guint32 pos;
+ gboolean byref, is_this = FALSE;
+ gboolean is_arg = i < cfg->locals_start;
+
+ if (ins == cfg->ret)
+ continue;
+
+ vmv = MONO_VARINFO (cfg, i);
+
+ /* For some reason, 'this' is byref */
+ if (sig->hasthis && ins == cfg->args [0] && !cfg->method->klass->valuetype) {
+ t = &cfg->method->klass->byval_arg;
+ is_this = TRUE;
+ }
+
+ byref = t->byref;
+
+ if (ins->opcode == OP_REGVAR) {
+ int hreg;
+ GCSlotType slot_type;
+
+ t = mini_type_get_underlying_type (NULL, t);
+
+ hreg = ins->dreg;
+ g_assert (hreg < MONO_MAX_IREGS);
+
+ if (byref)
+ slot_type = SLOT_PIN;
+ else
+ slot_type = MONO_TYPE_IS_REFERENCE (t) ? SLOT_REF : SLOT_NOREF;
+
+ if (slot_type == SLOT_PIN) {
+ /* These have no live interval, be conservative */
+ set_reg_slot_everywhere (gcfg, hreg, slot_type);
+ } else {
+ /*
+ * Unlike variables allocated to the stack, we generate liveness info
+ * for noref vars in registers in mono_spill_global_vars (), because
+ * knowing that a register doesn't contain a ref allows us to mark its save
+ * locations precisely.
+ */
+ for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
+ if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
+ set_reg_slot (gcfg, hreg, cindex, slot_type);
+ }
+
+ if (cfg->verbose_level > 1) {
+ printf ("\t%s %sreg %s(R%d)\n", slot_type_to_string (slot_type), is_arg ? "arg " : "", mono_arch_regname (hreg), vmv->vreg);
+ }
+
+ continue;
+ }
+
+ if (ins->opcode != OP_REGOFFSET)
+ continue;
+
+ if (ins->inst_offset % sizeof (mgreg_t) != 0)
+ continue;
+
+ if (is_arg && ins->inst_offset >= gcfg->max_offset)
+ /* In parent frame */
+ continue;
+
+ pos = fp_offset_to_slot (cfg, ins->inst_offset);
+
+ if (is_arg && ins->flags & MONO_INST_IS_DEAD) {
+ /* These do not get stored in the prolog */
+ set_slot_everywhere (gcfg, pos, SLOT_NOREF);
+
+ if (cfg->verbose_level > 1) {
+ printf ("\tdead arg at fp%s0x%x (slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "+", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, pos, mono_type_full_name (ins->inst_vtype));
+ }
+ continue;
+ }
+
+ if (MONO_TYPE_ISSTRUCT (t)) {
+ int numbits = 0, j;
+ gsize *bitmap = NULL;
+ gboolean pin = FALSE;
+ int size;
+ int size_in_slots;
+
+ if (ins->backend.is_pinvoke)
+ size = mono_class_native_size (ins->klass, NULL);
+ else
+ size = mono_class_value_size (ins->klass, NULL);
+ size_in_slots = ALIGN_TO (size, sizeof (mgreg_t)) / sizeof (mgreg_t);
+
+ if (!ins->klass->has_references) {
+ if (is_arg) {
+ for (j = 0; j < size_in_slots; ++j)
+ set_slot_everywhere (gcfg, pos + j, SLOT_NOREF);
+ }
+ continue;
+ }
+
+ if (ins->klass->generic_container || mono_class_is_open_constructed_type (t)) {
+ /* FIXME: Generic sharing */
+ pin = TRUE;
+ } else {
+ mono_class_compute_gc_descriptor (ins->klass);
+
+ bitmap = mono_gc_get_bitmap_for_descr (ins->klass->gc_descr, &numbits);
+ if (!bitmap)
+ pin = TRUE;
+
+ /*
+ * Most vtypes are marked volatile because of the LDADDR instructions,
+ * and they have no liveness information since they are decomposed
+ * before the liveness pass. We emit OP_GC_LIVENESS_DEF instructions for
+ * them during VZERO decomposition.
+ */
+ if (!pc_offsets [vmv->vreg])
+ pin = TRUE;
+ }
+
+ if (ins->backend.is_pinvoke)
+ pin = TRUE;
+
+ if (cfg->verbose_level > 1)
+ printf ("\tvtype R%d at fp+0x%x-0x%x: %s\n", vmv->vreg, (int)ins->inst_offset, (int)(ins->inst_offset + (size / sizeof (mgreg_t))), mono_type_full_name (ins->inst_vtype));
+
+ if (bitmap) {
+ for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
+ if (gcfg->callsites [cindex]->pc_offset > pc_offsets [vmv->vreg]) {
+ for (j = 0; j < numbits; ++j) {
+ if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD))) {
+ /* The descriptor is for the boxed object */
+ set_slot (gcfg, (pos + j - (sizeof (MonoObject) / sizeof (mgreg_t))), cindex, pin ? SLOT_PIN : SLOT_REF);
+ }
+ }
+ }
+ }
+
+ if (cfg->verbose_level > 1) {
+ for (j = 0; j < numbits; ++j) {
+ if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD)))
+ printf ("\t\t%s slot at 0x%x(fp) (slot = %d)\n", pin ? "pin" : "ref", (int)(ins->inst_offset + (j * sizeof (mgreg_t))), (int)(pos + j - (sizeof (MonoObject) / sizeof (mgreg_t))));
+ }
+ }
+ } else {
+ if (cfg->verbose_level > 1)
+ printf ("\t\tpinned\n");
+ for (j = 0; j < size_in_slots; ++j) {
+ set_slot_everywhere (gcfg, pos + j, SLOT_PIN);
+ }
+ }