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 "ci/ciField.hpp"
  26 #include "ci/ciValueKlass.hpp"
  27 #include "oops/valueKlass.hpp"
  28 
  29 // Number of value type factory parameters
  30 int ciValueKlass::param_count() const {
  31   VM_ENTRY_MARK
  32   valueKlassHandle vklass_h(ValueKlass::cast(get_Klass()));
  33   methodHandle factory_h(vklass_h->factory_method());
  34   int count = factory_h->constMethod()->valuefactory_parameter_mapping_length();
  35   return count;
  36 }
  37 
  38 // Size of value type factory parameters in words
  39 int ciValueKlass::param_size() {
  40   int size = 0;
  41   for (int i = 0; i < nof_declared_nonstatic_fields(); ++i) {
  42     size += get_field_type_by_index(i)->size();
  43   }
  44   return size;
  45 }
  46 
  47 int ciValueKlass::field_index_for_argument(int argument) {
  48   VM_ENTRY_MARK
  49   valueKlassHandle vklass_h(ValueKlass::cast(get_Klass()));
  50   methodHandle factory_h(vklass_h->factory_method());
  51   return factory_h->constMethod()->valuefactory_parameter_mapping_start()[argument].data.field_index;
  52 }
  53 
  54 // Returns the value factory parameter index of the field with the given offset.
  55 // If the field at 'offset' belongs to a flattened value type field, return the
  56 // index of the ValueType parameter corresponding to this flattened value type.
  57 int ciValueKlass::get_field_index_by_offset(int offset) {
  58   assert(contains_field_offset(offset), "invalid field offset");
  59   int best_offset = 0;
  60   int best_index = -1;
  61   // Search the field with the given offset
  62   for (int i = 0; i <  nof_declared_nonstatic_fields(); ++i) {
  63     int field_offset = get_field_offset_by_index(i);
  64     if (field_offset == offset) {
  65       // Exact match
  66       return i;
  67     } else if (field_offset < offset && field_offset > best_offset) {
  68       // No exact match. Save the index of the field with the closest offset that
  69       // is smaller than the given field offset. This index corresponds to the
  70       // flattened value type field that holds the field we are looking for.
  71       best_offset = field_offset;
  72       best_index = i;
  73     }
  74   }
  75   assert(best_index >= 0, "field not found");
  76   assert(best_offset == offset || get_field_type_by_index(best_index)->is_valuetype(), "offset should match for non-VTs");
  77   return best_index;
  78 }
  79 
  80 // Returns the field offset of the value factory parameter with the given index
  81 int ciValueKlass::get_field_offset_by_index(int index) const {
  82   // Compute the field index from the factory parameter index
  83   GUARDED_VM_ENTRY(
  84       valueKlassHandle vklass_h(ValueKlass::cast(get_Klass()));
  85       return vklass_h->field_offset(index);
  86   )
  87 }
  88 
  89 // Returns the field type of the field with the given index
  90 ciType* ciValueKlass::get_field_type_by_index(int index) {
  91   VM_ENTRY_MARK
  92   int offset = get_field_offset_by_index(index);
  93   return get_field_type_by_offset(offset);
  94 }
  95 
  96 // Offset of the first field in the value type
  97 int ciValueKlass::get_first_field_offset() const {
  98   GUARDED_VM_ENTRY(
  99       valueKlassHandle vklass_h(ValueKlass::cast(get_Klass()));
 100       return vklass_h()->first_field_offset();
 101   )
 102 }