1 /*
  2  * Copyright (c) 1999, 2020, 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.inline.hpp"
 28 #include "c1/c1_CodeStubs.hpp"
 29 #include "c1/c1_FrameMap.hpp"
 30 #include "c1/c1_LIRAssembler.hpp"
 31 #include "c1/c1_MacroAssembler.hpp"
 32 #include "c1/c1_Runtime1.hpp"
 33 #include "classfile/javaClasses.hpp"
 34 #include "nativeInst_aarch64.hpp"
 35 #include "runtime/sharedRuntime.hpp"
 36 #include "vmreg_aarch64.inline.hpp"
 37 
 38 
 39 #define __ ce->masm()->
 40 
 41 void CounterOverflowStub::emit_code(LIR_Assembler* ce) {
 42   __ bind(_entry);
 43   Metadata *m = _method->as_constant_ptr()->as_metadata();
 44   __ mov_metadata(rscratch1, m);
 45   ce->store_parameter(rscratch1, 1);
 46   ce->store_parameter(_bci, 0);
 47   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::counter_overflow_id)));
 48   ce->add_call_info_here(_info);
 49   ce->verify_oop_map(_info);
 50   __ b(_continuation);
 51 }
 52 
 53 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index, LIR_Opr array)
 54   : _index(index), _array(array), _throw_index_out_of_bounds_exception(false) {
 55   assert(info != NULL, "must have info");
 56   _info = new CodeEmitInfo(info);
 57 }
 58 
 59 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index)
 60   : _index(index), _array(NULL), _throw_index_out_of_bounds_exception(true) {
 61   assert(info != NULL, "must have info");
 62   _info = new CodeEmitInfo(info);
 63 }
 64 
 65 void RangeCheckStub::emit_code(LIR_Assembler* ce) {
 66   __ bind(_entry);
 67   if (_info->deoptimize_on_exception()) {
 68     address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
 69     __ far_call(RuntimeAddress(a));
 70     ce->add_call_info_here(_info);
 71     ce->verify_oop_map(_info);
 72     debug_only(__ should_not_reach_here());
 73     return;
 74   }
 75 
 76   if (_index->is_cpu_register()) {
 77     __ mov(rscratch1, _index->as_register());
 78   } else {
 79     __ mov(rscratch1, _index->as_jint());
 80   }
 81   Runtime1::StubID stub_id;
 82   if (_throw_index_out_of_bounds_exception) {
 83     stub_id = Runtime1::throw_index_exception_id;
 84   } else {
 85     assert(_array != NULL, "sanity");
 86     __ mov(rscratch2, _array->as_pointer_register());
 87     stub_id = Runtime1::throw_range_check_failed_id;
 88   }
 89   __ lea(lr, RuntimeAddress(Runtime1::entry_for(stub_id)));
 90   __ blr(lr);
 91   ce->add_call_info_here(_info);
 92   ce->verify_oop_map(_info);
 93   debug_only(__ should_not_reach_here());
 94 }
 95 
 96 PredicateFailedStub::PredicateFailedStub(CodeEmitInfo* info) {
 97   _info = new CodeEmitInfo(info);
 98 }
 99 
100 void PredicateFailedStub::emit_code(LIR_Assembler* ce) {
101   __ bind(_entry);
102   address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
103   __ far_call(RuntimeAddress(a));
104   ce->add_call_info_here(_info);
105   ce->verify_oop_map(_info);
106   debug_only(__ should_not_reach_here());
107 }
108 
109 void DivByZeroStub::emit_code(LIR_Assembler* ce) {
110   if (_offset != -1) {
111     ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
112   }
113   __ bind(_entry);
114   __ far_call(Address(Runtime1::entry_for(Runtime1::throw_div0_exception_id), relocInfo::runtime_call_type));
115   ce->add_call_info_here(_info);
116   ce->verify_oop_map(_info);
117 #ifdef ASSERT
118   __ should_not_reach_here();
119 #endif
120 }
121 
122 // Implementation of LoadFlattenedArrayStub
123 
124 LoadFlattenedArrayStub::LoadFlattenedArrayStub(LIR_Opr array, LIR_Opr index, LIR_Opr result, CodeEmitInfo* info) {
125   _array = array;
126   _index = index;
127   _result = result;
128   _scratch_reg = FrameMap::r0_oop_opr;
129   _info = new CodeEmitInfo(info);
130 }
131 
132 void LoadFlattenedArrayStub::emit_code(LIR_Assembler* ce) {
133   assert(__ rsp_offset() == 0, "frame size should be fixed");
134   __ bind(_entry);
135   ce->store_parameter(_array->as_register(), 1);
136   ce->store_parameter(_index->as_register(), 0);
137   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::load_flattened_array_id)));
138   ce->add_call_info_here(_info);
139   ce->verify_oop_map(_info);
140   if (_result->as_register() != r0) {
141     __ mov(_result->as_register(), r0);
142   }
143   __ b(_continuation);
144 }
145 
146 
147 // Implementation of StoreFlattenedArrayStub
148 
149 StoreFlattenedArrayStub::StoreFlattenedArrayStub(LIR_Opr array, LIR_Opr index, LIR_Opr value, CodeEmitInfo* info) {
150   _array = array;
151   _index = index;
152   _value = value;
153   _scratch_reg = FrameMap::r0_oop_opr;
154   _info = new CodeEmitInfo(info);
155 }
156 
157 
158 void StoreFlattenedArrayStub::emit_code(LIR_Assembler* ce) {
159   assert(__ rsp_offset() == 0, "frame size should be fixed");
160   __ bind(_entry);
161   ce->store_parameter(_array->as_register(), 2);
162   ce->store_parameter(_index->as_register(), 1);
163   ce->store_parameter(_value->as_register(), 0);
164   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::store_flattened_array_id)));
165   ce->add_call_info_here(_info);
166   ce->verify_oop_map(_info);
167   __ b(_continuation);
168 }
169 
170 // Implementation of SubstitutabilityCheckStub
171 SubstitutabilityCheckStub::SubstitutabilityCheckStub(LIR_Opr left, LIR_Opr right, CodeEmitInfo* info) {
172   _left = left;
173   _right = right;
174   _scratch_reg = FrameMap::r0_oop_opr;
175   _info = new CodeEmitInfo(info);
176 }
177 
178 void SubstitutabilityCheckStub::emit_code(LIR_Assembler* ce) {
179   assert(__ rsp_offset() == 0, "frame size should be fixed");
180   __ bind(_entry);
181   ce->store_parameter(_left->as_register(), 1);
182   ce->store_parameter(_right->as_register(), 0);
183   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::substitutability_check_id)));
184   ce->add_call_info_here(_info);
185   ce->verify_oop_map(_info);
186   __ b(_continuation);
187 }
188 
189 
190 // Implementation of NewInstanceStub
191 
192 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, Runtime1::StubID stub_id) {
193   _result = result;
194   _klass = klass;
195   _klass_reg = klass_reg;
196   _info = new CodeEmitInfo(info);
197   assert(stub_id == Runtime1::new_instance_id                 ||
198          stub_id == Runtime1::fast_new_instance_id            ||
199          stub_id == Runtime1::fast_new_instance_init_check_id,
200          "need new_instance id");
201   _stub_id   = stub_id;
202 }
203 
204 void NewInstanceStub::emit_code(LIR_Assembler* ce) {
205   assert(__ rsp_offset() == 0, "frame size should be fixed");
206   __ bind(_entry);
207   __ mov(r3, _klass_reg->as_register());
208   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub_id)));
209   ce->add_call_info_here(_info);
210   ce->verify_oop_map(_info);
211   assert(_result->as_register() == r0, "result must in r0,");
212   __ b(_continuation);
213 }
214 
215 
216 // Implementation of NewTypeArrayStub
217 
218 // Implementation of NewTypeArrayStub
219 
220 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
221   _klass_reg = klass_reg;
222   _length = length;
223   _result = result;
224   _info = new CodeEmitInfo(info);
225 }
226 
227 
228 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) {
229   assert(__ rsp_offset() == 0, "frame size should be fixed");
230   __ bind(_entry);
231   assert(_length->as_register() == r19, "length must in r19,");
232   assert(_klass_reg->as_register() == r3, "klass_reg must in r3");
233   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_type_array_id)));
234   ce->add_call_info_here(_info);
235   ce->verify_oop_map(_info);
236   assert(_result->as_register() == r0, "result must in r0");
237   __ b(_continuation);
238 }
239 
240 
241 // Implementation of NewObjectArrayStub
242 
243 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info, bool is_inline_type) {
244   _klass_reg = klass_reg;
245   _result = result;
246   _length = length;
247   _info = new CodeEmitInfo(info);
248   _is_inline_type = is_inline_type;
249 }
250 
251 
252 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
253   assert(__ rsp_offset() == 0, "frame size should be fixed");
254   __ bind(_entry);
255   assert(_length->as_register() == r19, "length must in r19,");
256   assert(_klass_reg->as_register() == r3, "klass_reg must in r3");
257 
258   if (_is_inline_type) {
259     __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_flat_array_id)));
260   } else {
261     __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_object_array_id)));
262   }
263 
264   ce->add_call_info_here(_info);
265   ce->verify_oop_map(_info);
266   assert(_result->as_register() == r0, "result must in r0");
267   __ b(_continuation);
268 }
269 // Implementation of MonitorAccessStubs
270 
271 MonitorEnterStub::MonitorEnterStub(LIR_Opr obj_reg, LIR_Opr lock_reg, CodeEmitInfo* info,  CodeStub* throw_imse_stub, LIR_Opr scratch_reg)
272 : MonitorAccessStub(obj_reg, lock_reg)
273 {
274   _info = new CodeEmitInfo(info);
275   _scratch_reg = scratch_reg;
276   _throw_imse_stub = throw_imse_stub;
277   if (_throw_imse_stub != NULL) {
278     assert(_scratch_reg != LIR_OprFact::illegalOpr, "must be");
279   }
280 }
281 
282 
283 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
284   assert(__ rsp_offset() == 0, "frame size should be fixed");
285   __ bind(_entry);
286   if (_throw_imse_stub != NULL) {
287     // When we come here, _obj_reg has already been checked to be non-null.
288     __ ldr(rscratch1, Address(_obj_reg->as_register(), oopDesc::mark_offset_in_bytes()));
289     __ mov(rscratch2, markWord::always_locked_pattern);
290     __ andr(rscratch1, rscratch1, rscratch2);
291 
292     __ cmp(rscratch1, rscratch2);
293     __ br(Assembler::NE, *_throw_imse_stub->entry());
294   }
295 
296   ce->store_parameter(_obj_reg->as_register(),  1);
297   ce->store_parameter(_lock_reg->as_register(), 0);
298   Runtime1::StubID enter_id;
299   if (ce->compilation()->has_fpu_code()) {
300     enter_id = Runtime1::monitorenter_id;
301   } else {
302     enter_id = Runtime1::monitorenter_nofpu_id;
303   }
304   __ far_call(RuntimeAddress(Runtime1::entry_for(enter_id)));
305   ce->add_call_info_here(_info);
306   ce->verify_oop_map(_info);
307   __ b(_continuation);
308 }
309 
310 
311 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
312   __ bind(_entry);
313   if (_compute_lock) {
314     // lock_reg was destroyed by fast unlocking attempt => recompute it
315     ce->monitor_address(_monitor_ix, _lock_reg);
316   }
317   ce->store_parameter(_lock_reg->as_register(), 0);
318   // note: non-blocking leaf routine => no call info needed
319   Runtime1::StubID exit_id;
320   if (ce->compilation()->has_fpu_code()) {
321     exit_id = Runtime1::monitorexit_id;
322   } else {
323     exit_id = Runtime1::monitorexit_nofpu_id;
324   }
325   __ adr(lr, _continuation);
326   __ far_jump(RuntimeAddress(Runtime1::entry_for(exit_id)));
327 }
328 
329 
330 // Implementation of patching:
331 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes)
332 // - Replace original code with a call to the stub
333 // At Runtime:
334 // - call to stub, jump to runtime
335 // - in runtime: preserve all registers (rspecially objects, i.e., source and destination object)
336 // - in runtime: after initializing class, restore original code, reexecute instruction
337 
338 int PatchingStub::_patch_info_offset = -NativeGeneralJump::instruction_size;
339 
340 void PatchingStub::align_patch_site(MacroAssembler* masm) {
341 }
342 
343 void PatchingStub::emit_code(LIR_Assembler* ce) {
344   assert(false, "AArch64 should not use C1 runtime patching");
345 }
346 
347 
348 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
349   __ bind(_entry);
350   ce->store_parameter(_trap_request, 0);
351   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::deoptimize_id)));
352   ce->add_call_info_here(_info);
353   DEBUG_ONLY(__ should_not_reach_here());
354 }
355 
356 
357 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
358   address a;
359   if (_info->deoptimize_on_exception()) {
360     // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
361     a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
362   } else {
363     a = Runtime1::entry_for(Runtime1::throw_null_pointer_exception_id);
364   }
365 
366   ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
367   __ bind(_entry);
368   __ far_call(RuntimeAddress(a));
369   ce->add_call_info_here(_info);
370   ce->verify_oop_map(_info);
371   debug_only(__ should_not_reach_here());
372 }
373 
374 
375 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
376   assert(__ rsp_offset() == 0, "frame size should be fixed");
377 
378   __ bind(_entry);
379   // pass the object in a scratch register because all other registers
380   // must be preserved
381   if (_obj->is_cpu_register()) {
382     __ mov(rscratch1, _obj->as_register());
383   }
384   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub)), NULL, rscratch2);
385   ce->add_call_info_here(_info);
386   debug_only(__ should_not_reach_here());
387 }
388 
389 
390 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
391   //---------------slow case: call to native-----------------
392   __ bind(_entry);
393   // Figure out where the args should go
394   // This should really convert the IntrinsicID to the Method* and signature
395   // but I don't know how to do that.
396   //
397   VMRegPair args[5];
398   BasicType signature[5] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT};
399   SharedRuntime::java_calling_convention(signature, args, 5, true);
400 
401   // push parameters
402   // (src, src_pos, dest, destPos, length)
403   Register r[5];
404   r[0] = src()->as_register();
405   r[1] = src_pos()->as_register();
406   r[2] = dst()->as_register();
407   r[3] = dst_pos()->as_register();
408   r[4] = length()->as_register();
409 
410   // next registers will get stored on the stack
411   for (int i = 0; i < 5 ; i++ ) {
412     VMReg r_1 = args[i].first();
413     if (r_1->is_stack()) {
414       int st_off = r_1->reg2stack() * wordSize;
415       __ str (r[i], Address(sp, st_off));
416     } else {
417       assert(r[i] == args[i].first()->as_Register(), "Wrong register for arg ");
418     }
419   }
420 
421   ce->align_call(lir_static_call);
422 
423   ce->emit_static_call_stub();
424   if (ce->compilation()->bailed_out()) {
425     return; // CodeCache is full
426   }
427   Address resolve(SharedRuntime::get_resolve_static_call_stub(),
428                   relocInfo::static_call_type);
429   address call = __ trampoline_call(resolve);
430   if (call == NULL) {
431     ce->bailout("trampoline stub overflow");
432     return;
433   }
434   ce->add_call_info_here(info());
435 
436 #ifndef PRODUCT
437   __ lea(rscratch2, ExternalAddress((address)&Runtime1::_arraycopy_slowcase_cnt));
438   __ incrementw(Address(rscratch2));
439 #endif
440 
441   __ b(_continuation);
442 }
443 
444 #undef __