Print this page
rev 1021 : 6858164: invokedynamic code needs some cleanup (post-6655638)
Note: The bug ID for this change set was erroneously used to call for review of 6815692.
Summary: Fix several crashers, remove needless paths for boxed-style bootstrap method call, refactor & simplify APIs for rewriter constantPoolOop, remove sun.dyn.CallSiteImpl
Reviewed-by: ?
rev 1026 : imported patch indy.compiler.inline.patch
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/vm/oops/generateOopMap.cpp
+++ new/src/share/vm/oops/generateOopMap.cpp
1 1 /*
2 2 * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 21 * have any questions.
22 22 *
23 23 */
24 24
25 25 //
26 26 //
27 27 // Compute stack layouts for each instruction in method.
28 28 //
29 29 // Problems:
30 30 // - What to do about jsr with different types of local vars?
31 31 // Need maps that are conditional on jsr path?
32 32 // - Jsr and exceptions should be done more efficiently (the retAddr stuff)
33 33 //
34 34 // Alternative:
35 35 // - Could extend verifier to provide this information.
36 36 // For: one fewer abstract interpreter to maintain. Against: the verifier
37 37 // solves a bigger problem so slower (undesirable to force verification of
38 38 // everything?).
39 39 //
40 40 // Algorithm:
41 41 // Partition bytecodes into basic blocks
42 42 // For each basic block: store entry state (vars, stack). For instructions
43 43 // inside basic blocks we do not store any state (instead we recompute it
44 44 // from state produced by previous instruction).
45 45 //
46 46 // Perform abstract interpretation of bytecodes over this lattice:
47 47 //
48 48 // _--'#'--_
49 49 // / / \ \
50 50 // / / \ \
51 51 // / | | \
52 52 // 'r' 'v' 'p' ' '
53 53 // \ | | /
54 54 // \ \ / /
55 55 // \ \ / /
56 56 // -- '@' --
57 57 //
58 58 // '#' top, result of conflict merge
59 59 // 'r' reference type
60 60 // 'v' value type
61 61 // 'p' pc type for jsr/ret
62 62 // ' ' uninitialized; never occurs on operand stack in Java
63 63 // '@' bottom/unexecuted; initial state each bytecode.
64 64 //
65 65 // Basic block headers are the only merge points. We use this iteration to
66 66 // compute the information:
67 67 //
68 68 // find basic blocks;
69 69 // initialize them with uninitialized state;
70 70 // initialize first BB according to method signature;
71 71 // mark first BB changed
72 72 // while (some BB is changed) do {
73 73 // perform abstract interpration of all bytecodes in BB;
74 74 // merge exit state of BB into entry state of all successor BBs,
75 75 // noting if any of these change;
76 76 // }
77 77 //
78 78 // One additional complication is necessary. The jsr instruction pushes
79 79 // a return PC on the stack (a 'p' type in the abstract interpretation).
80 80 // To be able to process "ret" bytecodes, we keep track of these return
81 81 // PC's in a 'retAddrs' structure in abstract interpreter context (when
82 82 // processing a "ret" bytecodes, it is not sufficient to know that it gets
83 83 // an argument of the right type 'p'; we need to know which address it
84 84 // returns to).
85 85 //
86 86 // (Note this comment is borrowed form the original author of the algorithm)
87 87
88 88 #include "incls/_precompiled.incl"
89 89 #include "incls/_generateOopMap.cpp.incl"
90 90
91 91 // ComputeCallStack
92 92 //
93 93 // Specialization of SignatureIterator - compute the effects of a call
94 94 //
95 95 class ComputeCallStack : public SignatureIterator {
96 96 CellTypeState *_effect;
97 97 int _idx;
98 98
99 99 void setup();
100 100 void set(CellTypeState state) { _effect[_idx++] = state; }
101 101 int length() { return _idx; };
102 102
103 103 virtual void do_bool () { set(CellTypeState::value); };
104 104 virtual void do_char () { set(CellTypeState::value); };
105 105 virtual void do_float () { set(CellTypeState::value); };
106 106 virtual void do_byte () { set(CellTypeState::value); };
107 107 virtual void do_short () { set(CellTypeState::value); };
108 108 virtual void do_int () { set(CellTypeState::value); };
109 109 virtual void do_void () { set(CellTypeState::bottom);};
110 110 virtual void do_object(int begin, int end) { set(CellTypeState::ref); };
111 111 virtual void do_array (int begin, int end) { set(CellTypeState::ref); };
112 112
113 113 void do_double() { set(CellTypeState::value);
114 114 set(CellTypeState::value); }
115 115 void do_long () { set(CellTypeState::value);
116 116 set(CellTypeState::value); }
117 117
118 118 public:
119 119 ComputeCallStack(symbolOop signature) : SignatureIterator(signature) {};
120 120
121 121 // Compute methods
122 122 int compute_for_parameters(bool is_static, CellTypeState *effect) {
123 123 _idx = 0;
124 124 _effect = effect;
125 125
126 126 if (!is_static)
127 127 effect[_idx++] = CellTypeState::ref;
128 128
129 129 iterate_parameters();
130 130
131 131 return length();
132 132 };
133 133
134 134 int compute_for_returntype(CellTypeState *effect) {
135 135 _idx = 0;
136 136 _effect = effect;
137 137 iterate_returntype();
138 138 set(CellTypeState::bottom); // Always terminate with a bottom state, so ppush works
139 139
140 140 return length();
141 141 }
142 142 };
143 143
144 144 //=========================================================================================
145 145 // ComputeEntryStack
146 146 //
147 147 // Specialization of SignatureIterator - in order to set up first stack frame
148 148 //
149 149 class ComputeEntryStack : public SignatureIterator {
150 150 CellTypeState *_effect;
151 151 int _idx;
152 152
153 153 void setup();
154 154 void set(CellTypeState state) { _effect[_idx++] = state; }
155 155 int length() { return _idx; };
156 156
157 157 virtual void do_bool () { set(CellTypeState::value); };
158 158 virtual void do_char () { set(CellTypeState::value); };
159 159 virtual void do_float () { set(CellTypeState::value); };
160 160 virtual void do_byte () { set(CellTypeState::value); };
161 161 virtual void do_short () { set(CellTypeState::value); };
162 162 virtual void do_int () { set(CellTypeState::value); };
163 163 virtual void do_void () { set(CellTypeState::bottom);};
164 164 virtual void do_object(int begin, int end) { set(CellTypeState::make_slot_ref(_idx)); }
165 165 virtual void do_array (int begin, int end) { set(CellTypeState::make_slot_ref(_idx)); }
166 166
167 167 void do_double() { set(CellTypeState::value);
168 168 set(CellTypeState::value); }
169 169 void do_long () { set(CellTypeState::value);
170 170 set(CellTypeState::value); }
171 171
172 172 public:
173 173 ComputeEntryStack(symbolOop signature) : SignatureIterator(signature) {};
174 174
175 175 // Compute methods
176 176 int compute_for_parameters(bool is_static, CellTypeState *effect) {
177 177 _idx = 0;
178 178 _effect = effect;
179 179
180 180 if (!is_static)
181 181 effect[_idx++] = CellTypeState::make_slot_ref(0);
182 182
183 183 iterate_parameters();
184 184
185 185 return length();
186 186 };
187 187
188 188 int compute_for_returntype(CellTypeState *effect) {
189 189 _idx = 0;
190 190 _effect = effect;
191 191 iterate_returntype();
192 192 set(CellTypeState::bottom); // Always terminate with a bottom state, so ppush works
193 193
194 194 return length();
195 195 }
196 196 };
197 197
198 198 //=====================================================================================
199 199 //
200 200 // Implementation of RetTable/RetTableEntry
201 201 //
202 202 // Contains function to itereate through all bytecodes
203 203 // and find all return entry points
204 204 //
205 205 int RetTable::_init_nof_entries = 10;
206 206 int RetTableEntry::_init_nof_jsrs = 5;
207 207
208 208 void RetTableEntry::add_delta(int bci, int delta) {
209 209 if (_target_bci > bci) _target_bci += delta;
210 210
211 211 for (int k = 0; k < _jsrs->length(); k++) {
212 212 int jsr = _jsrs->at(k);
213 213 if (jsr > bci) _jsrs->at_put(k, jsr+delta);
214 214 }
215 215 }
216 216
217 217 void RetTable::compute_ret_table(methodHandle method) {
218 218 BytecodeStream i(method);
219 219 Bytecodes::Code bytecode;
220 220
221 221 while( (bytecode = i.next()) >= 0) {
222 222 switch (bytecode) {
223 223 case Bytecodes::_jsr:
224 224 add_jsr(i.next_bci(), i.dest());
225 225 break;
226 226 case Bytecodes::_jsr_w:
227 227 add_jsr(i.next_bci(), i.dest_w());
228 228 break;
229 229 }
230 230 }
231 231 }
232 232
233 233 void RetTable::add_jsr(int return_bci, int target_bci) {
234 234 RetTableEntry* entry = _first;
235 235
236 236 // Scan table for entry
237 237 for (;entry && entry->target_bci() != target_bci; entry = entry->next());
238 238
239 239 if (!entry) {
240 240 // Allocate new entry and put in list
241 241 entry = new RetTableEntry(target_bci, _first);
242 242 _first = entry;
243 243 }
244 244
245 245 // Now "entry" is set. Make sure that the entry is initialized
246 246 // and has room for the new jsr.
247 247 entry->add_jsr(return_bci);
248 248 }
249 249
250 250 RetTableEntry* RetTable::find_jsrs_for_target(int targBci) {
251 251 RetTableEntry *cur = _first;
252 252
253 253 while(cur) {
254 254 assert(cur->target_bci() != -1, "sanity check");
255 255 if (cur->target_bci() == targBci) return cur;
256 256 cur = cur->next();
257 257 }
258 258 ShouldNotReachHere();
259 259 return NULL;
260 260 }
261 261
262 262 // The instruction at bci is changing size by "delta". Update the return map.
263 263 void RetTable::update_ret_table(int bci, int delta) {
264 264 RetTableEntry *cur = _first;
265 265 while(cur) {
266 266 cur->add_delta(bci, delta);
267 267 cur = cur->next();
268 268 }
269 269 }
270 270
271 271 //
272 272 // Celltype state
273 273 //
274 274
275 275 CellTypeState CellTypeState::bottom = CellTypeState::make_bottom();
276 276 CellTypeState CellTypeState::uninit = CellTypeState::make_any(uninit_value);
277 277 CellTypeState CellTypeState::ref = CellTypeState::make_any(ref_conflict);
278 278 CellTypeState CellTypeState::value = CellTypeState::make_any(val_value);
279 279 CellTypeState CellTypeState::refUninit = CellTypeState::make_any(ref_conflict | uninit_value);
280 280 CellTypeState CellTypeState::top = CellTypeState::make_top();
281 281 CellTypeState CellTypeState::addr = CellTypeState::make_any(addr_conflict);
282 282
283 283 // Commonly used constants
284 284 static CellTypeState epsilonCTS[1] = { CellTypeState::bottom };
285 285 static CellTypeState refCTS = CellTypeState::ref;
286 286 static CellTypeState valCTS = CellTypeState::value;
287 287 static CellTypeState vCTS[2] = { CellTypeState::value, CellTypeState::bottom };
288 288 static CellTypeState rCTS[2] = { CellTypeState::ref, CellTypeState::bottom };
289 289 static CellTypeState rrCTS[3] = { CellTypeState::ref, CellTypeState::ref, CellTypeState::bottom };
290 290 static CellTypeState vrCTS[3] = { CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
291 291 static CellTypeState vvCTS[3] = { CellTypeState::value, CellTypeState::value, CellTypeState::bottom };
292 292 static CellTypeState rvrCTS[4] = { CellTypeState::ref, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
293 293 static CellTypeState vvrCTS[4] = { CellTypeState::value, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
294 294 static CellTypeState vvvCTS[4] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::bottom };
295 295 static CellTypeState vvvrCTS[5] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
296 296 static CellTypeState vvvvCTS[5] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::bottom };
297 297
298 298 char CellTypeState::to_char() const {
299 299 if (can_be_reference()) {
300 300 if (can_be_value() || can_be_address())
301 301 return '#'; // Conflict that needs to be rewritten
302 302 else
303 303 return 'r';
304 304 } else if (can_be_value())
305 305 return 'v';
306 306 else if (can_be_address())
307 307 return 'p';
308 308 else if (can_be_uninit())
309 309 return ' ';
310 310 else
311 311 return '@';
312 312 }
313 313
314 314
315 315 // Print a detailed CellTypeState. Indicate all bits that are set. If
316 316 // the CellTypeState represents an address or a reference, print the
317 317 // value of the additional information.
318 318 void CellTypeState::print(outputStream *os) {
319 319 if (can_be_address()) {
320 320 os->print("(p");
321 321 } else {
322 322 os->print("( ");
323 323 }
324 324 if (can_be_reference()) {
325 325 os->print("r");
326 326 } else {
327 327 os->print(" ");
328 328 }
329 329 if (can_be_value()) {
330 330 os->print("v");
331 331 } else {
332 332 os->print(" ");
333 333 }
334 334 if (can_be_uninit()) {
335 335 os->print("u|");
336 336 } else {
337 337 os->print(" |");
338 338 }
339 339 if (is_info_top()) {
340 340 os->print("Top)");
341 341 } else if (is_info_bottom()) {
342 342 os->print("Bot)");
343 343 } else {
344 344 if (is_reference()) {
345 345 int info = get_info();
346 346 int data = info & ~(ref_not_lock_bit | ref_slot_bit);
347 347 if (info & ref_not_lock_bit) {
348 348 // Not a monitor lock reference.
349 349 if (info & ref_slot_bit) {
350 350 // slot
351 351 os->print("slot%d)", data);
352 352 } else {
353 353 // line
354 354 os->print("line%d)", data);
355 355 }
356 356 } else {
357 357 // lock
358 358 os->print("lock%d)", data);
359 359 }
360 360 } else {
361 361 os->print("%d)", get_info());
362 362 }
363 363 }
364 364 }
365 365
366 366 //
367 367 // Basicblock handling methods
368 368 //
369 369
370 370 void GenerateOopMap ::initialize_bb() {
371 371 _gc_points = 0;
372 372 _bb_count = 0;
373 373 _bb_hdr_bits.clear();
374 374 _bb_hdr_bits.resize(method()->code_size());
375 375 }
376 376
377 377 void GenerateOopMap::bb_mark_fct(GenerateOopMap *c, int bci, int *data) {
378 378 assert(bci>= 0 && bci < c->method()->code_size(), "index out of bounds");
379 379 if (c->is_bb_header(bci))
380 380 return;
381 381
382 382 if (TraceNewOopMapGeneration) {
383 383 tty->print_cr("Basicblock#%d begins at: %d", c->_bb_count, bci);
384 384 }
385 385 c->set_bbmark_bit(bci);
386 386 c->_bb_count++;
387 387 }
388 388
389 389
390 390 void GenerateOopMap::mark_bbheaders_and_count_gc_points() {
391 391 initialize_bb();
392 392
393 393 bool fellThrough = false; // False to get first BB marked.
394 394
395 395 // First mark all exception handlers as start of a basic-block
396 396 typeArrayOop excps = method()->exception_table();
397 397 for(int i = 0; i < excps->length(); i += 4) {
398 398 int handler_pc_idx = i+2;
399 399 bb_mark_fct(this, excps->int_at(handler_pc_idx), NULL);
400 400 }
401 401
402 402 // Then iterate through the code
403 403 BytecodeStream bcs(_method);
404 404 Bytecodes::Code bytecode;
405 405
406 406 while( (bytecode = bcs.next()) >= 0) {
407 407 int bci = bcs.bci();
408 408
409 409 if (!fellThrough)
410 410 bb_mark_fct(this, bci, NULL);
411 411
412 412 fellThrough = jump_targets_do(&bcs, &GenerateOopMap::bb_mark_fct, NULL);
413 413
414 414 /* We will also mark successors of jsr's as basic block headers. */
415 415 switch (bytecode) {
416 416 case Bytecodes::_jsr:
417 417 assert(!fellThrough, "should not happen");
418 418 bb_mark_fct(this, bci + Bytecodes::length_for(bytecode), NULL);
419 419 break;
420 420 case Bytecodes::_jsr_w:
421 421 assert(!fellThrough, "should not happen");
422 422 bb_mark_fct(this, bci + Bytecodes::length_for(bytecode), NULL);
423 423 break;
424 424 }
425 425
426 426 if (possible_gc_point(&bcs))
427 427 _gc_points++;
428 428 }
429 429 }
430 430
431 431 void GenerateOopMap::reachable_basicblock(GenerateOopMap *c, int bci, int *data) {
432 432 assert(bci>= 0 && bci < c->method()->code_size(), "index out of bounds");
433 433 BasicBlock* bb = c->get_basic_block_at(bci);
434 434 if (bb->is_dead()) {
435 435 bb->mark_as_alive();
436 436 *data = 1; // Mark basicblock as changed
437 437 }
438 438 }
439 439
440 440
441 441 void GenerateOopMap::mark_reachable_code() {
442 442 int change = 1; // int to get function pointers to work
443 443
444 444 // Mark entry basic block as alive and all exception handlers
445 445 _basic_blocks[0].mark_as_alive();
446 446 typeArrayOop excps = method()->exception_table();
447 447 for(int i = 0; i < excps->length(); i += 4) {
448 448 int handler_pc_idx = i+2;
449 449 BasicBlock *bb = get_basic_block_at(excps->int_at(handler_pc_idx));
450 450 // If block is not already alive (due to multiple exception handlers to same bb), then
451 451 // make it alive
452 452 if (bb->is_dead()) bb->mark_as_alive();
453 453 }
454 454
455 455 BytecodeStream bcs(_method);
456 456
457 457 // Iterate through all basic blocks until we reach a fixpoint
458 458 while (change) {
459 459 change = 0;
460 460
461 461 for (int i = 0; i < _bb_count; i++) {
462 462 BasicBlock *bb = &_basic_blocks[i];
463 463 if (bb->is_alive()) {
464 464 // Position bytecodestream at last bytecode in basicblock
465 465 bcs.set_start(bb->_end_bci);
466 466 bcs.next();
467 467 Bytecodes::Code bytecode = bcs.code();
468 468 int bci = bcs.bci();
469 469 assert(bci == bb->_end_bci, "wrong bci");
470 470
471 471 bool fell_through = jump_targets_do(&bcs, &GenerateOopMap::reachable_basicblock, &change);
472 472
473 473 // We will also mark successors of jsr's as alive.
474 474 switch (bytecode) {
475 475 case Bytecodes::_jsr:
476 476 case Bytecodes::_jsr_w:
477 477 assert(!fell_through, "should not happen");
478 478 reachable_basicblock(this, bci + Bytecodes::length_for(bytecode), &change);
479 479 break;
480 480 }
481 481 if (fell_through) {
482 482 // Mark successor as alive
483 483 if (bb[1].is_dead()) {
484 484 bb[1].mark_as_alive();
485 485 change = 1;
486 486 }
487 487 }
488 488 }
489 489 }
490 490 }
491 491 }
492 492
493 493 /* If the current instruction in "c" has no effect on control flow,
494 494 returns "true". Otherwise, calls "jmpFct" one or more times, with
495 495 "c", an appropriate "pcDelta", and "data" as arguments, then
496 496 returns "false". There is one exception: if the current
497 497 instruction is a "ret", returns "false" without calling "jmpFct".
498 498 Arrangements for tracking the control flow of a "ret" must be made
499 499 externally. */
500 500 bool GenerateOopMap::jump_targets_do(BytecodeStream *bcs, jmpFct_t jmpFct, int *data) {
501 501 int bci = bcs->bci();
502 502
503 503 switch (bcs->code()) {
504 504 case Bytecodes::_ifeq:
505 505 case Bytecodes::_ifne:
506 506 case Bytecodes::_iflt:
507 507 case Bytecodes::_ifge:
508 508 case Bytecodes::_ifgt:
509 509 case Bytecodes::_ifle:
510 510 case Bytecodes::_if_icmpeq:
511 511 case Bytecodes::_if_icmpne:
512 512 case Bytecodes::_if_icmplt:
513 513 case Bytecodes::_if_icmpge:
514 514 case Bytecodes::_if_icmpgt:
515 515 case Bytecodes::_if_icmple:
516 516 case Bytecodes::_if_acmpeq:
517 517 case Bytecodes::_if_acmpne:
518 518 case Bytecodes::_ifnull:
519 519 case Bytecodes::_ifnonnull:
520 520 (*jmpFct)(this, bcs->dest(), data);
521 521 (*jmpFct)(this, bci + 3, data);
522 522 break;
523 523
524 524 case Bytecodes::_goto:
525 525 (*jmpFct)(this, bcs->dest(), data);
526 526 break;
527 527 case Bytecodes::_goto_w:
528 528 (*jmpFct)(this, bcs->dest_w(), data);
529 529 break;
530 530 case Bytecodes::_tableswitch:
531 531 { Bytecode_tableswitch *tableswitch = Bytecode_tableswitch_at(bcs->bcp());
532 532 int len = tableswitch->length();
533 533
534 534 (*jmpFct)(this, bci + tableswitch->default_offset(), data); /* Default. jump address */
535 535 while (--len >= 0) {
536 536 (*jmpFct)(this, bci + tableswitch->dest_offset_at(len), data);
537 537 }
538 538 break;
539 539 }
540 540
541 541 case Bytecodes::_lookupswitch:
542 542 { Bytecode_lookupswitch *lookupswitch = Bytecode_lookupswitch_at(bcs->bcp());
543 543 int npairs = lookupswitch->number_of_pairs();
544 544 (*jmpFct)(this, bci + lookupswitch->default_offset(), data); /* Default. */
545 545 while(--npairs >= 0) {
546 546 LookupswitchPair *pair = lookupswitch->pair_at(npairs);
547 547 (*jmpFct)(this, bci + pair->offset(), data);
548 548 }
549 549 break;
550 550 }
551 551 case Bytecodes::_jsr:
552 552 assert(bcs->is_wide()==false, "sanity check");
553 553 (*jmpFct)(this, bcs->dest(), data);
554 554
555 555
556 556
557 557 break;
558 558 case Bytecodes::_jsr_w:
559 559 (*jmpFct)(this, bcs->dest_w(), data);
560 560 break;
561 561 case Bytecodes::_wide:
562 562 ShouldNotReachHere();
563 563 return true;
564 564 break;
565 565 case Bytecodes::_athrow:
566 566 case Bytecodes::_ireturn:
567 567 case Bytecodes::_lreturn:
568 568 case Bytecodes::_freturn:
569 569 case Bytecodes::_dreturn:
570 570 case Bytecodes::_areturn:
571 571 case Bytecodes::_return:
572 572 case Bytecodes::_ret:
573 573 break;
574 574 default:
575 575 return true;
576 576 }
577 577 return false;
578 578 }
579 579
580 580 /* Requires "pc" to be the head of a basic block; returns that basic
581 581 block. */
582 582 BasicBlock *GenerateOopMap::get_basic_block_at(int bci) const {
583 583 BasicBlock* bb = get_basic_block_containing(bci);
584 584 assert(bb->_bci == bci, "should have found BB");
585 585 return bb;
586 586 }
587 587
588 588 // Requires "pc" to be the start of an instruction; returns the basic
589 589 // block containing that instruction. */
590 590 BasicBlock *GenerateOopMap::get_basic_block_containing(int bci) const {
591 591 BasicBlock *bbs = _basic_blocks;
592 592 int lo = 0, hi = _bb_count - 1;
593 593
594 594 while (lo <= hi) {
595 595 int m = (lo + hi) / 2;
596 596 int mbci = bbs[m]._bci;
597 597 int nbci;
598 598
599 599 if ( m == _bb_count-1) {
600 600 assert( bci >= mbci && bci < method()->code_size(), "sanity check failed");
601 601 return bbs+m;
602 602 } else {
603 603 nbci = bbs[m+1]._bci;
604 604 }
605 605
606 606 if ( mbci <= bci && bci < nbci) {
607 607 return bbs+m;
608 608 } else if (mbci < bci) {
609 609 lo = m + 1;
610 610 } else {
611 611 assert(mbci > bci, "sanity check");
612 612 hi = m - 1;
613 613 }
614 614 }
615 615
616 616 fatal("should have found BB");
617 617 return NULL;
618 618 }
619 619
620 620 void GenerateOopMap::restore_state(BasicBlock *bb)
621 621 {
622 622 memcpy(_state, bb->_state, _state_len*sizeof(CellTypeState));
623 623 _stack_top = bb->_stack_top;
624 624 _monitor_top = bb->_monitor_top;
625 625 }
626 626
627 627 int GenerateOopMap::next_bb_start_pc(BasicBlock *bb) {
628 628 int bbNum = bb - _basic_blocks + 1;
629 629 if (bbNum == _bb_count)
630 630 return method()->code_size();
631 631
632 632 return _basic_blocks[bbNum]._bci;
633 633 }
634 634
635 635 //
636 636 // CellType handling methods
637 637 //
638 638
639 639 void GenerateOopMap::init_state() {
640 640 _state_len = _max_locals + _max_stack + _max_monitors;
641 641 _state = NEW_RESOURCE_ARRAY(CellTypeState, _state_len);
642 642 memset(_state, 0, _state_len * sizeof(CellTypeState));
643 643 _state_vec_buf = NEW_RESOURCE_ARRAY(char, MAX3(_max_locals, _max_stack, _max_monitors) + 1/*for null terminator char */);
644 644 }
645 645
646 646 void GenerateOopMap::make_context_uninitialized() {
647 647 CellTypeState* vs = vars();
648 648
649 649 for (int i = 0; i < _max_locals; i++)
650 650 vs[i] = CellTypeState::uninit;
651 651
652 652 _stack_top = 0;
653 653 _monitor_top = 0;
654 654 }
655 655
656 656 int GenerateOopMap::methodsig_to_effect(symbolOop signature, bool is_static, CellTypeState* effect) {
657 657 ComputeEntryStack ces(signature);
658 658 return ces.compute_for_parameters(is_static, effect);
659 659 }
660 660
661 661 // Return result of merging cts1 and cts2.
662 662 CellTypeState CellTypeState::merge(CellTypeState cts, int slot) const {
663 663 CellTypeState result;
664 664
665 665 assert(!is_bottom() && !cts.is_bottom(),
666 666 "merge of bottom values is handled elsewhere");
667 667
668 668 result._state = _state | cts._state;
669 669
670 670 // If the top bit is set, we don't need to do any more work.
671 671 if (!result.is_info_top()) {
672 672 assert((result.can_be_address() || result.can_be_reference()),
673 673 "only addresses and references have non-top info");
674 674
675 675 if (!equal(cts)) {
676 676 // The two values being merged are different. Raise to top.
677 677 if (result.is_reference()) {
678 678 result = CellTypeState::make_slot_ref(slot);
679 679 } else {
680 680 result._state |= info_conflict;
681 681 }
682 682 }
683 683 }
684 684 assert(result.is_valid_state(), "checking that CTS merge maintains legal state");
685 685
686 686 return result;
687 687 }
688 688
689 689 // Merge the variable state for locals and stack from cts into bbts.
690 690 bool GenerateOopMap::merge_local_state_vectors(CellTypeState* cts,
691 691 CellTypeState* bbts) {
692 692 int i;
693 693 int len = _max_locals + _stack_top;
694 694 bool change = false;
695 695
696 696 for (i = len - 1; i >= 0; i--) {
697 697 CellTypeState v = cts[i].merge(bbts[i], i);
698 698 change = change || !v.equal(bbts[i]);
699 699 bbts[i] = v;
700 700 }
701 701
702 702 return change;
703 703 }
704 704
705 705 // Merge the monitor stack state from cts into bbts.
706 706 bool GenerateOopMap::merge_monitor_state_vectors(CellTypeState* cts,
707 707 CellTypeState* bbts) {
708 708 bool change = false;
709 709 if (_max_monitors > 0 && _monitor_top != bad_monitors) {
710 710 // If there are no monitors in the program, or there has been
711 711 // a monitor matching error before this point in the program,
712 712 // then we do not merge in the monitor state.
713 713
714 714 int base = _max_locals + _max_stack;
715 715 int len = base + _monitor_top;
716 716 for (int i = len - 1; i >= base; i--) {
717 717 CellTypeState v = cts[i].merge(bbts[i], i);
718 718
719 719 // Can we prove that, when there has been a change, it will already
720 720 // have been detected at this point? That would make this equal
721 721 // check here unnecessary.
722 722 change = change || !v.equal(bbts[i]);
723 723 bbts[i] = v;
724 724 }
725 725 }
726 726
727 727 return change;
728 728 }
729 729
730 730 void GenerateOopMap::copy_state(CellTypeState *dst, CellTypeState *src) {
731 731 int len = _max_locals + _stack_top;
732 732 for (int i = 0; i < len; i++) {
733 733 if (src[i].is_nonlock_reference()) {
734 734 dst[i] = CellTypeState::make_slot_ref(i);
735 735 } else {
736 736 dst[i] = src[i];
737 737 }
738 738 }
739 739 if (_max_monitors > 0 && _monitor_top != bad_monitors) {
740 740 int base = _max_locals + _max_stack;
741 741 len = base + _monitor_top;
742 742 for (int i = base; i < len; i++) {
743 743 dst[i] = src[i];
744 744 }
745 745 }
746 746 }
747 747
748 748
749 749 // Merge the states for the current block and the next. As long as a
750 750 // block is reachable the locals and stack must be merged. If the
751 751 // stack heights don't match then this is a verification error and
752 752 // it's impossible to interpret the code. Simultaneously monitor
753 753 // states are being check to see if they nest statically. If monitor
754 754 // depths match up then their states are merged. Otherwise the
755 755 // mismatch is simply recorded and interpretation continues since
756 756 // monitor matching is purely informational and doesn't say anything
757 757 // about the correctness of the code.
758 758 void GenerateOopMap::merge_state_into_bb(BasicBlock *bb) {
759 759 assert(bb->is_alive(), "merging state into a dead basicblock");
760 760
761 761 if (_stack_top == bb->_stack_top) {
762 762 // always merge local state even if monitors don't match.
763 763 if (merge_local_state_vectors(_state, bb->_state)) {
764 764 bb->set_changed(true);
765 765 }
766 766 if (_monitor_top == bb->_monitor_top) {
767 767 // monitors still match so continue merging monitor states.
768 768 if (merge_monitor_state_vectors(_state, bb->_state)) {
769 769 bb->set_changed(true);
770 770 }
771 771 } else {
772 772 if (TraceMonitorMismatch) {
773 773 report_monitor_mismatch("monitor stack height merge conflict");
774 774 }
775 775 // When the monitor stacks are not matched, we set _monitor_top to
776 776 // bad_monitors. This signals that, from here on, the monitor stack cannot
777 777 // be trusted. In particular, monitorexit bytecodes may throw
778 778 // exceptions. We mark this block as changed so that the change
779 779 // propagates properly.
780 780 bb->_monitor_top = bad_monitors;
781 781 bb->set_changed(true);
782 782 _monitor_safe = false;
783 783 }
784 784 } else if (!bb->is_reachable()) {
785 785 // First time we look at this BB
786 786 copy_state(bb->_state, _state);
787 787 bb->_stack_top = _stack_top;
788 788 bb->_monitor_top = _monitor_top;
789 789 bb->set_changed(true);
790 790 } else {
791 791 verify_error("stack height conflict: %d vs. %d", _stack_top, bb->_stack_top);
792 792 }
793 793 }
794 794
795 795 void GenerateOopMap::merge_state(GenerateOopMap *gom, int bci, int* data) {
796 796 gom->merge_state_into_bb(gom->get_basic_block_at(bci));
797 797 }
798 798
799 799 void GenerateOopMap::set_var(int localNo, CellTypeState cts) {
800 800 assert(cts.is_reference() || cts.is_value() || cts.is_address(),
801 801 "wrong celltypestate");
802 802 if (localNo < 0 || localNo > _max_locals) {
803 803 verify_error("variable write error: r%d", localNo);
804 804 return;
805 805 }
806 806 vars()[localNo] = cts;
807 807 }
808 808
809 809 CellTypeState GenerateOopMap::get_var(int localNo) {
810 810 assert(localNo < _max_locals + _nof_refval_conflicts, "variable read error")
811 811 if (localNo < 0 || localNo > _max_locals) {
812 812 verify_error("variable read error: r%d", localNo);
813 813 return valCTS; // just to pick something;
814 814 }
815 815 return vars()[localNo];
816 816 }
817 817
818 818 CellTypeState GenerateOopMap::pop() {
819 819 if ( _stack_top <= 0) {
820 820 verify_error("stack underflow");
821 821 return valCTS; // just to pick something
822 822 }
823 823 return stack()[--_stack_top];
824 824 }
825 825
826 826 void GenerateOopMap::push(CellTypeState cts) {
827 827 if ( _stack_top >= _max_stack) {
828 828 verify_error("stack overflow");
829 829 return;
830 830 }
831 831 stack()[_stack_top++] = cts;
832 832 }
833 833
834 834 CellTypeState GenerateOopMap::monitor_pop() {
835 835 assert(_monitor_top != bad_monitors, "monitor_pop called on error monitor stack");
836 836 if (_monitor_top == 0) {
837 837 // We have detected a pop of an empty monitor stack.
838 838 _monitor_safe = false;
839 839 _monitor_top = bad_monitors;
840 840
841 841 if (TraceMonitorMismatch) {
842 842 report_monitor_mismatch("monitor stack underflow");
843 843 }
844 844 return CellTypeState::ref; // just to keep the analysis going.
845 845 }
846 846 return monitors()[--_monitor_top];
847 847 }
848 848
849 849 void GenerateOopMap::monitor_push(CellTypeState cts) {
850 850 assert(_monitor_top != bad_monitors, "monitor_push called on error monitor stack");
851 851 if (_monitor_top >= _max_monitors) {
852 852 // Some monitorenter is being executed more than once.
853 853 // This means that the monitor stack cannot be simulated.
854 854 _monitor_safe = false;
855 855 _monitor_top = bad_monitors;
856 856
857 857 if (TraceMonitorMismatch) {
858 858 report_monitor_mismatch("monitor stack overflow");
859 859 }
860 860 return;
861 861 }
862 862 monitors()[_monitor_top++] = cts;
863 863 }
864 864
865 865 //
866 866 // Interpretation handling methods
867 867 //
868 868
869 869 void GenerateOopMap::do_interpretation()
870 870 {
871 871 // "i" is just for debugging, so we can detect cases where this loop is
872 872 // iterated more than once.
873 873 int i = 0;
874 874 do {
875 875 #ifndef PRODUCT
876 876 if (TraceNewOopMapGeneration) {
877 877 tty->print("\n\nIteration #%d of do_interpretation loop, method:\n", i);
878 878 method()->print_name(tty);
879 879 tty->print("\n\n");
880 880 }
881 881 #endif
882 882 _conflict = false;
883 883 _monitor_safe = true;
884 884 // init_state is now called from init_basic_blocks. The length of a
885 885 // state vector cannot be determined until we have made a pass through
886 886 // the bytecodes counting the possible monitor entries.
887 887 if (!_got_error) init_basic_blocks();
888 888 if (!_got_error) setup_method_entry_state();
889 889 if (!_got_error) interp_all();
890 890 if (!_got_error) rewrite_refval_conflicts();
891 891 i++;
892 892 } while (_conflict && !_got_error);
893 893 }
894 894
895 895 void GenerateOopMap::init_basic_blocks() {
896 896 // Note: Could consider reserving only the needed space for each BB's state
897 897 // (entry stack may not be of maximal height for every basic block).
898 898 // But cumbersome since we don't know the stack heights yet. (Nor the
899 899 // monitor stack heights...)
900 900
901 901 _basic_blocks = NEW_RESOURCE_ARRAY(BasicBlock, _bb_count);
902 902
903 903 // Make a pass through the bytecodes. Count the number of monitorenters.
904 904 // This can be used an upper bound on the monitor stack depth in programs
905 905 // which obey stack discipline with their monitor usage. Initialize the
906 906 // known information about basic blocks.
907 907 BytecodeStream j(_method);
908 908 Bytecodes::Code bytecode;
909 909
910 910 int bbNo = 0;
911 911 int monitor_count = 0;
912 912 int prev_bci = -1;
913 913 while( (bytecode = j.next()) >= 0) {
914 914 if (j.code() == Bytecodes::_monitorenter) {
915 915 monitor_count++;
916 916 }
917 917
918 918 int bci = j.bci();
919 919 if (is_bb_header(bci)) {
920 920 // Initialize the basicblock structure
921 921 BasicBlock *bb = _basic_blocks + bbNo;
922 922 bb->_bci = bci;
923 923 bb->_max_locals = _max_locals;
924 924 bb->_max_stack = _max_stack;
925 925 bb->set_changed(false);
926 926 bb->_stack_top = BasicBlock::_dead_basic_block; // Initialize all basicblocks are dead.
927 927 bb->_monitor_top = bad_monitors;
928 928
929 929 if (bbNo > 0) {
930 930 _basic_blocks[bbNo - 1]._end_bci = prev_bci;
931 931 }
932 932
933 933 bbNo++;
934 934 }
935 935 // Remember prevous bci.
936 936 prev_bci = bci;
937 937 }
938 938 // Set
939 939 _basic_blocks[bbNo-1]._end_bci = prev_bci;
940 940
941 941
942 942 // Check that the correct number of basicblocks was found
943 943 if (bbNo !=_bb_count) {
944 944 if (bbNo < _bb_count) {
945 945 verify_error("jump into the middle of instruction?");
946 946 return;
947 947 } else {
948 948 verify_error("extra basic blocks - should not happen?");
949 949 return;
950 950 }
951 951 }
952 952
953 953 _max_monitors = monitor_count;
954 954
955 955 // Now that we have a bound on the depth of the monitor stack, we can
956 956 // initialize the CellTypeState-related information.
957 957 init_state();
958 958
959 959 // We allocate space for all state-vectors for all basicblocks in one huge chuck.
960 960 // Then in the next part of the code, we set a pointer in each _basic_block that
961 961 // points to each piece.
962 962 CellTypeState *basicBlockState = NEW_RESOURCE_ARRAY(CellTypeState, bbNo * _state_len);
963 963 memset(basicBlockState, 0, bbNo * _state_len * sizeof(CellTypeState));
964 964
965 965 // Make a pass over the basicblocks and assign their state vectors.
966 966 for (int blockNum=0; blockNum < bbNo; blockNum++) {
967 967 BasicBlock *bb = _basic_blocks + blockNum;
968 968 bb->_state = basicBlockState + blockNum * _state_len;
969 969
970 970 #ifdef ASSERT
971 971 if (blockNum + 1 < bbNo) {
972 972 address bcp = _method->bcp_from(bb->_end_bci);
973 973 int bc_len = Bytecodes::java_length_at(bcp);
974 974 assert(bb->_end_bci + bc_len == bb[1]._bci, "unmatched bci info in basicblock");
975 975 }
976 976 #endif
977 977 }
978 978 #ifdef ASSERT
979 979 { BasicBlock *bb = &_basic_blocks[bbNo-1];
980 980 address bcp = _method->bcp_from(bb->_end_bci);
981 981 int bc_len = Bytecodes::java_length_at(bcp);
982 982 assert(bb->_end_bci + bc_len == _method->code_size(), "wrong end bci");
983 983 }
984 984 #endif
985 985
986 986 // Mark all alive blocks
987 987 mark_reachable_code();
988 988 }
989 989
990 990 void GenerateOopMap::setup_method_entry_state() {
991 991
992 992 // Initialize all locals to 'uninit' and set stack-height to 0
993 993 make_context_uninitialized();
994 994
995 995 // Initialize CellState type of arguments
996 996 methodsig_to_effect(method()->signature(), method()->is_static(), vars());
997 997
998 998 // If some references must be pre-assigned to null, then set that up
999 999 initialize_vars();
1000 1000
1001 1001 // This is the start state
1002 1002 merge_state_into_bb(&_basic_blocks[0]);
1003 1003
1004 1004 assert(_basic_blocks[0].changed(), "we are not getting off the ground");
1005 1005 }
1006 1006
1007 1007 // The instruction at bci is changing size by "delta". Update the basic blocks.
1008 1008 void GenerateOopMap::update_basic_blocks(int bci, int delta,
1009 1009 int new_method_size) {
1010 1010 assert(new_method_size >= method()->code_size() + delta,
1011 1011 "new method size is too small");
1012 1012
1013 1013 BitMap::bm_word_t* new_bb_hdr_bits =
1014 1014 NEW_RESOURCE_ARRAY(BitMap::bm_word_t,
1015 1015 BitMap::word_align_up(new_method_size));
1016 1016 _bb_hdr_bits.set_map(new_bb_hdr_bits);
1017 1017 _bb_hdr_bits.set_size(new_method_size);
1018 1018 _bb_hdr_bits.clear();
1019 1019
1020 1020
1021 1021 for(int k = 0; k < _bb_count; k++) {
1022 1022 if (_basic_blocks[k]._bci > bci) {
1023 1023 _basic_blocks[k]._bci += delta;
1024 1024 _basic_blocks[k]._end_bci += delta;
1025 1025 }
1026 1026 _bb_hdr_bits.at_put(_basic_blocks[k]._bci, true);
1027 1027 }
1028 1028 }
1029 1029
1030 1030 //
1031 1031 // Initvars handling
1032 1032 //
1033 1033
1034 1034 void GenerateOopMap::initialize_vars() {
1035 1035 for (int k = 0; k < _init_vars->length(); k++)
1036 1036 _state[_init_vars->at(k)] = CellTypeState::make_slot_ref(k);
1037 1037 }
1038 1038
1039 1039 void GenerateOopMap::add_to_ref_init_set(int localNo) {
1040 1040
1041 1041 if (TraceNewOopMapGeneration)
1042 1042 tty->print_cr("Added init vars: %d", localNo);
1043 1043
1044 1044 // Is it already in the set?
1045 1045 if (_init_vars->contains(localNo) )
1046 1046 return;
1047 1047
1048 1048 _init_vars->append(localNo);
1049 1049 }
1050 1050
1051 1051 //
1052 1052 // Interpreration code
1053 1053 //
1054 1054
1055 1055 void GenerateOopMap::interp_all() {
1056 1056 bool change = true;
1057 1057
1058 1058 while (change && !_got_error) {
1059 1059 change = false;
1060 1060 for (int i = 0; i < _bb_count && !_got_error; i++) {
1061 1061 BasicBlock *bb = &_basic_blocks[i];
1062 1062 if (bb->changed()) {
1063 1063 if (_got_error) return;
1064 1064 change = true;
1065 1065 bb->set_changed(false);
1066 1066 interp_bb(bb);
1067 1067 }
1068 1068 }
1069 1069 }
1070 1070 }
1071 1071
1072 1072 void GenerateOopMap::interp_bb(BasicBlock *bb) {
1073 1073
1074 1074 // We do not want to do anything in case the basic-block has not been initialized. This
1075 1075 // will happen in the case where there is dead-code hang around in a method.
1076 1076 assert(bb->is_reachable(), "should be reachable or deadcode exist");
1077 1077 restore_state(bb);
1078 1078
1079 1079 BytecodeStream itr(_method);
1080 1080
1081 1081 // Set iterator interval to be the current basicblock
1082 1082 int lim_bci = next_bb_start_pc(bb);
1083 1083 itr.set_interval(bb->_bci, lim_bci);
1084 1084 assert(lim_bci != bb->_bci, "must be at least one instruction in a basicblock");
1085 1085 itr.next(); // read first instruction
1086 1086
1087 1087 // Iterates through all bytecodes except the last in a basic block.
1088 1088 // We handle the last one special, since there is controlflow change.
1089 1089 while(itr.next_bci() < lim_bci && !_got_error) {
1090 1090 if (_has_exceptions || _monitor_top != 0) {
1091 1091 // We do not need to interpret the results of exceptional
1092 1092 // continuation from this instruction when the method has no
1093 1093 // exception handlers and the monitor stack is currently
1094 1094 // empty.
1095 1095 do_exception_edge(&itr);
1096 1096 }
1097 1097 interp1(&itr);
1098 1098 itr.next();
1099 1099 }
1100 1100
1101 1101 // Handle last instruction.
1102 1102 if (!_got_error) {
1103 1103 assert(itr.next_bci() == lim_bci, "must point to end");
1104 1104 if (_has_exceptions || _monitor_top != 0) {
1105 1105 do_exception_edge(&itr);
1106 1106 }
1107 1107 interp1(&itr);
1108 1108
1109 1109 bool fall_through = jump_targets_do(&itr, GenerateOopMap::merge_state, NULL);
1110 1110 if (_got_error) return;
1111 1111
1112 1112 if (itr.code() == Bytecodes::_ret) {
1113 1113 assert(!fall_through, "cannot be set if ret instruction");
1114 1114 // Automatically handles 'wide' ret indicies
1115 1115 ret_jump_targets_do(&itr, GenerateOopMap::merge_state, itr.get_index(), NULL);
1116 1116 } else if (fall_through) {
1117 1117 // Hit end of BB, but the instr. was a fall-through instruction,
1118 1118 // so perform transition as if the BB ended in a "jump".
1119 1119 if (lim_bci != bb[1]._bci) {
1120 1120 verify_error("bytecodes fell through last instruction");
1121 1121 return;
1122 1122 }
1123 1123 merge_state_into_bb(bb + 1);
1124 1124 }
1125 1125 }
1126 1126 }
1127 1127
1128 1128 void GenerateOopMap::do_exception_edge(BytecodeStream* itr) {
1129 1129 // Only check exception edge, if bytecode can trap
1130 1130 if (!Bytecodes::can_trap(itr->code())) return;
1131 1131 switch (itr->code()) {
1132 1132 case Bytecodes::_aload_0:
1133 1133 // These bytecodes can trap for rewriting. We need to assume that
1134 1134 // they do not throw exceptions to make the monitor analysis work.
1135 1135 return;
1136 1136
1137 1137 case Bytecodes::_ireturn:
1138 1138 case Bytecodes::_lreturn:
1139 1139 case Bytecodes::_freturn:
1140 1140 case Bytecodes::_dreturn:
1141 1141 case Bytecodes::_areturn:
1142 1142 case Bytecodes::_return:
1143 1143 // If the monitor stack height is not zero when we leave the method,
1144 1144 // then we are either exiting with a non-empty stack or we have
1145 1145 // found monitor trouble earlier in our analysis. In either case,
1146 1146 // assume an exception could be taken here.
1147 1147 if (_monitor_top == 0) {
1148 1148 return;
1149 1149 }
1150 1150 break;
1151 1151
1152 1152 case Bytecodes::_monitorexit:
1153 1153 // If the monitor stack height is bad_monitors, then we have detected a
1154 1154 // monitor matching problem earlier in the analysis. If the
1155 1155 // monitor stack height is 0, we are about to pop a monitor
1156 1156 // off of an empty stack. In either case, the bytecode
1157 1157 // could throw an exception.
1158 1158 if (_monitor_top != bad_monitors && _monitor_top != 0) {
1159 1159 return;
1160 1160 }
1161 1161 break;
1162 1162 }
1163 1163
1164 1164 if (_has_exceptions) {
1165 1165 int bci = itr->bci();
1166 1166 typeArrayOop exct = method()->exception_table();
1167 1167 for(int i = 0; i< exct->length(); i+=4) {
1168 1168 int start_pc = exct->int_at(i);
1169 1169 int end_pc = exct->int_at(i+1);
1170 1170 int handler_pc = exct->int_at(i+2);
1171 1171 int catch_type = exct->int_at(i+3);
1172 1172
1173 1173 if (start_pc <= bci && bci < end_pc) {
1174 1174 BasicBlock *excBB = get_basic_block_at(handler_pc);
1175 1175 CellTypeState *excStk = excBB->stack();
1176 1176 CellTypeState *cOpStck = stack();
1177 1177 CellTypeState cOpStck_0 = cOpStck[0];
1178 1178 int cOpStackTop = _stack_top;
1179 1179
1180 1180 // Exception stacks are always the same.
1181 1181 assert(method()->max_stack() > 0, "sanity check");
1182 1182
1183 1183 // We remembered the size and first element of "cOpStck"
1184 1184 // above; now we temporarily set them to the appropriate
1185 1185 // values for an exception handler. */
1186 1186 cOpStck[0] = CellTypeState::make_slot_ref(_max_locals);
1187 1187 _stack_top = 1;
1188 1188
1189 1189 merge_state_into_bb(excBB);
1190 1190
1191 1191 // Now undo the temporary change.
1192 1192 cOpStck[0] = cOpStck_0;
1193 1193 _stack_top = cOpStackTop;
1194 1194
1195 1195 // If this is a "catch all" handler, then we do not need to
1196 1196 // consider any additional handlers.
1197 1197 if (catch_type == 0) {
1198 1198 return;
1199 1199 }
1200 1200 }
1201 1201 }
1202 1202 }
1203 1203
1204 1204 // It is possible that none of the exception handlers would have caught
1205 1205 // the exception. In this case, we will exit the method. We must
1206 1206 // ensure that the monitor stack is empty in this case.
1207 1207 if (_monitor_top == 0) {
1208 1208 return;
1209 1209 }
1210 1210
1211 1211 // We pessimistically assume that this exception can escape the
1212 1212 // method. (It is possible that it will always be caught, but
1213 1213 // we don't care to analyse the types of the catch clauses.)
1214 1214
1215 1215 // We don't set _monitor_top to bad_monitors because there are no successors
1216 1216 // to this exceptional exit.
1217 1217
1218 1218 if (TraceMonitorMismatch && _monitor_safe) {
1219 1219 // We check _monitor_safe so that we only report the first mismatched
1220 1220 // exceptional exit.
1221 1221 report_monitor_mismatch("non-empty monitor stack at exceptional exit");
1222 1222 }
1223 1223 _monitor_safe = false;
1224 1224
1225 1225 }
1226 1226
1227 1227 void GenerateOopMap::report_monitor_mismatch(const char *msg) {
1228 1228 #ifndef PRODUCT
1229 1229 tty->print(" Monitor mismatch in method ");
1230 1230 method()->print_short_name(tty);
1231 1231 tty->print_cr(": %s", msg);
1232 1232 #endif
1233 1233 }
1234 1234
1235 1235 void GenerateOopMap::print_states(outputStream *os,
1236 1236 CellTypeState* vec, int num) {
1237 1237 for (int i = 0; i < num; i++) {
1238 1238 vec[i].print(tty);
1239 1239 }
1240 1240 }
1241 1241
1242 1242 // Print the state values at the current bytecode.
1243 1243 void GenerateOopMap::print_current_state(outputStream *os,
1244 1244 BytecodeStream *currentBC,
1245 1245 bool detailed) {
1246 1246
1247 1247 if (detailed) {
1248 1248 os->print(" %4d vars = ", currentBC->bci());
1249 1249 print_states(os, vars(), _max_locals);
1250 1250 os->print(" %s", Bytecodes::name(currentBC->code()));
1251 1251 switch(currentBC->code()) {
1252 1252 case Bytecodes::_invokevirtual:
1253 1253 case Bytecodes::_invokespecial:
1254 1254 case Bytecodes::_invokestatic:
1255 1255 case Bytecodes::_invokedynamic:
1256 1256 case Bytecodes::_invokeinterface:
1257 1257 int idx = currentBC->get_index_int();
1258 1258 constantPoolOop cp = method()->constants();
1259 1259 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx);
1260 1260 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx);
1261 1261 symbolOop signature = cp->symbol_at(signatureIdx);
1262 1262 os->print("%s", signature->as_C_string());
1263 1263 }
1264 1264 os->cr();
1265 1265 os->print(" stack = ");
1266 1266 print_states(os, stack(), _stack_top);
1267 1267 os->cr();
1268 1268 if (_monitor_top != bad_monitors) {
1269 1269 os->print(" monitors = ");
1270 1270 print_states(os, monitors(), _monitor_top);
1271 1271 } else {
1272 1272 os->print(" [bad monitor stack]");
1273 1273 }
1274 1274 os->cr();
1275 1275 } else {
1276 1276 os->print(" %4d vars = '%s' ", currentBC->bci(), state_vec_to_string(vars(), _max_locals));
1277 1277 os->print(" stack = '%s' ", state_vec_to_string(stack(), _stack_top));
1278 1278 if (_monitor_top != bad_monitors) {
1279 1279 os->print(" monitors = '%s' \t%s", state_vec_to_string(monitors(), _monitor_top), Bytecodes::name(currentBC->code()));
1280 1280 } else {
1281 1281 os->print(" [bad monitor stack]");
1282 1282 }
1283 1283 switch(currentBC->code()) {
1284 1284 case Bytecodes::_invokevirtual:
1285 1285 case Bytecodes::_invokespecial:
1286 1286 case Bytecodes::_invokestatic:
1287 1287 case Bytecodes::_invokedynamic:
1288 1288 case Bytecodes::_invokeinterface:
1289 1289 int idx = currentBC->get_index_int();
1290 1290 constantPoolOop cp = method()->constants();
1291 1291 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx);
1292 1292 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx);
1293 1293 symbolOop signature = cp->symbol_at(signatureIdx);
1294 1294 os->print("%s", signature->as_C_string());
1295 1295 }
1296 1296 os->cr();
1297 1297 }
1298 1298 }
1299 1299
1300 1300 // Sets the current state to be the state after executing the
1301 1301 // current instruction, starting in the current state.
1302 1302 void GenerateOopMap::interp1(BytecodeStream *itr) {
1303 1303 if (TraceNewOopMapGeneration) {
1304 1304 print_current_state(tty, itr, TraceNewOopMapGenerationDetailed);
1305 1305 }
1306 1306
1307 1307 // Should we report the results? Result is reported *before* the instruction at the current bci is executed.
1308 1308 // However, not for calls. For calls we do not want to include the arguments, so we postpone the reporting until
1309 1309 // they have been popped (in method ppl).
1310 1310 if (_report_result == true) {
1311 1311 switch(itr->code()) {
1312 1312 case Bytecodes::_invokevirtual:
1313 1313 case Bytecodes::_invokespecial:
1314 1314 case Bytecodes::_invokestatic:
1315 1315 case Bytecodes::_invokedynamic:
1316 1316 case Bytecodes::_invokeinterface:
1317 1317 _itr_send = itr;
1318 1318 _report_result_for_send = true;
1319 1319 break;
1320 1320 default:
1321 1321 fill_stackmap_for_opcodes(itr, vars(), stack(), _stack_top);
1322 1322 break;
1323 1323 }
1324 1324 }
1325 1325
1326 1326 // abstract interpretation of current opcode
1327 1327 switch(itr->code()) {
1328 1328 case Bytecodes::_nop: break;
1329 1329 case Bytecodes::_goto: break;
1330 1330 case Bytecodes::_goto_w: break;
1331 1331 case Bytecodes::_iinc: break;
1332 1332 case Bytecodes::_return: do_return_monitor_check();
1333 1333 break;
1334 1334
1335 1335 case Bytecodes::_aconst_null:
1336 1336 case Bytecodes::_new: ppush1(CellTypeState::make_line_ref(itr->bci()));
1337 1337 break;
1338 1338
1339 1339 case Bytecodes::_iconst_m1:
1340 1340 case Bytecodes::_iconst_0:
1341 1341 case Bytecodes::_iconst_1:
1342 1342 case Bytecodes::_iconst_2:
1343 1343 case Bytecodes::_iconst_3:
1344 1344 case Bytecodes::_iconst_4:
1345 1345 case Bytecodes::_iconst_5:
1346 1346 case Bytecodes::_fconst_0:
1347 1347 case Bytecodes::_fconst_1:
1348 1348 case Bytecodes::_fconst_2:
1349 1349 case Bytecodes::_bipush:
1350 1350 case Bytecodes::_sipush: ppush1(valCTS); break;
1351 1351
1352 1352 case Bytecodes::_lconst_0:
1353 1353 case Bytecodes::_lconst_1:
1354 1354 case Bytecodes::_dconst_0:
1355 1355 case Bytecodes::_dconst_1: ppush(vvCTS); break;
1356 1356
1357 1357 case Bytecodes::_ldc2_w: ppush(vvCTS); break;
1358 1358
1359 1359 case Bytecodes::_ldc: do_ldc(itr->get_index(), itr->bci()); break;
1360 1360 case Bytecodes::_ldc_w: do_ldc(itr->get_index_big(), itr->bci());break;
1361 1361
1362 1362 case Bytecodes::_iload:
1363 1363 case Bytecodes::_fload: ppload(vCTS, itr->get_index()); break;
1364 1364
1365 1365 case Bytecodes::_lload:
1366 1366 case Bytecodes::_dload: ppload(vvCTS,itr->get_index()); break;
1367 1367
1368 1368 case Bytecodes::_aload: ppload(rCTS, itr->get_index()); break;
1369 1369
1370 1370 case Bytecodes::_iload_0:
1371 1371 case Bytecodes::_fload_0: ppload(vCTS, 0); break;
1372 1372 case Bytecodes::_iload_1:
1373 1373 case Bytecodes::_fload_1: ppload(vCTS, 1); break;
1374 1374 case Bytecodes::_iload_2:
1375 1375 case Bytecodes::_fload_2: ppload(vCTS, 2); break;
1376 1376 case Bytecodes::_iload_3:
1377 1377 case Bytecodes::_fload_3: ppload(vCTS, 3); break;
1378 1378
1379 1379 case Bytecodes::_lload_0:
1380 1380 case Bytecodes::_dload_0: ppload(vvCTS, 0); break;
1381 1381 case Bytecodes::_lload_1:
1382 1382 case Bytecodes::_dload_1: ppload(vvCTS, 1); break;
1383 1383 case Bytecodes::_lload_2:
1384 1384 case Bytecodes::_dload_2: ppload(vvCTS, 2); break;
1385 1385 case Bytecodes::_lload_3:
1386 1386 case Bytecodes::_dload_3: ppload(vvCTS, 3); break;
1387 1387
1388 1388 case Bytecodes::_aload_0: ppload(rCTS, 0); break;
1389 1389 case Bytecodes::_aload_1: ppload(rCTS, 1); break;
1390 1390 case Bytecodes::_aload_2: ppload(rCTS, 2); break;
1391 1391 case Bytecodes::_aload_3: ppload(rCTS, 3); break;
1392 1392
1393 1393 case Bytecodes::_iaload:
1394 1394 case Bytecodes::_faload:
1395 1395 case Bytecodes::_baload:
1396 1396 case Bytecodes::_caload:
1397 1397 case Bytecodes::_saload: pp(vrCTS, vCTS); break;
1398 1398
1399 1399 case Bytecodes::_laload: pp(vrCTS, vvCTS); break;
1400 1400 case Bytecodes::_daload: pp(vrCTS, vvCTS); break;
1401 1401
1402 1402 case Bytecodes::_aaload: pp_new_ref(vrCTS, itr->bci()); break;
1403 1403
1404 1404 case Bytecodes::_istore:
1405 1405 case Bytecodes::_fstore: ppstore(vCTS, itr->get_index()); break;
1406 1406
1407 1407 case Bytecodes::_lstore:
1408 1408 case Bytecodes::_dstore: ppstore(vvCTS, itr->get_index()); break;
1409 1409
1410 1410 case Bytecodes::_astore: do_astore(itr->get_index()); break;
1411 1411
1412 1412 case Bytecodes::_istore_0:
1413 1413 case Bytecodes::_fstore_0: ppstore(vCTS, 0); break;
1414 1414 case Bytecodes::_istore_1:
1415 1415 case Bytecodes::_fstore_1: ppstore(vCTS, 1); break;
1416 1416 case Bytecodes::_istore_2:
1417 1417 case Bytecodes::_fstore_2: ppstore(vCTS, 2); break;
1418 1418 case Bytecodes::_istore_3:
1419 1419 case Bytecodes::_fstore_3: ppstore(vCTS, 3); break;
1420 1420
1421 1421 case Bytecodes::_lstore_0:
1422 1422 case Bytecodes::_dstore_0: ppstore(vvCTS, 0); break;
1423 1423 case Bytecodes::_lstore_1:
1424 1424 case Bytecodes::_dstore_1: ppstore(vvCTS, 1); break;
1425 1425 case Bytecodes::_lstore_2:
1426 1426 case Bytecodes::_dstore_2: ppstore(vvCTS, 2); break;
1427 1427 case Bytecodes::_lstore_3:
1428 1428 case Bytecodes::_dstore_3: ppstore(vvCTS, 3); break;
1429 1429
1430 1430 case Bytecodes::_astore_0: do_astore(0); break;
1431 1431 case Bytecodes::_astore_1: do_astore(1); break;
1432 1432 case Bytecodes::_astore_2: do_astore(2); break;
1433 1433 case Bytecodes::_astore_3: do_astore(3); break;
1434 1434
1435 1435 case Bytecodes::_iastore:
1436 1436 case Bytecodes::_fastore:
1437 1437 case Bytecodes::_bastore:
1438 1438 case Bytecodes::_castore:
1439 1439 case Bytecodes::_sastore: ppop(vvrCTS); break;
1440 1440 case Bytecodes::_lastore:
1441 1441 case Bytecodes::_dastore: ppop(vvvrCTS); break;
1442 1442 case Bytecodes::_aastore: ppop(rvrCTS); break;
1443 1443
1444 1444 case Bytecodes::_pop: ppop_any(1); break;
1445 1445 case Bytecodes::_pop2: ppop_any(2); break;
1446 1446
1447 1447 case Bytecodes::_dup: ppdupswap(1, "11"); break;
1448 1448 case Bytecodes::_dup_x1: ppdupswap(2, "121"); break;
1449 1449 case Bytecodes::_dup_x2: ppdupswap(3, "1321"); break;
1450 1450 case Bytecodes::_dup2: ppdupswap(2, "2121"); break;
1451 1451 case Bytecodes::_dup2_x1: ppdupswap(3, "21321"); break;
1452 1452 case Bytecodes::_dup2_x2: ppdupswap(4, "214321"); break;
1453 1453 case Bytecodes::_swap: ppdupswap(2, "12"); break;
1454 1454
1455 1455 case Bytecodes::_iadd:
1456 1456 case Bytecodes::_fadd:
1457 1457 case Bytecodes::_isub:
1458 1458 case Bytecodes::_fsub:
1459 1459 case Bytecodes::_imul:
1460 1460 case Bytecodes::_fmul:
1461 1461 case Bytecodes::_idiv:
1462 1462 case Bytecodes::_fdiv:
1463 1463 case Bytecodes::_irem:
1464 1464 case Bytecodes::_frem:
1465 1465 case Bytecodes::_ishl:
1466 1466 case Bytecodes::_ishr:
1467 1467 case Bytecodes::_iushr:
1468 1468 case Bytecodes::_iand:
1469 1469 case Bytecodes::_ior:
1470 1470 case Bytecodes::_ixor:
1471 1471 case Bytecodes::_l2f:
1472 1472 case Bytecodes::_l2i:
1473 1473 case Bytecodes::_d2f:
1474 1474 case Bytecodes::_d2i:
1475 1475 case Bytecodes::_fcmpl:
1476 1476 case Bytecodes::_fcmpg: pp(vvCTS, vCTS); break;
1477 1477
1478 1478 case Bytecodes::_ladd:
1479 1479 case Bytecodes::_dadd:
1480 1480 case Bytecodes::_lsub:
1481 1481 case Bytecodes::_dsub:
1482 1482 case Bytecodes::_lmul:
1483 1483 case Bytecodes::_dmul:
1484 1484 case Bytecodes::_ldiv:
1485 1485 case Bytecodes::_ddiv:
1486 1486 case Bytecodes::_lrem:
1487 1487 case Bytecodes::_drem:
1488 1488 case Bytecodes::_land:
1489 1489 case Bytecodes::_lor:
1490 1490 case Bytecodes::_lxor: pp(vvvvCTS, vvCTS); break;
1491 1491
1492 1492 case Bytecodes::_ineg:
1493 1493 case Bytecodes::_fneg:
1494 1494 case Bytecodes::_i2f:
1495 1495 case Bytecodes::_f2i:
1496 1496 case Bytecodes::_i2c:
1497 1497 case Bytecodes::_i2s:
1498 1498 case Bytecodes::_i2b: pp(vCTS, vCTS); break;
1499 1499
1500 1500 case Bytecodes::_lneg:
1501 1501 case Bytecodes::_dneg:
1502 1502 case Bytecodes::_l2d:
1503 1503 case Bytecodes::_d2l: pp(vvCTS, vvCTS); break;
1504 1504
1505 1505 case Bytecodes::_lshl:
1506 1506 case Bytecodes::_lshr:
1507 1507 case Bytecodes::_lushr: pp(vvvCTS, vvCTS); break;
1508 1508
1509 1509 case Bytecodes::_i2l:
1510 1510 case Bytecodes::_i2d:
1511 1511 case Bytecodes::_f2l:
1512 1512 case Bytecodes::_f2d: pp(vCTS, vvCTS); break;
1513 1513
1514 1514 case Bytecodes::_lcmp: pp(vvvvCTS, vCTS); break;
1515 1515 case Bytecodes::_dcmpl:
1516 1516 case Bytecodes::_dcmpg: pp(vvvvCTS, vCTS); break;
1517 1517
1518 1518 case Bytecodes::_ifeq:
1519 1519 case Bytecodes::_ifne:
1520 1520 case Bytecodes::_iflt:
1521 1521 case Bytecodes::_ifge:
1522 1522 case Bytecodes::_ifgt:
1523 1523 case Bytecodes::_ifle:
1524 1524 case Bytecodes::_tableswitch: ppop1(valCTS);
1525 1525 break;
1526 1526 case Bytecodes::_ireturn:
1527 1527 case Bytecodes::_freturn: do_return_monitor_check();
1528 1528 ppop1(valCTS);
1529 1529 break;
1530 1530 case Bytecodes::_if_icmpeq:
1531 1531 case Bytecodes::_if_icmpne:
1532 1532 case Bytecodes::_if_icmplt:
1533 1533 case Bytecodes::_if_icmpge:
1534 1534 case Bytecodes::_if_icmpgt:
1535 1535 case Bytecodes::_if_icmple: ppop(vvCTS);
1536 1536 break;
1537 1537
1538 1538 case Bytecodes::_lreturn: do_return_monitor_check();
1539 1539 ppop(vvCTS);
1540 1540 break;
1541 1541
1542 1542 case Bytecodes::_dreturn: do_return_monitor_check();
1543 1543 ppop(vvCTS);
1544 1544 break;
1545 1545
1546 1546 case Bytecodes::_if_acmpeq:
1547 1547 case Bytecodes::_if_acmpne: ppop(rrCTS); break;
1548 1548
↓ open down ↓ |
1548 lines elided |
↑ open up ↑ |
1549 1549 case Bytecodes::_jsr: do_jsr(itr->dest()); break;
1550 1550 case Bytecodes::_jsr_w: do_jsr(itr->dest_w()); break;
1551 1551
1552 1552 case Bytecodes::_getstatic: do_field(true, true,
1553 1553 itr->get_index_big(),
1554 1554 itr->bci()); break;
1555 1555 case Bytecodes::_putstatic: do_field(false, true, itr->get_index_big(), itr->bci()); break;
1556 1556 case Bytecodes::_getfield: do_field(true, false, itr->get_index_big(), itr->bci()); break;
1557 1557 case Bytecodes::_putfield: do_field(false, false, itr->get_index_big(), itr->bci()); break;
1558 1558
1559 - case Bytecodes::_invokevirtual:
1560 - case Bytecodes::_invokespecial: do_method(false, false, itr->get_index_big(), itr->bci()); break;
1561 - case Bytecodes::_invokestatic: do_method(true, false, itr->get_index_big(), itr->bci()); break;
1559 + case Bytecodes::_invokevirtual:
1560 + case Bytecodes::_invokespecial: do_method(false, false, itr->get_index_big(), itr->bci()); break;
1561 + case Bytecodes::_invokestatic: do_method(true, false, itr->get_index_big(), itr->bci()); break;
1562 1562 case Bytecodes::_invokedynamic: do_method(true, false, itr->get_index_int(), itr->bci()); break;
1563 - case Bytecodes::_invokeinterface: do_method(false, true, itr->get_index_big(), itr->bci()); break;
1564 - case Bytecodes::_newarray:
1565 - case Bytecodes::_anewarray: pp_new_ref(vCTS, itr->bci()); break;
1563 + case Bytecodes::_invokeinterface: do_method(false, true, itr->get_index_big(), itr->bci()); break;
1564 + case Bytecodes::_newarray:
1565 + case Bytecodes::_anewarray: pp_new_ref(vCTS, itr->bci()); break;
1566 1566 case Bytecodes::_checkcast: do_checkcast(); break;
1567 1567 case Bytecodes::_arraylength:
1568 1568 case Bytecodes::_instanceof: pp(rCTS, vCTS); break;
1569 1569 case Bytecodes::_monitorenter: do_monitorenter(itr->bci()); break;
1570 1570 case Bytecodes::_monitorexit: do_monitorexit(itr->bci()); break;
1571 1571
1572 1572 case Bytecodes::_athrow: // handled by do_exception_edge() BUT ...
1573 1573 // vlh(apple): do_exception_edge() does not get
1574 1574 // called if method has no exception handlers
1575 1575 if ((!_has_exceptions) && (_monitor_top > 0)) {
1576 1576 _monitor_safe = false;
1577 1577 }
1578 1578 break;
1579 1579
1580 1580 case Bytecodes::_areturn: do_return_monitor_check();
1581 1581 ppop1(refCTS);
1582 1582 break;
1583 1583 case Bytecodes::_ifnull:
1584 1584 case Bytecodes::_ifnonnull: ppop1(refCTS); break;
1585 1585 case Bytecodes::_multianewarray: do_multianewarray(*(itr->bcp()+3), itr->bci()); break;
1586 1586
1587 1587 case Bytecodes::_wide: fatal("Iterator should skip this bytecode"); break;
1588 1588 case Bytecodes::_ret: break;
1589 1589
1590 1590 // Java opcodes
1591 1591 case Bytecodes::_lookupswitch: ppop1(valCTS); break;
1592 1592
1593 1593 default:
1594 1594 tty->print("unexpected opcode: %d\n", itr->code());
1595 1595 ShouldNotReachHere();
1596 1596 break;
1597 1597 }
1598 1598 }
1599 1599
1600 1600 void GenerateOopMap::check_type(CellTypeState expected, CellTypeState actual) {
1601 1601 if (!expected.equal_kind(actual)) {
1602 1602 verify_error("wrong type on stack (found: %c expected: %c)", actual.to_char(), expected.to_char());
1603 1603 }
1604 1604 }
1605 1605
1606 1606 void GenerateOopMap::ppstore(CellTypeState *in, int loc_no) {
1607 1607 while(!(*in).is_bottom()) {
1608 1608 CellTypeState expected =*in++;
1609 1609 CellTypeState actual = pop();
1610 1610 check_type(expected, actual);
1611 1611 assert(loc_no >= 0, "sanity check");
1612 1612 set_var(loc_no++, actual);
1613 1613 }
1614 1614 }
1615 1615
1616 1616 void GenerateOopMap::ppload(CellTypeState *out, int loc_no) {
1617 1617 while(!(*out).is_bottom()) {
1618 1618 CellTypeState out1 = *out++;
1619 1619 CellTypeState vcts = get_var(loc_no);
1620 1620 assert(out1.can_be_reference() || out1.can_be_value(),
1621 1621 "can only load refs. and values.");
1622 1622 if (out1.is_reference()) {
1623 1623 assert(loc_no>=0, "sanity check");
1624 1624 if (!vcts.is_reference()) {
1625 1625 // We were asked to push a reference, but the type of the
1626 1626 // variable can be something else
1627 1627 _conflict = true;
1628 1628 if (vcts.can_be_uninit()) {
1629 1629 // It is a ref-uninit conflict (at least). If there are other
1630 1630 // problems, we'll get them in the next round
1631 1631 add_to_ref_init_set(loc_no);
1632 1632 vcts = out1;
1633 1633 } else {
1634 1634 // It wasn't a ref-uninit conflict. So must be a
1635 1635 // ref-val or ref-pc conflict. Split the variable.
1636 1636 record_refval_conflict(loc_no);
1637 1637 vcts = out1;
1638 1638 }
1639 1639 push(out1); // recover...
1640 1640 } else {
1641 1641 push(vcts); // preserve reference.
1642 1642 }
1643 1643 // Otherwise it is a conflict, but one that verification would
1644 1644 // have caught if illegal. In particular, it can't be a topCTS
1645 1645 // resulting from mergeing two difference pcCTS's since the verifier
1646 1646 // would have rejected any use of such a merge.
1647 1647 } else {
1648 1648 push(out1); // handle val/init conflict
1649 1649 }
1650 1650 loc_no++;
1651 1651 }
1652 1652 }
1653 1653
1654 1654 void GenerateOopMap::ppdupswap(int poplen, const char *out) {
1655 1655 CellTypeState actual[5];
1656 1656 assert(poplen < 5, "this must be less than length of actual vector");
1657 1657
1658 1658 // pop all arguments
1659 1659 for(int i = 0; i < poplen; i++) actual[i] = pop();
1660 1660
1661 1661 // put them back
1662 1662 char push_ch = *out++;
1663 1663 while (push_ch != '\0') {
1664 1664 int idx = push_ch - '1';
1665 1665 assert(idx >= 0 && idx < poplen, "wrong arguments");
1666 1666 push(actual[idx]);
1667 1667 push_ch = *out++;
1668 1668 }
1669 1669 }
1670 1670
1671 1671 void GenerateOopMap::ppop1(CellTypeState out) {
1672 1672 CellTypeState actual = pop();
1673 1673 check_type(out, actual);
1674 1674 }
1675 1675
1676 1676 void GenerateOopMap::ppop(CellTypeState *out) {
1677 1677 while (!(*out).is_bottom()) {
1678 1678 ppop1(*out++);
1679 1679 }
1680 1680 }
1681 1681
1682 1682 void GenerateOopMap::ppush1(CellTypeState in) {
1683 1683 assert(in.is_reference() | in.is_value(), "sanity check");
1684 1684 push(in);
1685 1685 }
1686 1686
1687 1687 void GenerateOopMap::ppush(CellTypeState *in) {
1688 1688 while (!(*in).is_bottom()) {
1689 1689 ppush1(*in++);
1690 1690 }
1691 1691 }
1692 1692
1693 1693 void GenerateOopMap::pp(CellTypeState *in, CellTypeState *out) {
1694 1694 ppop(in);
1695 1695 ppush(out);
1696 1696 }
1697 1697
1698 1698 void GenerateOopMap::pp_new_ref(CellTypeState *in, int bci) {
1699 1699 ppop(in);
1700 1700 ppush1(CellTypeState::make_line_ref(bci));
1701 1701 }
1702 1702
1703 1703 void GenerateOopMap::ppop_any(int poplen) {
1704 1704 if (_stack_top >= poplen) {
1705 1705 _stack_top -= poplen;
1706 1706 } else {
1707 1707 verify_error("stack underflow");
1708 1708 }
1709 1709 }
1710 1710
1711 1711 // Replace all occurences of the state 'match' with the state 'replace'
1712 1712 // in our current state vector.
1713 1713 void GenerateOopMap::replace_all_CTS_matches(CellTypeState match,
1714 1714 CellTypeState replace) {
1715 1715 int i;
1716 1716 int len = _max_locals + _stack_top;
1717 1717 bool change = false;
1718 1718
1719 1719 for (i = len - 1; i >= 0; i--) {
1720 1720 if (match.equal(_state[i])) {
1721 1721 _state[i] = replace;
1722 1722 }
1723 1723 }
1724 1724
1725 1725 if (_monitor_top > 0) {
1726 1726 int base = _max_locals + _max_stack;
1727 1727 len = base + _monitor_top;
1728 1728 for (i = len - 1; i >= base; i--) {
1729 1729 if (match.equal(_state[i])) {
1730 1730 _state[i] = replace;
1731 1731 }
1732 1732 }
1733 1733 }
1734 1734 }
1735 1735
1736 1736 void GenerateOopMap::do_checkcast() {
1737 1737 CellTypeState actual = pop();
1738 1738 check_type(refCTS, actual);
1739 1739 push(actual);
1740 1740 }
1741 1741
1742 1742 void GenerateOopMap::do_monitorenter(int bci) {
1743 1743 CellTypeState actual = pop();
1744 1744 if (_monitor_top == bad_monitors) {
1745 1745 return;
1746 1746 }
1747 1747
1748 1748 // Bail out when we get repeated locks on an identical monitor. This case
1749 1749 // isn't too hard to handle and can be made to work if supporting nested
1750 1750 // redundant synchronized statements becomes a priority.
1751 1751 //
1752 1752 // See also "Note" in do_monitorexit(), below.
1753 1753 if (actual.is_lock_reference()) {
1754 1754 _monitor_top = bad_monitors;
1755 1755 _monitor_safe = false;
1756 1756
1757 1757 if (TraceMonitorMismatch) {
1758 1758 report_monitor_mismatch("nested redundant lock -- bailout...");
1759 1759 }
1760 1760 return;
1761 1761 }
1762 1762
1763 1763 CellTypeState lock = CellTypeState::make_lock_ref(bci);
1764 1764 check_type(refCTS, actual);
1765 1765 if (!actual.is_info_top()) {
1766 1766 replace_all_CTS_matches(actual, lock);
1767 1767 monitor_push(lock);
1768 1768 }
1769 1769 }
1770 1770
1771 1771 void GenerateOopMap::do_monitorexit(int bci) {
1772 1772 CellTypeState actual = pop();
1773 1773 if (_monitor_top == bad_monitors) {
1774 1774 return;
1775 1775 }
1776 1776 check_type(refCTS, actual);
1777 1777 CellTypeState expected = monitor_pop();
1778 1778 if (!actual.is_lock_reference() || !expected.equal(actual)) {
1779 1779 // The monitor we are exiting is not verifiably the one
1780 1780 // on the top of our monitor stack. This causes a monitor
1781 1781 // mismatch.
1782 1782 _monitor_top = bad_monitors;
1783 1783 _monitor_safe = false;
1784 1784
1785 1785 // We need to mark this basic block as changed so that
1786 1786 // this monitorexit will be visited again. We need to
1787 1787 // do this to ensure that we have accounted for the
1788 1788 // possibility that this bytecode will throw an
1789 1789 // exception.
1790 1790 BasicBlock* bb = get_basic_block_containing(bci);
1791 1791 bb->set_changed(true);
1792 1792 bb->_monitor_top = bad_monitors;
1793 1793
1794 1794 if (TraceMonitorMismatch) {
1795 1795 report_monitor_mismatch("improper monitor pair");
1796 1796 }
1797 1797 } else {
1798 1798 // This code is a fix for the case where we have repeated
1799 1799 // locking of the same object in straightline code. We clear
1800 1800 // out the lock when it is popped from the monitor stack
1801 1801 // and replace it with an unobtrusive reference value that can
1802 1802 // be locked again.
1803 1803 //
1804 1804 // Note: when generateOopMap is fixed to properly handle repeated,
1805 1805 // nested, redundant locks on the same object, then this
1806 1806 // fix will need to be removed at that time.
1807 1807 replace_all_CTS_matches(actual, CellTypeState::make_line_ref(bci));
1808 1808 }
1809 1809 }
1810 1810
1811 1811 void GenerateOopMap::do_return_monitor_check() {
1812 1812 if (_monitor_top > 0) {
1813 1813 // The monitor stack must be empty when we leave the method
1814 1814 // for the monitors to be properly matched.
1815 1815 _monitor_safe = false;
1816 1816
1817 1817 // Since there are no successors to the *return bytecode, it
1818 1818 // isn't necessary to set _monitor_top to bad_monitors.
1819 1819
1820 1820 if (TraceMonitorMismatch) {
1821 1821 report_monitor_mismatch("non-empty monitor stack at return");
1822 1822 }
↓ open down ↓ |
247 lines elided |
↑ open up ↑ |
1823 1823 }
1824 1824 }
1825 1825
1826 1826 void GenerateOopMap::do_jsr(int targ_bci) {
1827 1827 push(CellTypeState::make_addr(targ_bci));
1828 1828 }
1829 1829
1830 1830
1831 1831
1832 1832 void GenerateOopMap::do_ldc(int idx, int bci) {
1833 - constantPoolOop cp = method()->constants();
1834 - constantTag tag = cp->tag_at(idx);
1835 -
1836 - CellTypeState cts = (tag.is_string() || tag.is_unresolved_string() ||
1837 - tag.is_klass() || tag.is_unresolved_klass())
1838 - ? CellTypeState::make_line_ref(bci) : valCTS;
1833 + constantPoolOop cp = method()->constants();
1834 + CellTypeState cts = cp->is_pointer_entry(idx) ? CellTypeState::make_line_ref(bci) : valCTS;
1839 1835 ppush1(cts);
1840 1836 }
1841 1837
1842 1838 void GenerateOopMap::do_multianewarray(int dims, int bci) {
1843 1839 assert(dims >= 1, "sanity check");
1844 1840 for(int i = dims -1; i >=0; i--) {
1845 1841 ppop1(valCTS);
1846 1842 }
1847 1843 ppush1(CellTypeState::make_line_ref(bci));
1848 1844 }
1849 1845
1850 1846 void GenerateOopMap::do_astore(int idx) {
1851 1847 CellTypeState r_or_p = pop();
1852 1848 if (!r_or_p.is_address() && !r_or_p.is_reference()) {
1853 1849 // We actually expected ref or pc, but we only report that we expected a ref. It does not
1854 1850 // really matter (at least for now)
1855 1851 verify_error("wrong type on stack (found: %c, expected: {pr})", r_or_p.to_char());
1856 1852 return;
1857 1853 }
1858 1854 set_var(idx, r_or_p);
1859 1855 }
1860 1856
1861 1857 // Copies bottom/zero terminated CTS string from "src" into "dst".
1862 1858 // Does NOT terminate with a bottom. Returns the number of cells copied.
1863 1859 int GenerateOopMap::copy_cts(CellTypeState *dst, CellTypeState *src) {
1864 1860 int idx = 0;
1865 1861 while (!src[idx].is_bottom()) {
1866 1862 dst[idx] = src[idx];
1867 1863 idx++;
1868 1864 }
1869 1865 return idx;
1870 1866 }
1871 1867
1872 1868 void GenerateOopMap::do_field(int is_get, int is_static, int idx, int bci) {
1873 1869 // Dig up signature for field in constant pool
1874 1870 constantPoolOop cp = method()->constants();
1875 1871 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx);
1876 1872 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx);
1877 1873 symbolOop signature = cp->symbol_at(signatureIdx);
1878 1874
1879 1875 // Parse signature (espcially simple for fields)
1880 1876 assert(signature->utf8_length() > 0, "field signatures cannot have zero length");
1881 1877 // The signature is UFT8 encoded, but the first char is always ASCII for signatures.
1882 1878 char sigch = (char)*(signature->base());
1883 1879 CellTypeState temp[4];
1884 1880 CellTypeState *eff = sigchar_to_effect(sigch, bci, temp);
1885 1881
1886 1882 CellTypeState in[4];
1887 1883 CellTypeState *out;
1888 1884 int i = 0;
1889 1885
1890 1886 if (is_get) {
1891 1887 out = eff;
1892 1888 } else {
1893 1889 out = epsilonCTS;
1894 1890 i = copy_cts(in, eff);
1895 1891 }
1896 1892 if (!is_static) in[i++] = CellTypeState::ref;
1897 1893 in[i] = CellTypeState::bottom;
1898 1894 assert(i<=3, "sanity check");
1899 1895 pp(in, out);
1900 1896 }
1901 1897
1902 1898 void GenerateOopMap::do_method(int is_static, int is_interface, int idx, int bci) {
1903 1899 // Dig up signature for field in constant pool
1904 1900 constantPoolOop cp = _method->constants();
1905 1901 symbolOop signature = cp->signature_ref_at(idx);
1906 1902
1907 1903 // Parse method signature
1908 1904 CellTypeState out[4];
1909 1905 CellTypeState in[MAXARGSIZE+1]; // Includes result
1910 1906 ComputeCallStack cse(signature);
1911 1907
1912 1908 // Compute return type
1913 1909 int res_length= cse.compute_for_returntype(out);
1914 1910
1915 1911 // Temporary hack.
1916 1912 if (out[0].equal(CellTypeState::ref) && out[1].equal(CellTypeState::bottom)) {
1917 1913 out[0] = CellTypeState::make_line_ref(bci);
1918 1914 }
1919 1915
1920 1916 assert(res_length<=4, "max value should be vv");
1921 1917
1922 1918 // Compute arguments
1923 1919 int arg_length = cse.compute_for_parameters(is_static != 0, in);
1924 1920 assert(arg_length<=MAXARGSIZE, "too many locals");
1925 1921
1926 1922 // Pop arguments
1927 1923 for (int i = arg_length - 1; i >= 0; i--) ppop1(in[i]);// Do args in reverse order.
1928 1924
1929 1925 // Report results
1930 1926 if (_report_result_for_send == true) {
1931 1927 fill_stackmap_for_opcodes(_itr_send, vars(), stack(), _stack_top);
1932 1928 _report_result_for_send = false;
1933 1929 }
1934 1930
1935 1931 // Push return address
1936 1932 ppush(out);
1937 1933 }
1938 1934
1939 1935 // This is used to parse the signature for fields, since they are very simple...
1940 1936 CellTypeState *GenerateOopMap::sigchar_to_effect(char sigch, int bci, CellTypeState *out) {
1941 1937 // Object and array
1942 1938 if (sigch=='L' || sigch=='[') {
1943 1939 out[0] = CellTypeState::make_line_ref(bci);
1944 1940 out[1] = CellTypeState::bottom;
1945 1941 return out;
1946 1942 }
1947 1943 if (sigch == 'J' || sigch == 'D' ) return vvCTS; // Long and Double
1948 1944 if (sigch == 'V' ) return epsilonCTS; // Void
1949 1945 return vCTS; // Otherwise
1950 1946 }
1951 1947
1952 1948 long GenerateOopMap::_total_byte_count = 0;
1953 1949 elapsedTimer GenerateOopMap::_total_oopmap_time;
1954 1950
1955 1951 // This function assumes "bcs" is at a "ret" instruction and that the vars
1956 1952 // state is valid for that instruction. Furthermore, the ret instruction
1957 1953 // must be the last instruction in "bb" (we store information about the
1958 1954 // "ret" in "bb").
1959 1955 void GenerateOopMap::ret_jump_targets_do(BytecodeStream *bcs, jmpFct_t jmpFct, int varNo, int *data) {
1960 1956 CellTypeState ra = vars()[varNo];
1961 1957 if (!ra.is_good_address()) {
1962 1958 verify_error("ret returns from two jsr subroutines?");
1963 1959 return;
1964 1960 }
1965 1961 int target = ra.get_info();
1966 1962
1967 1963 RetTableEntry* rtEnt = _rt.find_jsrs_for_target(target);
1968 1964 int bci = bcs->bci();
1969 1965 for (int i = 0; i < rtEnt->nof_jsrs(); i++) {
1970 1966 int target_bci = rtEnt->jsrs(i);
1971 1967 // Make sure a jrtRet does not set the changed bit for dead basicblock.
1972 1968 BasicBlock* jsr_bb = get_basic_block_containing(target_bci - 1);
1973 1969 debug_only(BasicBlock* target_bb = &jsr_bb[1];)
1974 1970 assert(target_bb == get_basic_block_at(target_bci), "wrong calc. of successor basicblock");
1975 1971 bool alive = jsr_bb->is_alive();
1976 1972 if (TraceNewOopMapGeneration) {
1977 1973 tty->print("pc = %d, ret -> %d alive: %s\n", bci, target_bci, alive ? "true" : "false");
1978 1974 }
1979 1975 if (alive) jmpFct(this, target_bci, data);
1980 1976 }
1981 1977 }
1982 1978
1983 1979 //
1984 1980 // Debug method
1985 1981 //
1986 1982 char* GenerateOopMap::state_vec_to_string(CellTypeState* vec, int len) {
1987 1983 #ifdef ASSERT
1988 1984 int checklen = MAX3(_max_locals, _max_stack, _max_monitors) + 1;
1989 1985 assert(len < checklen, "state_vec_buf overflow");
1990 1986 #endif
1991 1987 for (int i = 0; i < len; i++) _state_vec_buf[i] = vec[i].to_char();
1992 1988 _state_vec_buf[len] = 0;
1993 1989 return _state_vec_buf;
1994 1990 }
1995 1991
1996 1992 void GenerateOopMap::print_time() {
1997 1993 tty->print_cr ("Accumulated oopmap times:");
1998 1994 tty->print_cr ("---------------------------");
1999 1995 tty->print_cr (" Total : %3.3f sec.", GenerateOopMap::_total_oopmap_time.seconds());
2000 1996 tty->print_cr (" (%3.0f bytecodes per sec) ",
2001 1997 GenerateOopMap::_total_byte_count / GenerateOopMap::_total_oopmap_time.seconds());
2002 1998 }
2003 1999
2004 2000 //
2005 2001 // ============ Main Entry Point ===========
2006 2002 //
2007 2003 GenerateOopMap::GenerateOopMap(methodHandle method) {
2008 2004 // We have to initialize all variables here, that can be queried directly
2009 2005 _method = method;
2010 2006 _max_locals=0;
2011 2007 _init_vars = NULL;
2012 2008
2013 2009 #ifndef PRODUCT
2014 2010 // If we are doing a detailed trace, include the regular trace information.
2015 2011 if (TraceNewOopMapGenerationDetailed) {
2016 2012 TraceNewOopMapGeneration = true;
2017 2013 }
2018 2014 #endif
2019 2015 }
2020 2016
2021 2017 void GenerateOopMap::compute_map(TRAPS) {
2022 2018 #ifndef PRODUCT
2023 2019 if (TimeOopMap2) {
2024 2020 method()->print_short_name(tty);
2025 2021 tty->print(" ");
2026 2022 }
2027 2023 if (TimeOopMap) {
2028 2024 _total_byte_count += method()->code_size();
2029 2025 }
2030 2026 #endif
2031 2027 TraceTime t_single("oopmap time", TimeOopMap2);
2032 2028 TraceTime t_all(NULL, &_total_oopmap_time, TimeOopMap);
2033 2029
2034 2030 // Initialize values
2035 2031 _got_error = false;
2036 2032 _conflict = false;
2037 2033 _max_locals = method()->max_locals();
2038 2034 _max_stack = method()->max_stack();
2039 2035 _has_exceptions = (method()->exception_table()->length() > 0);
2040 2036 _nof_refval_conflicts = 0;
2041 2037 _init_vars = new GrowableArray<intptr_t>(5); // There are seldom more than 5 init_vars
2042 2038 _report_result = false;
2043 2039 _report_result_for_send = false;
2044 2040 _new_var_map = NULL;
2045 2041 _ret_adr_tos = new GrowableArray<intptr_t>(5); // 5 seems like a good number;
2046 2042 _did_rewriting = false;
2047 2043 _did_relocation = false;
2048 2044
2049 2045 if (TraceNewOopMapGeneration) {
2050 2046 tty->print("Method name: %s\n", method()->name()->as_C_string());
2051 2047 if (Verbose) {
2052 2048 _method->print_codes();
2053 2049 tty->print_cr("Exception table:");
2054 2050 typeArrayOop excps = method()->exception_table();
2055 2051 for(int i = 0; i < excps->length(); i += 4) {
2056 2052 tty->print_cr("[%d - %d] -> %d", excps->int_at(i + 0), excps->int_at(i + 1), excps->int_at(i + 2));
2057 2053 }
2058 2054 }
2059 2055 }
2060 2056
2061 2057 // if no code - do nothing
2062 2058 // compiler needs info
2063 2059 if (method()->code_size() == 0 || _max_locals + method()->max_stack() == 0) {
2064 2060 fill_stackmap_prolog(0);
2065 2061 fill_stackmap_epilog();
2066 2062 return;
2067 2063 }
2068 2064 // Step 1: Compute all jump targets and their return value
2069 2065 if (!_got_error)
2070 2066 _rt.compute_ret_table(_method);
2071 2067
2072 2068 // Step 2: Find all basic blocks and count GC points
2073 2069 if (!_got_error)
2074 2070 mark_bbheaders_and_count_gc_points();
2075 2071
2076 2072 // Step 3: Calculate stack maps
2077 2073 if (!_got_error)
2078 2074 do_interpretation();
2079 2075
2080 2076 // Step 4:Return results
2081 2077 if (!_got_error && report_results())
2082 2078 report_result();
2083 2079
2084 2080 if (_got_error) {
2085 2081 THROW_HANDLE(_exception);
2086 2082 }
2087 2083 }
2088 2084
2089 2085 // Error handling methods
2090 2086 // These methods create an exception for the current thread which is thrown
2091 2087 // at the bottom of the call stack, when it returns to compute_map(). The
2092 2088 // _got_error flag controls execution. NOT TODO: The VM exception propagation
2093 2089 // mechanism using TRAPS/CHECKs could be used here instead but it would need
2094 2090 // to be added as a parameter to every function and checked for every call.
2095 2091 // The tons of extra code it would generate didn't seem worth the change.
2096 2092 //
2097 2093 void GenerateOopMap::error_work(const char *format, va_list ap) {
2098 2094 _got_error = true;
2099 2095 char msg_buffer[512];
2100 2096 vsnprintf(msg_buffer, sizeof(msg_buffer), format, ap);
2101 2097 // Append method name
2102 2098 char msg_buffer2[512];
2103 2099 jio_snprintf(msg_buffer2, sizeof(msg_buffer2), "%s in method %s", msg_buffer, method()->name()->as_C_string());
2104 2100 _exception = Exceptions::new_exception(Thread::current(),
2105 2101 vmSymbols::java_lang_LinkageError(), msg_buffer2);
2106 2102 }
2107 2103
2108 2104 void GenerateOopMap::report_error(const char *format, ...) {
2109 2105 va_list ap;
2110 2106 va_start(ap, format);
2111 2107 error_work(format, ap);
2112 2108 }
2113 2109
2114 2110 void GenerateOopMap::verify_error(const char *format, ...) {
2115 2111 // We do not distinguish between different types of errors for verification
2116 2112 // errors. Let the verifier give a better message.
2117 2113 const char *msg = "Illegal class file encountered. Try running with -Xverify:all";
2118 2114 error_work(msg, NULL);
2119 2115 }
2120 2116
2121 2117 //
2122 2118 // Report result opcodes
2123 2119 //
2124 2120 void GenerateOopMap::report_result() {
2125 2121
2126 2122 if (TraceNewOopMapGeneration) tty->print_cr("Report result pass");
2127 2123
2128 2124 // We now want to report the result of the parse
2129 2125 _report_result = true;
2130 2126
2131 2127 // Prolog code
2132 2128 fill_stackmap_prolog(_gc_points);
2133 2129
2134 2130 // Mark everything changed, then do one interpretation pass.
2135 2131 for (int i = 0; i<_bb_count; i++) {
2136 2132 if (_basic_blocks[i].is_reachable()) {
2137 2133 _basic_blocks[i].set_changed(true);
2138 2134 interp_bb(&_basic_blocks[i]);
2139 2135 }
2140 2136 }
2141 2137
2142 2138 // Note: Since we are skipping dead-code when we are reporting results, then
2143 2139 // the no. of encountered gc-points might be fewer than the previously number
2144 2140 // we have counted. (dead-code is a pain - it should be removed before we get here)
2145 2141 fill_stackmap_epilog();
2146 2142
2147 2143 // Report initvars
2148 2144 fill_init_vars(_init_vars);
2149 2145
2150 2146 _report_result = false;
2151 2147 }
2152 2148
2153 2149 void GenerateOopMap::result_for_basicblock(int bci) {
2154 2150 if (TraceNewOopMapGeneration) tty->print_cr("Report result pass for basicblock");
2155 2151
2156 2152 // We now want to report the result of the parse
2157 2153 _report_result = true;
2158 2154
2159 2155 // Find basicblock and report results
2160 2156 BasicBlock* bb = get_basic_block_containing(bci);
2161 2157 assert(bb->is_reachable(), "getting result from unreachable basicblock");
2162 2158 bb->set_changed(true);
2163 2159 interp_bb(bb);
2164 2160 }
2165 2161
2166 2162 //
2167 2163 // Conflict handling code
2168 2164 //
2169 2165
2170 2166 void GenerateOopMap::record_refval_conflict(int varNo) {
2171 2167 assert(varNo>=0 && varNo< _max_locals, "index out of range");
2172 2168
2173 2169 if (TraceOopMapRewrites) {
2174 2170 tty->print("### Conflict detected (local no: %d)\n", varNo);
2175 2171 }
2176 2172
2177 2173 if (!_new_var_map) {
2178 2174 _new_var_map = NEW_RESOURCE_ARRAY(int, _max_locals);
2179 2175 for (int k = 0; k < _max_locals; k++) _new_var_map[k] = k;
2180 2176 }
2181 2177
2182 2178 if ( _new_var_map[varNo] == varNo) {
2183 2179 // Check if max. number of locals has been reached
2184 2180 if (_max_locals + _nof_refval_conflicts >= MAX_LOCAL_VARS) {
2185 2181 report_error("Rewriting exceeded local variable limit");
2186 2182 return;
2187 2183 }
2188 2184 _new_var_map[varNo] = _max_locals + _nof_refval_conflicts;
2189 2185 _nof_refval_conflicts++;
2190 2186 }
2191 2187 }
2192 2188
2193 2189 void GenerateOopMap::rewrite_refval_conflicts()
2194 2190 {
2195 2191 // We can get here two ways: Either a rewrite conflict was detected, or
2196 2192 // an uninitialize reference was detected. In the second case, we do not
2197 2193 // do any rewriting, we just want to recompute the reference set with the
2198 2194 // new information
2199 2195
2200 2196 int nof_conflicts = 0; // Used for debugging only
2201 2197
2202 2198 if ( _nof_refval_conflicts == 0 )
2203 2199 return;
2204 2200
2205 2201 // Check if rewrites are allowed in this parse.
2206 2202 if (!allow_rewrites() && !IgnoreRewrites) {
2207 2203 fatal("Rewriting method not allowed at this stage");
2208 2204 }
2209 2205
2210 2206
2211 2207 // This following flag is to tempoary supress rewrites. The locals that might conflict will
2212 2208 // all be set to contain values. This is UNSAFE - however, until the rewriting has been completely
2213 2209 // tested it is nice to have.
2214 2210 if (IgnoreRewrites) {
2215 2211 if (Verbose) {
2216 2212 tty->print("rewrites suppressed for local no. ");
2217 2213 for (int l = 0; l < _max_locals; l++) {
2218 2214 if (_new_var_map[l] != l) {
2219 2215 tty->print("%d ", l);
2220 2216 vars()[l] = CellTypeState::value;
2221 2217 }
2222 2218 }
2223 2219 tty->cr();
2224 2220 }
2225 2221
2226 2222 // That was that...
2227 2223 _new_var_map = NULL;
2228 2224 _nof_refval_conflicts = 0;
2229 2225 _conflict = false;
2230 2226
2231 2227 return;
2232 2228 }
2233 2229
2234 2230 // Tracing flag
2235 2231 _did_rewriting = true;
2236 2232
2237 2233 if (TraceOopMapRewrites) {
2238 2234 tty->print_cr("ref/value conflict for method %s - bytecodes are getting rewritten", method()->name()->as_C_string());
2239 2235 method()->print();
2240 2236 method()->print_codes();
2241 2237 }
2242 2238
2243 2239 assert(_new_var_map!=NULL, "nothing to rewrite");
2244 2240 assert(_conflict==true, "We should not be here");
2245 2241
2246 2242 compute_ret_adr_at_TOS();
2247 2243 if (!_got_error) {
2248 2244 for (int k = 0; k < _max_locals && !_got_error; k++) {
2249 2245 if (_new_var_map[k] != k) {
2250 2246 if (TraceOopMapRewrites) {
2251 2247 tty->print_cr("Rewriting: %d -> %d", k, _new_var_map[k]);
2252 2248 }
2253 2249 rewrite_refval_conflict(k, _new_var_map[k]);
2254 2250 if (_got_error) return;
2255 2251 nof_conflicts++;
2256 2252 }
2257 2253 }
2258 2254 }
2259 2255
2260 2256 assert(nof_conflicts == _nof_refval_conflicts, "sanity check");
2261 2257
2262 2258 // Adjust the number of locals
2263 2259 method()->set_max_locals(_max_locals+_nof_refval_conflicts);
2264 2260 _max_locals += _nof_refval_conflicts;
2265 2261
2266 2262 // That was that...
2267 2263 _new_var_map = NULL;
2268 2264 _nof_refval_conflicts = 0;
2269 2265 }
2270 2266
2271 2267 void GenerateOopMap::rewrite_refval_conflict(int from, int to) {
2272 2268 bool startOver;
2273 2269 do {
2274 2270 // Make sure that the BytecodeStream is constructed in the loop, since
2275 2271 // during rewriting a new method oop is going to be used, and the next time
2276 2272 // around we want to use that.
2277 2273 BytecodeStream bcs(_method);
2278 2274 startOver = false;
2279 2275
2280 2276 while( bcs.next() >=0 && !startOver && !_got_error) {
2281 2277 startOver = rewrite_refval_conflict_inst(&bcs, from, to);
2282 2278 }
2283 2279 } while (startOver && !_got_error);
2284 2280 }
2285 2281
2286 2282 /* If the current instruction is one that uses local variable "from"
2287 2283 in a ref way, change it to use "to". There's a subtle reason why we
2288 2284 renumber the ref uses and not the non-ref uses: non-ref uses may be
2289 2285 2 slots wide (double, long) which would necessitate keeping track of
2290 2286 whether we should add one or two variables to the method. If the change
2291 2287 affected the width of some instruction, returns "TRUE"; otherwise, returns "FALSE".
2292 2288 Another reason for moving ref's value is for solving (addr, ref) conflicts, which
2293 2289 both uses aload/astore methods.
2294 2290 */
2295 2291 bool GenerateOopMap::rewrite_refval_conflict_inst(BytecodeStream *itr, int from, int to) {
2296 2292 Bytecodes::Code bc = itr->code();
2297 2293 int index;
2298 2294 int bci = itr->bci();
2299 2295
2300 2296 if (is_aload(itr, &index) && index == from) {
2301 2297 if (TraceOopMapRewrites) {
2302 2298 tty->print_cr("Rewriting aload at bci: %d", bci);
2303 2299 }
2304 2300 return rewrite_load_or_store(itr, Bytecodes::_aload, Bytecodes::_aload_0, to);
2305 2301 }
2306 2302
2307 2303 if (is_astore(itr, &index) && index == from) {
2308 2304 if (!stack_top_holds_ret_addr(bci)) {
2309 2305 if (TraceOopMapRewrites) {
2310 2306 tty->print_cr("Rewriting astore at bci: %d", bci);
2311 2307 }
2312 2308 return rewrite_load_or_store(itr, Bytecodes::_astore, Bytecodes::_astore_0, to);
2313 2309 } else {
2314 2310 if (TraceOopMapRewrites) {
2315 2311 tty->print_cr("Supress rewriting of astore at bci: %d", bci);
2316 2312 }
2317 2313 }
2318 2314 }
2319 2315
2320 2316 return false;
2321 2317 }
2322 2318
2323 2319 // The argument to this method is:
2324 2320 // bc : Current bytecode
2325 2321 // bcN : either _aload or _astore
2326 2322 // bc0 : either _aload_0 or _astore_0
2327 2323 bool GenerateOopMap::rewrite_load_or_store(BytecodeStream *bcs, Bytecodes::Code bcN, Bytecodes::Code bc0, unsigned int varNo) {
2328 2324 assert(bcN == Bytecodes::_astore || bcN == Bytecodes::_aload, "wrong argument (bcN)");
2329 2325 assert(bc0 == Bytecodes::_astore_0 || bc0 == Bytecodes::_aload_0, "wrong argument (bc0)");
2330 2326 int ilen = Bytecodes::length_at(bcs->bcp());
2331 2327 int newIlen;
2332 2328
2333 2329 if (ilen == 4) {
2334 2330 // Original instruction was wide; keep it wide for simplicity
2335 2331 newIlen = 4;
2336 2332 } else if (varNo < 4)
2337 2333 newIlen = 1;
2338 2334 else if (varNo >= 256)
2339 2335 newIlen = 4;
2340 2336 else
2341 2337 newIlen = 2;
2342 2338
2343 2339 // If we need to relocate in order to patch the byte, we
2344 2340 // do the patching in a temp. buffer, that is passed to the reloc.
2345 2341 // The patching of the bytecode stream is then done by the Relocator.
2346 2342 // This is neccesary, since relocating the instruction at a certain bci, might
2347 2343 // also relocate that instruction, e.g., if a _goto before it gets widen to a _goto_w.
2348 2344 // Hence, we do not know which bci to patch after relocation.
2349 2345
2350 2346 assert(newIlen <= 4, "sanity check");
2351 2347 u_char inst_buffer[4]; // Max. instruction size is 4.
2352 2348 address bcp;
2353 2349
2354 2350 if (newIlen != ilen) {
2355 2351 // Relocation needed do patching in temp. buffer
2356 2352 bcp = (address)inst_buffer;
2357 2353 } else {
2358 2354 bcp = _method->bcp_from(bcs->bci());
2359 2355 }
2360 2356
2361 2357 // Patch either directly in methodOop or in temp. buffer
2362 2358 if (newIlen == 1) {
2363 2359 assert(varNo < 4, "varNo too large");
2364 2360 *bcp = bc0 + varNo;
2365 2361 } else if (newIlen == 2) {
2366 2362 assert(varNo < 256, "2-byte index needed!");
2367 2363 *(bcp + 0) = bcN;
2368 2364 *(bcp + 1) = varNo;
2369 2365 } else {
2370 2366 assert(newIlen == 4, "Wrong instruction length");
2371 2367 *(bcp + 0) = Bytecodes::_wide;
2372 2368 *(bcp + 1) = bcN;
2373 2369 Bytes::put_Java_u2(bcp+2, varNo);
2374 2370 }
2375 2371
2376 2372 if (newIlen != ilen) {
2377 2373 expand_current_instr(bcs->bci(), ilen, newIlen, inst_buffer);
2378 2374 }
2379 2375
2380 2376
2381 2377 return (newIlen != ilen);
2382 2378 }
2383 2379
2384 2380 class RelocCallback : public RelocatorListener {
2385 2381 private:
2386 2382 GenerateOopMap* _gom;
2387 2383 public:
2388 2384 RelocCallback(GenerateOopMap* gom) { _gom = gom; };
2389 2385
2390 2386 // Callback method
2391 2387 virtual void relocated(int bci, int delta, int new_code_length) {
2392 2388 _gom->update_basic_blocks (bci, delta, new_code_length);
2393 2389 _gom->update_ret_adr_at_TOS(bci, delta);
2394 2390 _gom->_rt.update_ret_table (bci, delta);
2395 2391 }
2396 2392 };
2397 2393
2398 2394 // Returns true if expanding was succesful. Otherwise, reports an error and
2399 2395 // returns false.
2400 2396 void GenerateOopMap::expand_current_instr(int bci, int ilen, int newIlen, u_char inst_buffer[]) {
2401 2397 Thread *THREAD = Thread::current(); // Could really have TRAPS argument.
2402 2398 RelocCallback rcb(this);
2403 2399 Relocator rc(_method, &rcb);
2404 2400 methodHandle m= rc.insert_space_at(bci, newIlen, inst_buffer, THREAD);
2405 2401 if (m.is_null() || HAS_PENDING_EXCEPTION) {
2406 2402 report_error("could not rewrite method - exception occurred or bytecode buffer overflow");
2407 2403 return;
2408 2404 }
2409 2405
2410 2406 // Relocator returns a new method oop.
2411 2407 _did_relocation = true;
2412 2408 _method = m;
2413 2409 }
2414 2410
2415 2411
2416 2412 bool GenerateOopMap::is_astore(BytecodeStream *itr, int *index) {
2417 2413 Bytecodes::Code bc = itr->code();
2418 2414 switch(bc) {
2419 2415 case Bytecodes::_astore_0:
2420 2416 case Bytecodes::_astore_1:
2421 2417 case Bytecodes::_astore_2:
2422 2418 case Bytecodes::_astore_3:
2423 2419 *index = bc - Bytecodes::_astore_0;
2424 2420 return true;
2425 2421 case Bytecodes::_astore:
2426 2422 *index = itr->get_index();
2427 2423 return true;
2428 2424 }
2429 2425 return false;
2430 2426 }
2431 2427
2432 2428 bool GenerateOopMap::is_aload(BytecodeStream *itr, int *index) {
2433 2429 Bytecodes::Code bc = itr->code();
2434 2430 switch(bc) {
2435 2431 case Bytecodes::_aload_0:
2436 2432 case Bytecodes::_aload_1:
2437 2433 case Bytecodes::_aload_2:
2438 2434 case Bytecodes::_aload_3:
2439 2435 *index = bc - Bytecodes::_aload_0;
2440 2436 return true;
2441 2437
2442 2438 case Bytecodes::_aload:
2443 2439 *index = itr->get_index();
2444 2440 return true;
2445 2441 }
2446 2442 return false;
2447 2443 }
2448 2444
2449 2445
2450 2446 // Return true iff the top of the operand stack holds a return address at
2451 2447 // the current instruction
2452 2448 bool GenerateOopMap::stack_top_holds_ret_addr(int bci) {
2453 2449 for(int i = 0; i < _ret_adr_tos->length(); i++) {
2454 2450 if (_ret_adr_tos->at(i) == bci)
2455 2451 return true;
2456 2452 }
2457 2453
2458 2454 return false;
2459 2455 }
2460 2456
2461 2457 void GenerateOopMap::compute_ret_adr_at_TOS() {
2462 2458 assert(_ret_adr_tos != NULL, "must be initialized");
2463 2459 _ret_adr_tos->clear();
2464 2460
2465 2461 for (int i = 0; i < bb_count(); i++) {
2466 2462 BasicBlock* bb = &_basic_blocks[i];
2467 2463
2468 2464 // Make sure to only check basicblocks that are reachable
2469 2465 if (bb->is_reachable()) {
2470 2466
2471 2467 // For each Basic block we check all instructions
2472 2468 BytecodeStream bcs(_method);
2473 2469 bcs.set_interval(bb->_bci, next_bb_start_pc(bb));
2474 2470
2475 2471 restore_state(bb);
2476 2472
2477 2473 while (bcs.next()>=0 && !_got_error) {
2478 2474 // TDT: should this be is_good_address() ?
2479 2475 if (_stack_top > 0 && stack()[_stack_top-1].is_address()) {
2480 2476 _ret_adr_tos->append(bcs.bci());
2481 2477 if (TraceNewOopMapGeneration) {
2482 2478 tty->print_cr("Ret_adr TOS at bci: %d", bcs.bci());
2483 2479 }
2484 2480 }
2485 2481 interp1(&bcs);
2486 2482 }
2487 2483 }
2488 2484 }
2489 2485 }
2490 2486
2491 2487 void GenerateOopMap::update_ret_adr_at_TOS(int bci, int delta) {
2492 2488 for(int i = 0; i < _ret_adr_tos->length(); i++) {
2493 2489 int v = _ret_adr_tos->at(i);
2494 2490 if (v > bci) _ret_adr_tos->at_put(i, v + delta);
2495 2491 }
2496 2492 }
2497 2493
2498 2494 // ===================================================================
2499 2495
2500 2496 #ifndef PRODUCT
2501 2497 int ResolveOopMapConflicts::_nof_invocations = 0;
2502 2498 int ResolveOopMapConflicts::_nof_rewrites = 0;
2503 2499 int ResolveOopMapConflicts::_nof_relocations = 0;
2504 2500 #endif
2505 2501
2506 2502 methodHandle ResolveOopMapConflicts::do_potential_rewrite(TRAPS) {
2507 2503 compute_map(CHECK_(methodHandle()));
2508 2504
2509 2505 #ifndef PRODUCT
2510 2506 // Tracking and statistics
2511 2507 if (PrintRewrites) {
2512 2508 _nof_invocations++;
2513 2509 if (did_rewriting()) {
2514 2510 _nof_rewrites++;
2515 2511 if (did_relocation()) _nof_relocations++;
2516 2512 tty->print("Method was rewritten %s: ", (did_relocation()) ? "and relocated" : "");
2517 2513 method()->print_value(); tty->cr();
2518 2514 tty->print_cr("Cand.: %d rewrts: %d (%d%%) reloc.: %d (%d%%)",
2519 2515 _nof_invocations,
2520 2516 _nof_rewrites, (_nof_rewrites * 100) / _nof_invocations,
2521 2517 _nof_relocations, (_nof_relocations * 100) / _nof_invocations);
2522 2518 }
2523 2519 }
2524 2520 #endif
2525 2521 return methodHandle(THREAD, method());
2526 2522 }
↓ open down ↓ |
678 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX