3 * GC interface for the mono JIT
6 * Zoltan Varga (vargaz@gmail.com)
8 * Copyright 2009 Novell, Inc (http://www.novell.com)
9 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
15 #include "mini-runtime.h"
16 #include <mono/metadata/gc-internals.h>
19 get_provenance (StackFrameInfo
*frame
, MonoContext
*ctx
, gpointer data
)
21 MonoJitInfo
*ji
= frame
->ji
;
25 method
= jinfo_get_method (ji
);
26 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
28 *(gpointer
*)data
= method
;
33 get_provenance_func (void)
35 gpointer provenance
= NULL
;
36 mono_walk_stack (get_provenance
, MONO_UNWIND_DEFAULT
, (gpointer
)&provenance
);
41 //#if defined(MONO_ARCH_GC_MAPS_SUPPORTED)
43 #include <mono/metadata/sgen-conf.h>
44 #include <mono/metadata/gc-internals.h>
45 #include <mono/utils/mono-counters.h>
46 #include <mono/utils/unlocked.h>
48 //#define SIZEOF_SLOT ((int)sizeof (host_mgreg_t))
49 //#define SIZEOF_SLOT ((int)sizeof (target_mgreg_t))
51 #define GC_BITS_PER_WORD (sizeof (mword) * 8)
53 /* Contains state needed by the GC Map construction code */
56 * This contains information about stack slots initialized in the prolog, encoded using
57 * (slot_index << 16) | slot_type. The slot_index is relative to the CFA, i.e. 0
58 * means cfa+0, 1 means cfa-4/8, etc.
60 GSList
*stack_slots_from_cfa
;
61 /* Same for stack slots relative to the frame pointer */
62 GSList
*stack_slots_from_fp
;
64 /* Number of slots in the map */
66 /* The number of registers in the map */
68 /* Min and Max offsets of the stack frame relative to fp */
69 int min_offset
, max_offset
;
70 /* Same for the locals area */
71 int locals_min_offset
, locals_max_offset
;
73 /* The call sites where this frame can be stopped during GC */
74 GCCallSite
**callsites
;
75 /* The number of call sites */
79 * The width of the stack bitmaps in bytes. This is not equal to the bitmap width at
80 * runtime, since it includes columns which are 0.
82 int stack_bitmap_width
;
84 * A bitmap whose width equals nslots, and whose height equals ncallsites.
85 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
88 guint8
*stack_ref_bitmap
;
89 /* Same for SLOT_PIN */
90 guint8
*stack_pin_bitmap
;
93 * Similar bitmaps for registers. These have width MONO_MAX_IREGS in bits.
96 guint8
*reg_ref_bitmap
;
97 guint8
*reg_pin_bitmap
;
103 /* We don't support debug levels, its all-or-nothing */
104 #define DEBUG(s) do { s; fflush (logfile); } while (0)
105 #define DEBUG_ENABLED 1
112 #define DEBUG_PRECISE(s) do { s; } while (0)
113 #define DEBUG_PRECISE_ENABLED
115 #define DEBUG_PRECISE(s)
119 * Contains information collected during the conservative stack marking pass,
120 * used during the precise pass. This helps to avoid doing a stack walk twice, which
126 int frame_start_offset
;
128 /* Relative to stack_start */
129 int reg_locations
[MONO_MAX_IREGS
];
130 #ifdef DEBUG_PRECISE_ENABLED
133 int regs
[MONO_MAX_IREGS
];
137 /* Max number of frames stored in the TLS data */
138 #define MAX_FRAMES 50
141 * Per-thread data kept by this module. This is stored in the GC and passed to us as
142 * parameters, instead of being stored in a TLS variable, since during a collection,
143 * only the collection thread is active.
146 MonoThreadUnwindState unwind_state
;
147 MonoThreadInfo
*info
;
150 gpointer ref_to_track
;
151 /* Number of frames collected during the !precise pass */
153 FrameInfo frames
[MAX_FRAMES
];
156 /* These are constant so don't store them in the GC Maps */
157 /* Number of registers stored in gc maps */
158 #define NREGS MONO_MAX_IREGS
162 * Contains information needed to mark a stack frame.
163 * This is a transient structure, created from a compressed representation on-demand.
167 * The offsets of the GC tracked area inside the stack frame relative to the frame pointer.
168 * This includes memory which is NOREF thus doesn't need GC maps.
173 * The offset relative to frame_offset where the the memory described by the GC maps
177 /* The number of stack slots in the map */
179 /* The frame pointer register */
181 /* The size of each callsite table entry */
182 guint8 callsite_entry_size
;
183 guint has_pin_slots
: 1;
184 guint has_ref_slots
: 1;
185 guint has_ref_regs
: 1;
186 guint has_pin_regs
: 1;
188 /* The offsets below are into an external bitmaps array */
191 * A bitmap whose width is equal to bitmap_width, and whose height is equal to ncallsites.
192 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
195 guint32 stack_ref_bitmap_offset
;
197 * Same for SLOT_PIN. It is possible that the same bit is set in both bitmaps at
198 * different callsites, if the slot starts out as PIN, and later changes to REF.
200 guint32 stack_pin_bitmap_offset
;
203 * Corresponding bitmaps for registers
204 * These have width equal to the number of bits set in reg_ref_mask/reg_pin_mask.
205 * FIXME: Merge these with the normal bitmaps, i.e. reserve the first x slots for them ?
207 guint32 reg_pin_bitmap_offset
;
208 guint32 reg_ref_bitmap_offset
;
210 guint32 used_int_regs
, reg_ref_mask
, reg_pin_mask
;
212 /* The number of bits set in the two masks above */
213 guint8 nref_regs
, npin_regs
;
216 * A bit array marking slots which contain refs.
217 * This is used only for debugging.
221 /* Callsite offsets */
222 /* These can take up a lot of space, so encode them compactly */
232 * A compressed version of GCMap. This is what gets stored in MonoJitInfo.
236 //guint8 encoded_size;
239 * The arrays below are embedded after the struct.
240 * Their address needs to be computed.
243 /* The fixed fields of the GCMap encoded using LEB128 */
244 guint8 encoded
[MONO_ZERO_LEN_ARRAY
];
246 /* An array of ncallsites entries, each entry is callsite_entry_size bytes long */
247 guint8 callsites
[MONO_ZERO_LEN_ARRAY
];
250 guint8 bitmaps
[MONO_ZERO_LEN_ARRAY
];
253 static int precise_frame_count
[2], precise_frame_limit
= -1;
254 static gboolean precise_frame_limit_inited
;
258 gint32 scanned_stacks
;
260 gint32 scanned_precisely
;
261 gint32 scanned_conservatively
;
262 gint32 scanned_registers
;
263 gint32 scanned_native
;
264 gint32 scanned_other
;
272 gint32 gc_callsites_size
;
273 gint32 gc_callsites8_size
;
274 gint32 gc_callsites16_size
;
275 gint32 gc_callsites32_size
;
276 gint32 gc_bitmaps_size
;
277 gint32 gc_map_struct_size
;
281 static JITGCStats stats
;
283 static FILE *logfile
;
285 static gboolean enable_gc_maps_for_aot
;
288 mini_gc_enable_gc_maps_for_aot (void)
290 enable_gc_maps_for_aot
= TRUE
;
293 // FIXME: Move these to a shared place
296 encode_uleb128 (guint32 value
, guint8
*buf
, guint8
**endbuf
)
301 guint8 b
= value
& 0x7f;
303 if (value
!= 0) /* more bytes to come */
311 static G_GNUC_UNUSED
void
312 encode_sleb128 (gint32 value
, guint8
*buf
, guint8
**endbuf
)
315 gboolean negative
= (value
< 0);
323 /* the following is unnecessary if the
324 * implementation of >>= uses an arithmetic rather
325 * than logical shift for a signed left operand
329 value
|= - (1 <<(size
- 7));
330 /* sign bit of byte is second high order bit (0x40) */
331 if ((value
== 0 && !(byte
& 0x40)) ||
332 (value
== -1 && (byte
& 0x40)))
343 decode_uleb128 (guint8
*buf
, guint8
**endbuf
)
353 res
= res
| (((int)(b
& 0x7f)) << shift
);
365 decode_sleb128 (guint8
*buf
, guint8
**endbuf
)
375 res
= res
| (((int)(b
& 0x7f)) << shift
);
378 if (shift
< 32 && (b
& 0x40))
379 res
|= - (1 << shift
);
390 encode_frame_reg (int frame_reg
)
393 if (frame_reg
== AMD64_RSP
)
395 else if (frame_reg
== AMD64_RBP
)
397 #elif defined(TARGET_X86)
398 if (frame_reg
== X86_EBP
)
400 else if (frame_reg
== X86_ESP
)
402 #elif defined(TARGET_ARM)
403 if (frame_reg
== ARMREG_SP
)
405 else if (frame_reg
== ARMREG_FP
)
407 #elif defined(TARGET_S390X)
408 if (frame_reg
== S390_SP
)
410 else if (frame_reg
== S390_FP
)
412 #elif defined (TARGET_RISCV)
413 if (frame_reg
== RISCV_SP
)
415 else if (frame_reg
== RISCV_FP
)
420 g_assert_not_reached ();
425 decode_frame_reg (int encoded
)
430 else if (encoded
== 1)
432 #elif defined(TARGET_X86)
435 else if (encoded
== 1)
437 #elif defined(TARGET_ARM)
440 else if (encoded
== 1)
442 #elif defined(TARGET_S390X)
445 else if (encoded
== 1)
447 #elif defined (TARGET_RISCV)
450 else if (encoded
== 1)
455 g_assert_not_reached ();
461 static int callee_saved_regs
[] = { AMD64_RBP
, AMD64_RBX
, AMD64_R12
, AMD64_R13
, AMD64_R14
, AMD64_R15
, AMD64_RDI
, AMD64_RSI
};
463 static int callee_saved_regs
[] = { AMD64_RBP
, AMD64_RBX
, AMD64_R12
, AMD64_R13
, AMD64_R14
, AMD64_R15
};
465 #elif defined(TARGET_X86)
466 static int callee_saved_regs
[] = { X86_EBX
, X86_ESI
, X86_EDI
};
467 #elif defined(TARGET_ARM)
468 static int callee_saved_regs
[] = { ARMREG_V1
, ARMREG_V2
, ARMREG_V3
, ARMREG_V4
, ARMREG_V5
, ARMREG_V7
, ARMREG_FP
};
469 #elif defined(TARGET_ARM64)
471 static int callee_saved_regs
[] = { };
472 #elif defined(TARGET_S390X)
473 static int callee_saved_regs
[] = { s390_r6
, s390_r7
, s390_r8
, s390_r9
, s390_r10
, s390_r11
, s390_r12
, s390_r13
, s390_r14
};
474 #elif defined(TARGET_POWERPC64) && _CALL_ELF == 2
475 static int callee_saved_regs
[] = {
476 ppc_r13
, ppc_r14
, ppc_r15
, ppc_r16
,
477 ppc_r17
, ppc_r18
, ppc_r19
, ppc_r20
,
478 ppc_r21
, ppc_r22
, ppc_r23
, ppc_r24
,
479 ppc_r25
, ppc_r26
, ppc_r27
, ppc_r28
,
480 ppc_r29
, ppc_r30
, ppc_r31
};
481 #elif defined(TARGET_POWERPC)
482 static int callee_saved_regs
[] = { ppc_r6
, ppc_r7
, ppc_r8
, ppc_r9
, ppc_r10
, ppc_r11
, ppc_r12
, ppc_r13
, ppc_r14
};
483 #elif defined (TARGET_RISCV)
484 static int callee_saved_regs
[] = {
485 RISCV_S0
, RISCV_S1
, RISCV_S2
, RISCV_S3
, RISCV_S4
, RISCV_S5
,
486 RISCV_S6
, RISCV_S7
, RISCV_S8
, RISCV_S9
, RISCV_S10
, RISCV_S11
,
491 encode_regmask (guint32 regmask
)
497 for (i
= 0; i
< sizeof (callee_saved_regs
) / sizeof (int); ++i
) {
498 if (regmask
& (1 << callee_saved_regs
[i
])) {
500 regmask
-= (1 << callee_saved_regs
[i
]);
503 g_assert (regmask
== 0);
508 decode_regmask (guint32 regmask
)
514 for (i
= 0; i
< sizeof (callee_saved_regs
) / sizeof (int); ++i
)
515 if (regmask
& (1 << i
))
516 res
|= (1 << callee_saved_regs
[i
]);
523 * Encode the fixed fields of MAP into a buffer pointed to by BUF.
526 encode_gc_map (GCMap
*map
, guint8
*buf
, guint8
**endbuf
)
530 encode_sleb128 (map
->start_offset
/ SIZEOF_SLOT
, buf
, &buf
);
531 encode_sleb128 (map
->end_offset
/ SIZEOF_SLOT
, buf
, &buf
);
532 encode_sleb128 (map
->map_offset
/ SIZEOF_SLOT
, buf
, &buf
);
533 encode_uleb128 (map
->nslots
, buf
, &buf
);
534 g_assert (map
->callsite_entry_size
<= 4);
535 freg
= encode_frame_reg (map
->frame_reg
);
537 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);
538 encode_uleb128 (flags
, buf
, &buf
);
539 encode_uleb128 (encode_regmask (map
->used_int_regs
), buf
, &buf
);
540 if (map
->has_ref_regs
)
541 encode_uleb128 (encode_regmask (map
->reg_ref_mask
), buf
, &buf
);
542 if (map
->has_pin_regs
)
543 encode_uleb128 (encode_regmask (map
->reg_pin_mask
), buf
, &buf
);
544 encode_uleb128 (map
->ncallsites
, buf
, &buf
);
552 * Decode the encoded GC map representation in BUF and store the result into MAP.
555 decode_gc_map (guint8
*buf
, GCMap
*map
, guint8
**endbuf
)
558 int stack_bitmap_size
, reg_ref_bitmap_size
, reg_pin_bitmap_size
, offset
, freg
;
561 map
->start_offset
= decode_sleb128 (buf
, &buf
) * SIZEOF_SLOT
;
562 map
->end_offset
= decode_sleb128 (buf
, &buf
) * SIZEOF_SLOT
;
563 map
->map_offset
= decode_sleb128 (buf
, &buf
) * SIZEOF_SLOT
;
564 map
->nslots
= decode_uleb128 (buf
, &buf
);
565 flags
= decode_uleb128 (buf
, &buf
);
566 map
->has_ref_slots
= (flags
& 1) ? 1 : 0;
567 map
->has_pin_slots
= (flags
& 2) ? 1 : 0;
568 map
->has_ref_regs
= (flags
& 4) ? 1 : 0;
569 map
->has_pin_regs
= (flags
& 8) ? 1 : 0;
570 map
->callsite_entry_size
= ((flags
>> 4) & 0x3) + 1;
572 map
->frame_reg
= decode_frame_reg (freg
);
573 map
->used_int_regs
= decode_regmask (decode_uleb128 (buf
, &buf
));
574 if (map
->has_ref_regs
) {
575 map
->reg_ref_mask
= decode_regmask (decode_uleb128 (buf
, &buf
));
577 for (i
= 0; i
< NREGS
; ++i
)
578 if (map
->reg_ref_mask
& (1 << i
))
582 if (map
->has_pin_regs
) {
583 map
->reg_pin_mask
= decode_regmask (decode_uleb128 (buf
, &buf
));
585 for (i
= 0; i
< NREGS
; ++i
)
586 if (map
->reg_pin_mask
& (1 << i
))
590 map
->ncallsites
= decode_uleb128 (buf
, &buf
);
592 stack_bitmap_size
= (ALIGN_TO (map
->nslots
, 8) / 8) * map
->ncallsites
;
593 reg_ref_bitmap_size
= (ALIGN_TO (map
->nref_regs
, 8) / 8) * map
->ncallsites
;
594 reg_pin_bitmap_size
= (ALIGN_TO (map
->npin_regs
, 8) / 8) * map
->ncallsites
;
596 map
->stack_ref_bitmap_offset
= offset
;
597 if (map
->has_ref_slots
)
598 offset
+= stack_bitmap_size
;
599 map
->stack_pin_bitmap_offset
= offset
;
600 if (map
->has_pin_slots
)
601 offset
+= stack_bitmap_size
;
602 map
->reg_ref_bitmap_offset
= offset
;
603 if (map
->has_ref_regs
)
604 offset
+= reg_ref_bitmap_size
;
605 map
->reg_pin_bitmap_offset
= offset
;
606 if (map
->has_pin_regs
)
607 offset
+= reg_pin_bitmap_size
;
613 thread_attach_func (void)
617 tls
= g_new0 (TlsData
, 1);
618 tls
->tid
= mono_native_thread_id_get ();
619 tls
->info
= mono_thread_info_current ();
620 UnlockedAdd (&stats
.tlsdata_size
, sizeof (TlsData
));
626 thread_detach_func (gpointer user_data
)
628 TlsData
*tls
= user_data
;
634 thread_suspend_func (gpointer user_data
, void *sigctx
, MonoContext
*ctx
)
636 TlsData
*tls
= user_data
;
639 /* Happens during startup */
643 if (tls
->tid
!= mono_native_thread_id_get ()) {
644 /* Happens on osx because threads are not suspended using signals */
649 g_assert (tls
->info
);
653 res
= mono_thread_state_init_from_handle (&tls
->unwind_state
, tls
->info
, NULL
);
656 tls
->unwind_state
.unwind_data
[MONO_UNWIND_DATA_LMF
] = mono_get_lmf ();
658 mono_sigctx_to_monoctx (sigctx
, &tls
->unwind_state
.ctx
);
659 tls
->unwind_state
.valid
= TRUE
;
661 memcpy (&tls
->unwind_state
.ctx
, ctx
, sizeof (MonoContext
));
662 tls
->unwind_state
.valid
= TRUE
;
664 tls
->unwind_state
.valid
= FALSE
;
666 tls
->unwind_state
.unwind_data
[MONO_UNWIND_DATA_JIT_TLS
] = mono_tls_get_jit_tls ();
667 tls
->unwind_state
.unwind_data
[MONO_UNWIND_DATA_DOMAIN
] = mono_domain_get ();
670 if (!tls
->unwind_state
.unwind_data
[MONO_UNWIND_DATA_DOMAIN
]) {
671 /* Happens during startup */
672 tls
->unwind_state
.valid
= FALSE
;
677 #define DEAD_REF ((gpointer)(gssize)0x2a2a2a2a2a2a2a2aULL)
680 set_bit (guint8
*bitmap
, int width
, int y
, int x
)
682 bitmap
[(width
* y
) + (x
/ 8)] |= (1 << (x
% 8));
686 clear_bit (guint8
*bitmap
, int width
, int y
, int x
)
688 bitmap
[(width
* y
) + (x
/ 8)] &= ~(1 << (x
% 8));
692 get_bit (guint8
*bitmap
, int width
, int y
, int x
)
694 return bitmap
[(width
* y
) + (x
/ 8)] & (1 << (x
% 8));
698 slot_type_to_string (GCSlotType type
)
708 g_assert_not_reached ();
714 get_frame_pointer (MonoContext
*ctx
, int frame_reg
)
716 #if defined(TARGET_AMD64)
717 if (frame_reg
== AMD64_RSP
)
719 else if (frame_reg
== AMD64_RBP
)
721 #elif defined(TARGET_X86)
722 if (frame_reg
== X86_ESP
)
724 else if (frame_reg
== X86_EBP
)
726 #elif defined(TARGET_ARM)
727 if (frame_reg
== ARMREG_SP
)
728 return (host_mgreg_t
)MONO_CONTEXT_GET_SP (ctx
);
729 else if (frame_reg
== ARMREG_FP
)
730 return (host_mgreg_t
)MONO_CONTEXT_GET_BP (ctx
);
731 #elif defined(TARGET_S390X)
732 if (frame_reg
== S390_SP
)
733 return (host_mgreg_t
)MONO_CONTEXT_GET_SP (ctx
);
734 else if (frame_reg
== S390_FP
)
735 return (host_mgreg_t
)MONO_CONTEXT_GET_BP (ctx
);
736 #elif defined (TARGET_RISCV)
737 if (frame_reg
== RISCV_SP
)
738 return MONO_CONTEXT_GET_SP (ctx
);
739 else if (frame_reg
== RISCV_FP
)
740 return MONO_CONTEXT_GET_BP (ctx
);
742 g_assert_not_reached ();
747 * conservatively_pass:
749 * Mark a thread stack conservatively and collect information needed by the precise pass.
752 conservative_pass (TlsData
*tls
, guint8
*stack_start
, guint8
*stack_end
)
756 MonoContext ctx
, new_ctx
;
759 gboolean last
= TRUE
;
763 guint8
* fp
, *p
, *real_frame_start
, *frame_start
, *frame_end
;
764 int i
, pc_offset
, cindex
, bitmap_width
;
765 int scanned
= 0, scanned_precisely
, scanned_conservatively
, scanned_registers
;
767 StackFrameInfo frame
;
768 host_mgreg_t
*reg_locations
[MONO_MAX_IREGS
];
769 host_mgreg_t
*new_reg_locations
[MONO_MAX_IREGS
];
772 guint32 precise_regmask
;
776 tls
->ref_to_track
= NULL
;
779 /* tls == NULL can happen during startup */
780 if (mono_thread_internal_current () == NULL
|| !tls
) {
781 mono_gc_conservatively_scan_area (stack_start
, stack_end
);
782 UnlockedAdd (&stats
.scanned_stacks
, stack_end
- stack_start
);
786 lmf
= tls
->unwind_state
.unwind_data
[MONO_UNWIND_DATA_LMF
];
789 /* Number of bytes scanned based on GC map data */
791 /* Number of bytes scanned precisely based on GC map data */
792 scanned_precisely
= 0;
793 /* Number of bytes scanned conservatively based on GC map data */
794 scanned_conservatively
= 0;
795 /* Number of bytes scanned conservatively in register save areas */
796 scanned_registers
= 0;
798 /* This is one past the last address which we have scanned */
799 stack_limit
= stack_start
;
801 if (!tls
->unwind_state
.valid
)
802 memset (&new_ctx
, 0, sizeof (ctx
));
804 memcpy (&new_ctx
, &tls
->unwind_state
.ctx
, sizeof (MonoContext
));
806 memset (reg_locations
, 0, sizeof (reg_locations
));
807 memset (new_reg_locations
, 0, sizeof (new_reg_locations
));
810 if (!tls
->unwind_state
.valid
)
813 memcpy (&ctx
, &new_ctx
, sizeof (ctx
));
815 for (i
= 0; i
< MONO_MAX_IREGS
; ++i
) {
816 if (new_reg_locations
[i
]) {
818 * If the current frame saves the register, it means it might modify its
819 * value, thus the old location might not contain the same value, so
820 * we have to mark it conservatively.
822 if (reg_locations
[i
]) {
823 DEBUG (fprintf (logfile
, "\tscan saved reg %s location %p.\n", mono_arch_regname (i
), reg_locations
[i
]));
824 mono_gc_conservatively_scan_area (reg_locations
[i
], (char*)reg_locations
[i
] + SIZEOF_SLOT
);
825 scanned_registers
+= SIZEOF_SLOT
;
828 reg_locations
[i
] = new_reg_locations
[i
];
830 DEBUG (fprintf (logfile
, "\treg %s is now at location %p.\n", mono_arch_regname (i
), reg_locations
[i
]));
834 g_assert ((gsize
)stack_limit
% SIZEOF_SLOT
== 0);
836 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
);
842 // FIXME: For skipped frames, scan the param area of the parent frame conservatively ?
843 // FIXME: trampolines
845 if (frame
.type
== FRAME_TYPE_MANAGED_TO_NATIVE
) {
847 * These frames are problematic for several reasons:
848 * - they are unwound through an LMF, and we have no precise register tracking for those.
849 * - the LMF might not contain a precise ip, so we can't compute the call site.
850 * - the LMF only unwinds to the wrapper frame, so we get these methods twice.
852 DEBUG (fprintf (logfile
, "Mark(0): <Managed-to-native transition>\n"));
853 for (i
= 0; i
< MONO_MAX_IREGS
; ++i
) {
854 if (reg_locations
[i
]) {
855 DEBUG (fprintf (logfile
, "\tscan saved reg %s location %p.\n", mono_arch_regname (i
), reg_locations
[i
]));
856 mono_gc_conservatively_scan_area (reg_locations
[i
], (char*)reg_locations
[i
] + SIZEOF_SLOT
);
857 scanned_registers
+= SIZEOF_SLOT
;
859 reg_locations
[i
] = NULL
;
860 new_reg_locations
[i
] = NULL
;
867 method
= jinfo_get_method (ji
);
871 /* The last frame can be in any state so mark conservatively */
874 DEBUG (char *fname
= mono_method_full_name (method
, TRUE
); fprintf (logfile
, "Mark(0): %s+0x%x (%p)\n", fname
, pc_offset
, (gpointer
)MONO_CONTEXT_GET_IP (&ctx
)); g_free (fname
));
876 DEBUG (fprintf (logfile
, "\t <Last frame>\n"));
879 * new_reg_locations is not precise when a method is interrupted during its epilog, so clear it.
881 for (i
= 0; i
< MONO_MAX_IREGS
; ++i
) {
882 if (reg_locations
[i
]) {
883 DEBUG (fprintf (logfile
, "\tscan saved reg %s location %p.\n", mono_arch_regname (i
), reg_locations
[i
]));
884 mono_gc_conservatively_scan_area (reg_locations
[i
], (char*)reg_locations
[i
] + SIZEOF_SLOT
);
885 scanned_registers
+= SIZEOF_SLOT
;
887 if (new_reg_locations
[i
]) {
888 DEBUG (fprintf (logfile
, "\tscan saved reg %s location %p.\n", mono_arch_regname (i
), new_reg_locations
[i
]));
889 mono_gc_conservatively_scan_area (new_reg_locations
[i
], (char*)new_reg_locations
[i
] + SIZEOF_SLOT
);
890 scanned_registers
+= SIZEOF_SLOT
;
892 reg_locations
[i
] = NULL
;
893 new_reg_locations
[i
] = NULL
;
898 pc_offset
= (guint8
*)MONO_CONTEXT_GET_IP (&ctx
) - (guint8
*)ji
->code_start
;
900 /* These frames are very problematic */
901 if (method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) {
902 DEBUG (char *fname
= mono_method_full_name (method
, TRUE
); fprintf (logfile
, "Mark(0): %s+0x%x (%p)\n", fname
, pc_offset
, (gpointer
)MONO_CONTEXT_GET_IP (&ctx
)); g_free (fname
));
903 DEBUG (fprintf (logfile
, "\tSkip.\n"));
907 /* All the other frames are at a call site */
909 if (tls
->nframes
== MAX_FRAMES
) {
911 * Can't save information since the array is full. So scan the rest of the
912 * stack conservatively.
914 DEBUG (fprintf (logfile
, "Mark (0): Frame stack full.\n"));
918 /* Scan the frame of this method */
921 * A frame contains the following:
926 * - localloc-ed memory
928 g_assert (pc_offset
>= 0);
933 DEBUG (char *fname
= mono_method_full_name (jinfo_get_method (ji
), TRUE
); fprintf (logfile
, "Mark(0): %s+0x%x (%p)\n", fname
, pc_offset
, (gpointer
)MONO_CONTEXT_GET_IP (&ctx
)); g_free (fname
));
934 DEBUG (fprintf (logfile
, "\tNo GC Map.\n"));
938 /* The embedded callsite table requires this */
939 g_assert (((gsize
)emap
% 4) == 0);
942 * Debugging aid to control the number of frames scanned precisely
944 if (!precise_frame_limit_inited
) {
945 char *mono_precise_count
= g_getenv ("MONO_PRECISE_COUNT");
946 if (mono_precise_count
) {
947 precise_frame_limit
= atoi (mono_precise_count
);
948 g_free (mono_precise_count
);
950 precise_frame_limit_inited
= TRUE
;
953 if (precise_frame_limit
!= -1) {
954 if (precise_frame_count
[FALSE
] == precise_frame_limit
)
955 printf ("LAST PRECISE FRAME: %s\n", mono_method_full_name (method
, TRUE
));
956 if (precise_frame_count
[FALSE
] > precise_frame_limit
)
959 precise_frame_count
[FALSE
] ++;
961 /* Decode the encoded GC map */
963 memset (map
, 0, sizeof (GCMap
));
964 decode_gc_map (&emap
->encoded
[0], map
, &p
);
965 p
= (guint8
*)ALIGN_TO (p
, map
->callsite_entry_size
);
966 map
->callsites
.offsets8
= p
;
967 p
+= map
->callsite_entry_size
* map
->ncallsites
;
970 fp
= (guint8
*)get_frame_pointer (&ctx
, map
->frame_reg
);
972 real_frame_start
= fp
+ map
->start_offset
;
973 frame_start
= fp
+ map
->start_offset
+ map
->map_offset
;
974 frame_end
= fp
+ map
->end_offset
;
976 DEBUG (char *fname
= mono_method_full_name (jinfo_get_method (ji
), 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
));
978 /* Find the callsite index */
979 if (map
->callsite_entry_size
== 1) {
980 for (i
= 0; i
< map
->ncallsites
; ++i
)
981 /* ip points inside the call instruction */
982 if (map
->callsites
.offsets8
[i
] == pc_offset
+ 1)
984 } else if (map
->callsite_entry_size
== 2) {
985 // FIXME: Use a binary search
986 for (i
= 0; i
< map
->ncallsites
; ++i
)
987 /* ip points inside the call instruction */
988 if (map
->callsites
.offsets16
[i
] == pc_offset
+ 1)
991 // FIXME: Use a binary search
992 for (i
= 0; i
< map
->ncallsites
; ++i
)
993 /* ip points inside the call instruction */
994 if (map
->callsites
.offsets32
[i
] == pc_offset
+ 1)
997 if (i
== map
->ncallsites
) {
998 printf ("Unable to find ip offset 0x%x in callsite list of %s.\n", pc_offset
+ 1, mono_method_full_name (method
, TRUE
));
999 g_assert_not_reached ();
1004 * This is not neccessary true on x86 because frames have a different size at each
1007 //g_assert (real_frame_start >= stack_limit);
1009 if (real_frame_start
> stack_limit
) {
1010 /* This scans the previously skipped frames as well */
1011 DEBUG (fprintf (logfile
, "\tscan area %p-%p (%d).\n", stack_limit
, real_frame_start
, (int)(real_frame_start
- stack_limit
)));
1012 mono_gc_conservatively_scan_area (stack_limit
, real_frame_start
);
1013 UnlockedAdd (&stats
.scanned_other
, real_frame_start
- stack_limit
);
1016 /* Mark stack slots */
1017 if (map
->has_pin_slots
) {
1018 int bitmap_width
= ALIGN_TO (map
->nslots
, 8) / 8;
1019 guint8
*pin_bitmap
= &bitmaps
[map
->stack_pin_bitmap_offset
+ (bitmap_width
* cindex
)];
1024 for (i
= 0; i
< map
->nslots
; ++i
) {
1025 pinned
= pin_bitmap
[i
/ 8] & (1 << (i
% 8));
1027 DEBUG (fprintf (logfile
, "\tscan slot %s0x%x(fp)=%p.\n", (guint8
*)p
> (guint8
*)fp
? "" : "-", ABS ((int)((gssize
)p
- (gssize
)fp
)), p
));
1028 mono_gc_conservatively_scan_area (p
, p
+ SIZEOF_SLOT
);
1029 scanned_conservatively
+= SIZEOF_SLOT
;
1031 scanned_precisely
+= SIZEOF_SLOT
;
1036 scanned_precisely
+= (map
->nslots
* SIZEOF_SLOT
);
1039 /* The area outside of start-end is NOREF */
1040 scanned_precisely
+= (map
->end_offset
- map
->start_offset
) - (map
->nslots
* SIZEOF_SLOT
);
1042 /* Mark registers */
1043 precise_regmask
= map
->used_int_regs
| (1 << map
->frame_reg
);
1044 if (map
->has_pin_regs
) {
1045 int bitmap_width
= ALIGN_TO (map
->npin_regs
, 8) / 8;
1046 guint8
*pin_bitmap
= &bitmaps
[map
->reg_pin_bitmap_offset
+ (bitmap_width
* cindex
)];
1048 for (i
= 0; i
< NREGS
; ++i
) {
1049 if (!(map
->used_int_regs
& (1 << i
)))
1052 if (!(map
->reg_pin_mask
& (1 << i
)))
1055 if (pin_bitmap
[bindex
/ 8] & (1 << (bindex
% 8))) {
1056 DEBUG (fprintf (logfile
, "\treg %s saved at 0x%p is pinning.\n", mono_arch_regname (i
), reg_locations
[i
]));
1057 precise_regmask
&= ~(1 << i
);
1063 scanned
+= map
->end_offset
- map
->start_offset
;
1065 g_assert (scanned
== scanned_precisely
+ scanned_conservatively
);
1067 stack_limit
= frame_end
;
1069 /* Save information for the precise pass */
1070 fi
= &tls
->frames
[tls
->nframes
];
1071 fi
->nslots
= map
->nslots
;
1072 bitmap_width
= ALIGN_TO (map
->nslots
, 8) / 8;
1073 if (map
->has_ref_slots
)
1074 fi
->bitmap
= &bitmaps
[map
->stack_ref_bitmap_offset
+ (bitmap_width
* cindex
)];
1077 fi
->frame_start_offset
= frame_start
- stack_start
;
1078 fi
->nreg_locations
= 0;
1079 DEBUG_PRECISE (fi
->ji
= ji
);
1080 DEBUG_PRECISE (fi
->fp
= fp
);
1082 if (map
->has_ref_regs
) {
1083 int bitmap_width
= ALIGN_TO (map
->nref_regs
, 8) / 8;
1084 guint8
*ref_bitmap
= &bitmaps
[map
->reg_ref_bitmap_offset
+ (bitmap_width
* cindex
)];
1086 for (i
= 0; i
< NREGS
; ++i
) {
1087 if (!(map
->reg_ref_mask
& (1 << i
)))
1090 if (reg_locations
[i
] && (ref_bitmap
[bindex
/ 8] & (1 << (bindex
% 8)))) {
1091 DEBUG_PRECISE (fi
->regs
[fi
->nreg_locations
] = i
);
1092 DEBUG (fprintf (logfile
, "\treg %s saved at 0x%p is ref.\n", mono_arch_regname (i
), reg_locations
[i
]));
1093 fi
->reg_locations
[fi
->nreg_locations
] = (guint8
*)reg_locations
[i
] - stack_start
;
1094 fi
->nreg_locations
++;
1101 * Clear locations of precisely tracked registers.
1103 if (precise_regmask
) {
1104 for (i
= 0; i
< NREGS
; ++i
) {
1105 if (precise_regmask
& (1 << i
)) {
1107 * The method uses this register, and we have precise info for it.
1108 * This means the location will be scanned precisely.
1109 * Tell the code at the beginning of the loop that this location is
1112 if (reg_locations
[i
])
1113 DEBUG (fprintf (logfile
, "\treg %s at location %p (==%p) is precise.\n", mono_arch_regname (i
), reg_locations
[i
], (gpointer
)*reg_locations
[i
]));
1114 reg_locations
[i
] = NULL
;
1122 /* Scan the remaining register save locations */
1123 for (i
= 0; i
< MONO_MAX_IREGS
; ++i
) {
1124 if (reg_locations
[i
]) {
1125 DEBUG (fprintf (logfile
, "\tscan saved reg location %p.\n", reg_locations
[i
]));
1126 mono_gc_conservatively_scan_area (reg_locations
[i
], (char*)reg_locations
[i
] + SIZEOF_SLOT
);
1127 scanned_registers
+= SIZEOF_SLOT
;
1129 if (new_reg_locations
[i
]) {
1130 DEBUG (fprintf (logfile
, "\tscan saved reg location %p.\n", new_reg_locations
[i
]));
1131 mono_gc_conservatively_scan_area (new_reg_locations
[i
], (char*)new_reg_locations
[i
] + SIZEOF_SLOT
);
1132 scanned_registers
+= SIZEOF_SLOT
;
1136 if (stack_limit
< stack_end
) {
1137 DEBUG (fprintf (logfile
, "\tscan remaining stack %p-%p (%d).\n", stack_limit
, stack_end
, (int)(stack_end
- stack_limit
)));
1138 mono_gc_conservatively_scan_area (stack_limit
, stack_end
);
1139 UnlockedAdd (&stats
.scanned_native
, stack_end
- stack_limit
);
1142 DEBUG (fprintf (logfile
, "Marked %d bytes, p=%d,c=%d out of %d.\n", scanned
, scanned_precisely
, scanned_conservatively
, (int)(stack_end
- stack_start
)));
1144 UnlockedAdd (&stats
.scanned_stacks
, stack_end
- stack_start
);
1145 UnlockedAdd (&stats
.scanned
, scanned
);
1146 UnlockedAdd (&stats
.scanned_precisely
, scanned_precisely
);
1147 UnlockedAdd (&stats
.scanned_conservatively
, scanned_conservatively
);
1148 UnlockedAdd (&stats
.scanned_registers
, scanned_registers
);
1150 //mono_gc_conservatively_scan_area (stack_start, stack_end);
1156 * Mark a thread stack precisely based on information saved during the conservative
1160 precise_pass (TlsData
*tls
, guint8
*stack_start
, guint8
*stack_end
, void *gc_data
)
1164 guint8
*frame_start
;
1169 if (!tls
->unwind_state
.valid
)
1172 for (findex
= 0; findex
< tls
->nframes
; findex
++) {
1173 /* Load information saved by the !precise pass */
1174 fi
= &tls
->frames
[findex
];
1175 frame_start
= stack_start
+ fi
->frame_start_offset
;
1177 DEBUG (char *fname
= mono_method_full_name (jinfo_get_method (fi
->ji
), TRUE
); fprintf (logfile
, "Mark(1): %s\n", fname
); g_free (fname
));
1180 * FIXME: Add a function to mark using a bitmap, to avoid doing a
1181 * call for each object.
1184 /* Mark stack slots */
1186 guint8
*ref_bitmap
= fi
->bitmap
;
1189 for (i
= 0; i
< fi
->nslots
; ++i
) {
1190 MonoObject
**ptr
= (MonoObject
**)(frame_start
+ (i
* SIZEOF_SLOT
));
1192 live
= ref_bitmap
[i
/ 8] & (1 << (i
% 8));
1195 MonoObject
*obj
= *ptr
;
1197 DEBUG (fprintf (logfile
, "\tref %s0x%x(fp)=%p: %p ->", (guint8
*)ptr
>= (guint8
*)fi
->fp
? "" : "-", ABS ((int)((gssize
)ptr
- (gssize
)fi
->fp
)), ptr
, obj
));
1198 *ptr
= mono_gc_scan_object (obj
, gc_data
);
1199 DEBUG (fprintf (logfile
, " %p.\n", *ptr
));
1201 DEBUG (fprintf (logfile
, "\tref %s0x%x(fp)=%p: %p.\n", (guint8
*)ptr
>= (guint8
*)fi
->fp
? "" : "-", ABS ((int)((gssize
)ptr
- (gssize
)fi
->fp
)), ptr
, obj
));
1206 * This is disabled because the pointer takes up a lot of space.
1207 * Stack slots might be shared between ref and non-ref variables ?
1209 if (map
->ref_slots
[i
/ 8] & (1 << (i
% 8))) {
1210 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
));
1212 * Fail fast if the live range is incorrect, and
1213 * the JITted code tries to access this object
1222 /* Mark registers */
1225 * Registers are different from stack slots, they have no address where they
1226 * are stored. Instead, some frame below this frame in the stack saves them
1227 * in its prolog to the stack. We can mark this location precisely.
1229 for (i
= 0; i
< fi
->nreg_locations
; ++i
) {
1231 * reg_locations [i] contains the address of the stack slot where
1232 * a reg was last saved, so mark that slot.
1234 MonoObject
**ptr
= (MonoObject
**)((guint8
*)stack_start
+ fi
->reg_locations
[i
]);
1235 MonoObject
*obj
= *ptr
;
1238 DEBUG (fprintf (logfile
, "\treg %s saved at %p: %p ->", mono_arch_regname (fi
->regs
[i
]), ptr
, obj
));
1239 *ptr
= mono_gc_scan_object (obj
, gc_data
);
1240 DEBUG (fprintf (logfile
, " %p.\n", *ptr
));
1242 DEBUG (fprintf (logfile
, "\treg %s saved at %p: %p\n", mono_arch_regname (fi
->regs
[i
]), ptr
, obj
));
1248 * Debugging aid to check for missed refs.
1250 if (tls
->ref_to_track
) {
1253 for (p
= (gpointer
*)stack_start
; p
< (gpointer
*)stack_end
; ++p
)
1254 if (*p
== tls
->ref_to_track
)
1255 printf ("REF AT %p.\n", p
);
1262 * This is called by the GC twice to mark a thread stack. PRECISE is FALSE at the first
1263 * call, and TRUE at the second. USER_DATA points to a TlsData
1264 * structure filled up by thread_suspend_func.
1267 thread_mark_func (gpointer user_data
, guint8
*stack_start
, guint8
*stack_end
, gboolean precise
, void *gc_data
)
1269 TlsData
*tls
= user_data
;
1271 DEBUG (fprintf (logfile
, "****************************************\n"));
1272 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
));
1273 DEBUG (fprintf (logfile
, "****************************************\n"));
1276 conservative_pass (tls
, stack_start
, stack_end
);
1278 precise_pass (tls
, stack_start
, stack_end
, gc_data
);
1284 mini_gc_init_gc_map (MonoCompile
*cfg
)
1286 if (COMPILE_LLVM (cfg
))
1289 if (!mono_gc_is_moving ())
1292 if (cfg
->compile_aot
) {
1293 if (!enable_gc_maps_for_aot
)
1295 } else if (!mono_gc_precise_stack_mark_enabled ())
1299 /* Debugging support */
1301 static int precise_count
;
1304 char *mono_gcmap_count
= g_getenv ("MONO_GCMAP_COUNT");
1305 if (mono_gcmap_count
) {
1306 int count
= atoi (mono_gcmap_count
);
1307 g_free (mono_gcmap_count
);
1308 if (precise_count
== count
)
1309 printf ("LAST: %s\n", mono_method_full_name (cfg
->method
, TRUE
));
1310 if (precise_count
> count
)
1316 cfg
->compute_gc_maps
= TRUE
;
1318 cfg
->gc_info
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoCompileGC
));
1322 * mini_gc_set_slot_type_from_fp:
1324 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1325 * relative to the frame pointer. By default, all stack slots are type PIN, so there is no
1326 * need to call this function for those slots.
1329 mini_gc_set_slot_type_from_fp (MonoCompile
*cfg
, int slot_offset
, GCSlotType type
)
1331 MonoCompileGC
*gcfg
= (MonoCompileGC
*)cfg
->gc_info
;
1333 if (!cfg
->compute_gc_maps
)
1336 g_assert (slot_offset
% SIZEOF_SLOT
== 0);
1338 gcfg
->stack_slots_from_fp
= g_slist_prepend_mempool (cfg
->mempool
, gcfg
->stack_slots_from_fp
, GINT_TO_POINTER (((slot_offset
) << 16) | type
));
1342 * mini_gc_set_slot_type_from_cfa:
1344 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1345 * relative to the DWARF CFA value. This should be called from mono_arch_emit_prolog ().
1346 * If type is STACK_REF, the slot is assumed to be live from the end of the prolog until
1347 * the end of the method. By default, all stack slots are type PIN, so there is no need to
1348 * call this function for those slots.
1351 mini_gc_set_slot_type_from_cfa (MonoCompile
*cfg
, int slot_offset
, GCSlotType type
)
1353 MonoCompileGC
*gcfg
= (MonoCompileGC
*)cfg
->gc_info
;
1354 int slot
= - (slot_offset
/ SIZEOF_SLOT
);
1356 if (!cfg
->compute_gc_maps
)
1359 g_assert (slot_offset
<= 0);
1360 g_assert (slot_offset
% SIZEOF_SLOT
== 0);
1362 gcfg
->stack_slots_from_cfa
= g_slist_prepend_mempool (cfg
->mempool
, gcfg
->stack_slots_from_cfa
, GUINT_TO_POINTER (((slot
) << 16) | type
));
1366 fp_offset_to_slot (MonoCompile
*cfg
, int offset
)
1368 MonoCompileGC
*gcfg
= cfg
->gc_info
;
1370 return (offset
- gcfg
->min_offset
) / SIZEOF_SLOT
;
1374 slot_to_fp_offset (MonoCompile
*cfg
, int slot
)
1376 MonoCompileGC
*gcfg
= cfg
->gc_info
;
1378 return (slot
* SIZEOF_SLOT
) + gcfg
->min_offset
;
1381 static MONO_ALWAYS_INLINE
void
1382 set_slot (MonoCompileGC
*gcfg
, int slot
, int callsite_index
, GCSlotType type
)
1384 g_assert (slot
>= 0 && slot
< gcfg
->nslots
);
1386 if (type
== SLOT_PIN
) {
1387 clear_bit (gcfg
->stack_ref_bitmap
, gcfg
->stack_bitmap_width
, slot
, callsite_index
);
1388 set_bit (gcfg
->stack_pin_bitmap
, gcfg
->stack_bitmap_width
, slot
, callsite_index
);
1389 } else if (type
== SLOT_REF
) {
1390 set_bit (gcfg
->stack_ref_bitmap
, gcfg
->stack_bitmap_width
, slot
, callsite_index
);
1391 clear_bit (gcfg
->stack_pin_bitmap
, gcfg
->stack_bitmap_width
, slot
, callsite_index
);
1392 } else if (type
== SLOT_NOREF
) {
1393 clear_bit (gcfg
->stack_ref_bitmap
, gcfg
->stack_bitmap_width
, slot
, callsite_index
);
1394 clear_bit (gcfg
->stack_pin_bitmap
, gcfg
->stack_bitmap_width
, slot
, callsite_index
);
1399 set_slot_everywhere (MonoCompileGC
*gcfg
, int slot
, GCSlotType type
)
1402 guint8
*ref_bitmap
, *pin_bitmap
;
1407 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1408 set_slot (gcfg, slot, cindex, type);
1410 ref_bitmap
= gcfg
->stack_ref_bitmap
;
1411 pin_bitmap
= gcfg
->stack_pin_bitmap
;
1412 width
= gcfg
->stack_bitmap_width
;
1415 if (type
== SLOT_PIN
) {
1416 memset (ref_bitmap
+ pos
, 0, width
);
1417 memset (pin_bitmap
+ pos
, 0xff, width
);
1418 } else if (type
== SLOT_REF
) {
1419 memset (ref_bitmap
+ pos
, 0xff, width
);
1420 memset (pin_bitmap
+ pos
, 0, width
);
1421 } else if (type
== SLOT_NOREF
) {
1422 memset (ref_bitmap
+ pos
, 0, width
);
1423 memset (pin_bitmap
+ pos
, 0, width
);
1428 set_slot_in_range (MonoCompileGC
*gcfg
, int slot
, int from
, int to
, GCSlotType type
)
1432 for (cindex
= 0; cindex
< gcfg
->ncallsites
; ++cindex
) {
1433 int callsite_offset
= gcfg
->callsites
[cindex
]->pc_offset
;
1434 if (callsite_offset
>= from
&& callsite_offset
< to
)
1435 set_slot (gcfg
, slot
, cindex
, type
);
1440 set_reg_slot (MonoCompileGC
*gcfg
, int slot
, int callsite_index
, GCSlotType type
)
1442 g_assert (slot
>= 0 && slot
< gcfg
->nregs
);
1444 if (type
== SLOT_PIN
) {
1445 clear_bit (gcfg
->reg_ref_bitmap
, gcfg
->reg_bitmap_width
, slot
, callsite_index
);
1446 set_bit (gcfg
->reg_pin_bitmap
, gcfg
->reg_bitmap_width
, slot
, callsite_index
);
1447 } else if (type
== SLOT_REF
) {
1448 set_bit (gcfg
->reg_ref_bitmap
, gcfg
->reg_bitmap_width
, slot
, callsite_index
);
1449 clear_bit (gcfg
->reg_pin_bitmap
, gcfg
->reg_bitmap_width
, slot
, callsite_index
);
1450 } else if (type
== SLOT_NOREF
) {
1451 clear_bit (gcfg
->reg_ref_bitmap
, gcfg
->reg_bitmap_width
, slot
, callsite_index
);
1452 clear_bit (gcfg
->reg_pin_bitmap
, gcfg
->reg_bitmap_width
, slot
, callsite_index
);
1457 set_reg_slot_everywhere (MonoCompileGC
*gcfg
, int slot
, GCSlotType type
)
1461 for (cindex
= 0; cindex
< gcfg
->ncallsites
; ++cindex
)
1462 set_reg_slot (gcfg
, slot
, cindex
, type
);
1466 set_reg_slot_in_range (MonoCompileGC
*gcfg
, int slot
, int from
, int to
, GCSlotType type
)
1470 for (cindex
= 0; cindex
< gcfg
->ncallsites
; ++cindex
) {
1471 int callsite_offset
= gcfg
->callsites
[cindex
]->pc_offset
;
1472 if (callsite_offset
>= from
&& callsite_offset
< to
)
1473 set_reg_slot (gcfg
, slot
, cindex
, type
);
1478 process_spill_slots (MonoCompile
*cfg
)
1480 MonoCompileGC
*gcfg
= cfg
->gc_info
;
1485 /* Mark all ref/pin spill slots as NOREF by default outside of their live range */
1486 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
1487 for (l
= bb
->spill_slot_defs
; l
; l
= l
->next
) {
1488 MonoInst
*def
= l
->data
;
1489 int spill_slot
= def
->inst_c0
;
1490 int bank
= def
->inst_c1
;
1491 int offset
= cfg
->spill_info
[bank
][spill_slot
].offset
;
1492 int slot
= fp_offset_to_slot (cfg
, offset
);
1494 if (bank
== MONO_REG_INT_MP
|| bank
== MONO_REG_INT_REF
)
1495 set_slot_everywhere (gcfg
, slot
, SLOT_NOREF
);
1499 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
1500 for (l
= bb
->spill_slot_defs
; l
; l
= l
->next
) {
1501 MonoInst
*def
= l
->data
;
1502 int spill_slot
= def
->inst_c0
;
1503 int bank
= def
->inst_c1
;
1504 int offset
= cfg
->spill_info
[bank
][spill_slot
].offset
;
1505 int slot
= fp_offset_to_slot (cfg
, offset
);
1508 if (bank
== MONO_REG_INT_MP
)
1514 * Extend the live interval for the GC tracked spill slots
1515 * defined in this bblock.
1516 * FIXME: This is not needed.
1518 set_slot_in_range (gcfg
, slot
, def
->backend
.pc_offset
, bb
->native_offset
+ bb
->native_length
, type
);
1520 if (cfg
->verbose_level
> 1)
1521 printf ("\t%s spill slot at %s0x%x(fp) (slot = %d)\n", slot_type_to_string (type
), offset
>= 0 ? "" : "-", ABS (offset
), slot
);
1525 /* Set fp spill slots to NOREF */
1526 for (i
= 0; i
< cfg
->spill_info_len
[MONO_REG_DOUBLE
]; ++i
) {
1527 int offset
= cfg
->spill_info
[MONO_REG_DOUBLE
][i
].offset
;
1533 slot
= fp_offset_to_slot (cfg
, offset
);
1535 set_slot_everywhere (gcfg
, slot
, SLOT_NOREF
);
1537 if (cfg
->verbose_level
> 1)
1538 printf ("\tfp spill slot at %s0x%x(fp) (slot = %d)\n", offset
>= 0 ? "" : "-", ABS (offset
), slot
);
1541 /* Set int spill slots to NOREF */
1542 for (i
= 0; i
< cfg
->spill_info_len
[MONO_REG_INT
]; ++i
) {
1543 int offset
= cfg
->spill_info
[MONO_REG_INT
][i
].offset
;
1549 slot
= fp_offset_to_slot (cfg
, offset
);
1551 set_slot_everywhere (gcfg
, slot
, SLOT_NOREF
);
1552 if (cfg
->verbose_level
> 1)
1553 printf ("\tint spill slot at %s0x%x(fp) (slot = %d)\n", offset
>= 0 ? "" : "-", ABS (offset
), slot
);
1558 * process_other_slots:
1560 * Process stack slots registered using mini_gc_set_slot_type_... ().
1563 process_other_slots (MonoCompile
*cfg
)
1565 MonoCompileGC
*gcfg
= cfg
->gc_info
;
1568 /* Relative to the CFA */
1569 for (l
= gcfg
->stack_slots_from_cfa
; l
; l
= l
->next
) {
1570 guint data
= GPOINTER_TO_UINT (l
->data
);
1571 int cfa_slot
= data
>> 16;
1572 GCSlotType type
= data
& 0xff;
1576 * Map the cfa relative slot to an fp relative slot.
1577 * slot_addr == cfa - <cfa_slot>*4/8
1578 * fp + cfa_offset == cfa
1579 * -> slot_addr == fp + (cfa_offset - <cfa_slot>*4/8)
1581 slot
= (cfg
->cfa_offset
/ SIZEOF_SLOT
) - cfa_slot
- (gcfg
->min_offset
/ SIZEOF_SLOT
);
1583 set_slot_everywhere (gcfg
, slot
, type
);
1585 if (cfg
->verbose_level
> 1) {
1586 int fp_offset
= slot_to_fp_offset (cfg
, slot
);
1587 if (type
== SLOT_NOREF
)
1588 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
));
1592 /* Relative to the FP */
1593 for (l
= gcfg
->stack_slots_from_fp
; l
; l
= l
->next
) {
1594 gint data
= GPOINTER_TO_INT (l
->data
);
1595 int offset
= data
>> 16;
1596 GCSlotType type
= data
& 0xff;
1599 slot
= fp_offset_to_slot (cfg
, offset
);
1601 set_slot_everywhere (gcfg
, slot
, type
);
1603 /* Liveness for these slots is handled by process_spill_slots () */
1605 if (cfg
->verbose_level
> 1) {
1606 if (type
== SLOT_REF
)
1607 printf ("\tref slot at fp+0x%x (slot = %d)\n", offset
, slot
);
1608 else if (type
== SLOT_NOREF
)
1609 printf ("\tnoref slot at 0x%x(fp) (slot = %d)\n", offset
, slot
);
1615 get_vtype_bitmap (MonoType
*t
, int *numbits
)
1617 MonoClass
*klass
= mono_class_from_mono_type_internal (t
);
1619 if (klass
->generic_container
|| mono_class_is_open_constructed_type (t
)) {
1620 /* FIXME: Generic sharing */
1623 mono_class_compute_gc_descriptor (klass
);
1625 return mono_gc_get_bitmap_for_descr (klass
->gc_descr
, numbits
);
1630 get_offset_sign (int offset
)
1632 return offset
< 0 ? "-" : "+";
1636 get_offset_val (int offset
)
1638 return offset
< 0 ? (- offset
) : offset
;
1642 process_variables (MonoCompile
*cfg
)
1644 MonoCompileGC
*gcfg
= cfg
->gc_info
;
1645 MonoMethodSignature
*sig
= mono_method_signature_internal (cfg
->method
);
1646 int i
, locals_min_slot
, locals_max_slot
, cindex
;
1650 int locals_min_offset
= gcfg
->locals_min_offset
;
1651 int locals_max_offset
= gcfg
->locals_max_offset
;
1653 /* Slots for locals are NOREF by default */
1654 locals_min_slot
= (locals_min_offset
- gcfg
->min_offset
) / SIZEOF_SLOT
;
1655 locals_max_slot
= (locals_max_offset
- gcfg
->min_offset
) / SIZEOF_SLOT
;
1656 for (i
= locals_min_slot
; i
< locals_max_slot
; ++i
) {
1657 set_slot_everywhere (gcfg
, i
, SLOT_NOREF
);
1661 * Compute the offset where variables are initialized in the first bblock, if any.
1663 pc_offsets
= g_new0 (int, cfg
->next_vreg
);
1665 bb
= cfg
->bb_entry
->next_bb
;
1666 MONO_BB_FOR_EACH_INS (bb
, tmp
) {
1667 if (tmp
->opcode
== OP_GC_LIVENESS_DEF
) {
1668 int vreg
= tmp
->inst_c1
;
1669 if (pc_offsets
[vreg
] == 0) {
1670 g_assert (tmp
->backend
.pc_offset
> 0);
1671 pc_offsets
[vreg
] = tmp
->backend
.pc_offset
;
1677 * Stack slots holding arguments are initialized in the prolog.
1678 * This means we can treat them alive for the whole method.
1680 for (i
= 0; i
< cfg
->num_varinfo
; i
++) {
1681 MonoInst
*ins
= cfg
->varinfo
[i
];
1682 MonoType
*t
= ins
->inst_vtype
;
1685 gboolean byref
, is_this
= FALSE
;
1686 gboolean is_arg
= i
< cfg
->locals_start
;
1688 if (ins
== cfg
->ret
) {
1689 if (!(ins
->opcode
== OP_REGOFFSET
&& MONO_TYPE_ISSTRUCT (t
)))
1693 vmv
= MONO_VARINFO (cfg
, i
);
1695 /* For some reason, 'this' is byref */
1696 if (sig
->hasthis
&& ins
== cfg
->args
[0] && !cfg
->method
->klass
->valuetype
) {
1697 t
= m_class_get_byval_arg (cfg
->method
->klass
);
1703 if (ins
->opcode
== OP_REGVAR
) {
1705 GCSlotType slot_type
;
1707 t
= mini_get_underlying_type (t
);
1710 g_assert (hreg
< MONO_MAX_IREGS
);
1713 slot_type
= SLOT_PIN
;
1715 slot_type
= mini_type_is_reference (t
) ? SLOT_REF
: SLOT_NOREF
;
1717 if (slot_type
== SLOT_PIN
) {
1718 /* These have no live interval, be conservative */
1719 set_reg_slot_everywhere (gcfg
, hreg
, slot_type
);
1722 * Unlike variables allocated to the stack, we generate liveness info
1723 * for noref vars in registers in mono_spill_global_vars (), because
1724 * knowing that a register doesn't contain a ref allows us to mark its save
1725 * locations precisely.
1727 for (cindex
= 0; cindex
< gcfg
->ncallsites
; ++cindex
)
1728 if (gcfg
->callsites
[cindex
]->liveness
[i
/ 8] & (1 << (i
% 8)))
1729 set_reg_slot (gcfg
, hreg
, cindex
, slot_type
);
1732 if (cfg
->verbose_level
> 1) {
1733 printf ("\t%s %sreg %s(R%d)\n", slot_type_to_string (slot_type
), is_arg
? "arg " : "", mono_arch_regname (hreg
), vmv
->vreg
);
1739 if (ins
->opcode
!= OP_REGOFFSET
)
1742 if (ins
->inst_offset
% SIZEOF_SLOT
!= 0)
1745 pos
= fp_offset_to_slot (cfg
, ins
->inst_offset
);
1747 if (is_arg
&& ins
->flags
& MONO_INST_IS_DEAD
) {
1748 /* These do not get stored in the prolog */
1749 set_slot_everywhere (gcfg
, pos
, SLOT_NOREF
);
1751 if (cfg
->verbose_level
> 1) {
1752 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
));
1757 if (MONO_TYPE_ISSTRUCT (t
)) {
1759 gsize
*bitmap
= NULL
;
1760 gboolean pin
= FALSE
;
1764 if (ins
->backend
.is_pinvoke
)
1765 size
= mono_class_native_size (ins
->klass
, NULL
);
1767 size
= mono_class_value_size (ins
->klass
, NULL
);
1768 size_in_slots
= ALIGN_TO (size
, SIZEOF_SLOT
) / SIZEOF_SLOT
;
1770 if (cfg
->verbose_level
> 1)
1771 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
));
1773 if (!ins
->klass
->has_references
) {
1775 for (j
= 0; j
< size_in_slots
; ++j
)
1776 set_slot_everywhere (gcfg
, pos
+ j
, SLOT_NOREF
);
1781 bitmap
= get_vtype_bitmap (t
, &numbits
);
1786 * Most vtypes are marked volatile because of the LDADDR instructions,
1787 * and they have no liveness information since they are decomposed
1788 * before the liveness pass. We emit OP_GC_LIVENESS_DEF instructions for
1789 * them during VZERO decomposition.
1792 if (!pc_offsets
[vmv
->vreg
])
1795 if (ins
->backend
.is_pinvoke
)
1800 for (cindex
= 0; cindex
< gcfg
->ncallsites
; ++cindex
) {
1801 if (gcfg
->callsites
[cindex
]->pc_offset
> pc_offsets
[vmv
->vreg
]) {
1802 for (j
= 0; j
< numbits
; ++j
) {
1803 if (bitmap
[j
/ GC_BITS_PER_WORD
] & ((gsize
)1 << (j
% GC_BITS_PER_WORD
))) {
1804 /* The descriptor is for the boxed object */
1805 set_slot (gcfg
, (pos
+ j
- (MONO_ABI_SIZEOF (MonoObject
) / SIZEOF_SLOT
)), cindex
, pin
? SLOT_PIN
: SLOT_REF
);
1811 if (cfg
->verbose_level
> 1) {
1812 for (j
= 0; j
< numbits
; ++j
) {
1813 if (bitmap
[j
/ GC_BITS_PER_WORD
] & ((gsize
)1 << (j
% GC_BITS_PER_WORD
)))
1814 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
- (MONO_ABI_SIZEOF (MonoObject
) / SIZEOF_SLOT
)));
1818 if (cfg
->verbose_level
> 1)
1819 printf ("\t\tpinned\n");
1820 for (j
= 0; j
< size_in_slots
; ++j
) {
1821 set_slot_everywhere (gcfg
, pos
+ j
, SLOT_PIN
);
1830 if (!is_arg
&& (ins
->inst_offset
< gcfg
->min_offset
|| ins
->inst_offset
>= gcfg
->max_offset
))
1831 /* Vret addr etc. */
1836 set_slot_everywhere (gcfg
, pos
, SLOT_PIN
);
1838 for (cindex
= 0; cindex
< gcfg
->ncallsites
; ++cindex
)
1839 if (gcfg
->callsites
[cindex
]->liveness
[i
/ 8] & (1 << (i
% 8)))
1840 set_slot (gcfg
, pos
, cindex
, SLOT_PIN
);
1842 if (cfg
->verbose_level
> 1)
1843 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
));
1848 * This is currently disabled, but could be enabled to debug crashes.
1851 if (t
->type
== MONO_TYPE_I
) {
1853 * Variables created in mono_handle_global_vregs have type I, but they
1854 * could hold GC refs since the vregs they were created from might not been
1855 * marked as holding a GC ref. So be conservative.
1857 set_slot_everywhere (gcfg
, pos
, SLOT_PIN
);
1862 t
= mini_get_underlying_type (t
);
1864 if (!mini_type_is_reference (t
)) {
1865 set_slot_everywhere (gcfg
, pos
, SLOT_NOREF
);
1866 if (cfg
->verbose_level
> 1)
1867 printf ("\tnoref%s at %s0x%x(fp) (R%d, slot = %d): %s\n", (is_arg
? " arg" : ""), 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
));
1868 if (!t
->byref
&& sizeof (host_mgreg_t
) == 4 && (t
->type
== MONO_TYPE_I8
|| t
->type
== MONO_TYPE_U8
|| t
->type
== MONO_TYPE_R8
)) {
1869 set_slot_everywhere (gcfg
, pos
+ 1, SLOT_NOREF
);
1870 if (cfg
->verbose_level
> 1)
1871 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
));
1876 /* 'this' is marked INDIRECT for gshared methods */
1877 if (ins
->flags
& (MONO_INST_VOLATILE
| MONO_INST_INDIRECT
) && !is_this
) {
1879 * For volatile variables, treat them alive from the point they are
1880 * initialized in the first bblock until the end of the method.
1883 set_slot_everywhere (gcfg
, pos
, SLOT_REF
);
1884 } else if (pc_offsets
[vmv
->vreg
]) {
1885 set_slot_in_range (gcfg
, pos
, 0, pc_offsets
[vmv
->vreg
], SLOT_PIN
);
1886 set_slot_in_range (gcfg
, pos
, pc_offsets
[vmv
->vreg
], cfg
->code_size
, SLOT_REF
);
1888 set_slot_everywhere (gcfg
, pos
, SLOT_PIN
);
1890 if (cfg
->verbose_level
> 1)
1891 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
));
1896 /* Live for the whole method */
1897 set_slot_everywhere (gcfg
, pos
, SLOT_REF
);
1899 for (cindex
= 0; cindex
< gcfg
->ncallsites
; ++cindex
)
1900 if (gcfg
->callsites
[cindex
]->liveness
[i
/ 8] & (1 << (i
% 8)))
1901 set_slot (gcfg
, pos
, cindex
, SLOT_REF
);
1904 if (cfg
->verbose_level
> 1) {
1905 printf ("\tref%s at %s0x%x(fp) (R%d, slot = %d): %s\n", (is_arg
? " arg" : ""), 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
));
1909 g_free (pc_offsets
);
1913 sp_offset_to_fp_offset (MonoCompile
*cfg
, int sp_offset
)
1916 * Convert a sp relative offset to a slot index. This is
1917 * platform specific.
1920 /* fp = sp + offset */
1921 g_assert (cfg
->frame_reg
== AMD64_RBP
);
1922 return (- cfg
->arch
.sp_fp_offset
+ sp_offset
);
1923 #elif defined(TARGET_X86)
1924 /* The offset is computed from the sp at the start of the call sequence */
1925 g_assert (cfg
->frame_reg
== X86_EBP
);
1926 #ifdef MONO_X86_NO_PUSHES
1927 return (- cfg
->arch
.sp_fp_offset
+ sp_offset
);
1929 return (- cfg
->arch
.sp_fp_offset
- sp_offset
);
1938 process_param_area_slots (MonoCompile
*cfg
)
1940 MonoCompileGC
*gcfg
= cfg
->gc_info
;
1945 * These slots are used for passing parameters during calls. They are sp relative, not
1946 * fp relative, so they are harder to handle.
1948 if (cfg
->flags
& MONO_CFG_HAS_ALLOCA
)
1949 /* The distance between fp and sp is not constant */
1952 is_param
= mono_mempool_alloc0 (cfg
->mempool
, gcfg
->nslots
* sizeof (gboolean
));
1954 for (cindex
= 0; cindex
< gcfg
->ncallsites
; ++cindex
) {
1955 GCCallSite
*callsite
= gcfg
->callsites
[cindex
];
1958 for (l
= callsite
->param_slots
; l
; l
= l
->next
) {
1959 MonoInst
*def
= l
->data
;
1960 MonoType
*t
= def
->inst_vtype
;
1961 int sp_offset
= def
->inst_offset
;
1962 int fp_offset
= sp_offset_to_fp_offset (cfg
, sp_offset
);
1963 int slot
= fp_offset_to_slot (cfg
, fp_offset
);
1967 if (MONO_TYPE_ISSTRUCT (t
)) {
1968 size
= mini_type_stack_size_full (t
, &align
, FALSE
);
1970 size
= sizeof (target_mgreg_t
);
1973 for (i
= 0; i
< size
/ sizeof (target_mgreg_t
); ++i
) {
1974 g_assert (slot
+ i
>= 0 && slot
+ i
< gcfg
->nslots
);
1975 is_param
[slot
+ i
] = TRUE
;
1980 /* All param area slots are noref by default */
1981 for (i
= 0; i
< gcfg
->nslots
; ++i
) {
1983 set_slot_everywhere (gcfg
, i
, SLOT_NOREF
);
1987 * We treat param area slots as being part of the callee's frame, to be able to handle tailcalls which overwrite
1988 * the argument area of the caller.
1993 process_finally_clauses (MonoCompile
*cfg
)
1995 MonoCompileGC
*gcfg
= cfg
->gc_info
;
1996 GCCallSite
**callsites
;
1998 gboolean has_finally
;
1999 int i
, j
, nslots
, nregs
;
2001 ncallsites
= gcfg
->ncallsites
;
2002 nslots
= gcfg
->nslots
;
2003 nregs
= gcfg
->nregs
;
2004 callsites
= gcfg
->callsites
;
2007 * The calls to the finally clauses don't show up in the cfg. See
2008 * test_0_liveness_8 ().
2009 * Variables accessed inside the finally clause are already marked VOLATILE by
2010 * mono_liveness_handle_exception_clauses (). Variables not accessed inside the finally clause have
2011 * correct liveness outside the finally clause. So mark them PIN inside the finally clauses.
2013 has_finally
= FALSE
;
2014 for (i
= 0; i
< cfg
->header
->num_clauses
; ++i
) {
2015 MonoExceptionClause
*clause
= &cfg
->header
->clauses
[i
];
2017 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
) {
2022 if (cfg
->verbose_level
> 1)
2023 printf ("\tMethod has finally clauses, pessimizing live ranges.\n");
2024 for (j
= 0; j
< ncallsites
; ++j
) {
2025 MonoBasicBlock
*bb
= callsites
[j
]->bb
;
2026 MonoExceptionClause
*clause
;
2027 gboolean is_in_finally
= FALSE
;
2029 for (i
= 0; i
< cfg
->header
->num_clauses
; ++i
) {
2030 clause
= &cfg
->header
->clauses
[i
];
2032 if (MONO_OFFSET_IN_HANDLER (clause
, bb
->real_offset
)) {
2033 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
) {
2034 is_in_finally
= TRUE
;
2040 if (is_in_finally
) {
2041 for (i
= 0; i
< nslots
; ++i
)
2042 set_slot (gcfg
, i
, j
, SLOT_PIN
);
2043 for (i
= 0; i
< nregs
; ++i
)
2044 set_reg_slot (gcfg
, i
, j
, SLOT_PIN
);
2051 compute_frame_size (MonoCompile
*cfg
)
2053 int i
, locals_min_offset
, locals_max_offset
, cfa_min_offset
, cfa_max_offset
;
2054 int min_offset
, max_offset
;
2055 MonoCompileGC
*gcfg
= cfg
->gc_info
;
2056 MonoMethodSignature
*sig
= mono_method_signature_internal (cfg
->method
);
2059 /* Compute min/max offsets from the fp */
2062 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM) || defined(TARGET_S390X)
2063 locals_min_offset
= ALIGN_TO (cfg
->locals_min_stack_offset
, SIZEOF_SLOT
);
2064 locals_max_offset
= cfg
->locals_max_stack_offset
;
2066 /* min/max stack offset needs to be computed in mono_arch_allocate_vars () */
2070 locals_min_offset
= ALIGN_TO (locals_min_offset
, SIZEOF_SLOT
);
2071 locals_max_offset
= ALIGN_TO (locals_max_offset
, SIZEOF_SLOT
);
2073 min_offset
= locals_min_offset
;
2074 max_offset
= locals_max_offset
;
2077 for (i
= 0; i
< sig
->param_count
+ sig
->hasthis
; ++i
) {
2078 MonoInst
*ins
= cfg
->args
[i
];
2080 if (ins
->opcode
== OP_REGOFFSET
) {
2081 int size
, size_in_slots
;
2082 size
= mini_type_stack_size_full (ins
->inst_vtype
, NULL
, ins
->backend
.is_pinvoke
);
2083 size_in_slots
= ALIGN_TO (size
, SIZEOF_SLOT
) / SIZEOF_SLOT
;
2085 min_offset
= MIN (min_offset
, ins
->inst_offset
);
2086 max_offset
= MAX ((int)max_offset
, (int)(ins
->inst_offset
+ (size_in_slots
* SIZEOF_SLOT
)));
2091 g_assert (cfg
->frame_reg
== cfg
->cfa_reg
);
2092 g_assert (cfg
->cfa_offset
> 0);
2094 cfa_max_offset
= cfg
->cfa_offset
;
2096 min_offset
= MIN (min_offset
, cfa_min_offset
);
2097 max_offset
= MAX (max_offset
, cfa_max_offset
);
2099 /* Fp relative slots */
2100 for (l
= gcfg
->stack_slots_from_fp
; l
; l
= l
->next
) {
2101 gint data
= GPOINTER_TO_INT (l
->data
);
2102 int offset
= data
>> 16;
2104 min_offset
= MIN (min_offset
, offset
);
2108 if (!(cfg
->flags
& MONO_CFG_HAS_SPILLUP
)) {
2109 int stack_offset
= ALIGN_TO (cfg
->stack_offset
, SIZEOF_SLOT
);
2110 min_offset
= MIN (min_offset
, (-stack_offset
));
2113 /* Param area slots */
2115 min_offset
= MIN (min_offset
, -cfg
->arch
.sp_fp_offset
);
2116 #elif defined(TARGET_X86)
2117 #ifdef MONO_X86_NO_PUSHES
2118 min_offset
= MIN (min_offset
, -cfg
->arch
.sp_fp_offset
);
2120 min_offset
= MIN (min_offset
, - (cfg
->arch
.sp_fp_offset
+ cfg
->arch
.param_area_size
));
2122 #elif defined(TARGET_ARM)
2124 #elif defined(TARGET_s390X)
2130 gcfg
->min_offset
= min_offset
;
2131 gcfg
->max_offset
= max_offset
;
2132 gcfg
->locals_min_offset
= locals_min_offset
;
2133 gcfg
->locals_max_offset
= locals_max_offset
;
2137 init_gcfg (MonoCompile
*cfg
)
2139 int i
, nregs
, nslots
;
2140 MonoCompileGC
*gcfg
= cfg
->gc_info
;
2141 GCCallSite
**callsites
;
2150 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
2151 ncallsites
+= g_slist_length (bb
->gc_callsites
);
2153 callsites
= mono_mempool_alloc0 (cfg
->mempool
, ncallsites
* sizeof (GCCallSite
*));
2155 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
2156 for (l
= bb
->gc_callsites
; l
; l
= l
->next
)
2157 callsites
[i
++] = l
->data
;
2160 /* The callsites should already be ordered by pc offset */
2161 for (i
= 1; i
< ncallsites
; ++i
)
2162 g_assert (callsites
[i
- 1]->pc_offset
< callsites
[i
]->pc_offset
);
2165 * The stack frame looks like this:
2167 * <fp + max_offset> == cfa -> <end of previous frame>
2168 * <other stack slots>
2170 * <other stack slots>
2171 * fp + min_offset ->
2176 if (cfg
->verbose_level
> 1)
2177 printf ("GC Map for %s: 0x%x-0x%x\n", mono_method_full_name (cfg
->method
, TRUE
), gcfg
->min_offset
, gcfg
->max_offset
);
2179 nslots
= (gcfg
->max_offset
- gcfg
->min_offset
) / SIZEOF_SLOT
;
2182 gcfg
->nslots
= nslots
;
2183 gcfg
->nregs
= nregs
;
2184 gcfg
->callsites
= callsites
;
2185 gcfg
->ncallsites
= ncallsites
;
2186 gcfg
->stack_bitmap_width
= ALIGN_TO (ncallsites
, 8) / 8;
2187 gcfg
->reg_bitmap_width
= ALIGN_TO (ncallsites
, 8) / 8;
2188 gcfg
->stack_ref_bitmap
= mono_mempool_alloc0 (cfg
->mempool
, gcfg
->stack_bitmap_width
* nslots
);
2189 gcfg
->stack_pin_bitmap
= mono_mempool_alloc0 (cfg
->mempool
, gcfg
->stack_bitmap_width
* nslots
);
2190 gcfg
->reg_ref_bitmap
= mono_mempool_alloc0 (cfg
->mempool
, gcfg
->reg_bitmap_width
* nregs
);
2191 gcfg
->reg_pin_bitmap
= mono_mempool_alloc0 (cfg
->mempool
, gcfg
->reg_bitmap_width
* nregs
);
2193 /* All slots start out as PIN */
2194 memset (gcfg
->stack_pin_bitmap
, 0xff, gcfg
->stack_bitmap_width
* nregs
);
2195 for (i
= 0; i
< nregs
; ++i
) {
2197 * By default, registers are NOREF.
2198 * It is possible for a callee to save them before being defined in this method,
2199 * but the saved value is dead too, so it doesn't need to be marked.
2201 if ((cfg
->used_int_regs
& (1 << i
)))
2202 set_reg_slot_everywhere (gcfg
, i
, SLOT_NOREF
);
2207 has_bit_set (guint8
*bitmap
, int width
, int slot
)
2210 int pos
= width
* slot
;
2212 for (i
= 0; i
< width
; ++i
) {
2213 if (bitmap
[pos
+ i
])
2220 create_map (MonoCompile
*cfg
)
2223 int i
, j
, nregs
, nslots
, nref_regs
, npin_regs
, alloc_size
, bitmaps_size
, bitmaps_offset
;
2225 int stack_bitmap_width
, stack_bitmap_size
, reg_ref_bitmap_width
, reg_ref_bitmap_size
;
2226 int reg_pin_bitmap_width
, reg_pin_bitmap_size
, bindex
;
2228 gboolean has_ref_slots
, has_pin_slots
, has_ref_regs
, has_pin_regs
;
2229 MonoCompileGC
*gcfg
= cfg
->gc_info
;
2230 GCCallSite
**callsites
;
2232 guint8
*bitmap
, *bitmaps
;
2233 guint32 reg_ref_mask
, reg_pin_mask
;
2235 ncallsites
= gcfg
->ncallsites
;
2236 nslots
= gcfg
->nslots
;
2237 nregs
= gcfg
->nregs
;
2238 callsites
= gcfg
->callsites
;
2241 * Compute the real size of the bitmap i.e. ignore NOREF columns at the beginning and at
2242 * the end. Also, compute whenever the map needs ref/pin bitmaps, and collect stats.
2244 has_ref_slots
= FALSE
;
2245 has_pin_slots
= FALSE
;
2248 memset (ntypes
, 0, sizeof (ntypes
));
2249 for (i
= 0; i
< nslots
; ++i
) {
2250 gboolean has_ref
= FALSE
;
2251 gboolean has_pin
= FALSE
;
2253 if (has_bit_set (gcfg
->stack_pin_bitmap
, gcfg
->stack_bitmap_width
, i
))
2255 if (has_bit_set (gcfg
->stack_ref_bitmap
, gcfg
->stack_bitmap_width
, i
))
2259 has_ref_slots
= TRUE
;
2261 has_pin_slots
= TRUE
;
2264 ntypes
[SLOT_REF
] ++;
2266 ntypes
[SLOT_PIN
] ++;
2268 ntypes
[SLOT_NOREF
] ++;
2270 if (has_ref
|| has_pin
) {
2277 start
= end
= nslots
;
2279 g_assert (start
!= -1);
2280 g_assert (start
< end
);
2283 has_ref_regs
= FALSE
;
2284 has_pin_regs
= FALSE
;
2289 for (i
= 0; i
< nregs
; ++i
) {
2290 gboolean has_ref
= FALSE
;
2291 gboolean has_pin
= FALSE
;
2293 if (!(cfg
->used_int_regs
& (1 << i
)))
2296 if (has_bit_set (gcfg
->reg_pin_bitmap
, gcfg
->reg_bitmap_width
, i
))
2298 if (has_bit_set (gcfg
->reg_ref_bitmap
, gcfg
->reg_bitmap_width
, i
))
2302 reg_ref_mask
|= (1 << i
);
2303 has_ref_regs
= TRUE
;
2307 reg_pin_mask
|= (1 << i
);
2308 has_pin_regs
= TRUE
;
2313 if (cfg
->verbose_level
> 1)
2314 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
);
2316 /* Create the GC Map */
2318 /* The work bitmaps have one row for each slot, since this is how we access them during construction */
2319 stack_bitmap_width
= ALIGN_TO (end
- start
, 8) / 8;
2320 stack_bitmap_size
= stack_bitmap_width
* ncallsites
;
2321 reg_ref_bitmap_width
= ALIGN_TO (nref_regs
, 8) / 8;
2322 reg_ref_bitmap_size
= reg_ref_bitmap_width
* ncallsites
;
2323 reg_pin_bitmap_width
= ALIGN_TO (npin_regs
, 8) / 8;
2324 reg_pin_bitmap_size
= reg_pin_bitmap_width
* ncallsites
;
2325 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);
2327 map
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (GCMap
));
2329 map
->frame_reg
= cfg
->frame_reg
;
2330 map
->start_offset
= gcfg
->min_offset
;
2331 map
->end_offset
= gcfg
->min_offset
+ (nslots
* SIZEOF_SLOT
);
2332 map
->map_offset
= start
* SIZEOF_SLOT
;
2333 map
->nslots
= end
- start
;
2334 map
->has_ref_slots
= has_ref_slots
;
2335 map
->has_pin_slots
= has_pin_slots
;
2336 map
->has_ref_regs
= has_ref_regs
;
2337 map
->has_pin_regs
= has_pin_regs
;
2338 g_assert (nregs
< 32);
2339 map
->used_int_regs
= cfg
->used_int_regs
;
2340 map
->reg_ref_mask
= reg_ref_mask
;
2341 map
->reg_pin_mask
= reg_pin_mask
;
2342 map
->nref_regs
= nref_regs
;
2343 map
->npin_regs
= npin_regs
;
2345 bitmaps
= mono_mempool_alloc0 (cfg
->mempool
, bitmaps_size
);
2348 if (has_ref_slots
) {
2349 map
->stack_ref_bitmap_offset
= bitmaps_offset
;
2350 bitmaps_offset
+= stack_bitmap_size
;
2352 bitmap
= &bitmaps
[map
->stack_ref_bitmap_offset
];
2353 for (i
= 0; i
< nslots
; ++i
) {
2354 for (j
= 0; j
< ncallsites
; ++j
) {
2355 if (get_bit (gcfg
->stack_ref_bitmap
, gcfg
->stack_bitmap_width
, i
, j
))
2356 set_bit (bitmap
, stack_bitmap_width
, j
, i
- start
);
2360 if (has_pin_slots
) {
2361 map
->stack_pin_bitmap_offset
= bitmaps_offset
;
2362 bitmaps_offset
+= stack_bitmap_size
;
2364 bitmap
= &bitmaps
[map
->stack_pin_bitmap_offset
];
2365 for (i
= 0; i
< nslots
; ++i
) {
2366 for (j
= 0; j
< ncallsites
; ++j
) {
2367 if (get_bit (gcfg
->stack_pin_bitmap
, gcfg
->stack_bitmap_width
, i
, j
))
2368 set_bit (bitmap
, stack_bitmap_width
, j
, i
- start
);
2373 map
->reg_ref_bitmap_offset
= bitmaps_offset
;
2374 bitmaps_offset
+= reg_ref_bitmap_size
;
2376 bitmap
= &bitmaps
[map
->reg_ref_bitmap_offset
];
2378 for (i
= 0; i
< nregs
; ++i
) {
2379 if (reg_ref_mask
& (1 << i
)) {
2380 for (j
= 0; j
< ncallsites
; ++j
) {
2381 if (get_bit (gcfg
->reg_ref_bitmap
, gcfg
->reg_bitmap_width
, i
, j
))
2382 set_bit (bitmap
, reg_ref_bitmap_width
, j
, bindex
);
2389 map
->reg_pin_bitmap_offset
= bitmaps_offset
;
2390 bitmaps_offset
+= reg_pin_bitmap_size
;
2392 bitmap
= &bitmaps
[map
->reg_pin_bitmap_offset
];
2394 for (i
= 0; i
< nregs
; ++i
) {
2395 if (reg_pin_mask
& (1 << i
)) {
2396 for (j
= 0; j
< ncallsites
; ++j
) {
2397 if (get_bit (gcfg
->reg_pin_bitmap
, gcfg
->reg_bitmap_width
, i
, j
))
2398 set_bit (bitmap
, reg_pin_bitmap_width
, j
, bindex
);
2406 map
->ncallsites
= ncallsites
;
2407 if (cfg
->code_len
< 256)
2408 map
->callsite_entry_size
= 1;
2409 else if (cfg
->code_len
< 65536)
2410 map
->callsite_entry_size
= 2;
2412 map
->callsite_entry_size
= 4;
2414 /* Encode the GC Map */
2422 encode_gc_map (map
, buf
, &endbuf
);
2423 g_assert (endbuf
- buf
< 256);
2425 encoded_size
= endbuf
- buf
;
2426 alloc_size
= sizeof (GCEncodedMap
) + ALIGN_TO (encoded_size
, map
->callsite_entry_size
) + (map
->callsite_entry_size
* map
->ncallsites
) + bitmaps_size
;
2428 emap
= mono_domain_alloc0 (cfg
->domain
, alloc_size
);
2429 //emap->ref_slots = map->ref_slots;
2431 /* Encoded fixed fields */
2432 p
= &emap
->encoded
[0];
2433 //emap->encoded_size = encoded_size;
2434 memcpy (p
, buf
, encoded_size
);
2437 /* Callsite table */
2438 p
= (guint8
*)ALIGN_TO ((gsize
)p
, map
->callsite_entry_size
);
2439 if (map
->callsite_entry_size
== 1) {
2440 guint8
*offsets
= p
;
2441 for (i
= 0; i
< ncallsites
; ++i
)
2442 offsets
[i
] = callsites
[i
]->pc_offset
;
2443 UnlockedAdd (&stats
.gc_callsites8_size
, ncallsites
* sizeof (guint8
));
2444 } else if (map
->callsite_entry_size
== 2) {
2445 guint16
*offsets
= (guint16
*)p
;
2446 for (i
= 0; i
< ncallsites
; ++i
)
2447 offsets
[i
] = callsites
[i
]->pc_offset
;
2448 UnlockedAdd (&stats
.gc_callsites16_size
, ncallsites
* sizeof (guint16
));
2450 guint32
*offsets
= (guint32
*)p
;
2451 for (i
= 0; i
< ncallsites
; ++i
)
2452 offsets
[i
] = callsites
[i
]->pc_offset
;
2453 UnlockedAdd (&stats
.gc_callsites32_size
, ncallsites
* sizeof (guint32
));
2455 p
+= ncallsites
* map
->callsite_entry_size
;
2458 memcpy (p
, bitmaps
, bitmaps_size
);
2461 g_assert ((guint8
*)p
- (guint8
*)emap
<= alloc_size
);
2463 UnlockedAdd (&stats
.gc_maps_size
, alloc_size
);
2464 UnlockedAdd (&stats
.gc_callsites_size
, ncallsites
* map
->callsite_entry_size
);
2465 UnlockedAdd (&stats
.gc_bitmaps_size
, bitmaps_size
);
2466 UnlockedAdd (&stats
.gc_map_struct_size
, sizeof (GCEncodedMap
) + encoded_size
);
2468 cfg
->jit_info
->gc_info
= emap
;
2470 cfg
->gc_map
= (guint8
*)emap
;
2471 cfg
->gc_map_size
= alloc_size
;
2474 UnlockedAdd (&stats
.all_slots
, nslots
);
2475 UnlockedAdd (&stats
.ref_slots
, ntypes
[SLOT_REF
]);
2476 UnlockedAdd (&stats
.noref_slots
, ntypes
[SLOT_NOREF
]);
2477 UnlockedAdd (&stats
.pin_slots
, ntypes
[SLOT_PIN
]);
2481 mini_gc_create_gc_map (MonoCompile
*cfg
)
2483 if (!cfg
->compute_gc_maps
)
2487 * During marking, all frames except the top frame are at a call site, and we mark the
2488 * top frame conservatively. This means that we only need to compute and record
2489 * GC maps for call sites.
2492 if (!(cfg
->comp_done
& MONO_COMP_LIVENESS
))
2493 /* Without liveness info, the live ranges are not precise enough */
2496 mono_analyze_liveness_gc (cfg
);
2498 compute_frame_size (cfg
);
2502 process_spill_slots (cfg
);
2503 process_other_slots (cfg
);
2504 process_param_area_slots (cfg
);
2505 process_variables (cfg
);
2506 process_finally_clauses (cfg
);
2511 #endif /* DISABLE_JIT */
2514 parse_debug_options (void)
2519 env
= g_getenv ("MONO_GCMAP_DEBUG");
2523 opts
= g_strsplit (env
, ",", -1);
2524 for (ptr
= opts
; ptr
&& *ptr
; ptr
++) {
2525 /* No options yet */
2526 fprintf (stderr
, "Invalid format for the MONO_GCMAP_DEBUG env variable: '%s'\n", env
);
2538 memset (&cb
, 0, sizeof (cb
));
2539 cb
.thread_attach_func
= thread_attach_func
;
2540 cb
.thread_detach_func
= thread_detach_func
;
2541 cb
.thread_suspend_func
= thread_suspend_func
;
2542 /* Comment this out to disable precise stack marking */
2543 cb
.thread_mark_func
= thread_mark_func
;
2544 cb
.get_provenance_func
= get_provenance_func
;
2545 if (mono_use_interpreter
)
2546 cb
.interp_mark_func
= mini_get_interp_callbacks ()->mark_stack
;
2547 mono_gc_set_gc_callbacks (&cb
);
2549 logfile
= mono_gc_get_logfile ();
2551 parse_debug_options ();
2553 mono_counters_register ("GC Maps size",
2554 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.gc_maps_size
);
2555 mono_counters_register ("GC Call Sites size",
2556 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.gc_callsites_size
);
2557 mono_counters_register ("GC Bitmaps size",
2558 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.gc_bitmaps_size
);
2559 mono_counters_register ("GC Map struct size",
2560 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.gc_map_struct_size
);
2561 mono_counters_register ("GC Call Sites encoded using 8 bits",
2562 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.gc_callsites8_size
);
2563 mono_counters_register ("GC Call Sites encoded using 16 bits",
2564 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.gc_callsites16_size
);
2565 mono_counters_register ("GC Call Sites encoded using 32 bits",
2566 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.gc_callsites32_size
);
2568 mono_counters_register ("GC Map slots (all)",
2569 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.all_slots
);
2570 mono_counters_register ("GC Map slots (ref)",
2571 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.ref_slots
);
2572 mono_counters_register ("GC Map slots (noref)",
2573 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.noref_slots
);
2574 mono_counters_register ("GC Map slots (pin)",
2575 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.pin_slots
);
2577 mono_counters_register ("GC TLS Data size",
2578 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.tlsdata_size
);
2580 mono_counters_register ("Stack space scanned (all)",
2581 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.scanned_stacks
);
2582 mono_counters_register ("Stack space scanned (native)",
2583 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.scanned_native
);
2584 mono_counters_register ("Stack space scanned (other)",
2585 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.scanned_other
);
2586 mono_counters_register ("Stack space scanned (using GC Maps)",
2587 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.scanned
);
2588 mono_counters_register ("Stack space scanned (precise)",
2589 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.scanned_precisely
);
2590 mono_counters_register ("Stack space scanned (pin)",
2591 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.scanned_conservatively
);
2592 mono_counters_register ("Stack space scanned (pin registers)",
2593 MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stats
.scanned_registers
);
2599 mini_gc_enable_gc_maps_for_aot (void)
2607 memset (&cb
, 0, sizeof (cb
));
2608 cb
.get_provenance_func
= get_provenance_func
;
2609 if (mono_use_interpreter
)
2610 cb
.interp_mark_func
= mini_get_interp_callbacks ()->mark_stack
;
2611 mono_gc_set_gc_callbacks (&cb
);
2617 mini_gc_init_gc_map (MonoCompile
*cfg
)
2622 mini_gc_create_gc_map (MonoCompile
*cfg
)
2627 mini_gc_set_slot_type_from_fp (MonoCompile
*cfg
, int slot_offset
, GCSlotType type
)
2632 mini_gc_set_slot_type_from_cfa (MonoCompile
*cfg
, int slot_offset
, GCSlotType type
)
2636 #endif /* DISABLE_JIT */
2645 * Set GC specific options in CFG.
2648 mini_gc_init_cfg (MonoCompile
*cfg
)
2650 if (mono_gc_is_moving ()) {
2651 cfg
->disable_ref_noref_stack_slot_share
= TRUE
;
2652 cfg
->gen_write_barriers
= TRUE
;
2655 mini_gc_init_gc_map (cfg
);
2658 #endif /* DISABLE_JIT */
2661 * Problems with the current code:
2662 * - the stack walk is slow
2663 * - vtypes/refs used in EH regions are treated conservatively
2664 * - if the code is finished, less pinning will be done, causing problems because
2665 * we promote all surviving objects to old-gen.
2666 * - the unwind code can't handle a method stopped inside a finally region, it thinks the caller is
2667 * another method, but in reality it is either the exception handling code or the CALL_HANDLER opcode.
2668 * This manifests in "Unable to find ip offset x in callsite list" assertions.
2669 * - the unwind code also can't handle frames which are in the epilog, since the unwind info is not
2674 * Ideas for creating smaller GC maps:
2675 * - remove empty columns from the bitmaps. This requires adding a mask bit array for
2677 * - merge reg and stack slot bitmaps, so the unused bits at the end of the reg bitmap are
2679 * - if the bitmap width is not a multiple of 8, the remaining bits are wasted.
2680 * - group ref and non-ref stack slots together in mono_allocate_stack_slots ().
2681 * - add an index for the callsite table so that each entry can be encoded as a 1 byte difference
2682 * from an index entry.