ciStreams.cpp 16.5 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30
#include "precompiled.hpp"
#include "ci/ciCallSite.hpp"
#include "ci/ciConstant.hpp"
#include "ci/ciField.hpp"
#include "ci/ciStreams.hpp"
#include "ci/ciUtilities.hpp"
D
duke 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

// ciExceptionHandlerStream
//
// Walk over some selected set of a methods exception handlers.

// ------------------------------------------------------------------
// ciExceptionHandlerStream::count
//
// How many exception handlers are there in this stream?
//
// Implementation note: Compiler2 needs this functionality, so I had
int ciExceptionHandlerStream::count() {
  int save_pos = _pos;
  int save_end = _end;

  int count = 0;

  _pos = -1;
  _end = _method->_handler_count;


  next();
  while (!is_done()) {
    count++;
    next();
  }

  _pos = save_pos;
  _end = save_end;

  return count;
}

int ciExceptionHandlerStream::count_remaining() {
  int save_pos = _pos;
  int save_end = _end;

  int count = 0;

  while (!is_done()) {
    count++;
    next();
  }

  _pos = save_pos;
  _end = save_end;

  return count;
}

// ciBytecodeStream
//
// The class is used to iterate over the bytecodes of a method.
// It hides the details of constant pool structure/access by
// providing accessors for constant pool items.

// ------------------------------------------------------------------
88
// ciBytecodeStream::next_wide_or_table
D
duke 已提交
89 90
//
// Special handling for switch ops
91 92 93 94 95 96 97 98 99 100 101 102
Bytecodes::Code ciBytecodeStream::next_wide_or_table(Bytecodes::Code bc) {
  switch (bc) {                // Check for special bytecode handling
  case Bytecodes::_wide:
    // Special handling for the wide bytcode
    // Get following bytecode; do not return wide
    assert(Bytecodes::Code(_pc[0]) == Bytecodes::_wide, "");
    bc = Bytecodes::java_code(_raw_bc = (Bytecodes::Code)_pc[1]);
    assert(Bytecodes::wide_length_for(bc) > 2, "must make progress");
    _pc += Bytecodes::wide_length_for(bc);
    _was_wide = _pc;              // Flag last wide bytecode found
    assert(is_wide(), "accessor works right");
    break;
D
duke 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

  case Bytecodes::_lookupswitch:
    _pc++;                      // Skip wide bytecode
    _pc += (_start-_pc)&3;      // Word align
    _table_base = (jint*)_pc;   // Capture for later usage
                                // table_base[0] is default far_dest
    // Table has 2 lead elements (default, length), then pairs of u4 values.
    // So load table length, and compute address at end of table
    _pc = (address)&_table_base[2+ 2*Bytes::get_Java_u4((address)&_table_base[1])];
    break;

  case Bytecodes::_tableswitch: {
    _pc++;                      // Skip wide bytecode
    _pc += (_start-_pc)&3;      // Word align
    _table_base = (jint*)_pc;   // Capture for later usage
                                // table_base[0] is default far_dest
    int lo = Bytes::get_Java_u4((address)&_table_base[1]);// Low bound
    int hi = Bytes::get_Java_u4((address)&_table_base[2]);// High bound
    int len = hi - lo + 1;      // Dense table size
    _pc = (address)&_table_base[3+len]; // Skip past table
    break;
  }

  default:
    fatal("unhandled bytecode");
  }
  return bc;
}

// ------------------------------------------------------------------
// ciBytecodeStream::reset_to_bci
void ciBytecodeStream::reset_to_bci( int bci ) {
  _bc_start=_was_wide=0;
  _pc = _start+bci;
}

// ------------------------------------------------------------------
// ciBytecodeStream::force_bci
void ciBytecodeStream::force_bci(int bci) {
  if (bci < 0) {
    reset_to_bci(0);
    _bc_start = _start + bci;
    _bc = EOBC();
  } else {
    reset_to_bci(bci);
    next();
  }
}


// ------------------------------------------------------------------
// Constant pool access
// ------------------------------------------------------------------

// ------------------------------------------------------------------
// ciBytecodeStream::get_klass_index
//
// If this bytecodes references a klass, return the index of the
// referenced klass.
int ciBytecodeStream::get_klass_index() const {
  switch(cur_bc()) {
  case Bytecodes::_ldc:
165
    return get_index_u1();
D
duke 已提交
166 167 168 169 170 171 172 173
  case Bytecodes::_ldc_w:
  case Bytecodes::_ldc2_w:
  case Bytecodes::_checkcast:
  case Bytecodes::_instanceof:
  case Bytecodes::_anewarray:
  case Bytecodes::_multianewarray:
  case Bytecodes::_new:
  case Bytecodes::_newarray:
174
    return get_index_u2();
D
duke 已提交
175 176 177 178 179 180 181 182 183 184 185 186
  default:
    ShouldNotReachHere();
    return 0;
  }
}

