+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);
+}
+
+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);
+}
+