Clean merge -> gc7-branch
[cacao.git] / src / vm / jit / linenumbertable.c
1 /* src/vm/jit/linenumbertable.c - linenumber handling stuff
2
3    Copyright (C) 2007
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdint.h>
30
31 #include "mm/memory.h"
32
33 #include "vm/jit/code.h"
34 #include "vm/jit/codegen-common.h"
35 #include "vm/jit/linenumbertable.h"
36
37 #if defined(ENABLE_STATISTICS)
38 # include "vmcore/options.h"
39 # include "vmcore/statistics.h"
40 #endif
41
42 #if defined(__S390__)
43 #  define ADDR_MASK(type, x) ((type)((uintptr_t)(x) & 0x7FFFFFFF))
44 #else
45 #  define ADDR_MASK(type, x) (x)
46 #endif
47
48 /* linenumbertable_create ******************************************************
49
50    Creates the linenumber table.  We allocate an array and store the
51    linenumber entry in reverse-order, so we can search the correct
52    linenumber more easily.
53
54 *******************************************************************************/
55
56 void linenumbertable_create(jitdata *jd)
57 {
58         codeinfo                     *code;
59         codegendata                  *cd;
60         linenumbertable_t            *lnt;
61         linenumbertable_entry_t      *lnte;
62         list_t                       *l;
63         linenumbertable_list_entry_t *le;
64         uint8_t                      *pv;
65         void                         *pc;
66
67         /* Get required compiler data. */
68
69         code = jd->code;
70         cd   = jd->cd;
71
72         /* Don't allocate a linenumber table if we don't need one. */
73
74         l = cd->linenumbers;
75
76         if (l->size == 0)
77                 return;
78
79         /* Allocate the linenumber table and the entries array. */
80
81         lnt  = NEW(linenumbertable_t);
82         lnte = MNEW(linenumbertable_entry_t, l->size);
83
84 #if defined(ENABLE_STATISTICS)
85         if (opt_stat) {
86                 count_linenumbertable++;
87
88                 size_linenumbertable +=
89                         sizeof(linenumbertable_t) +
90                         sizeof(linenumbertable_entry_t) * l->size;
91         }
92 #endif
93
94         /* Fill the linenumber table. */
95
96         lnt->length  = l->size;
97         lnt->entries = lnte;
98
99         /* Fill the linenumber table entries in reverse order, so the
100            search can be forward. */
101
102         /* FIXME I only made this change to prevent a problem when moving
103            to C++. This should be changed back when this file has
104            converted to C++. */
105
106         pv = ADDR_MASK(uint8_t *, code->entrypoint);
107
108         for (le = list_first(l); le != NULL; le = list_next(l, le), lnte++) {
109                 /* If the entry contains an mcode pointer (normal case),
110                    resolve it (see doc/inlining_stacktrace.txt for
111                    details). */
112
113                 if (le->linenumber >= -2)
114                         pc = pv + le->mpc;
115                 else
116                         pc = (void *) le->mpc;
117
118                 /* Fill the linenumber table entry. */
119
120                 lnte->linenumber = le->linenumber;
121                 lnte->pc         = pc;
122         }
123
124         /* Store the linenumber table in the codeinfo. */
125
126         code->linenumbertable = lnt;
127 }
128
129
130 /* linenumbertable_list_entry_add **********************************************
131
132    Add a line number reference.
133
134    IN:
135       cd.............current codegen data
136       linenumber.....number of line that starts with the given mcodeptr
137
138 *******************************************************************************/
139
140 void linenumbertable_list_entry_add(codegendata *cd, int32_t linenumber)
141 {
142         linenumbertable_list_entry_t *le;
143
144         le = DNEW(linenumbertable_list_entry_t);
145
146         le->linenumber = linenumber;
147         le->mpc        = cd->mcodeptr - cd->mcodebase;
148
149         list_add_first(cd->linenumbers, le);
150 }
151
152
153 /* linenumbertable_list_entry_add_inline_start *********************************
154
155    Add a marker to the line number table indicating the start of an
156    inlined method body. (see doc/inlining_stacktrace.txt)
157
158    IN:
159       cd ..... current codegen data
160       iptr ... the ICMD_INLINE_BODY instruction
161
162 *******************************************************************************/
163
164 void linenumbertable_list_entry_add_inline_start(codegendata *cd, instruction *iptr)
165 {
166         linenumbertable_list_entry_t *le;
167         insinfo_inline               *insinfo;
168         uintptr_t                     mpc;
169
170         le = DNEW(linenumbertable_list_entry_t);
171
172         le->linenumber = (-2); /* marks start of inlined method */
173         le->mpc        = (mpc = cd->mcodeptr - cd->mcodebase);
174
175         list_add_first(cd->linenumbers, le);
176
177         insinfo = iptr->sx.s23.s3.inlineinfo;
178
179         insinfo->startmpc = mpc; /* store for corresponding INLINE_END */
180 }
181
182
183 /* linenumbertable_list_entry_add_inline_end ***********************************
184
185    Add a marker to the line number table indicating the end of an
186    inlined method body. (see doc/inlining_stacktrace.txt)
187
188    IN:
189       cd ..... current codegen data
190       iptr ... the ICMD_INLINE_END instruction
191
192    Note:
193       iptr->method must point to the inlined callee.
194
195 *******************************************************************************/
196
197 void linenumbertable_list_entry_add_inline_end(codegendata *cd, instruction *iptr)
198 {
199         linenumbertable_list_entry_t *le;
200         insinfo_inline               *insinfo;
201
202         insinfo = iptr->sx.s23.s3.inlineinfo;
203
204         assert(insinfo);
205
206         le = DNEW(linenumbertable_list_entry_t);
207
208         /* special entry containing the methodinfo * */
209         le->linenumber = (-3) - iptr->line;
210         le->mpc        = (uintptr_t) insinfo->method;
211
212         list_add_first(cd->linenumbers, le);
213
214         le = DNEW(linenumbertable_list_entry_t);
215
216         /* end marker with PC of start of body */
217         le->linenumber = (-1);
218         le->mpc        = insinfo->startmpc;
219
220         list_add_first(cd->linenumbers, le);
221 }
222
223
224 /* linenumbertable_linenumber_for_pc_intern ************************************
225
226    This function search the line number table for the line
227    corresponding to a given pc. The function recurses for inlined
228    methods.
229
230 *******************************************************************************/
231
232 static s4 linenumbertable_linenumber_for_pc_intern(methodinfo **pm, linenumbertable_entry_t *lnte, int32_t lntsize, void *pc)
233 {
234         linenumbertable_entry_t *lntinline;   /* special entry for inlined method */
235
236         pc = ADDR_MASK(void *, pc);
237
238         for (; lntsize > 0; lntsize--, lnte++) {
239                 /* Note: In case of inlining this may actually compare the pc
240                    against a methodinfo *, yielding a non-sensical
241                    result. This is no problem, however, as we ignore such
242                    entries in the switch below. This way we optimize for the
243                    common case (ie. a real pc in lntentry->pc). */
244
245                 if (pc >= lnte->pc) {
246                         /* did we reach the current line? */
247
248                         if (lnte->linenumber >= 0)
249                                 return lnte->linenumber;
250
251                         /* we found a special inline entry (see
252                            doc/inlining_stacktrace.txt for details */
253
254                         switch (lnte->linenumber) {
255                         case -1: 
256                                 /* begin of inlined method (ie. INLINE_END
257                                    instruction) */
258
259                                 lntinline = --lnte;            /* get entry with methodinfo * */
260                                 lnte--;                        /* skip the special entry      */
261                                 lntsize -= 2;
262
263                                 /* search inside the inlined method */
264
265                                 if (linenumbertable_linenumber_for_pc_intern(pm, lnte, lntsize, pc)) {
266                                         /* the inlined method contained the pc */
267
268                                         *pm = (methodinfo *) lntinline->pc;
269
270                                         assert(lntinline->linenumber <= -3);
271
272                                         return (-3) - lntinline->linenumber;
273                                 }
274
275                                 /* pc was not in inlined method, continue search.
276                                    Entries inside the inlined method will be skipped
277                                    because their lntentry->pc is higher than pc.  */
278                                 break;
279
280                         case -2: 
281                                 /* end of inlined method */
282
283                                 return 0;
284
285                                 /* default: is only reached for an -3-line entry after
286                                    a skipped -2 entry. We can safely ignore it and
287                                    continue searching.  */
288                         }
289                 }
290         }
291
292         /* PC not found. */
293
294         return 0;
295 }
296
297
298 /* linenumbertable_linenumber_for_pc *******************************************
299
300    A wrapper for linenumbertable_linenumber_for_pc_intern.
301
302    NOTE: We have a intern version because the function is called
303    recursively for inlined methods.
304
305 *******************************************************************************/
306
307 int32_t linenumbertable_linenumber_for_pc(methodinfo **pm, codeinfo *code, void *pc)
308 {
309         linenumbertable_t *lnt;
310         int32_t            linenumber;
311
312         /* Get line number table. */
313
314         lnt = code->linenumbertable;
315
316         if (lnt == NULL)
317                 return 0;
318
319         /* Get the line number. */
320
321         linenumber = linenumbertable_linenumber_for_pc_intern(pm, lnt->entries, lnt->length, pc);
322
323         return linenumber;
324 }
325
326
327 /*
328  * These are local overrides for various environment variables in Emacs.
329  * Please do not remove this and leave it at the end of the file, where
330  * Emacs will automagically detect them.
331  * ---------------------------------------------------------------------
332  * Local variables:
333  * mode: c
334  * indent-tabs-mode: t
335  * c-basic-offset: 4
336  * tab-width: 4
337  * End:
338  * vim:noexpandtab:sw=4:ts=4:
339  */