// ------------------------------------------------------------------
// ciBytecodeStream::get_klass
//
// If this bytecode is a new, newarray, multianewarray, instanceof,
// or checkcast, get the referenced klass.
ciKlass* ciBytecodeStream::get_klass(bool& will_link) {
187
  VM_ENTRY_MARK;
188
  constantPoolHandle cpool(_method->get_Method()->constants());
189
  return CURRENT_ENV->get_klass_by_index(cpool, get_klass_index(), will_link, _holder);
D
duke 已提交
190 191 192
}

// ------------------------------------------------------------------
193
// ciBytecodeStream::get_constant_raw_index
D
duke 已提交
194 195 196
//
// If this bytecode is one of the ldc variants, get the index of the
// referenced constant.
197 198 199
int ciBytecodeStream::get_constant_raw_index() const {
  // work-alike for Bytecode_loadconstant::raw_index()
  switch (cur_bc()) {
D
duke 已提交
200
  case Bytecodes::_ldc:
201
    return get_index_u1();
D
duke 已提交
202 203
  case Bytecodes::_ldc_w:
  case Bytecodes::_ldc2_w:
204
    return get_index_u2();
D
duke 已提交
205 206 207 208 209
  default:
    ShouldNotReachHere();
    return 0;
  }
}
210 211 212

// ------------------------------------------------------------------
// ciBytecodeStream::get_constant_pool_index
213
// Decode any reference index into a regular pool index.
214 215 216 217
int ciBytecodeStream::get_constant_pool_index() const {
  // work-alike for Bytecode_loadconstant::pool_index()
  int index = get_constant_raw_index();
  if (has_cache_index()) {
218 219 220
    VM_ENTRY_MARK;
    constantPoolHandle cpool(_method->get_Method()->constants());
    return cpool->object_to_cp_index(index);
221 222 223 224 225 226 227 228 229 230 231 232
  }
  return index;
}

// ------------------------------------------------------------------
// ciBytecodeStream::get_constant_cache_index
// Return the CP cache index, or -1 if there isn't any.
int ciBytecodeStream::get_constant_cache_index() const {
  // work-alike for Bytecode_loadconstant::cache_index()
  return has_cache_index() ? get_constant_raw_index() : -1;
}

D
duke 已提交
233 234 235 236 237 238
// ------------------------------------------------------------------
// ciBytecodeStream::get_constant
//
// If this bytecode is one of the ldc variants, get the referenced
// constant.
ciConstant ciBytecodeStream::get_constant() {
239 240 241 242 243 244
  int pool_index = get_constant_raw_index();
  int cache_index = -1;
  if (has_cache_index()) {
    cache_index = pool_index;
    pool_index = -1;
  }
245
  VM_ENTRY_MARK;
246
  constantPoolHandle cpool(_method->get_Method()->constants());
247
  return CURRENT_ENV->get_constant_by_index(cpool, pool_index, cache_index, _holder);
D
duke 已提交
248 249 250
}

// ------------------------------------------------------------------
251 252 253 254 255 256
// ciBytecodeStream::get_constant_pool_tag
//
// If this bytecode is one of the ldc variants, get the referenced
// constant.
constantTag ciBytecodeStream::get_constant_pool_tag(int index) const {
  VM_ENTRY_MARK;
257
  return _method->get_Method()->constants()->tag_at(index);
D
duke 已提交
258 259 260 261 262 263 264 265 266 267 268 269
}

// ------------------------------------------------------------------
// ciBytecodeStream::get_field_index
//
// If this is a field access bytecode, get the constant pool
// index of the referenced field.
int ciBytecodeStream::get_field_index() {
  assert(cur_bc() == Bytecodes::_getfield ||
         cur_bc() == Bytecodes::_putfield ||
         cur_bc() == Bytecodes::_getstatic ||
         cur_bc() == Bytecodes::_putstatic, "wrong bc");
270
  return get_index_u2_cpcache();
D
duke 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
}


// ------------------------------------------------------------------
// ciBytecodeStream::get_field
//
// If this bytecode is one of get_field, get_static, put_field,
// or put_static, get the referenced field.
ciField* ciBytecodeStream::get_field(bool& will_link) {
  ciField* f = CURRENT_ENV->get_field_by_index(_holder, get_field_index());
  will_link = f->will_link(_holder, _bc);
  return f;
}


// ------------------------------------------------------------------
// ciBytecodeStream::get_declared_field_holder
//
// Get the declared holder of the currently referenced field.
//
// Usage note: the holder() of a ciField class returns the canonical
// holder of the field, rather than the holder declared in the
// bytecodes.
//
// There is no "will_link" result passed back.  The user is responsible
// for checking linkability when retrieving the associated field.
ciInstanceKlass* ciBytecodeStream::get_declared_field_holder() {
298
  VM_ENTRY_MARK;
299
  constantPoolHandle cpool(_method->get_Method()->constants());
D
duke 已提交
300 301
  int holder_index = get_field_holder_index();
  bool ignore;
302
  return CURRENT_ENV->get_klass_by_index(cpool, holder_index, ignore, _holder)
D
duke 已提交
303 304 305 306 307 308 309 310 311 312
      ->as_instance_klass();
}

// ------------------------------------------------------------------
// ciBytecodeStream::get_field_holder_index
//
// Get the constant pool index of the declared holder of the field
// referenced by the current bytecode.  Used for generating
// deoptimization information.
int ciBytecodeStream::get_field_holder_index() {
313
  GUARDED_VM_ENTRY(
314
    ConstantPool* cpool = _holder->get_instanceKlass()->constants();
315 316
    return cpool->klass_ref_index_at(get_field_index());
  )
D
duke 已提交
317 318 319 320 321 322 323 324 325 326
}

// ------------------------------------------------------------------
// ciBytecodeStream::get_field_signature_index
//
// Get the constant pool index of the signature of the field
// referenced by the current bytecode.  Used for generating
// deoptimization information.
int ciBytecodeStream::get_field_signature_index() {
  VM_ENTRY_MARK;
327
  ConstantPool* cpool = _holder->get_instanceKlass()->constants();
D
duke 已提交
328 329 330 331 332 333 334 335 336 337
  int nt_index = cpool->name_and_type_ref_index_at(get_field_index());
  return cpool->signature_ref_index_at(nt_index);
}

// ------------------------------------------------------------------
// ciBytecodeStream::get_method_index
//
// If this is a method invocation bytecode, get the constant pool
// index of the invoked method.
int ciBytecodeStream::get_method_index() {
338
#ifdef ASSERT
D
duke 已提交
339 340 341 342 343
  switch (cur_bc()) {
  case Bytecodes::_invokeinterface:
  case Bytecodes::_invokevirtual:
  case Bytecodes::_invokespecial:
  case Bytecodes::_invokestatic:
344 345
  case Bytecodes::_invokedynamic:
    break;
D
duke 已提交
346 347 348
  default:
    ShouldNotReachHere();
  }
349
#endif
350 351 352
  if (has_index_u4())
    return get_index_u4();  // invokedynamic
  return get_index_u2_cpcache();
D
duke 已提交
353 354 355 356 357 358
}

// ------------------------------------------------------------------
// ciBytecodeStream::get_method
//
// If this is a method invocation bytecode, get the invoked method.
359 360 361
// Additionally return the declared signature to get more concrete
// type information if required (Cf. invokedynamic and invokehandle).
ciMethod* ciBytecodeStream::get_method(bool& will_link, ciSignature* *declared_signature_result) {
362
  VM_ENTRY_MARK;
363
  ciEnv* env = CURRENT_ENV;
364
  constantPoolHandle cpool(THREAD, _method->get_Method()->constants());
365
  ciMethod* m = env->get_method_by_index(cpool, get_method_index(), cur_bc(), _holder);
D
duke 已提交
366
  will_link = m->is_loaded();
367 368 369 370

  // Use the MethodType stored in the CP cache to create a signature
  // with correct types (in respect to class loaders).
  if (has_method_type()) {
371
    ciSymbol*     sig_sym     = env->get_symbol(cpool->symbol_at(get_method_signature_index(cpool)));
372 373 374 375
    ciKlass*      pool_holder = env->get_klass(cpool->pool_holder());
    ciMethodType* method_type = get_method_type();
    ciSignature* declared_signature = new (env->arena()) ciSignature(pool_holder, sig_sym, method_type);
    (*declared_signature_result) = declared_signature;
376 377 378
  } else {
    (*declared_signature_result) = m->signature();
  }
D
duke 已提交
379 380 381
  return m;
}

