1 /*
   2  * Copyright (c) 2016, 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 "ci/ciField.hpp"
  27 #include "ci/ciValueKlass.hpp"
  28 #include "oops/fieldStreams.hpp"
  29 #include "oops/valueKlass.hpp"
  30 
  31 int ciValueKlass::compute_field_index_map() {
  32   assert(is_loaded(), "value class must be loaded to compute mapping of field indeces");
  33 
  34   if (_field_index_map != NULL) {
  35     return _field_index_map->length();
  36   }
  37 
  38   Arena* arena = CURRENT_ENV->arena();
  39   guarantee(has_nonstatic_fields(), "value types without fields currently not supported");
  40 
  41   // FIXME: Once it is possible to construct class hierarchies with value types.
  42   assert(!super()->has_nonstatic_fields(), "a value type must not inherit fields from its superclass");
  43 
  44   _field_index_map = new (arena) GrowableArray<int>(arena, nof_declared_nonstatic_fields(), 0, 0);
  45   ValueKlass* vklass = ValueKlass::cast(get_Klass());
  46   for (JavaFieldStream fs(vklass); !fs.done(); fs.next()) {
  47     if (fs.access_flags().is_static()) {
  48       continue;
  49     }
  50     _field_index_map->append(fs.field_descriptor().index());
  51   }
  52   return _field_index_map->length();
  53 }
  54 
  55 // Number of value type fields
  56 int ciValueKlass::field_count() {
  57   if (this == ciEnv::current()->___Value_klass()) {
  58     return 0;
  59   }
  60   if (_field_index_map == NULL) {
  61     return compute_field_index_map();
  62   } else {
  63     return _field_index_map->length();
  64   }
  65 }
  66 
  67 // Size of value type fields in words
  68 int ciValueKlass::field_size() {
  69   int size = 0;
  70   for (int i = 0; i < field_count(); ++i) {
  71     size += field_type_by_index(i)->size();
  72   }
  73   return size;
  74 }
  75 
  76 // Returns the index of the field with the given offset. If the field at 'offset'
  77 // belongs to a flattened value type field, return the index of the field
  78 // in the flattened value type.
  79 int ciValueKlass::field_index_by_offset(int offset) {
  80   assert(contains_field_offset(offset), "invalid field offset");
  81   int best_offset = 0;
  82   int best_index = -1;
  83   // Search the field with the given offset
  84   for (int i = 0; i < field_count(); ++i) {
  85     int field_offset = field_offset_by_index(i);
  86     if (field_offset == offset) {
  87       // Exact match
  88       return i;
  89     } else if (field_offset < offset && field_offset > best_offset) {
  90       // No exact match. Save the index of the field with the closest offset that
  91       // is smaller than the given field offset. This index corresponds to the
  92       // flattened value type field that holds the field we are looking for.
  93       best_offset = field_offset;
  94       best_index = i;
  95     }
  96   }
  97   assert(best_index >= 0, "field not found");
  98   assert(best_offset == offset || field_type_by_index(best_index)->is_valuetype(), "offset should match for non-VTs");
  99   return best_index;
 100 }
 101 
 102 // Returns the field offset of the field with the given index
 103 int ciValueKlass::field_offset_by_index(int index) {
 104   if (_field_index_map == NULL) {
 105     compute_field_index_map();
 106   }
 107   GUARDED_VM_ENTRY(
 108     ValueKlass* vklass = ValueKlass::cast(get_Klass());
 109     return vklass->field_offset(_field_index_map->at(index));
 110   )
 111 }
 112 
 113 // Returns the field type of the field with the given index
 114 ciType* ciValueKlass::field_type_by_index(int index) {
 115   int offset = field_offset_by_index(index);
 116   VM_ENTRY_MARK;
 117   return get_field_type_by_offset(offset);
 118 }
 119 
 120 // Offset of the first field in the value type
 121 int ciValueKlass::first_field_offset() const {
 122   GUARDED_VM_ENTRY(
 123     ValueKlass* vklass = ValueKlass::cast(get_Klass());
 124     return vklass->first_field_offset();
 125   )
 126 }
 127 
 128 bool ciValueKlass::flatten_array() const {
 129   GUARDED_VM_ENTRY(
 130     ValueKlass* vklass = ValueKlass::cast(get_Klass());
 131     return vklass->flatten_array();
 132   )
 133 }
 134 
 135 bool ciValueKlass::contains_oops() const {
 136   GUARDED_VM_ENTRY(
 137     ValueKlass* vklass = ValueKlass::cast(get_Klass());
 138     return vklass->contains_oops();
 139   )
 140 }
 141 
 142 // When passing a value type's fields as arguments, count the number
 143 // of argument slots that are needed
 144 int ciValueKlass::value_arg_slots() {
 145   int slots = nof_nonstatic_fields();
 146   for (int j = 0; j < nof_nonstatic_fields(); j++) {
 147     ciField* f = nonstatic_field_at(j);
 148     BasicType bt = f->type()->basic_type();
 149     assert(bt != T_VALUETYPE, "embedded");
 150     if (bt == T_LONG || bt == T_DOUBLE) {
 151       slots++;
 152     }
 153   }
 154   return slots;
 155 }