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 # define ADDR_MASK(type, x) ((type)((uintptr_t)(x) & 0x7FFFFFFF))
45 # define ADDR_MASK(type, x) (x)
48 /* linenumbertable_create ******************************************************
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.
54 *******************************************************************************/
56 void linenumbertable_create(jitdata *jd)
60 linenumbertable_t *lnt;
61 linenumbertable_entry_t *lnte;
63 linenumbertable_list_entry_t *le;
67 /* Get required compiler data. */
72 /* Don't allocate a linenumber table if we don't need one. */
79 /* Allocate the linenumber table and the entries array. */
81 lnt = NEW(linenumbertable_t);
82 lnte = MNEW(linenumbertable_entry_t, l->size);
84 #if defined(ENABLE_STATISTICS)
86 count_linenumbertable++;
88 size_linenumbertable +=
89 sizeof(linenumbertable_t) +
90 sizeof(linenumbertable_entry_t) * l->size;
94 /* Fill the linenumber table. */
96 lnt->length = l->size;
99 /* Fill the linenumber table entries in reverse order, so the
100 search can be forward. */
102 pv = ADDR_MASK(uint8_t *, code->entrypoint);
104 for (le = list_last_unsynced(l); le != NULL;
105 le = list_prev_unsynced(l, le), lnte++) {
106 /* If the entry contains an mcode pointer (normal case),
107 resolve it (see doc/inlining_stacktrace.txt for
110 if (le->linenumber >= -2)
113 pc = (void *) le->mpc;
115 /* Fill the linenumber table entry. */
117 lnte->linenumber = le->linenumber;
121 /* Store the linenumber table in the codeinfo. */
123 code->linenumbertable = lnt;
127 /* linenumbertable_list_entry_add **********************************************
129 Add a line number reference.
132 cd.............current codegen data
133 linenumber.....number of line that starts with the given mcodeptr
135 *******************************************************************************/
137 void linenumbertable_list_entry_add(codegendata *cd, int32_t linenumber)
139 linenumbertable_list_entry_t *le;
141 le = DNEW(linenumbertable_list_entry_t);
143 le->linenumber = linenumber;
144 le->mpc = cd->mcodeptr - cd->mcodebase;
146 list_add_last_unsynced(cd->linenumbers, le);
150 /* linenumbertable_list_entry_add_inline_start *********************************
152 Add a marker to the line number table indicating the start of an
153 inlined method body. (see doc/inlining_stacktrace.txt)
156 cd ..... current codegen data
157 iptr ... the ICMD_INLINE_BODY instruction
159 *******************************************************************************/
161 void linenumbertable_list_entry_add_inline_start(codegendata *cd, instruction *iptr)
163 linenumbertable_list_entry_t *le;
164 insinfo_inline *insinfo;
167 le = DNEW(linenumbertable_list_entry_t);
169 le->linenumber = (-2); /* marks start of inlined method */
170 le->mpc = (mpc = cd->mcodeptr - cd->mcodebase);
172 list_add_last_unsynced(cd->linenumbers, le);
174 insinfo = iptr->sx.s23.s3.inlineinfo;
176 insinfo->startmpc = mpc; /* store for corresponding INLINE_END */
180 /* linenumbertable_list_entry_add_inline_end ***********************************
182 Add a marker to the line number table indicating the end of an
183 inlined method body. (see doc/inlining_stacktrace.txt)
186 cd ..... current codegen data
187 iptr ... the ICMD_INLINE_END instruction
190 iptr->method must point to the inlined callee.
192 *******************************************************************************/
194 void linenumbertable_list_entry_add_inline_end(codegendata *cd, instruction *iptr)
196 linenumbertable_list_entry_t *le;
197 insinfo_inline *insinfo;
199 insinfo = iptr->sx.s23.s3.inlineinfo;
203 le = DNEW(linenumbertable_list_entry_t);
205 /* special entry containing the methodinfo * */
206 le->linenumber = (-3) - iptr->line;
207 le->mpc = (uintptr_t) insinfo->method;
209 list_add_last_unsynced(cd->linenumbers, le);
211 le = DNEW(linenumbertable_list_entry_t);
213 /* end marker with PC of start of body */
214 le->linenumber = (-1);
215 le->mpc = insinfo->startmpc;
217 list_add_last_unsynced(cd->linenumbers, le);
221 /* linenumbertable_linenumber_for_pc_intern ************************************
223 This function search the line number table for the line
224 corresponding to a given pc. The function recurses for inlined
227 *******************************************************************************/
229 static s4 linenumbertable_linenumber_for_pc_intern(methodinfo **pm, linenumbertable_entry_t *lnte, int32_t lntsize, void *pc)
231 linenumbertable_entry_t *lntinline; /* special entry for inlined method */
233 pc = ADDR_MASK(void *, pc);
235 for (; lntsize > 0; lntsize--, lnte++) {
236 /* Note: In case of inlining this may actually compare the pc
237 against a methodinfo *, yielding a non-sensical
238 result. This is no problem, however, as we ignore such
239 entries in the switch below. This way we optimize for the
240 common case (ie. a real pc in lntentry->pc). */
242 if (pc >= lnte->pc) {
243 /* did we reach the current line? */
245 if (lnte->linenumber >= 0)
246 return lnte->linenumber;
248 /* we found a special inline entry (see
249 doc/inlining_stacktrace.txt for details */
251 switch (lnte->linenumber) {
253 /* begin of inlined method (ie. INLINE_END
256 lntinline = --lnte; /* get entry with methodinfo * */
257 lnte--; /* skip the special entry */
260 /* search inside the inlined method */
262 if (linenumbertable_linenumber_for_pc_intern(pm, lnte, lntsize, pc)) {
263 /* the inlined method contained the pc */
265 *pm = (methodinfo *) lntinline->pc;
267 assert(lntinline->linenumber <= -3);
269 return (-3) - lntinline->linenumber;
272 /* pc was not in inlined method, continue search.
273 Entries inside the inlined method will be skipped
274 because their lntentry->pc is higher than pc. */
278 /* end of inlined method */
282 /* default: is only reached for an -3-line entry after
283 a skipped -2 entry. We can safely ignore it and
284 continue searching. */
295 /* linenumbertable_linenumber_for_pc *******************************************
297 A wrapper for linenumbertable_linenumber_for_pc_intern.
299 NOTE: We have a intern version because the function is called
300 recursively for inlined methods.
302 *******************************************************************************/
304 int32_t linenumbertable_linenumber_for_pc(methodinfo **pm, codeinfo *code, void *pc)
306 linenumbertable_t *lnt;
309 /* Get line number table. */
311 lnt = code->linenumbertable;
316 /* Get the line number. */
318 linenumber = linenumbertable_linenumber_for_pc_intern(pm, lnt->entries, lnt->length, pc);
325 * These are local overrides for various environment variables in Emacs.
326 * Please do not remove this and leave it at the end of the file, where
327 * Emacs will automagically detect them.
328 * ---------------------------------------------------------------------
331 * indent-tabs-mode: t
335 * vim:noexpandtab:sw=4:ts=4: