1 /* src/vm/jit/linenumbertable.c - linenumber handling stuff
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine 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 #if defined(ENABLE_STATISTICS)
34 # include "vm/options.h"
35 # include "vm/statistics.h"
38 #include "vm/jit/code.h"
39 #include "vm/jit/codegen-common.hpp"
40 #include "vm/jit/linenumbertable.h"
44 # define ADDR_MASK(type, x) ((type)((uintptr_t)(x) & 0x7FFFFFFF))
46 # define ADDR_MASK(type, x) (x)
49 /* linenumbertable_create ******************************************************
51 Creates the linenumber table. We allocate an array and store the
52 linenumber entry in reverse-order, so we can search the correct
53 linenumber more easily.
55 *******************************************************************************/
57 void linenumbertable_create(jitdata *jd)
61 linenumbertable_t *lnt;
62 linenumbertable_entry_t *lnte;
64 linenumbertable_list_entry_t *le;
68 /* Get required compiler data. */
73 /* Don't allocate a linenumber table if we don't need one. */
80 /* Allocate the linenumber table and the entries array. */
82 lnt = NEW(linenumbertable_t);
83 lnte = MNEW(linenumbertable_entry_t, l->size);
85 #if defined(ENABLE_STATISTICS)
87 count_linenumbertable++;
89 size_linenumbertable +=
90 sizeof(linenumbertable_t) +
91 sizeof(linenumbertable_entry_t) * l->size;
95 /* Fill the linenumber table. */
97 lnt->length = l->size;
100 /* Fill the linenumber table entries in reverse order, so the
101 search can be forward. */
103 /* FIXME I only made this change to prevent a problem when moving
104 to C++. This should be changed back when this file has
107 pv = ADDR_MASK(uint8_t *, code->entrypoint);
109 for (le = list_first(l); le != NULL; le = list_next(l, le), lnte++) {
110 /* If the entry contains an mcode pointer (normal case),
111 resolve it (see doc/inlining_stacktrace.txt for
114 if (le->linenumber >= -2)
117 pc = (void *) le->mpc;
119 /* Fill the linenumber table entry. */
121 lnte->linenumber = le->linenumber;
125 /* Store the linenumber table in the codeinfo. */
127 code->linenumbertable = lnt;
131 /* linenumbertable_list_entry_add **********************************************
133 Add a line number reference.
136 cd.............current codegen data
137 linenumber.....number of line that starts with the given mcodeptr
139 *******************************************************************************/
141 void linenumbertable_list_entry_add(codegendata *cd, int32_t linenumber)
143 linenumbertable_list_entry_t *le;
145 le = DNEW(linenumbertable_list_entry_t);
147 le->linenumber = linenumber;
148 le->mpc = cd->mcodeptr - cd->mcodebase;
150 list_add_first(cd->linenumbers, le);
154 /* linenumbertable_list_entry_add_inline_start *********************************
156 Add a marker to the line number table indicating the start of an
157 inlined method body. (see doc/inlining_stacktrace.txt)
160 cd ..... current codegen data
161 iptr ... the ICMD_INLINE_BODY instruction
163 *******************************************************************************/
165 void linenumbertable_list_entry_add_inline_start(codegendata *cd, instruction *iptr)
167 linenumbertable_list_entry_t *le;
168 insinfo_inline *insinfo;
171 le = DNEW(linenumbertable_list_entry_t);
173 le->linenumber = (-2); /* marks start of inlined method */
174 le->mpc = (mpc = cd->mcodeptr - cd->mcodebase);
176 list_add_first(cd->linenumbers, le);
178 insinfo = iptr->sx.s23.s3.inlineinfo;
180 insinfo->startmpc = mpc; /* store for corresponding INLINE_END */
184 /* linenumbertable_list_entry_add_inline_end ***********************************
186 Add a marker to the line number table indicating the end of an
187 inlined method body. (see doc/inlining_stacktrace.txt)
190 cd ..... current codegen data
191 iptr ... the ICMD_INLINE_END instruction
194 iptr->method must point to the inlined callee.
196 *******************************************************************************/
198 void linenumbertable_list_entry_add_inline_end(codegendata *cd, instruction *iptr)
200 linenumbertable_list_entry_t *le;
201 insinfo_inline *insinfo;
203 insinfo = iptr->sx.s23.s3.inlineinfo;
207 le = DNEW(linenumbertable_list_entry_t);
209 /* special entry containing the methodinfo * */
210 le->linenumber = (-3) - iptr->line;
211 le->mpc = (uintptr_t) insinfo->method;
213 list_add_first(cd->linenumbers, le);
215 le = DNEW(linenumbertable_list_entry_t);
217 /* end marker with PC of start of body */
218 le->linenumber = (-1);
219 le->mpc = insinfo->startmpc;
221 list_add_first(cd->linenumbers, le);
225 /* linenumbertable_linenumber_for_pc_intern ************************************
227 This function search the line number table for the line
228 corresponding to a given pc. The function recurses for inlined
231 *******************************************************************************/
233 static s4 linenumbertable_linenumber_for_pc_intern(methodinfo **pm, linenumbertable_entry_t *lnte, int32_t lntsize, void *pc)
235 linenumbertable_entry_t *lntinline; /* special entry for inlined method */
237 pc = ADDR_MASK(void *, pc);
239 for (; lntsize > 0; lntsize--, lnte++) {
240 /* Note: In case of inlining this may actually compare the pc
241 against a methodinfo *, yielding a non-sensical
242 result. This is no problem, however, as we ignore such
243 entries in the switch below. This way we optimize for the
244 common case (ie. a real pc in lntentry->pc). */
246 if (pc >= lnte->pc) {
247 /* did we reach the current line? */
249 if (lnte->linenumber >= 0)
250 return lnte->linenumber;
252 /* we found a special inline entry (see
253 doc/inlining_stacktrace.txt for details */
255 switch (lnte->linenumber) {
257 /* begin of inlined method (ie. INLINE_END
260 lntinline = --lnte; /* get entry with methodinfo * */
261 lnte--; /* skip the special entry */
264 /* search inside the inlined method */
266 if (linenumbertable_linenumber_for_pc_intern(pm, lnte, lntsize, pc)) {
267 /* the inlined method contained the pc */
269 *pm = (methodinfo *) lntinline->pc;
271 assert(lntinline->linenumber <= -3);
273 return (-3) - lntinline->linenumber;
276 /* pc was not in inlined method, continue search.
277 Entries inside the inlined method will be skipped
278 because their lntentry->pc is higher than pc. */
282 /* end of inlined method */
286 /* default: is only reached for an -3-line entry after
287 a skipped -2 entry. We can safely ignore it and
288 continue searching. */
299 /* linenumbertable_linenumber_for_pc *******************************************
301 A wrapper for linenumbertable_linenumber_for_pc_intern.
303 NOTE: We have a intern version because the function is called
304 recursively for inlined methods.
306 *******************************************************************************/
308 int32_t linenumbertable_linenumber_for_pc(methodinfo **pm, codeinfo *code, void *pc)
310 linenumbertable_t *lnt;
313 /* Get line number table. */
315 lnt = code->linenumbertable;
320 /* Get the line number. */
322 linenumber = linenumbertable_linenumber_for_pc_intern(pm, lnt->entries, lnt->length, pc);
329 * These are local overrides for various environment variables in Emacs.
330 * Please do not remove this and leave it at the end of the file, where
331 * Emacs will automagically detect them.
332 * ---------------------------------------------------------------------
335 * indent-tabs-mode: t
339 * vim:noexpandtab:sw=4:ts=4: