author | xdono |
Mon, 09 Mar 2009 13:28:46 -0700 | |
changeset 2105 | 347008ce7984 |
parent 2006 | f2d2f0f20063 |
child 2570 | ecc7862946d4 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
2105 | 2 |
* Copyright 1998-2009 Sun Microsystems, Inc. All Rights Reserved. |
1 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 |
* have any questions. |
|
22 |
* |
|
23 |
*/ |
|
24 |
||
25 |
# include "incls/_precompiled.incl" |
|
26 |
# include "incls/_rewriter.cpp.incl" |
|
27 |
||
28 |
||
29 |
// Computes an index_map (new_index -> original_index) for contant pool entries |
|
30 |
// that are referred to by the interpreter at runtime via the constant pool cache. |
|
31 |
void Rewriter::compute_index_maps(constantPoolHandle pool, intArray*& index_map, intStack*& inverse_index_map) { |
|
32 |
const int length = pool->length(); |
|
33 |
index_map = new intArray(length, -1); |
|
34 |
// Choose an initial value large enough that we don't get frequent |
|
35 |
// calls to grow(). |
|
36 |
inverse_index_map = new intStack(length / 2); |
|
37 |
for (int i = 0; i < length; i++) { |
|
38 |
switch (pool->tag_at(i).value()) { |
|
39 |
case JVM_CONSTANT_Fieldref : // fall through |
|
40 |
case JVM_CONSTANT_Methodref : // fall through |
|
41 |
case JVM_CONSTANT_InterfaceMethodref: { |
|
42 |
index_map->at_put(i, inverse_index_map->length()); |
|
43 |
inverse_index_map->append(i); |
|
44 |
} |
|
45 |
} |
|
46 |
} |
|
47 |
} |
|
48 |
||
49 |
||
50 |
// Creates a constant pool cache given an inverse_index_map |
|
2006
f2d2f0f20063
6792421: assert(_bitMap->isMarked(addr+size-1),inconsistent Printezis mark)
jmasa
parents:
1
diff
changeset
|
51 |
// This creates the constant pool cache initially in a state |
f2d2f0f20063
6792421: assert(_bitMap->isMarked(addr+size-1),inconsistent Printezis mark)
jmasa
parents:
1
diff
changeset
|
52 |
// that is unsafe for concurrent GC processing but sets it to |
f2d2f0f20063
6792421: assert(_bitMap->isMarked(addr+size-1),inconsistent Printezis mark)
jmasa
parents:
1
diff
changeset
|
53 |
// a safe mode before the constant pool cache is returned. |
1 | 54 |
constantPoolCacheHandle Rewriter::new_constant_pool_cache(intArray& inverse_index_map, TRAPS) { |
55 |
const int length = inverse_index_map.length(); |
|
2006
f2d2f0f20063
6792421: assert(_bitMap->isMarked(addr+size-1),inconsistent Printezis mark)
jmasa
parents:
1
diff
changeset
|
56 |
constantPoolCacheOop cache = oopFactory::new_constantPoolCache(length, |
f2d2f0f20063
6792421: assert(_bitMap->isMarked(addr+size-1),inconsistent Printezis mark)
jmasa
parents:
1
diff
changeset
|
57 |
methodOopDesc::IsUnsafeConc, |
f2d2f0f20063
6792421: assert(_bitMap->isMarked(addr+size-1),inconsistent Printezis mark)
jmasa
parents:
1
diff
changeset
|
58 |
CHECK_(constantPoolCacheHandle())); |
1 | 59 |
cache->initialize(inverse_index_map); |
60 |
return constantPoolCacheHandle(THREAD, cache); |
|
61 |
} |
|
62 |
||
63 |
||
64 |
||
65 |
// The new finalization semantics says that registration of |
|
66 |
// finalizable objects must be performed on successful return from the |
|
67 |
// Object.<init> constructor. We could implement this trivially if |
|
68 |
// <init> were never rewritten but since JVMTI allows this to occur, a |
|
69 |
// more complicated solution is required. A special return bytecode |
|
70 |
// is used only by Object.<init> to signal the finalization |
|
71 |
// registration point. Additionally local 0 must be preserved so it's |
|
72 |
// available to pass to the registration function. For simplicty we |
|
73 |
// require that local 0 is never overwritten so it's available as an |
|
74 |
// argument for registration. |
|
75 |
||
76 |
void Rewriter::rewrite_Object_init(methodHandle method, TRAPS) { |
|
77 |
RawBytecodeStream bcs(method); |
|
78 |
while (!bcs.is_last_bytecode()) { |
|
79 |
Bytecodes::Code opcode = bcs.raw_next(); |
|
80 |
switch (opcode) { |
|
81 |
case Bytecodes::_return: *bcs.bcp() = Bytecodes::_return_register_finalizer; break; |
|
82 |
||
83 |
case Bytecodes::_istore: |
|
84 |
case Bytecodes::_lstore: |
|
85 |
case Bytecodes::_fstore: |
|
86 |
case Bytecodes::_dstore: |
|
87 |
case Bytecodes::_astore: |
|
88 |
if (bcs.get_index() != 0) continue; |
|
89 |
||
90 |
// fall through |
|
91 |
case Bytecodes::_istore_0: |
|
92 |
case Bytecodes::_lstore_0: |
|
93 |
case Bytecodes::_fstore_0: |
|
94 |
case Bytecodes::_dstore_0: |
|
95 |
case Bytecodes::_astore_0: |
|
96 |
THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), |
|
97 |
"can't overwrite local 0 in Object.<init>"); |
|
98 |
break; |
|
99 |
} |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
||
104 |
// Rewrites a method given the index_map information |
|
105 |
methodHandle Rewriter::rewrite_method(methodHandle method, intArray& index_map, TRAPS) { |
|
106 |
||
107 |
int nof_jsrs = 0; |
|
108 |
bool has_monitor_bytecodes = false; |
|
109 |
||
110 |
{ |
|
111 |
// We cannot tolerate a GC in this block, because we've |
|
112 |
// cached the bytecodes in 'code_base'. If the methodOop |
|
113 |
// moves, the bytecodes will also move. |
|
114 |
No_Safepoint_Verifier nsv; |
|
115 |
Bytecodes::Code c; |
|
116 |
||
117 |
// Bytecodes and their length |
|
118 |
const address code_base = method->code_base(); |
|
119 |
const int code_length = method->code_size(); |
|
120 |
||
121 |
int bc_length; |
|
122 |
for (int bci = 0; bci < code_length; bci += bc_length) { |
|
123 |
address bcp = code_base + bci; |
|
124 |
c = (Bytecodes::Code)(*bcp); |
|
125 |
||
126 |
// Since we have the code, see if we can get the length |
|
127 |
// directly. Some more complicated bytecodes will report |
|
128 |
// a length of zero, meaning we need to make another method |
|
129 |
// call to calculate the length. |
|
130 |
bc_length = Bytecodes::length_for(c); |
|
131 |
if (bc_length == 0) { |
|
132 |
bc_length = Bytecodes::length_at(bcp); |
|
133 |
||
134 |
// length_at will put us at the bytecode after the one modified |
|
135 |
// by 'wide'. We don't currently examine any of the bytecodes |
|
136 |
// modified by wide, but in case we do in the future... |
|
137 |
if (c == Bytecodes::_wide) { |
|
138 |
c = (Bytecodes::Code)bcp[1]; |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
assert(bc_length != 0, "impossible bytecode length"); |
|
143 |
||
144 |
switch (c) { |
|
145 |
case Bytecodes::_lookupswitch : { |
|
146 |
#ifndef CC_INTERP |
|
147 |
Bytecode_lookupswitch* bc = Bytecode_lookupswitch_at(bcp); |
|
148 |
bc->set_code( |
|
149 |
bc->number_of_pairs() < BinarySwitchThreshold |
|
150 |
? Bytecodes::_fast_linearswitch |
|
151 |
: Bytecodes::_fast_binaryswitch |
|
152 |
); |
|
153 |
#endif |
|
154 |
break; |
|
155 |
} |
|
156 |
case Bytecodes::_getstatic : // fall through |
|
157 |
case Bytecodes::_putstatic : // fall through |
|
158 |
case Bytecodes::_getfield : // fall through |
|
159 |
case Bytecodes::_putfield : // fall through |
|
160 |
case Bytecodes::_invokevirtual : // fall through |
|
161 |
case Bytecodes::_invokespecial : // fall through |
|
162 |
case Bytecodes::_invokestatic : // fall through |
|
163 |
case Bytecodes::_invokeinterface: { |
|
164 |
address p = bcp + 1; |
|
165 |
Bytes::put_native_u2(p, index_map[Bytes::get_Java_u2(p)]); |
|
166 |
break; |
|
167 |
} |
|
168 |
case Bytecodes::_jsr : // fall through |
|
169 |
case Bytecodes::_jsr_w : nof_jsrs++; break; |
|
170 |
case Bytecodes::_monitorenter : // fall through |
|
171 |
case Bytecodes::_monitorexit : has_monitor_bytecodes = true; break; |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
// Update access flags |
|
177 |
if (has_monitor_bytecodes) { |
|
178 |
method->set_has_monitor_bytecodes(); |
|
179 |
} |
|
180 |
||
181 |
// The present of a jsr bytecode implies that the method might potentially |
|
182 |
// have to be rewritten, so we run the oopMapGenerator on the method |
|
183 |
if (nof_jsrs > 0) { |
|
184 |
method->set_has_jsrs(); |
|
185 |
ResolveOopMapConflicts romc(method); |
|
186 |
methodHandle original_method = method; |
|
187 |
method = romc.do_potential_rewrite(CHECK_(methodHandle())); |
|
188 |
if (method() != original_method()) { |
|
189 |
// Insert invalid bytecode into original methodOop and set |
|
190 |
// interpreter entrypoint, so that a executing this method |
|
191 |
// will manifest itself in an easy recognizable form. |
|
192 |
address bcp = original_method->bcp_from(0); |
|
193 |
*bcp = (u1)Bytecodes::_shouldnotreachhere; |
|
194 |
int kind = Interpreter::method_kind(original_method); |
|
195 |
original_method->set_interpreter_kind(kind); |
|
196 |
} |
|
197 |
||
198 |
// Update monitor matching info. |
|
199 |
if (romc.monitor_safe()) { |
|
200 |
method->set_guaranteed_monitor_matching(); |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
// Setup method entrypoints for compiler and interpreter |
|
205 |
method->link_method(method, CHECK_(methodHandle())); |
|
206 |
||
207 |
return method; |
|
208 |
} |
|
209 |
||
210 |
||
211 |
void Rewriter::rewrite(instanceKlassHandle klass, TRAPS) { |
|
212 |
// gather starting points |
|
213 |
ResourceMark rm(THREAD); |
|
214 |
constantPoolHandle pool (THREAD, klass->constants()); |
|
215 |
objArrayHandle methods (THREAD, klass->methods()); |
|
216 |
assert(pool->cache() == NULL, "constant pool cache must not be set yet"); |
|
217 |
||
218 |
// determine index maps for methodOop rewriting |
|
219 |
intArray* index_map = NULL; |
|
220 |
intStack* inverse_index_map = NULL; |
|
221 |
compute_index_maps(pool, index_map, inverse_index_map); |
|
222 |
||
223 |
// allocate constant pool cache |
|
224 |
constantPoolCacheHandle cache = new_constant_pool_cache(*inverse_index_map, CHECK); |
|
225 |
pool->set_cache(cache()); |
|
226 |
cache->set_constant_pool(pool()); |
|
227 |
||
228 |
if (RegisterFinalizersAtInit && klass->name() == vmSymbols::java_lang_Object()) { |
|
229 |
int i = methods->length(); |
|
230 |
while (i-- > 0) { |
|
231 |
methodOop method = (methodOop)methods->obj_at(i); |
|
232 |
if (method->intrinsic_id() == vmIntrinsics::_Object_init) { |
|
233 |
// rewrite the return bytecodes of Object.<init> to register the |
|
234 |
// object for finalization if needed. |
|
235 |
methodHandle m(THREAD, method); |
|
236 |
rewrite_Object_init(m, CHECK); |
|
237 |
break; |
|
238 |
} |
|
239 |
} |
|
240 |
} |
|
241 |
||
242 |
// rewrite methods |
|
243 |
{ int i = methods->length(); |
|
244 |
while (i-- > 0) { |
|
245 |
methodHandle m(THREAD, (methodOop)methods->obj_at(i)); |
|
246 |
m = rewrite_method(m, *index_map, CHECK); |
|
247 |
// Method might have gotten rewritten. |
|
248 |
methods->obj_at_put(i, m()); |
|
249 |
} |
|
250 |
} |
|
251 |
} |