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 "ci/ciField.hpp"
 26 #include "ci/ciInlineKlass.hpp"
 27 #include "ci/ciUtilities.inline.hpp"
 28 #include "oops/inlineKlass.inline.hpp"
 29 
 30 int ciInlineKlass::compute_nonstatic_fields() {
 31   int result = ciInstanceKlass::compute_nonstatic_fields();
 32 
 33   // Abstract value classes can also have declared fields.
 34   ciInstanceKlass* super_klass = super();
 35   GrowableArray<ciField*>* super_klass_fields = nullptr;
 36   if (super_klass != nullptr && super_klass->has_nonstatic_fields()) {
 37     int super_flen = super_klass->nof_nonstatic_fields();
 38     super_klass_fields = super_klass->_nonstatic_fields;
 39     assert(super_flen == 0 || super_klass_fields != nullptr, "first get nof_fields");
 40   }
 41 
 42   // Compute declared non-static fields (without flattening of inline type fields)
 43   GrowableArray<ciField*>* fields = nullptr;
 44   GUARDED_VM_ENTRY(fields = compute_nonstatic_fields_impl(super_klass_fields, false /* no flattening */);)
 45   Arena* arena = CURRENT_ENV->arena();
 46   _declared_nonstatic_fields = (fields != nullptr) ? fields : new (arena) GrowableArray<ciField*>(arena, 0, 0, 0);
 47   return result;
 48 }
 49 
 50 // Offset of the first field in the inline type
 51 int ciInlineKlass::payload_offset() const {
 52   GUARDED_VM_ENTRY(return to_InlineKlass()->payload_offset();)
 53 }
 54 
 55 // Returns the index of the field with the given offset. If the field at 'offset'
 56 // belongs to a flat field, return the index of the field in the inline type of the flat field.
 57 int ciInlineKlass::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 = _declared_nonstatic_fields->at(i)->offset_in_bytes();
 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       // flat 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 || _declared_nonstatic_fields->at(best_index)->type()->is_inlinetype(), "offset should match for non-inline types");
 77   return best_index;
 78 }
 79 
 80 // Are arrays containing this inline type flat arrays?
 81 bool ciInlineKlass::flat_in_array() const {
 82   GUARDED_VM_ENTRY(return to_InlineKlass()->flat_array();)
 83 }
 84 
 85 // Can this inline type be passed as multiple values?
 86 bool ciInlineKlass::can_be_passed_as_fields() const {
 87   GUARDED_VM_ENTRY(return to_InlineKlass()->can_be_passed_as_fields();)
 88 }
 89 
 90 // Can this inline type be returned as multiple values?
 91 bool ciInlineKlass::can_be_returned_as_fields() const {
 92   GUARDED_VM_ENTRY(return to_InlineKlass()->can_be_returned_as_fields();)
 93 }
 94 
 95 bool ciInlineKlass::is_empty() {
 96   // Do not use InlineKlass::is_empty_inline_type here because it does
 97   // consider the container empty even if fields of empty inline types
 98   // are not flat
 99   return nof_declared_nonstatic_fields() == 0;
100 }
101 
102 // When passing an inline type's fields as arguments, count the number
103 // of argument slots that are needed
104 int ciInlineKlass::inline_arg_slots() {
105   VM_ENTRY_MARK;
106   const Array<SigEntry>* sig_vk = get_InlineKlass()->extended_sig();
107   int slots = 0;
108   for (int i = 0; i < sig_vk->length(); i++) {
109     BasicType bt = sig_vk->at(i)._bt;
110     if (bt == T_METADATA || bt == T_VOID) {
111       continue;
112     }
113     slots += type2size[bt];
114   }
115   return slots;
116 }
117 
118 bool ciInlineKlass::contains_oops() const {
119   GUARDED_VM_ENTRY(return get_InlineKlass()->contains_oops();)
120 }
121 
122 int ciInlineKlass::oop_count() const {
123   GUARDED_VM_ENTRY(return get_InlineKlass()->nonstatic_oop_count();)
124 }
125 
126 address ciInlineKlass::pack_handler() const {
127   GUARDED_VM_ENTRY(return get_InlineKlass()->pack_handler();)
128 }
129 
130 address ciInlineKlass::unpack_handler() const {
131   GUARDED_VM_ENTRY(return get_InlineKlass()->unpack_handler();)
132 }
133 
134 InlineKlass* ciInlineKlass::get_InlineKlass() const {
135   GUARDED_VM_ENTRY(return to_InlineKlass();)
136 }
137 
138 bool ciInlineKlass::has_non_atomic_layout() const {
139   GUARDED_VM_ENTRY(return get_InlineKlass()->has_non_atomic_layout();)
140 }
141 
142 bool ciInlineKlass::has_atomic_layout() const {
143   GUARDED_VM_ENTRY(return get_InlineKlass()->has_atomic_layout();)
144 }
145 
146 bool ciInlineKlass::has_nullable_atomic_layout() const {
147   GUARDED_VM_ENTRY(return get_InlineKlass()->has_nullable_atomic_layout();)
148 }
149 
150 int ciInlineKlass::null_marker_offset_in_payload() const {
151   GUARDED_VM_ENTRY(return get_InlineKlass()->null_marker_offset_in_payload();)
152 }
153 
154 // Convert size of atomic layout in bytes to corresponding BasicType
155 BasicType ciInlineKlass::atomic_size_to_basic_type(bool null_free) const {
156   VM_ENTRY_MARK
157   InlineKlass* vk = get_InlineKlass();
158   assert(!null_free || vk->has_atomic_layout(), "No null-free atomic layout available");
159   assert( null_free || vk->has_nullable_atomic_layout(), "No nullable atomic layout available");
160   int size = null_free ? vk->atomic_size_in_bytes() : vk->nullable_atomic_size_in_bytes();
161   BasicType bt;
162   if (size == sizeof(jlong)) {
163     bt = T_LONG;
164   } else if (size == sizeof(jint)) {
165     bt = T_INT;
166   } else if (size == sizeof(jshort)) {
167     bt = T_SHORT;
168   } else if (size == sizeof(jbyte)) {
169     bt = T_BYTE;
170   } else {
171     assert(false, "Unsupported size: %d", size);
172   }
173   return bt;
174 }