+static MonoClassField *
+interp_field_from_token (MonoMethod *method, guint32 token, MonoClass **klass, MonoGenericContext *generic_context)
+{
+ MonoClassField *field = NULL;
+ if (method->wrapper_type != MONO_WRAPPER_NONE) {
+ field = (MonoClassField *) mono_method_get_wrapper_data (method, token);
+ *klass = field->parent;
+ } else {
+ MonoError error;
+ error_init (&error);
+ field = mono_field_from_token_checked (method->klass->image, token, klass, generic_context, &error);
+ mono_error_cleanup (&error); /* FIXME: don't swallow the error */
+ }
+ return field;
+}
+
+static InterpBasicBlock*
+get_bb (TransformData *td, InterpBasicBlock *cbb, unsigned char *ip)
+{
+ int offset = ip - td->il_code;
+ InterpBasicBlock *bb = td->offset_to_bb [offset];
+
+ if (!bb) {
+ bb = mono_mempool_alloc0 (td->mempool, sizeof (InterpBasicBlock));
+ bb->ip = ip;
+ td->offset_to_bb [offset] = bb;
+
+ td->basic_blocks = g_list_append_mempool (td->mempool, td->basic_blocks, bb);
+ }
+
+ if (cbb)
+ bb->preds = g_slist_prepend_mempool (td->mempool, bb->preds, cbb);
+ return bb;
+}
+
+/*
+ * get_basic_blocks:
+ *
+ * Compute the set of IL level basic blocks.
+ */
+static void
+get_basic_blocks (TransformData *td)
+{
+ guint8 *start = (guint8*)td->il_code;
+ guint8 *end = (guint8*)td->il_code + td->code_size;
+ guint8 *ip = start;
+ unsigned char *target;
+ int i;
+ guint cli_addr;
+ const MonoOpcode *opcode;
+ InterpBasicBlock *cbb;
+
+ td->offset_to_bb = mono_mempool_alloc0 (td->mempool, sizeof (InterpBasicBlock*) * (end - start + 1));
+ td->entry_bb = cbb = get_bb (td, NULL, start);
+
+ while (ip < end) {
+ cli_addr = ip - start;
+ td->offset_to_bb [cli_addr] = cbb;
+ i = mono_opcode_value ((const guint8 **)&ip, end);
+ opcode = &mono_opcodes [i];
+ switch (opcode->argument) {
+ case MonoInlineNone:
+ ip++;
+ break;
+ case MonoInlineString:
+ case MonoInlineType:
+ case MonoInlineField:
+ case MonoInlineMethod:
+ case MonoInlineTok:
+ case MonoInlineSig:
+ case MonoShortInlineR:
+ case MonoInlineI:
+ ip += 5;
+ break;
+ case MonoInlineVar:
+ ip += 3;
+ break;
+ case MonoShortInlineVar:
+ case MonoShortInlineI:
+ ip += 2;
+ break;
+ case MonoShortInlineBrTarget:
+ target = start + cli_addr + 2 + (signed char)ip [1];
+ get_bb (td, cbb, target);
+ ip += 2;
+ cbb = get_bb (td, cbb, ip);
+ break;
+ case MonoInlineBrTarget:
+ target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
+ get_bb (td, cbb, target);
+ ip += 5;
+ cbb = get_bb (td, cbb, ip);
+ break;
+ case MonoInlineSwitch: {
+ guint32 n = read32 (ip + 1);
+ guint32 j;
+ ip += 5;
+ cli_addr += 5 + 4 * n;
+ target = start + cli_addr;
+ get_bb (td, cbb, target);
+
+ for (j = 0; j < n; ++j) {
+ target = start + cli_addr + (gint32)read32 (ip);
+ get_bb (td, cbb, target);
+ ip += 4;
+ }
+ cbb = get_bb (td, cbb, ip);
+ break;
+ }
+ case MonoInlineR:
+ case MonoInlineI8:
+ ip += 9;
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+ }
+}
+
+static void
+interp_save_debug_info (InterpMethod *rtm, MonoMethodHeader *header, TransformData *td, GArray *line_numbers)
+{
+ MonoDebugMethodJitInfo *dinfo;
+ int i;
+
+ if (!mono_debug_enabled ())
+ return;
+
+ /*
+ * We save the debug info in the same way the JIT does it, treating the interpreter IR as the native code.
+ */
+
+ dinfo = g_new0 (MonoDebugMethodJitInfo, 1);
+ dinfo->num_params = rtm->param_count;
+ dinfo->params = g_new0 (MonoDebugVarInfo, dinfo->num_params);
+ dinfo->num_locals = header->num_locals;
+ dinfo->locals = g_new0 (MonoDebugVarInfo, header->num_locals);
+ dinfo->code_start = (guint8*)rtm->code;
+ dinfo->code_size = td->new_ip - td->new_code;
+ dinfo->epilogue_begin = 0;
+ dinfo->has_var_info = TRUE;
+ dinfo->num_line_numbers = line_numbers->len;
+ dinfo->line_numbers = g_new0 (MonoDebugLineNumberEntry, dinfo->num_line_numbers);
+
+ for (i = 0; i < dinfo->num_params; i++) {
+ MonoDebugVarInfo *var = &dinfo->params [i];
+ var->type = rtm->param_types [i];
+ }
+ for (i = 0; i < dinfo->num_locals; i++) {
+ MonoDebugVarInfo *var = &dinfo->locals [i];
+ var->type = header->locals [i];
+ }
+
+ for (i = 0; i < dinfo->num_line_numbers; i++)
+ dinfo->line_numbers [i] = g_array_index (line_numbers, MonoDebugLineNumberEntry, i);
+ mono_debug_add_method (rtm->method, dinfo, mono_domain_get ());
+
+ mono_debug_free_method_jit_info (dinfo);
+}
+
+/* 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);
+}
+