+/* Same as the code in seq-points.c */
+static void
+insert_pred_seq_point (SeqPoint *last_sp, SeqPoint *sp, GSList **next)
+{
+ GSList *l;
+ int src_index = last_sp->next_offset;
+ int dst_index = sp->next_offset;
+
+ /* bb->in_bb might contain duplicates */
+ for (l = next [src_index]; l; l = l->next)
+ if (GPOINTER_TO_UINT (l->data) == dst_index)
+ break;
+ if (!l)
+ next [src_index] = g_slist_append (next [src_index], GUINT_TO_POINTER (dst_index));
+}
+
+static void
+recursively_make_pred_seq_points (TransformData *td, InterpBasicBlock *bb)
+{
+ const gpointer MONO_SEQ_SEEN_LOOP = GINT_TO_POINTER(-1);
+ GSList *l;
+
+ GArray *predecessors = g_array_new (FALSE, TRUE, sizeof (gpointer));
+ GHashTable *seen = g_hash_table_new_full (g_direct_hash, NULL, NULL, NULL);
+
+ // Insert/remove sentinel into the memoize table to detect loops containing bb
+ bb->pred_seq_points = MONO_SEQ_SEEN_LOOP;
+
+ for (l = bb->preds; l; l = l->next) {
+ InterpBasicBlock *in_bb = l->data;
+
+ // This bb has the last seq point, append it and continue
+ if (in_bb->last_seq_point != NULL) {
+ predecessors = g_array_append_val (predecessors, in_bb->last_seq_point);
+ continue;
+ }
+
+ // We've looped or handled this before, exit early.
+ // No last sequence points to find.
+ if (in_bb->pred_seq_points == MONO_SEQ_SEEN_LOOP)
+ continue;
+
+ // Take sequence points from incoming basic blocks
+
+ if (in_bb == td->entry_bb)
+ continue;
+
+ if (in_bb->pred_seq_points == NULL)
+ recursively_make_pred_seq_points (td, in_bb);
+
+ // Union sequence points with incoming bb's
+ for (int i=0; i < in_bb->num_pred_seq_points; i++) {
+ if (!g_hash_table_lookup (seen, in_bb->pred_seq_points [i])) {
+ g_array_append_val (predecessors, in_bb->pred_seq_points [i]);
+ g_hash_table_insert (seen, in_bb->pred_seq_points [i], (gpointer)&MONO_SEQ_SEEN_LOOP);
+ }
+ }
+ // predecessors = g_array_append_vals (predecessors, in_bb->pred_seq_points, in_bb->num_pred_seq_points);
+ }
+
+ g_hash_table_destroy (seen);
+
+ if (predecessors->len != 0) {
+ bb->pred_seq_points = mono_mempool_alloc0 (td->mempool, sizeof (SeqPoint *) * predecessors->len);
+ bb->num_pred_seq_points = predecessors->len;
+
+ for (int newer = 0; newer < bb->num_pred_seq_points; newer++) {
+ bb->pred_seq_points [newer] = g_array_index (predecessors, gpointer, newer);
+ }
+ }
+
+ g_array_free (predecessors, TRUE);
+}
+
+static void
+collect_pred_seq_points (TransformData *td, InterpBasicBlock *bb, SeqPoint *seqp, GSList **next)
+{
+ // Doesn't have a last sequence point, must find from incoming basic blocks
+ if (bb->pred_seq_points == NULL && bb != td->entry_bb)
+ recursively_make_pred_seq_points (td, bb);
+
+ for (int i = 0; i < bb->num_pred_seq_points; i++)
+ insert_pred_seq_point (bb->pred_seq_points [i], seqp, next);
+
+ return;
+}
+
+static void
+save_seq_points (TransformData *td)
+{
+ InterpMethod *rtm = td->rtm;
+ GByteArray *array;
+ int i, seq_info_size;
+ MonoSeqPointInfo *info;
+ MonoDomain *domain = mono_domain_get ();
+ GSList **next = NULL;
+ GList *bblist;
+
+ if (!td->gen_sdb_seq_points)
+ return;
+
+ /*
+ * For each sequence point, compute the list of sequence points immediately
+ * following it, this is needed to implement 'step over' in the debugger agent.
+ * Similar to the code in mono_save_seq_point_info ().
+ */
+ for (i = 0; i < td->seq_points->len; ++i) {
+ SeqPoint *sp = g_ptr_array_index (td->seq_points, i);
+
+ /* Store the seq point index here temporarily */
+ sp->next_offset = i;
+ }
+ next = mono_mempool_alloc0 (td->mempool, sizeof (GList*) * td->seq_points->len);
+ for (bblist = td->basic_blocks; bblist; bblist = bblist->next) {
+ InterpBasicBlock *bb = bblist->data;
+
+ GSList *bb_seq_points = g_slist_reverse (bb->seq_points);
+ SeqPoint *last = NULL;
+ for (GSList *l = bb_seq_points; l; l = l->next) {
+ SeqPoint *sp = l->data;
+
+ if (sp->il_offset == METHOD_ENTRY_IL_OFFSET || sp->il_offset == METHOD_EXIT_IL_OFFSET)
+ /* Used to implement method entry/exit events */
+ continue;
+
+ if (last != NULL) {
+ /* Link with the previous seq point in the same bb */
+ next [last->next_offset] = g_slist_append_mempool (td->mempool, next [last->next_offset], GINT_TO_POINTER (sp->next_offset));
+ } else {
+ /* Link with the last bb in the previous bblocks */
+ collect_pred_seq_points (td, bb, sp, next);
+ }
+ last = sp;
+ }
+ }
+
+ /* Serialize the seq points into a byte array */
+ array = g_byte_array_new ();
+ SeqPoint zero_seq_point = {0};
+ SeqPoint* last_seq_point = &zero_seq_point;
+ for (i = 0; i < td->seq_points->len; ++i) {
+ SeqPoint *sp = (SeqPoint*)g_ptr_array_index (td->seq_points, i);
+
+ sp->next_offset = 0;
+ if (mono_seq_point_info_add_seq_point (array, sp, last_seq_point, next [i], TRUE))
+ last_seq_point = sp;
+ }
+
+ if (td->verbose_level) {
+ g_print ("\nSEQ POINT MAP FOR %s: \n", td->method->name);
+
+ for (i = 0; i < td->seq_points->len; ++i) {
+ SeqPoint *sp = (SeqPoint*)g_ptr_array_index (td->seq_points, i);
+ GSList *l;
+
+ if (!next [i])
+ continue;
+
+ g_print ("\tIL0x%x[0x%0x] ->", sp->il_offset, sp->native_offset);
+ for (l = next [i]; l; l = l->next) {
+ int next_index = GPOINTER_TO_UINT (l->data);
+ g_print (" IL0x%x", ((SeqPoint*)g_ptr_array_index (td->seq_points, next_index))->il_offset);
+ }
+ g_print ("\n");
+ }
+ }
+
+ info = mono_seq_point_info_new (array->len, TRUE, array->data, TRUE, &seq_info_size);
+ InterlockedAdd (&mono_jit_stats.allocated_seq_points_size, seq_info_size);
+
+ g_byte_array_free (array, TRUE);
+
+ mono_domain_lock (domain);
+ g_hash_table_insert (domain_jit_info (domain)->seq_points, rtm->method, info);
+ mono_domain_unlock (domain);
+}
+
+static void
+emit_seq_point (TransformData *td, int il_offset, InterpBasicBlock *cbb, gboolean nonempty_stack)
+{
+ SeqPoint *seqp;
+
+ seqp = mono_mempool_alloc0 (td->mempool, sizeof (SeqPoint));
+ seqp->il_offset = il_offset;
+ seqp->native_offset = (guint8*)td->new_ip - (guint8*)td->new_code;
+ if (nonempty_stack)
+ seqp->flags |= MONO_SEQ_POINT_FLAG_NONEMPTY_STACK;
+
+ ADD_CODE (td, MINT_SDB_SEQ_POINT);
+ g_ptr_array_add (td->seq_points, seqp);
+
+ cbb->seq_points = g_slist_prepend_mempool (td->mempool, cbb->seq_points, seqp);
+ cbb->last_seq_point = seqp;
+}
+
+#define BARRIER_IF_VOLATILE(td) \
+ do { \
+ if (volatile_) { \
+ ADD_CODE (td, MINT_MONO_MEMORY_BARRIER); \
+ volatile_ = FALSE; \
+ } \
+ } while (0)
+