382 383 384 385 386 387 388
// ------------------------------------------------------------------
// ciBytecodeStream::has_appendix
//
// Returns true if there is an appendix argument stored in the
// constant pool cache at the current bci.
bool ciBytecodeStream::has_appendix() {
  VM_ENTRY_MARK;
389 390
  constantPoolHandle cpool(_method->get_Method()->constants());
  return ConstantPool::has_appendix_at_if_loaded(cpool, get_method_index());
391 392 393 394 395 396 397 398 399
}

// ------------------------------------------------------------------
// ciBytecodeStream::get_appendix
//
// Return the appendix argument stored in the constant pool cache at
// the current bci.
ciObject* ciBytecodeStream::get_appendix() {
  VM_ENTRY_MARK;
400 401
  constantPoolHandle cpool(_method->get_Method()->constants());
  oop appendix_oop = ConstantPool::appendix_at_if_loaded(cpool, get_method_index());
402 403 404
  return CURRENT_ENV->get_object(appendix_oop);
}

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
// ------------------------------------------------------------------
// ciBytecodeStream::has_method_type
//
// Returns true if there is a MethodType argument stored in the
// constant pool cache at the current bci.
bool ciBytecodeStream::has_method_type() {
  GUARDED_VM_ENTRY(
    constantPoolHandle cpool(_method->get_Method()->constants());
    return ConstantPool::has_method_type_at_if_loaded(cpool, get_method_index());
  )
}

// ------------------------------------------------------------------
// ciBytecodeStream::get_method_type
//
// Return the MethodType stored in the constant pool cache at
// the current bci.
ciMethodType* ciBytecodeStream::get_method_type() {
  GUARDED_VM_ENTRY(
    constantPoolHandle cpool(_method->get_Method()->constants());
    oop method_type_oop = ConstantPool::method_type_at_if_loaded(cpool, get_method_index());
    return CURRENT_ENV->get_object(method_type_oop)->as_method_type();
  )
}

D
duke 已提交
430 431 432 433 434 435 436 437 438 439 440 441
// ------------------------------------------------------------------
// ciBytecodeStream::get_declared_method_holder
//
// Get the declared holder of the currently referenced method.
//
// Usage note: the holder() of a ciMethod class returns the canonical
// holder of the method, rather than the holder declared in the
// bytecodes.
//
// There is no "will_link" result passed back.  The user is responsible
// for checking linkability when retrieving the associated method.
ciKlass* ciBytecodeStream::get_declared_method_holder() {
442
  VM_ENTRY_MARK;
443
  constantPoolHandle cpool(_method->get_Method()->constants());
D
duke 已提交
444
  bool ignore;
445
  // report as MethodHandle for invokedynamic, which is syntactically classless
446
  if (cur_bc() == Bytecodes::_invokedynamic)
447
    return CURRENT_ENV->get_klass_by_name(_holder, ciSymbol::java_lang_invoke_MethodHandle(), false);
448
  return CURRENT_ENV->get_klass_by_index(cpool, get_method_holder_index(), ignore, _holder);
D
duke 已提交
449 450 451 452 453 454 455 456 457
}

// ------------------------------------------------------------------
// ciBytecodeStream::get_method_holder_index
//
// Get the constant pool index of the declared holder of the method
// referenced by the current bytecode.  Used for generating
// deoptimization information.
int ciBytecodeStream::get_method_holder_index() {
458
  ConstantPool* cpool = _method->get_Method()->constants();
D
duke 已提交
459 460 461 462 463 464 465 466 467
  return cpool->klass_ref_index_at(get_method_index());
}

// ------------------------------------------------------------------
// ciBytecodeStream::get_method_signature_index
//
// Get the constant pool index of the signature of the method
// referenced by the current bytecode.  Used for generating
// deoptimization information.
468
int ciBytecodeStream::get_method_signature_index(const constantPoolHandle& cpool) {
469 470 471 472 473
  GUARDED_VM_ENTRY(
    const int method_index = get_method_index();
    const int name_and_type_index = cpool->name_and_type_ref_index_at(method_index);
    return cpool->signature_ref_index_at(name_and_type_index);
  )
D
duke 已提交
474
}
475 476

// ------------------------------------------------------------------
477 478
// ciBytecodeStream::get_resolved_references
ciObjArray* ciBytecodeStream::get_resolved_references() {
479 480
    VM_ENTRY_MARK;
    // Get the constant pool.
481
  ConstantPool*        cpool   = _holder->get_instanceKlass()->constants();
482

483 484
  // Create a resolved references array and return it.
  return CURRENT_ENV->get_object(cpool->resolved_references())->as_obj_array();
485
  }