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/ciInlineKlass.hpp"
 28 #include "ci/ciUtilities.inline.hpp"
 29 #include "oops/inlineKlass.inline.hpp"
 30 
 31 int ciInlineKlass::compute_nonstatic_fields() {
 32   int result = ciInstanceKlass::compute_nonstatic_fields();
 33   assert(super() == NULL || !super()->has_nonstatic_fields(), "an inline type must not inherit fields from its superclass");
 34 
 35   // Compute declared non-static fields (without flattening of inline 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 inline type
 44 int ciInlineKlass::first_field_offset() const {
 45   GUARDED_VM_ENTRY(return to_InlineKlass()->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 inline type field, return the index of the field
 50 // in the flattened inline type.
 51 int ciInlineKlass::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 inline 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_inlinetype(), "offset should match for non-inline types");
 71   return best_index;
 72 }
 73 
 74 // Are arrays containing this inline type flattened?
 75 bool ciInlineKlass::flatten_array() const {
 76   GUARDED_VM_ENTRY(return to_InlineKlass()->flatten_array();)
 77 }
 78 
 79 // Can this inline type be passed as multiple values?
 80 bool ciInlineKlass::can_be_passed_as_fields() const {
 81   GUARDED_VM_ENTRY(return to_InlineKlass()->can_be_passed_as_fields();)
 82 }
 83 
 84 // Can this inline type be returned as multiple values?
 85 bool ciInlineKlass::can_be_returned_as_fields() const {
 86   GUARDED_VM_ENTRY(return to_InlineKlass()->can_be_returned_as_fields();)
 87 }
 88 
 89 bool ciInlineKlass::is_empty() {
 90   // Do not use InlineKlass::is_empty_inline_type here because it does
 91   // consider the container empty even if fields of empty inline types
 92   // are not flattened
 93   return nof_nonstatic_fields() == 0;
 94 }
 95 
 96 // When passing an inline type's fields as arguments, count the number
 97 // of argument slots that are needed
 98 int ciInlineKlass::inline_arg_slots() {
 99   int slots = 0;
100   for (int j = 0; j < nof_nonstatic_fields(); j++) {
101     ciField* field = nonstatic_field_at(j);
102     slots += type2size[field->type()->basic_type()];
103   }
104   return slots;
105 }
106 
107 // Offset of the default oop in the mirror
108 int ciInlineKlass::default_value_offset() const {
109   GUARDED_VM_ENTRY(return to_InlineKlass()->default_value_offset();)
110 }
111 
112 ciInstance* ciInlineKlass::default_instance() const {
113   GUARDED_VM_ENTRY(
114     oop default_value = to_InlineKlass()->default_value();
115     return CURRENT_ENV->get_instance(default_value);
116   )
117 }
118 
119 ciInstance* ciInlineKlass::ref_instance() const {
120   GUARDED_VM_ENTRY(
121     oop ref_mirror = to_InlineKlass()->ref_mirror();
122     return CURRENT_ENV->get_instance(ref_mirror);
123   )
124 }
125 
126 ciInstance* ciInlineKlass::val_instance() const {
127   GUARDED_VM_ENTRY(
128     oop val_mirror = to_InlineKlass()->val_mirror();
129     return CURRENT_ENV->get_instance(val_mirror);
130   )
131 }
132 
133 bool ciInlineKlass::contains_oops() const {
134   GUARDED_VM_ENTRY(return get_InlineKlass()->contains_oops();)
135 }
136 
137 int ciInlineKlass::oop_count() const {
138   GUARDED_VM_ENTRY(return get_InlineKlass()->nonstatic_oop_count();)
139 }
140 
141 address ciInlineKlass::pack_handler() const {
142   GUARDED_VM_ENTRY(return get_InlineKlass()->pack_handler();)
143 }
144 
145 address ciInlineKlass::unpack_handler() const {
146   GUARDED_VM_ENTRY(return get_InlineKlass()->unpack_handler();)
147 }
148 
149 InlineKlass* ciInlineKlass::get_InlineKlass() const {
150   GUARDED_VM_ENTRY(return to_InlineKlass();)
151 }
152 
153 ciInstance* ciInlineKlass::ref_mirror() {
154   GUARDED_VM_ENTRY(return CURRENT_ENV->get_instance(to_InlineKlass()->ref_mirror());)
155 }
156 
157 ciInstance* ciInlineKlass::val_mirror() {
158   GUARDED_VM_ENTRY(return CURRENT_ENV->get_instance(to_InlineKlass()->val_mirror());)
159 }