1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "gc/shared/barrierSetAssembler.hpp"
  29 #include "interpreter/interp_masm.hpp"
  30 #include "oops/compressedOops.hpp"
  31 #include "runtime/jniHandles.hpp"
  32 
  33 #define __ masm->
  34 
  35 void BarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
  36                                    Register base, RegisterOrConstant ind_or_offs, Register val,
  37                                    Register tmp1, Register tmp2, Register tmp3, bool needs_frame) {
  38   bool in_heap = (decorators & IN_HEAP) != 0;
  39   bool in_native = (decorators & IN_NATIVE) != 0;
  40   bool not_null = (decorators & IS_NOT_NULL) != 0;
  41   assert(in_heap || in_native, "where?");
  42   assert_different_registers(base, val, tmp1, tmp2, R0);
  43 
  44   switch (type) {
  45   case T_ARRAY:
  46   case T_OBJECT: {
  47     if (UseCompressedOops && in_heap) {
  48       Register co = tmp1;
  49       if (val == noreg) {
  50         __ li(co, 0);
  51       } else {
  52         co = not_null ? __ encode_heap_oop_not_null(tmp1, val) : __ encode_heap_oop(tmp1, val);
  53       }
  54       __ stw(co, ind_or_offs, base, tmp2);
  55     } else {
  56       if (val == noreg) {
  57         val = tmp1;
  58         __ li(val, 0);
  59       }
  60       __ std(val, ind_or_offs, base, tmp2);
  61     }
  62     break;
  63   }
  64   default: Unimplemented();
  65   }
  66 }
  67 
  68 void BarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
  69                                   Register base, RegisterOrConstant ind_or_offs, Register dst,
  70                                   Register tmp1, Register tmp2, bool needs_frame, Label *L_handle_null) {
  71   bool in_heap = (decorators & IN_HEAP) != 0;
  72   bool in_native = (decorators & IN_NATIVE) != 0;
  73   bool not_null = (decorators & IS_NOT_NULL) != 0;
  74   assert(in_heap || in_native, "where?");
  75   assert_different_registers(ind_or_offs.register_or_noreg(), dst, R0);
  76 
  77   switch (type) {
  78   case T_ARRAY:
  79   case T_OBJECT: {
  80     if (UseCompressedOops && in_heap) {
  81       if (L_handle_null != NULL) { // Label provided.
  82         __ lwz(dst, ind_or_offs, base);
  83         __ cmpwi(CCR0, dst, 0);
  84         __ beq(CCR0, *L_handle_null);
  85         __ decode_heap_oop_not_null(dst);
  86       } else if (not_null) { // Guaranteed to be not null.
  87         Register narrowOop = (tmp1 != noreg && CompressedOops::base_disjoint()) ? tmp1 : dst;
  88         __ lwz(narrowOop, ind_or_offs, base);
  89         __ decode_heap_oop_not_null(dst, narrowOop);
  90       } else { // Any oop.
  91         __ lwz(dst, ind_or_offs, base);
  92         __ decode_heap_oop(dst);
  93       }
  94     } else {
  95       __ ld(dst, ind_or_offs, base);
  96       if (L_handle_null != NULL) {
  97         __ cmpdi(CCR0, dst, 0);
  98         __ beq(CCR0, *L_handle_null);
  99       }
 100     }
 101     break;
 102   }
 103   default: Unimplemented();
 104   }
 105 }
 106 
 107 void BarrierSetAssembler::resolve_jobject(MacroAssembler* masm, Register value,
 108                                           Register tmp1, Register tmp2, bool needs_frame) {
 109   Label done;
 110   __ cmpdi(CCR0, value, 0);
 111   __ beq(CCR0, done);         // Use NULL as-is.
 112 
 113   __ clrrdi(tmp1, value, JNIHandles::weak_tag_size);
 114   __ ld(value, 0, tmp1);      // Resolve (untagged) jobject.
 115 
 116   __ verify_oop(value, FILE_AND_LINE);
 117   __ bind(done);
 118 }
 119 
 120 void BarrierSetAssembler::try_resolve_jobject_in_native(MacroAssembler* masm, Register dst, Register jni_env,
 121                                                         Register obj, Register tmp, Label& slowpath) {
 122   __ clrrdi(dst, obj, JNIHandles::weak_tag_size);
 123   __ ld(dst, 0, dst);         // Resolve (untagged) jobject.
 124 }