1 /*
   2  * Copyright (c) 2017, 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/valueKlass.hpp"
  29 
  30 int ciValueKlass::compute_nonstatic_fields() {
  31   int result = ciInstanceKlass::compute_nonstatic_fields();
  32   assert(super() == NULL || !super()->has_nonstatic_fields(), "a value type must not inherit fields from its superclass");
  33 
  34   // Compute declared non-static fields (without flattening of value type fields)
  35   GrowableArray<ciField*>* fields = NULL;
  36   GUARDED_VM_ENTRY(fields = compute_nonstatic_fields_impl(NULL, false /* no flattening */);)
  37   Arena* arena = CURRENT_ENV->arena();
  38   _declared_nonstatic_fields = (fields != NULL) ? fields : new (arena) GrowableArray<ciField*>(arena, 0, 0, 0);
  39   return result;
  40 }
  41 
  42 // Offset of the first field in the value type
  43 int ciValueKlass::first_field_offset() const {
  44   GUARDED_VM_ENTRY(return ValueKlass::cast(get_Klass())->first_field_offset();)
  45 }
  46 
  47 // Returns the index of the field with the given offset. If the field at 'offset'
  48 // belongs to a flattened value type field, return the index of the field
  49 // in the flattened value type.
  50 int ciValueKlass::field_index_by_offset(int offset) {
  51   assert(contains_field_offset(offset), "invalid field offset");
  52   int best_offset = 0;
  53   int best_index = -1;
  54   // Search the field with the given offset
  55   for (int i = 0; i < nof_declared_nonstatic_fields(); ++i) {
  56     int field_offset = _declared_nonstatic_fields->at(i)->offset();
  57     if (field_offset == offset) {
  58       // Exact match
  59       return i;
  60     } else if (field_offset < offset && field_offset > best_offset) {
  61       // No exact match. Save the index of the field with the closest offset that
  62       // is smaller than the given field offset. This index corresponds to the
  63       // flattened value type field that holds the field we are looking for.
  64       best_offset = field_offset;
  65       best_index = i;
  66     }
  67   }
  68   assert(best_index >= 0, "field not found");
  69   assert(best_offset == offset || _declared_nonstatic_fields->at(best_index)->type()->is_valuetype(), "offset should match for non-VTs");
  70   return best_index;
  71 }
  72 
  73 // Are arrays containing this value type flattened?
  74 bool ciValueKlass::flatten_array() const {
  75   GUARDED_VM_ENTRY(return ValueKlass::cast(get_Klass())->flatten_array();)
  76 }
  77 
  78 // Can this value type be returned as multiple values?
  79 bool ciValueKlass::can_be_returned_as_fields() const {
  80   GUARDED_VM_ENTRY(return !is__Value() && ValueKlass::cast(get_Klass())->return_regs() != NULL;)
  81 }
  82 
  83 // When passing a value type's fields as arguments, count the number
  84 // of argument slots that are needed
  85 int ciValueKlass::value_arg_slots() {
  86   int slots = nof_nonstatic_fields();
  87   for (int j = 0; j < nof_nonstatic_fields(); j++) {
  88     ciField* f = nonstatic_field_at(j);
  89     BasicType bt = f->type()->basic_type();
  90     if (bt == T_LONG || bt == T_DOUBLE) {
  91       slots++;
  92     }
  93   }
  94   return slots;
  95 }