Share the log file between sgen and the stack marking code.
[mono.git] / mono / mini / mini-gc.c
1 /*
2  * mini-gc.c: GC interface for the mono JIT
3  *
4  * Author:
5  *   Zoltan Varga (vargaz@gmail.com)
6  *
7  * Copyright 2009 Novell, Inc (http://www.novell.com)
8  */
9
10 #include "config.h"
11 #include "mini-gc.h"
12 #include <mono/metadata/gc-internal.h>
13
14 //#if 0
15 #ifdef HAVE_SGEN_GC
16
17 #include <mono/metadata/sgen-gc.h>
18 #include <mono/metadata/gc-internal.h>
19 #include <mono/utils/mono-counters.h>
20
21 /* Contains state needed by the GC Map construction code */
22 typedef struct {
23         /*
24          * This contains information about stack slots initialized in the prolog, encoded using
25          * (slot_index << 16) | slot_type. The slot_index is relative to the CFA, i.e. 0
26          * means cfa+0, 1 means cfa-4/8, etc.
27          */
28         GSList *stack_slots_from_cfa;
29         /* Same for stack slots relative to the frame pointer */
30         GSList *stack_slots_from_fp;
31
32         /* Number of slots in the map */
33         int nslots;
34         /* The number of registers in the map */
35         int nregs;
36         /* Min and Max offsets of the stack frame relative to fp */
37         int min_offset, max_offset;
38         /* Same for the locals area */
39         int locals_min_offset, locals_max_offset;
40
41         /* The call sites where this frame can be stopped during GC */
42         GCCallSite **callsites;
43         /* The number of call sites */
44         int ncallsites;
45
46         /*
47          * The width of the stack bitmaps in bytes. This is not equal to the bitmap width at
48      * runtime, since it includes columns which are 0.
49          */
50         int stack_bitmap_width;
51         /* 
52          * A bitmap whose width equals nslots, and whose height equals ncallsites.
53          * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
54          * given callsite.
55          */
56         guint8 *stack_ref_bitmap;
57         /* Same for SLOT_PIN */
58         guint8 *stack_pin_bitmap;
59
60         /*
61          * Similar bitmaps for registers. These have width MONO_MAX_IREGS in bits.
62          */
63         int reg_bitmap_width;
64         guint8 *reg_ref_bitmap;
65         guint8 *reg_pin_bitmap;
66 } MonoCompileGC;
67
68 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
69
70 #if 1
71 /* We don't support debug levels, its all-or-nothing */
72 #define DEBUG(s) do { s; fflush (logfile); } while (0)
73 #define DEBUG_ENABLED 1
74 #else
75 #define DEBUG(s)
76 #endif
77
78 #ifdef DEBUG_ENABLED
79 //#if 1
80 #define DEBUG_PRECISE(s) do { s; } while (0)
81 #define DEBUG_PRECISE_ENABLED
82 #else
83 #define DEBUG_PRECISE(s)
84 #endif
85
86 #if 1
87 #define DEBUG_GC_MAP(s) do { s; fflush (stdout); } while (0)
88 #else
89 #define DEBUG_GC_MAP(s)
90 #endif
91
92 /*
93  * Contains information collected during the conservative stack marking pass,
94  * used during the precise pass. This helps to avoid doing a stack walk twice, which
95  * is expensive.
96  */
97 typedef struct {
98         guint8 *bitmap;
99         int nslots;
100     int frame_start_offset;
101         int nreg_locations;
102         /* Relative to stack_start */
103         int reg_locations [MONO_MAX_IREGS];
104 #ifdef DEBUG_PRECISE_ENABLED
105         MonoJitInfo *ji;
106         gpointer fp;
107         int regs [MONO_MAX_IREGS];
108 #endif
109 } FrameInfo;
110
111 /* Max number of frames stored in the TLS data */
112 #define MAX_FRAMES 50
113
114 /*
115  * Per-thread data kept by this module. This is stored in the GC and passed to us as
116  * parameters, instead of being stored in a TLS variable, since during a collection,
117  * only the collection thread is active.
118  */
119 typedef struct {
120         MonoLMF *lmf;
121         MonoContext ctx;
122         gboolean has_context;
123         MonoJitTlsData *jit_tls;
124         /* For debugging */
125         guint64 tid;
126         gpointer ref_to_track;
127         /* Number of frames collected during the !precise pass */
128         int nframes;
129         FrameInfo frames [MAX_FRAMES];
130 } TlsData;
131
132 /* These are constant so don't store them in the GC Maps */
133 /* Number of registers stored in gc maps */
134 #define NREGS MONO_MAX_IREGS
135
136 /* 
137  * The GC Map itself.
138  * Contains information needed to mark a stack frame.
139  * This is a transient structure, created from a compressed representation on-demand.
140  */
141 typedef struct {
142         /*
143          * The offsets of the GC tracked area inside the stack frame relative to the frame pointer.
144          * This includes memory which is NOREF thus doesn't need GC maps.
145          */
146         int start_offset;
147         int end_offset;
148         /*
149          * The offset relative to frame_offset where the the memory described by the GC maps
150          * begins.
151          */
152         int map_offset;
153         /* The number of stack slots in the map */
154         int nslots;
155         /* The frame pointer register */
156         guint8 frame_reg;
157         /* The size of each callsite table entry */
158         guint8 callsite_entry_size;
159         guint has_pin_slots : 1;
160         guint has_ref_slots : 1;
161         guint has_ref_regs : 1;
162         guint has_pin_regs : 1;
163
164         /* The offsets below are into an external bitmaps array */
165
166         /* 
167          * A bitmap whose width is equal to bitmap_width, and whose height is equal to ncallsites.
168          * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
169          * given callsite.
170          */
171         guint32 stack_ref_bitmap_offset;
172         /*
173          * Same for SLOT_PIN. It is possible that the same bit is set in both bitmaps at
174      * different callsites, if the slot starts out as PIN, and later changes to REF.
175          */
176         guint32 stack_pin_bitmap_offset;
177
178         /*
179          * Corresponding bitmaps for registers
180          * These have width equal to the number of bits set in reg_ref_mask/reg_pin_mask.
181          * FIXME: Merge these with the normal bitmaps, i.e. reserve the first x slots for them ?
182          */
183         guint32 reg_pin_bitmap_offset;
184         guint32 reg_ref_bitmap_offset;
185
186         guint32 used_int_regs, reg_ref_mask, reg_pin_mask;
187
188         /* The number of bits set in the two masks above */
189         guint8 nref_regs, npin_regs;
190
191         /*
192          * A bit array marking slots which contain refs.
193          * This is used only for debugging.
194          */
195         //guint8 *ref_slots;
196
197         /* Callsite offsets */
198         /* These can take up a lot of space, so encode them compactly */
199         union {
200                 guint8 *offsets8;
201                 guint16 *offsets16;
202                 guint32 *offsets32;
203         } callsites;
204         int ncallsites;
205 } GCMap;
206
207 /*
208  * A compressed version of GCMap. This is what gets stored in MonoJitInfo.
209  */
210 typedef struct {
211         //guint8 *ref_slots;
212         //guint8 encoded_size;
213
214         /*
215          * The arrays below are embedded after the struct.
216          * Their address needs to be computed.
217          */
218
219         /* The fixed fields of the GCMap encoded using LEB128 */
220         guint8 encoded [MONO_ZERO_LEN_ARRAY];
221
222         /* An array of ncallsites entries, each entry is callsite_entry_size bytes long */
223         guint8 callsites [MONO_ZERO_LEN_ARRAY];
224
225         /* The GC bitmaps */
226         guint8 bitmaps [MONO_ZERO_LEN_ARRAY];
227 } GCEncodedMap;
228
229 static int precise_frame_count [2], precise_frame_limit = -1;
230 static gboolean precise_frame_limit_inited;
231
232 /* Stats */
233 typedef struct {
234         int scanned_stacks;
235         int scanned;
236         int scanned_precisely;
237         int scanned_conservatively;
238         int scanned_registers;
239         int scanned_native;
240         int scanned_other;
241         
242         int all_slots;
243         int noref_slots;
244         int ref_slots;
245         int pin_slots;
246
247         int gc_maps_size;
248         int gc_callsites_size;
249         int gc_callsites8_size;
250         int gc_callsites16_size;
251         int gc_callsites32_size;
252         int gc_bitmaps_size;
253         int gc_map_struct_size;
254         int tlsdata_size;
255 } JITGCStats;
256
257 static JITGCStats stats;
258
259 static FILE *logfile;
260
261 // FIXME: Move these to a shared place
262
263 static inline void
264 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
265 {
266         guint8 *p = buf;
267
268         do {
269                 guint8 b = value & 0x7f;
270                 value >>= 7;
271                 if (value != 0) /* more bytes to come */
272                         b |= 0x80;
273                 *p ++ = b;
274         } while (value);
275
276         *endbuf = p;
277 }
278
279 static G_GNUC_UNUSED void
280 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
281 {
282         gboolean more = 1;
283         gboolean negative = (value < 0);
284         guint32 size = 32;
285         guint8 byte;
286         guint8 *p = buf;
287
288         while (more) {
289                 byte = value & 0x7f;
290                 value >>= 7;
291                 /* the following is unnecessary if the
292                  * implementation of >>= uses an arithmetic rather
293                  * than logical shift for a signed left operand
294                  */
295                 if (negative)
296                         /* sign extend */
297                         value |= - (1 <<(size - 7));
298                 /* sign bit of byte is second high order bit (0x40) */
299                 if ((value == 0 && !(byte & 0x40)) ||
300                         (value == -1 && (byte & 0x40)))
301                         more = 0;
302                 else
303                         byte |= 0x80;
304                 *p ++= byte;
305         }
306
307         *endbuf = p;
308 }
309
310 static inline guint32
311 decode_uleb128 (guint8 *buf, guint8 **endbuf)
312 {
313         guint8 *p = buf;
314         guint32 res = 0;
315         int shift = 0;
316
317         while (TRUE) {
318                 guint8 b = *p;
319                 p ++;
320
321                 res = res | (((int)(b & 0x7f)) << shift);
322                 if (!(b & 0x80))
323                         break;
324                 shift += 7;
325         }
326
327         *endbuf = p;
328
329         return res;
330 }
331
332 static inline gint32
333 decode_sleb128 (guint8 *buf, guint8 **endbuf)
334 {
335         guint8 *p = buf;
336         gint32 res = 0;
337         int shift = 0;
338
339         while (TRUE) {
340                 guint8 b = *p;
341                 p ++;
342
343                 res = res | (((int)(b & 0x7f)) << shift);
344                 shift += 7;
345                 if (!(b & 0x80)) {
346                         if (shift < 32 && (b & 0x40))
347                                 res |= - (1 << shift);
348                         break;
349                 }
350         }
351
352         *endbuf = p;
353
354         return res;
355 }
356
357 static int
358 encode_frame_reg (int frame_reg)
359 {
360 #ifdef TARGET_AMD64
361         if (frame_reg == AMD64_RSP)
362                 return 0;
363         else if (frame_reg == AMD64_RBP)
364                 return 1;
365 #else
366         NOT_IMPLEMENTED;
367 #endif
368         g_assert_not_reached ();
369         return -1;
370 }
371
372 static int
373 decode_frame_reg (int encoded)
374 {
375 #ifdef TARGET_AMD64
376         if (encoded == 0)
377                 return AMD64_RSP;
378         else if (encoded == 1)
379                 return AMD64_RBP;
380 #else
381         NOT_IMPLEMENTED;
382 #endif
383         g_assert_not_reached ();
384         return -1;
385 }
386
387 #ifdef TARGET_AMD64
388 #ifdef HOST_WIN32
389 static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15, AMD64_RDI, AMD64_RSI };
390 #else
391 static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15 };
392 #endif
393 #endif
394
395 static guint32
396 encode_regmask (guint32 regmask)
397 {
398 #ifdef TARGET_AMD64
399         int i;
400         guint32 res;
401
402         res = 0;
403         for (i = 0; i < sizeof (callee_saved_regs) / sizeof (int); ++i) {
404                 if (regmask & (1 << callee_saved_regs [i])) {
405                         res |= (1 << i);
406                         regmask -= (1 << callee_saved_regs [i]);
407                 }
408         }
409         g_assert (regmask == 0);
410         return res;
411 #else
412         NOT_IMPLEMENTED;
413         return regmask;
414 #endif
415 }
416
417 static guint32
418 decode_regmask (guint32 regmask)
419 {
420 #ifdef TARGET_AMD64
421         int i;
422         guint32 res;
423
424         res = 0;
425         for (i = 0; i < sizeof (callee_saved_regs) / sizeof (int); ++i)
426                 if (regmask & (1 << i))
427                         res |= (1 << callee_saved_regs [i]);
428         return res;
429 #else
430         NOT_IMPLEMENTED;
431         return regmask;
432 #endif
433 }
434
435 /*
436  * encode_gc_map:
437  *
438  *   Encode the fixed fields of MAP into a buffer pointed to by BUF.
439  */
440 static void
441 encode_gc_map (GCMap *map, guint8 *buf, guint8 **endbuf)
442 {
443         guint32 flags, freg;
444
445         encode_sleb128 (map->start_offset / sizeof (mgreg_t), buf, &buf);
446         encode_sleb128 (map->end_offset / sizeof (mgreg_t), buf, &buf);
447         encode_sleb128 (map->map_offset / sizeof (mgreg_t), buf, &buf);
448         encode_uleb128 (map->nslots, buf, &buf);
449         g_assert (map->callsite_entry_size <= 4);
450         freg = encode_frame_reg (map->frame_reg);
451         g_assert (freg < 2);
452         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);
453         encode_uleb128 (flags, buf, &buf);
454         encode_uleb128 (encode_regmask (map->used_int_regs), buf, &buf);
455         if (map->has_ref_regs)
456                 encode_uleb128 (encode_regmask (map->reg_ref_mask), buf, &buf);
457         if (map->has_pin_regs)
458                 encode_uleb128 (encode_regmask (map->reg_pin_mask), buf, &buf);
459         encode_uleb128 (map->ncallsites, buf, &buf);
460
461         *endbuf = buf;
462 }       
463
464 /*
465  * decode_gc_map:
466  *
467  *   Decode the encoded GC map representation in BUF and store the result into MAP.
468  */
469 static void
470 decode_gc_map (guint8 *buf, GCMap *map, guint8 **endbuf)
471 {
472         guint32 flags;
473         int stack_bitmap_size, reg_ref_bitmap_size, reg_pin_bitmap_size, offset, freg;
474         int i, n;
475
476         map->start_offset = decode_sleb128 (buf, &buf) * sizeof (mgreg_t);
477         map->end_offset = decode_sleb128 (buf, &buf) * sizeof (mgreg_t);
478         map->map_offset = decode_sleb128 (buf, &buf) * sizeof (mgreg_t);
479         map->nslots = decode_uleb128 (buf, &buf);
480         flags = decode_uleb128 (buf, &buf);
481         map->has_ref_slots = (flags & 1) ? 1 : 0;
482         map->has_pin_slots = (flags & 2) ? 1 : 0;
483         map->has_ref_regs = (flags & 4) ? 1 : 0;
484         map->has_pin_regs = (flags & 8) ? 1 : 0;
485         map->callsite_entry_size = ((flags >> 4) & 0x3) + 1;
486         freg = flags >> 6;
487         map->frame_reg = decode_frame_reg (freg);
488         map->used_int_regs = decode_regmask (decode_uleb128 (buf, &buf));
489         if (map->has_ref_regs) {
490                 map->reg_ref_mask = decode_regmask (decode_uleb128 (buf, &buf));
491                 n = 0;
492                 for (i = 0; i < NREGS; ++i)
493                         if (map->reg_ref_mask & (1 << i))
494                                 n ++;
495                 map->nref_regs = n;
496         }
497         if (map->has_pin_regs) {
498                 map->reg_pin_mask = decode_regmask (decode_uleb128 (buf, &buf));
499                 n = 0;
500                 for (i = 0; i < NREGS; ++i)
501                         if (map->reg_pin_mask & (1 << i))
502                                 n ++;
503                 map->npin_regs = n;
504         }
505         map->ncallsites = decode_uleb128 (buf, &buf);
506
507         stack_bitmap_size = (ALIGN_TO (map->nslots, 8) / 8) * map->ncallsites;
508         reg_ref_bitmap_size = (ALIGN_TO (map->nref_regs, 8) / 8) * map->ncallsites;
509         reg_pin_bitmap_size = (ALIGN_TO (map->npin_regs, 8) / 8) * map->ncallsites;
510         offset = 0;
511         map->stack_ref_bitmap_offset = offset;
512         if (map->has_ref_slots)
513                 offset += stack_bitmap_size;
514         map->stack_pin_bitmap_offset = offset;
515         if (map->has_pin_slots)
516                 offset += stack_bitmap_size;
517         map->reg_ref_bitmap_offset = offset;
518         if (map->has_ref_regs)
519                 offset += reg_ref_bitmap_size;
520         map->reg_pin_bitmap_offset = offset;
521         if (map->has_pin_regs)
522                 offset += reg_pin_bitmap_size;
523
524         *endbuf = buf;
525 }
526
527 static gpointer
528 thread_attach_func (void)
529 {
530         TlsData *tls;
531
532         tls = g_new0 (TlsData, 1);
533         tls->tid = GetCurrentThreadId ();
534         stats.tlsdata_size += sizeof (TlsData);
535
536         return tls;
537 }
538
539 static void
540 thread_detach_func (gpointer user_data)
541 {
542         TlsData *tls = user_data;
543
544         g_free (tls);
545 }
546
547 static void
548 thread_suspend_func (gpointer user_data, void *sigctx)
549 {
550         TlsData *tls = user_data;
551
552         if (!tls)
553                 /* Happens during startup */
554                 return;
555
556         tls->lmf = mono_get_lmf ();
557         if (sigctx) {
558                 mono_arch_sigctx_to_monoctx (sigctx, &tls->ctx);
559                 tls->has_context = TRUE;
560         } else {
561                 tls->has_context = FALSE;
562         }
563         tls->jit_tls = TlsGetValue (mono_jit_tls_id);
564 }
565
566 #define DEAD_REF ((gpointer)(gssize)0x2a2a2a2a2a2a2a2aULL)
567
568 static inline void
569 set_bit (guint8 *bitmap, int width, int y, int x)
570 {
571         bitmap [(width * y) + (x / 8)] |= (1 << (x % 8));
572 }
573
574 static inline void
575 clear_bit (guint8 *bitmap, int width, int y, int x)
576 {
577         bitmap [(width * y) + (x / 8)] &= ~(1 << (x % 8));
578 }
579
580 static inline int
581 get_bit (guint8 *bitmap, int width, int y, int x)
582 {
583         return bitmap [(width * y) + (x / 8)] & (1 << (x % 8));
584 }
585
586 static const char*
587 slot_type_to_string (GCSlotType type)
588 {
589         switch (type) {
590         case SLOT_REF:
591                 return "ref";
592         case SLOT_NOREF:
593                 return "noref";
594         case SLOT_PIN:
595                 return "pin";
596         default:
597                 g_assert_not_reached ();
598                 return NULL;
599         }
600 }
601
602 /*
603  * conservatively_pass:
604  *
605  *   Mark a thread stack conservatively and collect information needed by the precise pass.
606  */
607 static void
608 conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
609 {
610         MonoJitInfo *ji;
611         MonoContext ctx, new_ctx;
612         MonoLMF *lmf;
613         guint8 *stack_limit;
614         gboolean last = TRUE;
615         GCMap *map;
616         GCMap map_tmp;
617         GCEncodedMap *emap;
618         guint8* fp, *p, *real_frame_start, *frame_start, *frame_end;
619         int i, pc_offset, cindex, bitmap_width;
620         int scanned = 0, scanned_precisely, scanned_conservatively, scanned_registers;
621         gboolean res;
622         StackFrameInfo frame;
623         mgreg_t *reg_locations [MONO_MAX_IREGS];
624         mgreg_t *new_reg_locations [MONO_MAX_IREGS];
625         guint8 *bitmaps;
626         FrameInfo *fi;
627         guint32 precise_regmask;
628
629         if (tls) {
630                 tls->nframes = 0;
631                 tls->ref_to_track = NULL;
632         }
633
634         /* tls == NULL can happen during startup */
635         if (mono_thread_internal_current () == NULL || !tls) {
636                 mono_gc_conservatively_scan_area (stack_start, stack_end);
637                 stats.scanned_stacks += stack_end - stack_start;
638                 return;
639         }
640
641         lmf = tls->lmf;
642         frame.domain = NULL;
643
644         /* Number of bytes scanned based on GC map data */
645         scanned = 0;
646         /* Number of bytes scanned precisely based on GC map data */
647         scanned_precisely = 0;
648         /* Number of bytes scanned conservatively based on GC map data */
649         scanned_conservatively = 0;
650         /* Number of bytes scanned conservatively in register save areas */
651         scanned_registers = 0;
652
653         /* This is one past the last address which we have scanned */
654         stack_limit = stack_start;
655
656         if (!tls->has_context)
657                 memset (&new_ctx, 0, sizeof (ctx));
658         else
659                 memcpy (&new_ctx, &tls->ctx, sizeof (MonoContext));
660
661         memset (reg_locations, 0, sizeof (reg_locations));
662         memset (new_reg_locations, 0, sizeof (new_reg_locations));
663
664         while (TRUE) {
665                 memcpy (&ctx, &new_ctx, sizeof (ctx));
666
667                 for (i = 0; i < MONO_MAX_IREGS; ++i) {
668                         if (new_reg_locations [i]) {
669                                 /*
670                                  * If the current frame saves the register, it means it might modify its
671                                  * value, thus the old location might not contain the same value, so
672                                  * we have to mark it conservatively.
673                                  */
674                                 if (reg_locations [i]) {
675                                         DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
676                                         mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
677                                         scanned_registers += sizeof (mgreg_t);
678                                 }
679
680                                 reg_locations [i] = new_reg_locations [i];
681
682                                 DEBUG (fprintf (logfile, "\treg %s is now at location %p.\n", mono_arch_regname (i), reg_locations [i]));
683                         }
684                 }
685
686                 g_assert ((guint64)stack_limit % sizeof (mgreg_t) == 0);
687
688 #ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
689                 res = mono_find_jit_info_ext (frame.domain ? frame.domain : mono_domain_get (), tls->jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, new_reg_locations, &frame);
690                 if (!res)
691                         break;
692 #else
693                 break;
694 #endif
695
696                 /* The last frame can be in any state so mark conservatively */
697                 if (last) {
698                         last = FALSE;
699                         continue;
700                 }
701
702                 ji = frame.ji;
703                 pc_offset = (guint8*)MONO_CONTEXT_GET_IP (&ctx) - (guint8*)ji->code_start;
704
705                 if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE) {
706                         /* These frames are unwound through an LMF, and we have no precise register tracking for those */
707                         DEBUG (fprintf (logfile, "Mark(0): <Managed-to-native transition>\n"));
708                         for (i = 0; i < MONO_MAX_IREGS; ++i) {
709                                 if (reg_locations [i]) {
710                                         DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
711                                         mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
712                                         scanned_registers += sizeof (mgreg_t);
713                                 }
714                                 reg_locations [i] = NULL;
715                                 new_reg_locations [i] = NULL;
716                         }
717                         continue;
718                 }
719
720                 /* These frames are very problematic */
721                 if (ji->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
722                         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));
723                         DEBUG (fprintf (logfile, "\tSkip.\n"));
724                         continue;
725                 }
726
727                 /* All the other frames are at a call site */
728
729                 if (tls->nframes == MAX_FRAMES) {
730                         /* 
731                          * Can't save information since the array is full. So scan the rest of the
732                          * stack conservatively.
733                          */
734                         DEBUG (fprintf (logfile, "Mark (0): Frame stack full.\n"));
735                         break;
736                 }
737
738                 /* Scan the frame of this method */
739
740                 /*
741                  * A frame contains the following:
742                  * - saved registers
743                  * - saved args
744                  * - locals
745                  * - spill area
746                  * - localloc-ed memory
747                  */
748                 g_assert (pc_offset >= 0);
749
750                 emap = ji->gc_info;
751
752                 if (!emap) {
753                         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));
754                         DEBUG (fprintf (logfile, "\tNo GC Map.\n"));
755                         continue;
756                 }
757
758                 /*
759                  * Debugging aid to control the number of frames scanned precisely
760                  */
761                 if (!precise_frame_limit_inited) {
762                         if (getenv ("MONO_PRECISE_COUNT"))
763                                 precise_frame_limit = atoi (getenv ("MONO_PRECISE_COUNT"));
764                         precise_frame_limit_inited = TRUE;
765                 }
766                                 
767                 if (precise_frame_limit != -1) {
768                         if (precise_frame_count [FALSE] == precise_frame_limit)
769                                 printf ("LAST PRECISE FRAME: %s\n", mono_method_full_name (ji->method, TRUE));
770                         if (precise_frame_count [FALSE] > precise_frame_limit)
771                                 continue;
772                 }
773                 precise_frame_count [FALSE] ++;
774
775                 /* Decode the encoded GC map */
776                 map = &map_tmp;
777                 memset (map, 0, sizeof (GCMap));
778                 decode_gc_map (&emap->encoded [0], map, &p);
779                 p = (guint8*)ALIGN_TO (p, map->callsite_entry_size);
780                 map->callsites.offsets8 = p;
781                 p += map->callsite_entry_size * map->ncallsites;
782                 bitmaps = p;
783
784 #ifdef __x86_64__
785                 if (map->frame_reg == AMD64_RSP)
786                         fp = (guint8*)ctx.rsp;
787                 else if (map->frame_reg == AMD64_RBP)
788                         fp = (guint8*)ctx.rbp;
789                 else
790                         g_assert_not_reached ();
791 #else
792                 fp = NULL;
793                 g_assert_not_reached ();
794 #endif
795
796                 real_frame_start = fp + map->start_offset;
797                 frame_start = fp + map->start_offset + map->map_offset;
798                 frame_end = fp + map->end_offset;
799
800                 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));
801
802                 /* Find the callsite index */
803                 if (ji->code_size < 256) {
804                         for (i = 0; i < map->ncallsites; ++i)
805                                 /* ip points inside the call instruction */
806                                 if (map->callsites.offsets8 [i] == pc_offset + 1)
807                                         break;
808                 } else if (ji->code_size < 65536) {
809                         // FIXME: Use a binary search
810                         for (i = 0; i < map->ncallsites; ++i)
811                                 /* ip points inside the call instruction */
812                                 if (map->callsites.offsets16 [i] == pc_offset + 1)
813                                         break;
814                 } else {
815                         // FIXME: Use a binary search
816                         for (i = 0; i < map->ncallsites; ++i)
817                                 /* ip points inside the call instruction */
818                                 if (map->callsites.offsets32 [i] == pc_offset + 1)
819                                         break;
820                 }
821                 if (i == map->ncallsites) {
822                         printf ("Unable to find ip offset 0x%x in callsite list of %s.\n", pc_offset + 1, mono_method_full_name (ji->method, TRUE));
823                         g_assert_not_reached ();
824                 }
825                 cindex = i;
826
827                 g_assert (real_frame_start >= stack_limit);
828
829                 if (real_frame_start > stack_limit) {
830                         /* This scans the previously skipped frames as well */
831                         DEBUG (fprintf (logfile, "\tscan area %p-%p (%d).\n", stack_limit, real_frame_start, (int)(real_frame_start - stack_limit)));
832                         mono_gc_conservatively_scan_area (stack_limit, real_frame_start);
833                         stats.scanned_other += real_frame_start - stack_limit;
834                 }
835
836                 /* Mark stack slots */
837                 if (map->has_pin_slots) {
838                         int bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
839                         guint8 *pin_bitmap = &bitmaps [map->stack_pin_bitmap_offset + (bitmap_width * cindex)];
840                         guint8 *p;
841                         gboolean pinned;
842
843                         p = frame_start;
844                         for (i = 0; i < map->nslots; ++i) {
845                                 pinned = pin_bitmap [i / 8] & (1 << (i % 8));
846                                 if (pinned) {
847                                         DEBUG (fprintf (logfile, "\tscan slot %s0x%x(fp)=%p.\n", (guint8*)p > (guint8*)fp ? "" : "-", ABS ((int)((gssize)p - (gssize)fp)), p));
848                                         mono_gc_conservatively_scan_area (p, p + sizeof (mgreg_t));
849                                         scanned_conservatively += sizeof (mgreg_t);
850                                 } else {
851                                         scanned_precisely += sizeof (mgreg_t);
852                                 }
853                                 p += sizeof (mgreg_t);
854                         }
855                 } else {
856                         scanned_precisely += (map->nslots * sizeof (mgreg_t));
857                 }
858
859                 /* The area outside of start-end is NOREF */
860                 scanned_precisely += (map->end_offset - map->start_offset) - (map->nslots * sizeof (mgreg_t));
861
862                 /* Mark registers */
863                 precise_regmask = map->used_int_regs | (1 << map->frame_reg);
864                 if (map->has_pin_regs) {
865                         int bitmap_width = ALIGN_TO (map->npin_regs, 8) / 8;
866                         guint8 *pin_bitmap = &bitmaps [map->reg_pin_bitmap_offset + (bitmap_width * cindex)];
867                         int bindex = 0;
868                         for (i = 0; i < NREGS; ++i) {
869                                 if (!(map->used_int_regs & (1 << i)))
870                                         continue;
871                                 
872                                 if (!(map->reg_pin_mask & (1 << i)))
873                                         continue;
874
875                                 if (pin_bitmap [bindex / 8] & (1 << (bindex % 8))) {
876                                         DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is pinning.\n", mono_arch_regname (i), reg_locations [i]));
877                                         precise_regmask &= ~(1 << i);
878                                 }
879                                 bindex ++;
880                         }
881                 }
882
883                 scanned += map->end_offset - map->start_offset;
884
885                 g_assert (scanned == scanned_precisely + scanned_conservatively);
886
887                 stack_limit = frame_end;
888
889                 /* Save information for the precise pass */
890                 fi = &tls->frames [tls->nframes];
891                 fi->nslots = map->nslots;
892                 bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
893                 if (map->has_ref_slots)
894                         fi->bitmap = &bitmaps [map->stack_ref_bitmap_offset + (bitmap_width * cindex)];
895                 else
896                         fi->bitmap = NULL;
897                 fi->frame_start_offset = frame_start - stack_start;
898                 fi->nreg_locations = 0;
899                 DEBUG_PRECISE (fi->ji = ji);
900                 DEBUG_PRECISE (fi->fp = fp);
901
902                 if (map->has_ref_regs) {
903                         int bitmap_width = ALIGN_TO (map->nref_regs, 8) / 8;
904                         guint8 *ref_bitmap = &bitmaps [map->reg_ref_bitmap_offset + (bitmap_width * cindex)];
905                         int bindex = 0;
906                         for (i = 0; i < NREGS; ++i) {
907                                 if (!(map->reg_ref_mask & (1 << i)))
908                                         continue;
909
910                                 if (reg_locations [i] && (ref_bitmap [bindex / 8] & (1 << (bindex % 8)))) {
911                                         DEBUG_PRECISE (fi->regs [fi->nreg_locations] = i);
912                                         DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is ref.\n", mono_arch_regname (i), reg_locations [i]));
913                                         fi->reg_locations [fi->nreg_locations] = (guint8*)reg_locations [i] - stack_start;
914                                         fi->nreg_locations ++;
915                                 }
916                                 bindex ++;
917                         }
918                 }
919
920                 /*
921                  * Clear locations of precisely stacked registers.
922                  */
923                 if (precise_regmask) {
924                         for (i = 0; i < NREGS; ++i) {
925                                 if (precise_regmask & (1 << i)) {
926                                         /*
927                                          * The method uses this register, and we have precise info for it.
928                                          * This means the location will be scanned precisely.
929                                          * Tell the code at the beginning of the loop that this location is
930                                          * processed.
931                                          */
932                                         if (reg_locations [i])
933                                                 DEBUG (fprintf (logfile, "\treg %s at location %p (==%p) is precise.\n", mono_arch_regname (i), reg_locations [i], (gpointer)*reg_locations [i]));
934                                         reg_locations [i] = NULL;
935                                 }
936                         }
937                 }
938
939                 tls->nframes ++;
940         }
941
942         /* Scan the remaining register save locations */
943         for (i = 0; i < MONO_MAX_IREGS; ++i) {
944                 if (reg_locations [i]) {
945                         DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", reg_locations [i]));
946                         mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
947                         scanned_registers += sizeof (mgreg_t);
948                 }
949                 if (new_reg_locations [i]) {
950                         DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", new_reg_locations [i]));
951                         mono_gc_conservatively_scan_area (new_reg_locations [i], new_reg_locations [i] + sizeof (mgreg_t));
952                         scanned_registers += sizeof (mgreg_t);
953                 }
954         }
955
956         if (stack_limit < stack_end) {
957                 DEBUG (fprintf (logfile, "\tscan remaining stack %p-%p (%d).\n", stack_limit, stack_end, (int)(stack_end - stack_limit)));
958                 mono_gc_conservatively_scan_area (stack_limit, stack_end);
959                 stats.scanned_native += stack_end - stack_limit;
960         }
961
962         DEBUG (fprintf (logfile, "Marked %d bytes, p=%d,c=%d out of %d.\n", scanned, scanned_precisely, scanned_conservatively, (int)(stack_end - stack_start)));
963
964         stats.scanned_stacks += stack_end - stack_start;
965         stats.scanned += scanned;
966         stats.scanned_precisely += scanned_precisely;
967         stats.scanned_conservatively += scanned_conservatively;
968         stats.scanned_registers += scanned_registers;
969
970         //mono_gc_conservatively_scan_area (stack_start, stack_end);
971 }
972
973 /*
974  * precise_pass:
975  *
976  *   Mark a thread stack precisely based on information saved during the conservative
977  * pass.
978  */
979 static void
980 precise_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
981 {
982         int findex, i;
983         FrameInfo *fi;
984         guint8 *frame_start;
985
986         if (!tls)
987                 return;
988
989         for (findex = 0; findex < tls->nframes; findex ++) {
990                 /* Load information saved by the !precise pass */
991                 fi = &tls->frames [findex];
992                 frame_start = stack_start + fi->frame_start_offset;
993
994                 DEBUG (char *fname = mono_method_full_name (fi->ji->method, TRUE); fprintf (logfile, "Mark(1): %s\n", fname); g_free (fname));
995
996                 /* 
997                  * FIXME: Add a function to mark using a bitmap, to avoid doing a 
998                  * call for each object.
999                  */
1000
1001                 /* Mark stack slots */
1002                 if (fi->bitmap) {
1003                         guint8 *ref_bitmap = fi->bitmap;
1004                         gboolean live;
1005
1006                         for (i = 0; i < fi->nslots; ++i) {
1007                                 MonoObject **ptr = (MonoObject**)(frame_start + (i * sizeof (mgreg_t)));
1008
1009                                 live = ref_bitmap [i / 8] & (1 << (i % 8));
1010
1011                                 if (live) {
1012                                         MonoObject *obj = *ptr;
1013                                         if (obj) {
1014                                                 DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p ->", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
1015                                                 *ptr = mono_gc_scan_object (obj);
1016                                                 DEBUG (fprintf (logfile, " %p.\n", *ptr));
1017                                         } else {
1018                                                 DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p.\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
1019                                         }
1020                                 } else {
1021 #if 0
1022                                         /*
1023                                          * This is disabled because the pointer takes up a lot of space.
1024                                          * Stack slots might be shared between ref and non-ref variables ?
1025                                          */
1026                                         if (map->ref_slots [i / 8] & (1 << (i % 8))) {
1027                                                 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));
1028                                                 /*
1029                                                  * Fail fast if the live range is incorrect, and
1030                                                  * the JITted code tries to access this object
1031                                                  */
1032                                                 *ptr = DEAD_REF;
1033                                         }
1034 #endif
1035                                 }
1036                         }
1037                 }
1038
1039                 /* Mark registers */
1040
1041                 /*
1042                  * Registers are different from stack slots, they have no address where they
1043                  * are stored. Instead, some frame below this frame in the stack saves them
1044                  * in its prolog to the stack. We can mark this location precisely.
1045                  */
1046                 for (i = 0; i < fi->nreg_locations; ++i) {
1047                         /*
1048                          * reg_locations [i] contains the address of the stack slot where
1049                          * a reg was last saved, so mark that slot.
1050                          */
1051                         MonoObject **ptr = (MonoObject**)((guint8*)stack_start + fi->reg_locations [i]);
1052                         MonoObject *obj = *ptr;
1053
1054                         if (obj) {
1055                                 DEBUG (fprintf (logfile, "\treg %s saved at %p: %p ->", mono_arch_regname (fi->regs [i]), ptr, obj));
1056                                 *ptr = mono_gc_scan_object (obj);
1057                                 DEBUG (fprintf (logfile, " %p.\n", *ptr));
1058                         } else {
1059                                 DEBUG (fprintf (logfile, "\treg %s saved at %p: %p\n", mono_arch_regname (fi->regs [i]), ptr, obj));
1060                         }
1061                 }       
1062         }
1063
1064         /*
1065          * Debugging aid to check for missed refs.
1066          */
1067         if (tls->ref_to_track) {
1068                 mgreg_t *p;
1069
1070                 for (p = (mgreg_t*)stack_start; p < (mgreg_t*)stack_end; ++p)
1071                         if (*p == (mgreg_t)tls->ref_to_track)
1072                                 printf ("REF AT %p.\n", p);
1073         }
1074 }
1075
1076 /*
1077  * thread_mark_func:
1078  *
1079  *   This is called by the GC twice to mark a thread stack. PRECISE is FALSE at the first
1080  * call, and TRUE at the second. USER_DATA points to a TlsData
1081  * structure filled up by thread_suspend_func. 
1082  */
1083 static void
1084 thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise)
1085 {
1086         TlsData *tls = user_data;
1087
1088         DEBUG (fprintf (logfile, "****************************************\n"));
1089         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));
1090         DEBUG (fprintf (logfile, "****************************************\n"));
1091
1092         if (!precise)
1093                 conservative_pass (tls, stack_start, stack_end);
1094         else
1095                 precise_pass (tls, stack_start, stack_end);
1096 }
1097
1098 static void
1099 mini_gc_init_gc_map (MonoCompile *cfg)
1100 {
1101         if (COMPILE_LLVM (cfg))
1102                 return;
1103
1104 #if 1
1105         /* Debugging support */
1106         {
1107                 static int precise_count;
1108
1109                 precise_count ++;
1110                 if (getenv ("MONO_GCMAP_COUNT")) {
1111                         if (precise_count == atoi (getenv ("MONO_GCMAP_COUNT")))
1112                                 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
1113                         if (precise_count > atoi (getenv ("MONO_GCMAP_COUNT")))
1114                                 return;
1115                 }
1116         }
1117 #endif
1118
1119         cfg->compute_gc_maps = TRUE;
1120
1121         cfg->gc_info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoCompileGC));
1122 }
1123
1124 /*
1125  * mini_gc_set_slot_type_from_fp:
1126  *
1127  *   Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1128  * relative to the frame pointer. By default, all stack slots are type PIN, so there is no
1129  * need to call this function for those slots.
1130  */
1131 void
1132 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
1133 {
1134         MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
1135
1136         if (!cfg->compute_gc_maps)
1137                 return;
1138
1139         g_assert (slot_offset % sizeof (mgreg_t) == 0);
1140
1141         gcfg->stack_slots_from_fp = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_fp, GINT_TO_POINTER (((slot_offset) << 16) | type));
1142 }
1143
1144 /*
1145  * mini_gc_set_slot_type_from_cfa:
1146  *
1147  *   Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1148  * relative to the DWARF CFA value. This should be called from mono_arch_emit_prolog ().
1149  * If type is STACK_REF, the slot is assumed to be live from the end of the prolog until
1150  * the end of the method. By default, all stack slots are type PIN, so there is no need to
1151  * call this function for those slots.
1152  */
1153 void
1154 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
1155 {
1156         MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
1157         int slot = - (slot_offset / sizeof (mgreg_t));
1158
1159         if (!cfg->compute_gc_maps)
1160                 return;
1161
1162         g_assert (slot_offset <= 0);
1163         g_assert (slot_offset % sizeof (mgreg_t) == 0);
1164
1165         gcfg->stack_slots_from_cfa = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_cfa, GUINT_TO_POINTER (((slot) << 16) | type));
1166 }
1167
1168 static inline int
1169 fp_offset_to_slot (MonoCompile *cfg, int offset)
1170 {
1171         MonoCompileGC *gcfg = cfg->gc_info;
1172
1173         return (offset - gcfg->min_offset) / sizeof (mgreg_t);
1174 }
1175
1176 static inline int
1177 slot_to_fp_offset (MonoCompile *cfg, int slot)
1178 {
1179         MonoCompileGC *gcfg = cfg->gc_info;
1180
1181         return (slot * sizeof (mgreg_t)) + gcfg->min_offset;
1182 }
1183
1184 static inline void
1185 set_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1186 {
1187         g_assert (slot >= 0 && slot < gcfg->nslots);
1188
1189         if (type == SLOT_PIN) {
1190                 clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1191                 set_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1192         } else if (type == SLOT_REF) {
1193                 set_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1194                 clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1195         } else if (type == SLOT_NOREF) {
1196                 clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1197                 clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1198         }
1199 }
1200
1201 static inline void
1202 set_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1203 {
1204         int cindex;
1205
1206         for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1207                 set_slot (gcfg, slot, cindex, type);
1208 }
1209
1210 static inline void
1211 set_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1212 {
1213         int cindex;
1214
1215         for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1216                 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1217                 if (callsite_offset >= from && callsite_offset < to)
1218                         set_slot (gcfg, slot, cindex, type);
1219         }
1220 }
1221
1222 static inline void
1223 set_reg_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1224 {
1225         g_assert (slot >= 0 && slot < gcfg->nregs);
1226
1227         if (type == SLOT_PIN) {
1228                 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1229                 set_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1230         } else if (type == SLOT_REF) {
1231                 set_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1232                 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1233         } else if (type == SLOT_NOREF) {
1234                 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1235                 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1236         }
1237 }
1238
1239 static inline void
1240 set_reg_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1241 {
1242         int cindex;
1243
1244         for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1245                 set_reg_slot (gcfg, slot, cindex, type);
1246 }
1247
1248 static inline void
1249 set_reg_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1250 {
1251         int cindex;
1252
1253         for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1254                 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1255                 if (callsite_offset >= from && callsite_offset < to)
1256                         set_reg_slot (gcfg, slot, cindex, type);
1257         }
1258 }
1259
1260 static void
1261 process_spill_slots (MonoCompile *cfg)
1262 {
1263         MonoCompileGC *gcfg = cfg->gc_info;
1264         MonoBasicBlock *bb;
1265         GSList *l;
1266         int i;
1267
1268         /* Mark all ref/pin spill slots as NOREF by default outside of their live range */
1269         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1270                 for (l = bb->spill_slot_defs; l; l = l->next) {
1271                         MonoInst *def = l->data;
1272                         int spill_slot = def->inst_c0;
1273                         int bank = def->inst_c1;
1274                         int offset = cfg->spill_info [bank][spill_slot].offset;
1275                         int slot = fp_offset_to_slot (cfg, offset);
1276
1277                         if (bank == MONO_REG_INT_MP || bank == MONO_REG_INT_REF)
1278                                 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1279                 }
1280         }
1281
1282         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1283                 for (l = bb->spill_slot_defs; l; l = l->next) {
1284                         MonoInst *def = l->data;
1285                         int spill_slot = def->inst_c0;
1286                         int bank = def->inst_c1;
1287                         int offset = cfg->spill_info [bank][spill_slot].offset;
1288                         int slot = fp_offset_to_slot (cfg, offset);
1289                         GCSlotType type;
1290
1291                         if (bank == MONO_REG_INT_MP)
1292                                 type = SLOT_PIN;
1293                         else
1294                                 type = SLOT_REF;
1295
1296                         /*
1297                          * Extend the live interval for the GC tracked spill slots
1298                          * defined in this bblock.
1299                          * FIXME: This is not needed.
1300                          */
1301                         set_slot_in_range (gcfg, slot, def->backend.pc_offset, bb->native_offset + bb->native_length, type);
1302
1303                         if (cfg->verbose_level > 1)
1304                                 printf ("\t%s spill slot at %s0x%x(fp) (slot = %d)\n", slot_type_to_string (type), offset >= 0 ? "" : "-", ABS (offset), slot);
1305                 }
1306         }
1307
1308         /* Set fp spill slots to NOREF */
1309         for (i = 0; i < cfg->spill_info_len [MONO_REG_DOUBLE]; ++i) {
1310                 int offset = cfg->spill_info [MONO_REG_DOUBLE][i].offset;
1311                 int slot;
1312
1313                 if (offset == -1)
1314                         continue;
1315
1316                 slot = fp_offset_to_slot (cfg, offset);
1317
1318                 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1319                 /* FIXME: 32 bit */
1320                 if (cfg->verbose_level > 1)
1321                         printf ("\tfp spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
1322         }
1323
1324         /* Set int spill slots to NOREF */
1325         for (i = 0; i < cfg->spill_info_len [MONO_REG_INT]; ++i) {
1326                 int offset = cfg->spill_info [MONO_REG_INT][i].offset;
1327                 int slot;
1328
1329                 if (offset == -1)
1330                         continue;
1331
1332                 slot = fp_offset_to_slot (cfg, offset);
1333
1334                 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1335                 if (cfg->verbose_level > 1)
1336                         printf ("\tint spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
1337         }
1338 }
1339
1340 /*
1341  * process_other_slots:
1342  *
1343  *   Process stack slots registered using mini_gc_set_slot_type_... ().
1344  */
1345 static void
1346 process_other_slots (MonoCompile *cfg)
1347 {
1348         MonoCompileGC *gcfg = cfg->gc_info;
1349         GSList *l;
1350
1351         /* Relative to the CFA */
1352         for (l = gcfg->stack_slots_from_cfa; l; l = l->next) {
1353                 guint data = GPOINTER_TO_UINT (l->data);
1354                 int cfa_slot = data >> 16;
1355                 GCSlotType type = data & 0xff;
1356                 int slot;
1357                 
1358                 /*
1359                  * Map the cfa relative slot to an fp relative slot.
1360                  * slot_addr == cfa - <cfa_slot>*4/8
1361                  * fp + cfa_offset == cfa
1362                  * -> slot_addr == fp + (cfa_offset - <cfa_slot>*4/8)
1363                  */
1364                 slot = (cfg->cfa_offset / sizeof (mgreg_t)) - cfa_slot - (gcfg->min_offset / sizeof (mgreg_t));
1365
1366                 set_slot_everywhere (gcfg, slot, type);
1367
1368                 if (cfg->verbose_level > 1) {
1369                         int fp_offset = slot_to_fp_offset (cfg, slot);
1370                         if (type == SLOT_NOREF)
1371                                 printf ("\tnoref slot at %s0x%x(fp) (slot = %d) (cfa - 0x%x)\n", fp_offset >= 0 ? "" : "-", ABS (fp_offset), slot, (int)(cfa_slot * sizeof (mgreg_t)));
1372                 }
1373         }
1374
1375         /* Relative to the FP */
1376         for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
1377                 gint data = GPOINTER_TO_INT (l->data);
1378                 int offset = data >> 16;
1379                 GCSlotType type = data & 0xff;
1380                 int slot;
1381                 
1382                 slot = fp_offset_to_slot (cfg, offset);
1383
1384                 set_slot_everywhere (gcfg, slot, type);
1385
1386                 /* Liveness for these slots is handled by process_spill_slots () */
1387
1388                 if (cfg->verbose_level > 1) {
1389                         if (type == SLOT_REF)
1390                                 printf ("\tref slot at fp+0x%x (slot = %d)\n", offset, slot);
1391                 }
1392         }
1393 }
1394
1395 static void
1396 process_variables (MonoCompile *cfg)
1397 {
1398         MonoCompileGC *gcfg = cfg->gc_info;
1399         MonoMethodSignature *sig = mono_method_signature (cfg->method);
1400         int i, locals_min_slot, locals_max_slot, cindex;
1401         MonoBasicBlock *bb;
1402         MonoInst *tmp;
1403         int *pc_offsets;
1404         int locals_min_offset = gcfg->locals_min_offset;
1405         int locals_max_offset = gcfg->locals_max_offset;
1406
1407         /* Slots for locals are NOREF by default */
1408         locals_min_slot = (locals_min_offset - gcfg->min_offset) / sizeof (mgreg_t);
1409         locals_max_slot = (locals_max_offset - gcfg->min_offset) / sizeof (mgreg_t);
1410         for (i = locals_min_slot; i < locals_max_slot; ++i) {
1411                 set_slot_everywhere (gcfg, i, SLOT_NOREF);
1412         }
1413
1414         /*
1415          * Compute the offset where variables are initialized in the first bblock, if any.
1416          */
1417         pc_offsets = g_new0 (int, cfg->next_vreg);
1418
1419         bb = cfg->bb_entry->next_bb;
1420         MONO_BB_FOR_EACH_INS (bb, tmp) {
1421                 if (tmp->opcode == OP_GC_LIVENESS_DEF) {
1422                         int vreg = tmp->inst_c1;
1423                         if (pc_offsets [vreg] == 0) {
1424                                 g_assert (tmp->backend.pc_offset > 0);
1425                                 pc_offsets [vreg] = tmp->backend.pc_offset;
1426                         }
1427                 }
1428         }
1429
1430         /*
1431          * Stack slots holding arguments are initialized in the prolog.
1432          * This means we can treat them alive for the whole method.
1433          */
1434         for (i = 0; i < cfg->num_varinfo; i++) {
1435                 MonoInst *ins = cfg->varinfo [i];
1436                 MonoType *t = ins->inst_vtype;
1437                 MonoMethodVar *vmv;
1438                 guint32 pos;
1439                 gboolean byref, is_this = FALSE;
1440                 gboolean is_arg = i < cfg->locals_start;
1441
1442                 if (ins == cfg->ret)
1443                         continue;
1444
1445                 vmv = MONO_VARINFO (cfg, i);
1446
1447                 /* For some reason, 'this' is byref */
1448                 if (sig->hasthis && ins == cfg->args [0] && !cfg->method->klass->valuetype) {
1449                         t = &cfg->method->klass->byval_arg;
1450                         is_this = TRUE;
1451                 }
1452
1453                 byref = t->byref;
1454
1455                 if (ins->opcode == OP_REGVAR) {
1456                         int hreg;
1457                         GCSlotType slot_type;
1458
1459                         t = mini_type_get_underlying_type (NULL, t);
1460
1461                         hreg = ins->dreg;
1462                         g_assert (hreg < MONO_MAX_IREGS);
1463
1464                         if (byref)
1465                                 slot_type = SLOT_PIN;
1466                         else
1467                                 slot_type = MONO_TYPE_IS_REFERENCE (t) ? SLOT_REF : SLOT_NOREF;
1468
1469                         if (slot_type == SLOT_PIN) {
1470                                 /* These have no live interval, be conservative */
1471                                 set_reg_slot_everywhere (gcfg, hreg, slot_type);
1472                         } else {
1473                                 /*
1474                                  * Unlike variables allocated to the stack, we generate liveness info
1475                                  * for noref vars in registers in mono_spill_global_vars (), because
1476                                  * knowing that a register doesn't contain a ref allows us to mark its save
1477                                  * locations precisely.
1478                                  */
1479                                 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1480                                         if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1481                                                 set_reg_slot (gcfg, hreg, cindex, slot_type);
1482                         }
1483
1484                         if (cfg->verbose_level > 1) {
1485                                 printf ("\t%s %sreg %s(R%d)\n", slot_type_to_string (slot_type), is_arg ? "arg " : "", mono_arch_regname (hreg), vmv->vreg);
1486                         }
1487
1488                         continue;
1489                 }
1490
1491                 if (ins->opcode != OP_REGOFFSET)
1492                         continue;
1493
1494                 if (ins->inst_offset % sizeof (mgreg_t) != 0)
1495                         continue;
1496
1497                 if (is_arg && ins->inst_offset >= gcfg->max_offset)
1498                         /* In parent frame */
1499                         continue;
1500
1501                 pos = fp_offset_to_slot (cfg, ins->inst_offset);
1502
1503                 if (is_arg && ins->flags & MONO_INST_IS_DEAD) {
1504                         /* These do not get stored in the prolog */
1505                         set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1506
1507                         if (cfg->verbose_level > 1) {
1508                                 printf ("\tdead arg at fp%s0x%x (slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "+", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, pos, mono_type_full_name (ins->inst_vtype));
1509                         }
1510                         continue;
1511                 }
1512
1513                 if (MONO_TYPE_ISSTRUCT (t)) {
1514                         int numbits = 0, j;
1515                         gsize *bitmap = NULL;
1516                         gboolean pin = FALSE;
1517                         int size;
1518                         int size_in_slots;
1519                         
1520                         if (ins->backend.is_pinvoke)
1521                                 size = mono_class_native_size (ins->klass, NULL);
1522                         else
1523                                 size = mono_class_value_size (ins->klass, NULL);
1524                         size_in_slots = ALIGN_TO (size, sizeof (mgreg_t)) / sizeof (mgreg_t);
1525
1526                         if (!ins->klass->has_references) {
1527                                 if (is_arg) {
1528                                         for (j = 0; j < size_in_slots; ++j)
1529                                                 set_slot_everywhere (gcfg, pos + j, SLOT_NOREF);
1530                                 }
1531                                 continue;
1532                         }
1533
1534                         if (ins->klass->generic_container || mono_class_is_open_constructed_type (t)) {
1535                                 /* FIXME: Generic sharing */
1536                                 pin = TRUE;
1537                         } else {
1538                                 mono_class_compute_gc_descriptor (ins->klass);
1539
1540                                 bitmap = mono_gc_get_bitmap_for_descr (ins->klass->gc_descr, &numbits);
1541                                 g_assert (bitmap);
1542
1543                                 /*
1544                                  * Most vtypes are marked volatile because of the LDADDR instructions,
1545                                  * and they have no liveness information since they are decomposed
1546                                  * before the liveness pass. We emit OP_GC_LIVENESS_DEF instructions for
1547                                  * them during VZERO decomposition.
1548                                  */
1549                                 if (!pc_offsets [vmv->vreg])
1550                                         pin = TRUE;
1551                         }
1552
1553                         if (ins->backend.is_pinvoke)
1554                                 pin = TRUE;
1555
1556                         if (cfg->verbose_level > 1)
1557                                 printf ("\tvtype R%d at fp+0x%x-0x%x: %s\n", vmv->vreg, (int)ins->inst_offset, (int)(ins->inst_offset + (size / sizeof (mgreg_t))), mono_type_full_name (ins->inst_vtype));
1558
1559                         if (bitmap) {
1560                                 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1561                                         if (gcfg->callsites [cindex]->pc_offset > pc_offsets [vmv->vreg]) {
1562                                                 for (j = 0; j < numbits; ++j) {
1563                                                         if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD))) {
1564                                                                 /* The descriptor is for the boxed object */
1565                                                                 set_slot (gcfg, (pos + j - (sizeof (MonoObject) / sizeof (mgreg_t))), cindex, pin ? SLOT_PIN : SLOT_REF);
1566                                                         }
1567                                                 }
1568                                         }
1569                                 }
1570
1571                                 if (cfg->verbose_level > 1) {
1572                                         for (j = 0; j < numbits; ++j) {
1573                                                 if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD)))
1574                                                         printf ("\t\t%s slot at 0x%x(fp) (slot = %d)\n", pin ? "pin" : "ref", (int)(ins->inst_offset + (j * sizeof (mgreg_t))), (int)(pos + j - (sizeof (MonoObject) / sizeof (mgreg_t))));
1575                                         }
1576                                 }
1577                         } else {
1578                                 if (cfg->verbose_level > 1)
1579                                         printf ("\t\tpinned\n");
1580                                 for (j = 0; j < size_in_slots; ++j) {
1581                                         set_slot_everywhere (gcfg, pos + j, SLOT_PIN);
1582                                 }
1583                         }
1584
1585                         g_free (bitmap);
1586
1587                         continue;
1588                 }
1589
1590                 if (!is_arg && (ins->inst_offset < gcfg->min_offset || ins->inst_offset >= gcfg->max_offset))
1591                         /* Vret addr etc. */
1592                         continue;
1593
1594                 if (t->byref) {
1595                         if (is_arg) {
1596                                 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1597                         } else {
1598                                 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1599                                         if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1600                                                 set_slot (gcfg, pos, cindex, SLOT_PIN);
1601                         }
1602                         if (cfg->verbose_level > 1)
1603                                 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));
1604                         continue;
1605                 }
1606
1607                 /*
1608                  * This is currently disabled, but could be enabled to debug crashes.
1609                  */
1610 #if 0
1611                 if (t->type == MONO_TYPE_I) {
1612                         /*
1613                          * Variables created in mono_handle_global_vregs have type I, but they
1614                          * could hold GC refs since the vregs they were created from might not been
1615                          * marked as holding a GC ref. So be conservative.
1616                          */
1617                         set_slot_everywhere (gcfg, pos, SLOT_PIN);
1618                         continue;
1619                 }
1620 #endif
1621
1622                 t = mini_type_get_underlying_type (NULL, t);
1623
1624                 if (!MONO_TYPE_IS_REFERENCE (t)) {
1625                         set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1626                         if (cfg->verbose_level > 1)
1627                                 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));
1628                         continue;
1629                 }
1630
1631                 /* 'this' is marked INDIRECT for gshared methods */
1632                 if (ins->flags & (MONO_INST_VOLATILE | MONO_INST_INDIRECT) && !is_this) {
1633                         /*
1634                          * For volatile variables, treat them alive from the point they are
1635                          * initialized in the first bblock until the end of the method.
1636                          */
1637                         if (is_arg) {
1638                                 set_slot_everywhere (gcfg, pos, SLOT_REF);
1639                         } else if (pc_offsets [vmv->vreg]) {
1640                                 set_slot_in_range (gcfg, pos, 0, pc_offsets [vmv->vreg], SLOT_PIN);
1641                                 set_slot_in_range (gcfg, pos, pc_offsets [vmv->vreg], cfg->code_size, SLOT_REF);
1642                         } else {
1643                                 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1644                         }
1645                         if (cfg->verbose_level > 1)
1646                                 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));
1647                         continue;
1648                 }
1649
1650                 if (is_arg) {
1651                         /* Live for the whole method */
1652                         set_slot_everywhere (gcfg, pos, SLOT_REF);
1653                 } else {
1654                         for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1655                                 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1656                                         set_slot (gcfg, pos, cindex, SLOT_REF);
1657                 }
1658
1659                 if (cfg->verbose_level > 1) {
1660                         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));
1661                 }
1662         }
1663
1664         g_free (pc_offsets);
1665 }
1666
1667 static int
1668 sp_offset_to_fp_offset (MonoCompile *cfg, int sp_offset)
1669 {
1670         /* 
1671          * Convert a sp relative offset to a slot index. This is
1672          * platform specific.
1673          */
1674 #ifdef TARGET_AMD64
1675         /* fp = sp + offset */
1676         g_assert (cfg->frame_reg == AMD64_RBP);
1677         return (- cfg->arch.sp_fp_offset + sp_offset);
1678 #else
1679         NOT_IMPLEMENTED;
1680         return -1;
1681 #endif
1682 }
1683
1684 static GCSlotType
1685 type_to_gc_slot_type (MonoType *t)
1686 {
1687         if (t->byref)
1688                 return SLOT_PIN;
1689         t = mini_type_get_underlying_type (NULL, t);
1690         if (MONO_TYPE_IS_REFERENCE (t))
1691                 return SLOT_REF;
1692         else {
1693                 if (MONO_TYPE_ISSTRUCT (t)) {
1694                         MonoClass *klass = mono_class_from_mono_type (t);
1695                         if (!klass->has_references) {
1696                                 return SLOT_NOREF;
1697                         } else {
1698                                 // FIXME:
1699                                 return SLOT_PIN;
1700                         }
1701                 }
1702                 return SLOT_NOREF;
1703         }
1704 }
1705
1706 static void
1707 process_param_area_slots (MonoCompile *cfg)
1708 {
1709         MonoCompileGC *gcfg = cfg->gc_info;
1710         int i;
1711         gboolean *is_param;
1712
1713         /*
1714          * These slots are used for passing parameters during calls. They are sp relative, not
1715          * fp relative, so they are harder to handle.
1716          */
1717         if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1718                 /* The distance between fp and sp is not constant */
1719                 return;
1720
1721         is_param = mono_mempool_alloc0 (cfg->mempool, gcfg->nslots * sizeof (gboolean));
1722
1723         for (i = 0; i < gcfg->ncallsites; ++i) {
1724                 GCCallSite *callsite = gcfg->callsites [i];
1725                 GSList *l;
1726
1727                 for (l = callsite->param_slots; l; l = l->next) {
1728                         MonoInst *def = l->data;
1729                         int sp_offset = def->inst_offset;
1730                         int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
1731                         int slot = fp_offset_to_slot (cfg, fp_offset);
1732
1733                         g_assert (slot >= 0 && slot < gcfg->nslots);
1734                         is_param [slot] = TRUE;
1735                 }
1736         }
1737
1738         /* All param area slots are noref by default */
1739         for (i = 0; i < gcfg->nslots; ++i) {
1740                 if (is_param [i])
1741                         set_slot_everywhere (gcfg, i, SLOT_NOREF);
1742         }
1743
1744         for (i = 0; i < gcfg->ncallsites; ++i) {
1745                 GCCallSite *callsite = gcfg->callsites [i];
1746                 GSList *l;
1747
1748                 for (l = callsite->param_slots; l; l = l->next) {
1749                         MonoInst *def = l->data;
1750                         MonoType *t = def->inst_vtype;
1751                         int sp_offset = def->inst_offset;
1752                         int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
1753                         int slot = fp_offset_to_slot (cfg, fp_offset);
1754                         GCSlotType type = type_to_gc_slot_type (t);
1755
1756                         /* The slot is live between the def instruction and the call */
1757                         set_slot_in_range (gcfg, slot, def->backend.pc_offset, callsite->pc_offset + 1, type);
1758                         if (cfg->verbose_level > 1)
1759                                 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), fp_offset >= 0 ? "+" : "-", ABS (fp_offset), sp_offset, slot, def->backend.pc_offset, callsite->pc_offset + 1);
1760                 }
1761         }
1762 }
1763
1764 static void
1765 process_finally_clauses (MonoCompile *cfg)
1766 {
1767         MonoCompileGC *gcfg = cfg->gc_info;
1768         GCCallSite **callsites;
1769         int ncallsites;
1770         gboolean has_finally;
1771         int i, j, nslots, nregs;
1772
1773         ncallsites = gcfg->ncallsites;
1774         nslots = gcfg->nslots;
1775         nregs = gcfg->nregs;
1776         callsites = gcfg->callsites;
1777
1778         /*
1779          * The calls to the finally clauses don't show up in the cfg. See
1780          * test_0_liveness_8 ().
1781          * Variables accessed inside the finally clause are already marked VOLATILE by
1782          * mono_liveness_handle_exception_clauses (). Variables not accessed inside the finally clause have
1783          * correct liveness outside the finally clause. So mark them PIN inside the finally clauses.
1784          */
1785         has_finally = FALSE;
1786         for (i = 0; i < cfg->header->num_clauses; ++i) {
1787                 MonoExceptionClause *clause = &cfg->header->clauses [i];
1788
1789                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
1790                         has_finally = TRUE;
1791                 }
1792         }
1793         if (has_finally) {
1794                 if (cfg->verbose_level > 1)
1795                         printf ("\tMethod has finally clauses, pessimizing live ranges.\n");
1796                 for (j = 0; j < ncallsites; ++j) {
1797                         MonoBasicBlock *bb = callsites [j]->bb;
1798                         MonoExceptionClause *clause;
1799                         gboolean is_in_finally = FALSE;
1800
1801                         for (i = 0; i < cfg->header->num_clauses; ++i) {
1802                                 clause = &cfg->header->clauses [i];
1803                            
1804                                 if (MONO_OFFSET_IN_HANDLER (clause, bb->real_offset)) {
1805                                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
1806                                                 is_in_finally = TRUE;
1807                                                 break;
1808                                         }
1809                                 }
1810                         }
1811
1812                         if (is_in_finally) {
1813                                 for (i = 0; i < nslots; ++i)
1814                                         set_slot (gcfg, i, j, SLOT_PIN);
1815                                 for (i = 0; i < nregs; ++i)
1816                                         set_reg_slot (gcfg, i, j, SLOT_PIN);
1817                         }
1818                 }
1819         }
1820 }
1821
1822 static void
1823 compute_frame_size (MonoCompile *cfg)
1824 {
1825         int i, locals_min_offset, locals_max_offset, cfa_min_offset, cfa_max_offset;
1826         int min_offset, max_offset;
1827         MonoCompileGC *gcfg = cfg->gc_info;
1828         MonoMethodSignature *sig = mono_method_signature (cfg->method);
1829         GSList *l;
1830
1831         /* Compute min/max offsets from the fp */
1832
1833         /* Locals */
1834 #ifdef TARGET_AMD64
1835         locals_min_offset = ALIGN_TO (cfg->locals_min_stack_offset, sizeof (mgreg_t));
1836         locals_max_offset = cfg->locals_max_stack_offset;
1837 #else
1838         /* min/max stack offset needs to be computed in mono_arch_allocate_vars () */
1839         NOT_IMPLEMENTED;
1840 #endif
1841
1842         locals_min_offset = ALIGN_TO (locals_min_offset, sizeof (mgreg_t));
1843         locals_max_offset = ALIGN_TO (locals_max_offset, sizeof (mgreg_t));
1844
1845         min_offset = locals_min_offset;
1846         max_offset = locals_max_offset;
1847
1848         /* Arguments */
1849         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1850                 MonoInst *ins = cfg->args [i];
1851
1852                 if (ins->opcode == OP_REGOFFSET)
1853                         min_offset = MIN (min_offset, ins->inst_offset);
1854         }
1855
1856         /* Cfa slots */
1857         g_assert (cfg->frame_reg == cfg->cfa_reg);
1858         g_assert (cfg->cfa_offset > 0);
1859         cfa_min_offset = 0;
1860         cfa_max_offset = cfg->cfa_offset;
1861
1862         min_offset = MIN (min_offset, cfa_min_offset);
1863         max_offset = MAX (max_offset, cfa_max_offset);
1864
1865         /* Fp relative slots */
1866         for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
1867                 gint data = GPOINTER_TO_INT (l->data);
1868                 int offset = data >> 16;
1869
1870                 min_offset = MIN (min_offset, offset);
1871         }
1872
1873         /* Spill slots */
1874         if (!(cfg->flags & MONO_CFG_HAS_SPILLUP)) {
1875                 int stack_offset = ALIGN_TO (cfg->stack_offset, sizeof (mgreg_t));
1876                 min_offset = MIN (min_offset, (-stack_offset));
1877         }
1878
1879         /* Param area slots */
1880 #ifdef TARGET_AMD64
1881         min_offset = MIN (min_offset, -cfg->arch.sp_fp_offset);
1882 #endif
1883
1884         gcfg->min_offset = min_offset;
1885         gcfg->max_offset = max_offset;
1886         gcfg->locals_min_offset = locals_min_offset;
1887         gcfg->locals_max_offset = locals_max_offset;
1888 }
1889
1890 static void
1891 init_gcfg (MonoCompile *cfg)
1892 {
1893         int i, nregs, nslots;
1894         MonoCompileGC *gcfg = cfg->gc_info;
1895         GCCallSite **callsites;
1896         int ncallsites;
1897         MonoBasicBlock *bb;
1898         GSList *l;
1899
1900         /*
1901          * Collect callsites
1902          */
1903         ncallsites = 0;
1904         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1905                 ncallsites += g_slist_length (bb->gc_callsites);
1906         }
1907         callsites = mono_mempool_alloc0 (cfg->mempool, ncallsites * sizeof (GCCallSite*));
1908         i = 0;
1909         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1910                 for (l = bb->gc_callsites; l; l = l->next)
1911                         callsites [i++] = l->data;
1912         }
1913
1914         /* The callsites should already be ordered by pc offset */
1915         for (i = 1; i < ncallsites; ++i)
1916                 g_assert (callsites [i - 1]->pc_offset < callsites [i]->pc_offset);
1917
1918         /*
1919          * The stack frame looks like this:
1920          *
1921          * <fp + max_offset> == cfa ->  <end of previous frame>
1922          *                              <other stack slots>
1923          *                              <locals>
1924          *                              <other stack slots>
1925          * fp + min_offset          ->
1926          * ...
1927          * fp                       ->
1928          */
1929
1930         if (cfg->verbose_level > 1)
1931                 printf ("GC Map for %s: 0x%x-0x%x\n", mono_method_full_name (cfg->method, TRUE), gcfg->min_offset, gcfg->max_offset);
1932
1933         nslots = (gcfg->max_offset - gcfg->min_offset) / sizeof (mgreg_t);
1934         nregs = NREGS;
1935
1936         gcfg->nslots = nslots;
1937         gcfg->nregs = nregs;
1938         gcfg->callsites = callsites;
1939         gcfg->ncallsites = ncallsites;
1940         gcfg->stack_bitmap_width = ALIGN_TO (nslots, 8) / 8;
1941         gcfg->reg_bitmap_width = ALIGN_TO (nregs, 8) / 8;
1942         gcfg->stack_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * ncallsites);
1943         gcfg->stack_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * ncallsites);
1944         gcfg->reg_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * ncallsites);
1945         gcfg->reg_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * ncallsites);
1946
1947         /* All slots start out as PIN */
1948         memset (gcfg->stack_pin_bitmap, 0xff, gcfg->stack_bitmap_width * ncallsites);
1949         for (i = 0; i < nregs; ++i) {
1950                 /*
1951                  * By default, registers are NOREF.
1952                  * It is possible for a callee to save them before being defined in this method,
1953                  * but the saved value is dead too, so it doesn't need to be marked.
1954                  */
1955                 if ((cfg->used_int_regs & (1 << i)))
1956                         set_reg_slot_everywhere (gcfg, i, SLOT_NOREF);
1957         }
1958 }
1959
1960 static void
1961 create_map (MonoCompile *cfg)
1962 {
1963         GCMap *map;
1964         int i, j, nregs, nslots, nref_regs, npin_regs, alloc_size, bitmaps_size, bitmaps_offset;
1965         int ntypes [16];
1966         int stack_bitmap_width, stack_bitmap_size, reg_ref_bitmap_width, reg_ref_bitmap_size;
1967         int reg_pin_bitmap_width, reg_pin_bitmap_size, bindex;
1968         int start, end;
1969         gboolean has_ref_slots, has_pin_slots, has_ref_regs, has_pin_regs;
1970         MonoCompileGC *gcfg = cfg->gc_info;
1971         GCCallSite **callsites;
1972         int ncallsites;
1973         guint8 *bitmap, *bitmaps;
1974         guint32 reg_ref_mask, reg_pin_mask;
1975
1976         ncallsites = gcfg->ncallsites;
1977         nslots = gcfg->nslots;
1978         nregs = gcfg->nregs;
1979         callsites = gcfg->callsites;
1980
1981         /* 
1982          * Compute the real size of the bitmap i.e. ignore NOREF columns at the beginning and at
1983          * the end. Also, compute whenever the map needs ref/pin bitmaps, and collect stats.
1984          */
1985         has_ref_slots = FALSE;
1986         has_pin_slots = FALSE;
1987         start = -1;
1988         end = -1;
1989         memset (ntypes, 0, sizeof (ntypes));
1990         for (i = 0; i < nslots; ++i) {
1991                 gboolean has_ref = FALSE;
1992                 gboolean has_pin = FALSE;
1993
1994                 for (j = 0; j < ncallsites; ++j) {
1995                         if (get_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, j, i))
1996                                 has_pin = TRUE;
1997                         if (get_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, j, i))
1998                                 has_ref = TRUE;
1999                 }
2000
2001                 if (has_ref)
2002                         has_ref_slots = TRUE;
2003                 if (has_pin)
2004                         has_pin_slots = TRUE;
2005
2006                 if (has_ref)
2007                         ntypes [SLOT_REF] ++;
2008                 else if (has_pin)
2009                         ntypes [SLOT_PIN] ++;
2010                 else
2011                         ntypes [SLOT_NOREF] ++;
2012
2013                 if (has_ref || has_pin) {
2014                         if (start == -1)
2015                                 start = i;
2016                         end = i + 1;
2017                 }
2018         }
2019         if (start == -1) {
2020                 start = end = nslots;
2021         } else {
2022                 g_assert (start != -1);
2023                 g_assert (start < end);
2024         }
2025
2026         has_ref_regs = FALSE;
2027         has_pin_regs = FALSE;
2028         reg_ref_mask = 0;
2029         reg_pin_mask = 0;
2030         nref_regs = 0;
2031         npin_regs = 0;
2032         for (i = 0; i < nregs; ++i) {
2033                 gboolean has_ref = FALSE;
2034                 gboolean has_pin = FALSE;
2035
2036                 if (!(cfg->used_int_regs & (1 << i)))
2037                         continue;
2038
2039                 for (j = 0; j < ncallsites; ++j) {
2040                         if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, j, i)) {
2041                                 has_ref = TRUE;
2042                                 break;
2043                         }
2044                 }
2045                 for (j = 0; j < ncallsites; ++j) {
2046                         if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, j, i)) {
2047                                 has_pin = TRUE;
2048                                 break;
2049                         }
2050                 }
2051
2052                 if (has_ref) {
2053                         reg_ref_mask |= (1 << i);
2054                         has_ref_regs = TRUE;
2055                         nref_regs ++;
2056                 }
2057                 if (has_pin) {
2058                         reg_pin_mask |= (1 << i);
2059                         has_pin_regs = TRUE;
2060                         npin_regs ++;
2061                 }
2062         }
2063
2064         if (cfg->verbose_level > 1)
2065                 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);
2066
2067         /* Create the GC Map */
2068
2069         stack_bitmap_width = ALIGN_TO (end - start, 8) / 8;
2070         stack_bitmap_size = stack_bitmap_width * ncallsites;
2071         reg_ref_bitmap_width = ALIGN_TO (nref_regs, 8) / 8;
2072         reg_ref_bitmap_size = reg_ref_bitmap_width * ncallsites;
2073         reg_pin_bitmap_width = ALIGN_TO (npin_regs, 8) / 8;
2074         reg_pin_bitmap_size = reg_pin_bitmap_width * ncallsites;
2075         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);
2076         
2077         map = mono_mempool_alloc0 (cfg->mempool, sizeof (GCMap));
2078
2079         map->frame_reg = cfg->frame_reg;
2080         map->start_offset = gcfg->min_offset;
2081         map->end_offset = gcfg->min_offset + (nslots * sizeof (mgreg_t));
2082         map->map_offset = start * sizeof (mgreg_t);
2083         map->nslots = end - start;
2084         map->has_ref_slots = has_ref_slots;
2085         map->has_pin_slots = has_pin_slots;
2086         map->has_ref_regs = has_ref_regs;
2087         map->has_pin_regs = has_pin_regs;
2088         g_assert (nregs < 32);
2089         map->used_int_regs = cfg->used_int_regs;
2090         map->reg_ref_mask = reg_ref_mask;
2091         map->reg_pin_mask = reg_pin_mask;
2092         map->nref_regs = nref_regs;
2093         map->npin_regs = npin_regs;
2094
2095         bitmaps = mono_mempool_alloc0 (cfg->mempool, bitmaps_size);
2096
2097         bitmaps_offset = 0;
2098         if (has_ref_slots) {
2099                 map->stack_ref_bitmap_offset = bitmaps_offset;
2100                 bitmaps_offset += stack_bitmap_size;
2101
2102                 bitmap = &bitmaps [map->stack_ref_bitmap_offset];
2103                 for (i = 0; i < nslots; ++i) {
2104                         for (j = 0; j < ncallsites; ++j) {
2105                                 if (get_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, j, i))
2106                                         set_bit (bitmap, stack_bitmap_width, j, i - start);
2107                         }
2108                 }
2109         }
2110         if (has_pin_slots) {
2111                 map->stack_pin_bitmap_offset = bitmaps_offset;
2112                 bitmaps_offset += stack_bitmap_size;
2113
2114                 bitmap = &bitmaps [map->stack_pin_bitmap_offset];
2115                 for (i = 0; i < nslots; ++i) {
2116                         for (j = 0; j < ncallsites; ++j) {
2117                                 if (get_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, j, i))
2118                                         set_bit (bitmap, stack_bitmap_width, j, i - start);
2119                         }
2120                 }
2121         }
2122         if (has_ref_regs) {
2123                 map->reg_ref_bitmap_offset = bitmaps_offset;
2124                 bitmaps_offset += reg_ref_bitmap_size;
2125
2126                 bitmap = &bitmaps [map->reg_ref_bitmap_offset];
2127                 bindex = 0;
2128                 for (i = 0; i < nregs; ++i) {
2129                         if (reg_ref_mask & (1 << i)) {
2130                                 for (j = 0; j < ncallsites; ++j) {
2131                                         if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, j, i))
2132                                                 set_bit (bitmap, reg_ref_bitmap_width, j, bindex);
2133                                 }
2134                                 bindex ++;
2135                         }
2136                 }
2137         }
2138         if (has_pin_regs) {
2139                 map->reg_pin_bitmap_offset = bitmaps_offset;
2140                 bitmaps_offset += reg_pin_bitmap_size;
2141
2142                 bitmap = &bitmaps [map->reg_pin_bitmap_offset];
2143                 bindex = 0;
2144                 for (i = 0; i < nregs; ++i) {
2145                         if (reg_pin_mask & (1 << i)) {
2146                                 for (j = 0; j < ncallsites; ++j) {
2147                                         if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, j, i))
2148                                                 set_bit (bitmap, reg_pin_bitmap_width, j, bindex);
2149                                 }
2150                                 bindex ++;
2151                         }
2152                 }
2153         }
2154
2155         /* Call sites */
2156         map->ncallsites = ncallsites;
2157         if (cfg->code_len < 256)
2158                 map->callsite_entry_size = 1;
2159         else if (cfg->code_len < 65536)
2160                 map->callsite_entry_size = 2;
2161         else
2162                 map->callsite_entry_size = 4;
2163
2164         /* Encode the GC Map */
2165         {
2166                 guint8 buf [256];
2167                 guint8 *endbuf;
2168                 GCEncodedMap *emap;
2169                 int encoded_size;
2170                 guint8 *p;
2171
2172                 encode_gc_map (map, buf, &endbuf);
2173                 g_assert (endbuf - buf < 256);
2174
2175                 encoded_size = endbuf - buf;
2176                 alloc_size = sizeof (GCEncodedMap) + ALIGN_TO (encoded_size, map->callsite_entry_size) + (map->callsite_entry_size * map->ncallsites) + bitmaps_size;
2177
2178                 emap = mono_domain_alloc0 (cfg->domain, alloc_size);
2179                 //emap->ref_slots = map->ref_slots;
2180
2181                 /* Encoded fixed fields */
2182                 p = &emap->encoded [0];
2183                 //emap->encoded_size = encoded_size;
2184                 memcpy (p, buf, encoded_size);
2185                 p += encoded_size;
2186
2187                 /* Callsite table */
2188                 p = (guint8*)ALIGN_TO ((guint64)p, map->callsite_entry_size);
2189                 if (map->callsite_entry_size == 1) {
2190                         guint8 *offsets = p;
2191                         for (i = 0; i < ncallsites; ++i)
2192                                 offsets [i] = callsites [i]->pc_offset;
2193                         stats.gc_callsites8_size += ncallsites * sizeof (guint8);
2194                 } else if (map->callsite_entry_size == 2) {
2195                         guint16 *offsets = (guint16*)p;
2196                         for (i = 0; i < ncallsites; ++i)
2197                                 offsets [i] = callsites [i]->pc_offset;
2198                         stats.gc_callsites16_size += ncallsites * sizeof (guint16);
2199                 } else {
2200                         guint32 *offsets = (guint32*)p;
2201                         for (i = 0; i < ncallsites; ++i)
2202                                 offsets [i] = callsites [i]->pc_offset;
2203                         stats.gc_callsites32_size += ncallsites * sizeof (guint32);
2204                 }
2205                 p += ncallsites * map->callsite_entry_size;
2206
2207                 /* Bitmaps */
2208                 memcpy (p, bitmaps, bitmaps_size);
2209
2210                 stats.gc_maps_size += alloc_size;
2211                 stats.gc_callsites_size += ncallsites * map->callsite_entry_size;
2212                 stats.gc_bitmaps_size += bitmaps_size;
2213                 stats.gc_map_struct_size += sizeof (GCEncodedMap) + encoded_size;
2214
2215                 cfg->jit_info->gc_info = emap;
2216         }
2217
2218         stats.all_slots += nslots;
2219         stats.ref_slots += ntypes [SLOT_REF];
2220         stats.noref_slots += ntypes [SLOT_NOREF];
2221         stats.pin_slots += ntypes [SLOT_PIN];
2222 }
2223
2224 void
2225 mini_gc_create_gc_map (MonoCompile *cfg)
2226 {
2227         if (!cfg->compute_gc_maps)
2228                 return;
2229
2230         /*
2231          * During marking, all frames except the top frame are at a call site, and we mark the
2232          * top frame conservatively. This means that we only need to compute and record
2233          * GC maps for call sites.
2234          */
2235
2236         if (!(cfg->comp_done & MONO_COMP_LIVENESS))
2237                 /* Without liveness info, the live ranges are not precise enough */
2238                 return;
2239
2240         mono_analyze_liveness_gc (cfg);
2241
2242         compute_frame_size (cfg);
2243
2244         init_gcfg (cfg);
2245
2246         process_spill_slots (cfg);
2247         process_other_slots (cfg);
2248         process_param_area_slots (cfg);
2249         process_variables (cfg);
2250         process_finally_clauses (cfg);
2251
2252         create_map (cfg);
2253 }
2254
2255 static void
2256 parse_debug_options (void)
2257 {
2258         char **opts, **ptr;
2259         char *env;
2260
2261         env = getenv ("MONO_GCMAP_DEBUG");
2262         if (!env)
2263                 return;
2264
2265         opts = g_strsplit (env, ",", -1);
2266         for (ptr = opts; ptr && *ptr; ptr ++) {
2267                 /* No options yet */
2268                 fprintf (stderr, "Invalid format for the MONO_GCMAP_DEBUG env variable: '%s'\n", env);
2269                 exit (1);
2270         }
2271         g_strfreev (opts);
2272 }
2273
2274 void
2275 mini_gc_init (void)
2276 {
2277         MonoGCCallbacks cb;
2278
2279         memset (&cb, 0, sizeof (cb));
2280         cb.thread_attach_func = thread_attach_func;
2281         cb.thread_detach_func = thread_detach_func;
2282         cb.thread_suspend_func = thread_suspend_func;
2283         /* Comment this out to disable precise stack marking */
2284         cb.thread_mark_func = thread_mark_func;
2285         mono_gc_set_gc_callbacks (&cb);
2286
2287         logfile = mono_sgen_get_logfile ();
2288
2289         parse_debug_options ();
2290
2291         mono_counters_register ("GC Maps size",
2292                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_maps_size);
2293         mono_counters_register ("GC Call Sites size",
2294                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites_size);
2295         mono_counters_register ("GC Bitmaps size",
2296                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_bitmaps_size);
2297         mono_counters_register ("GC Map struct size",
2298                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_map_struct_size);
2299         mono_counters_register ("GC Call Sites encoded using 8 bits",
2300                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites8_size);
2301         mono_counters_register ("GC Call Sites encoded using 16 bits",
2302                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites16_size);
2303         mono_counters_register ("GC Call Sites encoded using 32 bits",
2304                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites32_size);
2305
2306         mono_counters_register ("GC Map slots (all)",
2307                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.all_slots);
2308         mono_counters_register ("GC Map slots (ref)",
2309                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.ref_slots);
2310         mono_counters_register ("GC Map slots (noref)",
2311                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.noref_slots);
2312         mono_counters_register ("GC Map slots (pin)",
2313                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.pin_slots);
2314
2315         mono_counters_register ("GC TLS Data size",
2316                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.tlsdata_size);
2317
2318         mono_counters_register ("Stack space scanned (all)",
2319                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_stacks);
2320         mono_counters_register ("Stack space scanned (native)",
2321                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_native);
2322         mono_counters_register ("Stack space scanned (other)",
2323                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_other);
2324         mono_counters_register ("Stack space scanned (using GC Maps)",
2325                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned);
2326         mono_counters_register ("Stack space scanned (precise)",
2327                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_precisely);
2328         mono_counters_register ("Stack space scanned (pin)",
2329                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_conservatively);
2330         mono_counters_register ("Stack space scanned (pin registers)",
2331                                                         MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_registers);
2332 }
2333
2334 #else
2335
2336 void
2337 mini_gc_init (void)
2338 {
2339 }
2340
2341 static void
2342 mini_gc_init_gc_map (MonoCompile *cfg)
2343 {
2344 }
2345
2346 void
2347 mini_gc_create_gc_map (MonoCompile *cfg)
2348 {
2349 }
2350
2351 void
2352 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
2353 {
2354 }
2355
2356 void
2357 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
2358 {
2359 }
2360
2361 #endif
2362
2363 /*
2364  * mini_gc_init_cfg:
2365  *
2366  *   Set GC specific options in CFG.
2367  */
2368 void
2369 mini_gc_init_cfg (MonoCompile *cfg)
2370 {
2371         if (mono_gc_is_moving ()) {
2372                 cfg->disable_ref_noref_stack_slot_share = TRUE;
2373                 cfg->gen_write_barriers = TRUE;
2374         }
2375
2376         mini_gc_init_gc_map (cfg);
2377 }
2378
2379 /*
2380  * Problems with the current code:
2381  * - the stack walk is slow
2382  * - vtypes/refs used in EH regions are treated conservatively
2383  * - if the code is finished, less pinning will be done, causing problems because
2384  *   we promote all surviving objects to old-gen.
2385  * - the unwind code can't handle a method stopped inside a finally region, it thinks the caller is
2386  *   another method, but in reality it is either the exception handling code or the CALL_HANDLER opcode.
2387  *   This manifests in "Unable to find ip offset x in callsite list" assertions.
2388  * - the unwind code also can't handle frames which are in the epilog, since the unwind info is not
2389  *   precise there.
2390  */
2391
2392 /*
2393  * Ideas for creating smaller GC maps:
2394  * - remove empty columns from the bitmaps. This requires adding a mask bit array for
2395  *   each bitmap.
2396  * - merge reg and stack slot bitmaps, so the unused bits at the end of the reg bitmap are
2397  *   not wasted.
2398  * - if the bitmap width is not a multiple of 8, the remaining bits are wasted.
2399  * - group ref and non-ref stack slots together in mono_allocate_stack_slots ().
2400  * - add an index for the callsite table so that each entry can be encoded as a 1 byte difference
2401  *   from an index entry.
2402  */