Thu, 27 Dec 2018 11:43:33 +0800
Merge
1 /*
2 * Copyright (c) 1997, 2017, 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 */
25 #include "precompiled.hpp"
26 #include "asm/macroAssembler.inline.hpp"
27 #include "code/vtableStubs.hpp"
28 #include "interp_masm_sparc.hpp"
29 #include "memory/resourceArea.hpp"
30 #include "oops/compiledICHolder.hpp"
31 #include "oops/instanceKlass.hpp"
32 #include "oops/klassVtable.hpp"
33 #include "runtime/sharedRuntime.hpp"
34 #include "vmreg_sparc.inline.hpp"
35 #ifdef COMPILER2
36 #include "opto/runtime.hpp"
37 #endif
39 // machine-dependent part of VtableStubs: create vtableStub of correct size and
40 // initialize its code
42 #define __ masm->
45 #ifndef PRODUCT
46 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oopDesc* receiver, int index);
47 #endif
50 // Used by compiler only; may use only caller saved, non-argument registers
51 // NOTE: %%%% if any change is made to this stub make sure that the function
52 // pd_code_size_limit is changed to ensure the correct size for VtableStub
53 VtableStub* VtableStubs::create_vtable_stub(int vtable_index) {
54 const int sparc_code_length = VtableStub::pd_code_size_limit(true);
55 VtableStub* s = new(sparc_code_length) VtableStub(true, vtable_index);
56 // Can be NULL if there is no free space in the code cache.
57 if (s == NULL) {
58 return NULL;
59 }
61 ResourceMark rm;
62 CodeBuffer cb(s->entry_point(), sparc_code_length);
63 MacroAssembler* masm = new MacroAssembler(&cb);
65 #ifndef PRODUCT
66 if (CountCompiledCalls) {
67 __ inc_counter(SharedRuntime::nof_megamorphic_calls_addr(), G5, G3_scratch);
68 }
69 #endif /* PRODUCT */
71 assert(VtableStub::receiver_location() == O0->as_VMReg(), "receiver expected in O0");
73 // get receiver klass
74 address npe_addr = __ pc();
75 __ load_klass(O0, G3_scratch);
77 // set Method* (in case of interpreted method), and destination address
78 #ifndef PRODUCT
79 if (DebugVtables) {
80 Label L;
81 // check offset vs vtable length
82 __ ld(G3_scratch, InstanceKlass::vtable_length_offset()*wordSize, G5);
83 __ cmp_and_br_short(G5, vtable_index*vtableEntry::size(), Assembler::greaterUnsigned, Assembler::pt, L);
84 __ set(vtable_index, O2);
85 __ call_VM(noreg, CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), O0, O2);
86 __ bind(L);
87 }
88 #endif
90 __ lookup_virtual_method(G3_scratch, vtable_index, G5_method);
92 #ifndef PRODUCT
93 if (DebugVtables) {
94 Label L;
95 __ br_notnull_short(G5_method, Assembler::pt, L);
96 __ stop("Vtable entry is ZERO");
97 __ bind(L);
98 }
99 #endif
101 address ame_addr = __ pc(); // if the vtable entry is null, the method is abstract
102 // NOTE: for vtable dispatches, the vtable entry will never be null.
104 __ ld_ptr(G5_method, in_bytes(Method::from_compiled_offset()), G3_scratch);
106 // jump to target (either compiled code or c2iadapter)
107 __ JMP(G3_scratch, 0);
108 // load Method* (in case we call c2iadapter)
109 __ delayed()->nop();
111 masm->flush();
113 if (PrintMiscellaneous && (WizardMode || Verbose)) {
114 tty->print_cr("vtable #%d at " PTR_FORMAT "[%d] left over: %d",
115 vtable_index, s->entry_point(),
116 (int)(s->code_end() - s->entry_point()),
117 (int)(s->code_end() - __ pc()));
118 }
119 guarantee(__ pc() <= s->code_end(), "overflowed buffer");
120 // shut the door on sizing bugs
121 int slop = 2*BytesPerInstWord; // 32-bit offset is this much larger than a 13-bit one
122 assert(vtable_index > 10 || __ pc() + slop <= s->code_end(), "room for sethi;add");
124 s->set_exception_points(npe_addr, ame_addr);
125 return s;
126 }
129 // NOTE: %%%% if any change is made to this stub make sure that the function
130 // pd_code_size_limit is changed to ensure the correct size for VtableStub
131 VtableStub* VtableStubs::create_itable_stub(int itable_index) {
132 const int sparc_code_length = VtableStub::pd_code_size_limit(false);
133 VtableStub* s = new(sparc_code_length) VtableStub(false, itable_index);
134 // Can be NULL if there is no free space in the code cache.
135 if (s == NULL) {
136 return NULL;
137 }
139 ResourceMark rm;
140 CodeBuffer cb(s->entry_point(), sparc_code_length);
141 MacroAssembler* masm = new MacroAssembler(&cb);
143 Register G3_Klass = G3_scratch;
144 Register G5_icholder = G5; // Passed in as an argument
145 Register G4_interface = G4_scratch;
146 Label search;
148 // Entry arguments:
149 // G5_interface: Interface
150 // O0: Receiver
151 assert(VtableStub::receiver_location() == O0->as_VMReg(), "receiver expected in O0");
153 // get receiver klass (also an implicit null-check)
154 address npe_addr = __ pc();
155 __ load_klass(O0, G3_Klass);
157 // Push a new window to get some temp registers. This chops the head of all
158 // my 64-bit %o registers in the LION build, but this is OK because no longs
159 // are passed in the %o registers. Instead, longs are passed in G1 and G4
160 // and so those registers are not available here.
161 __ save(SP,-frame::register_save_words*wordSize,SP);
163 #ifndef PRODUCT
164 if (CountCompiledCalls) {
165 __ inc_counter(SharedRuntime::nof_megamorphic_calls_addr(), L0, L1);
166 }
167 #endif /* PRODUCT */
169 Label L_no_such_interface;
171 Register L5_method = L5;
173 // Receiver subtype check against REFC.
174 __ ld_ptr(G5_icholder, CompiledICHolder::holder_klass_offset(), G4_interface);
175 __ lookup_interface_method(// inputs: rec. class, interface, itable index
176 G3_Klass, G4_interface, itable_index,
177 // outputs: scan temp. reg1, scan temp. reg2
178 L5_method, L2, L3,
179 L_no_such_interface,
180 /*return_method=*/ false);
182 // Get Method* and entrypoint for compiler
183 __ ld_ptr(G5_icholder, CompiledICHolder::holder_metadata_offset(), G4_interface);
184 __ lookup_interface_method(// inputs: rec. class, interface, itable index
185 G3_Klass, G4_interface, itable_index,
186 // outputs: method, scan temp. reg
187 L5_method, L2, L3,
188 L_no_such_interface);
190 #ifndef PRODUCT
191 if (DebugVtables) {
192 Label L01;
193 __ br_notnull_short(L5_method, Assembler::pt, L01);
194 __ stop("Method* is null");
195 __ bind(L01);
196 }
197 #endif
199 // If the following load is through a NULL pointer, we'll take an OS
200 // exception that should translate into an AbstractMethodError. We need the
201 // window count to be correct at that time.
202 __ restore(L5_method, 0, G5_method);
203 // Restore registers *before* the AME point.
205 address ame_addr = __ pc(); // if the vtable entry is null, the method is abstract
206 __ ld_ptr(G5_method, in_bytes(Method::from_compiled_offset()), G3_scratch);
208 // G5_method: Method*
209 // O0: Receiver
210 // G3_scratch: entry point
211 __ JMP(G3_scratch, 0);
212 __ delayed()->nop();
214 __ bind(L_no_such_interface);
215 AddressLiteral icce(StubRoutines::throw_IncompatibleClassChangeError_entry());
216 __ jump_to(icce, G3_scratch);
217 __ delayed()->restore();
219 masm->flush();
221 if (PrintMiscellaneous && (WizardMode || Verbose)) {
222 tty->print_cr("itable #%d at " PTR_FORMAT "[%d] left over: %d",
223 itable_index, s->entry_point(),
224 (int)(s->code_end() - s->entry_point()),
225 (int)(s->code_end() - __ pc()));
226 }
227 guarantee(__ pc() <= s->code_end(), "overflowed buffer");
228 // shut the door on sizing bugs
229 int slop = 2*BytesPerInstWord; // 32-bit offset is this much larger than a 13-bit one
230 assert(itable_index > 10 || __ pc() + slop <= s->code_end(), "room for sethi;add");
232 s->set_exception_points(npe_addr, ame_addr);
233 return s;
234 }
237 int VtableStub::pd_code_size_limit(bool is_vtable_stub) {
238 if (TraceJumps || DebugVtables || CountCompiledCalls || VerifyOops) return 1000;
239 else {
240 const int slop = 2*BytesPerInstWord; // sethi;add (needed for long offsets)
241 if (is_vtable_stub) {
242 // ld;ld;ld,jmp,nop
243 const int basic = 5*BytesPerInstWord +
244 // shift;add for load_klass (only shift with zero heap based)
245 (UseCompressedClassPointers ?
246 MacroAssembler::instr_size_for_decode_klass_not_null() : 0);
247 return basic + slop;
248 } else {
249 const int basic = (48 LP64_ONLY(+ 6)) * BytesPerInstWord +
250 // shift;add for load_klass (only shift with zero heap based)
251 (UseCompressedClassPointers ?
252 MacroAssembler::instr_size_for_decode_klass_not_null() : 0);
253 return (basic + slop);
254 }
255 }
257 // In order to tune these parameters, run the JVM with VM options
258 // +PrintMiscellaneous and +WizardMode to see information about
259 // actual itable stubs. Look for lines like this:
260 // itable #1 at 0x5551212[116] left over: 8
261 // Reduce the constants so that the "left over" number is 8
262 // Do not aim at a left-over number of zero, because a very
263 // large vtable or itable offset (> 4K) will require an extra
264 // sethi/or pair of instructions.
265 //
266 // The JVM98 app. _202_jess has a megamorphic interface call.
267 // The itable code looks like this:
268 // Decoding VtableStub itbl[1]@16
269 // ld [ %o0 + 4 ], %g3
270 // save %sp, -64, %sp
271 // ld [ %g3 + 0xe8 ], %l2
272 // sll %l2, 2, %l2
273 // add %l2, 0x134, %l2
274 // and %l2, -8, %l2 ! NOT_LP64 only
275 // add %g3, %l2, %l2
276 // add %g3, 4, %g3
277 // ld [ %l2 ], %l5
278 // brz,pn %l5, throw_icce
279 // cmp %l5, %g5
280 // be %icc, success
281 // add %l2, 8, %l2
282 // loop:
283 // ld [ %l2 ], %l5
284 // brz,pn %l5, throw_icce
285 // cmp %l5, %g5
286 // bne,pn %icc, loop
287 // add %l2, 8, %l2
288 // success:
289 // ld [ %l2 + -4 ], %l2
290 // ld [ %g3 + %l2 ], %l5
291 // restore %l5, 0, %g5
292 // ld [ %g5 + 0x44 ], %g3
293 // jmp %g3
294 // nop
295 // throw_icce:
296 // sethi %hi(throw_ICCE_entry), %g3
297 // ! 5 more instructions here, LP64_ONLY
298 // jmp %g3 + %lo(throw_ICCE_entry)
299 // restore
300 }
303 int VtableStub::pd_code_alignment() {
304 // UltraSPARC cache line size is 8 instructions:
305 const unsigned int icache_line_size = 32;
306 return icache_line_size;
307 }