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 seq_point_read (SeqPoint* seq_point, guint8* ptr)
69 ptr += decode_var_int (ptr, &value);
70 seq_point->il_offset += decode_zig_zag (value);
72 ptr += decode_var_int (ptr, &value);
73 seq_point->native_offset += decode_zig_zag (value);
75 ptr += decode_var_int (ptr, &value);
76 seq_point->flags = value;
78 ptr += decode_var_int (ptr, &value);
79 seq_point->next_len = value;
81 if (seq_point->next_len) {
82 ptr += decode_var_int (ptr, &value);
83 seq_point->next_offset = value;
89 static MonoSeqPointInfo*
90 seq_point_info_new (gboolean alloc_arrays)
92 MonoSeqPointInfo* info = g_new0 (MonoSeqPointInfo, 1);
93 info->alloc_arrays = alloc_arrays;
95 info->array = g_byte_array_new ();
96 info->next_array = g_byte_array_new ();
98 info->array = g_new0 (GByteArray, 1);
99 info->next_array = g_new0 (GByteArray, 1);
105 seq_point_info_free (gpointer ptr)
107 MonoSeqPointInfo* info = (MonoSeqPointInfo*) ptr;
109 if (info->alloc_arrays) {
110 g_byte_array_free (info->array, TRUE);
111 g_byte_array_free (info->next_array, TRUE);
113 g_free (info->array);
114 g_free (info->next_array);
120 seq_point_info_add_seq_point (MonoSeqPointInfo *info, SeqPoint *sp, SeqPoint *last_seq_point, GSList *next)
122 int il_delta, native_delta;
127 /* check that data can be added to the arrays */
128 g_assert (info->alloc_arrays);
130 // TODO use flag instead of encoding 4 bytes for METHOD_EXIT_IL_OFFSET
132 sp->next_offset = info->next_array->len;
133 sp->next_len = g_slist_length (next);
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 (info->array, buffer, len);
141 len = encode_var_int (buffer, encode_zig_zag (native_delta));
142 g_byte_array_append (info->array, buffer, len);
144 len = encode_var_int (buffer, sp->flags);
145 g_byte_array_append (info->array, buffer, len);
147 len = encode_var_int (buffer, sp->next_len);
148 g_byte_array_append (info->array, buffer, len);
151 len = encode_var_int (buffer, sp->next_offset);
152 g_byte_array_append (info->array, buffer, len);
155 for (l = next; l; l = l->next) {
156 int next_index = GPOINTER_TO_UINT (l->data);
158 int len = encode_var_int (buffer, next_index);
159 g_byte_array_append (info->next_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 if (!cfg->seq_points)
207 seq_points = g_new0 (SeqPoint, cfg->seq_points->len);
209 for (i = 0; i < cfg->seq_points->len; ++i) {
210 SeqPoint *sp = &seq_points [i];
211 MonoInst *ins = g_ptr_array_index (cfg->seq_points, i);
213 sp->il_offset = ins->inst_imm;
214 sp->native_offset = ins->inst_offset;
215 if (ins->flags & MONO_INST_NONEMPTY_STACK)
216 sp->flags |= MONO_SEQ_POINT_FLAG_NONEMPTY_STACK;
219 ins->backend.size = i;
223 * For each sequence point, compute the list of sequence points immediately
224 * following it, this is needed to implement 'step over' in the debugger agent.
226 next = g_new0 (GSList*, cfg->seq_points->len);
227 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
228 bb_seq_points = g_slist_reverse (bb->seq_points);
230 for (l = bb_seq_points; l; l = l->next) {
231 MonoInst *ins = l->data;
233 if (ins->inst_imm == METHOD_ENTRY_IL_OFFSET || ins->inst_imm == METHOD_EXIT_IL_OFFSET)
234 /* Used to implement method entry/exit events */
236 if (ins->inst_offset == SEQ_POINT_NATIVE_OFFSET_DEAD_CODE)
240 /* Link with the previous seq point in the same bb */
241 next [last->backend.size] = g_slist_append (next [last->backend.size], GUINT_TO_POINTER (ins->backend.size));
243 /* Link with the last bb in the previous bblocks */
244 collect_pred_seq_points (bb, ins, next, 0);
250 if (bb->last_ins && bb->last_ins->opcode == OP_ENDFINALLY && bb->seq_points) {
252 MonoInst *endfinally_seq_point = NULL;
255 * The ENDFINALLY branches are not represented in the cfg, so link it with all seq points starting bbs.
257 l = g_slist_last (bb->seq_points);
259 endfinally_seq_point = l->data;
261 for (bb2 = cfg->bb_entry; bb2; bb2 = bb2->next_bb) {
262 GSList *l = g_slist_last (bb2->seq_points);
265 MonoInst *ins = l->data;
267 if (!(ins->inst_imm == METHOD_ENTRY_IL_OFFSET || ins->inst_imm == METHOD_EXIT_IL_OFFSET) && ins != endfinally_seq_point)
268 next [endfinally_seq_point->backend.size] = g_slist_append (next [endfinally_seq_point->backend.size], GUINT_TO_POINTER (ins->backend.size));
275 if (cfg->verbose_level > 2) {
276 printf ("\nSEQ POINT MAP: \n");
278 for (i = 0; i < cfg->seq_points->len; ++i) {
279 SeqPoint *sp = &seq_points [i];
285 printf ("\tIL0x%x ->", sp->il_offset);
286 for (l = next [i]; l; l = l->next) {
287 int next_index = GPOINTER_TO_UINT (l->data);
288 printf (" IL0x%x", seq_points [next_index].il_offset);
294 cfg->seq_point_info = seq_point_info_new (TRUE);
296 { /* Add sequence points to seq_point_info */
297 SeqPoint zero_seq_point = {0};
298 SeqPoint* last_seq_point = &zero_seq_point;
300 for (i = 0; i < cfg->seq_points->len; ++i) {
301 SeqPoint *sp = &seq_points [i];
303 if (seq_point_info_add_seq_point (cfg->seq_point_info, sp, last_seq_point, next[i]))
306 g_slist_free (next [i]);
312 // FIXME: dynamic methods
313 if (!cfg->compile_aot) {
314 mono_domain_lock (domain);
315 // FIXME: How can the lookup succeed ?
316 if (!g_hash_table_lookup (domain_jit_info (domain)->seq_points, cfg->method_to_register))
317 g_hash_table_insert (domain_jit_info (domain)->seq_points, cfg->method_to_register, cfg->seq_point_info);
318 mono_domain_unlock (domain);
321 g_ptr_array_free (cfg->seq_points, TRUE);
322 cfg->seq_points = NULL;
326 get_seq_points (MonoDomain *domain, MonoMethod *method)
328 MonoSeqPointInfo *seq_points;
330 mono_domain_lock (domain);
331 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, method);
332 if (!seq_points && method->is_inflated) {
333 /* generic sharing + aot */
334 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, mono_method_get_declaring_generic_method (method));
336 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, mini_get_shared_method (method));
338 mono_domain_unlock (domain);
344 seq_point_find_next_by_native_offset (MonoSeqPointInfo* info, int native_offset, SeqPoint* seq_point)
347 seq_point_iterator_init (&it, info);
348 while (seq_point_iterator_next (&it)) {
349 if (it.seq_point.native_offset >= native_offset) {
350 memcpy (seq_point, &it.seq_point, sizeof (SeqPoint));
359 seq_point_find_prev_by_native_offset (MonoSeqPointInfo* info, int native_offset, SeqPoint* seq_point)
361 SeqPoint prev_seq_point;
362 gboolean is_first = TRUE;
364 seq_point_iterator_init (&it, info);
365 while (seq_point_iterator_next (&it) && it.seq_point.native_offset <= native_offset) {
366 memcpy (&prev_seq_point, &it.seq_point, sizeof (SeqPoint));
370 if (!is_first && prev_seq_point.native_offset <= native_offset) {
371 memcpy (seq_point, &prev_seq_point, sizeof (SeqPoint));
379 seq_point_find_by_il_offset (MonoSeqPointInfo* info, int il_offset, SeqPoint* seq_point)
382 seq_point_iterator_init (&it, info);
383 while (seq_point_iterator_next (&it)) {
384 if (it.seq_point.il_offset == il_offset) {
385 memcpy (seq_point, &it.seq_point, sizeof (SeqPoint));
394 * find_next_seq_point_for_native_offset:
396 * Find the first sequence point after NATIVE_OFFSET.
399 find_next_seq_point_for_native_offset (MonoDomain *domain, MonoMethod *method, gint32 native_offset, MonoSeqPointInfo **info, SeqPoint* seq_point)
401 MonoSeqPointInfo *seq_points;
403 seq_points = get_seq_points (domain, method);
412 return seq_point_find_next_by_native_offset (seq_points, native_offset, seq_point);
416 * find_prev_seq_point_for_native_offset:
418 * Find the first sequence point before NATIVE_OFFSET.
421 find_prev_seq_point_for_native_offset (MonoDomain *domain, MonoMethod *method, gint32 native_offset, MonoSeqPointInfo **info, SeqPoint* seq_point)
423 MonoSeqPointInfo *seq_points;
425 seq_points = get_seq_points (domain, method);
434 return seq_point_find_prev_by_native_offset (seq_points, native_offset, seq_point);
440 * Find the sequence point corresponding to the IL offset IL_OFFSET, which
441 * should be the location of a sequence point.
444 find_seq_point (MonoDomain *domain, MonoMethod *method, gint32 il_offset, MonoSeqPointInfo **info, SeqPoint *seq_point)
446 MonoSeqPointInfo *seq_points;
448 seq_points = get_seq_points (domain, method);
457 return seq_point_find_by_il_offset (seq_points, il_offset, seq_point);
461 seq_point_init_next (MonoSeqPointInfo* info, SeqPoint sp, SeqPoint* next)
466 GArray* seq_points = g_array_new (FALSE, TRUE, sizeof (SeqPoint));
468 seq_point_iterator_init (&it, info);
469 while (seq_point_iterator_next (&it))
470 g_array_append_vals (seq_points, &it.seq_point, 1);
472 ptr = info->next_array->data + sp.next_offset;
473 for (i = 0; i < sp.next_len; i++) {
475 ptr += decode_var_int (ptr, &next_index);
476 g_assert (next_index < seq_points->len);
477 memcpy (&next[i], seq_points->data + next_index * sizeof (SeqPoint), sizeof (SeqPoint));
482 seq_point_iterator_next (SeqPointIterator* it)
484 if (it->ptr >= it->info->array->data + it->info->array->len)
487 it->ptr += seq_point_read (&it->seq_point, it->ptr);
493 seq_point_iterator_init (SeqPointIterator* it, MonoSeqPointInfo* info)
496 it->ptr = it->info->array->data;
497 memset(&it->seq_point, 0, sizeof(SeqPoint));
501 seq_point_info_write (MonoSeqPointInfo* info, guint8* buffer)
503 guint8* buffer0 = buffer;
505 //Write sequence points
506 buffer += encode_var_int (buffer, info->array->len);
507 memcpy (buffer, info->array->data, info->array->len);
508 buffer += info->array->len;
511 buffer += encode_var_int (buffer, info->next_array->len);
512 memcpy (buffer, info->next_array->data, info->next_array->len);
513 buffer += info->next_array->len;
515 return buffer - buffer0;
519 seq_point_info_read (MonoSeqPointInfo** info, guint8* buffer, gboolean copy)
521 guint8* buffer0 = buffer;
524 (*info) = seq_point_info_new (copy);
526 buffer += decode_var_int (buffer, &size);
528 g_byte_array_append ((*info)->array, buffer, size);
530 (*info)->array->data = buffer;
531 (*info)->array->len = size;
535 buffer += decode_var_int (buffer, &size);
537 g_byte_array_append ((*info)->array, buffer, size);
539 (*info)->array->data = buffer;
540 (*info)->array->len = size;
544 return buffer - buffer0;
548 * Returns the maximum size of mono_seq_point_info_write.
551 seq_point_info_write_size (MonoSeqPointInfo* info)
553 //8 is the maximum size required to store the size of the arrays.
554 return 8 + info->array->len + info->next_array->len;