1 /*
   2  * Copyright (c) 2016, 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/ciInstanceKlass.hpp"
  27 #include "ci/ciValueArrayKlass.hpp"
  28 #include "ci/ciValueKlass.hpp"
  29 #include "ci/ciSymbol.hpp"
  30 #include "ci/ciUtilities.hpp"
  31 #include "ci/ciUtilities.inline.hpp"
  32 #include "oops/valueArrayKlass.hpp"
  33 
  34 // ciValueArrayKlass
  35 //
  36 // This class represents a Klass* in the HotSpot virtual machine
  37 // whose Klass part is a ValueArrayKlass.
  38 
  39 // ------------------------------------------------------------------
  40 // ciValueArrayKlass::ciValueArrayKlass
  41 //
  42 // Constructor for loaded value array klasses.
  43 ciValueArrayKlass::ciValueArrayKlass(Klass* h_k) : ciArrayKlass(h_k) {
  44   assert(get_Klass()->is_valueArray_klass(), "wrong type");
  45   ValueKlass* element_Klass = get_ValueArrayKlass()->element_klass();
  46   _base_element_klass = CURRENT_ENV->get_klass(element_Klass);
  47   assert(_base_element_klass->is_valuetype(), "bad base klass");
  48   if (dimension() == 1) {
  49     _element_klass = _base_element_klass;
  50   } else {
  51     _element_klass = NULL;
  52   }
  53   if (!ciObjectFactory::is_initialized()) {
  54     assert(_element_klass->is_java_lang_Object(), "only arrays of object are shared");
  55   }
  56 }
  57 
  58 ciValueArrayKlass::ciValueArrayKlass(ciSymbol* array_name,
  59                                      ciValueKlass* base_element_klass,
  60                                      int dimension)
  61   : ciArrayKlass(array_name, dimension, T_VALUETYPE) {
  62   _base_element_klass = base_element_klass;
  63   _element_klass = base_element_klass;
  64 }
  65 
  66 // ------------------------------------------------------------------
  67 // ciValueArrayKlass::element_klass
  68 //
  69 // What is the one-level element type of this array?
  70 ciKlass* ciValueArrayKlass::element_klass() {
  71   if (_element_klass == NULL) {
  72     assert(dimension() > 1, "_element_klass should not be NULL");
  73     assert(is_loaded(), "valueArrayKlass must be loaded");
  74     // Produce the element klass.
  75     VM_ENTRY_MARK;
  76     Klass* element_Klass = get_ValueArrayKlass()->element_klass();
  77     _element_klass = CURRENT_THREAD_ENV->get_klass(element_Klass);
  78   }
  79   return _element_klass;
  80 }
  81 
  82 // ------------------------------------------------------------------
  83 // ciValueArrayKlass::construct_array_name
  84 //
  85 // Build an array name from an element name and a dimension.
  86 ciSymbol* ciValueArrayKlass::construct_array_name(ciSymbol* element_name,
  87                                                   int dimension) {
  88   EXCEPTION_CONTEXT;
  89   int element_len = element_name->utf8_length();
  90 
  91   Symbol* base_name_sym = element_name->get_symbol();
  92   char* name;
  93 
  94   if (base_name_sym->char_at(0) == '[' ||
  95       (base_name_sym->char_at(0) == 'L' &&  // watch package name 'Lxx'
  96        base_name_sym->char_at(element_len-1) == ';')) {
  97 
  98     int new_len = element_len + dimension + 1; // for the ['s and '\0'
  99     name = CURRENT_THREAD_ENV->name_buffer(new_len);
 100 
 101     int pos = 0;
 102     for ( ; pos < dimension; pos++) {
 103       name[pos] = '[';
 104     }
 105     strncpy(name+pos, (char*)element_name->base(), element_len);
 106     name[new_len-1] = '\0';
 107   } else {
 108     int new_len =   3                       // for L, ;, and '\0'
 109                   + dimension               // for ['s
 110                   + element_len;
 111 
 112     name = CURRENT_THREAD_ENV->name_buffer(new_len);
 113     int pos = 0;
 114     for ( ; pos < dimension; pos++) {
 115       name[pos] = '[';
 116     }
 117     name[pos++] = 'Q';
 118     strncpy(name+pos, (char*)element_name->base(), element_len);
 119     name[new_len-2] = ';';
 120     name[new_len-1] = '\0';
 121   }
 122   return ciSymbol::make(name);
 123 }
 124 
 125 // ------------------------------------------------------------------
 126 // ciValueArrayKlass::make_impl
 127 //
 128 // Implementation of make.
 129 ciValueArrayKlass* ciValueArrayKlass::make_impl(ciKlass* element_klass) {
 130   assert(ValueArrayFlatten, "should only be used for flattened value type arrays");
 131   assert(element_klass->is_valuetype(), "element type must be value type");
 132   assert(element_klass->is_loaded(), "unloaded Q klasses are represented by ciInstanceKlass");
 133   {
 134     EXCEPTION_CONTEXT;
 135     // The element klass is loaded
 136     Klass* array = element_klass->get_Klass()->array_klass(ArrayStorageProperties::flattened_and_null_free, 1, THREAD);
 137     assert(ValueArrayKlass::cast(array)->storage_properties().is_flattened(), "should be flattened");
 138     assert(ValueArrayKlass::cast(array)->storage_properties().is_null_free(), "should be null free");
 139     if (HAS_PENDING_EXCEPTION) {
 140       CLEAR_PENDING_EXCEPTION;
 141       CURRENT_THREAD_ENV->record_out_of_memory_failure();
 142       // TODO handle this
 143       guarantee(false, "out of memory");
 144       return NULL;
 145     }
 146     return CURRENT_THREAD_ENV->get_value_array_klass(array);
 147   }
 148 }
 149 
 150 // ------------------------------------------------------------------
 151 // ciValueArrayKlass::make
 152 //
 153 // Make an array klass corresponding to the specified primitive type.
 154 ciValueArrayKlass* ciValueArrayKlass::make(ciKlass* element_klass) {
 155   GUARDED_VM_ENTRY(return make_impl(element_klass);)
 156 }
 157 
 158 ciKlass* ciValueArrayKlass::exact_klass() {
 159   assert(element_klass()->is_loaded() && element_klass()->as_value_klass()->exact_klass() != NULL, "must have exact klass");
 160   return this;
 161 }