1 /* src/vm/jit/linenumbertable.c - linenumber handling stuff
4 CACAOVM - Verein zu Foerderung der freien virtuellen Machine CACAO
6 This file is part of CACAO.
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.
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.
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
31 #include "mm/memory.h"
33 #include "vm/jit/code.h"
34 #include "vm/jit/codegen-common.h"
35 #include "vm/jit/linenumbertable.h"
37 #if defined(ENABLE_STATISTICS)
38 # include "vmcore/options.h"
39 # include "vmcore/statistics.h"
43 /* linenumbertable_create ******************************************************
45 Creates the linenumber table. We allocate an array and store the
46 linenumber entry in reverse-order, so we can search the correct
47 linenumber more easily.
49 *******************************************************************************/
51 void linenumbertable_create(jitdata *jd)
55 linenumbertable_t *lnt;
56 linenumbertable_entry_t *lnte;
58 linenumbertable_list_entry_t *le;
62 /* Get required compiler data. */
67 /* Don't allocate a linenumber table if we don't need one. */
74 /* Allocate the linenumber table and the entries array. */
76 lnt = NEW(linenumbertable_t);
77 lnte = MNEW(linenumbertable_entry_t, l->size);
79 #if defined(ENABLE_STATISTICS)
81 count_linenumbertable++;
83 size_linenumbertable +=
84 sizeof(linenumbertable_t) +
85 sizeof(linenumbertable_entry_t) * l->size;
89 /* Fill the linenumber table. */
91 lnt->length = l->size;
94 /* Fill the linenumber table entries in reverse order, so the
95 search can be forward. */
97 pv = code->entrypoint;
99 for (le = list_last_unsynced(l); le != NULL;
100 le = list_prev_unsynced(l, le), lnte++) {
101 /* If the entry contains an mcode pointer (normal case),
102 resolve it (see doc/inlining_stacktrace.txt for
105 if (le->linenumber >= -2)
108 pc = (void *) le->mpc;
110 /* Fill the linenumber table entry. */
112 lnte->linenumber = le->linenumber;
116 /* Store the linenumber table in the codeinfo. */
118 code->linenumbertable = lnt;
122 /* linenumbertable_list_entry_add **********************************************
124 Add a line number reference.
127 cd.............current codegen data
128 linenumber.....number of line that starts with the given mcodeptr
130 *******************************************************************************/
132 void linenumbertable_list_entry_add(codegendata *cd, int32_t linenumber)
134 linenumbertable_list_entry_t *le;
136 le = DNEW(linenumbertable_list_entry_t);
138 le->linenumber = linenumber;
139 le->mpc = cd->mcodeptr - cd->mcodebase;
141 list_add_last_unsynced(cd->linenumbers, le);
145 /* linenumbertable_list_entry_add_inline_start *********************************
147 Add a marker to the line number table indicating the start of an
148 inlined method body. (see doc/inlining_stacktrace.txt)
151 cd ..... current codegen data
152 iptr ... the ICMD_INLINE_BODY instruction
154 *******************************************************************************/
156 void linenumbertable_list_entry_add_inline_start(codegendata *cd, instruction *iptr)
158 linenumbertable_list_entry_t *le;
159 insinfo_inline *insinfo;
162 le = DNEW(linenumbertable_list_entry_t);
164 le->linenumber = (-2); /* marks start of inlined method */
165 le->mpc = (mpc = cd->mcodeptr - cd->mcodebase);
167 list_add_last_unsynced(cd->linenumbers, le);
169 insinfo = iptr->sx.s23.s3.inlineinfo;
171 insinfo->startmpc = mpc; /* store for corresponding INLINE_END */
175 /* linenumbertable_list_entry_add_inline_end ***********************************
177 Add a marker to the line number table indicating the end of an
178 inlined method body. (see doc/inlining_stacktrace.txt)
181 cd ..... current codegen data
182 iptr ... the ICMD_INLINE_END instruction
185 iptr->method must point to the inlined callee.
187 *******************************************************************************/
189 void linenumbertable_list_entry_add_inline_end(codegendata *cd, instruction *iptr)
191 linenumbertable_list_entry_t *le;
192 insinfo_inline *insinfo;
194 insinfo = iptr->sx.s23.s3.inlineinfo;
198 le = DNEW(linenumbertable_list_entry_t);
200 /* special entry containing the methodinfo * */
201 le->linenumber = (-3) - iptr->line;
202 le->mpc = (uintptr_t) insinfo->method;
204 list_add_last_unsynced(cd->linenumbers, le);
206 le = DNEW(linenumbertable_list_entry_t);
208 /* end marker with PC of start of body */
209 le->linenumber = (-1);
210 le->mpc = insinfo->startmpc;
212 list_add_last_unsynced(cd->linenumbers, le);
216 /* linenumbertable_linenumber_for_pc_intern ************************************
218 This function search the line number table for the line
219 corresponding to a given pc. The function recurses for inlined
222 *******************************************************************************/
224 static s4 linenumbertable_linenumber_for_pc_intern(methodinfo **pm, linenumbertable_entry_t *lnte, int32_t lntsize, void *pc)
226 linenumbertable_entry_t *lntinline; /* special entry for inlined method */
228 for (; lntsize > 0; lntsize--, lnte++) {
229 /* Note: In case of inlining this may actually compare the pc
230 against a methodinfo *, yielding a non-sensical
231 result. This is no problem, however, as we ignore such
232 entries in the switch below. This way we optimize for the
233 common case (ie. a real pc in lntentry->pc). */
235 if (pc >= lnte->pc) {
236 /* did we reach the current line? */
238 if (lnte->linenumber >= 0)
239 return lnte->linenumber;
241 /* we found a special inline entry (see
242 doc/inlining_stacktrace.txt for details */
244 switch (lnte->linenumber) {
246 /* begin of inlined method (ie. INLINE_END
249 lntinline = --lnte; /* get entry with methodinfo * */
250 lnte--; /* skip the special entry */
253 /* search inside the inlined method */
255 if (linenumbertable_linenumber_for_pc_intern(pm, lnte, lntsize, pc)) {
256 /* the inlined method contained the pc */
258 *pm = (methodinfo *) lntinline->pc;
260 assert(lntinline->linenumber <= -3);
262 return (-3) - lntinline->linenumber;
265 /* pc was not in inlined method, continue search.
266 Entries inside the inlined method will be skipped
267 because their lntentry->pc is higher than pc. */
271 /* end of inlined method */
275 /* default: is only reached for an -3-line entry after
276 a skipped -2 entry. We can safely ignore it and
277 continue searching. */
288 /* linenumbertable_linenumber_for_pc *******************************************
290 A wrapper for linenumbertable_linenumber_for_pc_intern.
292 NOTE: We have a intern version because the function is called
293 recursively for inlined methods.
295 *******************************************************************************/
297 int32_t linenumbertable_linenumber_for_pc(methodinfo **pm, codeinfo *code, void *pc)
299 linenumbertable_t *lnt;
302 /* Get line number table. */
304 lnt = code->linenumbertable;
309 /* Get the line number. */
311 linenumber = linenumbertable_linenumber_for_pc_intern(pm, lnt->entries, lnt->length, pc);
318 * These are local overrides for various environment variables in Emacs.
319 * Please do not remove this and leave it at the end of the file, where
320 * Emacs will automagically detect them.
321 * ---------------------------------------------------------------------
324 * indent-tabs-mode: t
328 * vim:noexpandtab:sw=4:ts=4: