1 /* 2 * Copyright (c) 2008, 2018, 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 #include "precompiled.hpp" 26 #include "asm/assembler.hpp" 27 #include "asm/macroAssembler.inline.hpp" 28 #include "assembler_arm.inline.hpp" 29 #include "code/vtableStubs.hpp" 30 #include "interp_masm_arm.hpp" 31 #include "memory/resourceArea.hpp" 32 #include "oops/compiledICHolder.hpp" 33 #include "oops/instanceKlass.hpp" 34 #include "oops/klassVtable.hpp" 35 #include "runtime/sharedRuntime.hpp" 36 #include "vmreg_arm.inline.hpp" 37 #ifdef COMPILER2 38 #include "opto/runtime.hpp" 39 #endif 40 41 // machine-dependent part of VtableStubs: create VtableStub of correct size and 42 // initialize its code 43 44 #define __ masm-> 45 46 #ifndef PRODUCT 47 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index); 48 #endif 49 50 VtableStub* VtableStubs::create_vtable_stub(int vtable_index) { 51 // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing. 52 const int stub_code_length = VtableStub::code_size_limit(true); 53 VtableStub* s = new(stub_code_length) VtableStub(true, vtable_index); 54 // Can be NULL if there is no free space in the code cache. 55 if (s == NULL) { 56 return NULL; 57 } 58 59 // Count unused bytes in instruction sequences of variable size. 60 // We add them to the computed buffer size in order to avoid 61 // overflow in subsequently generated stubs. 62 address start_pc; 63 int slop_bytes = 0; 64 int slop_delta = 0; 65 66 ResourceMark rm; 67 CodeBuffer cb(s->entry_point(), stub_code_length); 68 MacroAssembler* masm = new MacroAssembler(&cb); 69 70 #if (!defined(PRODUCT) && defined(COMPILER2)) 71 if (CountCompiledCalls) { 72 // Implementation required? 73 } 74 #endif 75 76 assert(VtableStub::receiver_location() == R0->as_VMReg(), "receiver expected in R0"); 77 78 const Register tmp = Rtemp; // Rtemp OK, should be free at call sites 79 80 address npe_addr = __ pc(); 81 __ load_klass(tmp, R0); 82 83 #ifndef PRODUCT 84 if (DebugVtables) { 85 // Implementation required? 86 } 87 #endif 88 89 start_pc = __ pc(); 90 { // lookup virtual method 91 int entry_offset = in_bytes(Klass::vtable_start_offset()) + vtable_index * vtableEntry::size_in_bytes(); 92 int method_offset = vtableEntry::method_offset_in_bytes() + entry_offset; 93 94 assert ((method_offset & (wordSize - 1)) == 0, "offset should be aligned"); 95 int offset_mask = AARCH64_ONLY(0xfff << LogBytesPerWord) NOT_AARCH64(0xfff); 96 if (method_offset & ~offset_mask) { 97 __ add(tmp, tmp, method_offset & ~offset_mask); 98 } 99 __ ldr(Rmethod, Address(tmp, method_offset & offset_mask)); 100 } 101 slop_delta = 8 - (int)(__ pc() - start_pc); 102 slop_bytes += slop_delta; 103 assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta); 104 105 #ifndef PRODUCT 106 if (DebugVtables) { 107 // Implementation required? 108 } 109 #endif 110 111 address ame_addr = __ pc(); 112 #ifdef AARCH64 113 __ ldr(tmp, Address(Rmethod, Method::from_compiled_offset())); 114 __ br(tmp); 115 #else 116 __ ldr(PC, Address(Rmethod, Method::from_compiled_offset())); 117 #endif // AARCH64 118 119 masm->flush(); 120 bookkeeping(masm, tty, s, npe_addr, ame_addr, true, vtable_index, slop_bytes, 0); 121 122 return s; 123 } 124 125 VtableStub* VtableStubs::create_itable_stub(int itable_index) { 126 // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing. 127 const int stub_code_length = VtableStub::code_size_limit(false); 128 VtableStub* s = new(stub_code_length) VtableStub(false, itable_index); 129 // Can be NULL if there is no free space in the code cache. 130 if (s == NULL) { 131 return NULL; 132 } 133 // Count unused bytes in instruction sequences of variable size. 134 // We add them to the computed buffer size in order to avoid 135 // overflow in subsequently generated stubs. 136 address start_pc; 137 int slop_bytes = 0; 138 int slop_delta = 0; 139 140 ResourceMark rm; 141 CodeBuffer cb(s->entry_point(), stub_code_length); 142 MacroAssembler* masm = new MacroAssembler(&cb); 143 144 #if (!defined(PRODUCT) && defined(COMPILER2)) 145 if (CountCompiledCalls) { 146 // Implementation required? 147 } 148 #endif 149 150 assert(VtableStub::receiver_location() == R0->as_VMReg(), "receiver expected in R0"); 151 152 // R0-R3 / R0-R7 registers hold the arguments and cannot be spoiled 153 const Register Rclass = AARCH64_ONLY(R9) NOT_AARCH64(R4); 154 const Register Rintf = AARCH64_ONLY(R10) NOT_AARCH64(R5); 155 const Register Rscan = AARCH64_ONLY(R11) NOT_AARCH64(R6); 156 157 assert_different_registers(Ricklass, Rclass, Rintf, Rscan, Rtemp); 158 159 // Calculate the start of itable (itable goes after vtable) 160 const int scale = exact_log2(vtableEntry::size_in_bytes()); 161 address npe_addr = __ pc(); 162 __ load_klass(Rclass, R0); 163 164 Label L_no_such_interface; 165 166 start_pc = __ pc(); 167 168 // Receiver subtype check against REFC. 169 __ ldr(Rintf, Address(Ricklass, CompiledICHolder::holder_klass_offset())); 170 __ lookup_interface_method(// inputs: rec. class, interface, itable index 171 Rclass, Rintf, noreg, 172 // outputs: temp reg1, temp reg2 173 noreg, Rscan, Rtemp, 174 L_no_such_interface); 175 176 const ptrdiff_t typecheckSize = __ pc() - start_pc; 177 start_pc = __ pc(); 178 179 // Get Method* and entry point for compiler 180 __ ldr(Rintf, Address(Ricklass, CompiledICHolder::holder_metadata_offset())); 181 __ lookup_interface_method(// inputs: rec. class, interface, itable index 182 Rclass, Rintf, itable_index, 183 // outputs: temp reg1, temp reg2, temp reg3 184 Rmethod, Rscan, Rtemp, 185 L_no_such_interface); 186 187 const ptrdiff_t lookupSize = __ pc() - start_pc; 188 189 // Reduce "estimate" such that "padding" does not drop below 8. 190 const ptrdiff_t estimate = 130; 191 const ptrdiff_t codesize = typecheckSize + lookupSize; 192 slop_delta = (int)(estimate - codesize); 193 slop_bytes += slop_delta; 194 assert(slop_delta >= 0, "itable #%d: Code size estimate (%d) for lookup_interface_method too small, required: %d", itable_index, (int)estimate, (int)codesize); 195 196 #ifndef PRODUCT 197 if (DebugVtables) { 198 // Implementation required? 199 } 200 #endif 201 202 address ame_addr = __ pc(); 203 204 #ifdef AARCH64 205 __ ldr(Rtemp, Address(Rmethod, Method::from_compiled_offset())); 206 __ br(Rtemp); 207 #else 208 __ ldr(PC, Address(Rmethod, Method::from_compiled_offset())); 209 #endif // AARCH64 210 211 __ bind(L_no_such_interface); 212 // Handle IncompatibleClassChangeError in itable stubs. 213 // More detailed error message. 214 // We force resolving of the call site by jumping to the "handle 215 // wrong method" stub, and so let the interpreter runtime do all the 216 // dirty work. 217 assert(SharedRuntime::get_handle_wrong_method_stub() != NULL, "check initialization order"); 218 __ jump(SharedRuntime::get_handle_wrong_method_stub(), relocInfo::runtime_call_type, Rtemp); 219 220 masm->flush(); 221 bookkeeping(masm, tty, s, npe_addr, ame_addr, false, itable_index, slop_bytes, 0); 222 223 return s; 224 } 225 226 int VtableStub::pd_code_alignment() { 227 // arm cache line size is 64 bytes, but we just align on word size. 228 const unsigned int icache_line_size = wordSize; 229 return icache_line_size; 230 }