1 /*
  2  * Copyright (c) 2008, 2020, 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/macroAssembler.inline.hpp"
 27 #include "c1/c1_CodeStubs.hpp"
 28 #include "c1/c1_FrameMap.hpp"
 29 #include "c1/c1_LIRAssembler.hpp"
 30 #include "c1/c1_MacroAssembler.hpp"
 31 #include "c1/c1_Runtime1.hpp"
 32 #include "classfile/javaClasses.hpp"
 33 #include "memory/universe.hpp"
 34 #include "nativeInst_arm.hpp"
 35 #include "runtime/sharedRuntime.hpp"
 36 #include "utilities/macros.hpp"
 37 #include "vmreg_arm.inline.hpp"
 38 
 39 #define __ ce->masm()->
 40 
 41 void C1SafepointPollStub::emit_code(LIR_Assembler* ce) {
 42   ShouldNotReachHere();
 43 }
 44 
 45 void CounterOverflowStub::emit_code(LIR_Assembler* ce) {
 46   __ bind(_entry);
 47   ce->store_parameter(_bci, 0);
 48   ce->store_parameter(_method->as_constant_ptr()->as_metadata(), 1);
 49   __ call(Runtime1::entry_for(Runtime1::counter_overflow_id), relocInfo::runtime_call_type);
 50   ce->add_call_info_here(_info);
 51   ce->verify_oop_map(_info);
 52 
 53   __ b(_continuation);
 54 }
 55 
 56 
 57 // TODO: ARM - is it possible to inline these stubs into the main code stream?
 58 
 59 
 60 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index, LIR_Opr array)
 61   : _index(index), _array(array), _throw_index_out_of_bounds_exception(false) {
 62   assert(info != NULL, "must have info");
 63   _info = new CodeEmitInfo(info);
 64 }
 65 
 66 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index)
 67   : _index(index), _array(), _throw_index_out_of_bounds_exception(true) {
 68   assert(info != NULL, "must have info");
 69   _info = new CodeEmitInfo(info);
 70 }
 71 
 72 void RangeCheckStub::emit_code(LIR_Assembler* ce) {
 73   __ bind(_entry);
 74 
 75   if (_info->deoptimize_on_exception()) {
 76     __ call(Runtime1::entry_for(Runtime1::predicate_failed_trap_id), relocInfo::runtime_call_type);
 77     ce->add_call_info_here(_info);
 78     ce->verify_oop_map(_info);
 79     debug_only(__ should_not_reach_here());
 80     return;
 81   }
 82   // Pass the array index on stack because all registers must be preserved
 83   ce->verify_reserved_argument_area_size(_throw_index_out_of_bounds_exception ? 1 : 2);
 84   if (_index->is_cpu_register()) {
 85     __ str_32(_index->as_register(), Address(SP));
 86   } else {
 87     __ mov_slow(Rtemp, _index->as_jint()); // Rtemp should be OK in C1
 88     __ str_32(Rtemp, Address(SP));
 89   }
 90 
 91   if (_throw_index_out_of_bounds_exception) {
 92     __ call(Runtime1::entry_for(Runtime1::throw_index_exception_id), relocInfo::runtime_call_type);
 93   } else {
 94     __ str(_array->as_pointer_register(), Address(SP, BytesPerWord)); // ??? Correct offset? Correct instruction?
 95     __ call(Runtime1::entry_for(Runtime1::throw_range_check_failed_id), relocInfo::runtime_call_type);
 96   }
 97   ce->add_call_info_here(_info);
 98   ce->verify_oop_map(_info);
 99   DEBUG_ONLY(STOP("RangeCheck");)
100 }
101 
102 PredicateFailedStub::PredicateFailedStub(CodeEmitInfo* info) {
103   _info = new CodeEmitInfo(info);
104 }
105 
106 void PredicateFailedStub::emit_code(LIR_Assembler* ce) {
107   __ bind(_entry);
108   __ call(Runtime1::entry_for(Runtime1::predicate_failed_trap_id), relocInfo::runtime_call_type);
109   ce->add_call_info_here(_info);
110   ce->verify_oop_map(_info);
111   debug_only(__ should_not_reach_here());
112 }
113 
114 void DivByZeroStub::emit_code(LIR_Assembler* ce) {
115   if (_offset != -1) {
116     ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
117   }
118   __ bind(_entry);
119   __ call(Runtime1::entry_for(Runtime1::throw_div0_exception_id),
120           relocInfo::runtime_call_type);
121   ce->add_call_info_here(_info);
122   DEBUG_ONLY(STOP("DivByZero");)
123 }
124 
125 
126 // Implementation of NewInstanceStub
127 
128 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, Runtime1::StubID stub_id) {
129   _result = result;
130   _klass = klass;
131   _klass_reg = klass_reg;
132   _info = new CodeEmitInfo(info);
133   assert(stub_id == Runtime1::new_instance_id                 ||
134          stub_id == Runtime1::fast_new_instance_id            ||
135          stub_id == Runtime1::fast_new_instance_init_check_id,
136          "need new_instance id");
137   _stub_id   = stub_id;
138 }
139 
140 
141 void NewInstanceStub::emit_code(LIR_Assembler* ce) {
142   assert(_result->as_register() == R0, "runtime call setup");
143   assert(_klass_reg->as_register() == R1, "runtime call setup");
144   __ bind(_entry);
145   __ call(Runtime1::entry_for(_stub_id), relocInfo::runtime_call_type);
146   ce->add_call_info_here(_info);
147   ce->verify_oop_map(_info);
148   __ b(_continuation);
149 }
150 
151 
152 // Implementation of NewTypeArrayStub
153 
154 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
155   _klass_reg = klass_reg;
156   _length = length;
157   _result = result;
158   _info = new CodeEmitInfo(info);
159 }
160 
161 
162 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) {
163   assert(_result->as_register() == R0, "runtime call setup");
164   assert(_klass_reg->as_register() == R1, "runtime call setup");
165   assert(_length->as_register() == R2, "runtime call setup");
166   __ bind(_entry);
167   __ call(Runtime1::entry_for(Runtime1::new_type_array_id), relocInfo::runtime_call_type);
168   ce->add_call_info_here(_info);
169   ce->verify_oop_map(_info);
170   __ b(_continuation);
171 }
172 
173 
174 // Implementation of NewObjectArrayStub
175 
176 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
177   _klass_reg = klass_reg;
178   _result = result;
179   _length = length;
180   _info = new CodeEmitInfo(info);
181 }
182 
183 
184 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
185   assert(_result->as_register() == R0, "runtime call setup");
186   assert(_klass_reg->as_register() == R1, "runtime call setup");
187   assert(_length->as_register() == R2, "runtime call setup");
188   __ bind(_entry);
189   __ call(Runtime1::entry_for(Runtime1::new_object_array_id), relocInfo::runtime_call_type);
190   ce->add_call_info_here(_info);
191   ce->verify_oop_map(_info);
192   __ b(_continuation);
193 }
194 
195 
196 // Implementation of MonitorAccessStubs
197 
198 MonitorEnterStub::MonitorEnterStub(LIR_Opr obj_reg, LIR_Opr lock_reg, CodeEmitInfo* info)
199 : MonitorAccessStub(obj_reg, lock_reg)
200 {
201   _info = new CodeEmitInfo(info);
202 }
203 
204 
205 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
206   __ bind(_entry);
207   const Register obj_reg = _obj_reg->as_pointer_register();
208   const Register lock_reg = _lock_reg->as_pointer_register();
209 
210   ce->verify_reserved_argument_area_size(2);
211   if (obj_reg < lock_reg) {
212     __ stmia(SP, RegisterSet(obj_reg) | RegisterSet(lock_reg));
213   } else {
214     __ str(obj_reg, Address(SP));
215     __ str(lock_reg, Address(SP, BytesPerWord));
216   }
217 
218   Runtime1::StubID enter_id = ce->compilation()->has_fpu_code() ?
219                               Runtime1::monitorenter_id :
220                               Runtime1::monitorenter_nofpu_id;
221   __ call(Runtime1::entry_for(enter_id), relocInfo::runtime_call_type);
222   ce->add_call_info_here(_info);
223   ce->verify_oop_map(_info);
224   __ b(_continuation);
225 }
226 
227 
228 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
229   __ bind(_entry);
230   if (_compute_lock) {
231     ce->monitor_address(_monitor_ix, _lock_reg);
232   }
233   const Register lock_reg = _lock_reg->as_pointer_register();
234 
235   ce->verify_reserved_argument_area_size(1);
236   __ str(lock_reg, Address(SP));
237 
238   // Non-blocking leaf routine - no call info needed
239   Runtime1::StubID exit_id = ce->compilation()->has_fpu_code() ?
240                              Runtime1::monitorexit_id :
241                              Runtime1::monitorexit_nofpu_id;
242   __ call(Runtime1::entry_for(exit_id), relocInfo::runtime_call_type);
243   __ b(_continuation);
244 }
245 
246 void LoadKlassStub::emit_code(LIR_Assembler* ce) {
247   // Currently not needed.
248   Unimplemented();
249 }
250 
251 // Call return is directly after patch word
252 int PatchingStub::_patch_info_offset = 0;
253 
254 void PatchingStub::align_patch_site(MacroAssembler* masm) {
255 #if 0
256   // TODO: investigate if we required to implement this
257     ShouldNotReachHere();
258 #endif
259 }
260 
261 void PatchingStub::emit_code(LIR_Assembler* ce) {
262   const int patchable_instruction_offset = 0;
263 
264   assert(NativeCall::instruction_size <= _bytes_to_copy && _bytes_to_copy <= 0xFF,
265          "not enough room for call");
266   assert((_bytes_to_copy & 3) == 0, "must copy a multiple of four bytes");
267   Label call_patch;
268   bool is_load = (_id == load_klass_id) || (_id == load_mirror_id) || (_id == load_appendix_id);
269 
270 
271   if (is_load && !VM_Version::supports_movw()) {
272     address start = __ pc();
273 
274     // The following sequence duplicates code provided in MacroAssembler::patchable_mov_oop()
275     // without creating relocation info entry.
276 
277     assert((__ pc() - start) == patchable_instruction_offset, "should be");
278     __ ldr(_obj, Address(PC));
279     // Extra nop to handle case of large offset of oop placeholder (see NativeMovConstReg::set_data).
280     __ nop();
281 
282 #ifdef ASSERT
283     for (int i = 0; i < _bytes_to_copy; i++) {
284       assert(((address)_pc_start)[i] == start[i], "should be the same code");
285     }
286 #endif // ASSERT
287   }
288 
289   address being_initialized_entry = __ pc();
290   if (CommentedAssembly) {
291     __ block_comment(" patch template");
292   }
293   if (is_load) {
294     address start = __ pc();
295     if (_id == load_mirror_id || _id == load_appendix_id) {
296       __ patchable_mov_oop(_obj, (jobject)Universe::non_oop_word(), _index);
297     } else {
298       __ patchable_mov_metadata(_obj, (Metadata*)Universe::non_oop_word(), _index);
299     }
300 #ifdef ASSERT
301     for (int i = 0; i < _bytes_to_copy; i++) {
302       assert(((address)_pc_start)[i] == start[i], "should be the same code");
303     }
304 #endif // ASSERT
305   } else {
306     int* start = (int*)_pc_start;
307     int* end = start + (_bytes_to_copy / BytesPerInt);
308     while (start < end) {
309       __ emit_int32(*start++);
310     }
311   }
312   address end_of_patch = __ pc();
313 
314   int bytes_to_skip = 0;
315   if (_id == load_mirror_id) {
316     int offset = __ offset();
317     if (CommentedAssembly) {
318       __ block_comment(" being_initialized check");
319     }
320 
321     assert(_obj != noreg, "must be a valid register");
322     // Rtemp should be OK in C1
323     __ ldr(Rtemp, Address(_obj, java_lang_Class::klass_offset()));
324     __ ldr(Rtemp, Address(Rtemp, InstanceKlass::init_thread_offset()));
325     __ cmp(Rtemp, Rthread);
326     __ b(call_patch, ne);
327     __ b(_patch_site_continuation);
328 
329     bytes_to_skip += __ offset() - offset;
330   }
331 
332   if (CommentedAssembly) {
333     __ block_comment("patch data - 3 high bytes of the word");
334   }
335   const int sizeof_patch_record = 4;
336   bytes_to_skip += sizeof_patch_record;
337   int being_initialized_entry_offset = __ pc() - being_initialized_entry + sizeof_patch_record;
338   __ emit_int32(0xff | being_initialized_entry_offset << 8 | bytes_to_skip << 16 | _bytes_to_copy << 24);
339 
340   address patch_info_pc = __ pc();
341   assert(patch_info_pc - end_of_patch == bytes_to_skip, "incorrect patch info");
342 
343   // runtime call will return here
344   Label call_return;
345   __ bind(call_return);
346   ce->add_call_info_here(_info);
347   assert(_patch_info_offset == (patch_info_pc - __ pc()), "must not change");
348   __ b(_patch_site_entry);
349 
350   address entry = __ pc();
351   NativeGeneralJump::insert_unconditional((address)_pc_start, entry);
352   address target = NULL;
353   relocInfo::relocType reloc_type = relocInfo::none;
354   switch (_id) {
355     case access_field_id:  target = Runtime1::entry_for(Runtime1::access_field_patching_id); break;
356     case load_klass_id:    target = Runtime1::entry_for(Runtime1::load_klass_patching_id); reloc_type = relocInfo::metadata_type; break;
357     case load_mirror_id:   target = Runtime1::entry_for(Runtime1::load_mirror_patching_id); reloc_type = relocInfo::oop_type; break;
358     case load_appendix_id: target = Runtime1::entry_for(Runtime1::load_appendix_patching_id); reloc_type = relocInfo::oop_type; break;
359     default: ShouldNotReachHere();
360   }
361   __ bind(call_patch);
362 
363   if (CommentedAssembly) {
364     __ block_comment("patch entry point");
365   }
366 
367   // arrange for call to return just after patch word
368   __ adr(LR, call_return);
369   __ jump(target, relocInfo::runtime_call_type, Rtemp);
370 
371   if (is_load) {
372     CodeSection* cs = __ code_section();
373     address pc = (address)_pc_start;
374     RelocIterator iter(cs, pc, pc + 1);
375     relocInfo::change_reloc_info_for_address(&iter, pc, reloc_type, relocInfo::none);
376   }
377 }
378 
379 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
380   __ bind(_entry);
381   __ mov_slow(Rtemp, _trap_request);
382   ce->verify_reserved_argument_area_size(1);
383   __ str(Rtemp, Address(SP));
384   __ call(Runtime1::entry_for(Runtime1::deoptimize_id), relocInfo::runtime_call_type);
385   ce->add_call_info_here(_info);
386   DEBUG_ONLY(__ should_not_reach_here());
387 }
388 
389 
390 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
391   address a;
392   if (_info->deoptimize_on_exception()) {
393     // Deoptimize, do not throw the exception, because it is
394     // probably wrong to do it here.
395     a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
396   } else {
397     a = Runtime1::entry_for(Runtime1::throw_null_pointer_exception_id);
398   }
399   ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
400   __ bind(_entry);
401   __ call(a, relocInfo::runtime_call_type);
402   ce->add_call_info_here(_info);
403   ce->verify_oop_map(_info);
404   DEBUG_ONLY(STOP("ImplicitNullCheck");)
405 }
406 
407 
408 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
409   __ bind(_entry);
410   // Pass the object on stack because all registers must be preserved
411   if (_obj->is_cpu_register()) {
412     ce->verify_reserved_argument_area_size(1);
413     __ str(_obj->as_pointer_register(), Address(SP));
414   } else {
415     assert(_obj->is_illegal(), "should be");
416   }
417   __ call(Runtime1::entry_for(_stub), relocInfo::runtime_call_type);
418   ce->add_call_info_here(_info);
419   DEBUG_ONLY(STOP("SimpleException");)
420 }
421 
422 
423 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
424   __ bind(_entry);
425 
426   VMRegPair args[5];
427   BasicType signature[5] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT };
428   SharedRuntime::java_calling_convention(signature, args, 5);
429 
430   Register r[5];
431   r[0] = src()->as_pointer_register();
432   r[1] = src_pos()->as_register();
433   r[2] = dst()->as_pointer_register();
434   r[3] = dst_pos()->as_register();
435   r[4] = length()->as_register();
436 
437   for (int i = 0; i < 5; i++) {
438     VMReg arg = args[i].first();
439     if (arg->is_stack()) {
440       __ str(r[i], Address(SP, arg->reg2stack() * VMRegImpl::stack_slot_size));
441     } else {
442       assert(r[i] == arg->as_Register(), "Calling conventions must match");
443     }
444   }
445 
446   ce->emit_static_call_stub();
447   if (ce->compilation()->bailed_out()) {
448     return; // CodeCache is full
449   }
450   int ret_addr_offset = __ patchable_call(SharedRuntime::get_resolve_static_call_stub(), relocInfo::static_call_type);
451   assert(ret_addr_offset == __ offset(), "embedded return address not allowed");
452   ce->add_call_info_here(info());
453   ce->verify_oop_map(info());
454   __ b(_continuation);
455 }
456 
457 #undef __