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