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/ciUtilities.inline.hpp"
  28 #include "ci/ciValueKlass.hpp"
  29 #include "oops/valueKlass.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 ValueKlass::cast(get_Klass())->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 ValueKlass::cast(get_Klass())->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 ValueKlass::cast(get_Klass())->can_be_returned_as_fields();)
  82 }
  83 
  84 // Can this value type be returned as multiple values?
  85 bool ciValueKlass::is_bufferable() const {
  86   GUARDED_VM_ENTRY(return ValueKlass::cast(get_Klass())->is_bufferable();)
  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 = nof_nonstatic_fields();
  93   for (int j = 0; j < nof_nonstatic_fields(); j++) {
  94     ciField* f = nonstatic_field_at(j);
  95     BasicType bt = f->type()->basic_type();
  96     if (bt == T_LONG || bt == T_DOUBLE) {
  97       slots++;
  98     }
  99   }
 100   return slots;
 101 }
 102 
 103 // Offset of the default oop in the mirror
 104 int ciValueKlass::default_value_offset() const {
 105   GUARDED_VM_ENTRY(return ValueKlass::cast(get_Klass())->default_value_offset();)
 106 }
 107 
 108 ciInstance* ciValueKlass::default_value_instance() const {
 109   GUARDED_VM_ENTRY(
 110     oop default_value = ValueKlass::cast(get_Klass())->default_value();
 111     return CURRENT_ENV->get_instance(default_value);
 112   )
 113 }
 114 
 115 bool ciValueKlass::contains_oops() const {
 116   GUARDED_VM_ENTRY(return ValueKlass::cast(get_Klass())->contains_oops();)
 117 }