1 /*
   2  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. 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.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "prims/jniFastGetField.hpp"
  30 #include "prims/jvm_misc.hpp"
  31 #include "runtime/safepoint.hpp"
  32 #include "runtime/jfieldIDWorkaround.hpp"
  33 
  34 #define __ masm->
  35 
  36 #define BUFFER_SIZE 30*wordSize
  37 
  38 // Instead of issuing a LoadLoad barrier we create an address
  39 // dependency between loads; this might be more efficient.
  40 
  41 // Common register usage:
  42 // r0/v0:      result
  43 // c_rarg0:    jni env
  44 // c_rarg1:    obj
  45 // c_rarg2:    jfield id
  46 
  47 static const Register robj          = r3;
  48 static const Register rcounter      = r4;
  49 static const Register roffset       = r5;
  50 static const Register rcounter_addr = r6;
  51 static const Register result        = r7;
  52 
  53 address JNI_FastGetField::generate_fast_get_int_field0(BasicType type) {
  54   const char *name;
  55   switch (type) {
  56     case T_BOOLEAN: name = "jni_fast_GetBooleanField"; break;
  57     case T_BYTE:    name = "jni_fast_GetByteField";    break;
  58     case T_CHAR:    name = "jni_fast_GetCharField";    break;
  59     case T_SHORT:   name = "jni_fast_GetShortField";   break;
  60     case T_INT:     name = "jni_fast_GetIntField";     break;
  61     case T_LONG:    name = "jni_fast_GetLongField";    break;
  62     case T_FLOAT:   name = "jni_fast_GetFloatField";   break;
  63     case T_DOUBLE:  name = "jni_fast_GetDoubleField";  break;
  64     default:        ShouldNotReachHere();
  65   }
  66   ResourceMark rm;
  67   BufferBlob* blob = BufferBlob::create(name, BUFFER_SIZE);
  68   CodeBuffer cbuf(blob);
  69   MacroAssembler* masm = new MacroAssembler(&cbuf);
  70   address fast_entry = __ pc();
  71 
  72   Label slow;
  73 
  74   unsigned long offset;
  75   __ adrp(rcounter_addr,
  76           SafepointSynchronize::safepoint_counter_addr(), offset);
  77   Address safepoint_counter_addr(rcounter_addr, offset);
  78   __ ldrw(rcounter, safepoint_counter_addr);
  79   __ andw(rscratch1, rcounter, 1);
  80   __ cbnzw(rscratch1, slow);
  81   __ eor(robj, c_rarg1, rcounter);
  82   __ eor(robj, robj, rcounter);               // obj, since
  83                                               // robj ^ rcounter ^ rcounter == robj
  84                                               // robj is address dependent on rcounter.
  85   __ ldr(robj, Address(robj, 0));             // *obj
  86   __ lsr(roffset, c_rarg2, jfieldIDWorkaround::offset_shift); // offset
  87 
  88   assert(count < LIST_CAPACITY, "LIST_CAPACITY too small");
  89   speculative_load_pclist[count] = __ pc();   // Used by the segfault handler
  90   switch (type) {
  91     case T_BOOLEAN: __ ldrb    (result, Address(robj, roffset)); break;
  92     case T_BYTE:    __ ldrsb   (result, Address(robj, roffset)); break;
  93     case T_CHAR:    __ ldrh    (result, Address(robj, roffset)); break;
  94     case T_SHORT:   __ ldrsh   (result, Address(robj, roffset)); break;
  95     case T_FLOAT:   __ ldrw    (result, Address(robj, roffset)); break;
  96     case T_INT:     __ ldrsw   (result, Address(robj, roffset)); break;
  97     case T_DOUBLE:
  98     case T_LONG:    __ ldr     (result, Address(robj, roffset)); break;
  99     default:        ShouldNotReachHere();
 100   }
 101 
 102   // counter_addr is address dependent on result.
 103   __ eor(rcounter_addr, rcounter_addr, result);
 104   __ eor(rcounter_addr, rcounter_addr, result);
 105   __ ldrw(rscratch1, safepoint_counter_addr);
 106   __ cmpw(rcounter, rscratch1);
 107   __ br (Assembler::NE, slow);
 108 
 109   switch (type) {
 110     case T_FLOAT:   __ fmovs(v0, result); break;
 111     case T_DOUBLE:  __ fmovd(v0, result); break;
 112     default:        __ mov(r0, result);   break;
 113   }
 114   __ ret(lr);
 115 
 116   slowcase_entry_pclist[count++] = __ pc();
 117   __ bind(slow);
 118   address slow_case_addr;
 119   switch (type) {
 120     case T_BOOLEAN: slow_case_addr = jni_GetBooleanField_addr(); break;
 121     case T_BYTE:    slow_case_addr = jni_GetByteField_addr();    break;
 122     case T_CHAR:    slow_case_addr = jni_GetCharField_addr();    break;
 123     case T_SHORT:   slow_case_addr = jni_GetShortField_addr();   break;
 124     case T_INT:     slow_case_addr = jni_GetIntField_addr();     break;
 125     case T_LONG:    slow_case_addr = jni_GetLongField_addr();    break;
 126     case T_FLOAT:   slow_case_addr = jni_GetFloatField_addr();   break;
 127     case T_DOUBLE:  slow_case_addr = jni_GetDoubleField_addr();  break;
 128     default:        ShouldNotReachHere();
 129   }
 130 
 131   {
 132     __ enter();
 133     __ lea(rscratch1, ExternalAddress(slow_case_addr));
 134     __ blr(rscratch1);
 135     __ maybe_isb();
 136     __ leave();
 137     __ ret(lr);
 138   }
 139   __ flush ();
 140 
 141   return fast_entry;
 142 }
 143 
 144 address JNI_FastGetField::generate_fast_get_boolean_field() {
 145   return generate_fast_get_int_field0(T_BOOLEAN);
 146 }
 147 
 148 address JNI_FastGetField::generate_fast_get_byte_field() {
 149   return generate_fast_get_int_field0(T_BYTE);
 150 }
 151 
 152 address JNI_FastGetField::generate_fast_get_char_field() {
 153   return generate_fast_get_int_field0(T_CHAR);
 154 }
 155 
 156 address JNI_FastGetField::generate_fast_get_short_field() {
 157   return generate_fast_get_int_field0(T_SHORT);
 158 }
 159 
 160 address JNI_FastGetField::generate_fast_get_int_field() {
 161   return generate_fast_get_int_field0(T_INT);
 162 }
 163 
 164 address JNI_FastGetField::generate_fast_get_long_field() {
 165   return generate_fast_get_int_field0(T_LONG);
 166 }
 167 
 168 address JNI_FastGetField::generate_fast_get_float_field() {
 169   return generate_fast_get_int_field0(T_FLOAT);
 170 }
 171 
 172 address JNI_FastGetField::generate_fast_get_double_field() {
 173   return generate_fast_get_int_field0(T_DOUBLE);
 174 }
 175 
--- EOF ---