2 * seq-points.c: Sequence Points functions
5 * Marcos Henrich (marcos.henrich@xamarin.com)
7 * Copyright 2014 Xamarin, Inc (http://www.xamarin.com)
11 #include "seq-points.h"
14 encode_var_int (guint8* buf, int val)
19 guint8 byte = val & 0x7f;
20 g_assert (size < 4 && "value has more than 28 bits");
31 decode_var_int (guint8* buf, int* val)
37 b = *(p++); low = (b & 0x7f) ; if(!(b & 0x80)) goto done;
38 b = *(p++); low |= (b & 0x7f) << 7; if(!(b & 0x80)) goto done;
39 b = *(p++); low |= (b & 0x7f) << 14; if(!(b & 0x80)) goto done;
40 b = *(p++); low |= (b & 0x7f) << 21; if(!(b & 0x80)) goto done;
42 g_assert (FALSE && "value has more than 28 bits");
51 encode_zig_zag (int val)
53 return (val << 1) ^ (val >> 31);
57 decode_zig_zag (guint32 val)
60 return (n >> 1) ^ (-(n & 1));
64 allocated_seq_point_size_add (MonoSeqPointInfo *info)
66 int size = sizeof (MonoSeqPointInfo) + info->len;
68 mono_jit_stats.allocated_seq_points_size += size;
72 seq_point_read (SeqPoint* seq_point, guint8* ptr, guint8* buffer_ptr, gboolean has_debug_data)
77 ptr += decode_var_int (ptr, &value);
78 seq_point->il_offset += decode_zig_zag (value);
80 ptr += decode_var_int (ptr, &value);
81 seq_point->native_offset += decode_zig_zag (value);
84 ptr += decode_var_int (ptr, &value);
85 seq_point->flags = value;
87 ptr += decode_var_int (ptr, &value);
88 seq_point->next_len = value;
90 if (seq_point->next_len) {
91 // store next offset and skip it
92 seq_point->next_offset = ptr - buffer_ptr;
93 ptr += seq_point->next_len;
100 static MonoSeqPointInfo*
101 seq_point_info_new (int len, gboolean alloc_data, gboolean has_debug_data)
103 MonoSeqPointInfo* info = g_new0 (MonoSeqPointInfo, 1);
104 info->has_debug_data = has_debug_data;
105 info->alloc_data = alloc_data;
108 if (info->alloc_data)
109 info->data = g_new0 (guint8, len);
115 seq_point_info_free (gpointer ptr)
117 MonoSeqPointInfo* info = (MonoSeqPointInfo*) ptr;
118 if (info->alloc_data)
124 seq_point_info_add_seq_point (GByteArray* array, SeqPoint *sp, SeqPoint *last_seq_point, GSList *next, gboolean has_debug_data)
126 int il_delta, native_delta;
131 if (!has_debug_data &&
132 (sp->il_offset == METHOD_ENTRY_IL_OFFSET || sp->il_offset == METHOD_EXIT_IL_OFFSET))
135 il_delta = sp->il_offset - last_seq_point->il_offset;
136 native_delta = sp->native_offset - last_seq_point->native_offset;
138 len = encode_var_int (buffer, encode_zig_zag (il_delta));
139 g_byte_array_append (array, buffer, len);
141 len = encode_var_int (buffer, encode_zig_zag (native_delta));
142 g_byte_array_append (array, buffer, len);
144 if (has_debug_data) {
145 sp->next_offset = array->len;
146 sp->next_len = g_slist_length (next);
148 len = encode_var_int (buffer, sp->flags);
149 g_byte_array_append (array, buffer, len);
151 len = encode_var_int (buffer, sp->next_len);
152 g_byte_array_append (array, buffer, len);
154 for (l = next; l; l = l->next) {
155 int next_index = GPOINTER_TO_UINT (l->data);
157 int len = encode_var_int (buffer, next_index);
158 g_byte_array_append (array, buffer, len);
166 collect_pred_seq_points (MonoBasicBlock *bb, MonoInst *ins, GSList **next, int depth)
169 MonoBasicBlock *in_bb;
172 for (i = 0; i < bb->in_count; ++i) {
173 in_bb = bb->in_bb [i];
175 if (in_bb->last_seq_point) {
176 int src_index = in_bb->last_seq_point->backend.size;
177 int dst_index = ins->backend.size;
179 /* bb->in_bb might contain duplicates */
180 for (l = next [src_index]; l; l = l->next)
181 if (GPOINTER_TO_UINT (l->data) == dst_index)
184 next [src_index] = g_slist_append (next [src_index], GUINT_TO_POINTER (dst_index));
186 /* Have to look at its predecessors */
188 collect_pred_seq_points (in_bb, ins, next, depth + 1);
194 mono_save_seq_point_info (MonoCompile *cfg)
197 GSList *bb_seq_points, *l;
199 MonoDomain *domain = cfg->domain;
202 SeqPoint* seq_points;
204 gboolean has_debug_data = cfg->gen_seq_points_debug_data;
206 if (!cfg->seq_points)
209 seq_points = g_new0 (SeqPoint, cfg->seq_points->len);
211 for (i = 0; i < cfg->seq_points->len; ++i) {
212 SeqPoint *sp = &seq_points [i];
213 MonoInst *ins = g_ptr_array_index (cfg->seq_points, i);
215 sp->il_offset = ins->inst_imm;
216 sp->native_offset = ins->inst_offset;
217 if (ins->flags & MONO_INST_NONEMPTY_STACK)
218 sp->flags |= MONO_SEQ_POINT_FLAG_NONEMPTY_STACK;
221 ins->backend.size = i;
224 if (has_debug_data) {
226 * For each sequence point, compute the list of sequence points immediately
227 * following it, this is needed to implement 'step over' in the debugger agent.
229 next = g_new0 (GSList*, cfg->seq_points->len);
230 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
231 bb_seq_points = g_slist_reverse (bb->seq_points);
233 for (l = bb_seq_points; l; l = l->next) {
234 MonoInst *ins = l->data;
236 if (ins->inst_imm == METHOD_ENTRY_IL_OFFSET || ins->inst_imm == METHOD_EXIT_IL_OFFSET)
237 /* Used to implement method entry/exit events */
239 if (ins->inst_offset == SEQ_POINT_NATIVE_OFFSET_DEAD_CODE)
243 /* Link with the previous seq point in the same bb */
244 next [last->backend.size] = g_slist_append (next [last->backend.size], GUINT_TO_POINTER (ins->backend.size));
246 /* Link with the last bb in the previous bblocks */
247 collect_pred_seq_points (bb, ins, next, 0);
253 if (bb->last_ins && bb->last_ins->opcode == OP_ENDFINALLY && bb->seq_points) {
255 MonoInst *endfinally_seq_point = NULL;
258 * The ENDFINALLY branches are not represented in the cfg, so link it with all seq points starting bbs.
260 l = g_slist_last (bb->seq_points);
262 endfinally_seq_point = l->data;
264 for (bb2 = cfg->bb_entry; bb2; bb2 = bb2->next_bb) {
265 GSList *l = g_slist_last (bb2->seq_points);
268 MonoInst *ins = l->data;
270 if (!(ins->inst_imm == METHOD_ENTRY_IL_OFFSET || ins->inst_imm == METHOD_EXIT_IL_OFFSET) && ins != endfinally_seq_point)
271 next [endfinally_seq_point->backend.size] = g_slist_append (next [endfinally_seq_point->backend.size], GUINT_TO_POINTER (ins->backend.size));
278 if (cfg->verbose_level > 2) {
279 printf ("\nSEQ POINT MAP: \n");
281 for (i = 0; i < cfg->seq_points->len; ++i) {
282 SeqPoint *sp = &seq_points [i];
288 printf ("\tIL0x%x ->", sp->il_offset);
289 for (l = next [i]; l; l = l->next) {
290 int next_index = GPOINTER_TO_UINT (l->data);
291 printf (" IL0x%x", seq_points [next_index].il_offset);
298 array = g_byte_array_new ();
300 { /* Add sequence points to seq_point_info */
301 SeqPoint zero_seq_point = {0};
302 SeqPoint* last_seq_point = &zero_seq_point;
304 for (i = 0; i < cfg->seq_points->len; ++i) {
305 SeqPoint *sp = &seq_points [i];
306 GSList* next_list = NULL;
311 if (seq_point_info_add_seq_point (array, sp, last_seq_point, next_list, has_debug_data))
315 g_slist_free (next [i]);
322 cfg->seq_point_info = seq_point_info_new (array->len, TRUE, has_debug_data);
323 memcpy (cfg->seq_point_info->data, array->data, array->len);
324 g_byte_array_free (array, TRUE);
326 allocated_seq_point_size_add (cfg->seq_point_info);
328 // FIXME: dynamic methods
329 if (!cfg->compile_aot) {
330 mono_domain_lock (domain);
331 // FIXME: How can the lookup succeed ?
332 if (!g_hash_table_lookup (domain_jit_info (domain)->seq_points, cfg->method_to_register))
333 g_hash_table_insert (domain_jit_info (domain)->seq_points, cfg->method_to_register, cfg->seq_point_info);
334 mono_domain_unlock (domain);
337 g_ptr_array_free (cfg->seq_points, TRUE);
338 cfg->seq_points = NULL;
342 get_seq_points (MonoDomain *domain, MonoMethod *method)
344 MonoSeqPointInfo *seq_points;
346 mono_domain_lock (domain);
347 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, method);
348 if (!seq_points && method->is_inflated) {
349 /* generic sharing + aot */
350 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, mono_method_get_declaring_generic_method (method));
352 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, mini_get_shared_method (method));
354 mono_domain_unlock (domain);
360 seq_point_find_next_by_native_offset (MonoSeqPointInfo* info, int native_offset, SeqPoint* seq_point)
363 seq_point_iterator_init (&it, info);
364 while (seq_point_iterator_next (&it)) {
365 if (it.seq_point.native_offset >= native_offset) {
366 memcpy (seq_point, &it.seq_point, sizeof (SeqPoint));
375 seq_point_find_prev_by_native_offset (MonoSeqPointInfo* info, int native_offset, SeqPoint* seq_point)
377 SeqPoint prev_seq_point;
378 gboolean is_first = TRUE;
380 seq_point_iterator_init (&it, info);
381 while (seq_point_iterator_next (&it) && it.seq_point.native_offset <= native_offset) {
382 memcpy (&prev_seq_point, &it.seq_point, sizeof (SeqPoint));
386 if (!is_first && prev_seq_point.native_offset <= native_offset) {
387 memcpy (seq_point, &prev_seq_point, sizeof (SeqPoint));
395 seq_point_find_by_il_offset (MonoSeqPointInfo* info, int il_offset, SeqPoint* seq_point)
398 seq_point_iterator_init (&it, info);
399 while (seq_point_iterator_next (&it)) {
400 if (it.seq_point.il_offset == il_offset) {
401 memcpy (seq_point, &it.seq_point, sizeof (SeqPoint));
410 * find_next_seq_point_for_native_offset:
412 * Find the first sequence point after NATIVE_OFFSET.
415 find_next_seq_point_for_native_offset (MonoDomain *domain, MonoMethod *method, gint32 native_offset, MonoSeqPointInfo **info, SeqPoint* seq_point)
417 MonoSeqPointInfo *seq_points;
419 seq_points = get_seq_points (domain, method);
428 return seq_point_find_next_by_native_offset (seq_points, native_offset, seq_point);
432 * find_prev_seq_point_for_native_offset:
434 * Find the first sequence point before NATIVE_OFFSET.
437 find_prev_seq_point_for_native_offset (MonoDomain *domain, MonoMethod *method, gint32 native_offset, MonoSeqPointInfo **info, SeqPoint* seq_point)
439 MonoSeqPointInfo *seq_points;
441 seq_points = get_seq_points (domain, method);
450 return seq_point_find_prev_by_native_offset (seq_points, native_offset, seq_point);
456 * Find the sequence point corresponding to the IL offset IL_OFFSET, which
457 * should be the location of a sequence point.
460 find_seq_point (MonoDomain *domain, MonoMethod *method, gint32 il_offset, MonoSeqPointInfo **info, SeqPoint *seq_point)
462 MonoSeqPointInfo *seq_points;
464 seq_points = get_seq_points (domain, method);
473 return seq_point_find_by_il_offset (seq_points, il_offset, seq_point);
477 seq_point_init_next (MonoSeqPointInfo* info, SeqPoint sp, SeqPoint* next)
482 GArray* seq_points = g_array_new (FALSE, TRUE, sizeof (SeqPoint));
484 g_assert (info->has_debug_data);
486 seq_point_iterator_init (&it, info);
487 while (seq_point_iterator_next (&it))
488 g_array_append_vals (seq_points, &it.seq_point, 1);
490 ptr = info->data + sp.next_offset;
491 for (i = 0; i < sp.next_len; i++) {
493 ptr += decode_var_int (ptr, &next_index);
494 g_assert (next_index < seq_points->len);
495 memcpy (&next[i], seq_points->data + next_index * sizeof (SeqPoint), sizeof (SeqPoint));
498 g_array_free (seq_points, TRUE);
502 seq_point_iterator_next (SeqPointIterator* it)
504 if (it->ptr >= it->info->data + it->info->len)
507 it->ptr += seq_point_read (&it->seq_point, it->ptr, it->info->data, it->info->has_debug_data);
513 seq_point_iterator_init (SeqPointIterator* it, MonoSeqPointInfo* info)
516 it->ptr = it->info->data;
517 memset(&it->seq_point, 0, sizeof(SeqPoint));
521 seq_point_info_write (MonoSeqPointInfo* info, guint8* buffer)
523 guint8* buffer0 = buffer;
525 memcpy (buffer, &info->has_debug_data, 1);
528 //Write sequence points
529 buffer += encode_var_int (buffer, info->len);
530 memcpy (buffer, info->data, info->len);
533 return buffer - buffer0;
537 seq_point_info_read (MonoSeqPointInfo** info, guint8* buffer, gboolean copy)
539 guint8* buffer0 = buffer;
541 gboolean has_debug_data;
543 memcpy (&has_debug_data, buffer, 1);
546 buffer += decode_var_int (buffer, &size);
547 (*info) = seq_point_info_new (size, copy, has_debug_data);
549 memcpy ((*info)->data, buffer, size);
551 (*info)->data = buffer;
554 return buffer - buffer0;
558 * Returns the maximum size of mono_seq_point_info_write.
561 seq_point_info_write_size (MonoSeqPointInfo* info)
563 //4 is the maximum size required to store the size of the data.
564 //1 is the byte used to store has_debug_data.
565 int size = 4 + 1 + info->len;