1 /*
   2  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "compiler/compileLog.hpp"
  27 #include "interpreter/linkResolver.hpp"
  28 #include "memory/universe.inline.hpp"
  29 #include "oops/objArrayKlass.hpp"
  30 #include "oops/valueArrayKlass.hpp"
  31 #include "opto/addnode.hpp"
  32 #include "opto/castnode.hpp"
  33 #include "opto/memnode.hpp"
  34 #include "opto/parse.hpp"
  35 #include "opto/rootnode.hpp"
  36 #include "opto/runtime.hpp"
  37 #include "opto/subnode.hpp"
  38 #include "opto/valuetypenode.hpp"
  39 #include "runtime/deoptimization.hpp"
  40 #include "runtime/handles.inline.hpp"
  41 
  42 //=============================================================================
  43 // Helper methods for _get* and _put* bytecodes
  44 //=============================================================================
  45 bool Parse::static_field_ok_in_clinit(ciField *field, ciMethod *method) {
  46   // Could be the field_holder's <clinit> method, or <clinit> for a subklass.
  47   // Better to check now than to Deoptimize as soon as we execute
  48   assert( field->is_static(), "Only check if field is static");
  49   // is_being_initialized() is too generous.  It allows access to statics
  50   // by threads that are not running the <clinit> before the <clinit> finishes.
  51   // return field->holder()->is_being_initialized();
  52 
  53   // The following restriction is correct but conservative.
  54   // It is also desirable to allow compilation of methods called from <clinit>
  55   // but this generated code will need to be made safe for execution by
  56   // other threads, or the transition from interpreted to compiled code would
  57   // need to be guarded.
  58   ciInstanceKlass *field_holder = field->holder();
  59 
  60   bool access_OK = false;
  61   if (method->holder()->is_subclass_of(field_holder)) {
  62     if (method->is_static()) {
  63       if (method->name() == ciSymbol::class_initializer_name()) {
  64         // OK to access static fields inside initializer
  65         access_OK = true;
  66       }
  67     } else {
  68       if (method->name() == ciSymbol::object_initializer_name()) {
  69         // It's also OK to access static fields inside a constructor,
  70         // because any thread calling the constructor must first have
  71         // synchronized on the class by executing a '_new' bytecode.
  72         access_OK = true;
  73       }
  74     }
  75   }
  76 
  77   return access_OK;
  78 
  79 }
  80 
  81 
  82 void Parse::do_field_access(bool is_get, bool is_field) {
  83   bool will_link;
  84   ciField* field = iter().get_field(will_link);
  85   assert(will_link, "getfield: typeflow responsibility");
  86 
  87   ciInstanceKlass* field_holder = field->holder();
  88 
  89   if (is_field == field->is_static()) {
  90     // Interpreter will throw java_lang_IncompatibleClassChangeError
  91     // Check this before allowing <clinit> methods to access static fields
  92     uncommon_trap(Deoptimization::Reason_unhandled,
  93                   Deoptimization::Action_none);
  94     return;
  95   }
  96 
  97   if (!is_field && !field_holder->is_initialized()) {
  98     if (!static_field_ok_in_clinit(field, method())) {
  99       uncommon_trap(Deoptimization::Reason_uninitialized,
 100                     Deoptimization::Action_reinterpret,
 101                     NULL, "!static_field_ok_in_clinit");
 102       return;
 103     }
 104   }
 105 
 106   // Deoptimize on putfield writes to call site target field.
 107   if (!is_get && field->is_call_site_target()) {
 108     uncommon_trap(Deoptimization::Reason_unhandled,
 109                   Deoptimization::Action_reinterpret,
 110                   NULL, "put to call site target field");
 111     return;
 112   }
 113 
 114   assert(field->will_link(method(), bc()), "getfield: typeflow responsibility");
 115 
 116   // Note:  We do not check for an unloaded field type here any more.
 117 
 118   // Generate code for the object pointer.
 119   Node* obj;
 120   if (is_field) {
 121     int obj_depth = is_get ? 0 : field->type()->size();
 122     obj = null_check(peek(obj_depth));
 123     // Compile-time detect of null-exception?
 124     if (stopped())  return;
 125 
 126 #ifdef ASSERT
 127     const TypeInstPtr *tjp = TypeInstPtr::make(TypePtr::NotNull, iter().get_declared_field_holder());
 128     assert(_gvn.type(obj)->higher_equal(tjp), "cast_up is no longer needed");
 129 #endif
 130 
 131     if (is_get) {
 132       (void) pop();  // pop receiver before getting
 133       do_get_xxx(obj, field, is_field);
 134     } else {
 135       do_put_xxx(obj, field, is_field);
 136       (void) pop();  // pop receiver after putting
 137     }
 138   } else {
 139     const TypeInstPtr* tip = TypeInstPtr::make(field_holder->java_mirror());
 140     obj = _gvn.makecon(tip);
 141     if (is_get) {
 142       do_get_xxx(obj, field, is_field);
 143     } else {
 144       do_put_xxx(obj, field, is_field);
 145     }
 146   }
 147 }
 148 
 149 void Parse::do_vgetfield() {
 150   // fixme null/top check?
 151   bool will_link;
 152   ciField* field = iter().get_field(will_link);
 153   BasicType bt = field->layout_type();
 154   ValueTypeNode* vt = pop()->as_ValueType();
 155   Node* value = vt->field_value_by_offset(field->offset());
 156   push_node(bt, value);
 157 }
 158 
 159 void Parse::do_get_xxx(Node* obj, ciField* field, bool is_field) {
 160   BasicType bt = field->layout_type();
 161 
 162   // Does this field have a constant value?  If so, just push the value.
 163   if (field->is_constant() &&
 164       // Keep consistent with types found by ciTypeFlow: for an
 165       // unloaded field type, ciTypeFlow::StateVector::do_getstatic()
 166       // speculates the field is null. The code in the rest of this
 167       // method does the same. We must not bypass it and use a non
 168       // null constant here.
 169       (bt != T_OBJECT || field->type()->is_loaded())) {
 170     // final or stable field
 171     Node* con = make_constant_from_field(field, obj);
 172     if (con != NULL) {
 173       push_node(field->layout_type(), con);
 174       return;
 175     }
 176   }
 177 
 178   ciType* field_klass = field->type();
 179   bool is_vol = field->is_volatile();
 180 
 181   // Compute address and memory type.
 182   int offset = field->offset_in_bytes();
 183   const TypePtr* adr_type = C->alias_type(field)->adr_type();
 184   Node *adr = basic_plus_adr(obj, obj, offset);
 185 
 186   // Build the resultant type of the load
 187   const Type *type;
 188 
 189   bool must_assert_null = false;
 190   if (bt == T_OBJECT || bt == T_VALUETYPE) {
 191     if (!field->type()->is_loaded()) {
 192       type = TypeInstPtr::BOTTOM;
 193       must_assert_null = true;
 194     } else if (field->is_static_constant()) {
 195       // This can happen if the constant oop is non-perm.
 196       ciObject* con = field->constant_value().as_object();
 197       // Do not "join" in the previous type; it doesn't add value,
 198       // and may yield a vacuous result if the field is of interface type.
 199       if (con->is_null_object()) {
 200         type = TypePtr::NULL_PTR;
 201       } else {
 202         type = TypeOopPtr::make_from_constant(con)->isa_oopptr();
 203       }
 204       assert(type != NULL, "field singleton type must be consistent");
 205     } else {
 206       type = TypeOopPtr::make_from_klass(field_klass->as_klass());
 207     }
 208   } else {
 209     type = Type::get_const_basic_type(bt);
 210   }
 211   if (support_IRIW_for_not_multiple_copy_atomic_cpu && field->is_volatile()) {
 212     insert_mem_bar(Op_MemBarVolatile);   // StoreLoad barrier
 213   }
 214 
 215   // Build the load.
 216   //
 217   MemNode::MemOrd mo = is_vol ? MemNode::acquire : MemNode::unordered;
 218   bool needs_atomic_access = is_vol || AlwaysAtomicAccesses;
 219   Node* ld = NULL;
 220    if (bt == T_VALUETYPE && !field->is_static()) {
 221     // Load flattened value type from non-static field
 222     ld = ValueTypeNode::make(_gvn, field_klass->as_value_klass(), map()->memory(), obj, obj, field->holder(), offset);
 223   } else {
 224     ld = make_load(NULL, adr, type, bt, adr_type, mo, LoadNode::DependsOnlyOnTest, needs_atomic_access);
 225   }
 226 
 227   // Adjust Java stack
 228   if (type2size[bt] == 1)
 229     push(ld);
 230   else
 231     push_pair(ld);
 232 
 233   if (must_assert_null) {
 234     // Do not take a trap here.  It's possible that the program
 235     // will never load the field's class, and will happily see
 236     // null values in this field forever.  Don't stumble into a
 237     // trap for such a program, or we might get a long series
 238     // of useless recompilations.  (Or, we might load a class
 239     // which should not be loaded.)  If we ever see a non-null
 240     // value, we will then trap and recompile.  (The trap will
 241     // not need to mention the class index, since the class will
 242     // already have been loaded if we ever see a non-null value.)
 243     // uncommon_trap(iter().get_field_signature_index());
 244     if (PrintOpto && (Verbose || WizardMode)) {
 245       method()->print_name(); tty->print_cr(" asserting nullness of field at bci: %d", bci());
 246     }
 247     if (C->log() != NULL) {
 248       C->log()->elem("assert_null reason='field' klass='%d'",
 249                      C->log()->identify(field->type()));
 250     }
 251     // If there is going to be a trap, put it at the next bytecode:
 252     set_bci(iter().next_bci());
 253     null_assert(peek());
 254     set_bci(iter().cur_bci()); // put it back
 255   }
 256 
 257   // If reference is volatile, prevent following memory ops from
 258   // floating up past the volatile read.  Also prevents commoning
 259   // another volatile read.
 260   if (field->is_volatile()) {
 261     // Memory barrier includes bogus read of value to force load BEFORE membar
 262     insert_mem_bar(Op_MemBarAcquire, ld);
 263   }
 264 }
 265 
 266 void Parse::do_put_xxx(Node* obj, ciField* field, bool is_field) {
 267   bool is_vol = field->is_volatile();
 268   // If reference is volatile, prevent following memory ops from
 269   // floating down past the volatile write.  Also prevents commoning
 270   // another volatile read.
 271   if (is_vol)  insert_mem_bar(Op_MemBarRelease);
 272 
 273   // Compute address and memory type.
 274   int offset = field->offset_in_bytes();
 275   const TypePtr* adr_type = C->alias_type(field)->adr_type();
 276   Node* adr = basic_plus_adr(obj, obj, offset);
 277   BasicType bt = field->layout_type();
 278   // Value to be stored
 279   Node* val = type2size[bt] == 1 ? pop() : pop_pair();
 280   // Round doubles before storing
 281   if (bt == T_DOUBLE)  val = dstore_rounding(val);
 282 
 283   // Conservatively release stores of object references.
 284   const MemNode::MemOrd mo =
 285     is_vol ?
 286     // Volatile fields need releasing stores.
 287     MemNode::release :
 288     // Non-volatile fields also need releasing stores if they hold an
 289     // object reference, because the object reference might point to
 290     // a freshly created object.
 291     StoreNode::release_if_reference(bt);
 292 
 293   // Store the value.
 294   if (bt == T_OBJECT || bt == T_VALUETYPE) {
 295     const TypeOopPtr* field_type;
 296     if (!field->type()->is_loaded()) {
 297       field_type = TypeInstPtr::BOTTOM;
 298     } else {
 299       field_type = TypeOopPtr::make_from_klass(field->type()->as_klass());
 300     }
 301     if (bt == T_VALUETYPE && !field->is_static()) {
 302       // Store flattened value type to non-static field
 303       val->as_ValueType()->store(this, obj, obj, field->holder(), offset);
 304     } else {
 305       store_oop_to_object(control(), obj, adr, adr_type, val, field_type, bt, mo);
 306     }
 307   } else {
 308     bool needs_atomic_access = is_vol || AlwaysAtomicAccesses;
 309     store_to_memory(control(), adr, val, bt, adr_type, mo, needs_atomic_access);
 310   }
 311 
 312   // If reference is volatile, prevent following volatiles ops from
 313   // floating up before the volatile write.
 314   if (is_vol) {
 315     // If not multiple copy atomic, we do the MemBarVolatile before the load.
 316     if (!support_IRIW_for_not_multiple_copy_atomic_cpu) {
 317       insert_mem_bar(Op_MemBarVolatile); // Use fat membar
 318     }
 319     // Remember we wrote a volatile field.
 320     // For not multiple copy atomic cpu (ppc64) a barrier should be issued
 321     // in constructors which have such stores. See do_exits() in parse1.cpp.
 322     if (is_field) {
 323       set_wrote_volatile(true);
 324     }
 325   }
 326 
 327   if (is_field) {
 328     set_wrote_fields(true);
 329   }
 330 
 331   // If the field is final, the rules of Java say we are in <init> or <clinit>.
 332   // Note the presence of writes to final non-static fields, so that we
 333   // can insert a memory barrier later on to keep the writes from floating
 334   // out of the constructor.
 335   // Any method can write a @Stable field; insert memory barriers after those also.
 336   if (is_field && (field->is_final() || field->is_stable())) {
 337     if (field->is_final()) {
 338         set_wrote_final(true);
 339     }
 340     if (field->is_stable()) {
 341         set_wrote_stable(true);
 342     }
 343 
 344     // Preserve allocation ptr to create precedent edge to it in membar
 345     // generated on exit from constructor.
 346     // Can't bind stable with its allocation, only record allocation for final field.
 347     if (field->is_final() && AllocateNode::Ideal_allocation(obj, &_gvn) != NULL) {
 348       set_alloc_with_final(obj);
 349     }
 350   }
 351 }
 352 
 353 //=============================================================================
 354 
 355 void Parse::do_newarray() {
 356   bool will_link;
 357   ciKlass* klass = iter().get_klass(will_link);
 358 
 359   // Uncommon Trap when class that array contains is not loaded
 360   // we need the loaded class for the rest of graph; do not
 361   // initialize the container class (see Java spec)!!!
 362   assert(will_link, "newarray: typeflow responsibility");
 363 
 364   ciArrayKlass* array_klass = ciArrayKlass::make(klass);
 365   // Check that array_klass object is loaded
 366   if (!array_klass->is_loaded()) {
 367     // Generate uncommon_trap for unloaded array_class
 368     uncommon_trap(Deoptimization::Reason_unloaded,
 369                   Deoptimization::Action_reinterpret,
 370                   array_klass);
 371     return;
 372   } else if (array_klass->element_klass() != NULL &&
 373              array_klass->element_klass()->is_valuetype() &&
 374              !array_klass->element_klass()->as_value_klass()->is_initialized()) {
 375     uncommon_trap(Deoptimization::Reason_uninitialized,
 376                   Deoptimization::Action_reinterpret,
 377                   NULL);
 378     return;
 379   }
 380 
 381   kill_dead_locals();
 382 
 383   const TypeKlassPtr* array_klass_type = TypeKlassPtr::make(array_klass);
 384   Node* count_val = pop();
 385   Node* obj = new_array(makecon(array_klass_type), count_val, 1);
 386   push(obj);
 387 }
 388 
 389 
 390 void Parse::do_newarray(BasicType elem_type) {
 391   kill_dead_locals();
 392 
 393   Node*   count_val = pop();
 394   const TypeKlassPtr* array_klass = TypeKlassPtr::make(ciTypeArrayKlass::make(elem_type));
 395   Node*   obj = new_array(makecon(array_klass), count_val, 1);
 396   // Push resultant oop onto stack
 397   push(obj);
 398 }
 399 
 400 // Expand simple expressions like new int[3][5] and new Object[2][nonConLen].
 401 // Also handle the degenerate 1-dimensional case of anewarray.
 402 Node* Parse::expand_multianewarray(ciArrayKlass* array_klass, Node* *lengths, int ndimensions, int nargs) {
 403   Node* length = lengths[0];
 404   assert(length != NULL, "");
 405   Node* array = new_array(makecon(TypeKlassPtr::make(array_klass)), length, nargs);
 406   if (ndimensions > 1) {
 407     jint length_con = find_int_con(length, -1);
 408     guarantee(length_con >= 0, "non-constant multianewarray");
 409     ciArrayKlass* array_klass_1 = array_klass->as_obj_array_klass()->element_klass()->as_array_klass();
 410     const TypePtr* adr_type = TypeAryPtr::OOPS;
 411     const TypeOopPtr*    elemtype = _gvn.type(array)->is_aryptr()->elem()->make_oopptr();
 412     const intptr_t header   = arrayOopDesc::base_offset_in_bytes(T_OBJECT);
 413     for (jint i = 0; i < length_con; i++) {
 414       Node*    elem   = expand_multianewarray(array_klass_1, &lengths[1], ndimensions-1, nargs);
 415       intptr_t offset = header + ((intptr_t)i << LogBytesPerHeapOop);
 416       Node*    eaddr  = basic_plus_adr(array, offset);
 417       store_oop_to_array(control(), array, eaddr, adr_type, elem, elemtype, T_OBJECT, MemNode::unordered);
 418     }
 419   }
 420   return array;
 421 }
 422 
 423 void Parse::do_multianewarray() {
 424   int ndimensions = iter().get_dimensions();
 425 
 426   // the m-dimensional array
 427   bool will_link;
 428   ciArrayKlass* array_klass = iter().get_klass(will_link)->as_array_klass();
 429   assert(will_link, "multianewarray: typeflow responsibility");
 430 
 431   // Note:  Array classes are always initialized; no is_initialized check.
 432 
 433   kill_dead_locals();
 434 
 435   // get the lengths from the stack (first dimension is on top)
 436   Node** length = NEW_RESOURCE_ARRAY(Node*, ndimensions + 1);
 437   length[ndimensions] = NULL;  // terminating null for make_runtime_call
 438   int j;
 439   for (j = ndimensions-1; j >= 0 ; j--) length[j] = pop();
 440 
 441   // The original expression was of this form: new T[length0][length1]...
 442   // It is often the case that the lengths are small (except the last).
 443   // If that happens, use the fast 1-d creator a constant number of times.
 444   const jint expand_limit = MIN2((jint)MultiArrayExpandLimit, 100);
 445   jint expand_count = 1;        // count of allocations in the expansion
 446   jint expand_fanout = 1;       // running total fanout
 447   for (j = 0; j < ndimensions-1; j++) {
 448     jint dim_con = find_int_con(length[j], -1);
 449     expand_fanout *= dim_con;
 450     expand_count  += expand_fanout; // count the level-J sub-arrays
 451     if (dim_con <= 0
 452         || dim_con > expand_limit
 453         || expand_count > expand_limit) {
 454       expand_count = 0;
 455       break;
 456     }
 457   }
 458 
 459   // Can use multianewarray instead of [a]newarray if only one dimension,
 460   // or if all non-final dimensions are small constants.
 461   if (ndimensions == 1 || (1 <= expand_count && expand_count <= expand_limit)) {
 462     Node* obj = NULL;
 463     // Set the original stack and the reexecute bit for the interpreter
 464     // to reexecute the multianewarray bytecode if deoptimization happens.
 465     // Do it unconditionally even for one dimension multianewarray.
 466     // Note: the reexecute bit will be set in GraphKit::add_safepoint_edges()
 467     // when AllocateArray node for newarray is created.
 468     { PreserveReexecuteState preexecs(this);
 469       inc_sp(ndimensions);
 470       // Pass 0 as nargs since uncommon trap code does not need to restore stack.
 471       obj = expand_multianewarray(array_klass, &length[0], ndimensions, 0);
 472     } //original reexecute and sp are set back here
 473     push(obj);
 474     return;
 475   }
 476 
 477   address fun = NULL;
 478   switch (ndimensions) {
 479   case 1: ShouldNotReachHere(); break;
 480   case 2: fun = OptoRuntime::multianewarray2_Java(); break;
 481   case 3: fun = OptoRuntime::multianewarray3_Java(); break;
 482   case 4: fun = OptoRuntime::multianewarray4_Java(); break;
 483   case 5: fun = OptoRuntime::multianewarray5_Java(); break;
 484   };
 485   Node* c = NULL;
 486 
 487   if (fun != NULL) {
 488     c = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
 489                           OptoRuntime::multianewarray_Type(ndimensions),
 490                           fun, NULL, TypeRawPtr::BOTTOM,
 491                           makecon(TypeKlassPtr::make(array_klass)),
 492                           length[0], length[1], length[2],
 493                           (ndimensions > 2) ? length[3] : NULL,
 494                           (ndimensions > 3) ? length[4] : NULL);
 495   } else {
 496     // Create a java array for dimension sizes
 497     Node* dims = NULL;
 498     { PreserveReexecuteState preexecs(this);
 499       inc_sp(ndimensions);
 500       Node* dims_array_klass = makecon(TypeKlassPtr::make(ciArrayKlass::make(ciType::make(T_INT))));
 501       dims = new_array(dims_array_klass, intcon(ndimensions), 0);
 502 
 503       // Fill-in it with values
 504       for (j = 0; j < ndimensions; j++) {
 505         Node *dims_elem = array_element_address(dims, intcon(j), T_INT);
 506         store_to_memory(control(), dims_elem, length[j], T_INT, TypeAryPtr::INTS, MemNode::unordered);
 507       }
 508     }
 509 
 510     c = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
 511                           OptoRuntime::multianewarrayN_Type(),
 512                           OptoRuntime::multianewarrayN_Java(), NULL, TypeRawPtr::BOTTOM,
 513                           makecon(TypeKlassPtr::make(array_klass)),
 514                           dims);
 515   }
 516   make_slow_call_ex(c, env()->Throwable_klass(), false);
 517 
 518   Node* res = _gvn.transform(new ProjNode(c, TypeFunc::Parms));
 519 
 520   const Type* type = TypeOopPtr::make_from_klass_raw(array_klass);
 521 
 522   // Improve the type:  We know it's not null, exact, and of a given length.
 523   type = type->is_ptr()->cast_to_ptr_type(TypePtr::NotNull);
 524   type = type->is_aryptr()->cast_to_exactness(true);
 525 
 526   const TypeInt* ltype = _gvn.find_int_type(length[0]);
 527   if (ltype != NULL)
 528     type = type->is_aryptr()->cast_to_size(ltype);
 529 
 530     // We cannot sharpen the nested sub-arrays, since the top level is mutable.
 531 
 532   Node* cast = _gvn.transform( new CheckCastPPNode(control(), res, type) );
 533   push(cast);
 534 
 535   // Possible improvements:
 536   // - Make a fast path for small multi-arrays.  (W/ implicit init. loops.)
 537   // - Issue CastII against length[*] values, to TypeInt::POS.
 538 }
 539 
 540 void Parse::do_vbox() {
 541   // Obtain target type (from bytecodes)
 542   bool will_link;
 543   ciKlass* target_klass = iter().get_klass(will_link);
 544   guarantee(will_link, "vbox: Value-capable class must be loaded");
 545   guarantee(target_klass->is_instance_klass(), "vbox: Target class must be an instance type");
 546 
 547   // Obtain source type
 548   ValueTypeNode* vt = peek()->as_ValueType();
 549   const TypeValueType* src_type = gvn().type(vt)->isa_valuetype();
 550   guarantee(src_type != NULL, "vbox: Source type must not be null");
 551   ciValueKlass* src_vk = src_type->value_klass();
 552   guarantee(src_vk != NULL && src_vk->is_loaded() && src_vk->exact_klass(),
 553             "vbox: Source class must be a value type and must be loaded and exact");
 554 
 555   kill_dead_locals();
 556 
 557   ciInstanceKlass* target_vcc_klass = target_klass->as_instance_klass();
 558   ciInstanceKlass* src_vcc_klass = src_vk->vcc_klass();;
 559 
 560   // TODO: Extend type check below if (and once) value type class hierarchies become available.
 561   // (incl. extension to support dynamic type checks).
 562   if (!src_vcc_klass->equals(target_vcc_klass)) {
 563     builtin_throw(Deoptimization::Reason_class_check);
 564     guarantee(stopped(), "A ClassCastException must be always thrown on this path");
 565     return;
 566   }
 567   guarantee(src_vk->is_valuetype(), "vbox: Target DVT must be a value type");
 568   pop();
 569 
 570   // Create new object
 571   Node* kls = makecon(TypeKlassPtr::make(target_vcc_klass));
 572   Node* obj = new_instance(kls);
 573 
 574   // Store all field values to the newly created object.
 575   // The code below relies on the assumption that the VCC has the
 576   // same memory layout as the derived value type.
 577   // TODO: Once the layout of the two is not the same, update code below.
 578   vt->as_ValueType()->store_values(this, obj, obj, target_vcc_klass);
 579 
 580   // Push the new object onto the stack
 581   push(obj);
 582 }
 583 
 584 void Parse::do_vunbox() {
 585   kill_dead_locals();
 586 
 587   // Check if the VCC instance is null.
 588   Node* not_null_obj = null_check(peek());
 589 
 590   // Value determined to be null at compile time
 591   if (stopped()) {
 592     return;
 593   }
 594 
 595   // Obtain target type (from bytecodes)
 596   bool will_link;
 597   ciKlass* target_klass = iter().get_klass(will_link);
 598   guarantee(will_link, "vunbox: Derived value type must be loaded");
 599   guarantee(target_klass->is_instance_klass(), "vunbox: Target class must be an instance type");
 600 
 601   // Obtain source type
 602   const TypeOopPtr* source_type = gvn().type(not_null_obj)->isa_oopptr();
 603   guarantee(source_type != NULL && source_type->klass() != NULL &&
 604             source_type->klass()->is_instance_klass() && source_type->klass()->is_loaded(),
 605             "vunbox: Source class must be an instance type and must be loaded");
 606 
 607   ciInstanceKlass* target_dvt_klass = target_klass->as_instance_klass();
 608   ciInstanceKlass* target_vcc_klass = target_dvt_klass->vcc_klass();
 609 
 610   // Check if the class of the source is a subclass of the value-capable class
 611   // corresponding to the target.
 612   // TOOD: Implement profiling of vunbox bytecodes to enable type speculation.
 613   if (target_vcc_klass == NULL || !source_type->klass()->is_subclass_of(target_vcc_klass)) {
 614     // It is obvious at compile-time that source and target are unrelated.
 615     builtin_throw(Deoptimization::Reason_class_check);
 616     guarantee(stopped(), "A ClassCastException must be always thrown on this path");
 617     return;
 618   }
 619   guarantee(target_dvt_klass->is_valuetype(), "vunbox: Target DVT must be a value type");
 620 
 621   if (!target_vcc_klass->equals(source_type->klass()) || !source_type->klass_is_exact()) {
 622     Node* exact_obj = not_null_obj;
 623     Node* slow_ctl  = type_check_receiver(exact_obj, target_vcc_klass, 1.0, &exact_obj);
 624     {
 625       PreserveJVMState pjvms(this);
 626       set_control(slow_ctl);
 627       builtin_throw(Deoptimization::Reason_class_check);
 628     }
 629     replace_in_map(not_null_obj, exact_obj);
 630     not_null_obj = exact_obj;
 631   }
 632 
 633   // Remove object from the top of the stack
 634   pop();
 635 
 636   // Create a value type node with the corresponding type
 637   ciValueKlass* vk = target_dvt_klass->as_value_klass();
 638   Node* vt = ValueTypeNode::make(gvn(), vk, map()->memory(), not_null_obj, not_null_obj, target_vcc_klass, vk->first_field_offset());
 639 
 640   // Push the value type onto the stack
 641   push(vt);
 642 }