1 /*
   2  * Copyright (c) 2015, 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 #ifndef SHARE_CLASSFILE_JAVACLASSES_INLINE_HPP
  26 #define SHARE_CLASSFILE_JAVACLASSES_INLINE_HPP
  27 
  28 #include "classfile/javaClasses.hpp"
  29 #include "oops/access.inline.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "oops/oopsHierarchy.hpp"
  32 
  33 void java_lang_String::set_coder(oop string, jbyte coder) {
  34   assert(initialized && (coder_offset > 0), "Must be initialized");
  35   string->byte_field_put(coder_offset, coder);
  36 }
  37 
  38 void java_lang_String::set_value_raw(oop string, typeArrayOop buffer) {
  39   assert(initialized, "Must be initialized");
  40   string->obj_field_put_raw(value_offset, buffer);
  41 }
  42 
  43 void java_lang_String::set_value(oop string, typeArrayOop buffer) {
  44   assert(initialized && (value_offset > 0), "Must be initialized");
  45   string->obj_field_put(value_offset, (oop)buffer);
  46 }
  47 
  48 bool java_lang_String::hash_is_set(oop java_string) {
  49   assert(initialized && (hash_offset > 0) && (hashIsZero_offset > 0), "Must be initialized");
  50   return java_string->int_field(hash_offset) != 0 || java_string->bool_field(hashIsZero_offset) != 0;
  51 }
  52 
  53 // Accessors
  54 bool java_lang_String::value_equals(typeArrayOop str_value1, typeArrayOop str_value2) {
  55   return ((str_value1 == str_value2) ||
  56           (str_value1->length() == str_value2->length() &&
  57            (!memcmp(str_value1->base(T_BYTE),
  58                     str_value2->base(T_BYTE),
  59                     str_value2->length() * sizeof(jbyte)))));
  60 }
  61 
  62 typeArrayOop java_lang_String::value(oop java_string) {
  63   assert(initialized && (value_offset > 0), "Must be initialized");
  64   assert(is_instance(java_string), "must be java_string");
  65   return (typeArrayOop) java_string->obj_field(value_offset);
  66 }
  67 
  68 typeArrayOop java_lang_String::value_no_keepalive(oop java_string) {
  69   assert(initialized && (value_offset > 0), "Must be initialized");
  70   assert(is_instance(java_string), "must be java_string");
  71   return (typeArrayOop) java_string->obj_field_access<AS_NO_KEEPALIVE>(value_offset);
  72 }
  73 
  74 bool java_lang_String::is_latin1(oop java_string) {
  75   assert(initialized && (coder_offset > 0), "Must be initialized");
  76   assert(is_instance(java_string), "must be java_string");
  77   jbyte coder = java_string->byte_field(coder_offset);
  78   assert(CompactStrings || coder == CODER_UTF16, "Must be UTF16 without CompactStrings");
  79   return coder == CODER_LATIN1;
  80 }
  81 
  82 int java_lang_String::length(oop java_string, typeArrayOop value) {
  83   assert(initialized, "Must be initialized");
  84   assert(is_instance(java_string), "must be java_string");
  85   assert(value_equals(value, java_lang_String::value(java_string)),
  86          "value must be equal to java_lang_String::value(java_string)");
  87   if (value == NULL) {
  88     return 0;
  89   }
  90   int arr_length = value->length();
  91   if (!is_latin1(java_string)) {
  92     assert((arr_length & 1) == 0, "should be even for UTF16 string");
  93     arr_length >>= 1; // convert number of bytes to number of elements
  94   }
  95   return arr_length;
  96 }
  97 
  98 int java_lang_String::length(oop java_string) {
  99   assert(initialized, "Must be initialized");
 100   assert(is_instance(java_string), "must be java_string");
 101   typeArrayOop value = java_lang_String::value_no_keepalive(java_string);
 102   return length(java_string, value);
 103 }
 104 
 105 bool java_lang_String::is_instance_inlined(oop obj) {
 106   return obj != NULL && obj->klass() == SystemDictionary::String_klass();
 107 }
 108 
 109 // Accessors
 110 oop java_lang_ref_Reference::referent(oop ref) {
 111   return ref->obj_field(referent_offset);
 112 }
 113 
 114 void java_lang_ref_Reference::set_referent(oop ref, oop value) {
 115   ref->obj_field_put(referent_offset, value);
 116 }
 117 
 118 void java_lang_ref_Reference::set_referent_raw(oop ref, oop value) {
 119   ref->obj_field_put_raw(referent_offset, value);
 120 }
 121 
 122 HeapWord* java_lang_ref_Reference::referent_addr_raw(oop ref) {
 123   return ref->obj_field_addr_raw<HeapWord>(referent_offset);
 124 }
 125 
 126 oop java_lang_ref_Reference::next(oop ref) {
 127   return ref->obj_field(next_offset);
 128 }
 129 
 130 void java_lang_ref_Reference::set_next(oop ref, oop value) {
 131   ref->obj_field_put(next_offset, value);
 132 }
 133 
 134 void java_lang_ref_Reference::set_next_raw(oop ref, oop value) {
 135   ref->obj_field_put_raw(next_offset, value);
 136 }
 137 
 138 HeapWord* java_lang_ref_Reference::next_addr_raw(oop ref) {
 139   return ref->obj_field_addr_raw<HeapWord>(next_offset);
 140 }
 141 
 142 oop java_lang_ref_Reference::discovered(oop ref) {
 143   return ref->obj_field(discovered_offset);
 144 }
 145 
 146 void java_lang_ref_Reference::set_discovered(oop ref, oop value) {
 147   ref->obj_field_put(discovered_offset, value);
 148 }
 149 
 150 void java_lang_ref_Reference::set_discovered_raw(oop ref, oop value) {
 151   ref->obj_field_put_raw(discovered_offset, value);
 152 }
 153 
 154 HeapWord* java_lang_ref_Reference::discovered_addr_raw(oop ref) {
 155   return ref->obj_field_addr_raw<HeapWord>(discovered_offset);
 156 }
 157 
 158 bool java_lang_ref_Reference::is_final(oop ref) {
 159   return InstanceKlass::cast(ref->klass())->reference_type() == REF_FINAL;
 160 }
 161 
 162 bool java_lang_ref_Reference::is_phantom(oop ref) {
 163   return InstanceKlass::cast(ref->klass())->reference_type() == REF_PHANTOM;
 164 }
 165 
 166 inline void java_lang_invoke_CallSite::set_target_volatile(oop site, oop target) {
 167   site->obj_field_put_volatile(_target_offset, target);
 168 }
 169 
 170 inline oop  java_lang_invoke_CallSite::target(oop site) {
 171   return site->obj_field(_target_offset);
 172 }
 173 
 174 inline void java_lang_invoke_CallSite::set_target(oop site, oop target) {
 175   site->obj_field_put(_target_offset, target);
 176 }
 177 
 178 inline bool java_lang_invoke_CallSite::is_instance(oop obj) {
 179   return obj != NULL && is_subclass(obj->klass());
 180 }
 181 
 182 inline bool java_lang_invoke_MethodHandleNatives_CallSiteContext::is_instance(oop obj) {
 183   return obj != NULL && is_subclass(obj->klass());
 184 }
 185 
 186 inline bool java_lang_invoke_MemberName::is_instance(oop obj) {
 187   return obj != NULL && obj->klass() == SystemDictionary::MemberName_klass();
 188 }
 189 
 190 inline bool java_lang_invoke_ResolvedMethodName::is_instance(oop obj) {
 191   return obj != NULL && obj->klass() == SystemDictionary::ResolvedMethodName_klass();
 192 }
 193 
 194 inline bool java_lang_invoke_MethodType::is_instance(oop obj) {
 195   return obj != NULL && obj->klass() == SystemDictionary::MethodType_klass();
 196 }
 197 
 198 inline bool java_lang_invoke_MethodHandle::is_instance(oop obj) {
 199   return obj != NULL && is_subclass(obj->klass());
 200 }
 201 
 202 inline bool java_lang_Class::is_instance(oop obj) {
 203   return obj != NULL && obj->klass() == SystemDictionary::Class_klass();
 204 }
 205 
 206 inline bool java_lang_Class::is_primitive(oop java_class) {
 207   // should assert:
 208   //assert(java_lang_Class::is_instance(java_class), "must be a Class object");
 209   bool is_primitive = (java_class->metadata_field(_klass_offset) == NULL);
 210 
 211 #ifdef ASSERT
 212   if (is_primitive) {
 213     Klass* k = ((Klass*)java_class->metadata_field(_array_klass_offset));
 214     assert(k == NULL || is_java_primitive(ArrayKlass::cast(k)->element_type()),
 215         "Should be either the T_VOID primitive or a java primitive");
 216   }
 217 #endif
 218 
 219   return is_primitive;
 220 }
 221 
 222 inline int java_lang_Class::oop_size_raw(oop java_class) {
 223   assert(_oop_size_offset != 0, "must be set");
 224   int size = java_class->int_field_raw(_oop_size_offset);
 225   assert(size > 0, "Oop size must be greater than zero, not %d", size);
 226   return size;
 227 }
 228 
 229 inline bool java_lang_invoke_DirectMethodHandle::is_instance(oop obj) {
 230   return obj != NULL && is_subclass(obj->klass());
 231 }
 232 
 233 inline bool java_lang_Module::is_instance(oop obj) {
 234   return obj != NULL && obj->klass() == SystemDictionary::Module_klass();
 235 }
 236 
 237 inline int Backtrace::merge_bci_and_version(int bci, int version) {
 238   // only store u2 for version, checking for overflow.
 239   if (version > USHRT_MAX || version < 0) version = USHRT_MAX;
 240   assert((jushort)bci == bci, "bci should be short");
 241   return build_int_from_shorts(version, bci);
 242 }
 243 
 244 inline int Backtrace::merge_mid_and_cpref(int mid, int cpref) {
 245   // only store u2 for mid and cpref, checking for overflow.
 246   assert((jushort)mid == mid, "mid should be short");
 247   assert((jushort)cpref == cpref, "cpref should be short");
 248   return build_int_from_shorts(cpref, mid);
 249 }
 250 
 251 inline int Backtrace::bci_at(unsigned int merged) {
 252   return extract_high_short_from_int(merged);
 253 }
 254 
 255 inline int Backtrace::version_at(unsigned int merged) {
 256   return extract_low_short_from_int(merged);
 257 }
 258 
 259 inline int Backtrace::mid_at(unsigned int merged) {
 260   return extract_high_short_from_int(merged);
 261 }
 262 
 263 inline int Backtrace::cpref_at(unsigned int merged) {
 264   return extract_low_short_from_int(merged);
 265 }
 266 
 267 inline int Backtrace::get_line_number(const methodHandle& method, int bci) {
 268   int line_number = 0;
 269   if (method->is_native()) {
 270     // Negative value different from -1 below, enabling Java code in
 271     // class java.lang.StackTraceElement to distinguish "native" from
 272     // "no LineNumberTable".  JDK tests for -2.
 273     line_number = -2;
 274   } else {
 275     // Returns -1 if no LineNumberTable, and otherwise actual line number
 276     line_number = method->line_number_from_bci(bci);
 277   }
 278   return line_number;
 279 }
 280 
 281 inline Symbol* Backtrace::get_source_file_name(InstanceKlass* holder, int version) {
 282   // RedefineClasses() currently permits redefine operations to
 283   // happen in parallel using a "last one wins" philosophy. That
 284   // spec laxness allows the constant pool entry associated with
 285   // the source_file_name_index for any older constant pool version
 286   // to be unstable so we shouldn't try to use it.
 287   if (holder->constants()->version() != version) {
 288     return NULL;
 289   } else {
 290     return holder->source_file_name();
 291   }
 292 }
 293 
 294 #endif // SHARE_CLASSFILE_JAVACLASSES_INLINE_HPP