2 * mini-gc.c: GC interface for the mono JIT
5 * Zoltan Varga (vargaz@gmail.com)
7 * Copyright 2009 Novell, Inc (http://www.novell.com)
8 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
13 #include <mono/metadata/gc-internal.h>
16 #if defined(MONO_ARCH_GC_MAPS_SUPPORTED)
18 #include <mono/metadata/gc-internal.h>
19 #include <mono/utils/mono-counters.h>
21 #if SIZEOF_VOID_P == 4
22 typedef guint32 mword;
24 typedef guint64 mword;
27 #define SIZEOF_SLOT ((int)sizeof (mgreg_t))
29 #define GC_BITS_PER_WORD (sizeof (mword) * 8)
31 /* Contains state needed by the GC Map construction code */
34 * This contains information about stack slots initialized in the prolog, encoded using
35 * (slot_index << 16) | slot_type. The slot_index is relative to the CFA, i.e. 0
36 * means cfa+0, 1 means cfa-4/8, etc.
38 GSList *stack_slots_from_cfa;
39 /* Same for stack slots relative to the frame pointer */
40 GSList *stack_slots_from_fp;
42 /* Number of slots in the map */
44 /* The number of registers in the map */
46 /* Min and Max offsets of the stack frame relative to fp */
47 int min_offset, max_offset;
48 /* Same for the locals area */
49 int locals_min_offset, locals_max_offset;
51 /* The call sites where this frame can be stopped during GC */
52 GCCallSite **callsites;
53 /* The number of call sites */
57 * The width of the stack bitmaps in bytes. This is not equal to the bitmap width at
58 * runtime, since it includes columns which are 0.
60 int stack_bitmap_width;
62 * A bitmap whose width equals nslots, and whose height equals ncallsites.
63 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
66 guint8 *stack_ref_bitmap;
67 /* Same for SLOT_PIN */
68 guint8 *stack_pin_bitmap;
71 * Similar bitmaps for registers. These have width MONO_MAX_IREGS in bits.
74 guint8 *reg_ref_bitmap;
75 guint8 *reg_pin_bitmap;
78 #define ALIGN_TO(val,align) ((((mgreg_t)val) + ((align) - 1)) & ~((align) - 1))
83 /* We don't support debug levels, its all-or-nothing */
84 #define DEBUG(s) do { s; fflush (logfile); } while (0)
85 #define DEBUG_ENABLED 1
92 #define DEBUG_PRECISE(s) do { s; } while (0)
93 #define DEBUG_PRECISE_ENABLED
95 #define DEBUG_PRECISE(s)
99 * Contains information collected during the conservative stack marking pass,
100 * used during the precise pass. This helps to avoid doing a stack walk twice, which
106 int frame_start_offset;
108 /* Relative to stack_start */
109 int reg_locations [MONO_MAX_IREGS];
110 #ifdef DEBUG_PRECISE_ENABLED
113 int regs [MONO_MAX_IREGS];
117 /* Max number of frames stored in the TLS data */
118 #define MAX_FRAMES 50
121 * Per-thread data kept by this module. This is stored in the GC and passed to us as
122 * parameters, instead of being stored in a TLS variable, since during a collection,
123 * only the collection thread is active.
126 MonoThreadUnwindState unwind_state;
127 MonoThreadInfo *info;
130 gpointer ref_to_track;
131 /* Number of frames collected during the !precise pass */
133 FrameInfo frames [MAX_FRAMES];
136 /* These are constant so don't store them in the GC Maps */
137 /* Number of registers stored in gc maps */
138 #define NREGS MONO_MAX_IREGS
142 * Contains information needed to mark a stack frame.
143 * This is a transient structure, created from a compressed representation on-demand.
147 * The offsets of the GC tracked area inside the stack frame relative to the frame pointer.
148 * This includes memory which is NOREF thus doesn't need GC maps.
153 * The offset relative to frame_offset where the the memory described by the GC maps
157 /* The number of stack slots in the map */
159 /* The frame pointer register */
161 /* The size of each callsite table entry */
162 guint8 callsite_entry_size;
163 guint has_pin_slots : 1;
164 guint has_ref_slots : 1;
165 guint has_ref_regs : 1;
166 guint has_pin_regs : 1;
168 /* The offsets below are into an external bitmaps array */
171 * A bitmap whose width is equal to bitmap_width, and whose height is equal to ncallsites.
172 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
175 guint32 stack_ref_bitmap_offset;
177 * Same for SLOT_PIN. It is possible that the same bit is set in both bitmaps at
178 * different callsites, if the slot starts out as PIN, and later changes to REF.
180 guint32 stack_pin_bitmap_offset;
183 * Corresponding bitmaps for registers
184 * These have width equal to the number of bits set in reg_ref_mask/reg_pin_mask.
185 * FIXME: Merge these with the normal bitmaps, i.e. reserve the first x slots for them ?
187 guint32 reg_pin_bitmap_offset;
188 guint32 reg_ref_bitmap_offset;
190 guint32 used_int_regs, reg_ref_mask, reg_pin_mask;
192 /* The number of bits set in the two masks above */
193 guint8 nref_regs, npin_regs;
196 * A bit array marking slots which contain refs.
197 * This is used only for debugging.
201 /* Callsite offsets */
202 /* These can take up a lot of space, so encode them compactly */
212 * A compressed version of GCMap. This is what gets stored in MonoJitInfo.
216 //guint8 encoded_size;
219 * The arrays below are embedded after the struct.
220 * Their address needs to be computed.
223 /* The fixed fields of the GCMap encoded using LEB128 */
224 guint8 encoded [MONO_ZERO_LEN_ARRAY];
226 /* An array of ncallsites entries, each entry is callsite_entry_size bytes long */
227 guint8 callsites [MONO_ZERO_LEN_ARRAY];
230 guint8 bitmaps [MONO_ZERO_LEN_ARRAY];
233 static int precise_frame_count [2], precise_frame_limit = -1;
234 static gboolean precise_frame_limit_inited;
240 int scanned_precisely;
241 int scanned_conservatively;
242 int scanned_registers;
252 int gc_callsites_size;
253 int gc_callsites8_size;
254 int gc_callsites16_size;
255 int gc_callsites32_size;
257 int gc_map_struct_size;
261 static JITGCStats stats;
263 static FILE *logfile;
265 // FIXME: Move these to a shared place
268 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
273 guint8 b = value & 0x7f;
275 if (value != 0) /* more bytes to come */
283 static G_GNUC_UNUSED void
284 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
287 gboolean negative = (value < 0);
295 /* the following is unnecessary if the
296 * implementation of >>= uses an arithmetic rather
297 * than logical shift for a signed left operand
301 value |= - (1 <<(size - 7));
302 /* sign bit of byte is second high order bit (0x40) */
303 if ((value == 0 && !(byte & 0x40)) ||
304 (value == -1 && (byte & 0x40)))
314 static inline guint32
315 decode_uleb128 (guint8 *buf, guint8 **endbuf)
325 res = res | (((int)(b & 0x7f)) << shift);
337 decode_sleb128 (guint8 *buf, guint8 **endbuf)
347 res = res | (((int)(b & 0x7f)) << shift);
350 if (shift < 32 && (b & 0x40))
351 res |= - (1 << shift);
362 encode_frame_reg (int frame_reg)
365 if (frame_reg == AMD64_RSP)
367 else if (frame_reg == AMD64_RBP)
369 #elif defined(TARGET_X86)
370 if (frame_reg == X86_EBP)
372 else if (frame_reg == X86_ESP)
374 #elif defined(TARGET_ARM)
375 if (frame_reg == ARMREG_SP)
377 else if (frame_reg == ARMREG_FP)
379 #elif defined(TARGET_S390X)
380 if (frame_reg == S390_SP)
382 else if (frame_reg == S390_FP)
387 g_assert_not_reached ();
392 decode_frame_reg (int encoded)
397 else if (encoded == 1)
399 #elif defined(TARGET_X86)
402 else if (encoded == 1)
404 #elif defined(TARGET_ARM)
407 else if (encoded == 1)
409 #elif defined(TARGET_S390X)
412 else if (encoded == 1)
417 g_assert_not_reached ();
423 static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15, AMD64_RDI, AMD64_RSI };
425 static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15 };
427 #elif defined(TARGET_X86)
428 static int callee_saved_regs [] = { X86_EBX, X86_ESI, X86_EDI };
429 #elif defined(TARGET_ARM)
430 static int callee_saved_regs [] = { ARMREG_V1, ARMREG_V2, ARMREG_V3, ARMREG_V4, ARMREG_V5, ARMREG_V7, ARMREG_FP };
431 #elif defined(TARGET_S390X)
432 static int callee_saved_regs [] = { s390_r6, s390_r7, s390_r8, s390_r9, s390_r10, s390_r11, s390_r12, s390_r13, s390_r14 };
436 encode_regmask (guint32 regmask)
442 for (i = 0; i < sizeof (callee_saved_regs) / sizeof (int); ++i) {
443 if (regmask & (1 << callee_saved_regs [i])) {
445 regmask -= (1 << callee_saved_regs [i]);
448 g_assert (regmask == 0);
453 decode_regmask (guint32 regmask)
459 for (i = 0; i < sizeof (callee_saved_regs) / sizeof (int); ++i)
460 if (regmask & (1 << i))
461 res |= (1 << callee_saved_regs [i]);
468 * Encode the fixed fields of MAP into a buffer pointed to by BUF.
471 encode_gc_map (GCMap *map, guint8 *buf, guint8 **endbuf)
475 encode_sleb128 (map->start_offset / SIZEOF_SLOT, buf, &buf);
476 encode_sleb128 (map->end_offset / SIZEOF_SLOT, buf, &buf);
477 encode_sleb128 (map->map_offset / SIZEOF_SLOT, buf, &buf);
478 encode_uleb128 (map->nslots, buf, &buf);
479 g_assert (map->callsite_entry_size <= 4);
480 freg = encode_frame_reg (map->frame_reg);
482 flags = (map->has_ref_slots ? 1 : 0) | (map->has_pin_slots ? 2 : 0) | (map->has_ref_regs ? 4 : 0) | (map->has_pin_regs ? 8 : 0) | ((map->callsite_entry_size - 1) << 4) | (freg << 6);
483 encode_uleb128 (flags, buf, &buf);
484 encode_uleb128 (encode_regmask (map->used_int_regs), buf, &buf);
485 if (map->has_ref_regs)
486 encode_uleb128 (encode_regmask (map->reg_ref_mask), buf, &buf);
487 if (map->has_pin_regs)
488 encode_uleb128 (encode_regmask (map->reg_pin_mask), buf, &buf);
489 encode_uleb128 (map->ncallsites, buf, &buf);
497 * Decode the encoded GC map representation in BUF and store the result into MAP.
500 decode_gc_map (guint8 *buf, GCMap *map, guint8 **endbuf)
503 int stack_bitmap_size, reg_ref_bitmap_size, reg_pin_bitmap_size, offset, freg;
506 map->start_offset = decode_sleb128 (buf, &buf) * SIZEOF_SLOT;
507 map->end_offset = decode_sleb128 (buf, &buf) * SIZEOF_SLOT;
508 map->map_offset = decode_sleb128 (buf, &buf) * SIZEOF_SLOT;
509 map->nslots = decode_uleb128 (buf, &buf);
510 flags = decode_uleb128 (buf, &buf);
511 map->has_ref_slots = (flags & 1) ? 1 : 0;
512 map->has_pin_slots = (flags & 2) ? 1 : 0;
513 map->has_ref_regs = (flags & 4) ? 1 : 0;
514 map->has_pin_regs = (flags & 8) ? 1 : 0;
515 map->callsite_entry_size = ((flags >> 4) & 0x3) + 1;
517 map->frame_reg = decode_frame_reg (freg);
518 map->used_int_regs = decode_regmask (decode_uleb128 (buf, &buf));
519 if (map->has_ref_regs) {
520 map->reg_ref_mask = decode_regmask (decode_uleb128 (buf, &buf));
522 for (i = 0; i < NREGS; ++i)
523 if (map->reg_ref_mask & (1 << i))
527 if (map->has_pin_regs) {
528 map->reg_pin_mask = decode_regmask (decode_uleb128 (buf, &buf));
530 for (i = 0; i < NREGS; ++i)
531 if (map->reg_pin_mask & (1 << i))
535 map->ncallsites = decode_uleb128 (buf, &buf);
537 stack_bitmap_size = (ALIGN_TO (map->nslots, 8) / 8) * map->ncallsites;
538 reg_ref_bitmap_size = (ALIGN_TO (map->nref_regs, 8) / 8) * map->ncallsites;
539 reg_pin_bitmap_size = (ALIGN_TO (map->npin_regs, 8) / 8) * map->ncallsites;
541 map->stack_ref_bitmap_offset = offset;
542 if (map->has_ref_slots)
543 offset += stack_bitmap_size;
544 map->stack_pin_bitmap_offset = offset;
545 if (map->has_pin_slots)
546 offset += stack_bitmap_size;
547 map->reg_ref_bitmap_offset = offset;
548 if (map->has_ref_regs)
549 offset += reg_ref_bitmap_size;
550 map->reg_pin_bitmap_offset = offset;
551 if (map->has_pin_regs)
552 offset += reg_pin_bitmap_size;
558 thread_attach_func (void)
562 tls = g_new0 (TlsData, 1);
563 tls->tid = GetCurrentThreadId ();
564 tls->info = mono_thread_info_current ();
565 stats.tlsdata_size += sizeof (TlsData);
571 thread_detach_func (gpointer user_data)
573 TlsData *tls = user_data;
579 thread_suspend_func (gpointer user_data, void *sigctx, MonoContext *ctx)
581 TlsData *tls = user_data;
584 /* Happens during startup */
585 tls->unwind_state.valid = FALSE;
589 if (tls->tid != GetCurrentThreadId ()) {
590 /* Happens on osx because threads are not suspended using signals */
593 g_assert (tls->info);
594 res = mono_thread_state_init_from_handle (&tls->unwind_state, (MonoNativeThreadId)tls->tid, tls->info->native_handle);
596 tls->unwind_state.unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
598 mono_arch_sigctx_to_monoctx (sigctx, &tls->unwind_state.ctx);
599 tls->unwind_state.valid = TRUE;
601 memcpy (&tls->unwind_state.ctx, ctx, sizeof (MonoContext));
602 tls->unwind_state.valid = TRUE;
604 tls->unwind_state.valid = FALSE;
606 tls->unwind_state.unwind_data [MONO_UNWIND_DATA_JIT_TLS] = mono_native_tls_get_value (mono_jit_tls_id);
607 tls->unwind_state.unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
610 if (!tls->unwind_state.unwind_data [MONO_UNWIND_DATA_DOMAIN]) {
611 /* Happens during startup */
612 tls->unwind_state.valid = FALSE;
617 #define DEAD_REF ((gpointer)(gssize)0x2a2a2a2a2a2a2a2aULL)
620 set_bit (guint8 *bitmap, int width, int y, int x)
622 bitmap [(width * y) + (x / 8)] |= (1 << (x % 8));
626 clear_bit (guint8 *bitmap, int width, int y, int x)
628 bitmap [(width * y) + (x / 8)] &= ~(1 << (x % 8));
632 get_bit (guint8 *bitmap, int width, int y, int x)
634 return bitmap [(width * y) + (x / 8)] & (1 << (x % 8));
638 slot_type_to_string (GCSlotType type)
648 g_assert_not_reached ();
653 static inline mgreg_t
654 get_frame_pointer (MonoContext *ctx, int frame_reg)
656 #if defined(TARGET_AMD64)
657 if (frame_reg == AMD64_RSP)
659 else if (frame_reg == AMD64_RBP)
661 #elif defined(TARGET_X86)
662 if (frame_reg == X86_ESP)
664 else if (frame_reg == X86_EBP)
666 #elif defined(TARGET_ARM)
667 if (frame_reg == ARMREG_SP)
668 return (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
669 else if (frame_reg == ARMREG_FP)
670 return (mgreg_t)MONO_CONTEXT_GET_BP (ctx);
671 #elif defined(TARGET_S390X)
672 if (frame_reg == S390_SP)
673 return (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
674 else if (frame_reg == S390_FP)
675 return (mgreg_t)MONO_CONTEXT_GET_BP (ctx);
677 g_assert_not_reached ();
682 * conservatively_pass:
684 * Mark a thread stack conservatively and collect information needed by the precise pass.
687 conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
690 MonoContext ctx, new_ctx;
693 gboolean last = TRUE;
697 guint8* fp, *p, *real_frame_start, *frame_start, *frame_end;
698 int i, pc_offset, cindex, bitmap_width;
699 int scanned = 0, scanned_precisely, scanned_conservatively, scanned_registers;
701 StackFrameInfo frame;
702 mgreg_t *reg_locations [MONO_MAX_IREGS];
703 mgreg_t *new_reg_locations [MONO_MAX_IREGS];
706 guint32 precise_regmask;
710 tls->ref_to_track = NULL;
713 /* tls == NULL can happen during startup */
714 if (mono_thread_internal_current () == NULL || !tls) {
715 mono_gc_conservatively_scan_area (stack_start, stack_end);
716 stats.scanned_stacks += stack_end - stack_start;
720 lmf = tls->unwind_state.unwind_data [MONO_UNWIND_DATA_LMF];
723 /* Number of bytes scanned based on GC map data */
725 /* Number of bytes scanned precisely based on GC map data */
726 scanned_precisely = 0;
727 /* Number of bytes scanned conservatively based on GC map data */
728 scanned_conservatively = 0;
729 /* Number of bytes scanned conservatively in register save areas */
730 scanned_registers = 0;
732 /* This is one past the last address which we have scanned */
733 stack_limit = stack_start;
735 if (!tls->unwind_state.valid)
736 memset (&new_ctx, 0, sizeof (ctx));
738 memcpy (&new_ctx, &tls->unwind_state.ctx, sizeof (MonoContext));
740 memset (reg_locations, 0, sizeof (reg_locations));
741 memset (new_reg_locations, 0, sizeof (new_reg_locations));
744 if (!tls->unwind_state.valid)
747 memcpy (&ctx, &new_ctx, sizeof (ctx));
749 for (i = 0; i < MONO_MAX_IREGS; ++i) {
750 if (new_reg_locations [i]) {
752 * If the current frame saves the register, it means it might modify its
753 * value, thus the old location might not contain the same value, so
754 * we have to mark it conservatively.
756 if (reg_locations [i]) {
757 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
758 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
759 scanned_registers += SIZEOF_SLOT;
762 reg_locations [i] = new_reg_locations [i];
764 DEBUG (fprintf (logfile, "\treg %s is now at location %p.\n", mono_arch_regname (i), reg_locations [i]));
768 g_assert ((mgreg_t)stack_limit % SIZEOF_SLOT == 0);
770 res = mono_find_jit_info_ext (frame.domain ? frame.domain : tls->unwind_state.unwind_data [MONO_UNWIND_DATA_DOMAIN], tls->unwind_state.unwind_data [MONO_UNWIND_DATA_JIT_TLS], NULL, &ctx, &new_ctx, NULL, &lmf, new_reg_locations, &frame);
776 if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE) {
778 * These frames are problematic for several reasons:
779 * - they are unwound through an LMF, and we have no precise register tracking for those.
780 * - the LMF might not contain a precise ip, so we can't compute the call site.
781 * - the LMF only unwinds to the wrapper frame, so we get these methods twice.
783 DEBUG (fprintf (logfile, "Mark(0): <Managed-to-native transition>\n"));
784 for (i = 0; i < MONO_MAX_IREGS; ++i) {
785 if (reg_locations [i]) {
786 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
787 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
788 scanned_registers += SIZEOF_SLOT;
790 reg_locations [i] = NULL;
791 new_reg_locations [i] = NULL;
797 /* The last frame can be in any state so mark conservatively */
800 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));
802 DEBUG (fprintf (logfile, "\t <Last frame>\n"));
805 * new_reg_locations is not precise when a method is interrupted during its epilog, so clear it.
807 for (i = 0; i < MONO_MAX_IREGS; ++i) {
808 if (reg_locations [i]) {
809 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
810 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
811 scanned_registers += SIZEOF_SLOT;
813 reg_locations [i] = NULL;
814 new_reg_locations [i] = NULL;
819 pc_offset = (guint8*)MONO_CONTEXT_GET_IP (&ctx) - (guint8*)ji->code_start;
821 /* These frames are very problematic */
822 if (ji->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
823 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));
824 DEBUG (fprintf (logfile, "\tSkip.\n"));
828 /* All the other frames are at a call site */
830 if (tls->nframes == MAX_FRAMES) {
832 * Can't save information since the array is full. So scan the rest of the
833 * stack conservatively.
835 DEBUG (fprintf (logfile, "Mark (0): Frame stack full.\n"));
839 /* Scan the frame of this method */
842 * A frame contains the following:
847 * - localloc-ed memory
849 g_assert (pc_offset >= 0);
854 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));
855 DEBUG (fprintf (logfile, "\tNo GC Map.\n"));
859 /* The embedded callsite table requires this */
860 g_assert (((mgreg_t)emap % 4) == 0);
863 * Debugging aid to control the number of frames scanned precisely
865 if (!precise_frame_limit_inited) {
866 if (getenv ("MONO_PRECISE_COUNT"))
867 precise_frame_limit = atoi (getenv ("MONO_PRECISE_COUNT"));
868 precise_frame_limit_inited = TRUE;
871 if (precise_frame_limit != -1) {
872 if (precise_frame_count [FALSE] == precise_frame_limit)
873 printf ("LAST PRECISE FRAME: %s\n", mono_method_full_name (ji->method, TRUE));
874 if (precise_frame_count [FALSE] > precise_frame_limit)
877 precise_frame_count [FALSE] ++;
879 /* Decode the encoded GC map */
881 memset (map, 0, sizeof (GCMap));
882 decode_gc_map (&emap->encoded [0], map, &p);
883 p = (guint8*)ALIGN_TO (p, map->callsite_entry_size);
884 map->callsites.offsets8 = p;
885 p += map->callsite_entry_size * map->ncallsites;
888 fp = (guint8*)get_frame_pointer (&ctx, map->frame_reg);
890 real_frame_start = fp + map->start_offset;
891 frame_start = fp + map->start_offset + map->map_offset;
892 frame_end = fp + map->end_offset;
894 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));
896 /* Find the callsite index */
897 if (map->callsite_entry_size == 1) {
898 for (i = 0; i < map->ncallsites; ++i)
899 /* ip points inside the call instruction */
900 if (map->callsites.offsets8 [i] == pc_offset + 1)
902 } else if (map->callsite_entry_size == 2) {
903 // FIXME: Use a binary search
904 for (i = 0; i < map->ncallsites; ++i)
905 /* ip points inside the call instruction */
906 if (map->callsites.offsets16 [i] == pc_offset + 1)
909 // FIXME: Use a binary search
910 for (i = 0; i < map->ncallsites; ++i)
911 /* ip points inside the call instruction */
912 if (map->callsites.offsets32 [i] == pc_offset + 1)
915 if (i == map->ncallsites) {
916 printf ("Unable to find ip offset 0x%x in callsite list of %s.\n", pc_offset + 1, mono_method_full_name (ji->method, TRUE));
917 g_assert_not_reached ();
922 * This is not neccessary true on x86 because frames have a different size at each
925 //g_assert (real_frame_start >= stack_limit);
927 if (real_frame_start > stack_limit) {
928 /* This scans the previously skipped frames as well */
929 DEBUG (fprintf (logfile, "\tscan area %p-%p (%d).\n", stack_limit, real_frame_start, (int)(real_frame_start - stack_limit)));
930 mono_gc_conservatively_scan_area (stack_limit, real_frame_start);
931 stats.scanned_other += real_frame_start - stack_limit;
934 /* Mark stack slots */
935 if (map->has_pin_slots) {
936 int bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
937 guint8 *pin_bitmap = &bitmaps [map->stack_pin_bitmap_offset + (bitmap_width * cindex)];
942 for (i = 0; i < map->nslots; ++i) {
943 pinned = pin_bitmap [i / 8] & (1 << (i % 8));
945 DEBUG (fprintf (logfile, "\tscan slot %s0x%x(fp)=%p.\n", (guint8*)p > (guint8*)fp ? "" : "-", ABS ((int)((gssize)p - (gssize)fp)), p));
946 mono_gc_conservatively_scan_area (p, p + SIZEOF_SLOT);
947 scanned_conservatively += SIZEOF_SLOT;
949 scanned_precisely += SIZEOF_SLOT;
954 scanned_precisely += (map->nslots * SIZEOF_SLOT);
957 /* The area outside of start-end is NOREF */
958 scanned_precisely += (map->end_offset - map->start_offset) - (map->nslots * SIZEOF_SLOT);
961 precise_regmask = map->used_int_regs | (1 << map->frame_reg);
962 if (map->has_pin_regs) {
963 int bitmap_width = ALIGN_TO (map->npin_regs, 8) / 8;
964 guint8 *pin_bitmap = &bitmaps [map->reg_pin_bitmap_offset + (bitmap_width * cindex)];
966 for (i = 0; i < NREGS; ++i) {
967 if (!(map->used_int_regs & (1 << i)))
970 if (!(map->reg_pin_mask & (1 << i)))
973 if (pin_bitmap [bindex / 8] & (1 << (bindex % 8))) {
974 DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is pinning.\n", mono_arch_regname (i), reg_locations [i]));
975 precise_regmask &= ~(1 << i);
981 scanned += map->end_offset - map->start_offset;
983 g_assert (scanned == scanned_precisely + scanned_conservatively);
985 stack_limit = frame_end;
987 /* Save information for the precise pass */
988 fi = &tls->frames [tls->nframes];
989 fi->nslots = map->nslots;
990 bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
991 if (map->has_ref_slots)
992 fi->bitmap = &bitmaps [map->stack_ref_bitmap_offset + (bitmap_width * cindex)];
995 fi->frame_start_offset = frame_start - stack_start;
996 fi->nreg_locations = 0;
997 DEBUG_PRECISE (fi->ji = ji);
998 DEBUG_PRECISE (fi->fp = fp);
1000 if (map->has_ref_regs) {
1001 int bitmap_width = ALIGN_TO (map->nref_regs, 8) / 8;
1002 guint8 *ref_bitmap = &bitmaps [map->reg_ref_bitmap_offset + (bitmap_width * cindex)];
1004 for (i = 0; i < NREGS; ++i) {
1005 if (!(map->reg_ref_mask & (1 << i)))
1008 if (reg_locations [i] && (ref_bitmap [bindex / 8] & (1 << (bindex % 8)))) {
1009 DEBUG_PRECISE (fi->regs [fi->nreg_locations] = i);
1010 DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is ref.\n", mono_arch_regname (i), reg_locations [i]));
1011 fi->reg_locations [fi->nreg_locations] = (guint8*)reg_locations [i] - stack_start;
1012 fi->nreg_locations ++;
1019 * Clear locations of precisely stacked registers.
1021 if (precise_regmask) {
1022 for (i = 0; i < NREGS; ++i) {
1023 if (precise_regmask & (1 << i)) {
1025 * The method uses this register, and we have precise info for it.
1026 * This means the location will be scanned precisely.
1027 * Tell the code at the beginning of the loop that this location is
1030 if (reg_locations [i])
1031 DEBUG (fprintf (logfile, "\treg %s at location %p (==%p) is precise.\n", mono_arch_regname (i), reg_locations [i], (gpointer)*reg_locations [i]));
1032 reg_locations [i] = NULL;
1040 /* Scan the remaining register save locations */
1041 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1042 if (reg_locations [i]) {
1043 DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", reg_locations [i]));
1044 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
1045 scanned_registers += SIZEOF_SLOT;
1047 if (new_reg_locations [i]) {
1048 DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", new_reg_locations [i]));
1049 mono_gc_conservatively_scan_area (new_reg_locations [i], (char*)new_reg_locations [i] + SIZEOF_SLOT);
1050 scanned_registers += SIZEOF_SLOT;
1054 if (stack_limit < stack_end) {
1055 DEBUG (fprintf (logfile, "\tscan remaining stack %p-%p (%d).\n", stack_limit, stack_end, (int)(stack_end - stack_limit)));
1056 mono_gc_conservatively_scan_area (stack_limit, stack_end);
1057 stats.scanned_native += stack_end - stack_limit;
1060 DEBUG (fprintf (logfile, "Marked %d bytes, p=%d,c=%d out of %d.\n", scanned, scanned_precisely, scanned_conservatively, (int)(stack_end - stack_start)));
1062 stats.scanned_stacks += stack_end - stack_start;
1063 stats.scanned += scanned;
1064 stats.scanned_precisely += scanned_precisely;
1065 stats.scanned_conservatively += scanned_conservatively;
1066 stats.scanned_registers += scanned_registers;
1068 //mono_gc_conservatively_scan_area (stack_start, stack_end);
1074 * Mark a thread stack precisely based on information saved during the conservative
1078 precise_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
1082 guint8 *frame_start;
1087 if (!tls->unwind_state.valid)
1090 for (findex = 0; findex < tls->nframes; findex ++) {
1091 /* Load information saved by the !precise pass */
1092 fi = &tls->frames [findex];
1093 frame_start = stack_start + fi->frame_start_offset;
1095 DEBUG (char *fname = mono_method_full_name (fi->ji->method, TRUE); fprintf (logfile, "Mark(1): %s\n", fname); g_free (fname));
1098 * FIXME: Add a function to mark using a bitmap, to avoid doing a
1099 * call for each object.
1102 /* Mark stack slots */
1104 guint8 *ref_bitmap = fi->bitmap;
1107 for (i = 0; i < fi->nslots; ++i) {
1108 MonoObject **ptr = (MonoObject**)(frame_start + (i * SIZEOF_SLOT));
1110 live = ref_bitmap [i / 8] & (1 << (i % 8));
1113 MonoObject *obj = *ptr;
1115 DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p ->", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
1116 *ptr = mono_gc_scan_object (obj);
1117 DEBUG (fprintf (logfile, " %p.\n", *ptr));
1119 DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p.\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
1124 * This is disabled because the pointer takes up a lot of space.
1125 * Stack slots might be shared between ref and non-ref variables ?
1127 if (map->ref_slots [i / 8] & (1 << (i % 8))) {
1128 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));
1130 * Fail fast if the live range is incorrect, and
1131 * the JITted code tries to access this object
1140 /* Mark registers */
1143 * Registers are different from stack slots, they have no address where they
1144 * are stored. Instead, some frame below this frame in the stack saves them
1145 * in its prolog to the stack. We can mark this location precisely.
1147 for (i = 0; i < fi->nreg_locations; ++i) {
1149 * reg_locations [i] contains the address of the stack slot where
1150 * a reg was last saved, so mark that slot.
1152 MonoObject **ptr = (MonoObject**)((guint8*)stack_start + fi->reg_locations [i]);
1153 MonoObject *obj = *ptr;
1156 DEBUG (fprintf (logfile, "\treg %s saved at %p: %p ->", mono_arch_regname (fi->regs [i]), ptr, obj));
1157 *ptr = mono_gc_scan_object (obj);
1158 DEBUG (fprintf (logfile, " %p.\n", *ptr));
1160 DEBUG (fprintf (logfile, "\treg %s saved at %p: %p\n", mono_arch_regname (fi->regs [i]), ptr, obj));
1166 * Debugging aid to check for missed refs.
1168 if (tls->ref_to_track) {
1171 for (p = (mgreg_t*)stack_start; p < (mgreg_t*)stack_end; ++p)
1172 if (*p == (mgreg_t)tls->ref_to_track)
1173 printf ("REF AT %p.\n", p);
1180 * This is called by the GC twice to mark a thread stack. PRECISE is FALSE at the first
1181 * call, and TRUE at the second. USER_DATA points to a TlsData
1182 * structure filled up by thread_suspend_func.
1185 thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise)
1187 TlsData *tls = user_data;
1189 DEBUG (fprintf (logfile, "****************************************\n"));
1190 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));
1191 DEBUG (fprintf (logfile, "****************************************\n"));
1194 conservative_pass (tls, stack_start, stack_end);
1196 precise_pass (tls, stack_start, stack_end);
1200 mini_gc_init_gc_map (MonoCompile *cfg)
1202 if (COMPILE_LLVM (cfg))
1205 if (!mono_gc_is_moving ())
1208 if (!cfg->compile_aot && !mono_gc_precise_stack_mark_enabled ())
1212 /* Debugging support */
1214 static int precise_count;
1217 if (getenv ("MONO_GCMAP_COUNT")) {
1218 if (precise_count == atoi (getenv ("MONO_GCMAP_COUNT")))
1219 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
1220 if (precise_count > atoi (getenv ("MONO_GCMAP_COUNT")))
1226 cfg->compute_gc_maps = TRUE;
1228 cfg->gc_info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoCompileGC));
1232 * mini_gc_set_slot_type_from_fp:
1234 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1235 * relative to the frame pointer. By default, all stack slots are type PIN, so there is no
1236 * need to call this function for those slots.
1239 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
1241 MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
1243 if (!cfg->compute_gc_maps)
1246 g_assert (slot_offset % SIZEOF_SLOT == 0);
1248 gcfg->stack_slots_from_fp = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_fp, GINT_TO_POINTER (((slot_offset) << 16) | type));
1252 * mini_gc_set_slot_type_from_cfa:
1254 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1255 * relative to the DWARF CFA value. This should be called from mono_arch_emit_prolog ().
1256 * If type is STACK_REF, the slot is assumed to be live from the end of the prolog until
1257 * the end of the method. By default, all stack slots are type PIN, so there is no need to
1258 * call this function for those slots.
1261 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
1263 MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
1264 int slot = - (slot_offset / SIZEOF_SLOT);
1266 if (!cfg->compute_gc_maps)
1269 g_assert (slot_offset <= 0);
1270 g_assert (slot_offset % SIZEOF_SLOT == 0);
1272 gcfg->stack_slots_from_cfa = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_cfa, GUINT_TO_POINTER (((slot) << 16) | type));
1276 fp_offset_to_slot (MonoCompile *cfg, int offset)
1278 MonoCompileGC *gcfg = cfg->gc_info;
1280 return (offset - gcfg->min_offset) / SIZEOF_SLOT;
1284 slot_to_fp_offset (MonoCompile *cfg, int slot)
1286 MonoCompileGC *gcfg = cfg->gc_info;
1288 return (slot * SIZEOF_SLOT) + gcfg->min_offset;
1292 set_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1294 g_assert (slot >= 0 && slot < gcfg->nslots);
1296 if (type == SLOT_PIN) {
1297 clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1298 set_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1299 } else if (type == SLOT_REF) {
1300 set_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1301 clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1302 } else if (type == SLOT_NOREF) {
1303 clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1304 clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1309 set_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1313 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1314 set_slot (gcfg, slot, cindex, type);
1318 set_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1322 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1323 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1324 if (callsite_offset >= from && callsite_offset < to)
1325 set_slot (gcfg, slot, cindex, type);
1330 set_reg_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1332 g_assert (slot >= 0 && slot < gcfg->nregs);
1334 if (type == SLOT_PIN) {
1335 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1336 set_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1337 } else if (type == SLOT_REF) {
1338 set_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1339 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1340 } else if (type == SLOT_NOREF) {
1341 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1342 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1347 set_reg_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1351 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1352 set_reg_slot (gcfg, slot, cindex, type);
1356 set_reg_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1360 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1361 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1362 if (callsite_offset >= from && callsite_offset < to)
1363 set_reg_slot (gcfg, slot, cindex, type);
1368 process_spill_slots (MonoCompile *cfg)
1370 MonoCompileGC *gcfg = cfg->gc_info;
1375 /* Mark all ref/pin spill slots as NOREF by default outside of their live range */
1376 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1377 for (l = bb->spill_slot_defs; l; l = l->next) {
1378 MonoInst *def = l->data;
1379 int spill_slot = def->inst_c0;
1380 int bank = def->inst_c1;
1381 int offset = cfg->spill_info [bank][spill_slot].offset;
1382 int slot = fp_offset_to_slot (cfg, offset);
1384 if (bank == MONO_REG_INT_MP || bank == MONO_REG_INT_REF)
1385 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1389 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1390 for (l = bb->spill_slot_defs; l; l = l->next) {
1391 MonoInst *def = l->data;
1392 int spill_slot = def->inst_c0;
1393 int bank = def->inst_c1;
1394 int offset = cfg->spill_info [bank][spill_slot].offset;
1395 int slot = fp_offset_to_slot (cfg, offset);
1398 if (bank == MONO_REG_INT_MP)
1404 * Extend the live interval for the GC tracked spill slots
1405 * defined in this bblock.
1406 * FIXME: This is not needed.
1408 set_slot_in_range (gcfg, slot, def->backend.pc_offset, bb->native_offset + bb->native_length, type);
1410 if (cfg->verbose_level > 1)
1411 printf ("\t%s spill slot at %s0x%x(fp) (slot = %d)\n", slot_type_to_string (type), offset >= 0 ? "" : "-", ABS (offset), slot);
1415 /* Set fp spill slots to NOREF */
1416 for (i = 0; i < cfg->spill_info_len [MONO_REG_DOUBLE]; ++i) {
1417 int offset = cfg->spill_info [MONO_REG_DOUBLE][i].offset;
1423 slot = fp_offset_to_slot (cfg, offset);
1425 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1427 if (cfg->verbose_level > 1)
1428 printf ("\tfp spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
1431 /* Set int spill slots to NOREF */
1432 for (i = 0; i < cfg->spill_info_len [MONO_REG_INT]; ++i) {
1433 int offset = cfg->spill_info [MONO_REG_INT][i].offset;
1439 slot = fp_offset_to_slot (cfg, offset);
1441 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1442 if (cfg->verbose_level > 1)
1443 printf ("\tint spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
1448 * process_other_slots:
1450 * Process stack slots registered using mini_gc_set_slot_type_... ().
1453 process_other_slots (MonoCompile *cfg)
1455 MonoCompileGC *gcfg = cfg->gc_info;
1458 /* Relative to the CFA */
1459 for (l = gcfg->stack_slots_from_cfa; l; l = l->next) {
1460 guint data = GPOINTER_TO_UINT (l->data);
1461 int cfa_slot = data >> 16;
1462 GCSlotType type = data & 0xff;
1466 * Map the cfa relative slot to an fp relative slot.
1467 * slot_addr == cfa - <cfa_slot>*4/8
1468 * fp + cfa_offset == cfa
1469 * -> slot_addr == fp + (cfa_offset - <cfa_slot>*4/8)
1471 slot = (cfg->cfa_offset / SIZEOF_SLOT) - cfa_slot - (gcfg->min_offset / SIZEOF_SLOT);
1473 set_slot_everywhere (gcfg, slot, type);
1475 if (cfg->verbose_level > 1) {
1476 int fp_offset = slot_to_fp_offset (cfg, slot);
1477 if (type == SLOT_NOREF)
1478 printf ("\tnoref slot at %s0x%x(fp) (slot = %d) (cfa - 0x%x)\n", fp_offset >= 0 ? "" : "-", ABS (fp_offset), slot, (int)(cfa_slot * SIZEOF_SLOT));
1482 /* Relative to the FP */
1483 for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
1484 gint data = GPOINTER_TO_INT (l->data);
1485 int offset = data >> 16;
1486 GCSlotType type = data & 0xff;
1489 slot = fp_offset_to_slot (cfg, offset);
1491 set_slot_everywhere (gcfg, slot, type);
1493 /* Liveness for these slots is handled by process_spill_slots () */
1495 if (cfg->verbose_level > 1) {
1496 if (type == SLOT_REF)
1497 printf ("\tref slot at fp+0x%x (slot = %d)\n", offset, slot);
1498 else if (type == SLOT_NOREF)
1499 printf ("\tnoref slot at 0x%x(fp) (slot = %d)\n", offset, slot);
1505 get_vtype_bitmap (MonoType *t, int *numbits)
1507 MonoClass *klass = mono_class_from_mono_type (t);
1509 if (klass->generic_container || mono_class_is_open_constructed_type (t)) {
1510 /* FIXME: Generic sharing */
1513 mono_class_compute_gc_descriptor (klass);
1515 return mono_gc_get_bitmap_for_descr (klass->gc_descr, numbits);
1519 static inline const char*
1520 get_offset_sign (int offset)
1522 return offset < 0 ? "-" : "+";
1526 get_offset_val (int offset)
1528 return offset < 0 ? (- offset) : offset;
1532 process_variables (MonoCompile *cfg)
1534 MonoCompileGC *gcfg = cfg->gc_info;
1535 MonoMethodSignature *sig = mono_method_signature (cfg->method);
1536 int i, locals_min_slot, locals_max_slot, cindex;
1540 int locals_min_offset = gcfg->locals_min_offset;
1541 int locals_max_offset = gcfg->locals_max_offset;
1543 /* Slots for locals are NOREF by default */
1544 locals_min_slot = (locals_min_offset - gcfg->min_offset) / SIZEOF_SLOT;
1545 locals_max_slot = (locals_max_offset - gcfg->min_offset) / SIZEOF_SLOT;
1546 for (i = locals_min_slot; i < locals_max_slot; ++i) {
1547 set_slot_everywhere (gcfg, i, SLOT_NOREF);
1551 * Compute the offset where variables are initialized in the first bblock, if any.
1553 pc_offsets = g_new0 (int, cfg->next_vreg);
1555 bb = cfg->bb_entry->next_bb;
1556 MONO_BB_FOR_EACH_INS (bb, tmp) {
1557 if (tmp->opcode == OP_GC_LIVENESS_DEF) {
1558 int vreg = tmp->inst_c1;
1559 if (pc_offsets [vreg] == 0) {
1560 g_assert (tmp->backend.pc_offset > 0);
1561 pc_offsets [vreg] = tmp->backend.pc_offset;
1567 * Stack slots holding arguments are initialized in the prolog.
1568 * This means we can treat them alive for the whole method.
1570 for (i = 0; i < cfg->num_varinfo; i++) {
1571 MonoInst *ins = cfg->varinfo [i];
1572 MonoType *t = ins->inst_vtype;
1575 gboolean byref, is_this = FALSE;
1576 gboolean is_arg = i < cfg->locals_start;
1578 if (ins == cfg->ret) {
1579 if (!(ins->opcode == OP_REGOFFSET && MONO_TYPE_ISSTRUCT (t)))
1583 vmv = MONO_VARINFO (cfg, i);
1585 /* For some reason, 'this' is byref */
1586 if (sig->hasthis && ins == cfg->args [0] && !cfg->method->klass->valuetype) {
1587 t = &cfg->method->klass->byval_arg;
1593 if (ins->opcode == OP_REGVAR) {
1595 GCSlotType slot_type;
1597 t = mini_type_get_underlying_type (NULL, t);
1600 g_assert (hreg < MONO_MAX_IREGS);
1603 slot_type = SLOT_PIN;
1605 slot_type = mini_type_is_reference (cfg, t) ? SLOT_REF : SLOT_NOREF;
1607 if (slot_type == SLOT_PIN) {
1608 /* These have no live interval, be conservative */
1609 set_reg_slot_everywhere (gcfg, hreg, slot_type);
1612 * Unlike variables allocated to the stack, we generate liveness info
1613 * for noref vars in registers in mono_spill_global_vars (), because
1614 * knowing that a register doesn't contain a ref allows us to mark its save
1615 * locations precisely.
1617 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1618 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1619 set_reg_slot (gcfg, hreg, cindex, slot_type);
1622 if (cfg->verbose_level > 1) {
1623 printf ("\t%s %sreg %s(R%d)\n", slot_type_to_string (slot_type), is_arg ? "arg " : "", mono_arch_regname (hreg), vmv->vreg);
1629 if (ins->opcode != OP_REGOFFSET)
1632 if (ins->inst_offset % SIZEOF_SLOT != 0)
1635 if (is_arg && ins->inst_offset >= gcfg->max_offset)
1636 /* In parent frame */
1639 pos = fp_offset_to_slot (cfg, ins->inst_offset);
1641 if (is_arg && ins->flags & MONO_INST_IS_DEAD) {
1642 /* These do not get stored in the prolog */
1643 set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1645 if (cfg->verbose_level > 1) {
1646 printf ("\tdead arg at fp%s0x%x (slot = %d): %s\n", get_offset_sign (ins->inst_offset), get_offset_val (ins->inst_offset), pos, mono_type_full_name (ins->inst_vtype));
1651 if (MONO_TYPE_ISSTRUCT (t)) {
1653 gsize *bitmap = NULL;
1654 gboolean pin = FALSE;
1658 if (ins->backend.is_pinvoke)
1659 size = mono_class_native_size (ins->klass, NULL);
1661 size = mono_class_value_size (ins->klass, NULL);
1662 size_in_slots = ALIGN_TO (size, SIZEOF_SLOT) / SIZEOF_SLOT;
1664 if (cfg->verbose_level > 1)
1665 printf ("\tvtype R%d at %s0x%x(fp)-%s0x%x(fp) (slot %d-%d): %s\n", vmv->vreg, get_offset_sign (ins->inst_offset), get_offset_val (ins->inst_offset), get_offset_sign (ins->inst_offset), get_offset_val (ins->inst_offset + (size_in_slots * SIZEOF_SLOT)), pos, pos + size_in_slots, mono_type_full_name (ins->inst_vtype));
1667 if (!ins->klass->has_references) {
1669 for (j = 0; j < size_in_slots; ++j)
1670 set_slot_everywhere (gcfg, pos + j, SLOT_NOREF);
1675 bitmap = get_vtype_bitmap (t, &numbits);
1680 * Most vtypes are marked volatile because of the LDADDR instructions,
1681 * and they have no liveness information since they are decomposed
1682 * before the liveness pass. We emit OP_GC_LIVENESS_DEF instructions for
1683 * them during VZERO decomposition.
1685 if (!pc_offsets [vmv->vreg])
1688 if (ins->backend.is_pinvoke)
1692 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1693 if (gcfg->callsites [cindex]->pc_offset > pc_offsets [vmv->vreg]) {
1694 for (j = 0; j < numbits; ++j) {
1695 if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD))) {
1696 /* The descriptor is for the boxed object */
1697 set_slot (gcfg, (pos + j - (sizeof (MonoObject) / SIZEOF_SLOT)), cindex, pin ? SLOT_PIN : SLOT_REF);
1703 if (cfg->verbose_level > 1) {
1704 for (j = 0; j < numbits; ++j) {
1705 if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD)))
1706 printf ("\t\t%s slot at 0x%x(fp) (slot = %d)\n", pin ? "pin" : "ref", (int)(ins->inst_offset + (j * SIZEOF_SLOT)), (int)(pos + j - (sizeof (MonoObject) / SIZEOF_SLOT)));
1710 if (cfg->verbose_level > 1)
1711 printf ("\t\tpinned\n");
1712 for (j = 0; j < size_in_slots; ++j) {
1713 set_slot_everywhere (gcfg, pos + j, SLOT_PIN);
1722 if (!is_arg && (ins->inst_offset < gcfg->min_offset || ins->inst_offset >= gcfg->max_offset))
1723 /* Vret addr etc. */
1728 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1730 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1731 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1732 set_slot (gcfg, pos, cindex, SLOT_PIN);
1734 if (cfg->verbose_level > 1)
1735 printf ("\tbyref at %s0x%x(fp) (R%d, slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, vmv->vreg, pos, mono_type_full_name (ins->inst_vtype));
1740 * This is currently disabled, but could be enabled to debug crashes.
1743 if (t->type == MONO_TYPE_I) {
1745 * Variables created in mono_handle_global_vregs have type I, but they
1746 * could hold GC refs since the vregs they were created from might not been
1747 * marked as holding a GC ref. So be conservative.
1749 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1754 t = mini_type_get_underlying_type (NULL, t);
1756 if (!mini_type_is_reference (cfg, t)) {
1757 set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1758 if (cfg->verbose_level > 1)
1759 printf ("\tnoref at %s0x%x(fp) (R%d, slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, vmv->vreg, pos, mono_type_full_name (ins->inst_vtype));
1760 if (!t->byref && sizeof (mgreg_t) == 4 && (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_R8)) {
1761 set_slot_everywhere (gcfg, pos + 1, SLOT_NOREF);
1762 if (cfg->verbose_level > 1)
1763 printf ("\tnoref at %s0x%x(fp) (R%d, slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)(ins->inst_offset + 4) : (int)ins->inst_offset + 4, vmv->vreg, pos + 1, mono_type_full_name (ins->inst_vtype));
1768 /* 'this' is marked INDIRECT for gshared methods */
1769 if (ins->flags & (MONO_INST_VOLATILE | MONO_INST_INDIRECT) && !is_this) {
1771 * For volatile variables, treat them alive from the point they are
1772 * initialized in the first bblock until the end of the method.
1775 set_slot_everywhere (gcfg, pos, SLOT_REF);
1776 } else if (pc_offsets [vmv->vreg]) {
1777 set_slot_in_range (gcfg, pos, 0, pc_offsets [vmv->vreg], SLOT_PIN);
1778 set_slot_in_range (gcfg, pos, pc_offsets [vmv->vreg], cfg->code_size, SLOT_REF);
1780 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1782 if (cfg->verbose_level > 1)
1783 printf ("\tvolatile ref at %s0x%x(fp) (R%d, slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, vmv->vreg, pos, mono_type_full_name (ins->inst_vtype));
1788 /* Live for the whole method */
1789 set_slot_everywhere (gcfg, pos, SLOT_REF);
1791 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1792 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1793 set_slot (gcfg, pos, cindex, SLOT_REF);
1796 if (cfg->verbose_level > 1) {
1797 printf ("\tref at %s0x%x(fp) (R%d, slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, vmv->vreg, pos, mono_type_full_name (ins->inst_vtype));
1801 g_free (pc_offsets);
1805 sp_offset_to_fp_offset (MonoCompile *cfg, int sp_offset)
1808 * Convert a sp relative offset to a slot index. This is
1809 * platform specific.
1812 /* fp = sp + offset */
1813 g_assert (cfg->frame_reg == AMD64_RBP);
1814 return (- cfg->arch.sp_fp_offset + sp_offset);
1815 #elif defined(TARGET_X86)
1816 /* The offset is computed from the sp at the start of the call sequence */
1817 g_assert (cfg->frame_reg == X86_EBP);
1818 return (- cfg->arch.sp_fp_offset - sp_offset);
1826 type_to_gc_slot_type (MonoCompile *cfg, MonoType *t)
1830 t = mini_type_get_underlying_type (NULL, t);
1831 if (mini_type_is_reference (cfg, t))
1834 if (MONO_TYPE_ISSTRUCT (t)) {
1835 MonoClass *klass = mono_class_from_mono_type (t);
1836 if (!klass->has_references) {
1848 process_param_area_slots (MonoCompile *cfg)
1850 MonoCompileGC *gcfg = cfg->gc_info;
1855 * These slots are used for passing parameters during calls. They are sp relative, not
1856 * fp relative, so they are harder to handle.
1858 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1859 /* The distance between fp and sp is not constant */
1862 is_param = mono_mempool_alloc0 (cfg->mempool, gcfg->nslots * sizeof (gboolean));
1864 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1865 GCCallSite *callsite = gcfg->callsites [cindex];
1868 for (l = callsite->param_slots; l; l = l->next) {
1869 MonoInst *def = l->data;
1870 MonoType *t = def->inst_vtype;
1871 int sp_offset = def->inst_offset;
1872 int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
1873 int slot = fp_offset_to_slot (cfg, fp_offset);
1877 if (MONO_TYPE_ISSTRUCT (t)) {
1878 size = mini_type_stack_size_full (cfg->generic_sharing_context, t, &align, FALSE);
1880 size = sizeof (mgreg_t);
1883 for (i = 0; i < size / sizeof (mgreg_t); ++i) {
1884 g_assert (slot + i >= 0 && slot + i < gcfg->nslots);
1885 is_param [slot + i] = TRUE;
1890 /* All param area slots are noref by default */
1891 for (i = 0; i < gcfg->nslots; ++i) {
1893 set_slot_everywhere (gcfg, i, SLOT_NOREF);
1896 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1897 GCCallSite *callsite = gcfg->callsites [cindex];
1900 for (l = callsite->param_slots; l; l = l->next) {
1901 MonoInst *def = l->data;
1902 MonoType *t = def->inst_vtype;
1903 int sp_offset = def->inst_offset;
1904 int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
1905 int slot = fp_offset_to_slot (cfg, fp_offset);
1906 GCSlotType type = type_to_gc_slot_type (cfg, t);
1908 if (MONO_TYPE_ISSTRUCT (t)) {
1915 size = mini_type_stack_size_full (cfg->generic_sharing_context, t, &align, FALSE);
1916 size_in_slots = ALIGN_TO (size, SIZEOF_SLOT) / SIZEOF_SLOT;
1918 bitmap = get_vtype_bitmap (t, &numbits);
1919 if (type == SLOT_NOREF || !bitmap) {
1920 for (i = 0; i < size_in_slots; ++i) {
1921 set_slot_in_range (gcfg, slot + i, def->backend.pc_offset, callsite->pc_offset + 1, type);
1923 if (cfg->verbose_level > 1)
1924 printf ("\t%s param area slots at %s0x%x(fp)=0x%x(sp) (slot = %d-%d) [0x%x-0x%x]\n", slot_type_to_string (type), get_offset_sign (fp_offset), get_offset_val (fp_offset), sp_offset, slot, slot + (size / (int)sizeof (mgreg_t)), def->backend.pc_offset, callsite->pc_offset + 1);
1926 for (j = 0; j < numbits; ++j) {
1927 if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD))) {
1928 /* The descriptor is for the boxed object */
1929 set_slot (gcfg, (slot + j - (sizeof (MonoObject) / SIZEOF_SLOT)), cindex, SLOT_REF);
1932 if (cfg->verbose_level > 1)
1933 printf ("\tvtype param area slots at %s0x%x(fp)=0x%x(sp) (slot = %d-%d) [0x%x-0x%x]\n", get_offset_sign (fp_offset), get_offset_val (fp_offset), sp_offset, slot, slot + (size / (int)sizeof (mgreg_t)), def->backend.pc_offset, callsite->pc_offset + 1);
1937 /* The slot is live between the def instruction and the call */
1938 set_slot_in_range (gcfg, slot, def->backend.pc_offset, callsite->pc_offset + 1, type);
1939 if (cfg->verbose_level > 1)
1940 printf ("\t%s param area slot at %s0x%x(fp)=0x%x(sp) (slot = %d) [0x%x-0x%x]\n", slot_type_to_string (type), get_offset_sign (fp_offset), get_offset_val (fp_offset), sp_offset, slot, def->backend.pc_offset, callsite->pc_offset + 1);
1947 process_finally_clauses (MonoCompile *cfg)
1949 MonoCompileGC *gcfg = cfg->gc_info;
1950 GCCallSite **callsites;
1952 gboolean has_finally;
1953 int i, j, nslots, nregs;
1955 ncallsites = gcfg->ncallsites;
1956 nslots = gcfg->nslots;
1957 nregs = gcfg->nregs;
1958 callsites = gcfg->callsites;
1961 * The calls to the finally clauses don't show up in the cfg. See
1962 * test_0_liveness_8 ().
1963 * Variables accessed inside the finally clause are already marked VOLATILE by
1964 * mono_liveness_handle_exception_clauses (). Variables not accessed inside the finally clause have
1965 * correct liveness outside the finally clause. So mark them PIN inside the finally clauses.
1967 has_finally = FALSE;
1968 for (i = 0; i < cfg->header->num_clauses; ++i) {
1969 MonoExceptionClause *clause = &cfg->header->clauses [i];
1971 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
1976 if (cfg->verbose_level > 1)
1977 printf ("\tMethod has finally clauses, pessimizing live ranges.\n");
1978 for (j = 0; j < ncallsites; ++j) {
1979 MonoBasicBlock *bb = callsites [j]->bb;
1980 MonoExceptionClause *clause;
1981 gboolean is_in_finally = FALSE;
1983 for (i = 0; i < cfg->header->num_clauses; ++i) {
1984 clause = &cfg->header->clauses [i];
1986 if (MONO_OFFSET_IN_HANDLER (clause, bb->real_offset)) {
1987 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
1988 is_in_finally = TRUE;
1994 if (is_in_finally) {
1995 for (i = 0; i < nslots; ++i)
1996 set_slot (gcfg, i, j, SLOT_PIN);
1997 for (i = 0; i < nregs; ++i)
1998 set_reg_slot (gcfg, i, j, SLOT_PIN);
2005 compute_frame_size (MonoCompile *cfg)
2007 int i, locals_min_offset, locals_max_offset, cfa_min_offset, cfa_max_offset;
2008 int min_offset, max_offset;
2009 MonoCompileGC *gcfg = cfg->gc_info;
2010 MonoMethodSignature *sig = mono_method_signature (cfg->method);
2013 /* Compute min/max offsets from the fp */
2016 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM) || defined(TARGET_S390X)
2017 locals_min_offset = ALIGN_TO (cfg->locals_min_stack_offset, SIZEOF_SLOT);
2018 locals_max_offset = cfg->locals_max_stack_offset;
2020 /* min/max stack offset needs to be computed in mono_arch_allocate_vars () */
2024 locals_min_offset = ALIGN_TO (locals_min_offset, SIZEOF_SLOT);
2025 locals_max_offset = ALIGN_TO (locals_max_offset, SIZEOF_SLOT);
2027 min_offset = locals_min_offset;
2028 max_offset = locals_max_offset;
2031 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2032 MonoInst *ins = cfg->args [i];
2034 if (ins->opcode == OP_REGOFFSET)
2035 min_offset = MIN (min_offset, ins->inst_offset);
2039 g_assert (cfg->frame_reg == cfg->cfa_reg);
2040 g_assert (cfg->cfa_offset > 0);
2042 cfa_max_offset = cfg->cfa_offset;
2044 min_offset = MIN (min_offset, cfa_min_offset);
2045 max_offset = MAX (max_offset, cfa_max_offset);
2047 /* Fp relative slots */
2048 for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
2049 gint data = GPOINTER_TO_INT (l->data);
2050 int offset = data >> 16;
2052 min_offset = MIN (min_offset, offset);
2056 if (!(cfg->flags & MONO_CFG_HAS_SPILLUP)) {
2057 int stack_offset = ALIGN_TO (cfg->stack_offset, SIZEOF_SLOT);
2058 min_offset = MIN (min_offset, (-stack_offset));
2061 /* Param area slots */
2063 min_offset = MIN (min_offset, -cfg->arch.sp_fp_offset);
2064 #elif defined(TARGET_X86)
2065 min_offset = MIN (min_offset, - (cfg->arch.sp_fp_offset + cfg->arch.param_area_size));
2066 #elif defined(TARGET_ARM)
2068 #elif defined(TARGET_s390X)
2074 gcfg->min_offset = min_offset;
2075 gcfg->max_offset = max_offset;
2076 gcfg->locals_min_offset = locals_min_offset;
2077 gcfg->locals_max_offset = locals_max_offset;
2081 init_gcfg (MonoCompile *cfg)
2083 int i, nregs, nslots;
2084 MonoCompileGC *gcfg = cfg->gc_info;
2085 GCCallSite **callsites;
2094 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2095 ncallsites += g_slist_length (bb->gc_callsites);
2097 callsites = mono_mempool_alloc0 (cfg->mempool, ncallsites * sizeof (GCCallSite*));
2099 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2100 for (l = bb->gc_callsites; l; l = l->next)
2101 callsites [i++] = l->data;
2104 /* The callsites should already be ordered by pc offset */
2105 for (i = 1; i < ncallsites; ++i)
2106 g_assert (callsites [i - 1]->pc_offset < callsites [i]->pc_offset);
2109 * The stack frame looks like this:
2111 * <fp + max_offset> == cfa -> <end of previous frame>
2112 * <other stack slots>
2114 * <other stack slots>
2115 * fp + min_offset ->
2120 if (cfg->verbose_level > 1)
2121 printf ("GC Map for %s: 0x%x-0x%x\n", mono_method_full_name (cfg->method, TRUE), gcfg->min_offset, gcfg->max_offset);
2123 nslots = (gcfg->max_offset - gcfg->min_offset) / SIZEOF_SLOT;
2126 gcfg->nslots = nslots;
2127 gcfg->nregs = nregs;
2128 gcfg->callsites = callsites;
2129 gcfg->ncallsites = ncallsites;
2130 gcfg->stack_bitmap_width = ALIGN_TO (nslots, 8) / 8;
2131 gcfg->reg_bitmap_width = ALIGN_TO (nregs, 8) / 8;
2132 gcfg->stack_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * ncallsites);
2133 gcfg->stack_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * ncallsites);
2134 gcfg->reg_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * ncallsites);
2135 gcfg->reg_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * ncallsites);
2137 /* All slots start out as PIN */
2138 memset (gcfg->stack_pin_bitmap, 0xff, gcfg->stack_bitmap_width * ncallsites);
2139 for (i = 0; i < nregs; ++i) {
2141 * By default, registers are NOREF.
2142 * It is possible for a callee to save them before being defined in this method,
2143 * but the saved value is dead too, so it doesn't need to be marked.
2145 if ((cfg->used_int_regs & (1 << i)))
2146 set_reg_slot_everywhere (gcfg, i, SLOT_NOREF);
2151 create_map (MonoCompile *cfg)
2154 int i, j, nregs, nslots, nref_regs, npin_regs, alloc_size, bitmaps_size, bitmaps_offset;
2156 int stack_bitmap_width, stack_bitmap_size, reg_ref_bitmap_width, reg_ref_bitmap_size;
2157 int reg_pin_bitmap_width, reg_pin_bitmap_size, bindex;
2159 gboolean has_ref_slots, has_pin_slots, has_ref_regs, has_pin_regs;
2160 MonoCompileGC *gcfg = cfg->gc_info;
2161 GCCallSite **callsites;
2163 guint8 *bitmap, *bitmaps;
2164 guint32 reg_ref_mask, reg_pin_mask;
2166 ncallsites = gcfg->ncallsites;
2167 nslots = gcfg->nslots;
2168 nregs = gcfg->nregs;
2169 callsites = gcfg->callsites;
2172 * Compute the real size of the bitmap i.e. ignore NOREF columns at the beginning and at
2173 * the end. Also, compute whenever the map needs ref/pin bitmaps, and collect stats.
2175 has_ref_slots = FALSE;
2176 has_pin_slots = FALSE;
2179 memset (ntypes, 0, sizeof (ntypes));
2180 for (i = 0; i < nslots; ++i) {
2181 gboolean has_ref = FALSE;
2182 gboolean has_pin = FALSE;
2184 for (j = 0; j < ncallsites; ++j) {
2185 if (get_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, j, i))
2187 if (get_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, j, i))
2192 has_ref_slots = TRUE;
2194 has_pin_slots = TRUE;
2197 ntypes [SLOT_REF] ++;
2199 ntypes [SLOT_PIN] ++;
2201 ntypes [SLOT_NOREF] ++;
2203 if (has_ref || has_pin) {
2210 start = end = nslots;
2212 g_assert (start != -1);
2213 g_assert (start < end);
2216 has_ref_regs = FALSE;
2217 has_pin_regs = FALSE;
2222 for (i = 0; i < nregs; ++i) {
2223 gboolean has_ref = FALSE;
2224 gboolean has_pin = FALSE;
2226 if (!(cfg->used_int_regs & (1 << i)))
2229 for (j = 0; j < ncallsites; ++j) {
2230 if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, j, i)) {
2235 for (j = 0; j < ncallsites; ++j) {
2236 if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, j, i)) {
2243 reg_ref_mask |= (1 << i);
2244 has_ref_regs = TRUE;
2248 reg_pin_mask |= (1 << i);
2249 has_pin_regs = TRUE;
2254 if (cfg->verbose_level > 1)
2255 printf ("Slots: %d Start: %d End: %d Refs: %d NoRefs: %d Pin: %d Callsites: %d\n", nslots, start, end, ntypes [SLOT_REF], ntypes [SLOT_NOREF], ntypes [SLOT_PIN], ncallsites);
2257 /* Create the GC Map */
2259 stack_bitmap_width = ALIGN_TO (end - start, 8) / 8;
2260 stack_bitmap_size = stack_bitmap_width * ncallsites;
2261 reg_ref_bitmap_width = ALIGN_TO (nref_regs, 8) / 8;
2262 reg_ref_bitmap_size = reg_ref_bitmap_width * ncallsites;
2263 reg_pin_bitmap_width = ALIGN_TO (npin_regs, 8) / 8;
2264 reg_pin_bitmap_size = reg_pin_bitmap_width * ncallsites;
2265 bitmaps_size = (has_ref_slots ? stack_bitmap_size : 0) + (has_pin_slots ? stack_bitmap_size : 0) + (has_ref_regs ? reg_ref_bitmap_size : 0) + (has_pin_regs ? reg_pin_bitmap_size : 0);
2267 map = mono_mempool_alloc0 (cfg->mempool, sizeof (GCMap));
2269 map->frame_reg = cfg->frame_reg;
2270 map->start_offset = gcfg->min_offset;
2271 map->end_offset = gcfg->min_offset + (nslots * SIZEOF_SLOT);
2272 map->map_offset = start * SIZEOF_SLOT;
2273 map->nslots = end - start;
2274 map->has_ref_slots = has_ref_slots;
2275 map->has_pin_slots = has_pin_slots;
2276 map->has_ref_regs = has_ref_regs;
2277 map->has_pin_regs = has_pin_regs;
2278 g_assert (nregs < 32);
2279 map->used_int_regs = cfg->used_int_regs;
2280 map->reg_ref_mask = reg_ref_mask;
2281 map->reg_pin_mask = reg_pin_mask;
2282 map->nref_regs = nref_regs;
2283 map->npin_regs = npin_regs;
2285 bitmaps = mono_mempool_alloc0 (cfg->mempool, bitmaps_size);
2288 if (has_ref_slots) {
2289 map->stack_ref_bitmap_offset = bitmaps_offset;
2290 bitmaps_offset += stack_bitmap_size;
2292 bitmap = &bitmaps [map->stack_ref_bitmap_offset];
2293 for (i = 0; i < nslots; ++i) {
2294 for (j = 0; j < ncallsites; ++j) {
2295 if (get_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, j, i))
2296 set_bit (bitmap, stack_bitmap_width, j, i - start);
2300 if (has_pin_slots) {
2301 map->stack_pin_bitmap_offset = bitmaps_offset;
2302 bitmaps_offset += stack_bitmap_size;
2304 bitmap = &bitmaps [map->stack_pin_bitmap_offset];
2305 for (i = 0; i < nslots; ++i) {
2306 for (j = 0; j < ncallsites; ++j) {
2307 if (get_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, j, i))
2308 set_bit (bitmap, stack_bitmap_width, j, i - start);
2313 map->reg_ref_bitmap_offset = bitmaps_offset;
2314 bitmaps_offset += reg_ref_bitmap_size;
2316 bitmap = &bitmaps [map->reg_ref_bitmap_offset];
2318 for (i = 0; i < nregs; ++i) {
2319 if (reg_ref_mask & (1 << i)) {
2320 for (j = 0; j < ncallsites; ++j) {
2321 if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, j, i))
2322 set_bit (bitmap, reg_ref_bitmap_width, j, bindex);
2329 map->reg_pin_bitmap_offset = bitmaps_offset;
2330 bitmaps_offset += reg_pin_bitmap_size;
2332 bitmap = &bitmaps [map->reg_pin_bitmap_offset];
2334 for (i = 0; i < nregs; ++i) {
2335 if (reg_pin_mask & (1 << i)) {
2336 for (j = 0; j < ncallsites; ++j) {
2337 if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, j, i))
2338 set_bit (bitmap, reg_pin_bitmap_width, j, bindex);
2346 map->ncallsites = ncallsites;
2347 if (cfg->code_len < 256)
2348 map->callsite_entry_size = 1;
2349 else if (cfg->code_len < 65536)
2350 map->callsite_entry_size = 2;
2352 map->callsite_entry_size = 4;
2354 /* Encode the GC Map */
2362 encode_gc_map (map, buf, &endbuf);
2363 g_assert (endbuf - buf < 256);
2365 encoded_size = endbuf - buf;
2366 alloc_size = sizeof (GCEncodedMap) + ALIGN_TO (encoded_size, map->callsite_entry_size) + (map->callsite_entry_size * map->ncallsites) + bitmaps_size;
2368 emap = mono_domain_alloc0 (cfg->domain, alloc_size);
2369 //emap->ref_slots = map->ref_slots;
2371 /* Encoded fixed fields */
2372 p = &emap->encoded [0];
2373 //emap->encoded_size = encoded_size;
2374 memcpy (p, buf, encoded_size);
2377 /* Callsite table */
2378 p = (guint8*)ALIGN_TO ((mgreg_t)p, map->callsite_entry_size);
2379 if (map->callsite_entry_size == 1) {
2380 guint8 *offsets = p;
2381 for (i = 0; i < ncallsites; ++i)
2382 offsets [i] = callsites [i]->pc_offset;
2383 stats.gc_callsites8_size += ncallsites * sizeof (guint8);
2384 } else if (map->callsite_entry_size == 2) {
2385 guint16 *offsets = (guint16*)p;
2386 for (i = 0; i < ncallsites; ++i)
2387 offsets [i] = callsites [i]->pc_offset;
2388 stats.gc_callsites16_size += ncallsites * sizeof (guint16);
2390 guint32 *offsets = (guint32*)p;
2391 for (i = 0; i < ncallsites; ++i)
2392 offsets [i] = callsites [i]->pc_offset;
2393 stats.gc_callsites32_size += ncallsites * sizeof (guint32);
2395 p += ncallsites * map->callsite_entry_size;
2398 memcpy (p, bitmaps, bitmaps_size);
2401 g_assert ((guint8*)p - (guint8*)emap <= alloc_size);
2403 stats.gc_maps_size += alloc_size;
2404 stats.gc_callsites_size += ncallsites * map->callsite_entry_size;
2405 stats.gc_bitmaps_size += bitmaps_size;
2406 stats.gc_map_struct_size += sizeof (GCEncodedMap) + encoded_size;
2408 cfg->jit_info->gc_info = emap;
2410 cfg->gc_map = (guint8*)emap;
2411 cfg->gc_map_size = alloc_size;
2414 stats.all_slots += nslots;
2415 stats.ref_slots += ntypes [SLOT_REF];
2416 stats.noref_slots += ntypes [SLOT_NOREF];
2417 stats.pin_slots += ntypes [SLOT_PIN];
2421 mini_gc_create_gc_map (MonoCompile *cfg)
2423 if (!cfg->compute_gc_maps)
2427 * During marking, all frames except the top frame are at a call site, and we mark the
2428 * top frame conservatively. This means that we only need to compute and record
2429 * GC maps for call sites.
2432 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
2433 /* Without liveness info, the live ranges are not precise enough */
2436 mono_analyze_liveness_gc (cfg);
2438 compute_frame_size (cfg);
2442 process_spill_slots (cfg);
2443 process_other_slots (cfg);
2444 process_param_area_slots (cfg);
2445 process_variables (cfg);
2446 process_finally_clauses (cfg);
2452 parse_debug_options (void)
2457 env = getenv ("MONO_GCMAP_DEBUG");
2461 opts = g_strsplit (env, ",", -1);
2462 for (ptr = opts; ptr && *ptr; ptr ++) {
2463 /* No options yet */
2464 fprintf (stderr, "Invalid format for the MONO_GCMAP_DEBUG env variable: '%s'\n", env);
2475 memset (&cb, 0, sizeof (cb));
2476 cb.thread_attach_func = thread_attach_func;
2477 cb.thread_detach_func = thread_detach_func;
2478 cb.thread_suspend_func = thread_suspend_func;
2479 /* Comment this out to disable precise stack marking */
2480 cb.thread_mark_func = thread_mark_func;
2481 mono_gc_set_gc_callbacks (&cb);
2483 logfile = mono_gc_get_logfile ();
2485 parse_debug_options ();
2487 mono_counters_register ("GC Maps size",
2488 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_maps_size);
2489 mono_counters_register ("GC Call Sites size",
2490 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites_size);
2491 mono_counters_register ("GC Bitmaps size",
2492 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_bitmaps_size);
2493 mono_counters_register ("GC Map struct size",
2494 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_map_struct_size);
2495 mono_counters_register ("GC Call Sites encoded using 8 bits",
2496 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites8_size);
2497 mono_counters_register ("GC Call Sites encoded using 16 bits",
2498 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites16_size);
2499 mono_counters_register ("GC Call Sites encoded using 32 bits",
2500 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites32_size);
2502 mono_counters_register ("GC Map slots (all)",
2503 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.all_slots);
2504 mono_counters_register ("GC Map slots (ref)",
2505 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.ref_slots);
2506 mono_counters_register ("GC Map slots (noref)",
2507 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.noref_slots);
2508 mono_counters_register ("GC Map slots (pin)",
2509 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.pin_slots);
2511 mono_counters_register ("GC TLS Data size",
2512 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.tlsdata_size);
2514 mono_counters_register ("Stack space scanned (all)",
2515 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_stacks);
2516 mono_counters_register ("Stack space scanned (native)",
2517 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_native);
2518 mono_counters_register ("Stack space scanned (other)",
2519 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_other);
2520 mono_counters_register ("Stack space scanned (using GC Maps)",
2521 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned);
2522 mono_counters_register ("Stack space scanned (precise)",
2523 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_precisely);
2524 mono_counters_register ("Stack space scanned (pin)",
2525 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_conservatively);
2526 mono_counters_register ("Stack space scanned (pin registers)",
2527 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_registers);
2538 mini_gc_init_gc_map (MonoCompile *cfg)
2543 mini_gc_create_gc_map (MonoCompile *cfg)
2548 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
2553 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
2562 * Set GC specific options in CFG.
2565 mini_gc_init_cfg (MonoCompile *cfg)
2567 if (mono_gc_is_moving ()) {
2568 cfg->disable_ref_noref_stack_slot_share = TRUE;
2569 cfg->gen_write_barriers = TRUE;
2572 mini_gc_init_gc_map (cfg);
2576 * Problems with the current code:
2577 * - the stack walk is slow
2578 * - vtypes/refs used in EH regions are treated conservatively
2579 * - if the code is finished, less pinning will be done, causing problems because
2580 * we promote all surviving objects to old-gen.
2581 * - the unwind code can't handle a method stopped inside a finally region, it thinks the caller is
2582 * another method, but in reality it is either the exception handling code or the CALL_HANDLER opcode.
2583 * This manifests in "Unable to find ip offset x in callsite list" assertions.
2584 * - the unwind code also can't handle frames which are in the epilog, since the unwind info is not
2589 * Ideas for creating smaller GC maps:
2590 * - remove empty columns from the bitmaps. This requires adding a mask bit array for
2592 * - merge reg and stack slot bitmaps, so the unused bits at the end of the reg bitmap are
2594 * - if the bitmap width is not a multiple of 8, the remaining bits are wasted.
2595 * - group ref and non-ref stack slots together in mono_allocate_stack_slots ().
2596 * - add an index for the callsite table so that each entry can be encoded as a 1 byte difference
2597 * from an index entry.