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