CACAO
code.cpp
Go to the documentation of this file.
1 /* src/vm/jit/code.cpp - codeinfo struct for representing compiled code
2 
3  Copyright (C) 1996-2013
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 #include "vm/jit/code.hpp"
26 #include "mm/codememory.hpp" // for CFREE
27 #include "mm/memory.hpp" // for OFFSET, FREE, NEW
28 #include "vm/jit/linenumbertable.hpp" // for LinenumberTable
29 #include "vm/jit/methodtree.hpp" // for methodtree_find, etc
30 #include "vm/jit/patcher-common.hpp" // for patcher_list_create, etc
31 #include "vm/jit/replace.hpp" // for replace_free_replacement_points
32 #include "vm/options.hpp" // for checksync
33 #include "vm/vm.hpp" // for vm_abort
34 
35 STAT_DECLARE_GROUP(info_struct_stat)
36 STAT_REGISTER_GROUP_VAR(int,size_codeinfo,0,"size codeinfo","codeinfo",info_struct_stat) // sizeof(codeinfo)?
37 
38 struct methodinfo;
39 
40 /* code_init *******************************************************************
41 
42  Initialize the code-subsystem.
43 
44 *******************************************************************************/
45 
46 void code_init(void)
47 {
48  /* Check if offset of codeinfo.m == 0 (see comment in code.h). */
49 
50  if (OFFSET(codeinfo, m) != 0)
51  vm_abort("code_init: offset of codeinfo.m != 0: %d != 0", OFFSET(codeinfo, m));
52 }
53 
54 
55 /* code_codeinfo_new ***********************************************************
56 
57  Create a new codeinfo for the given method.
58 
59  IN:
60  m................method to create a new codeinfo for
61 
62  The following fields are set in codeinfo:
63  m
64  patchers
65 
66  RETURN VALUE:
67  a new, initialized codeinfo, or
68  NULL if an exception occurred.
69 
70 *******************************************************************************/
71 
73 {
74  codeinfo *code;
75 
76  code = NEW(codeinfo);
77 
78  code->m = m;
79 
80  patcher_list_create(code);
81 
82  STATISTICS(size_codeinfo += sizeof(codeinfo));
83 
84  return code;
85 }
86 
87 
88 /* code_find_codeinfo_for_pc ***************************************************
89 
90  Return the codeinfo for the compilation unit that contains the
91  given PC.
92 
93  ARGUMENTS:
94  pc...............machine code position
95 
96  RETURN VALUE:
97  the codeinfo * for the given PC
98 
99 *******************************************************************************/
100 
102 {
103  void *pv;
104 
105  pv = methodtree_find(pc);
106 
107  return code_get_codeinfo_for_pv(pv);
108 }
109 
110 
111 /* code_find_codeinfo_for_pc ***************************************************
112 
113  Return the codeinfo for the compilation unit that contains the
114  given PC. This method does not check the return value and is used
115  by the GC.
116 
117  IN:
118  pc...............machine code position
119 
120  RETURN VALUE:
121  the codeinfo * for the given PC, or NULL
122 
123 *******************************************************************************/
124 
126 {
127  void *pv;
128 
129  pv = methodtree_find_nocheck(pc);
130 
131  if (pv == NULL)
132  return NULL;
133 
134  return code_get_codeinfo_for_pv(pv);
135 }
136 
137 
138 /* code_get_methodinfo_for_pv **************************************************
139 
140  Return the methodinfo for the given PV.
141 
142  IN:
143  pv...............PV
144 
145  RETURN VALUE:
146  the methodinfo *
147 
148 *******************************************************************************/
149 
151 {
152  codeinfo *code;
153 
154  code = code_get_codeinfo_for_pv(pv);
155 
156  /* This is the case for asm_vm_call_method. */
157 
158  if (code == NULL)
159  return NULL;
160 
161  return code->m;
162 }
163 
164 
165 /* code_get_sync_slot_count ****************************************************
166 
167  Return the number of stack slots used for storing the synchronized object
168  (and the return value around lock_monitor_exit calls) by the given code.
169 
170  IN:
171  code.............the codeinfo of the code in question
172  (must be != NULL)
173 
174  RETURN VALUE:
175  the number of stack slots used for synchronization
176 
177 *******************************************************************************/
178 
179 #if defined(ENABLE_REPLACEMENT)
180 int code_get_sync_slot_count(codeinfo *code)
181 {
182  int count;
183 
184  assert(code);
185 
186  if (!checksync)
187  return 0;
188 
189  if (!code_is_synchronized(code))
190  return 0;
191 
192  count = 1;
193 
194 #if defined(__POWERPC__)
195  /* powerpc needs an extra slot */
196  count++;
197 #endif
198 
199  return count;
200 }
201 #endif /* defined(ENABLE_REPLACEMENT) */
202 
203 
204 /* code_codeinfo_free **********************************************************
205 
206  Free the memory used by a codeinfo.
207 
208  IN:
209  code.............the codeinfo to free
210 
211 *******************************************************************************/
212 
214 {
215  if (code == NULL)
216  return;
217 
218  if (code->mcode != NULL)
219  CFREE((void *) (ptrint) code->mcode, code->mcodelength);
220 
221  patcher_list_free(code);
222 
223 #if defined(ENABLE_REPLACEMENT)
225 #endif
226 
227 #if defined(ENABLE_PROFILING)
228  /* Release memory for basic block profiling information. */
229 
230  if (code->bbfrequency != NULL)
231  MFREE(code->bbfrequency, u4, code->basicblockcount);
232 #endif
233 
234  FREE(code, codeinfo);
235 
236  STATISTICS(size_codeinfo -= sizeof(codeinfo));
237 }
238 
239 
240 /* code_free_code_of_method ****************************************************
241 
242  Free all codeinfos of the given method
243 
244  IN:
245  m................the method of which the codeinfos are to be freed
246 
247 *******************************************************************************/
248 
250 {
251  codeinfo *nextcode;
252  codeinfo *code;
253 
254  if (!m)
255  return;
256 
257  nextcode = m->code;
258  while (nextcode) {
259  code = nextcode;
260  nextcode = code->prev;
261  code_codeinfo_free(code);
262  }
263 
264  m->code = NULL;
265 }
266 
267 /*
268  * These are local overrides for various environment variables in Emacs.
269  * Please do not remove this and leave it at the end of the file, where
270  * Emacs will automagically detect them.
271  * ---------------------------------------------------------------------
272  * Local variables:
273  * mode: c++
274  * indent-tabs-mode: t
275  * c-basic-offset: 4
276  * tab-width: 4
277  * End:
278  * vim:noexpandtab:sw=4:ts=4:
279  */
#define methodtree_find
Definition: md-asm.hpp:101
#define pv
Definition: md-asm.hpp:65
#define STATISTICS(x)
Wrapper for statistics only code.
Definition: statistics.hpp:975
void replace_free_replacement_points(codeinfo *code)
Definition: replace.cpp:722
void code_codeinfo_free(codeinfo *code)
Definition: code.cpp:213
methodinfo * code_get_methodinfo_for_pv(void *pv)
Definition: code.cpp:150
#define NEW(type)
Definition: memory.hpp:93
void code_init(void)
Definition: code.cpp:46
#define FREE(ptr, type)
Definition: memory.hpp:94
codeinfo * code
Definition: jit.hpp:128
codeinfo * code_find_codeinfo_for_pc_nocheck(void *pc)
Definition: code.cpp:125
s4 mcodelength
Definition: code.hpp:85
JNIEnv jthread jobject jclass jlong size
Definition: jvmti.h:387
void code_free_code_of_method(methodinfo *m)
Definition: code.cpp:249
void vm_abort(const char *text,...)
Definition: vm.cpp:2586
methodinfo * m
Definition: code.hpp:75
codeinfo * code_codeinfo_new(methodinfo *m)
Definition: code.cpp:72
u1 * mcode
Definition: code.hpp:83
codeinfo * code_find_codeinfo_for_pc(void *pc)
Definition: code.cpp:101
void patcher_list_create(codeinfo *code)
bool checksync
Definition: options.cpp:90
static codeinfo * code_get_codeinfo_for_pv(void *pv)
Definition: code.hpp:201
#define STAT_REGISTER_GROUP_VAR(type, var, init, name, description, group)
Register an statistics variable and add it to a group.
Definition: statistics.hpp:967
void * methodtree_find_nocheck(void *pc)
Definition: methodtree.cpp:223
#define CFREE(ptr, num)
Definition: codememory.hpp:35
codeinfo * code
Definition: method.hpp:103
codeinfo * prev
Definition: code.hpp:76
uint32_t u4
Definition: types.hpp:46
#define pc
Definition: md-asm.hpp:56
methodinfo * m
Definition: jit.hpp:127
#define STAT_DECLARE_GROUP(var)
Declare an external group (or subgroup).
Definition: statistics.hpp:970
static int code_is_synchronized(codeinfo *code)
Definition: code.hpp:173
uintptr_t ptrint
Definition: types.hpp:54
#define MFREE(ptr, type, num)
Definition: memory.hpp:97
#define OFFSET(s, el)
Definition: memory.hpp:90
void patcher_list_free(codeinfo *code)