[MSBuild] Fix minor assembly resolution issue
[mono.git] / mono / mini / seq-points.c
1 /*
2  * seq-points.c: Sequence Points functions
3  *
4  * Authors:
5  *   Marcos Henrich (marcos.henrich@xamarin.com)
6  *
7  * Copyright 2014 Xamarin, Inc (http://www.xamarin.com)
8  */
9
10 #include "mini.h"
11 #include "seq-points.h"
12
13 static void
14 collect_pred_seq_points (MonoBasicBlock *bb, MonoInst *ins, GSList **next, int depth)
15 {
16         int i;
17         MonoBasicBlock *in_bb;
18         GSList *l;
19
20         for (i = 0; i < bb->in_count; ++i) {
21                 in_bb = bb->in_bb [i];
22
23                 if (in_bb->last_seq_point) {
24                         int src_index = in_bb->last_seq_point->backend.size;
25                         int dst_index = ins->backend.size;
26
27                         /* bb->in_bb might contain duplicates */
28                         for (l = next [src_index]; l; l = l->next)
29                                 if (GPOINTER_TO_UINT (l->data) == dst_index)
30                                         break;
31                         if (!l)
32                                 next [src_index] = g_slist_append (next [src_index], GUINT_TO_POINTER (dst_index));
33                 } else {
34                         /* Have to look at its predecessors */
35                         if (depth < 5)
36                                 collect_pred_seq_points (in_bb, ins, next, depth + 1);
37                 }
38         }
39 }
40
41 void
42 mono_save_seq_point_info (MonoCompile *cfg)
43 {
44         MonoBasicBlock *bb;
45         GSList *bb_seq_points, *l;
46         MonoInst *last;
47         MonoDomain *domain = cfg->domain;
48         int i, seq_info_size;
49         GSList **next = NULL;
50         SeqPoint* seq_points;
51         GByteArray* array;
52         gboolean has_debug_data = cfg->gen_sdb_seq_points;
53
54         if (!cfg->seq_points)
55                 return;
56
57         seq_points = g_new0 (SeqPoint, cfg->seq_points->len);
58
59         for (i = 0; i < cfg->seq_points->len; ++i) {
60                 SeqPoint *sp = &seq_points [i];
61                 MonoInst *ins = g_ptr_array_index (cfg->seq_points, i);
62
63                 sp->il_offset = ins->inst_imm;
64                 sp->native_offset = ins->inst_offset;
65                 if (ins->flags & MONO_INST_NONEMPTY_STACK)
66                         sp->flags |= MONO_SEQ_POINT_FLAG_NONEMPTY_STACK;
67
68                 /* Used below */
69                 ins->backend.size = i;
70         }
71
72         if (has_debug_data) {
73                 /*
74                  * For each sequence point, compute the list of sequence points immediately
75                  * following it, this is needed to implement 'step over' in the debugger agent.
76                  */
77                 next = g_new0 (GSList*, cfg->seq_points->len);
78                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
79                         bb_seq_points = g_slist_reverse (bb->seq_points);
80                         last = NULL;
81                         for (l = bb_seq_points; l; l = l->next) {
82                                 MonoInst *ins = l->data;
83
84                                 if (ins->inst_imm == METHOD_ENTRY_IL_OFFSET || ins->inst_imm == METHOD_EXIT_IL_OFFSET)
85                                 /* Used to implement method entry/exit events */
86                                         continue;
87                                 if (ins->inst_offset == SEQ_POINT_NATIVE_OFFSET_DEAD_CODE)
88                                         continue;
89
90                                 if (last != NULL) {
91                                         /* Link with the previous seq point in the same bb */
92                                         next [last->backend.size] = g_slist_append (next [last->backend.size], GUINT_TO_POINTER (ins->backend.size));
93                                 } else {
94                                         /* Link with the last bb in the previous bblocks */
95                                         collect_pred_seq_points (bb, ins, next, 0);
96                                 }
97
98                                 last = ins;
99                         }
100
101                         if (bb->last_ins && bb->last_ins->opcode == OP_ENDFINALLY && bb->seq_points) {
102                                 MonoBasicBlock *bb2;
103                                 MonoInst *endfinally_seq_point = NULL;
104
105                                 /*
106                                  * The ENDFINALLY branches are not represented in the cfg, so link it with all seq points starting bbs.
107                                  */
108                                 l = g_slist_last (bb->seq_points);
109                                 if (l) {
110                                         endfinally_seq_point = l->data;
111
112                                         for (bb2 = cfg->bb_entry; bb2; bb2 = bb2->next_bb) {
113                                                 GSList *l = g_slist_last (bb2->seq_points);
114
115                                                 if (l) {
116                                                         MonoInst *ins = l->data;
117
118                                                         if (!(ins->inst_imm == METHOD_ENTRY_IL_OFFSET || ins->inst_imm == METHOD_EXIT_IL_OFFSET) && ins != endfinally_seq_point)
119                                                                 next [endfinally_seq_point->backend.size] = g_slist_append (next [endfinally_seq_point->backend.size], GUINT_TO_POINTER (ins->backend.size));
120                                                 }
121                                         }
122                                 }
123                         }
124                 }
125
126                 if (cfg->verbose_level > 2) {
127                         printf ("\nSEQ POINT MAP: \n");
128
129                         for (i = 0; i < cfg->seq_points->len; ++i) {
130                                 SeqPoint *sp = &seq_points [i];
131                                 GSList *l;
132
133                                 if (!next [i])
134                                         continue;
135
136                                 printf ("\tIL0x%x[0x%0x] ->", sp->il_offset, sp->native_offset);
137                                 for (l = next [i]; l; l = l->next) {
138                                         int next_index = GPOINTER_TO_UINT (l->data);
139                                         printf (" IL0x%x", seq_points [next_index].il_offset);
140                                 }
141                                 printf ("\n");
142                         }
143                 }
144         }
145
146         array = g_byte_array_new ();
147
148         { /* Add sequence points to seq_point_info */
149                 SeqPoint zero_seq_point = {0};
150                 SeqPoint* last_seq_point = &zero_seq_point;
151
152                 for (i = 0; i < cfg->seq_points->len; ++i) {
153                         SeqPoint *sp = &seq_points [i];
154                         GSList* next_list = NULL;
155
156                         if (has_debug_data)
157                                 next_list = next[i];
158
159                         if (mono_seq_point_info_add_seq_point (array, sp, last_seq_point, next_list, has_debug_data))
160                                 last_seq_point = sp;
161
162                         if (has_debug_data)
163                                 g_slist_free (next [i]);
164                 }
165         }
166
167         if (has_debug_data)
168                 g_free (next);
169
170         cfg->seq_point_info = mono_seq_point_info_new (array->len, TRUE, array->data, has_debug_data, &seq_info_size);
171         mono_jit_stats.allocated_seq_points_size += seq_info_size;
172
173         g_byte_array_free (array, TRUE);
174
175         // FIXME: dynamic methods
176         if (!cfg->compile_aot) {
177                 mono_domain_lock (domain);
178                 // FIXME: How can the lookup succeed ?
179                 if (!g_hash_table_lookup (domain_jit_info (domain)->seq_points, cfg->method_to_register))
180                         g_hash_table_insert (domain_jit_info (domain)->seq_points, cfg->method_to_register, cfg->seq_point_info);
181                 mono_domain_unlock (domain);
182         }
183
184         g_ptr_array_free (cfg->seq_points, TRUE);
185         cfg->seq_points = NULL;
186 }
187
188 MonoSeqPointInfo*
189 mono_get_seq_points (MonoDomain *domain, MonoMethod *method)
190 {
191         MonoSeqPointInfo *seq_points;
192
193         mono_domain_lock (domain);
194         seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, method);
195         if (!seq_points && method->is_inflated) {
196                 /* generic sharing + aot */
197                 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, mono_method_get_declaring_generic_method (method));
198                 if (!seq_points)
199                         seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, mini_get_shared_method (method));
200         }
201         mono_domain_unlock (domain);
202
203         return seq_points;
204 }
205
206 /*
207  * mono_find_next_seq_point_for_native_offset:
208  *
209  *   Find the first sequence point after NATIVE_OFFSET.
210  */
211 gboolean
212 mono_find_next_seq_point_for_native_offset (MonoDomain *domain, MonoMethod *method, gint32 native_offset, MonoSeqPointInfo **info, SeqPoint* seq_point)
213 {
214         MonoSeqPointInfo *seq_points;
215
216         seq_points = mono_get_seq_points (domain, method);
217         if (!seq_points) {
218                 if (info)
219                         *info = NULL;
220                 return FALSE;
221         }
222         if (info)
223                 *info = seq_points;
224
225         return mono_seq_point_find_next_by_native_offset (seq_points, native_offset, seq_point);
226 }
227
228 /*
229  * mono_find_prev_seq_point_for_native_offset:
230  *
231  *   Find the first sequence point before NATIVE_OFFSET.
232  */
233 gboolean
234 mono_find_prev_seq_point_for_native_offset (MonoDomain *domain, MonoMethod *method, gint32 native_offset, MonoSeqPointInfo **info, SeqPoint* seq_point)
235 {
236         MonoSeqPointInfo *seq_points;
237
238         seq_points = mono_get_seq_points (domain, method);
239         if (!seq_points) {
240                 if (info)
241                         *info = NULL;
242                 return FALSE;
243         }
244         if (info)
245                 *info = seq_points;
246
247         return mono_seq_point_find_prev_by_native_offset (seq_points, native_offset, seq_point);
248 }
249
250 /*
251  * mono_find_seq_point:
252  *
253  *   Find the sequence point corresponding to the IL offset IL_OFFSET, which
254  * should be the location of a sequence point.
255  */
256 gboolean
257 mono_find_seq_point (MonoDomain *domain, MonoMethod *method, gint32 il_offset, MonoSeqPointInfo **info, SeqPoint *seq_point)
258 {
259         MonoSeqPointInfo *seq_points;
260
261         seq_points = mono_get_seq_points (domain, method);
262         if (!seq_points) {
263                 if (info)
264                         *info = NULL;
265                 return FALSE;
266         }
267         if (info)
268                 *info = seq_points;
269
270         return mono_seq_point_find_by_il_offset (seq_points, il_offset, seq_point);
271 }
272
273 void
274 mono_bb_deduplicate_op_il_seq_points (MonoCompile *cfg, MonoBasicBlock *bb)
275 {
276         MonoInst *ins, *n, *prev;
277
278         MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
279                 if (ins->opcode != OP_IL_SEQ_POINT)
280                         continue;
281
282                 prev = mono_inst_prev (ins, FILTER_NOP);
283
284                 if (!prev || ins == prev || prev->opcode != OP_IL_SEQ_POINT)
285                         continue;
286
287                 MONO_REMOVE_INS (bb, prev);
288         };
289 }
290
291 void
292 mono_image_get_aot_seq_point_path (MonoImage *image, char **str)
293 {
294         int size = strlen (image->name) + strlen (SEQ_POINT_AOT_EXT) + 1;
295         *str = g_malloc (size);
296         g_sprintf (*str, "%s%s", image->name, SEQ_POINT_AOT_EXT);
297 }