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 #include "oops/typeArrayOop.inline.hpp"
  33 
  34 void java_lang_String::set_coder(oop string, jbyte coder) {
  35   assert(initialized && (coder_offset > 0), "Must be initialized");
  36   string->byte_field_put(coder_offset, coder);
  37 }
  38 
  39 void java_lang_String::set_value_raw(oop string, typeArrayOop buffer) {
  40   assert(initialized, "Must be initialized");
  41   string->obj_field_put_raw(value_offset, buffer);
  42 }
  43 
  44 void java_lang_String::set_value(oop string, typeArrayOop buffer) {
  45   assert(initialized && (value_offset > 0), "Must be initialized");
  46   string->obj_field_put(value_offset, (oop)buffer);
  47 }
  48 
  49 bool java_lang_String::hash_is_set(oop java_string) {
  50   assert(initialized && (hash_offset > 0) && (hashIsZero_offset > 0), "Must be initialized");
  51   return java_string->int_field(hash_offset) != 0 || java_string->bool_field(hashIsZero_offset) != 0;
  52 }
  53 
  54 // Accessors
  55 bool java_lang_String::value_equals(typeArrayOop str_value1, typeArrayOop str_value2) {
  56   return (oopDesc::equals(str_value1, str_value2) ||
  57           (str_value1->length() == str_value2->length() &&
  58            (!memcmp(str_value1->base(T_BYTE),
  59                     str_value2->base(T_BYTE),
  60                     str_value2->length() * sizeof(jbyte)))));
  61 }
  62 
  63 typeArrayOop java_lang_String::value(oop java_string) {
  64   assert(initialized && (value_offset > 0), "Must be initialized");
  65   assert(is_instance(java_string), "must be java_string");
  66   return (typeArrayOop) java_string->obj_field(value_offset);
  67 }
  68 
  69 typeArrayOop java_lang_String::value_no_keepalive(oop java_string) {
  70   assert(initialized && (value_offset > 0), "Must be initialized");
  71   assert(is_instance(java_string), "must be java_string");
  72   return (typeArrayOop) java_string->obj_field_access<AS_NO_KEEPALIVE>(value_offset);
  73 }
  74 
  75 bool java_lang_String::is_latin1(oop java_string) {
  76   assert(initialized && (coder_offset > 0), "Must be initialized");
  77   assert(is_instance(java_string), "must be java_string");
  78   jbyte coder = java_string->byte_field(coder_offset);
  79   assert(CompactStrings || coder == CODER_UTF16, "Must be UTF16 without CompactStrings");
  80   return coder == CODER_LATIN1;
  81 }
  82 
  83 int java_lang_String::length(oop java_string, typeArrayOop value) {
  84   assert(initialized, "Must be initialized");
  85   assert(is_instance(java_string), "must be java_string");
  86   assert(value_equals(value, java_lang_String::value(java_string)),
  87          "value must be equal to java_lang_String::value(java_string)");
  88   if (value == NULL) {
  89     return 0;
  90   }
  91   int arr_length = value->length();
  92   if (!is_latin1(java_string)) {
  93     assert((arr_length & 1) == 0, "should be even for UTF16 string");
  94     arr_length >>= 1; // convert number of bytes to number of elements
  95   }
  96   return arr_length;
  97 }
  98 
  99 int java_lang_String::length(oop java_string) {
 100   assert(initialized, "Must be initialized");
 101   assert(is_instance(java_string), "must be java_string");
 102   typeArrayOop value = java_lang_String::value_no_keepalive(java_string);
 103   return length(java_string, value);
 104 }
 105 
 106 bool java_lang_String::is_instance_inlined(oop obj) {
 107   return obj != NULL && obj->klass() == SystemDictionary::String_klass();
 108 }
 109 
 110 // Accessors
 111 oop java_lang_ref_Reference::referent(oop ref) {
 112   return ref->obj_field(referent_offset);
 113 }
 114 
 115 void java_lang_ref_Reference::set_referent(oop ref, oop value) {
 116   ref->obj_field_put(referent_offset, value);
 117 }
 118 
 119 void java_lang_ref_Reference::set_referent_raw(oop ref, oop value) {
 120   ref->obj_field_put_raw(referent_offset, value);
 121 }
 122 
 123 HeapWord* java_lang_ref_Reference::referent_addr_raw(oop ref) {
 124   return ref->obj_field_addr_raw<HeapWord>(referent_offset);
 125 }
 126 
 127 oop java_lang_ref_Reference::next(oop ref) {
 128   return ref->obj_field(next_offset);
 129 }
 130 
 131 void java_lang_ref_Reference::set_next(oop ref, oop value) {
 132   ref->obj_field_put(next_offset, value);
 133 }
 134 
 135 void java_lang_ref_Reference::set_next_raw(oop ref, oop value) {
 136   ref->obj_field_put_raw(next_offset, value);
 137 }
 138 
 139 HeapWord* java_lang_ref_Reference::next_addr_raw(oop ref) {
 140   return ref->obj_field_addr_raw<HeapWord>(next_offset);
 141 }
 142 
 143 oop java_lang_ref_Reference::discovered(oop ref) {
 144   return ref->obj_field(discovered_offset);
 145 }
 146 
 147 void java_lang_ref_Reference::set_discovered(oop ref, oop value) {
 148   ref->obj_field_put(discovered_offset, value);
 149 }
 150 
 151 void java_lang_ref_Reference::set_discovered_raw(oop ref, oop value) {
 152   ref->obj_field_put_raw(discovered_offset, value);
 153 }
 154 
 155 HeapWord* java_lang_ref_Reference::discovered_addr_raw(oop ref) {
 156   return ref->obj_field_addr_raw<HeapWord>(discovered_offset);
 157 }
 158 
 159 bool java_lang_ref_Reference::is_final(oop ref) {
 160   return InstanceKlass::cast(ref->klass())->reference_type() == REF_FINAL;
 161 }
 162 
 163 bool java_lang_ref_Reference::is_phantom(oop ref) {
 164   return InstanceKlass::cast(ref->klass())->reference_type() == REF_PHANTOM;
 165 }
 166 
 167 inline oop java_lang_ContinuationScope::name(oop ref) {
 168   return ref->obj_field(_name_offset);
 169 }
 170 
 171 inline oop java_lang_Continuation::scope(oop ref) {
 172   return ref->obj_field(_scope_offset);
 173 }
 174 inline oop java_lang_Continuation::target(oop ref) {
 175   return ref->obj_field(_target_offset);
 176 }
 177 inline oop java_lang_Continuation::parent(oop ref) {
 178   return ref->obj_field(_parent_offset);
 179 }
 180 inline oop java_lang_Continuation::yieldInfo(oop ref) {
 181   return ref->obj_field(_yieldInfo_offset);
 182 }
 183 inline void java_lang_Continuation::set_yieldInfo(oop ref, oop value) {
 184   ref->obj_field_put(_yieldInfo_offset, value);
 185 }
 186 inline typeArrayOop java_lang_Continuation::stack(oop ref) {
 187   oop a = ref->obj_field(_stack_offset);
 188   return (typeArrayOop)a;
 189 }
 190 inline objArrayOop java_lang_Continuation::refStack(oop ref) {
 191   oop a = ref->obj_field(_refStack_offset);
 192   return (objArrayOop)a;
 193 }
 194 inline void java_lang_Continuation::set_stack(oop ref, oop value) {
 195   ref->obj_field_put(_stack_offset, value);
 196 }
 197 inline void java_lang_Continuation::set_refStack(oop ref, oop value) {
 198   ref->obj_field_put(_refStack_offset, value);
 199 }
 200 inline jlong java_lang_Continuation::fp(oop ref) {
 201   return ref->long_field(_fp_offset);
 202 }
 203 inline void java_lang_Continuation::set_fp(oop ref, const jlong i) {
 204   ref->long_field_put(_fp_offset, i);
 205 }
 206 inline jint java_lang_Continuation::sp(oop ref) {
 207   return ref->int_field(_sp_offset);
 208 }
 209 inline void java_lang_Continuation::set_sp(oop ref, const jint i) {
 210   ref->int_field_put(_sp_offset, i);
 211 }
 212 inline void* java_lang_Continuation::pc(oop ref) {
 213   return (void*)ref->long_field(_pc_offset);
 214 }
 215 inline void java_lang_Continuation::set_pc(oop ref, const void* pc) {
 216   ref->long_field_put(_pc_offset, (jlong)pc);
 217 }
 218 inline jint java_lang_Continuation::refSP(oop ref) {
 219   return ref->int_field(_refSP_offset);
 220 }
 221 inline void java_lang_Continuation::set_refSP(oop ref, jint i) {
 222   ref->int_field_put(_refSP_offset, i);
 223 }
 224 inline intptr_t* java_lang_Continuation::entrySP(oop ref) {
 225   return (intptr_t*)ref->long_field(_entrySP_offset);
 226 }
 227 inline void java_lang_Continuation::set_entrySP(oop ref, intptr_t* sp) {
 228   ref->long_field_put(_entrySP_offset, (jlong)sp);
 229 }
 230 inline intptr_t* java_lang_Continuation::entryFP(oop ref) {
 231   return (intptr_t*)ref->long_field(_entryFP_offset);
 232 }
 233 inline void java_lang_Continuation::set_entryFP(oop ref, intptr_t* fp) {
 234   ref->long_field_put(_entryFP_offset, (jlong)fp);
 235 }
 236 inline address java_lang_Continuation::entryPC(oop ref) {
 237   return (address)ref->long_field(_entryPC_offset);
 238 }
 239 inline void java_lang_Continuation::set_entryPC(oop ref, address pc) {
 240   ref->long_field_put(_entryPC_offset, (jlong)pc);
 241 }
 242 inline address* java_lang_Continuation::entryPC_addr(oop ref) {
 243   return (address*)ref->field_addr(_entryPC_offset);
 244 }
 245 inline jint java_lang_Continuation::maxSize(oop ref) {
 246   return ref->int_field(_maxSize_offset);
 247 }
 248 inline void java_lang_Continuation::set_maxSize(oop ref, jint i) {
 249   ref->int_field_put(_maxSize_offset, i);
 250 }
 251 inline unsigned char java_lang_Continuation::flags(oop ref) {
 252   return (unsigned char)ref->byte_field(_flags_offset);
 253 }
 254 inline void java_lang_Continuation::set_flags(oop ref, unsigned char flags) {
 255   ref->byte_field_put(_flags_offset, (jbyte)flags);
 256 }
 257 inline jshort java_lang_Continuation::critical_section(oop ref) {
 258   return ref->short_field(_cs_offset);
 259 }
 260 inline bool java_lang_Continuation::is_reset(oop ref) {
 261   return ref->bool_field(_reset_offset);
 262 }
 263 inline jshort java_lang_Continuation::numFrames(oop ref) {
 264   return ref->short_field(_numFrames_offset);
 265 }
 266 inline void java_lang_Continuation::set_numFrames(oop ref, jshort i) {
 267   ref->short_field_put(_numFrames_offset, i);
 268 }
 269 inline jshort java_lang_Continuation::numInterpretedFrames(oop ref) {
 270   return ref->short_field(_numInterpretedFrames_offset);
 271 }
 272 inline void java_lang_Continuation::set_numInterpretedFrames(oop ref, jshort i) {
 273   ref->short_field_put(_numInterpretedFrames_offset, i);
 274 }
 275 inline int java_lang_Continuation::stack_size(oop ref) {
 276   return stack(ref)->length() * 4;
 277 }
 278 inline void* java_lang_Continuation::stack_base(oop ref) {
 279   return stack(ref)->base(T_INT);
 280 }
 281 inline HeapWord* java_lang_Continuation::refStack_base(oop ref) {
 282   return refStack(ref)->base();
 283 }
 284 
 285 inline void java_lang_invoke_CallSite::set_target_volatile(oop site, oop target) {
 286   site->obj_field_put_volatile(_target_offset, target);
 287 }
 288 
 289 inline oop  java_lang_invoke_CallSite::target(oop site) {
 290   return site->obj_field(_target_offset);
 291 }
 292 
 293 inline void java_lang_invoke_CallSite::set_target(oop site, oop target) {
 294   site->obj_field_put(_target_offset, target);
 295 }
 296 
 297 inline bool java_lang_invoke_CallSite::is_instance(oop obj) {
 298   return obj != NULL && is_subclass(obj->klass());
 299 }
 300 
 301 inline bool java_lang_invoke_MethodHandleNatives_CallSiteContext::is_instance(oop obj) {
 302   return obj != NULL && is_subclass(obj->klass());
 303 }
 304 
 305 inline bool java_lang_invoke_MemberName::is_instance(oop obj) {
 306   return obj != NULL && obj->klass() == SystemDictionary::MemberName_klass();
 307 }
 308 
 309 inline bool java_lang_invoke_ResolvedMethodName::is_instance(oop obj) {
 310   return obj != NULL && obj->klass() == SystemDictionary::ResolvedMethodName_klass();
 311 }
 312 
 313 inline bool java_lang_invoke_MethodType::is_instance(oop obj) {
 314   return obj != NULL && obj->klass() == SystemDictionary::MethodType_klass();
 315 }
 316 
 317 inline bool java_lang_invoke_MethodHandle::is_instance(oop obj) {
 318   return obj != NULL && is_subclass(obj->klass());
 319 }
 320 
 321 inline bool java_lang_Class::is_instance(oop obj) {
 322   return obj != NULL && obj->klass() == SystemDictionary::Class_klass();
 323 }
 324 
 325 inline bool java_lang_Class::is_primitive(oop java_class) {
 326   // should assert:
 327   //assert(java_lang_Class::is_instance(java_class), "must be a Class object");
 328   bool is_primitive = (java_class->metadata_field(_klass_offset) == NULL);
 329 
 330 #ifdef ASSERT
 331   if (is_primitive) {
 332     Klass* k = ((Klass*)java_class->metadata_field(_array_klass_offset));
 333     assert(k == NULL || is_java_primitive(ArrayKlass::cast(k)->element_type()),
 334         "Should be either the T_VOID primitive or a java primitive");
 335   }
 336 #endif
 337 
 338   return is_primitive;
 339 }
 340 
 341 inline int java_lang_Class::oop_size_raw(oop java_class) {
 342   assert(_oop_size_offset != 0, "must be set");
 343   int size = java_class->int_field_raw(_oop_size_offset);
 344   assert(size > 0, "Oop size must be greater than zero, not %d", size);
 345   return size;
 346 }
 347 
 348 inline bool java_lang_invoke_DirectMethodHandle::is_instance(oop obj) {
 349   return obj != NULL && is_subclass(obj->klass());
 350 }
 351 
 352 inline bool java_lang_Module::is_instance(oop obj) {
 353   return obj != NULL && obj->klass() == SystemDictionary::Module_klass();
 354 }
 355 
 356 inline int Backtrace::merge_bci_and_version(int bci, int version) {
 357   // only store u2 for version, checking for overflow.
 358   if (version > USHRT_MAX || version < 0) version = USHRT_MAX;
 359   assert((jushort)bci == bci, "bci should be short");
 360   return build_int_from_shorts(version, bci);
 361 }
 362 
 363 inline int Backtrace::merge_mid_and_cpref(int mid, int cpref) {
 364   // only store u2 for mid and cpref, checking for overflow.
 365   assert((jushort)mid == mid, "mid should be short");
 366   assert((jushort)cpref == cpref, "cpref should be short");
 367   return build_int_from_shorts(cpref, mid);
 368 }
 369 
 370 inline int Backtrace::bci_at(unsigned int merged) {
 371   return extract_high_short_from_int(merged);
 372 }
 373 
 374 inline int Backtrace::version_at(unsigned int merged) {
 375   return extract_low_short_from_int(merged);
 376 }
 377 
 378 inline int Backtrace::mid_at(unsigned int merged) {
 379   return extract_high_short_from_int(merged);
 380 }
 381 
 382 inline int Backtrace::cpref_at(unsigned int merged) {
 383   return extract_low_short_from_int(merged);
 384 }
 385 
 386 inline int Backtrace::get_line_number(const methodHandle& method, int bci) {
 387   int line_number = 0;
 388   if (method->is_native()) {
 389     // Negative value different from -1 below, enabling Java code in
 390     // class java.lang.StackTraceElement to distinguish "native" from
 391     // "no LineNumberTable".  JDK tests for -2.
 392     line_number = -2;
 393   } else {
 394     // Returns -1 if no LineNumberTable, and otherwise actual line number
 395     line_number = method->line_number_from_bci(bci);
 396     if (line_number == -1 && ShowHiddenFrames) {
 397       line_number = bci + 1000000;
 398     }
 399   }
 400   return line_number;
 401 }
 402 
 403 inline Symbol* Backtrace::get_source_file_name(InstanceKlass* holder, int version) {
 404   // RedefineClasses() currently permits redefine operations to
 405   // happen in parallel using a "last one wins" philosophy. That
 406   // spec laxness allows the constant pool entry associated with
 407   // the source_file_name_index for any older constant pool version
 408   // to be unstable so we shouldn't try to use it.
 409   if (holder->constants()->version() != version) {
 410     return NULL;
 411   } else {
 412     return holder->source_file_name();
 413   }
 414 }
 415 
 416 #endif // SHARE_CLASSFILE_JAVACLASSES_INLINE_HPP