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  * Copyright (c) 2020, 2021, Huawei Technologies Co., Ltd. All rights reserved.
  5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  * This code is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU General Public License version 2 only, as
  9  * published by the Free Software Foundation.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 
 27 #include "precompiled.hpp"
 28 #include "asm/macroAssembler.inline.hpp"
 29 #include "c1/c1_CodeStubs.hpp"
 30 #include "c1/c1_FrameMap.hpp"
 31 #include "c1/c1_LIRAssembler.hpp"
 32 #include "c1/c1_MacroAssembler.hpp"
 33 #include "c1/c1_Runtime1.hpp"
 34 #include "classfile/javaClasses.hpp"
 35 #include "nativeInst_riscv.hpp"
 36 #include "runtime/sharedRuntime.hpp"
 37 #include "vmreg_riscv.inline.hpp"
 38 
 39 
 40 #define __ ce->masm()->
 41 
 42 void C1SafepointPollStub::emit_code(LIR_Assembler* ce)
 43 {
 44   __ bind(_entry);
 45   InternalAddress safepoint_pc(__ pc() - __ offset() + safepoint_offset());
 46   __ code_section()->relocate(__ pc(), safepoint_pc.rspec());
 47   __ la(t0, safepoint_pc.target());
 48   __ sd(t0, Address(xthread, JavaThread::saved_exception_pc_offset()));
 49 
 50   assert(SharedRuntime::polling_page_return_handler_blob() != NULL,
 51          "polling page return stub not created yet");
 52   address stub = SharedRuntime::polling_page_return_handler_blob()->entry_point();
 53 
 54   __ far_jump(RuntimeAddress(stub));
 55 }
 56 
 57 void CounterOverflowStub::emit_code(LIR_Assembler* ce)
 58 {
 59   __ bind(_entry);
 60   Metadata *m = _method->as_constant_ptr()->as_metadata();
 61   __ mov_metadata(t0, m);
 62   ce->store_parameter(t0, 1);
 63   ce->store_parameter(_bci, 0);
 64   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::counter_overflow_id)));
 65   ce->add_call_info_here(_info);
 66   ce->verify_oop_map(_info);
 67   __ j(_continuation);
 68 }
 69 
 70 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index, LIR_Opr array)
 71   : _index(index), _array(array), _throw_index_out_of_bounds_exception(false)
 72 {
 73   assert(info != NULL, "must have info");
 74   _info = new CodeEmitInfo(info);
 75 }
 76 
 77 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index)
 78   : _index(index), _array(NULL), _throw_index_out_of_bounds_exception(true)
 79 {
 80   assert(info != NULL, "must have info");
 81   _info = new CodeEmitInfo(info);
 82 }
 83 
 84 void RangeCheckStub::emit_code(LIR_Assembler* ce)
 85 {
 86   __ bind(_entry);
 87   if (_info->deoptimize_on_exception()) {
 88     address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
 89     __ far_call(RuntimeAddress(a));
 90     ce->add_call_info_here(_info);
 91     ce->verify_oop_map(_info);
 92     debug_only(__ should_not_reach_here());
 93     return;
 94   }
 95 
 96   if (_index->is_cpu_register()) {
 97     __ mv(t0, _index->as_register());
 98   } else {
 99     __ mv(t0, _index->as_jint());
100   }
101   Runtime1::StubID stub_id;
102   if (_throw_index_out_of_bounds_exception) {
103     stub_id = Runtime1::throw_index_exception_id;
104   } else {
105     assert(_array != NULL, "sanity");
106     __ mv(t1, _array->as_pointer_register());
107     stub_id = Runtime1::throw_range_check_failed_id;
108   }
109   int32_t off = 0;
110   __ la_patchable(lr, RuntimeAddress(Runtime1::entry_for(stub_id)), off);
111   __ jalr(lr, lr, off);
112   ce->add_call_info_here(_info);
113   ce->verify_oop_map(_info);
114   debug_only(__ should_not_reach_here());
115 }
116 
117 PredicateFailedStub::PredicateFailedStub(CodeEmitInfo* info)
118 {
119   _info = new CodeEmitInfo(info);
120 }
121 
122 void PredicateFailedStub::emit_code(LIR_Assembler* ce)
123 {
124   __ bind(_entry);
125   address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
126   __ far_call(RuntimeAddress(a));
127   ce->add_call_info_here(_info);
128   ce->verify_oop_map(_info);
129   debug_only(__ should_not_reach_here());
130 }
131 
132 void DivByZeroStub::emit_code(LIR_Assembler* ce)
133 {
134   if (_offset != -1) {
135     ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
136   }
137   __ bind(_entry);
138   __ far_call(Address(Runtime1::entry_for(Runtime1::throw_div0_exception_id), relocInfo::runtime_call_type));
139   ce->add_call_info_here(_info);
140   ce->verify_oop_map(_info);
141 #ifdef ASSERT
142   __ should_not_reach_here();
143 #endif
144 }
145 
146 // Implementation of NewInstanceStub
147 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, Runtime1::StubID stub_id)
148 {
149   _result = result;
150   _klass = klass;
151   _klass_reg = klass_reg;
152   _info = new CodeEmitInfo(info);
153   assert(stub_id == Runtime1::new_instance_id ||
154          stub_id == Runtime1::fast_new_instance_id ||
155          stub_id == Runtime1::fast_new_instance_init_check_id,
156          "need new_instance id");
157   _stub_id = stub_id;
158 }
159 
160 void NewInstanceStub::emit_code(LIR_Assembler* ce)
161 {
162   assert(__ rsp_offset() == 0, "frame size should be fixed");
163   __ bind(_entry);
164   __ mv(x13, _klass_reg->as_register());
165   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub_id)));
166   ce->add_call_info_here(_info);
167   ce->verify_oop_map(_info);
168   assert(_result->as_register() == x10, "result must in x10");
169   __ j(_continuation);
170 }
171 
172 // Implementation of NewTypeArrayStub
173 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info)
174 {
175   _klass_reg = klass_reg;
176   _length = length;
177   _result = result;
178   _info = new CodeEmitInfo(info);
179 }
180 
181 void NewTypeArrayStub::emit_code(LIR_Assembler* ce)
182 {
183   assert(__ rsp_offset() == 0, "frame size should be fixed");
184   __ bind(_entry);
185   assert(_length->as_register() == x9, "length must in x9");
186   assert(_klass_reg->as_register() == x13, "klass_reg must in x13");
187   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_type_array_id)));
188   ce->add_call_info_here(_info);
189   ce->verify_oop_map(_info);
190   assert(_result->as_register() == x10, "result must in x10");
191   __ j(_continuation);
192 }
193 
194 // Implementation of NewObjectArrayStub
195 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info)
196 {
197   _klass_reg = klass_reg;
198   _result = result;
199   _length = length;
200   _info = new CodeEmitInfo(info);
201 }
202 
203 void NewObjectArrayStub::emit_code(LIR_Assembler* ce)
204 {
205   assert(__ rsp_offset() == 0, "frame size should be fixed");
206   __ bind(_entry);
207   assert(_length->as_register() == x9, "length must in x9");
208   assert(_klass_reg->as_register() == x13, "klass_reg must in x13");
209   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_object_array_id)));
210   ce->add_call_info_here(_info);
211   ce->verify_oop_map(_info);
212   assert(_result->as_register() == x10, "result must in x10");
213   __ j(_continuation);
214 }
215 
216 // Implementation of MonitorAccessStubs
217 MonitorEnterStub::MonitorEnterStub(LIR_Opr obj_reg, LIR_Opr lock_reg, CodeEmitInfo* info)
218 : MonitorAccessStub(obj_reg, lock_reg)
219 {
220   _info = new CodeEmitInfo(info);
221 }
222 
223 void MonitorEnterStub::emit_code(LIR_Assembler* ce)
224 {
225   assert(__ rsp_offset() == 0, "frame size should be fixed");
226   __ bind(_entry);
227   ce->store_parameter(_obj_reg->as_register(),  1);
228   ce->store_parameter(_lock_reg->as_register(), 0);
229   Runtime1::StubID enter_id;
230   if (ce->compilation()->has_fpu_code()) {
231     enter_id = Runtime1::monitorenter_id;
232   } else {
233     enter_id = Runtime1::monitorenter_nofpu_id;
234   }
235   __ far_call(RuntimeAddress(Runtime1::entry_for(enter_id)));
236   ce->add_call_info_here(_info);
237   ce->verify_oop_map(_info);
238   __ j(_continuation);
239 }
240 
241 void MonitorExitStub::emit_code(LIR_Assembler* ce)
242 {
243   __ bind(_entry);
244   if (_compute_lock) {
245     // lock_reg was destroyed by fast unlocking attempt => recompute it
246     ce->monitor_address(_monitor_ix, _lock_reg);
247   }
248   ce->store_parameter(_lock_reg->as_register(), 0);
249   // note: non-blocking leaf routine => no call info needed
250   Runtime1::StubID exit_id;
251   if (ce->compilation()->has_fpu_code()) {
252     exit_id = Runtime1::monitorexit_id;
253   } else {
254     exit_id = Runtime1::monitorexit_nofpu_id;
255   }
256   __ la(lr, _continuation);
257   __ far_jump(RuntimeAddress(Runtime1::entry_for(exit_id)));
258 }
259 
260 int PatchingStub::_patch_info_offset = -NativeGeneralJump::instruction_size;
261 
262 void PatchingStub::align_patch_site(MacroAssembler* masm) {}
263 
264 // RISCV64 don't use C1 runtime patching. When need patch, just deoptimize.
265 void PatchingStub::emit_code(LIR_Assembler* ce)
266 {
267   assert(false, "RISCV64 should not use C1 runtime patching");
268 }
269 
270 void DeoptimizeStub::emit_code(LIR_Assembler* ce)
271 {
272   __ bind(_entry);
273   ce->store_parameter(_trap_request, 0);
274   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::deoptimize_id)));
275   ce->add_call_info_here(_info);
276   DEBUG_ONLY(__ should_not_reach_here());
277 }
278 
279 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce)
280 {
281   address a = NULL;
282   if (_info->deoptimize_on_exception()) {
283     // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
284     a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
285   } else {
286     a = Runtime1::entry_for(Runtime1::throw_null_pointer_exception_id);
287   }
288 
289   ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
290   __ bind(_entry);
291   __ far_call(RuntimeAddress(a));
292   ce->add_call_info_here(_info);
293   ce->verify_oop_map(_info);
294   debug_only(__ should_not_reach_here());
295 }
296 
297 void SimpleExceptionStub::emit_code(LIR_Assembler* ce)
298 {
299   assert(__ rsp_offset() == 0, "frame size should be fixed");
300 
301   __ bind(_entry);
302   // pass the object in a tmp register because all other registers
303   // must be preserved
304   if (_obj->is_cpu_register()) {
305     __ mv(t0, _obj->as_register());
306   }
307   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub)), NULL, t1);
308   ce->add_call_info_here(_info);
309   debug_only(__ should_not_reach_here());
310 }
311 
312 void ArrayCopyStub::emit_code(LIR_Assembler* ce)
313 {
314   // ---------------slow case: call to native-----------------
315   __ bind(_entry);
316   // Figure out where the args should go
317   // This should really convert the IntrinsicID to the Method* and signature
318   // but I don't know how to do that.
319   //
320   const int args_num = 5;
321   VMRegPair args[args_num];
322   BasicType signature[args_num] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT };
323   SharedRuntime::java_calling_convention(signature, args, args_num);
324 
325   // push parameters
326   Register r[args_num];
327   int i = 0;
328   r[i++] = src()->as_register();
329   r[i++] = src_pos()->as_register();
330   r[i++] = dst()->as_register();
331   r[i++] = dst_pos()->as_register();
332   r[i++] = length()->as_register();
333 
334   // next registers will get stored on the stack
335   for (int j = 0; j < args_num; j++) {
336     VMReg r_1 = args[j].first();
337     if (r_1->is_stack()) {
338       int st_off = r_1->reg2stack() * wordSize;
339       __ sd(r[j], Address(sp, st_off));
340     } else {
341       assert(r[j] == args[j].first()->as_Register(), "Wrong register for arg ");
342     }
343   }
344 
345   ce->align_call(lir_static_call);
346 
347   ce->emit_static_call_stub();
348   if (ce->compilation()->bailed_out()) {
349     return; // CodeCache is full
350   }
351   Address resolve(SharedRuntime::get_resolve_static_call_stub(),
352                   relocInfo::static_call_type);
353   address call = __ trampoline_call(resolve);
354   if (call == NULL) {
355     ce->bailout("trampoline stub overflow");
356     return;
357   }
358   ce->add_call_info_here(info());
359 
360 #ifndef PRODUCT
361   if (PrintC1Statistics) {
362     __ la(t1, ExternalAddress((address)&Runtime1::_arraycopy_slowcase_cnt));
363     __ add_memory_int32(Address(t1), 1);
364   }
365 #endif
366 
367   __ j(_continuation);
368 }
369 
370 #undef __