27 #include "code/vtableStubs.hpp"
28 #include "interp_masm_x86.hpp"
29 #include "memory/resourceArea.hpp"
30 #include "oops/instanceKlass.hpp"
31 #include "oops/klassVtable.hpp"
32 #include "runtime/sharedRuntime.hpp"
33 #include "vmreg_x86.inline.hpp"
34 #ifdef COMPILER2
35 #include "opto/runtime.hpp"
36 #endif
37
38 // machine-dependent part of VtableStubs: create VtableStub of correct size and
39 // initialize its code
40
41 #define __ masm->
42
43 #ifndef PRODUCT
44 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index);
45 #endif
46
47 VtableStub* VtableStubs::create_vtable_stub(int vtable_index) {
48 // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing.
49 const int stub_code_length = code_size_limit(true);
50 VtableStub* s = new(stub_code_length) VtableStub(true, vtable_index);
51 // Can be null if there is no free space in the code cache.
52 if (s == nullptr) {
53 return nullptr;
54 }
55
56 // Count unused bytes in instruction sequences of variable size.
57 // We add them to the computed buffer size in order to avoid
58 // overflow in subsequently generated stubs.
59 address start_pc;
60 int slop_bytes = 0;
61 int slop_delta = 0;
62 // No variance was detected in vtable stub sizes. Setting index_dependent_slop == 0 will unveil any deviation from this observation.
63 const int index_dependent_slop = 0;
64
65 ResourceMark rm;
66 CodeBuffer cb(s->entry_point(), stub_code_length);
67 MacroAssembler* masm = new MacroAssembler(&cb);
68
69 #if (!defined(PRODUCT) && defined(COMPILER2))
70 if (CountCompiledCalls) {
71 __ incrementq(ExternalAddress(SharedRuntime::nof_megamorphic_calls_addr()), rscratch1);
72 }
73 #endif
74
75 // get receiver (need to skip return address on top of stack)
76 assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0");
77
78 // Free registers (non-args) are rax, rbx
79
80 // get receiver klass
81 address npe_addr = __ pc();
82 __ load_klass(rax, j_rarg0, rscratch1);
83
100 slop_bytes += slop_delta;
101 assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta);
102 __ bind(L);
103 }
104 #endif // PRODUCT
105
106 const Register method = rbx;
107
108 // load Method* and target address
109 start_pc = __ pc();
110 __ lookup_virtual_method(rax, vtable_index, method);
111 slop_delta = 8 - (int)(__ pc() - start_pc);
112 slop_bytes += slop_delta;
113 assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta);
114
115 #ifndef PRODUCT
116 if (DebugVtables) {
117 Label L;
118 __ cmpptr(method, NULL_WORD);
119 __ jcc(Assembler::equal, L);
120 __ cmpptr(Address(method, Method::from_compiled_offset()), NULL_WORD);
121 __ jcc(Assembler::notZero, L);
122 __ stop("Vtable entry is null");
123 __ bind(L);
124 }
125 #endif // PRODUCT
126
127 // rax: receiver klass
128 // method (rbx): Method*
129 // rcx: receiver
130 address ame_addr = __ pc();
131 __ jmp( Address(rbx, Method::from_compiled_offset()));
132
133 masm->flush();
134 slop_bytes += index_dependent_slop; // add'l slop for size variance due to large itable offsets
135 bookkeeping(masm, tty, s, npe_addr, ame_addr, true, vtable_index, slop_bytes, index_dependent_slop);
136
137 return s;
138 }
139
140
141 VtableStub* VtableStubs::create_itable_stub(int itable_index) {
142 // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing.
143 const int stub_code_length = code_size_limit(false);
144 VtableStub* s = new(stub_code_length) VtableStub(false, itable_index);
145 // Can be null if there is no free space in the code cache.
146 if (s == nullptr) {
147 return nullptr;
148 }
149
150 // Count unused bytes in instruction sequences of variable size.
151 // We add them to the computed buffer size in order to avoid
152 // overflow in subsequently generated stubs.
153 address start_pc;
154 int slop_bytes = 0;
155 int slop_delta = 0;
156 const int index_dependent_slop = (itable_index == 0) ? 4 : // code size change with transition from 8-bit to 32-bit constant (@index == 16).
157 (itable_index < 16) ? 3 : 0; // index == 0 generates even shorter code.
158
159 ResourceMark rm;
160 CodeBuffer cb(s->entry_point(), stub_code_length);
161 MacroAssembler *masm = new MacroAssembler(&cb);
162
163 #if (!defined(PRODUCT) && defined(COMPILER2))
164 if (CountCompiledCalls) {
165 __ incrementq(ExternalAddress(SharedRuntime::nof_megamorphic_calls_addr()), rscratch1);
192
193 start_pc = __ pc();
194
195 // Receiver subtype check against REFC.
196 // Get selected method from declaring class and itable index
197 __ lookup_interface_method_stub(recv_klass_reg, // input
198 holder_klass_reg, // input
199 resolved_klass_reg, // input
200 method, // output
201 temp_reg,
202 temp_reg2,
203 noreg,
204 itable_index,
205 L_no_such_interface);
206
207 const ptrdiff_t lookupSize = __ pc() - start_pc;
208
209 // We expect we need index_dependent_slop extra bytes. Reason:
210 // The emitted code in lookup_interface_method changes when itable_index exceeds 15.
211 // For linux, a very narrow estimate would be 112, but Solaris requires some more space (130).
212 const ptrdiff_t estimate = 136;
213 const ptrdiff_t codesize = lookupSize + index_dependent_slop;
214 slop_delta = (int)(estimate - codesize);
215 slop_bytes += slop_delta;
216 assert(slop_delta >= 0, "itable #%d: Code size estimate (%d) for lookup_interface_method too small, required: %d", itable_index, (int)estimate, (int)codesize);
217
218 // If we take a trap while this arg is on the stack we will not
219 // be able to walk the stack properly. This is not an issue except
220 // when there are mistakes in this assembly code that could generate
221 // a spurious fault. Ask me how I know...
222
223 // method (rbx): Method*
224 // j_rarg0: receiver
225
226 #ifdef ASSERT
227 if (DebugVtables) {
228 Label L2;
229 __ cmpptr(method, NULL_WORD);
230 __ jcc(Assembler::equal, L2);
231 __ cmpptr(Address(method, Method::from_compiled_offset()), NULL_WORD);
232 __ jcc(Assembler::notZero, L2);
233 __ stop("compiler entrypoint is null");
234 __ bind(L2);
235 }
236 #endif // ASSERT
237
238 address ame_addr = __ pc();
239 __ jmp(Address(method, Method::from_compiled_offset()));
240
241 __ bind(L_no_such_interface);
242 // Handle IncompatibleClassChangeError in itable stubs.
243 // More detailed error message.
244 // We force resolving of the call site by jumping to the "handle
245 // wrong method" stub, and so let the interpreter runtime do all the
246 // dirty work.
247 __ jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub()));
248
249 masm->flush();
250 slop_bytes += index_dependent_slop; // add'l slop for size variance due to large itable offsets
251 bookkeeping(masm, tty, s, npe_addr, ame_addr, false, itable_index, slop_bytes, index_dependent_slop);
252
253 return s;
254 }
255
256 int VtableStub::pd_code_alignment() {
257 // x86 cache line size is 64 bytes, but we want to limit alignment loss.
258 const unsigned int icache_line_size = wordSize;
259 return icache_line_size;
|
27 #include "code/vtableStubs.hpp"
28 #include "interp_masm_x86.hpp"
29 #include "memory/resourceArea.hpp"
30 #include "oops/instanceKlass.hpp"
31 #include "oops/klassVtable.hpp"
32 #include "runtime/sharedRuntime.hpp"
33 #include "vmreg_x86.inline.hpp"
34 #ifdef COMPILER2
35 #include "opto/runtime.hpp"
36 #endif
37
38 // machine-dependent part of VtableStubs: create VtableStub of correct size and
39 // initialize its code
40
41 #define __ masm->
42
43 #ifndef PRODUCT
44 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index);
45 #endif
46
47 VtableStub* VtableStubs::create_vtable_stub(int vtable_index, bool caller_is_c1) {
48 // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing.
49 const int stub_code_length = code_size_limit(true);
50 VtableStub* s = new(stub_code_length) VtableStub(true, vtable_index, caller_is_c1);
51 // Can be nullptr if there is no free space in the code cache.
52 if (s == nullptr) {
53 return nullptr;
54 }
55
56 // Count unused bytes in instruction sequences of variable size.
57 // We add them to the computed buffer size in order to avoid
58 // overflow in subsequently generated stubs.
59 address start_pc;
60 int slop_bytes = 0;
61 int slop_delta = 0;
62 // No variance was detected in vtable stub sizes. Setting index_dependent_slop == 0 will unveil any deviation from this observation.
63 const int index_dependent_slop = 0;
64 ByteSize entry_offset = caller_is_c1 ? Method::from_compiled_inline_offset() : Method::from_compiled_inline_ro_offset();
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 __ incrementq(ExternalAddress(SharedRuntime::nof_megamorphic_calls_addr()), rscratch1);
73 }
74 #endif
75
76 // get receiver (need to skip return address on top of stack)
77 assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0");
78
79 // Free registers (non-args) are rax, rbx
80
81 // get receiver klass
82 address npe_addr = __ pc();
83 __ load_klass(rax, j_rarg0, rscratch1);
84
101 slop_bytes += slop_delta;
102 assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta);
103 __ bind(L);
104 }
105 #endif // PRODUCT
106
107 const Register method = rbx;
108
109 // load Method* and target address
110 start_pc = __ pc();
111 __ lookup_virtual_method(rax, vtable_index, method);
112 slop_delta = 8 - (int)(__ pc() - start_pc);
113 slop_bytes += slop_delta;
114 assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta);
115
116 #ifndef PRODUCT
117 if (DebugVtables) {
118 Label L;
119 __ cmpptr(method, NULL_WORD);
120 __ jcc(Assembler::equal, L);
121 __ cmpptr(Address(method, entry_offset), NULL_WORD);
122 __ jcc(Assembler::notZero, L);
123 __ stop("Vtable entry is null");
124 __ bind(L);
125 }
126 #endif // PRODUCT
127
128 // rax: receiver klass
129 // method (rbx): Method*
130 // rcx: receiver
131 address ame_addr = __ pc();
132 __ jmp( Address(rbx, entry_offset));
133
134 masm->flush();
135 slop_bytes += index_dependent_slop; // add'l slop for size variance due to large itable offsets
136 bookkeeping(masm, tty, s, npe_addr, ame_addr, true, vtable_index, slop_bytes, index_dependent_slop);
137
138 return s;
139 }
140
141
142 VtableStub* VtableStubs::create_itable_stub(int itable_index, bool caller_is_c1) {
143 // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing.
144 const int stub_code_length = code_size_limit(false);
145 ByteSize entry_offset = caller_is_c1 ? Method::from_compiled_inline_offset() : Method::from_compiled_inline_ro_offset();
146 VtableStub* s = new(stub_code_length) VtableStub(false, itable_index, caller_is_c1);
147 // Can be nullptr if there is no free space in the code cache.
148 if (s == nullptr) {
149 return nullptr;
150 }
151
152 // Count unused bytes in instruction sequences of variable size.
153 // We add them to the computed buffer size in order to avoid
154 // overflow in subsequently generated stubs.
155 address start_pc;
156 int slop_bytes = 0;
157 int slop_delta = 0;
158 const int index_dependent_slop = (itable_index == 0) ? 4 : // code size change with transition from 8-bit to 32-bit constant (@index == 16).
159 (itable_index < 16) ? 3 : 0; // index == 0 generates even shorter code.
160
161 ResourceMark rm;
162 CodeBuffer cb(s->entry_point(), stub_code_length);
163 MacroAssembler *masm = new MacroAssembler(&cb);
164
165 #if (!defined(PRODUCT) && defined(COMPILER2))
166 if (CountCompiledCalls) {
167 __ incrementq(ExternalAddress(SharedRuntime::nof_megamorphic_calls_addr()), rscratch1);
194
195 start_pc = __ pc();
196
197 // Receiver subtype check against REFC.
198 // Get selected method from declaring class and itable index
199 __ lookup_interface_method_stub(recv_klass_reg, // input
200 holder_klass_reg, // input
201 resolved_klass_reg, // input
202 method, // output
203 temp_reg,
204 temp_reg2,
205 noreg,
206 itable_index,
207 L_no_such_interface);
208
209 const ptrdiff_t lookupSize = __ pc() - start_pc;
210
211 // We expect we need index_dependent_slop extra bytes. Reason:
212 // The emitted code in lookup_interface_method changes when itable_index exceeds 15.
213 // For linux, a very narrow estimate would be 112, but Solaris requires some more space (130).
214 const ptrdiff_t estimate = 144;
215 const ptrdiff_t codesize = lookupSize + index_dependent_slop;
216 slop_delta = (int)(estimate - codesize);
217 slop_bytes += slop_delta;
218 assert(slop_delta >= 0, "itable #%d: Code size estimate (%d) for lookup_interface_method too small, required: %d", itable_index, (int)estimate, (int)codesize);
219
220 // If we take a trap while this arg is on the stack we will not
221 // be able to walk the stack properly. This is not an issue except
222 // when there are mistakes in this assembly code that could generate
223 // a spurious fault. Ask me how I know...
224
225 // method (rbx): Method*
226 // j_rarg0: receiver
227
228 #ifdef ASSERT
229 if (DebugVtables) {
230 Label L2;
231 __ cmpptr(method, NULL_WORD);
232 __ jcc(Assembler::equal, L2);
233 __ cmpptr(Address(method, entry_offset), NULL_WORD);
234 __ jcc(Assembler::notZero, L2);
235 __ stop("compiler entrypoint is null");
236 __ bind(L2);
237 }
238 #endif // ASSERT
239
240 address ame_addr = __ pc();
241 __ jmp(Address(method, entry_offset));
242
243 __ bind(L_no_such_interface);
244 // Handle IncompatibleClassChangeError in itable stubs.
245 // More detailed error message.
246 // We force resolving of the call site by jumping to the "handle
247 // wrong method" stub, and so let the interpreter runtime do all the
248 // dirty work.
249 __ jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub()));
250
251 masm->flush();
252 slop_bytes += index_dependent_slop; // add'l slop for size variance due to large itable offsets
253 bookkeeping(masm, tty, s, npe_addr, ame_addr, false, itable_index, slop_bytes, index_dependent_slop);
254
255 return s;
256 }
257
258 int VtableStub::pd_code_alignment() {
259 // x86 cache line size is 64 bytes, but we want to limit alignment loss.
260 const unsigned int icache_line_size = wordSize;
261 return icache_line_size;
|