1 /*
   2  * Copyright (c) 1999, 2005, 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 "incls/_precompiled.incl"
  26 #include "incls/_ciInstance.cpp.incl"
  27 
  28 // ciInstance
  29 //
  30 // This class represents an instanceOop in the HotSpot virtual
  31 // machine.
  32 
  33 // ------------------------------------------------------------------
  34 // ciObject::java_mirror_type
  35 ciType* ciInstance::java_mirror_type() {
  36   VM_ENTRY_MARK;
  37   oop m = get_oop();
  38   // Return NULL if it is not java.lang.Class.
  39   if (m == NULL || m->klass() != SystemDictionary::Class_klass()) {
  40     return NULL;
  41   }
  42   // Return either a primitive type or a klass.
  43   if (java_lang_Class::is_primitive(m)) {
  44     return ciType::make(java_lang_Class::primitive_type(m));
  45   } else {
  46     klassOop k = java_lang_Class::as_klassOop(m);
  47     assert(k != NULL, "");
  48     return CURRENT_THREAD_ENV->get_object(k)->as_klass();
  49   }
  50 }
  51 
  52 // ------------------------------------------------------------------
  53 // ciInstance::field_value
  54 //
  55 // Constant value of a field.
  56 ciConstant ciInstance::field_value(ciField* field) {
  57   assert(is_loaded() &&
  58          field->holder()->is_loaded() &&
  59          klass()->is_subclass_of(field->holder()),
  60          "invalid access");
  61   VM_ENTRY_MARK;
  62   ciConstant result;
  63   oop obj = get_oop();
  64   assert(obj != NULL, "bad oop");
  65   BasicType field_btype = field->type()->basic_type();
  66   int offset = field->offset();
  67 
  68   switch(field_btype) {
  69   case T_BYTE:
  70     return ciConstant(field_btype, obj->byte_field(offset));
  71     break;
  72   case T_CHAR:
  73     return ciConstant(field_btype, obj->char_field(offset));
  74     break;
  75   case T_SHORT:
  76     return ciConstant(field_btype, obj->short_field(offset));
  77     break;
  78   case T_BOOLEAN:
  79     return ciConstant(field_btype, obj->bool_field(offset));
  80     break;
  81   case T_INT:
  82     return ciConstant(field_btype, obj->int_field(offset));
  83     break;
  84   case T_FLOAT:
  85     return ciConstant(obj->float_field(offset));
  86     break;
  87   case T_DOUBLE:
  88     return ciConstant(obj->double_field(offset));
  89     break;
  90   case T_LONG:
  91     return ciConstant(obj->long_field(offset));
  92     break;
  93   case T_OBJECT:
  94   case T_ARRAY:
  95     {
  96       oop o = obj->obj_field(offset);
  97 
  98       // A field will be "constant" if it is known always to be
  99       // a non-null reference to an instance of a particular class,
 100       // or to a particular array.  This can happen even if the instance
 101       // or array is not perm.  In such a case, an "unloaded" ciArray
 102       // or ciInstance is created.  The compiler may be able to use
 103       // information about the object's class (which is exact) or length.
 104 
 105       if (o == NULL) {
 106         return ciConstant(field_btype, ciNullObject::make());
 107       } else {
 108         return ciConstant(field_btype, CURRENT_ENV->get_object(o));
 109       }
 110     }
 111   }
 112   ShouldNotReachHere();
 113   // to shut up the compiler
 114   return ciConstant();
 115 }
 116 
 117 // ------------------------------------------------------------------
 118 // ciInstance::field_value_by_offset
 119 //
 120 // Constant value of a field at the specified offset.
 121 ciConstant ciInstance::field_value_by_offset(int field_offset) {
 122   ciInstanceKlass* ik = klass()->as_instance_klass();
 123   ciField* field = ik->get_field_by_offset(field_offset, false);
 124   return field_value(field);
 125 }
 126 
 127 // ------------------------------------------------------------------
 128 // ciInstance::print_impl
 129 //
 130 // Implementation of the print method.
 131 void ciInstance::print_impl(outputStream* st) {
 132   st->print(" type=");
 133   klass()->print(st);
 134 }