1 /*
   2  * Copyright (c) 2017, 2019, 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/ciUtilities.inline.hpp"
  28 #include "ci/ciValueKlass.hpp"
  29 #include "oops/valueKlass.inline.hpp"
  30 
  31 int ciValueKlass::compute_nonstatic_fields() {
  32   int result = ciInstanceKlass::compute_nonstatic_fields();
  33   assert(super() == NULL || !super()->has_nonstatic_fields(), "a value type must not inherit fields from its superclass");
  34 
  35   // Compute declared non-static fields (without flattening of value type fields)
  36   GrowableArray<ciField*>* fields = NULL;
  37   GUARDED_VM_ENTRY(fields = compute_nonstatic_fields_impl(NULL, false /* no flattening */);)
  38   Arena* arena = CURRENT_ENV->arena();
  39   _declared_nonstatic_fields = (fields != NULL) ? fields : new (arena) GrowableArray<ciField*>(arena, 0, 0, 0);
  40   return result;
  41 }
  42 
  43 // Offset of the first field in the value type
  44 int ciValueKlass::first_field_offset() const {
  45   GUARDED_VM_ENTRY(return to_ValueKlass()->first_field_offset();)
  46 }
  47 
  48 // Returns the index of the field with the given offset. If the field at 'offset'
  49 // belongs to a flattened value type field, return the index of the field
  50 // in the flattened value type.
  51 int ciValueKlass::field_index_by_offset(int offset) {
  52   assert(contains_field_offset(offset), "invalid field offset");
  53   int best_offset = 0;
  54   int best_index = -1;
  55   // Search the field with the given offset
  56   for (int i = 0; i < nof_declared_nonstatic_fields(); ++i) {
  57     int field_offset = _declared_nonstatic_fields->at(i)->offset();
  58     if (field_offset == offset) {
  59       // Exact match
  60       return i;
  61     } else if (field_offset < offset && field_offset > best_offset) {
  62       // No exact match. Save the index of the field with the closest offset that
  63       // is smaller than the given field offset. This index corresponds to the
  64       // flattened value type field that holds the field we are looking for.
  65       best_offset = field_offset;
  66       best_index = i;
  67     }
  68   }
  69   assert(best_index >= 0, "field not found");
  70   assert(best_offset == offset || _declared_nonstatic_fields->at(best_index)->type()->is_valuetype(), "offset should match for non-VTs");
  71   return best_index;
  72 }
  73 
  74 // Are arrays containing this value type flattened?
  75 bool ciValueKlass::flatten_array() const {
  76   GUARDED_VM_ENTRY(return to_ValueKlass()->flatten_array();)
  77 }
  78 
  79 // Can this value type be returned as multiple values?
  80 bool ciValueKlass::can_be_returned_as_fields() const {
  81   GUARDED_VM_ENTRY(return to_ValueKlass()->can_be_returned_as_fields();)
  82 }
  83 
  84 // Can this value type be scalarized?
  85 bool ciValueKlass::is_scalarizable() const {
  86   GUARDED_VM_ENTRY(return to_ValueKlass()->is_scalarizable();)
  87 }
  88 
  89 // When passing a value type's fields as arguments, count the number
  90 // of argument slots that are needed
  91 int ciValueKlass::value_arg_slots() {
  92   int slots = 0;
  93   for (int j = 0; j < nof_nonstatic_fields(); j++) {
  94     ciField* field = nonstatic_field_at(j);
  95     slots += type2size[field->type()->basic_type()];
  96   }
  97   return slots;
  98 }
  99 
 100 // Offset of the default oop in the mirror
 101 int ciValueKlass::default_value_offset() const {
 102   GUARDED_VM_ENTRY(return to_ValueKlass()->default_value_offset();)
 103 }
 104 
 105 ciInstance* ciValueKlass::default_value_instance() const {
 106   GUARDED_VM_ENTRY(
 107     oop default_value = to_ValueKlass()->default_value();
 108     return CURRENT_ENV->get_instance(default_value);
 109   )
 110 }
 111 
 112 ciInstance* ciValueKlass::inline_mirror_instance() const {
 113   GUARDED_VM_ENTRY(
 114     oop value_mirror = to_ValueKlass()->value_mirror();
 115     return CURRENT_ENV->get_instance(value_mirror);
 116   )
 117 }
 118 
 119 ciInstance* ciValueKlass::indirect_mirror_instance() const {
 120   GUARDED_VM_ENTRY(
 121     oop mirror = to_ValueKlass()->indirect_mirror();
 122     return CURRENT_ENV->get_instance(mirror);
 123   )
 124 }
 125 
 126 bool ciValueKlass::contains_oops() const {
 127   GUARDED_VM_ENTRY(return get_ValueKlass()->contains_oops();)
 128 }
 129 
 130 Array<SigEntry>* ciValueKlass::extended_sig() const {
 131   GUARDED_VM_ENTRY(return get_ValueKlass()->extended_sig();)
 132 }
 133 
 134 address ciValueKlass::pack_handler() const {
 135   GUARDED_VM_ENTRY(return get_ValueKlass()->pack_handler();)
 136 }
 137 
 138 address ciValueKlass::unpack_handler() const {
 139   GUARDED_VM_ENTRY(return get_ValueKlass()->unpack_handler();)
 140 }
 141 
 142 ValueKlass* ciValueKlass::get_ValueKlass() const {
 143   GUARDED_VM_ENTRY(return to_ValueKlass();)
 144 }