1 /*
2 * Copyright (c) 2003, 2025, 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 "asm/assembler.hpp"
26 #include "asm/macroAssembler.hpp"
27 #include "classfile/javaClasses.hpp"
28 #include "classfile/vmIntrinsics.hpp"
29 #include "compiler/oopMap.hpp"
30 #include "gc/shared/barrierSet.hpp"
31 #include "gc/shared/barrierSetAssembler.hpp"
32 #include "gc/shared/barrierSetNMethod.hpp"
33 #include "gc/shared/gc_globals.hpp"
34 #include "memory/universe.hpp"
35 #include "prims/jvmtiExport.hpp"
36 #include "prims/upcallLinker.hpp"
37 #include "runtime/arguments.hpp"
38 #include "runtime/continuationEntry.hpp"
39 #include "runtime/javaThread.hpp"
40 #include "runtime/sharedRuntime.hpp"
41 #include "runtime/stubRoutines.hpp"
42 #include "utilities/macros.hpp"
43 #include "vmreg_x86.inline.hpp"
44 #include "stubGenerator_x86_64.hpp"
45 #ifdef COMPILER2
46 #include "opto/runtime.hpp"
47 #include "opto/c2_globals.hpp"
48 #endif
49 #if INCLUDE_JVMCI
50 #include "jvmci/jvmci_globals.hpp"
51 #endif
52
53 // For a more detailed description of the stub routine structure
54 // see the comment in stubRoutines.hpp
55
56 #define __ _masm->
57 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
58
59 #ifdef PRODUCT
60 #define BLOCK_COMMENT(str) /* nothing */
61 #else
62 #define BLOCK_COMMENT(str) __ block_comment(str)
63 #endif // PRODUCT
64
65 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
66
67 //
68 // Linux Arguments:
69 // c_rarg0: call wrapper address address
70 // c_rarg1: result address
71 // c_rarg2: result type BasicType
72 // c_rarg3: method Method*
73 // c_rarg4: (interpreter) entry point address
74 // c_rarg5: parameters intptr_t*
75 // 16(rbp): parameter size (in words) int
76 // 24(rbp): thread Thread*
77 //
78 // [ return_from_Java ] <--- rsp
79 // [ argument word n ]
80 // ...
81 // -12 [ argument word 1 ]
82 // -11 [ saved r15 ] <--- rsp_after_call
83 // -10 [ saved r14 ]
84 // -9 [ saved r13 ]
85 // -8 [ saved r12 ]
86 // -7 [ saved rbx ]
87 // -6 [ call wrapper ]
88 // -5 [ result ]
89 // -4 [ result type ]
90 // -3 [ method ]
91 // -2 [ entry point ]
92 // -1 [ parameters ]
93 // 0 [ saved rbp ] <--- rbp
94 // 1 [ return address ]
95 // 2 [ parameter size ]
96 // 3 [ thread ]
97 //
98 // Windows Arguments:
99 // c_rarg0: call wrapper address address
100 // c_rarg1: result address
101 // c_rarg2: result type BasicType
102 // c_rarg3: method Method*
103 // 48(rbp): (interpreter) entry point address
104 // 56(rbp): parameters intptr_t*
105 // 64(rbp): parameter size (in words) int
106 // 72(rbp): thread Thread*
107 //
108 // [ return_from_Java ] <--- rsp
109 // [ argument word n ]
110 // ...
111 // -28 [ argument word 1 ]
112 // -27 [ saved xmm15 ] <--- rsp after_call
113 // [ saved xmm7-xmm14 ]
114 // -9 [ saved xmm6 ] (each xmm register takes 2 slots)
115 // -7 [ saved r15 ]
116 // -6 [ saved r14 ]
117 // -5 [ saved r13 ]
118 // -4 [ saved r12 ]
119 // -3 [ saved rdi ]
120 // -2 [ saved rsi ]
121 // -1 [ saved rbx ]
122 // 0 [ saved rbp ] <--- rbp
123 // 1 [ return address ]
124 // 2 [ call wrapper ]
125 // 3 [ result ]
126 // 4 [ result type ]
127 // 5 [ method ]
128 // 6 [ entry point ]
129 // 7 [ parameters ]
130 // 8 [ parameter size ]
131 // 9 [ thread ]
132 //
133 // Windows reserves the callers stack space for arguments 1-4.
134 // We spill c_rarg0-c_rarg3 to this space.
135
136 // Call stub stack layout word offsets from rbp
137 #ifdef _WIN64
138 enum call_stub_layout {
139 xmm_save_first = 6, // save from xmm6
140 xmm_save_last = 15, // to xmm15
141 xmm_save_base = -9,
142 rsp_after_call_off = xmm_save_base - 2 * (xmm_save_last - xmm_save_first), // -27
143 r15_off = -7,
144 r14_off = -6,
145 r13_off = -5,
146 r12_off = -4,
147 rdi_off = -3,
148 rsi_off = -2,
149 rbx_off = -1,
150 rbp_off = 0,
151 retaddr_off = 1,
152 call_wrapper_off = 2,
153 result_off = 3,
154 result_type_off = 4,
155 method_off = 5,
156 entry_point_off = 6,
157 parameters_off = 7,
158 parameter_size_off = 8,
159 thread_off = 9
160 };
161
162 static Address xmm_save(int reg) {
163 assert(reg >= xmm_save_first && reg <= xmm_save_last, "XMM register number out of range");
164 return Address(rbp, (xmm_save_base - (reg - xmm_save_first) * 2) * wordSize);
165 }
166 #else // !_WIN64
167 enum call_stub_layout {
168 rsp_after_call_off = -12,
169 mxcsr_off = rsp_after_call_off,
170 r15_off = -11,
171 r14_off = -10,
172 r13_off = -9,
173 r12_off = -8,
174 rbx_off = -7,
175 call_wrapper_off = -6,
176 result_off = -5,
177 result_type_off = -4,
178 method_off = -3,
179 entry_point_off = -2,
180 parameters_off = -1,
181 rbp_off = 0,
182 retaddr_off = 1,
183 parameter_size_off = 2,
184 thread_off = 3
185 };
186 #endif // _WIN64
187
188 address StubGenerator::generate_call_stub(address& return_address) {
189
190 assert((int)frame::entry_frame_after_call_words == -(int)rsp_after_call_off + 1 &&
191 (int)frame::entry_frame_call_wrapper_offset == (int)call_wrapper_off,
192 "adjust this code");
193 StubId stub_id = StubId::stubgen_call_stub_id;
194 StubCodeMark mark(this, stub_id);
195 address start = __ pc();
196
197 // same as in generate_catch_exception()!
198 const Address rsp_after_call(rbp, rsp_after_call_off * wordSize);
199
200 const Address call_wrapper (rbp, call_wrapper_off * wordSize);
201 const Address result (rbp, result_off * wordSize);
202 const Address result_type (rbp, result_type_off * wordSize);
203 const Address method (rbp, method_off * wordSize);
204 const Address entry_point (rbp, entry_point_off * wordSize);
205 const Address parameters (rbp, parameters_off * wordSize);
206 const Address parameter_size(rbp, parameter_size_off * wordSize);
207
208 // same as in generate_catch_exception()!
209 const Address thread (rbp, thread_off * wordSize);
210
211 const Address r15_save(rbp, r15_off * wordSize);
212 const Address r14_save(rbp, r14_off * wordSize);
213 const Address r13_save(rbp, r13_off * wordSize);
214 const Address r12_save(rbp, r12_off * wordSize);
215 const Address rbx_save(rbp, rbx_off * wordSize);
216
217 // stub code
218 __ enter();
219 __ subptr(rsp, -rsp_after_call_off * wordSize);
220
221 // save register parameters
222 #ifndef _WIN64
223 __ movptr(parameters, c_rarg5); // parameters
224 __ movptr(entry_point, c_rarg4); // entry_point
225 #endif
226
227 __ movptr(method, c_rarg3); // method
228 __ movl(result_type, c_rarg2); // result type
229 __ movptr(result, c_rarg1); // result
230 __ movptr(call_wrapper, c_rarg0); // call wrapper
231
232 // save regs belonging to calling function
233 __ movptr(rbx_save, rbx);
234 __ movptr(r12_save, r12);
235 __ movptr(r13_save, r13);
236 __ movptr(r14_save, r14);
237 __ movptr(r15_save, r15);
238
239 #ifdef _WIN64
240 int last_reg = 15;
241 for (int i = xmm_save_first; i <= last_reg; i++) {
242 __ movdqu(xmm_save(i), as_XMMRegister(i));
243 }
244
245 const Address rdi_save(rbp, rdi_off * wordSize);
246 const Address rsi_save(rbp, rsi_off * wordSize);
247
248 __ movptr(rsi_save, rsi);
249 __ movptr(rdi_save, rdi);
250 #else
251 const Address mxcsr_save(rbp, mxcsr_off * wordSize);
252 {
253 Label skip_ldmx;
254 __ cmp32_mxcsr_std(mxcsr_save, rax, rscratch1);
255 __ jcc(Assembler::equal, skip_ldmx);
256 ExternalAddress mxcsr_std(StubRoutines::x86::addr_mxcsr_std());
257 __ ldmxcsr(mxcsr_std, rscratch1);
258 __ bind(skip_ldmx);
259 }
260 #endif
261
262 // Load up thread register
263 __ movptr(r15_thread, thread);
264 __ reinit_heapbase();
265
266 #ifdef ASSERT
267 // make sure we have no pending exceptions
268 {
269 Label L;
270 __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), NULL_WORD);
271 __ jcc(Assembler::equal, L);
272 __ stop("StubRoutines::call_stub: entered with pending exception");
273 __ bind(L);
274 }
275 #endif
276
277 // pass parameters if any
278 BLOCK_COMMENT("pass parameters if any");
279 Label parameters_done;
280 __ movl(c_rarg3, parameter_size);
281 __ testl(c_rarg3, c_rarg3);
282 __ jcc(Assembler::zero, parameters_done);
283
284 Label loop;
285 __ movptr(c_rarg2, parameters); // parameter pointer
286 __ movl(c_rarg1, c_rarg3); // parameter counter is in c_rarg1
287 __ BIND(loop);
288 __ movptr(rax, Address(c_rarg2, 0));// get parameter
289 __ addptr(c_rarg2, wordSize); // advance to next parameter
290 __ decrementl(c_rarg1); // decrement counter
291 __ push(rax); // pass parameter
292 __ jcc(Assembler::notZero, loop);
293
294 // call Java function
295 __ BIND(parameters_done);
296 __ movptr(rbx, method); // get Method*
297 __ movptr(c_rarg1, entry_point); // get entry_point
298 __ mov(r13, rsp); // set sender sp
299 BLOCK_COMMENT("call Java function");
300 __ call(c_rarg1);
301
302 BLOCK_COMMENT("call_stub_return_address:");
303 return_address = __ pc();
304
305 // store result depending on type (everything that is not
306 // T_OBJECT, T_LONG, T_FLOAT or T_DOUBLE is treated as T_INT)
307 __ movptr(r13, result);
308 Label is_long, is_float, is_double, check_prim, exit;
309 __ movl(rbx, result_type);
310 __ cmpl(rbx, T_OBJECT);
311 __ jcc(Assembler::equal, check_prim);
312 __ cmpl(rbx, T_LONG);
313 __ jcc(Assembler::equal, is_long);
314 __ cmpl(rbx, T_FLOAT);
315 __ jcc(Assembler::equal, is_float);
316 __ cmpl(rbx, T_DOUBLE);
317 __ jcc(Assembler::equal, is_double);
318 #ifdef ASSERT
319 // make sure the type is INT
320 {
321 Label L;
322 __ cmpl(rbx, T_INT);
323 __ jcc(Assembler::equal, L);
324 __ stop("StubRoutines::call_stub: unexpected result type");
325 __ bind(L);
326 }
327 #endif
328
329 // handle T_INT case
330 __ movl(Address(r13, 0), rax);
331
332 __ BIND(exit);
333
334 // pop parameters
335 __ lea(rsp, rsp_after_call);
336
337 #ifdef ASSERT
338 // verify that threads correspond
339 {
340 Label L1, L2, L3;
341 __ cmpptr(r15_thread, thread);
342 __ jcc(Assembler::equal, L1);
343 __ stop("StubRoutines::call_stub: r15_thread is corrupted");
344 __ bind(L1);
345 __ get_thread_slow(rbx);
346 __ cmpptr(r15_thread, thread);
347 __ jcc(Assembler::equal, L2);
348 __ stop("StubRoutines::call_stub: r15_thread is modified by call");
349 __ bind(L2);
350 __ cmpptr(r15_thread, rbx);
351 __ jcc(Assembler::equal, L3);
352 __ stop("StubRoutines::call_stub: threads must correspond");
353 __ bind(L3);
354 }
355 #endif
356
357 __ pop_cont_fastpath();
358
359 // restore regs belonging to calling function
360 #ifdef _WIN64
361 // emit the restores for xmm regs
362 for (int i = xmm_save_first; i <= last_reg; i++) {
363 __ movdqu(as_XMMRegister(i), xmm_save(i));
364 }
365 #endif
366 __ movptr(r15, r15_save);
367 __ movptr(r14, r14_save);
368 __ movptr(r13, r13_save);
369 __ movptr(r12, r12_save);
370 __ movptr(rbx, rbx_save);
371
372 #ifdef _WIN64
373 __ movptr(rdi, rdi_save);
374 __ movptr(rsi, rsi_save);
375 #else
376 __ ldmxcsr(mxcsr_save);
377 #endif
378
379 // restore rsp
380 __ addptr(rsp, -rsp_after_call_off * wordSize);
381
382 // return
383 __ vzeroupper();
384 __ pop(rbp);
385 __ ret(0);
386
387 // handle return types different from T_INT
388 __ BIND(check_prim);
389 if (InlineTypeReturnedAsFields) {
390 // Check for scalarized return value
391 __ testptr(rax, 1);
392 __ jcc(Assembler::zero, is_long);
393 // Load pack handler address
394 __ andptr(rax, -2);
395 __ movptr(rax, Address(rax, InstanceKlass::adr_inlineklass_fixed_block_offset()));
396 __ movptr(rbx, Address(rax, InlineKlass::pack_handler_jobject_offset()));
397 // Call pack handler to initialize the buffer
398 __ call(rbx);
399 __ jmp(exit);
400 }
401 __ BIND(is_long);
402 __ movq(Address(r13, 0), rax);
403 __ jmp(exit);
404
405 __ BIND(is_float);
406 __ movflt(Address(r13, 0), xmm0);
407 __ jmp(exit);
408
409 __ BIND(is_double);
410 __ movdbl(Address(r13, 0), xmm0);
411 __ jmp(exit);
412
413 return start;
414 }
415
416 // Return point for a Java call if there's an exception thrown in
417 // Java code. The exception is caught and transformed into a
418 // pending exception stored in JavaThread that can be tested from
419 // within the VM.
420 //
421 // Note: Usually the parameters are removed by the callee. In case
422 // of an exception crossing an activation frame boundary, that is
423 // not the case if the callee is compiled code => need to setup the
424 // rsp.
425 //
426 // rax: exception oop
427
428 address StubGenerator::generate_catch_exception() {
429 StubId stub_id = StubId::stubgen_catch_exception_id;
430 StubCodeMark mark(this, stub_id);
431 address start = __ pc();
432
433 // same as in generate_call_stub():
434 const Address rsp_after_call(rbp, rsp_after_call_off * wordSize);
435 const Address thread (rbp, thread_off * wordSize);
436
437 #ifdef ASSERT
438 // verify that threads correspond
439 {
440 Label L1, L2, L3;
441 __ cmpptr(r15_thread, thread);
442 __ jcc(Assembler::equal, L1);
443 __ stop("StubRoutines::catch_exception: r15_thread is corrupted");
444 __ bind(L1);
445 __ get_thread_slow(rbx);
446 __ cmpptr(r15_thread, thread);
447 __ jcc(Assembler::equal, L2);
448 __ stop("StubRoutines::catch_exception: r15_thread is modified by call");
449 __ bind(L2);
450 __ cmpptr(r15_thread, rbx);
451 __ jcc(Assembler::equal, L3);
452 __ stop("StubRoutines::catch_exception: threads must correspond");
453 __ bind(L3);
454 }
455 #endif
456
457 // set pending exception
458 __ verify_oop(rax);
459
460 __ movptr(Address(r15_thread, Thread::pending_exception_offset()), rax);
461 __ lea(rscratch1, ExternalAddress((address)__FILE__));
462 __ movptr(Address(r15_thread, Thread::exception_file_offset()), rscratch1);
463 __ movl(Address(r15_thread, Thread::exception_line_offset()), (int) __LINE__);
464
465 // complete return to VM
466 assert(StubRoutines::_call_stub_return_address != nullptr,
467 "_call_stub_return_address must have been generated before");
468 __ jump(RuntimeAddress(StubRoutines::_call_stub_return_address));
469
470 return start;
471 }
472
473 // Continuation point for runtime calls returning with a pending
474 // exception. The pending exception check happened in the runtime
475 // or native call stub. The pending exception in Thread is
476 // converted into a Java-level exception.
477 //
478 // Contract with Java-level exception handlers:
479 // rax: exception
480 // rdx: throwing pc
481 //
482 // NOTE: At entry of this stub, exception-pc must be on stack !!
483
484 address StubGenerator::generate_forward_exception() {
485 StubId stub_id = StubId::stubgen_forward_exception_id;
486 StubCodeMark mark(this, stub_id);
487 address start = __ pc();
488
489 // Upon entry, the sp points to the return address returning into
490 // Java (interpreted or compiled) code; i.e., the return address
491 // becomes the throwing pc.
492 //
493 // Arguments pushed before the runtime call are still on the stack
494 // but the exception handler will reset the stack pointer ->
495 // ignore them. A potential result in registers can be ignored as
496 // well.
497
498 #ifdef ASSERT
499 // make sure this code is only executed if there is a pending exception
500 {
501 Label L;
502 __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), NULL_WORD);
503 __ jcc(Assembler::notEqual, L);
504 __ stop("StubRoutines::forward exception: no pending exception (1)");
505 __ bind(L);
506 }
507 #endif
508
509 // compute exception handler into rbx
510 __ movptr(c_rarg0, Address(rsp, 0));
511 BLOCK_COMMENT("call exception_handler_for_return_address");
512 __ call_VM_leaf(CAST_FROM_FN_PTR(address,
513 SharedRuntime::exception_handler_for_return_address),
514 r15_thread, c_rarg0);
515 __ mov(rbx, rax);
516
517 // setup rax & rdx, remove return address & clear pending exception
518 __ pop(rdx);
519 __ movptr(rax, Address(r15_thread, Thread::pending_exception_offset()));
520 __ movptr(Address(r15_thread, Thread::pending_exception_offset()), NULL_WORD);
521
522 #ifdef ASSERT
523 // make sure exception is set
524 {
525 Label L;
526 __ testptr(rax, rax);
527 __ jcc(Assembler::notEqual, L);
528 __ stop("StubRoutines::forward exception: no pending exception (2)");
529 __ bind(L);
530 }
531 #endif
532
533 // continue at exception handler (return address removed)
534 // rax: exception
535 // rbx: exception handler
536 // rdx: throwing pc
537 __ verify_oop(rax);
538 __ jmp(rbx);
539
540 return start;
541 }
542
543 // Support for intptr_t OrderAccess::fence()
544 //
545 // Arguments :
546 //
547 // Result:
548 address StubGenerator::generate_orderaccess_fence() {
549 StubId stub_id = StubId::stubgen_fence_id;
550 StubCodeMark mark(this, stub_id);
551 address start = __ pc();
552
553 __ membar(Assembler::StoreLoad);
554 __ ret(0);
555
556 return start;
557 }
558
559
560 //----------------------------------------------------------------------------------------------------
561 // Support for void verify_mxcsr()
562 //
563 // This routine is used with -Xcheck:jni to verify that native
564 // JNI code does not return to Java code without restoring the
565 // MXCSR register to our expected state.
566
567 address StubGenerator::generate_verify_mxcsr() {
568 StubId stub_id = StubId::stubgen_verify_mxcsr_id;
569 StubCodeMark mark(this, stub_id);
570 address start = __ pc();
571
572 const Address mxcsr_save(rsp, 0);
573
574 if (CheckJNICalls) {
575 Label ok_ret;
576 ExternalAddress mxcsr_std(StubRoutines::x86::addr_mxcsr_std());
577 __ push_ppx(rax);
578 __ subptr(rsp, wordSize); // allocate a temp location
579 __ cmp32_mxcsr_std(mxcsr_save, rax, rscratch1);
580 __ jcc(Assembler::equal, ok_ret);
581
582 __ warn("MXCSR changed by native JNI code, use -XX:+RestoreMXCSROnJNICall");
583
584 __ ldmxcsr(mxcsr_std, rscratch1);
585
586 __ bind(ok_ret);
587 __ addptr(rsp, wordSize);
588 __ pop_ppx(rax);
589 }
590
591 __ ret(0);
592
593 return start;
594 }
595
596 address StubGenerator::generate_f2i_fixup() {
597 StubId stub_id = StubId::stubgen_f2i_fixup_id;
598 StubCodeMark mark(this, stub_id);
599 Address inout(rsp, 5 * wordSize); // return address + 4 saves
600
601 address start = __ pc();
602
603 Label L;
604
605 __ push_ppx(rax);
606 __ push_ppx(c_rarg3);
607 __ push_ppx(c_rarg2);
608 __ push_ppx(c_rarg1);
609
610 __ movl(rax, 0x7f800000);
611 __ xorl(c_rarg3, c_rarg3);
612 __ movl(c_rarg2, inout);
613 __ movl(c_rarg1, c_rarg2);
614 __ andl(c_rarg1, 0x7fffffff);
615 __ cmpl(rax, c_rarg1); // NaN? -> 0
616 __ jcc(Assembler::negative, L);
617 __ testl(c_rarg2, c_rarg2); // signed ? min_jint : max_jint
618 __ movl(c_rarg3, 0x80000000);
619 __ movl(rax, 0x7fffffff);
620 __ cmovl(Assembler::positive, c_rarg3, rax);
621
622 __ bind(L);
623 __ movptr(inout, c_rarg3);
624
625 __ pop_ppx(c_rarg1);
626 __ pop_ppx(c_rarg2);
627 __ pop_ppx(c_rarg3);
628 __ pop_ppx(rax);
629
630 __ ret(0);
631
632 return start;
633 }
634
635 address StubGenerator::generate_f2l_fixup() {
636 StubId stub_id = StubId::stubgen_f2l_fixup_id;
637 StubCodeMark mark(this, stub_id);
638 Address inout(rsp, 5 * wordSize); // return address + 4 saves
639 address start = __ pc();
640
641 Label L;
642
643 __ push_ppx(rax);
644 __ push_ppx(c_rarg3);
645 __ push_ppx(c_rarg2);
646 __ push_ppx(c_rarg1);
647
648 __ movl(rax, 0x7f800000);
649 __ xorl(c_rarg3, c_rarg3);
650 __ movl(c_rarg2, inout);
651 __ movl(c_rarg1, c_rarg2);
652 __ andl(c_rarg1, 0x7fffffff);
653 __ cmpl(rax, c_rarg1); // NaN? -> 0
654 __ jcc(Assembler::negative, L);
655 __ testl(c_rarg2, c_rarg2); // signed ? min_jlong : max_jlong
656 __ mov64(c_rarg3, 0x8000000000000000);
657 __ mov64(rax, 0x7fffffffffffffff);
658 __ cmov(Assembler::positive, c_rarg3, rax);
659
660 __ bind(L);
661 __ movptr(inout, c_rarg3);
662
663 __ pop_ppx(c_rarg1);
664 __ pop_ppx(c_rarg2);
665 __ pop_ppx(c_rarg3);
666 __ pop_ppx(rax);
667
668 __ ret(0);
669
670 return start;
671 }
672
673 address StubGenerator::generate_d2i_fixup() {
674 StubId stub_id = StubId::stubgen_d2i_fixup_id;
675 StubCodeMark mark(this, stub_id);
676 Address inout(rsp, 6 * wordSize); // return address + 5 saves
677
678 address start = __ pc();
679
680 Label L;
681
682 __ push_ppx(rax);
683 __ push_ppx(c_rarg3);
684 __ push_ppx(c_rarg2);
685 __ push_ppx(c_rarg1);
686 __ push_ppx(c_rarg0);
687
688 __ movl(rax, 0x7ff00000);
689 __ movq(c_rarg2, inout);
690 __ movl(c_rarg3, c_rarg2);
691 __ mov(c_rarg1, c_rarg2);
692 __ mov(c_rarg0, c_rarg2);
693 __ negl(c_rarg3);
694 __ shrptr(c_rarg1, 0x20);
695 __ orl(c_rarg3, c_rarg2);
696 __ andl(c_rarg1, 0x7fffffff);
697 __ xorl(c_rarg2, c_rarg2);
698 __ shrl(c_rarg3, 0x1f);
699 __ orl(c_rarg1, c_rarg3);
700 __ cmpl(rax, c_rarg1);
701 __ jcc(Assembler::negative, L); // NaN -> 0
702 __ testptr(c_rarg0, c_rarg0); // signed ? min_jint : max_jint
703 __ movl(c_rarg2, 0x80000000);
704 __ movl(rax, 0x7fffffff);
705 __ cmov(Assembler::positive, c_rarg2, rax);
706
707 __ bind(L);
708 __ movptr(inout, c_rarg2);
709
710 __ pop_ppx(c_rarg0);
711 __ pop_ppx(c_rarg1);
712 __ pop_ppx(c_rarg2);
713 __ pop_ppx(c_rarg3);
714 __ pop_ppx(rax);
715
716 __ ret(0);
717
718 return start;
719 }
720
721 address StubGenerator::generate_d2l_fixup() {
722 StubId stub_id = StubId::stubgen_d2l_fixup_id;
723 StubCodeMark mark(this, stub_id);
724 Address inout(rsp, 6 * wordSize); // return address + 5 saves
725
726 address start = __ pc();
727
728 Label L;
729
730 __ push_ppx(rax);
731 __ push_ppx(c_rarg3);
732 __ push_ppx(c_rarg2);
733 __ push_ppx(c_rarg1);
734 __ push_ppx(c_rarg0);
735
736 __ movl(rax, 0x7ff00000);
737 __ movq(c_rarg2, inout);
738 __ movl(c_rarg3, c_rarg2);
739 __ mov(c_rarg1, c_rarg2);
740 __ mov(c_rarg0, c_rarg2);
741 __ negl(c_rarg3);
742 __ shrptr(c_rarg1, 0x20);
743 __ orl(c_rarg3, c_rarg2);
744 __ andl(c_rarg1, 0x7fffffff);
745 __ xorl(c_rarg2, c_rarg2);
746 __ shrl(c_rarg3, 0x1f);
747 __ orl(c_rarg1, c_rarg3);
748 __ cmpl(rax, c_rarg1);
749 __ jcc(Assembler::negative, L); // NaN -> 0
750 __ testq(c_rarg0, c_rarg0); // signed ? min_jlong : max_jlong
751 __ mov64(c_rarg2, 0x8000000000000000);
752 __ mov64(rax, 0x7fffffffffffffff);
753 __ cmovq(Assembler::positive, c_rarg2, rax);
754
755 __ bind(L);
756 __ movq(inout, c_rarg2);
757
758 __ pop_ppx(c_rarg0);
759 __ pop_ppx(c_rarg1);
760 __ pop_ppx(c_rarg2);
761 __ pop_ppx(c_rarg3);
762 __ pop_ppx(rax);
763
764 __ ret(0);
765
766 return start;
767 }
768
769 address StubGenerator::generate_count_leading_zeros_lut() {
770 __ align64();
771 StubId stub_id = StubId::stubgen_vector_count_leading_zeros_lut_id;
772 StubCodeMark mark(this, stub_id);
773 address start = __ pc();
774
775 __ emit_data64(0x0101010102020304, relocInfo::none);
776 __ emit_data64(0x0000000000000000, relocInfo::none);
777 __ emit_data64(0x0101010102020304, relocInfo::none);
778 __ emit_data64(0x0000000000000000, relocInfo::none);
779 __ emit_data64(0x0101010102020304, relocInfo::none);
780 __ emit_data64(0x0000000000000000, relocInfo::none);
781 __ emit_data64(0x0101010102020304, relocInfo::none);
782 __ emit_data64(0x0000000000000000, relocInfo::none);
783
784 return start;
785 }
786
787 address StubGenerator::generate_popcount_avx_lut() {
788 __ align64();
789 StubId stub_id = StubId::stubgen_vector_popcount_lut_id;
790 StubCodeMark mark(this, stub_id);
791 address start = __ pc();
792
793 __ emit_data64(0x0302020102010100, relocInfo::none);
794 __ emit_data64(0x0403030203020201, relocInfo::none);
795 __ emit_data64(0x0302020102010100, relocInfo::none);
796 __ emit_data64(0x0403030203020201, relocInfo::none);
797 __ emit_data64(0x0302020102010100, relocInfo::none);
798 __ emit_data64(0x0403030203020201, relocInfo::none);
799 __ emit_data64(0x0302020102010100, relocInfo::none);
800 __ emit_data64(0x0403030203020201, relocInfo::none);
801
802 return start;
803 }
804
805 address StubGenerator::generate_iota_indices() {
806 __ align(CodeEntryAlignment);
807 StubId stub_id = StubId::stubgen_vector_iota_indices_id;
808 StubCodeMark mark(this, stub_id);
809 address start = __ pc();
810 // B
811 __ emit_data64(0x0706050403020100, relocInfo::none);
812 __ emit_data64(0x0F0E0D0C0B0A0908, relocInfo::none);
813 __ emit_data64(0x1716151413121110, relocInfo::none);
814 __ emit_data64(0x1F1E1D1C1B1A1918, relocInfo::none);
815 __ emit_data64(0x2726252423222120, relocInfo::none);
816 __ emit_data64(0x2F2E2D2C2B2A2928, relocInfo::none);
817 __ emit_data64(0x3736353433323130, relocInfo::none);
818 __ emit_data64(0x3F3E3D3C3B3A3938, relocInfo::none);
819 // W
820 __ emit_data64(0x0003000200010000, relocInfo::none);
821 __ emit_data64(0x0007000600050004, relocInfo::none);
822 __ emit_data64(0x000B000A00090008, relocInfo::none);
823 __ emit_data64(0x000F000E000D000C, relocInfo::none);
824 __ emit_data64(0x0013001200110010, relocInfo::none);
825 __ emit_data64(0x0017001600150014, relocInfo::none);
826 __ emit_data64(0x001B001A00190018, relocInfo::none);
827 __ emit_data64(0x001F001E001D001C, relocInfo::none);
828 // D
829 __ emit_data64(0x0000000100000000, relocInfo::none);
830 __ emit_data64(0x0000000300000002, relocInfo::none);
831 __ emit_data64(0x0000000500000004, relocInfo::none);
832 __ emit_data64(0x0000000700000006, relocInfo::none);
833 __ emit_data64(0x0000000900000008, relocInfo::none);
834 __ emit_data64(0x0000000B0000000A, relocInfo::none);
835 __ emit_data64(0x0000000D0000000C, relocInfo::none);
836 __ emit_data64(0x0000000F0000000E, relocInfo::none);
837 // Q
838 __ emit_data64(0x0000000000000000, relocInfo::none);
839 __ emit_data64(0x0000000000000001, relocInfo::none);
840 __ emit_data64(0x0000000000000002, relocInfo::none);
841 __ emit_data64(0x0000000000000003, relocInfo::none);
842 __ emit_data64(0x0000000000000004, relocInfo::none);
843 __ emit_data64(0x0000000000000005, relocInfo::none);
844 __ emit_data64(0x0000000000000006, relocInfo::none);
845 __ emit_data64(0x0000000000000007, relocInfo::none);
846 // D - FP
847 __ emit_data64(0x3F80000000000000, relocInfo::none); // 0.0f, 1.0f
848 __ emit_data64(0x4040000040000000, relocInfo::none); // 2.0f, 3.0f
849 __ emit_data64(0x40A0000040800000, relocInfo::none); // 4.0f, 5.0f
850 __ emit_data64(0x40E0000040C00000, relocInfo::none); // 6.0f, 7.0f
851 __ emit_data64(0x4110000041000000, relocInfo::none); // 8.0f, 9.0f
852 __ emit_data64(0x4130000041200000, relocInfo::none); // 10.0f, 11.0f
853 __ emit_data64(0x4150000041400000, relocInfo::none); // 12.0f, 13.0f
854 __ emit_data64(0x4170000041600000, relocInfo::none); // 14.0f, 15.0f
855 // Q - FP
856 __ emit_data64(0x0000000000000000, relocInfo::none); // 0.0d
857 __ emit_data64(0x3FF0000000000000, relocInfo::none); // 1.0d
858 __ emit_data64(0x4000000000000000, relocInfo::none); // 2.0d
859 __ emit_data64(0x4008000000000000, relocInfo::none); // 3.0d
860 __ emit_data64(0x4010000000000000, relocInfo::none); // 4.0d
861 __ emit_data64(0x4014000000000000, relocInfo::none); // 5.0d
862 __ emit_data64(0x4018000000000000, relocInfo::none); // 6.0d
863 __ emit_data64(0x401c000000000000, relocInfo::none); // 7.0d
864 return start;
865 }
866
867 address StubGenerator::generate_vector_reverse_bit_lut() {
868 __ align(CodeEntryAlignment);
869 StubId stub_id = StubId::stubgen_vector_reverse_bit_lut_id;
870 StubCodeMark mark(this, stub_id);
871 address start = __ pc();
872
873 __ emit_data64(0x0E060A020C040800, relocInfo::none);
874 __ emit_data64(0x0F070B030D050901, relocInfo::none);
875 __ emit_data64(0x0E060A020C040800, relocInfo::none);
876 __ emit_data64(0x0F070B030D050901, relocInfo::none);
877 __ emit_data64(0x0E060A020C040800, relocInfo::none);
878 __ emit_data64(0x0F070B030D050901, relocInfo::none);
879 __ emit_data64(0x0E060A020C040800, relocInfo::none);
880 __ emit_data64(0x0F070B030D050901, relocInfo::none);
881
882 return start;
883 }
884
885 address StubGenerator::generate_vector_reverse_byte_perm_mask_long() {
886 __ align(CodeEntryAlignment);
887 StubId stub_id = StubId::stubgen_vector_reverse_byte_perm_mask_long_id;
888 StubCodeMark mark(this, stub_id);
889 address start = __ pc();
890
891 __ emit_data64(0x0001020304050607, relocInfo::none);
892 __ emit_data64(0x08090A0B0C0D0E0F, relocInfo::none);
893 __ emit_data64(0x0001020304050607, relocInfo::none);
894 __ emit_data64(0x08090A0B0C0D0E0F, relocInfo::none);
895 __ emit_data64(0x0001020304050607, relocInfo::none);
896 __ emit_data64(0x08090A0B0C0D0E0F, relocInfo::none);
897 __ emit_data64(0x0001020304050607, relocInfo::none);
898 __ emit_data64(0x08090A0B0C0D0E0F, relocInfo::none);
899
900 return start;
901 }
902
903 address StubGenerator::generate_vector_reverse_byte_perm_mask_int() {
904 __ align(CodeEntryAlignment);
905 StubId stub_id = StubId::stubgen_vector_reverse_byte_perm_mask_int_id;
906 StubCodeMark mark(this, stub_id);
907 address start = __ pc();
908
909 __ emit_data64(0x0405060700010203, relocInfo::none);
910 __ emit_data64(0x0C0D0E0F08090A0B, relocInfo::none);
911 __ emit_data64(0x0405060700010203, relocInfo::none);
912 __ emit_data64(0x0C0D0E0F08090A0B, relocInfo::none);
913 __ emit_data64(0x0405060700010203, relocInfo::none);
914 __ emit_data64(0x0C0D0E0F08090A0B, relocInfo::none);
915 __ emit_data64(0x0405060700010203, relocInfo::none);
916 __ emit_data64(0x0C0D0E0F08090A0B, relocInfo::none);
917
918 return start;
919 }
920
921 address StubGenerator::generate_vector_reverse_byte_perm_mask_short() {
922 __ align(CodeEntryAlignment);
923 StubId stub_id = StubId::stubgen_vector_reverse_byte_perm_mask_short_id;
924 StubCodeMark mark(this, stub_id);
925 address start = __ pc();
926
927 __ emit_data64(0x0607040502030001, relocInfo::none);
928 __ emit_data64(0x0E0F0C0D0A0B0809, relocInfo::none);
929 __ emit_data64(0x0607040502030001, relocInfo::none);
930 __ emit_data64(0x0E0F0C0D0A0B0809, relocInfo::none);
931 __ emit_data64(0x0607040502030001, relocInfo::none);
932 __ emit_data64(0x0E0F0C0D0A0B0809, relocInfo::none);
933 __ emit_data64(0x0607040502030001, relocInfo::none);
934 __ emit_data64(0x0E0F0C0D0A0B0809, relocInfo::none);
935
936 return start;
937 }
938
939 address StubGenerator::generate_vector_byte_shuffle_mask() {
940 __ align(CodeEntryAlignment);
941 StubId stub_id = StubId::stubgen_vector_byte_shuffle_mask_id;
942 StubCodeMark mark(this, stub_id);
943 address start = __ pc();
944
945 __ emit_data64(0x7070707070707070, relocInfo::none);
946 __ emit_data64(0x7070707070707070, relocInfo::none);
947 __ emit_data64(0xF0F0F0F0F0F0F0F0, relocInfo::none);
948 __ emit_data64(0xF0F0F0F0F0F0F0F0, relocInfo::none);
949
950 return start;
951 }
952
953 address StubGenerator::generate_fp_mask(StubId stub_id, int64_t mask) {
954 __ align(CodeEntryAlignment);
955 StubCodeMark mark(this, stub_id);
956 address start = __ pc();
957
958 __ emit_data64( mask, relocInfo::none );
959 __ emit_data64( mask, relocInfo::none );
960
961 return start;
962 }
963
964 address StubGenerator::generate_compress_perm_table(StubId stub_id) {
965 int esize;
966 switch (stub_id) {
967 case StubId::stubgen_compress_perm_table32_id:
968 esize = 32;
969 break;
970 case StubId::stubgen_compress_perm_table64_id:
971 esize = 64;
972 break;
973 default:
974 ShouldNotReachHere();
975 }
976 __ align(CodeEntryAlignment);
977 StubCodeMark mark(this, stub_id);
978 address start = __ pc();
979 if (esize == 32) {
980 // Loop to generate 256 x 8 int compression permute index table. A row is
981 // accessed using 8 bit index computed using vector mask. An entry in
982 // a row holds either a valid permute index corresponding to set bit position
983 // or a -1 (default) value.
984 for (int mask = 0; mask < 256; mask++) {
985 int ctr = 0;
986 for (int j = 0; j < 8; j++) {
987 if (mask & (1 << j)) {
988 __ emit_data(j, relocInfo::none);
989 ctr++;
990 }
991 }
992 for (; ctr < 8; ctr++) {
993 __ emit_data(-1, relocInfo::none);
994 }
995 }
996 } else {
997 assert(esize == 64, "");
998 // Loop to generate 16 x 4 long compression permute index table. A row is
999 // accessed using 4 bit index computed using vector mask. An entry in
1000 // a row holds either a valid permute index pair for a quadword corresponding
1001 // to set bit position or a -1 (default) value.
1002 for (int mask = 0; mask < 16; mask++) {
1003 int ctr = 0;
1004 for (int j = 0; j < 4; j++) {
1005 if (mask & (1 << j)) {
1006 __ emit_data(2 * j, relocInfo::none);
1007 __ emit_data(2 * j + 1, relocInfo::none);
1008 ctr++;
1009 }
1010 }
1011 for (; ctr < 4; ctr++) {
1012 __ emit_data64(-1L, relocInfo::none);
1013 }
1014 }
1015 }
1016 return start;
1017 }
1018
1019 address StubGenerator::generate_expand_perm_table(StubId stub_id) {
1020 int esize;
1021 switch (stub_id) {
1022 case StubId::stubgen_expand_perm_table32_id:
1023 esize = 32;
1024 break;
1025 case StubId::stubgen_expand_perm_table64_id:
1026 esize = 64;
1027 break;
1028 default:
1029 ShouldNotReachHere();
1030 }
1031 __ align(CodeEntryAlignment);
1032 StubCodeMark mark(this, stub_id);
1033 address start = __ pc();
1034 if (esize == 32) {
1035 // Loop to generate 256 x 8 int expand permute index table. A row is accessed
1036 // using 8 bit index computed using vector mask. An entry in a row holds either
1037 // a valid permute index (starting from least significant lane) placed at poisition
1038 // corresponding to set bit position or a -1 (default) value.
1039 for (int mask = 0; mask < 256; mask++) {
1040 int ctr = 0;
1041 for (int j = 0; j < 8; j++) {
1042 if (mask & (1 << j)) {
1043 __ emit_data(ctr++, relocInfo::none);
1044 } else {
1045 __ emit_data(-1, relocInfo::none);
1046 }
1047 }
1048 }
1049 } else {
1050 assert(esize == 64, "");
1051 // Loop to generate 16 x 4 long expand permute index table. A row is accessed
1052 // using 4 bit index computed using vector mask. An entry in a row holds either
1053 // a valid doubleword permute index pair representing a quadword index (starting
1054 // from least significant lane) placed at poisition corresponding to set bit
1055 // position or a -1 (default) value.
1056 for (int mask = 0; mask < 16; mask++) {
1057 int ctr = 0;
1058 for (int j = 0; j < 4; j++) {
1059 if (mask & (1 << j)) {
1060 __ emit_data(2 * ctr, relocInfo::none);
1061 __ emit_data(2 * ctr + 1, relocInfo::none);
1062 ctr++;
1063 } else {
1064 __ emit_data64(-1L, relocInfo::none);
1065 }
1066 }
1067 }
1068 }
1069 return start;
1070 }
1071
1072 address StubGenerator::generate_vector_mask(StubId stub_id, int64_t mask) {
1073 __ align(CodeEntryAlignment);
1074 StubCodeMark mark(this, stub_id);
1075 address start = __ pc();
1076
1077 __ emit_data64(mask, relocInfo::none);
1078 __ emit_data64(mask, relocInfo::none);
1079 __ emit_data64(mask, relocInfo::none);
1080 __ emit_data64(mask, relocInfo::none);
1081 __ emit_data64(mask, relocInfo::none);
1082 __ emit_data64(mask, relocInfo::none);
1083 __ emit_data64(mask, relocInfo::none);
1084 __ emit_data64(mask, relocInfo::none);
1085
1086 return start;
1087 }
1088
1089 address StubGenerator::generate_vector_byte_perm_mask() {
1090 __ align(CodeEntryAlignment);
1091 StubId stub_id = StubId::stubgen_vector_byte_perm_mask_id;
1092 StubCodeMark mark(this, stub_id);
1093 address start = __ pc();
1094
1095 __ emit_data64(0x0000000000000001, relocInfo::none);
1096 __ emit_data64(0x0000000000000003, relocInfo::none);
1097 __ emit_data64(0x0000000000000005, relocInfo::none);
1098 __ emit_data64(0x0000000000000007, relocInfo::none);
1099 __ emit_data64(0x0000000000000000, relocInfo::none);
1100 __ emit_data64(0x0000000000000002, relocInfo::none);
1101 __ emit_data64(0x0000000000000004, relocInfo::none);
1102 __ emit_data64(0x0000000000000006, relocInfo::none);
1103
1104 return start;
1105 }
1106
1107 address StubGenerator::generate_vector_fp_mask(StubId stub_id, int64_t mask) {
1108 __ align(CodeEntryAlignment);
1109 StubCodeMark mark(this, stub_id);
1110 address start = __ pc();
1111
1112 __ emit_data64(mask, relocInfo::none);
1113 __ emit_data64(mask, relocInfo::none);
1114 __ emit_data64(mask, relocInfo::none);
1115 __ emit_data64(mask, relocInfo::none);
1116 __ emit_data64(mask, relocInfo::none);
1117 __ emit_data64(mask, relocInfo::none);
1118 __ emit_data64(mask, relocInfo::none);
1119 __ emit_data64(mask, relocInfo::none);
1120
1121 return start;
1122 }
1123
1124 address StubGenerator::generate_vector_custom_i32(StubId stub_id, Assembler::AvxVectorLen len,
1125 int32_t val0, int32_t val1, int32_t val2, int32_t val3,
1126 int32_t val4, int32_t val5, int32_t val6, int32_t val7,
1127 int32_t val8, int32_t val9, int32_t val10, int32_t val11,
1128 int32_t val12, int32_t val13, int32_t val14, int32_t val15) {
1129 __ align(CodeEntryAlignment);
1130 StubCodeMark mark(this, stub_id);
1131 address start = __ pc();
1132
1133 assert(len != Assembler::AVX_NoVec, "vector len must be specified");
1134 __ emit_data(val0, relocInfo::none, 0);
1135 __ emit_data(val1, relocInfo::none, 0);
1136 __ emit_data(val2, relocInfo::none, 0);
1137 __ emit_data(val3, relocInfo::none, 0);
1138 if (len >= Assembler::AVX_256bit) {
1139 __ emit_data(val4, relocInfo::none, 0);
1140 __ emit_data(val5, relocInfo::none, 0);
1141 __ emit_data(val6, relocInfo::none, 0);
1142 __ emit_data(val7, relocInfo::none, 0);
1143 if (len >= Assembler::AVX_512bit) {
1144 __ emit_data(val8, relocInfo::none, 0);
1145 __ emit_data(val9, relocInfo::none, 0);
1146 __ emit_data(val10, relocInfo::none, 0);
1147 __ emit_data(val11, relocInfo::none, 0);
1148 __ emit_data(val12, relocInfo::none, 0);
1149 __ emit_data(val13, relocInfo::none, 0);
1150 __ emit_data(val14, relocInfo::none, 0);
1151 __ emit_data(val15, relocInfo::none, 0);
1152 }
1153 }
1154 return start;
1155 }
1156
1157 // Non-destructive plausibility checks for oops
1158 //
1159 // Arguments:
1160 // all args on stack!
1161 //
1162 // Stack after saving c_rarg3:
1163 // [tos + 0]: saved c_rarg3
1164 // [tos + 1]: saved c_rarg2
1165 // [tos + 2]: saved r12 (several TemplateTable methods use it)
1166 // [tos + 3]: saved flags
1167 // [tos + 4]: return address
1168 // * [tos + 5]: error message (char*)
1169 // * [tos + 6]: object to verify (oop)
1170 // * [tos + 7]: saved rax - saved by caller and bashed
1171 // * [tos + 8]: saved r10 (rscratch1) - saved by caller
1172 // * = popped on exit
1173 address StubGenerator::generate_verify_oop() {
1174 StubId stub_id = StubId::stubgen_verify_oop_id;
1175 StubCodeMark mark(this, stub_id);
1176 address start = __ pc();
1177
1178 Label exit, error;
1179
1180 __ pushf();
1181 __ incrementl(ExternalAddress((address) StubRoutines::verify_oop_count_addr()), rscratch1);
1182
1183 __ push_ppx(r12);
1184
1185 // save c_rarg2 and c_rarg3
1186 __ push_ppx(c_rarg2);
1187 __ push_ppx(c_rarg3);
1188
1189 enum {
1190 // After previous pushes.
1191 oop_to_verify = 6 * wordSize,
1192 saved_rax = 7 * wordSize,
1193 saved_r10 = 8 * wordSize,
1194
1195 // Before the call to MacroAssembler::debug(), see below.
1196 return_addr = 16 * wordSize,
1197 error_msg = 17 * wordSize
1198 };
1199
1200 // get object
1201 __ movptr(rax, Address(rsp, oop_to_verify));
1202
1203 // make sure object is 'reasonable'
1204 __ testptr(rax, rax);
1205 __ jcc(Assembler::zero, exit); // if obj is null it is OK
1206
1207 BarrierSetAssembler* bs_asm = BarrierSet::barrier_set()->barrier_set_assembler();
1208 bs_asm->check_oop(_masm, rax, c_rarg2, c_rarg3, error);
1209
1210 // return if everything seems ok
1211 __ bind(exit);
1212 __ movptr(rax, Address(rsp, saved_rax)); // get saved rax back
1213 __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
1214 __ pop_ppx(c_rarg3); // restore c_rarg3
1215 __ pop_ppx(c_rarg2); // restore c_rarg2
1216 __ pop_ppx(r12); // restore r12
1217 __ popf(); // restore flags
1218 __ ret(4 * wordSize); // pop caller saved stuff
1219
1220 // handle errors
1221 __ bind(error);
1222 __ movptr(rax, Address(rsp, saved_rax)); // get saved rax back
1223 __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
1224 __ pop_ppx(c_rarg3); // get saved c_rarg3 back
1225 __ pop_ppx(c_rarg2); // get saved c_rarg2 back
1226 __ pop_ppx(r12); // get saved r12 back
1227 __ popf(); // get saved flags off stack --
1228 // will be ignored
1229
1230 __ pusha(); // push registers
1231 // (rip is already
1232 // already pushed)
1233 // debug(char* msg, int64_t pc, int64_t regs[])
1234 // We've popped the registers we'd saved (c_rarg3, c_rarg2 and flags), and
1235 // pushed all the registers, so now the stack looks like:
1236 // [tos + 0] 16 saved registers
1237 // [tos + 16] return address
1238 // * [tos + 17] error message (char*)
1239 // * [tos + 18] object to verify (oop)
1240 // * [tos + 19] saved rax - saved by caller and bashed
1241 // * [tos + 20] saved r10 (rscratch1) - saved by caller
1242 // * = popped on exit
1243
1244 __ movptr(c_rarg0, Address(rsp, error_msg)); // pass address of error message
1245 __ movptr(c_rarg1, Address(rsp, return_addr)); // pass return address
1246 __ movq(c_rarg2, rsp); // pass address of regs on stack
1247 __ mov(r12, rsp); // remember rsp
1248 __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1249 __ andptr(rsp, -16); // align stack as required by ABI
1250 BLOCK_COMMENT("call MacroAssembler::debug");
1251 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, MacroAssembler::debug64)));
1252 __ hlt();
1253
1254 return start;
1255 }
1256
1257
1258 // Shuffle first three arg regs on Windows into Linux/Solaris locations.
1259 //
1260 // Outputs:
1261 // rdi - rcx
1262 // rsi - rdx
1263 // rdx - r8
1264 // rcx - r9
1265 //
1266 // Registers r9 and r10 are used to save rdi and rsi on Windows, which latter
1267 // are non-volatile. r9 and r10 should not be used by the caller.
1268 //
1269 void StubGenerator::setup_arg_regs(int nargs) {
1270 const Register saved_rdi = r9;
1271 const Register saved_rsi = r10;
1272 assert(nargs == 3 || nargs == 4, "else fix");
1273 #ifdef _WIN64
1274 assert(c_rarg0 == rcx && c_rarg1 == rdx && c_rarg2 == r8 && c_rarg3 == r9,
1275 "unexpected argument registers");
1276 if (nargs == 4) {
1277 __ mov(rax, r9); // r9 is also saved_rdi
1278 }
1279 __ movptr(saved_rdi, rdi);
1280 __ movptr(saved_rsi, rsi);
1281 __ mov(rdi, rcx); // c_rarg0
1282 __ mov(rsi, rdx); // c_rarg1
1283 __ mov(rdx, r8); // c_rarg2
1284 if (nargs == 4) {
1285 __ mov(rcx, rax); // c_rarg3 (via rax)
1286 }
1287 #else
1288 assert(c_rarg0 == rdi && c_rarg1 == rsi && c_rarg2 == rdx && c_rarg3 == rcx,
1289 "unexpected argument registers");
1290 #endif
1291 DEBUG_ONLY(_regs_in_thread = false;)
1292 }
1293
1294
1295 void StubGenerator::restore_arg_regs() {
1296 assert(!_regs_in_thread, "wrong call to restore_arg_regs");
1297 const Register saved_rdi = r9;
1298 const Register saved_rsi = r10;
1299 #ifdef _WIN64
1300 __ movptr(rdi, saved_rdi);
1301 __ movptr(rsi, saved_rsi);
1302 #endif
1303 }
1304
1305
1306 // This is used in places where r10 is a scratch register, and can
1307 // be adapted if r9 is needed also.
1308 void StubGenerator::setup_arg_regs_using_thread(int nargs) {
1309 const Register saved_r15 = r9;
1310 assert(nargs == 3 || nargs == 4, "else fix");
1311 #ifdef _WIN64
1312 if (nargs == 4) {
1313 __ mov(rax, r9); // r9 is also saved_r15
1314 }
1315 __ mov(saved_r15, r15); // r15 is callee saved and needs to be restored
1316 __ get_thread_slow(r15_thread);
1317 assert(c_rarg0 == rcx && c_rarg1 == rdx && c_rarg2 == r8 && c_rarg3 == r9,
1318 "unexpected argument registers");
1319 __ movptr(Address(r15_thread, in_bytes(JavaThread::windows_saved_rdi_offset())), rdi);
1320 __ movptr(Address(r15_thread, in_bytes(JavaThread::windows_saved_rsi_offset())), rsi);
1321
1322 __ mov(rdi, rcx); // c_rarg0
1323 __ mov(rsi, rdx); // c_rarg1
1324 __ mov(rdx, r8); // c_rarg2
1325 if (nargs == 4) {
1326 __ mov(rcx, rax); // c_rarg3 (via rax)
1327 }
1328 #else
1329 assert(c_rarg0 == rdi && c_rarg1 == rsi && c_rarg2 == rdx && c_rarg3 == rcx,
1330 "unexpected argument registers");
1331 #endif
1332 DEBUG_ONLY(_regs_in_thread = true;)
1333 }
1334
1335
1336 void StubGenerator::restore_arg_regs_using_thread() {
1337 assert(_regs_in_thread, "wrong call to restore_arg_regs");
1338 const Register saved_r15 = r9;
1339 #ifdef _WIN64
1340 __ get_thread_slow(r15_thread);
1341 __ movptr(rsi, Address(r15_thread, in_bytes(JavaThread::windows_saved_rsi_offset())));
1342 __ movptr(rdi, Address(r15_thread, in_bytes(JavaThread::windows_saved_rdi_offset())));
1343 __ mov(r15, saved_r15); // r15 is callee saved and needs to be restored
1344 #endif
1345 }
1346
1347
1348 void StubGenerator::setup_argument_regs(BasicType type) {
1349 if (type == T_BYTE || type == T_SHORT) {
1350 setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1351 // r9 and r10 may be used to save non-volatile registers
1352 } else {
1353 setup_arg_regs_using_thread(); // from => rdi, to => rsi, count => rdx
1354 // r9 is used to save r15_thread
1355 }
1356 }
1357
1358
1359 void StubGenerator::restore_argument_regs(BasicType type) {
1360 if (type == T_BYTE || type == T_SHORT) {
1361 restore_arg_regs();
1362 } else {
1363 restore_arg_regs_using_thread();
1364 }
1365 }
1366
1367 address StubGenerator::generate_data_cache_writeback() {
1368 const Register src = c_rarg0; // source address
1369
1370 __ align(CodeEntryAlignment);
1371
1372 StubId stub_id = StubId::stubgen_data_cache_writeback_id;
1373 StubCodeMark mark(this, stub_id);
1374
1375 address start = __ pc();
1376
1377 __ enter();
1378 __ cache_wb(Address(src, 0));
1379 __ leave();
1380 __ ret(0);
1381
1382 return start;
1383 }
1384
1385 address StubGenerator::generate_data_cache_writeback_sync() {
1386 const Register is_pre = c_rarg0; // pre or post sync
1387
1388 __ align(CodeEntryAlignment);
1389
1390 StubId stub_id = StubId::stubgen_data_cache_writeback_sync_id;
1391 StubCodeMark mark(this, stub_id);
1392
1393 // pre wbsync is a no-op
1394 // post wbsync translates to an sfence
1395
1396 Label skip;
1397 address start = __ pc();
1398
1399 __ enter();
1400 __ cmpl(is_pre, 0);
1401 __ jcc(Assembler::notEqual, skip);
1402 __ cache_wbsync(false);
1403 __ bind(skip);
1404 __ leave();
1405 __ ret(0);
1406
1407 return start;
1408 }
1409
1410 // ofs and limit are use for multi-block byte array.
1411 // int com.sun.security.provider.MD5.implCompress(byte[] b, int ofs)
1412 address StubGenerator::generate_md5_implCompress(StubId stub_id) {
1413 bool multi_block;
1414 switch (stub_id) {
1415 case StubId::stubgen_md5_implCompress_id:
1416 multi_block = false;
1417 break;
1418 case StubId::stubgen_md5_implCompressMB_id:
1419 multi_block = true;
1420 break;
1421 default:
1422 ShouldNotReachHere();
1423 }
1424 __ align(CodeEntryAlignment);
1425 StubCodeMark mark(this, stub_id);
1426 address start = __ pc();
1427
1428 const Register buf_param = r15;
1429 const Address state_param(rsp, 0 * wordSize);
1430 const Address ofs_param (rsp, 1 * wordSize );
1431 const Address limit_param(rsp, 1 * wordSize + 4);
1432
1433 __ enter();
1434 __ push_ppx(rbx);
1435 __ push_ppx(rdi);
1436 __ push_ppx(rsi);
1437 __ push_ppx(r15);
1438 __ subptr(rsp, 2 * wordSize);
1439
1440 __ movptr(buf_param, c_rarg0);
1441 __ movptr(state_param, c_rarg1);
1442 if (multi_block) {
1443 __ movl(ofs_param, c_rarg2);
1444 __ movl(limit_param, c_rarg3);
1445 }
1446 __ fast_md5(buf_param, state_param, ofs_param, limit_param, multi_block);
1447
1448 __ addptr(rsp, 2 * wordSize);
1449 __ pop_ppx(r15);
1450 __ pop_ppx(rsi);
1451 __ pop_ppx(rdi);
1452 __ pop_ppx(rbx);
1453 __ leave();
1454 __ ret(0);
1455
1456 return start;
1457 }
1458
1459 address StubGenerator::generate_upper_word_mask() {
1460 __ align64();
1461 StubId stub_id = StubId::stubgen_upper_word_mask_id;
1462 StubCodeMark mark(this, stub_id);
1463 address start = __ pc();
1464
1465 __ emit_data64(0x0000000000000000, relocInfo::none);
1466 __ emit_data64(0xFFFFFFFF00000000, relocInfo::none);
1467
1468 return start;
1469 }
1470
1471 address StubGenerator::generate_shuffle_byte_flip_mask() {
1472 __ align64();
1473 StubId stub_id = StubId::stubgen_shuffle_byte_flip_mask_id;
1474 StubCodeMark mark(this, stub_id);
1475 address start = __ pc();
1476
1477 __ emit_data64(0x08090a0b0c0d0e0f, relocInfo::none);
1478 __ emit_data64(0x0001020304050607, relocInfo::none);
1479
1480 return start;
1481 }
1482
1483 // ofs and limit are use for multi-block byte array.
1484 // int com.sun.security.provider.DigestBase.implCompressMultiBlock(byte[] b, int ofs, int limit)
1485 address StubGenerator::generate_sha1_implCompress(StubId stub_id) {
1486 bool multi_block;
1487 switch (stub_id) {
1488 case StubId::stubgen_sha1_implCompress_id:
1489 multi_block = false;
1490 break;
1491 case StubId::stubgen_sha1_implCompressMB_id:
1492 multi_block = true;
1493 break;
1494 default:
1495 ShouldNotReachHere();
1496 }
1497 __ align(CodeEntryAlignment);
1498 StubCodeMark mark(this, stub_id);
1499 address start = __ pc();
1500
1501 Register buf = c_rarg0;
1502 Register state = c_rarg1;
1503 Register ofs = c_rarg2;
1504 Register limit = c_rarg3;
1505
1506 const XMMRegister abcd = xmm0;
1507 const XMMRegister e0 = xmm1;
1508 const XMMRegister e1 = xmm2;
1509 const XMMRegister msg0 = xmm3;
1510
1511 const XMMRegister msg1 = xmm4;
1512 const XMMRegister msg2 = xmm5;
1513 const XMMRegister msg3 = xmm6;
1514 const XMMRegister shuf_mask = xmm7;
1515
1516 __ enter();
1517
1518 __ subptr(rsp, 4 * wordSize);
1519
1520 __ fast_sha1(abcd, e0, e1, msg0, msg1, msg2, msg3, shuf_mask,
1521 buf, state, ofs, limit, rsp, multi_block);
1522
1523 __ addptr(rsp, 4 * wordSize);
1524
1525 __ leave();
1526 __ ret(0);
1527
1528 return start;
1529 }
1530
1531 address StubGenerator::generate_pshuffle_byte_flip_mask() {
1532 __ align64();
1533 StubId stub_id = StubId::stubgen_pshuffle_byte_flip_mask_id;
1534 StubCodeMark mark(this, stub_id);
1535 address start = __ pc();
1536
1537 __ emit_data64(0x0405060700010203, relocInfo::none);
1538 __ emit_data64(0x0c0d0e0f08090a0b, relocInfo::none);
1539
1540 if (VM_Version::supports_avx2()) {
1541 __ emit_data64(0x0405060700010203, relocInfo::none); // second copy
1542 __ emit_data64(0x0c0d0e0f08090a0b, relocInfo::none);
1543 // _SHUF_00BA
1544 __ emit_data64(0x0b0a090803020100, relocInfo::none);
1545 __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
1546 __ emit_data64(0x0b0a090803020100, relocInfo::none);
1547 __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
1548 // _SHUF_DC00
1549 __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
1550 __ emit_data64(0x0b0a090803020100, relocInfo::none);
1551 __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
1552 __ emit_data64(0x0b0a090803020100, relocInfo::none);
1553 }
1554
1555 return start;
1556 }
1557
1558 //Mask for byte-swapping a couple of qwords in an XMM register using (v)pshufb.
1559 address StubGenerator::generate_pshuffle_byte_flip_mask_sha512() {
1560 __ align32();
1561 StubId stub_id = StubId::stubgen_pshuffle_byte_flip_mask_sha512_id;
1562 StubCodeMark mark(this, stub_id);
1563 address start = __ pc();
1564
1565 if (VM_Version::supports_avx2()) {
1566 __ emit_data64(0x0001020304050607, relocInfo::none); // PSHUFFLE_BYTE_FLIP_MASK
1567 __ emit_data64(0x08090a0b0c0d0e0f, relocInfo::none);
1568 __ emit_data64(0x1011121314151617, relocInfo::none);
1569 __ emit_data64(0x18191a1b1c1d1e1f, relocInfo::none);
1570 __ emit_data64(0x0000000000000000, relocInfo::none); //MASK_YMM_LO
1571 __ emit_data64(0x0000000000000000, relocInfo::none);
1572 __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
1573 __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
1574 }
1575
1576 return start;
1577 }
1578
1579 // ofs and limit are use for multi-block byte array.
1580 // int com.sun.security.provider.DigestBase.implCompressMultiBlock(byte[] b, int ofs, int limit)
1581 address StubGenerator::generate_sha256_implCompress(StubId stub_id) {
1582 bool multi_block;
1583 switch (stub_id) {
1584 case StubId::stubgen_sha256_implCompress_id:
1585 multi_block = false;
1586 break;
1587 case StubId::stubgen_sha256_implCompressMB_id:
1588 multi_block = true;
1589 break;
1590 default:
1591 ShouldNotReachHere();
1592 }
1593 assert(VM_Version::supports_sha() || VM_Version::supports_avx2(), "");
1594 __ align(CodeEntryAlignment);
1595 StubCodeMark mark(this, stub_id);
1596 address start = __ pc();
1597
1598 Register buf = c_rarg0;
1599 Register state = c_rarg1;
1600 Register ofs = c_rarg2;
1601 Register limit = c_rarg3;
1602
1603 const XMMRegister msg = xmm0;
1604 const XMMRegister state0 = xmm1;
1605 const XMMRegister state1 = xmm2;
1606 const XMMRegister msgtmp0 = xmm3;
1607
1608 const XMMRegister msgtmp1 = xmm4;
1609 const XMMRegister msgtmp2 = xmm5;
1610 const XMMRegister msgtmp3 = xmm6;
1611 const XMMRegister msgtmp4 = xmm7;
1612
1613 const XMMRegister shuf_mask = xmm8;
1614
1615 __ enter();
1616
1617 __ subptr(rsp, 4 * wordSize);
1618
1619 if (VM_Version::supports_sha()) {
1620 __ fast_sha256(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
1621 buf, state, ofs, limit, rsp, multi_block, shuf_mask);
1622 } else if (VM_Version::supports_avx2()) {
1623 __ sha256_AVX2(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
1624 buf, state, ofs, limit, rsp, multi_block, shuf_mask);
1625 }
1626 __ addptr(rsp, 4 * wordSize);
1627 __ vzeroupper();
1628 __ leave();
1629 __ ret(0);
1630
1631 return start;
1632 }
1633
1634 address StubGenerator::generate_sha512_implCompress(StubId stub_id) {
1635 bool multi_block;
1636 switch (stub_id) {
1637 case StubId::stubgen_sha512_implCompress_id:
1638 multi_block = false;
1639 break;
1640 case StubId::stubgen_sha512_implCompressMB_id:
1641 multi_block = true;
1642 break;
1643 default:
1644 ShouldNotReachHere();
1645 }
1646 assert(VM_Version::supports_avx2(), "");
1647 assert(VM_Version::supports_bmi2() || VM_Version::supports_sha512(), "");
1648 __ align(CodeEntryAlignment);
1649 StubCodeMark mark(this, stub_id);
1650 address start = __ pc();
1651
1652 Register buf = c_rarg0;
1653 Register state = c_rarg1;
1654 Register ofs = c_rarg2;
1655 Register limit = c_rarg3;
1656
1657 __ enter();
1658
1659 if (VM_Version::supports_sha512()) {
1660 __ sha512_update_ni_x1(state, buf, ofs, limit, multi_block);
1661 } else {
1662 const XMMRegister msg = xmm0;
1663 const XMMRegister state0 = xmm1;
1664 const XMMRegister state1 = xmm2;
1665 const XMMRegister msgtmp0 = xmm3;
1666 const XMMRegister msgtmp1 = xmm4;
1667 const XMMRegister msgtmp2 = xmm5;
1668 const XMMRegister msgtmp3 = xmm6;
1669 const XMMRegister msgtmp4 = xmm7;
1670
1671 const XMMRegister shuf_mask = xmm8;
1672 __ sha512_AVX2(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
1673 buf, state, ofs, limit, rsp, multi_block, shuf_mask);
1674 }
1675 __ vzeroupper();
1676 __ leave();
1677 __ ret(0);
1678
1679 return start;
1680 }
1681
1682 address StubGenerator::base64_shuffle_addr() {
1683 __ align64();
1684 StubId stub_id = StubId::stubgen_shuffle_base64_id;
1685 StubCodeMark mark(this, stub_id);
1686 address start = __ pc();
1687
1688 assert(((unsigned long long)start & 0x3f) == 0,
1689 "Alignment problem (0x%08llx)", (unsigned long long)start);
1690 __ emit_data64(0x0405030401020001, relocInfo::none);
1691 __ emit_data64(0x0a0b090a07080607, relocInfo::none);
1692 __ emit_data64(0x10110f100d0e0c0d, relocInfo::none);
1693 __ emit_data64(0x1617151613141213, relocInfo::none);
1694 __ emit_data64(0x1c1d1b1c191a1819, relocInfo::none);
1695 __ emit_data64(0x222321221f201e1f, relocInfo::none);
1696 __ emit_data64(0x2829272825262425, relocInfo::none);
1697 __ emit_data64(0x2e2f2d2e2b2c2a2b, relocInfo::none);
1698
1699 return start;
1700 }
1701
1702 address StubGenerator::base64_avx2_shuffle_addr() {
1703 __ align32();
1704 StubId stub_id = StubId::stubgen_avx2_shuffle_base64_id;
1705 StubCodeMark mark(this, stub_id);
1706 address start = __ pc();
1707
1708 __ emit_data64(0x0809070805060405, relocInfo::none);
1709 __ emit_data64(0x0e0f0d0e0b0c0a0b, relocInfo::none);
1710 __ emit_data64(0x0405030401020001, relocInfo::none);
1711 __ emit_data64(0x0a0b090a07080607, relocInfo::none);
1712
1713 return start;
1714 }
1715
1716 address StubGenerator::base64_avx2_input_mask_addr() {
1717 __ align32();
1718 StubId stub_id = StubId::stubgen_avx2_input_mask_base64_id;
1719 StubCodeMark mark(this, stub_id);
1720 address start = __ pc();
1721
1722 __ emit_data64(0x8000000000000000, relocInfo::none);
1723 __ emit_data64(0x8000000080000000, relocInfo::none);
1724 __ emit_data64(0x8000000080000000, relocInfo::none);
1725 __ emit_data64(0x8000000080000000, relocInfo::none);
1726
1727 return start;
1728 }
1729
1730 address StubGenerator::base64_avx2_lut_addr() {
1731 __ align32();
1732 StubId stub_id = StubId::stubgen_avx2_lut_base64_id;
1733 StubCodeMark mark(this, stub_id);
1734 address start = __ pc();
1735
1736 __ emit_data64(0xfcfcfcfcfcfc4741, relocInfo::none);
1737 __ emit_data64(0x0000f0edfcfcfcfc, relocInfo::none);
1738 __ emit_data64(0xfcfcfcfcfcfc4741, relocInfo::none);
1739 __ emit_data64(0x0000f0edfcfcfcfc, relocInfo::none);
1740
1741 // URL LUT
1742 __ emit_data64(0xfcfcfcfcfcfc4741, relocInfo::none);
1743 __ emit_data64(0x000020effcfcfcfc, relocInfo::none);
1744 __ emit_data64(0xfcfcfcfcfcfc4741, relocInfo::none);
1745 __ emit_data64(0x000020effcfcfcfc, relocInfo::none);
1746
1747 return start;
1748 }
1749
1750 address StubGenerator::base64_encoding_table_addr() {
1751 __ align64();
1752 StubId stub_id = StubId::stubgen_encoding_table_base64_id;
1753 StubCodeMark mark(this, stub_id);
1754 address start = __ pc();
1755
1756 assert(((unsigned long long)start & 0x3f) == 0, "Alignment problem (0x%08llx)", (unsigned long long)start);
1757 __ emit_data64(0x4847464544434241, relocInfo::none);
1758 __ emit_data64(0x504f4e4d4c4b4a49, relocInfo::none);
1759 __ emit_data64(0x5857565554535251, relocInfo::none);
1760 __ emit_data64(0x6665646362615a59, relocInfo::none);
1761 __ emit_data64(0x6e6d6c6b6a696867, relocInfo::none);
1762 __ emit_data64(0x767574737271706f, relocInfo::none);
1763 __ emit_data64(0x333231307a797877, relocInfo::none);
1764 __ emit_data64(0x2f2b393837363534, relocInfo::none);
1765
1766 // URL table
1767 __ emit_data64(0x4847464544434241, relocInfo::none);
1768 __ emit_data64(0x504f4e4d4c4b4a49, relocInfo::none);
1769 __ emit_data64(0x5857565554535251, relocInfo::none);
1770 __ emit_data64(0x6665646362615a59, relocInfo::none);
1771 __ emit_data64(0x6e6d6c6b6a696867, relocInfo::none);
1772 __ emit_data64(0x767574737271706f, relocInfo::none);
1773 __ emit_data64(0x333231307a797877, relocInfo::none);
1774 __ emit_data64(0x5f2d393837363534, relocInfo::none);
1775
1776 return start;
1777 }
1778
1779 // Code for generating Base64 encoding.
1780 // Intrinsic function prototype in Base64.java:
1781 // private void encodeBlock(byte[] src, int sp, int sl, byte[] dst, int dp,
1782 // boolean isURL) {
1783 address StubGenerator::generate_base64_encodeBlock()
1784 {
1785 __ align(CodeEntryAlignment);
1786 StubId stub_id = StubId::stubgen_base64_encodeBlock_id;
1787 StubCodeMark mark(this, stub_id);
1788 address start = __ pc();
1789
1790 __ enter();
1791
1792 // Save callee-saved registers before using them
1793 __ push_ppx(r12);
1794 __ push_ppx(r13);
1795 __ push_ppx(r14);
1796 __ push_ppx(r15);
1797
1798 // arguments
1799 const Register source = c_rarg0; // Source Array
1800 const Register start_offset = c_rarg1; // start offset
1801 const Register end_offset = c_rarg2; // end offset
1802 const Register dest = c_rarg3; // destination array
1803
1804 #ifndef _WIN64
1805 const Register dp = c_rarg4; // Position for writing to dest array
1806 const Register isURL = c_rarg5; // Base64 or URL character set
1807 #else
1808 const Address dp_mem(rbp, 6 * wordSize); // length is on stack on Win64
1809 const Address isURL_mem(rbp, 7 * wordSize);
1810 const Register isURL = r10; // pick the volatile windows register
1811 const Register dp = r12;
1812 __ movl(dp, dp_mem);
1813 __ movl(isURL, isURL_mem);
1814 #endif
1815
1816 const Register length = r14;
1817 const Register encode_table = r13;
1818 Label L_process3, L_exit, L_processdata, L_vbmiLoop, L_not512, L_32byteLoop;
1819
1820 // calculate length from offsets
1821 __ movl(length, end_offset);
1822 __ subl(length, start_offset);
1823 __ jcc(Assembler::lessEqual, L_exit);
1824
1825 // Code for 512-bit VBMI encoding. Encodes 48 input bytes into 64
1826 // output bytes. We read 64 input bytes and ignore the last 16, so be
1827 // sure not to read past the end of the input buffer.
1828 if (VM_Version::supports_avx512_vbmi()) {
1829 __ cmpl(length, 64); // Do not overrun input buffer.
1830 __ jcc(Assembler::below, L_not512);
1831
1832 __ shll(isURL, 6); // index into decode table based on isURL
1833 __ lea(encode_table, ExternalAddress(StubRoutines::x86::base64_encoding_table_addr()));
1834 __ addptr(encode_table, isURL);
1835 __ shrl(isURL, 6); // restore isURL
1836
1837 __ mov64(rax, 0x3036242a1016040aull); // Shifts
1838 __ evmovdquq(xmm3, ExternalAddress(StubRoutines::x86::base64_shuffle_addr()), Assembler::AVX_512bit, r15);
1839 __ evmovdquq(xmm2, Address(encode_table, 0), Assembler::AVX_512bit);
1840 __ evpbroadcastq(xmm1, rax, Assembler::AVX_512bit);
1841
1842 __ align32();
1843 __ BIND(L_vbmiLoop);
1844
1845 __ vpermb(xmm0, xmm3, Address(source, start_offset), Assembler::AVX_512bit);
1846 __ subl(length, 48);
1847
1848 // Put the input bytes into the proper lanes for writing, then
1849 // encode them.
1850 __ evpmultishiftqb(xmm0, xmm1, xmm0, Assembler::AVX_512bit);
1851 __ vpermb(xmm0, xmm0, xmm2, Assembler::AVX_512bit);
1852
1853 // Write to destination
1854 __ evmovdquq(Address(dest, dp), xmm0, Assembler::AVX_512bit);
1855
1856 __ addptr(dest, 64);
1857 __ addptr(source, 48);
1858 __ cmpl(length, 64);
1859 __ jcc(Assembler::aboveEqual, L_vbmiLoop);
1860
1861 __ vzeroupper();
1862 }
1863
1864 __ BIND(L_not512);
1865 if (VM_Version::supports_avx2()) {
1866 /*
1867 ** This AVX2 encoder is based off the paper at:
1868 ** https://dl.acm.org/doi/10.1145/3132709
1869 **
1870 ** We use AVX2 SIMD instructions to encode 24 bytes into 32
1871 ** output bytes.
1872 **
1873 */
1874 // Lengths under 32 bytes are done with scalar routine
1875 __ cmpl(length, 31);
1876 __ jcc(Assembler::belowEqual, L_process3);
1877
1878 // Set up supporting constant table data
1879 __ vmovdqu(xmm9, ExternalAddress(StubRoutines::x86::base64_avx2_shuffle_addr()), rax);
1880 // 6-bit mask for 2nd and 4th (and multiples) 6-bit values
1881 __ movl(rax, 0x0fc0fc00);
1882 __ movdl(xmm8, rax);
1883 __ vmovdqu(xmm1, ExternalAddress(StubRoutines::x86::base64_avx2_input_mask_addr()), rax);
1884 __ vpbroadcastd(xmm8, xmm8, Assembler::AVX_256bit);
1885
1886 // Multiplication constant for "shifting" right by 6 and 10
1887 // bits
1888 __ movl(rax, 0x04000040);
1889
1890 __ subl(length, 24);
1891 __ movdl(xmm7, rax);
1892 __ vpbroadcastd(xmm7, xmm7, Assembler::AVX_256bit);
1893
1894 // For the first load, we mask off reading of the first 4
1895 // bytes into the register. This is so we can get 4 3-byte
1896 // chunks into each lane of the register, avoiding having to
1897 // handle end conditions. We then shuffle these bytes into a
1898 // specific order so that manipulation is easier.
1899 //
1900 // The initial read loads the XMM register like this:
1901 //
1902 // Lower 128-bit lane:
1903 // +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
1904 // | XX | XX | XX | XX | A0 | A1 | A2 | B0 | B1 | B2 | C0 | C1
1905 // | C2 | D0 | D1 | D2 |
1906 // +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
1907 //
1908 // Upper 128-bit lane:
1909 // +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
1910 // | E0 | E1 | E2 | F0 | F1 | F2 | G0 | G1 | G2 | H0 | H1 | H2
1911 // | XX | XX | XX | XX |
1912 // +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
1913 //
1914 // Where A0 is the first input byte, B0 is the fourth, etc.
1915 // The alphabetical significance denotes the 3 bytes to be
1916 // consumed and encoded into 4 bytes.
1917 //
1918 // We then shuffle the register so each 32-bit word contains
1919 // the sequence:
1920 // A1 A0 A2 A1, B1, B0, B2, B1, etc.
1921 // Each of these byte sequences are then manipulated into 4
1922 // 6-bit values ready for encoding.
1923 //
1924 // If we focus on one set of 3-byte chunks, changing the
1925 // nomenclature such that A0 => a, A1 => b, and A2 => c, we
1926 // shuffle such that each 24-bit chunk contains:
1927 //
1928 // b7 b6 b5 b4 b3 b2 b1 b0 | a7 a6 a5 a4 a3 a2 a1 a0 | c7 c6
1929 // c5 c4 c3 c2 c1 c0 | b7 b6 b5 b4 b3 b2 b1 b0
1930 // Explain this step.
1931 // b3 b2 b1 b0 c5 c4 c3 c2 | c1 c0 d5 d4 d3 d2 d1 d0 | a5 a4
1932 // a3 a2 a1 a0 b5 b4 | b3 b2 b1 b0 c5 c4 c3 c2
1933 //
1934 // W first and off all but bits 4-9 and 16-21 (c5..c0 and
1935 // a5..a0) and shift them using a vector multiplication
1936 // operation (vpmulhuw) which effectively shifts c right by 6
1937 // bits and a right by 10 bits. We similarly mask bits 10-15
1938 // (d5..d0) and 22-27 (b5..b0) and shift them left by 8 and 4
1939 // bits respectively. This is done using vpmullw. We end up
1940 // with 4 6-bit values, thus splitting the 3 input bytes,
1941 // ready for encoding:
1942 // 0 0 d5..d0 0 0 c5..c0 0 0 b5..b0 0 0 a5..a0
1943 //
1944 // For translation, we recognize that there are 5 distinct
1945 // ranges of legal Base64 characters as below:
1946 //
1947 // +-------------+-------------+------------+
1948 // | 6-bit value | ASCII range | offset |
1949 // +-------------+-------------+------------+
1950 // | 0..25 | A..Z | 65 |
1951 // | 26..51 | a..z | 71 |
1952 // | 52..61 | 0..9 | -4 |
1953 // | 62 | + or - | -19 or -17 |
1954 // | 63 | / or _ | -16 or 32 |
1955 // +-------------+-------------+------------+
1956 //
1957 // We note that vpshufb does a parallel lookup in a
1958 // destination register using the lower 4 bits of bytes from a
1959 // source register. If we use a saturated subtraction and
1960 // subtract 51 from each 6-bit value, bytes from [0,51]
1961 // saturate to 0, and [52,63] map to a range of [1,12]. We
1962 // distinguish the [0,25] and [26,51] ranges by assigning a
1963 // value of 13 for all 6-bit values less than 26. We end up
1964 // with:
1965 //
1966 // +-------------+-------------+------------+
1967 // | 6-bit value | Reduced | offset |
1968 // +-------------+-------------+------------+
1969 // | 0..25 | 13 | 65 |
1970 // | 26..51 | 0 | 71 |
1971 // | 52..61 | 0..9 | -4 |
1972 // | 62 | 11 | -19 or -17 |
1973 // | 63 | 12 | -16 or 32 |
1974 // +-------------+-------------+------------+
1975 //
1976 // We then use a final vpshufb to add the appropriate offset,
1977 // translating the bytes.
1978 //
1979 // Load input bytes - only 28 bytes. Mask the first load to
1980 // not load into the full register.
1981 __ vpmaskmovd(xmm1, xmm1, Address(source, start_offset, Address::times_1, -4), Assembler::AVX_256bit);
1982
1983 // Move 3-byte chunks of input (12 bytes) into 16 bytes,
1984 // ordering by:
1985 // 1, 0, 2, 1; 4, 3, 5, 4; etc. This groups 6-bit chunks
1986 // for easy masking
1987 __ vpshufb(xmm1, xmm1, xmm9, Assembler::AVX_256bit);
1988
1989 __ addl(start_offset, 24);
1990
1991 // Load masking register for first and third (and multiples)
1992 // 6-bit values.
1993 __ movl(rax, 0x003f03f0);
1994 __ movdl(xmm6, rax);
1995 __ vpbroadcastd(xmm6, xmm6, Assembler::AVX_256bit);
1996 // Multiplication constant for "shifting" left by 4 and 8 bits
1997 __ movl(rax, 0x01000010);
1998 __ movdl(xmm5, rax);
1999 __ vpbroadcastd(xmm5, xmm5, Assembler::AVX_256bit);
2000
2001 // Isolate 6-bit chunks of interest
2002 __ vpand(xmm0, xmm8, xmm1, Assembler::AVX_256bit);
2003
2004 // Load constants for encoding
2005 __ movl(rax, 0x19191919);
2006 __ movdl(xmm3, rax);
2007 __ vpbroadcastd(xmm3, xmm3, Assembler::AVX_256bit);
2008 __ movl(rax, 0x33333333);
2009 __ movdl(xmm4, rax);
2010 __ vpbroadcastd(xmm4, xmm4, Assembler::AVX_256bit);
2011
2012 // Shift output bytes 0 and 2 into proper lanes
2013 __ vpmulhuw(xmm2, xmm0, xmm7, Assembler::AVX_256bit);
2014
2015 // Mask and shift output bytes 1 and 3 into proper lanes and
2016 // combine
2017 __ vpand(xmm0, xmm6, xmm1, Assembler::AVX_256bit);
2018 __ vpmullw(xmm0, xmm5, xmm0, Assembler::AVX_256bit);
2019 __ vpor(xmm0, xmm0, xmm2, Assembler::AVX_256bit);
2020
2021 // Find out which are 0..25. This indicates which input
2022 // values fall in the range of 'A'-'Z', which require an
2023 // additional offset (see comments above)
2024 __ vpcmpgtb(xmm2, xmm0, xmm3, Assembler::AVX_256bit);
2025 __ vpsubusb(xmm1, xmm0, xmm4, Assembler::AVX_256bit);
2026 __ vpsubb(xmm1, xmm1, xmm2, Assembler::AVX_256bit);
2027
2028 // Load the proper lookup table
2029 __ lea(r11, ExternalAddress(StubRoutines::x86::base64_avx2_lut_addr()));
2030 __ movl(r15, isURL);
2031 __ shll(r15, 5);
2032 __ vmovdqu(xmm2, Address(r11, r15));
2033
2034 // Shuffle the offsets based on the range calculation done
2035 // above. This allows us to add the correct offset to the
2036 // 6-bit value corresponding to the range documented above.
2037 __ vpshufb(xmm1, xmm2, xmm1, Assembler::AVX_256bit);
2038 __ vpaddb(xmm0, xmm1, xmm0, Assembler::AVX_256bit);
2039
2040 // Store the encoded bytes
2041 __ vmovdqu(Address(dest, dp), xmm0);
2042 __ addl(dp, 32);
2043
2044 __ cmpl(length, 31);
2045 __ jcc(Assembler::belowEqual, L_process3);
2046
2047 __ align32();
2048 __ BIND(L_32byteLoop);
2049
2050 // Get next 32 bytes
2051 __ vmovdqu(xmm1, Address(source, start_offset, Address::times_1, -4));
2052
2053 __ subl(length, 24);
2054 __ addl(start_offset, 24);
2055
2056 // This logic is identical to the above, with only constant
2057 // register loads removed. Shuffle the input, mask off 6-bit
2058 // chunks, shift them into place, then add the offset to
2059 // encode.
2060 __ vpshufb(xmm1, xmm1, xmm9, Assembler::AVX_256bit);
2061
2062 __ vpand(xmm0, xmm8, xmm1, Assembler::AVX_256bit);
2063 __ vpmulhuw(xmm10, xmm0, xmm7, Assembler::AVX_256bit);
2064 __ vpand(xmm0, xmm6, xmm1, Assembler::AVX_256bit);
2065 __ vpmullw(xmm0, xmm5, xmm0, Assembler::AVX_256bit);
2066 __ vpor(xmm0, xmm0, xmm10, Assembler::AVX_256bit);
2067 __ vpcmpgtb(xmm10, xmm0, xmm3, Assembler::AVX_256bit);
2068 __ vpsubusb(xmm1, xmm0, xmm4, Assembler::AVX_256bit);
2069 __ vpsubb(xmm1, xmm1, xmm10, Assembler::AVX_256bit);
2070 __ vpshufb(xmm1, xmm2, xmm1, Assembler::AVX_256bit);
2071 __ vpaddb(xmm0, xmm1, xmm0, Assembler::AVX_256bit);
2072
2073 // Store the encoded bytes
2074 __ vmovdqu(Address(dest, dp), xmm0);
2075 __ addl(dp, 32);
2076
2077 __ cmpl(length, 31);
2078 __ jcc(Assembler::above, L_32byteLoop);
2079
2080 __ BIND(L_process3);
2081 __ vzeroupper();
2082 } else {
2083 __ BIND(L_process3);
2084 }
2085
2086 __ cmpl(length, 3);
2087 __ jcc(Assembler::below, L_exit);
2088
2089 // Load the encoding table based on isURL
2090 __ lea(r11, ExternalAddress(StubRoutines::x86::base64_encoding_table_addr()));
2091 __ movl(r15, isURL);
2092 __ shll(r15, 6);
2093 __ addptr(r11, r15);
2094
2095 __ BIND(L_processdata);
2096
2097 // Load 3 bytes
2098 __ load_unsigned_byte(r15, Address(source, start_offset));
2099 __ load_unsigned_byte(r10, Address(source, start_offset, Address::times_1, 1));
2100 __ load_unsigned_byte(r13, Address(source, start_offset, Address::times_1, 2));
2101
2102 // Build a 32-bit word with bytes 1, 2, 0, 1
2103 __ movl(rax, r10);
2104 __ shll(r10, 24);
2105 __ orl(rax, r10);
2106
2107 __ subl(length, 3);
2108
2109 __ shll(r15, 8);
2110 __ shll(r13, 16);
2111 __ orl(rax, r15);
2112
2113 __ addl(start_offset, 3);
2114
2115 __ orl(rax, r13);
2116 // At this point, rax contains | byte1 | byte2 | byte0 | byte1
2117 // r13 has byte2 << 16 - need low-order 6 bits to translate.
2118 // This translated byte is the fourth output byte.
2119 __ shrl(r13, 16);
2120 __ andl(r13, 0x3f);
2121
2122 // The high-order 6 bits of r15 (byte0) is translated.
2123 // The translated byte is the first output byte.
2124 __ shrl(r15, 10);
2125
2126 __ load_unsigned_byte(r13, Address(r11, r13));
2127 __ load_unsigned_byte(r15, Address(r11, r15));
2128
2129 __ movb(Address(dest, dp, Address::times_1, 3), r13);
2130
2131 // Extract high-order 4 bits of byte1 and low-order 2 bits of byte0.
2132 // This translated byte is the second output byte.
2133 __ shrl(rax, 4);
2134 __ movl(r10, rax);
2135 __ andl(rax, 0x3f);
2136
2137 __ movb(Address(dest, dp, Address::times_1, 0), r15);
2138
2139 __ load_unsigned_byte(rax, Address(r11, rax));
2140
2141 // Extract low-order 2 bits of byte1 and high-order 4 bits of byte2.
2142 // This translated byte is the third output byte.
2143 __ shrl(r10, 18);
2144 __ andl(r10, 0x3f);
2145
2146 __ load_unsigned_byte(r10, Address(r11, r10));
2147
2148 __ movb(Address(dest, dp, Address::times_1, 1), rax);
2149 __ movb(Address(dest, dp, Address::times_1, 2), r10);
2150
2151 __ addl(dp, 4);
2152 __ cmpl(length, 3);
2153 __ jcc(Assembler::aboveEqual, L_processdata);
2154
2155 __ BIND(L_exit);
2156 __ pop_ppx(r15);
2157 __ pop_ppx(r14);
2158 __ pop_ppx(r13);
2159 __ pop_ppx(r12);
2160 __ leave();
2161 __ ret(0);
2162
2163 return start;
2164 }
2165
2166 // base64 AVX512vbmi tables
2167 address StubGenerator::base64_vbmi_lookup_lo_addr() {
2168 __ align64();
2169 StubId stub_id = StubId::stubgen_lookup_lo_base64_id;
2170 StubCodeMark mark(this, stub_id);
2171 address start = __ pc();
2172
2173 assert(((unsigned long long)start & 0x3f) == 0,
2174 "Alignment problem (0x%08llx)", (unsigned long long)start);
2175 __ emit_data64(0x8080808080808080, relocInfo::none);
2176 __ emit_data64(0x8080808080808080, relocInfo::none);
2177 __ emit_data64(0x8080808080808080, relocInfo::none);
2178 __ emit_data64(0x8080808080808080, relocInfo::none);
2179 __ emit_data64(0x8080808080808080, relocInfo::none);
2180 __ emit_data64(0x3f8080803e808080, relocInfo::none);
2181 __ emit_data64(0x3b3a393837363534, relocInfo::none);
2182 __ emit_data64(0x8080808080803d3c, relocInfo::none);
2183
2184 return start;
2185 }
2186
2187 address StubGenerator::base64_vbmi_lookup_hi_addr() {
2188 __ align64();
2189 StubId stub_id = StubId::stubgen_lookup_hi_base64_id;
2190 StubCodeMark mark(this, stub_id);
2191 address start = __ pc();
2192
2193 assert(((unsigned long long)start & 0x3f) == 0,
2194 "Alignment problem (0x%08llx)", (unsigned long long)start);
2195 __ emit_data64(0x0605040302010080, relocInfo::none);
2196 __ emit_data64(0x0e0d0c0b0a090807, relocInfo::none);
2197 __ emit_data64(0x161514131211100f, relocInfo::none);
2198 __ emit_data64(0x8080808080191817, relocInfo::none);
2199 __ emit_data64(0x201f1e1d1c1b1a80, relocInfo::none);
2200 __ emit_data64(0x2827262524232221, relocInfo::none);
2201 __ emit_data64(0x302f2e2d2c2b2a29, relocInfo::none);
2202 __ emit_data64(0x8080808080333231, relocInfo::none);
2203
2204 return start;
2205 }
2206 address StubGenerator::base64_vbmi_lookup_lo_url_addr() {
2207 __ align64();
2208 StubId stub_id = StubId::stubgen_lookup_lo_base64url_id;
2209 StubCodeMark mark(this, stub_id);
2210 address start = __ pc();
2211
2212 assert(((unsigned long long)start & 0x3f) == 0,
2213 "Alignment problem (0x%08llx)", (unsigned long long)start);
2214 __ emit_data64(0x8080808080808080, relocInfo::none);
2215 __ emit_data64(0x8080808080808080, relocInfo::none);
2216 __ emit_data64(0x8080808080808080, relocInfo::none);
2217 __ emit_data64(0x8080808080808080, relocInfo::none);
2218 __ emit_data64(0x8080808080808080, relocInfo::none);
2219 __ emit_data64(0x80803e8080808080, relocInfo::none);
2220 __ emit_data64(0x3b3a393837363534, relocInfo::none);
2221 __ emit_data64(0x8080808080803d3c, relocInfo::none);
2222
2223 return start;
2224 }
2225
2226 address StubGenerator::base64_vbmi_lookup_hi_url_addr() {
2227 __ align64();
2228 StubId stub_id = StubId::stubgen_lookup_hi_base64url_id;
2229 StubCodeMark mark(this, stub_id);
2230 address start = __ pc();
2231
2232 assert(((unsigned long long)start & 0x3f) == 0,
2233 "Alignment problem (0x%08llx)", (unsigned long long)start);
2234 __ emit_data64(0x0605040302010080, relocInfo::none);
2235 __ emit_data64(0x0e0d0c0b0a090807, relocInfo::none);
2236 __ emit_data64(0x161514131211100f, relocInfo::none);
2237 __ emit_data64(0x3f80808080191817, relocInfo::none);
2238 __ emit_data64(0x201f1e1d1c1b1a80, relocInfo::none);
2239 __ emit_data64(0x2827262524232221, relocInfo::none);
2240 __ emit_data64(0x302f2e2d2c2b2a29, relocInfo::none);
2241 __ emit_data64(0x8080808080333231, relocInfo::none);
2242
2243 return start;
2244 }
2245
2246 address StubGenerator::base64_vbmi_pack_vec_addr() {
2247 __ align64();
2248 StubId stub_id = StubId::stubgen_pack_vec_base64_id;
2249 StubCodeMark mark(this, stub_id);
2250 address start = __ pc();
2251
2252 assert(((unsigned long long)start & 0x3f) == 0,
2253 "Alignment problem (0x%08llx)", (unsigned long long)start);
2254 __ emit_data64(0x090a040506000102, relocInfo::none);
2255 __ emit_data64(0x161011120c0d0e08, relocInfo::none);
2256 __ emit_data64(0x1c1d1e18191a1415, relocInfo::none);
2257 __ emit_data64(0x292a242526202122, relocInfo::none);
2258 __ emit_data64(0x363031322c2d2e28, relocInfo::none);
2259 __ emit_data64(0x3c3d3e38393a3435, relocInfo::none);
2260 __ emit_data64(0x0000000000000000, relocInfo::none);
2261 __ emit_data64(0x0000000000000000, relocInfo::none);
2262
2263 return start;
2264 }
2265
2266 address StubGenerator::base64_vbmi_join_0_1_addr() {
2267 __ align64();
2268 StubId stub_id = StubId::stubgen_join_0_1_base64_id;
2269 StubCodeMark mark(this, stub_id);
2270 address start = __ pc();
2271
2272 assert(((unsigned long long)start & 0x3f) == 0,
2273 "Alignment problem (0x%08llx)", (unsigned long long)start);
2274 __ emit_data64(0x090a040506000102, relocInfo::none);
2275 __ emit_data64(0x161011120c0d0e08, relocInfo::none);
2276 __ emit_data64(0x1c1d1e18191a1415, relocInfo::none);
2277 __ emit_data64(0x292a242526202122, relocInfo::none);
2278 __ emit_data64(0x363031322c2d2e28, relocInfo::none);
2279 __ emit_data64(0x3c3d3e38393a3435, relocInfo::none);
2280 __ emit_data64(0x494a444546404142, relocInfo::none);
2281 __ emit_data64(0x565051524c4d4e48, relocInfo::none);
2282
2283 return start;
2284 }
2285
2286 address StubGenerator::base64_vbmi_join_1_2_addr() {
2287 __ align64();
2288 StubId stub_id = StubId::stubgen_join_1_2_base64_id;
2289 StubCodeMark mark(this, stub_id);
2290 address start = __ pc();
2291
2292 assert(((unsigned long long)start & 0x3f) == 0,
2293 "Alignment problem (0x%08llx)", (unsigned long long)start);
2294 __ emit_data64(0x1c1d1e18191a1415, relocInfo::none);
2295 __ emit_data64(0x292a242526202122, relocInfo::none);
2296 __ emit_data64(0x363031322c2d2e28, relocInfo::none);
2297 __ emit_data64(0x3c3d3e38393a3435, relocInfo::none);
2298 __ emit_data64(0x494a444546404142, relocInfo::none);
2299 __ emit_data64(0x565051524c4d4e48, relocInfo::none);
2300 __ emit_data64(0x5c5d5e58595a5455, relocInfo::none);
2301 __ emit_data64(0x696a646566606162, relocInfo::none);
2302
2303 return start;
2304 }
2305
2306 address StubGenerator::base64_vbmi_join_2_3_addr() {
2307 __ align64();
2308 StubId stub_id = StubId::stubgen_join_2_3_base64_id;
2309 StubCodeMark mark(this, stub_id);
2310 address start = __ pc();
2311
2312 assert(((unsigned long long)start & 0x3f) == 0,
2313 "Alignment problem (0x%08llx)", (unsigned long long)start);
2314 __ emit_data64(0x363031322c2d2e28, relocInfo::none);
2315 __ emit_data64(0x3c3d3e38393a3435, relocInfo::none);
2316 __ emit_data64(0x494a444546404142, relocInfo::none);
2317 __ emit_data64(0x565051524c4d4e48, relocInfo::none);
2318 __ emit_data64(0x5c5d5e58595a5455, relocInfo::none);
2319 __ emit_data64(0x696a646566606162, relocInfo::none);
2320 __ emit_data64(0x767071726c6d6e68, relocInfo::none);
2321 __ emit_data64(0x7c7d7e78797a7475, relocInfo::none);
2322
2323 return start;
2324 }
2325
2326 address StubGenerator::base64_AVX2_decode_tables_addr() {
2327 __ align64();
2328 StubId stub_id = StubId::stubgen_avx2_decode_tables_base64_id;
2329 StubCodeMark mark(this, stub_id);
2330 address start = __ pc();
2331
2332 assert(((unsigned long long)start & 0x3f) == 0,
2333 "Alignment problem (0x%08llx)", (unsigned long long)start);
2334 __ emit_data(0x2f2f2f2f, relocInfo::none, 0);
2335 __ emit_data(0x5f5f5f5f, relocInfo::none, 0); // for URL
2336
2337 __ emit_data(0xffffffff, relocInfo::none, 0);
2338 __ emit_data(0xfcfcfcfc, relocInfo::none, 0); // for URL
2339
2340 // Permute table
2341 __ emit_data64(0x0000000100000000, relocInfo::none);
2342 __ emit_data64(0x0000000400000002, relocInfo::none);
2343 __ emit_data64(0x0000000600000005, relocInfo::none);
2344 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2345
2346 // Shuffle table
2347 __ emit_data64(0x090a040506000102, relocInfo::none);
2348 __ emit_data64(0xffffffff0c0d0e08, relocInfo::none);
2349 __ emit_data64(0x090a040506000102, relocInfo::none);
2350 __ emit_data64(0xffffffff0c0d0e08, relocInfo::none);
2351
2352 // merge table
2353 __ emit_data(0x01400140, relocInfo::none, 0);
2354
2355 // merge multiplier
2356 __ emit_data(0x00011000, relocInfo::none, 0);
2357
2358 return start;
2359 }
2360
2361 address StubGenerator::base64_AVX2_decode_LUT_tables_addr() {
2362 __ align64();
2363 StubId stub_id = StubId::stubgen_avx2_decode_lut_tables_base64_id;
2364 StubCodeMark mark(this, stub_id);
2365 address start = __ pc();
2366
2367 assert(((unsigned long long)start & 0x3f) == 0,
2368 "Alignment problem (0x%08llx)", (unsigned long long)start);
2369 // lut_lo
2370 __ emit_data64(0x1111111111111115, relocInfo::none);
2371 __ emit_data64(0x1a1b1b1b1a131111, relocInfo::none);
2372 __ emit_data64(0x1111111111111115, relocInfo::none);
2373 __ emit_data64(0x1a1b1b1b1a131111, relocInfo::none);
2374
2375 // lut_roll
2376 __ emit_data64(0xb9b9bfbf04131000, relocInfo::none);
2377 __ emit_data64(0x0000000000000000, relocInfo::none);
2378 __ emit_data64(0xb9b9bfbf04131000, relocInfo::none);
2379 __ emit_data64(0x0000000000000000, relocInfo::none);
2380
2381 // lut_lo URL
2382 __ emit_data64(0x1111111111111115, relocInfo::none);
2383 __ emit_data64(0x1b1b1a1b1b131111, relocInfo::none);
2384 __ emit_data64(0x1111111111111115, relocInfo::none);
2385 __ emit_data64(0x1b1b1a1b1b131111, relocInfo::none);
2386
2387 // lut_roll URL
2388 __ emit_data64(0xb9b9bfbf0411e000, relocInfo::none);
2389 __ emit_data64(0x0000000000000000, relocInfo::none);
2390 __ emit_data64(0xb9b9bfbf0411e000, relocInfo::none);
2391 __ emit_data64(0x0000000000000000, relocInfo::none);
2392
2393 // lut_hi
2394 __ emit_data64(0x0804080402011010, relocInfo::none);
2395 __ emit_data64(0x1010101010101010, relocInfo::none);
2396 __ emit_data64(0x0804080402011010, relocInfo::none);
2397 __ emit_data64(0x1010101010101010, relocInfo::none);
2398
2399 return start;
2400 }
2401
2402 address StubGenerator::base64_decoding_table_addr() {
2403 StubId stub_id = StubId::stubgen_decoding_table_base64_id;
2404 StubCodeMark mark(this, stub_id);
2405 address start = __ pc();
2406
2407 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2408 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2409 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2410 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2411 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2412 __ emit_data64(0x3fffffff3effffff, relocInfo::none);
2413 __ emit_data64(0x3b3a393837363534, relocInfo::none);
2414 __ emit_data64(0xffffffffffff3d3c, relocInfo::none);
2415 __ emit_data64(0x06050403020100ff, relocInfo::none);
2416 __ emit_data64(0x0e0d0c0b0a090807, relocInfo::none);
2417 __ emit_data64(0x161514131211100f, relocInfo::none);
2418 __ emit_data64(0xffffffffff191817, relocInfo::none);
2419 __ emit_data64(0x201f1e1d1c1b1aff, relocInfo::none);
2420 __ emit_data64(0x2827262524232221, relocInfo::none);
2421 __ emit_data64(0x302f2e2d2c2b2a29, relocInfo::none);
2422 __ emit_data64(0xffffffffff333231, relocInfo::none);
2423 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2424 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2425 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2426 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2427 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2428 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2429 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2430 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2431 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2432 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2433 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2434 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2435 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2436 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2437 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2438 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2439
2440 // URL table
2441 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2442 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2443 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2444 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2445 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2446 __ emit_data64(0xffff3effffffffff, relocInfo::none);
2447 __ emit_data64(0x3b3a393837363534, relocInfo::none);
2448 __ emit_data64(0xffffffffffff3d3c, relocInfo::none);
2449 __ emit_data64(0x06050403020100ff, relocInfo::none);
2450 __ emit_data64(0x0e0d0c0b0a090807, relocInfo::none);
2451 __ emit_data64(0x161514131211100f, relocInfo::none);
2452 __ emit_data64(0x3fffffffff191817, relocInfo::none);
2453 __ emit_data64(0x201f1e1d1c1b1aff, relocInfo::none);
2454 __ emit_data64(0x2827262524232221, relocInfo::none);
2455 __ emit_data64(0x302f2e2d2c2b2a29, relocInfo::none);
2456 __ emit_data64(0xffffffffff333231, relocInfo::none);
2457 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2458 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2459 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2460 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2461 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2462 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2463 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2464 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2465 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2466 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2467 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2468 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2469 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2470 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2471 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2472 __ emit_data64(0xffffffffffffffff, relocInfo::none);
2473
2474 return start;
2475 }
2476
2477
2478 // Code for generating Base64 decoding.
2479 //
2480 // Based on the article (and associated code) from https://arxiv.org/abs/1910.05109.
2481 //
2482 // Intrinsic function prototype in Base64.java:
2483 // private void decodeBlock(byte[] src, int sp, int sl, byte[] dst, int dp, boolean isURL, isMIME) {
2484 address StubGenerator::generate_base64_decodeBlock() {
2485 __ align(CodeEntryAlignment);
2486 StubId stub_id = StubId::stubgen_base64_decodeBlock_id;
2487 StubCodeMark mark(this, stub_id);
2488 address start = __ pc();
2489
2490 __ enter();
2491
2492 // Save callee-saved registers before using them
2493 __ push_ppx(r12);
2494 __ push_ppx(r13);
2495 __ push_ppx(r14);
2496 __ push_ppx(r15);
2497 __ push_ppx(rbx);
2498
2499 // arguments
2500 const Register source = c_rarg0; // Source Array
2501 const Register start_offset = c_rarg1; // start offset
2502 const Register end_offset = c_rarg2; // end offset
2503 const Register dest = c_rarg3; // destination array
2504 const Register isMIME = rbx;
2505
2506 #ifndef _WIN64
2507 const Register dp = c_rarg4; // Position for writing to dest array
2508 const Register isURL = c_rarg5;// Base64 or URL character set
2509 __ movl(isMIME, Address(rbp, 2 * wordSize));
2510 #else
2511 const Address dp_mem(rbp, 6 * wordSize); // length is on stack on Win64
2512 const Address isURL_mem(rbp, 7 * wordSize);
2513 const Register isURL = r10; // pick the volatile windows register
2514 const Register dp = r12;
2515 __ movl(dp, dp_mem);
2516 __ movl(isURL, isURL_mem);
2517 __ movl(isMIME, Address(rbp, 8 * wordSize));
2518 #endif
2519
2520 const XMMRegister lookup_lo = xmm5;
2521 const XMMRegister lookup_hi = xmm6;
2522 const XMMRegister errorvec = xmm7;
2523 const XMMRegister pack16_op = xmm9;
2524 const XMMRegister pack32_op = xmm8;
2525 const XMMRegister input0 = xmm3;
2526 const XMMRegister input1 = xmm20;
2527 const XMMRegister input2 = xmm21;
2528 const XMMRegister input3 = xmm19;
2529 const XMMRegister join01 = xmm12;
2530 const XMMRegister join12 = xmm11;
2531 const XMMRegister join23 = xmm10;
2532 const XMMRegister translated0 = xmm2;
2533 const XMMRegister translated1 = xmm1;
2534 const XMMRegister translated2 = xmm0;
2535 const XMMRegister translated3 = xmm4;
2536
2537 const XMMRegister merged0 = xmm2;
2538 const XMMRegister merged1 = xmm1;
2539 const XMMRegister merged2 = xmm0;
2540 const XMMRegister merged3 = xmm4;
2541 const XMMRegister merge_ab_bc0 = xmm2;
2542 const XMMRegister merge_ab_bc1 = xmm1;
2543 const XMMRegister merge_ab_bc2 = xmm0;
2544 const XMMRegister merge_ab_bc3 = xmm4;
2545
2546 const XMMRegister pack24bits = xmm4;
2547
2548 const Register length = r14;
2549 const Register output_size = r13;
2550 const Register output_mask = r15;
2551 const KRegister input_mask = k1;
2552
2553 const XMMRegister input_initial_valid_b64 = xmm0;
2554 const XMMRegister tmp = xmm10;
2555 const XMMRegister mask = xmm0;
2556 const XMMRegister invalid_b64 = xmm1;
2557
2558 Label L_process256, L_process64, L_process64Loop, L_exit, L_processdata, L_loadURL;
2559 Label L_continue, L_finalBit, L_padding, L_donePadding, L_bruteForce;
2560 Label L_forceLoop, L_bottomLoop, L_checkMIME, L_exit_no_vzero, L_lastChunk;
2561
2562 // calculate length from offsets
2563 __ movl(length, end_offset);
2564 __ subl(length, start_offset);
2565 __ push_ppx(dest); // Save for return value calc
2566
2567 // If AVX512 VBMI not supported, just compile non-AVX code
2568 if(VM_Version::supports_avx512_vbmi() &&
2569 VM_Version::supports_avx512bw()) {
2570 __ cmpl(length, 31); // 32-bytes is break-even for AVX-512
2571 __ jcc(Assembler::lessEqual, L_lastChunk);
2572
2573 __ cmpl(isMIME, 0);
2574 __ jcc(Assembler::notEqual, L_lastChunk);
2575
2576 // Load lookup tables based on isURL
2577 __ cmpl(isURL, 0);
2578 __ jcc(Assembler::notZero, L_loadURL);
2579
2580 __ evmovdquq(lookup_lo, ExternalAddress(StubRoutines::x86::base64_vbmi_lookup_lo_addr()), Assembler::AVX_512bit, r13);
2581 __ evmovdquq(lookup_hi, ExternalAddress(StubRoutines::x86::base64_vbmi_lookup_hi_addr()), Assembler::AVX_512bit, r13);
2582
2583 __ BIND(L_continue);
2584
2585 __ movl(r15, 0x01400140);
2586 __ evpbroadcastd(pack16_op, r15, Assembler::AVX_512bit);
2587
2588 __ movl(r15, 0x00011000);
2589 __ evpbroadcastd(pack32_op, r15, Assembler::AVX_512bit);
2590
2591 __ cmpl(length, 0xff);
2592 __ jcc(Assembler::lessEqual, L_process64);
2593
2594 // load masks required for decoding data
2595 __ BIND(L_processdata);
2596 __ evmovdquq(join01, ExternalAddress(StubRoutines::x86::base64_vbmi_join_0_1_addr()), Assembler::AVX_512bit,r13);
2597 __ evmovdquq(join12, ExternalAddress(StubRoutines::x86::base64_vbmi_join_1_2_addr()), Assembler::AVX_512bit, r13);
2598 __ evmovdquq(join23, ExternalAddress(StubRoutines::x86::base64_vbmi_join_2_3_addr()), Assembler::AVX_512bit, r13);
2599
2600 __ align32();
2601 __ BIND(L_process256);
2602 // Grab input data
2603 __ evmovdquq(input0, Address(source, start_offset, Address::times_1, 0x00), Assembler::AVX_512bit);
2604 __ evmovdquq(input1, Address(source, start_offset, Address::times_1, 0x40), Assembler::AVX_512bit);
2605 __ evmovdquq(input2, Address(source, start_offset, Address::times_1, 0x80), Assembler::AVX_512bit);
2606 __ evmovdquq(input3, Address(source, start_offset, Address::times_1, 0xc0), Assembler::AVX_512bit);
2607
2608 // Copy the low part of the lookup table into the destination of the permutation
2609 __ evmovdquq(translated0, lookup_lo, Assembler::AVX_512bit);
2610 __ evmovdquq(translated1, lookup_lo, Assembler::AVX_512bit);
2611 __ evmovdquq(translated2, lookup_lo, Assembler::AVX_512bit);
2612 __ evmovdquq(translated3, lookup_lo, Assembler::AVX_512bit);
2613
2614 // Translate the base64 input into "decoded" bytes
2615 __ evpermt2b(translated0, input0, lookup_hi, Assembler::AVX_512bit);
2616 __ evpermt2b(translated1, input1, lookup_hi, Assembler::AVX_512bit);
2617 __ evpermt2b(translated2, input2, lookup_hi, Assembler::AVX_512bit);
2618 __ evpermt2b(translated3, input3, lookup_hi, Assembler::AVX_512bit);
2619
2620 // OR all of the translations together to check for errors (high-order bit of byte set)
2621 __ vpternlogd(input0, 0xfe, input1, input2, Assembler::AVX_512bit);
2622
2623 __ vpternlogd(input3, 0xfe, translated0, translated1, Assembler::AVX_512bit);
2624 __ vpternlogd(input0, 0xfe, translated2, translated3, Assembler::AVX_512bit);
2625 __ vpor(errorvec, input3, input0, Assembler::AVX_512bit);
2626
2627 // Check if there was an error - if so, try 64-byte chunks
2628 __ evpmovb2m(k3, errorvec, Assembler::AVX_512bit);
2629 __ kortestql(k3, k3);
2630 __ jcc(Assembler::notZero, L_process64);
2631
2632 // The merging and shuffling happens here
2633 // We multiply each byte pair [00dddddd | 00cccccc | 00bbbbbb | 00aaaaaa]
2634 // Multiply [00cccccc] by 2^6 added to [00dddddd] to get [0000cccc | ccdddddd]
2635 // The pack16_op is a vector of 0x01400140, so multiply D by 1 and C by 0x40
2636 __ vpmaddubsw(merge_ab_bc0, translated0, pack16_op, Assembler::AVX_512bit);
2637 __ vpmaddubsw(merge_ab_bc1, translated1, pack16_op, Assembler::AVX_512bit);
2638 __ vpmaddubsw(merge_ab_bc2, translated2, pack16_op, Assembler::AVX_512bit);
2639 __ vpmaddubsw(merge_ab_bc3, translated3, pack16_op, Assembler::AVX_512bit);
2640
2641 // Now do the same with packed 16-bit values.
2642 // We start with [0000cccc | ccdddddd | 0000aaaa | aabbbbbb]
2643 // pack32_op is 0x00011000 (2^12, 1), so this multiplies [0000aaaa | aabbbbbb] by 2^12
2644 // and adds [0000cccc | ccdddddd] to yield [00000000 | aaaaaabb | bbbbcccc | ccdddddd]
2645 __ vpmaddwd(merged0, merge_ab_bc0, pack32_op, Assembler::AVX_512bit);
2646 __ vpmaddwd(merged1, merge_ab_bc1, pack32_op, Assembler::AVX_512bit);
2647 __ vpmaddwd(merged2, merge_ab_bc2, pack32_op, Assembler::AVX_512bit);
2648 __ vpmaddwd(merged3, merge_ab_bc3, pack32_op, Assembler::AVX_512bit);
2649
2650 // The join vectors specify which byte from which vector goes into the outputs
2651 // One of every 4 bytes in the extended vector is zero, so we pack them into their
2652 // final positions in the register for storing (256 bytes in, 192 bytes out)
2653 __ evpermt2b(merged0, join01, merged1, Assembler::AVX_512bit);
2654 __ evpermt2b(merged1, join12, merged2, Assembler::AVX_512bit);
2655 __ evpermt2b(merged2, join23, merged3, Assembler::AVX_512bit);
2656
2657 // Store result
2658 __ evmovdquq(Address(dest, dp, Address::times_1, 0x00), merged0, Assembler::AVX_512bit);
2659 __ evmovdquq(Address(dest, dp, Address::times_1, 0x40), merged1, Assembler::AVX_512bit);
2660 __ evmovdquq(Address(dest, dp, Address::times_1, 0x80), merged2, Assembler::AVX_512bit);
2661
2662 __ addptr(source, 0x100);
2663 __ addptr(dest, 0xc0);
2664 __ subl(length, 0x100);
2665 __ cmpl(length, 64 * 4);
2666 __ jcc(Assembler::greaterEqual, L_process256);
2667
2668 // At this point, we've decoded 64 * 4 * n bytes.
2669 // The remaining length will be <= 64 * 4 - 1.
2670 // UNLESS there was an error decoding the first 256-byte chunk. In this
2671 // case, the length will be arbitrarily long.
2672 //
2673 // Note that this will be the path for MIME-encoded strings.
2674
2675 __ BIND(L_process64);
2676
2677 __ evmovdquq(pack24bits, ExternalAddress(StubRoutines::x86::base64_vbmi_pack_vec_addr()), Assembler::AVX_512bit, r13);
2678
2679 __ cmpl(length, 63);
2680 __ jcc(Assembler::lessEqual, L_finalBit);
2681
2682 __ mov64(rax, 0x0000ffffffffffff);
2683 __ kmovql(k2, rax);
2684
2685 __ align32();
2686 __ BIND(L_process64Loop);
2687
2688 // Handle first 64-byte block
2689
2690 __ evmovdquq(input0, Address(source, start_offset), Assembler::AVX_512bit);
2691 __ evmovdquq(translated0, lookup_lo, Assembler::AVX_512bit);
2692 __ evpermt2b(translated0, input0, lookup_hi, Assembler::AVX_512bit);
2693
2694 __ vpor(errorvec, translated0, input0, Assembler::AVX_512bit);
2695
2696 // Check for error and bomb out before updating dest
2697 __ evpmovb2m(k3, errorvec, Assembler::AVX_512bit);
2698 __ kortestql(k3, k3);
2699 __ jcc(Assembler::notZero, L_exit);
2700
2701 // Pack output register, selecting correct byte ordering
2702 __ vpmaddubsw(merge_ab_bc0, translated0, pack16_op, Assembler::AVX_512bit);
2703 __ vpmaddwd(merged0, merge_ab_bc0, pack32_op, Assembler::AVX_512bit);
2704 __ vpermb(merged0, pack24bits, merged0, Assembler::AVX_512bit);
2705
2706 __ evmovdqub(Address(dest, dp), k2, merged0, true, Assembler::AVX_512bit);
2707
2708 __ subl(length, 64);
2709 __ addptr(source, 64);
2710 __ addptr(dest, 48);
2711
2712 __ cmpl(length, 64);
2713 __ jcc(Assembler::greaterEqual, L_process64Loop);
2714
2715 __ cmpl(length, 0);
2716 __ jcc(Assembler::lessEqual, L_exit);
2717
2718 __ BIND(L_finalBit);
2719 // Now have 1 to 63 bytes left to decode
2720
2721 // I was going to let Java take care of the final fragment
2722 // however it will repeatedly call this routine for every 4 bytes
2723 // of input data, so handle the rest here.
2724 __ movq(rax, -1);
2725 __ bzhiq(rax, rax, length); // Input mask in rax
2726
2727 __ movl(output_size, length);
2728 __ shrl(output_size, 2); // Find (len / 4) * 3 (output length)
2729 __ lea(output_size, Address(output_size, output_size, Address::times_2, 0));
2730 // output_size in r13
2731
2732 // Strip pad characters, if any, and adjust length and mask
2733 __ addq(length, start_offset);
2734 __ cmpb(Address(source, length, Address::times_1, -1), '=');
2735 __ jcc(Assembler::equal, L_padding);
2736
2737 __ BIND(L_donePadding);
2738 __ subq(length, start_offset);
2739
2740 // Output size is (64 - output_size), output mask is (all 1s >> output_size).
2741 __ kmovql(input_mask, rax);
2742 __ movq(output_mask, -1);
2743 __ bzhiq(output_mask, output_mask, output_size);
2744
2745 // Load initial input with all valid base64 characters. Will be used
2746 // in merging source bytes to avoid masking when determining if an error occurred.
2747 __ movl(rax, 0x61616161);
2748 __ evpbroadcastd(input_initial_valid_b64, rax, Assembler::AVX_512bit);
2749
2750 // A register containing all invalid base64 decoded values
2751 __ movl(rax, 0x80808080);
2752 __ evpbroadcastd(invalid_b64, rax, Assembler::AVX_512bit);
2753
2754 // input_mask is in k1
2755 // output_size is in r13
2756 // output_mask is in r15
2757 // zmm0 - free
2758 // zmm1 - 0x00011000
2759 // zmm2 - 0x01400140
2760 // zmm3 - errorvec
2761 // zmm4 - pack vector
2762 // zmm5 - lookup_lo
2763 // zmm6 - lookup_hi
2764 // zmm7 - errorvec
2765 // zmm8 - 0x61616161
2766 // zmm9 - 0x80808080
2767
2768 // Load only the bytes from source, merging into our "fully-valid" register
2769 __ evmovdqub(input_initial_valid_b64, input_mask, Address(source, start_offset, Address::times_1, 0x0), true, Assembler::AVX_512bit);
2770
2771 // Decode all bytes within our merged input
2772 __ evmovdquq(tmp, lookup_lo, Assembler::AVX_512bit);
2773 __ evpermt2b(tmp, input_initial_valid_b64, lookup_hi, Assembler::AVX_512bit);
2774 __ evporq(mask, tmp, input_initial_valid_b64, Assembler::AVX_512bit);
2775
2776 // Check for error. Compare (decoded | initial) to all invalid.
2777 // If any bytes have their high-order bit set, then we have an error.
2778 __ evptestmb(k2, mask, invalid_b64, Assembler::AVX_512bit);
2779 __ kortestql(k2, k2);
2780
2781 // If we have an error, use the brute force loop to decode what we can (4-byte chunks).
2782 __ jcc(Assembler::notZero, L_bruteForce);
2783
2784 // Shuffle output bytes
2785 __ vpmaddubsw(tmp, tmp, pack16_op, Assembler::AVX_512bit);
2786 __ vpmaddwd(tmp, tmp, pack32_op, Assembler::AVX_512bit);
2787
2788 __ vpermb(tmp, pack24bits, tmp, Assembler::AVX_512bit);
2789 __ kmovql(k1, output_mask);
2790 __ evmovdqub(Address(dest, dp), k1, tmp, true, Assembler::AVX_512bit);
2791
2792 __ addptr(dest, output_size);
2793
2794 __ BIND(L_exit);
2795 __ vzeroupper();
2796 __ pop_ppx(rax); // Get original dest value
2797 __ subptr(dest, rax); // Number of bytes converted
2798 __ movptr(rax, dest);
2799 __ pop_ppx(rbx);
2800 __ pop_ppx(r15);
2801 __ pop_ppx(r14);
2802 __ pop_ppx(r13);
2803 __ pop_ppx(r12);
2804 __ leave();
2805 __ ret(0);
2806
2807 __ BIND(L_loadURL);
2808 __ evmovdquq(lookup_lo, ExternalAddress(StubRoutines::x86::base64_vbmi_lookup_lo_url_addr()), Assembler::AVX_512bit, r13);
2809 __ evmovdquq(lookup_hi, ExternalAddress(StubRoutines::x86::base64_vbmi_lookup_hi_url_addr()), Assembler::AVX_512bit, r13);
2810 __ jmp(L_continue);
2811
2812 __ BIND(L_padding);
2813 __ decrementq(output_size, 1);
2814 __ shrq(rax, 1);
2815
2816 __ cmpb(Address(source, length, Address::times_1, -2), '=');
2817 __ jcc(Assembler::notEqual, L_donePadding);
2818
2819 __ decrementq(output_size, 1);
2820 __ shrq(rax, 1);
2821 __ jmp(L_donePadding);
2822
2823 __ align32();
2824 __ BIND(L_bruteForce);
2825 } // End of if(avx512_vbmi)
2826
2827 if (VM_Version::supports_avx2()) {
2828 Label L_tailProc, L_topLoop, L_enterLoop;
2829
2830 __ cmpl(isMIME, 0);
2831 __ jcc(Assembler::notEqual, L_lastChunk);
2832
2833 // Check for buffer too small (for algorithm)
2834 __ subl(length, 0x2c);
2835 __ jcc(Assembler::less, L_tailProc);
2836
2837 __ shll(isURL, 2);
2838
2839 // Algorithm adapted from https://arxiv.org/abs/1704.00605, "Faster Base64
2840 // Encoding and Decoding using AVX2 Instructions". URL modifications added.
2841
2842 // Set up constants
2843 __ lea(r13, ExternalAddress(StubRoutines::x86::base64_AVX2_decode_tables_addr()));
2844 __ vpbroadcastd(xmm4, Address(r13, isURL, Address::times_1), Assembler::AVX_256bit); // 2F or 5F
2845 __ vpbroadcastd(xmm10, Address(r13, isURL, Address::times_1, 0x08), Assembler::AVX_256bit); // -1 or -4
2846 __ vmovdqu(xmm12, Address(r13, 0x10)); // permute
2847 __ vmovdqu(xmm13, Address(r13, 0x30)); // shuffle
2848 __ vpbroadcastd(xmm7, Address(r13, 0x50), Assembler::AVX_256bit); // merge
2849 __ vpbroadcastd(xmm6, Address(r13, 0x54), Assembler::AVX_256bit); // merge mult
2850
2851 __ lea(r13, ExternalAddress(StubRoutines::x86::base64_AVX2_decode_LUT_tables_addr()));
2852 __ shll(isURL, 4);
2853 __ vmovdqu(xmm11, Address(r13, isURL, Address::times_1, 0x00)); // lut_lo
2854 __ vmovdqu(xmm8, Address(r13, isURL, Address::times_1, 0x20)); // lut_roll
2855 __ shrl(isURL, 6); // restore isURL
2856 __ vmovdqu(xmm9, Address(r13, 0x80)); // lut_hi
2857 __ jmp(L_enterLoop);
2858
2859 __ align32();
2860 __ bind(L_topLoop);
2861 // Add in the offset value (roll) to get 6-bit out values
2862 __ vpaddb(xmm0, xmm0, xmm2, Assembler::AVX_256bit);
2863 // Merge and permute the output bits into appropriate output byte lanes
2864 __ vpmaddubsw(xmm0, xmm0, xmm7, Assembler::AVX_256bit);
2865 __ vpmaddwd(xmm0, xmm0, xmm6, Assembler::AVX_256bit);
2866 __ vpshufb(xmm0, xmm0, xmm13, Assembler::AVX_256bit);
2867 __ vpermd(xmm0, xmm12, xmm0, Assembler::AVX_256bit);
2868 // Store the output bytes
2869 __ vmovdqu(Address(dest, dp, Address::times_1, 0), xmm0);
2870 __ addptr(source, 0x20);
2871 __ addptr(dest, 0x18);
2872 __ subl(length, 0x20);
2873 __ jcc(Assembler::less, L_tailProc);
2874
2875 __ bind(L_enterLoop);
2876
2877 // Load in encoded string (32 bytes)
2878 __ vmovdqu(xmm2, Address(source, start_offset, Address::times_1, 0x0));
2879 // Extract the high nibble for indexing into the lut tables. High 4 bits are don't care.
2880 __ vpsrld(xmm1, xmm2, 0x4, Assembler::AVX_256bit);
2881 __ vpand(xmm1, xmm4, xmm1, Assembler::AVX_256bit);
2882 // Extract the low nibble. 5F/2F will isolate the low-order 4 bits. High 4 bits are don't care.
2883 __ vpand(xmm3, xmm2, xmm4, Assembler::AVX_256bit);
2884 // Check for special-case (0x2F or 0x5F (URL))
2885 __ vpcmpeqb(xmm0, xmm4, xmm2, Assembler::AVX_256bit);
2886 // Get the bitset based on the low nibble. vpshufb uses low-order 4 bits only.
2887 __ vpshufb(xmm3, xmm11, xmm3, Assembler::AVX_256bit);
2888 // Get the bit value of the high nibble
2889 __ vpshufb(xmm5, xmm9, xmm1, Assembler::AVX_256bit);
2890 // Make sure 2F / 5F shows as valid
2891 __ vpandn(xmm3, xmm0, xmm3, Assembler::AVX_256bit);
2892 // Make adjustment for roll index. For non-URL, this is a no-op,
2893 // for URL, this adjusts by -4. This is to properly index the
2894 // roll value for 2F / 5F.
2895 __ vpand(xmm0, xmm0, xmm10, Assembler::AVX_256bit);
2896 // If the and of the two is non-zero, we have an invalid input character
2897 __ vptest(xmm3, xmm5);
2898 // Extract the "roll" value - value to add to the input to get 6-bit out value
2899 __ vpaddb(xmm0, xmm0, xmm1, Assembler::AVX_256bit); // Handle 2F / 5F
2900 __ vpshufb(xmm0, xmm8, xmm0, Assembler::AVX_256bit);
2901 __ jcc(Assembler::equal, L_topLoop); // Fall through on error
2902
2903 __ bind(L_tailProc);
2904
2905 __ addl(length, 0x2c);
2906
2907 __ vzeroupper();
2908 }
2909
2910 // Use non-AVX code to decode 4-byte chunks into 3 bytes of output
2911
2912 // Register state (Linux):
2913 // r12-15 - saved on stack
2914 // rdi - src
2915 // rsi - sp
2916 // rdx - sl
2917 // rcx - dst
2918 // r8 - dp
2919 // r9 - isURL
2920
2921 // Register state (Windows):
2922 // r12-15 - saved on stack
2923 // rcx - src
2924 // rdx - sp
2925 // r8 - sl
2926 // r9 - dst
2927 // r12 - dp
2928 // r10 - isURL
2929
2930 // Registers (common):
2931 // length (r14) - bytes in src
2932
2933 const Register decode_table = r11;
2934 const Register out_byte_count = rbx;
2935 const Register byte1 = r13;
2936 const Register byte2 = r15;
2937 const Register byte3 = WIN64_ONLY(r8) NOT_WIN64(rdx);
2938 const Register byte4 = WIN64_ONLY(r10) NOT_WIN64(r9);
2939
2940 __ bind(L_lastChunk);
2941
2942 __ shrl(length, 2); // Multiple of 4 bytes only - length is # 4-byte chunks
2943 __ cmpl(length, 0);
2944 __ jcc(Assembler::lessEqual, L_exit_no_vzero);
2945
2946 __ shll(isURL, 8); // index into decode table based on isURL
2947 __ lea(decode_table, ExternalAddress(StubRoutines::x86::base64_decoding_table_addr()));
2948 __ addptr(decode_table, isURL);
2949
2950 __ jmp(L_bottomLoop);
2951
2952 __ align32();
2953 __ BIND(L_forceLoop);
2954 __ shll(byte1, 18);
2955 __ shll(byte2, 12);
2956 __ shll(byte3, 6);
2957 __ orl(byte1, byte2);
2958 __ orl(byte1, byte3);
2959 __ orl(byte1, byte4);
2960
2961 __ addptr(source, 4);
2962
2963 __ movb(Address(dest, dp, Address::times_1, 2), byte1);
2964 __ shrl(byte1, 8);
2965 __ movb(Address(dest, dp, Address::times_1, 1), byte1);
2966 __ shrl(byte1, 8);
2967 __ movb(Address(dest, dp, Address::times_1, 0), byte1);
2968
2969 __ addptr(dest, 3);
2970 __ decrementl(length, 1);
2971 __ jcc(Assembler::zero, L_exit_no_vzero);
2972
2973 __ BIND(L_bottomLoop);
2974 __ load_unsigned_byte(byte1, Address(source, start_offset, Address::times_1, 0x00));
2975 __ load_unsigned_byte(byte2, Address(source, start_offset, Address::times_1, 0x01));
2976 __ load_signed_byte(byte1, Address(decode_table, byte1));
2977 __ load_signed_byte(byte2, Address(decode_table, byte2));
2978 __ load_unsigned_byte(byte3, Address(source, start_offset, Address::times_1, 0x02));
2979 __ load_unsigned_byte(byte4, Address(source, start_offset, Address::times_1, 0x03));
2980 __ load_signed_byte(byte3, Address(decode_table, byte3));
2981 __ load_signed_byte(byte4, Address(decode_table, byte4));
2982
2983 __ mov(rax, byte1);
2984 __ orl(rax, byte2);
2985 __ orl(rax, byte3);
2986 __ orl(rax, byte4);
2987 __ jcc(Assembler::positive, L_forceLoop);
2988
2989 __ BIND(L_exit_no_vzero);
2990 __ pop_ppx(rax); // Get original dest value
2991 __ subptr(dest, rax); // Number of bytes converted
2992 __ movptr(rax, dest);
2993 __ pop_ppx(rbx);
2994 __ pop_ppx(r15);
2995 __ pop_ppx(r14);
2996 __ pop_ppx(r13);
2997 __ pop_ppx(r12);
2998 __ leave();
2999 __ ret(0);
3000
3001 return start;
3002 }
3003
3004
3005 /**
3006 * Arguments:
3007 *
3008 * Inputs:
3009 * c_rarg0 - int crc
3010 * c_rarg1 - byte* buf
3011 * c_rarg2 - int length
3012 *
3013 * Output:
3014 * rax - int crc result
3015 */
3016 address StubGenerator::generate_updateBytesCRC32() {
3017 assert(UseCRC32Intrinsics, "need AVX and CLMUL instructions");
3018
3019 __ align(CodeEntryAlignment);
3020 StubId stub_id = StubId::stubgen_updateBytesCRC32_id;
3021 StubCodeMark mark(this, stub_id);
3022
3023 address start = __ pc();
3024
3025 // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
3026 // Unix: rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
3027 // rscratch1: r10
3028 const Register crc = c_rarg0; // crc
3029 const Register buf = c_rarg1; // source java byte array address
3030 const Register len = c_rarg2; // length
3031 const Register table = c_rarg3; // crc_table address (reuse register)
3032 const Register tmp1 = r11;
3033 const Register tmp2 = r10;
3034 assert_different_registers(crc, buf, len, table, tmp1, tmp2, rax);
3035
3036 BLOCK_COMMENT("Entry:");
3037 __ enter(); // required for proper stackwalking of RuntimeStub frame
3038
3039 if (VM_Version::supports_sse4_1() && VM_Version::supports_avx512_vpclmulqdq() &&
3040 VM_Version::supports_avx512bw() &&
3041 VM_Version::supports_avx512vl()) {
3042 // The constants used in the CRC32 algorithm requires the 1's compliment of the initial crc value.
3043 // However, the constant table for CRC32-C assumes the original crc value. Account for this
3044 // difference before calling and after returning.
3045 __ lea(table, ExternalAddress(StubRoutines::x86::crc_table_avx512_addr()));
3046 __ notl(crc);
3047 __ kernel_crc32_avx512(crc, buf, len, table, tmp1, tmp2);
3048 __ notl(crc);
3049 } else {
3050 __ kernel_crc32(crc, buf, len, table, tmp1);
3051 }
3052
3053 __ movl(rax, crc);
3054 __ vzeroupper();
3055 __ leave(); // required for proper stackwalking of RuntimeStub frame
3056 __ ret(0);
3057
3058 return start;
3059 }
3060
3061 /**
3062 * Arguments:
3063 *
3064 * Inputs:
3065 * c_rarg0 - int crc
3066 * c_rarg1 - byte* buf
3067 * c_rarg2 - long length
3068 * c_rarg3 - table_start - optional (present only when doing a library_call,
3069 * not used by x86 algorithm)
3070 *
3071 * Output:
3072 * rax - int crc result
3073 */
3074 address StubGenerator::generate_updateBytesCRC32C(bool is_pclmulqdq_supported) {
3075 assert(UseCRC32CIntrinsics, "need SSE4_2");
3076 __ align(CodeEntryAlignment);
3077 StubId stub_id = StubId::stubgen_updateBytesCRC32C_id;
3078 StubCodeMark mark(this, stub_id);
3079 address start = __ pc();
3080
3081 //reg.arg int#0 int#1 int#2 int#3 int#4 int#5 float regs
3082 //Windows RCX RDX R8 R9 none none XMM0..XMM3
3083 //Lin / Sol RDI RSI RDX RCX R8 R9 XMM0..XMM7
3084 const Register crc = c_rarg0; // crc
3085 const Register buf = c_rarg1; // source java byte array address
3086 const Register len = c_rarg2; // length
3087 const Register a = rax;
3088 const Register j = r9;
3089 const Register k = r10;
3090 const Register l = r11;
3091 #ifdef _WIN64
3092 const Register y = rdi;
3093 const Register z = rsi;
3094 #else
3095 const Register y = rcx;
3096 const Register z = r8;
3097 #endif
3098 assert_different_registers(crc, buf, len, a, j, k, l, y, z);
3099
3100 BLOCK_COMMENT("Entry:");
3101 __ enter(); // required for proper stackwalking of RuntimeStub frame
3102 Label L_continue;
3103
3104 if (VM_Version::supports_sse4_1() && VM_Version::supports_avx512_vpclmulqdq() &&
3105 VM_Version::supports_avx512bw() &&
3106 VM_Version::supports_avx512vl()) {
3107 Label L_doSmall;
3108
3109 __ cmpl(len, 384);
3110 __ jcc(Assembler::lessEqual, L_doSmall);
3111
3112 __ lea(j, ExternalAddress(StubRoutines::x86::crc32c_table_avx512_addr()));
3113 __ kernel_crc32_avx512(crc, buf, len, j, l, k);
3114
3115 __ jmp(L_continue);
3116
3117 __ bind(L_doSmall);
3118 }
3119 #ifdef _WIN64
3120 __ push_ppx(y);
3121 __ push_ppx(z);
3122 #endif
3123 __ crc32c_ipl_alg2_alt2(crc, buf, len,
3124 a, j, k,
3125 l, y, z,
3126 c_farg0, c_farg1, c_farg2,
3127 is_pclmulqdq_supported);
3128 #ifdef _WIN64
3129 __ pop_ppx(z);
3130 __ pop_ppx(y);
3131 #endif
3132
3133 __ bind(L_continue);
3134 __ movl(rax, crc);
3135 __ vzeroupper();
3136 __ leave(); // required for proper stackwalking of RuntimeStub frame
3137 __ ret(0);
3138
3139 return start;
3140 }
3141
3142
3143 /**
3144 * Arguments:
3145 *
3146 * Input:
3147 * c_rarg0 - x address
3148 * c_rarg1 - x length
3149 * c_rarg2 - y address
3150 * c_rarg3 - y length
3151 * not Win64
3152 * c_rarg4 - z address
3153 * Win64
3154 * rsp+40 - z address
3155 */
3156 address StubGenerator::generate_multiplyToLen() {
3157 __ align(CodeEntryAlignment);
3158 StubId stub_id = StubId::stubgen_multiplyToLen_id;
3159 StubCodeMark mark(this, stub_id);
3160 address start = __ pc();
3161
3162 // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
3163 // Unix: rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
3164 const Register x = rdi;
3165 const Register xlen = rax;
3166 const Register y = rsi;
3167 const Register ylen = rcx;
3168 const Register z = r8;
3169
3170 // Next registers will be saved on stack in multiply_to_len().
3171 const Register tmp0 = r11;
3172 const Register tmp1 = r12;
3173 const Register tmp2 = r13;
3174 const Register tmp3 = r14;
3175 const Register tmp4 = r15;
3176 const Register tmp5 = rbx;
3177
3178 BLOCK_COMMENT("Entry:");
3179 __ enter(); // required for proper stackwalking of RuntimeStub frame
3180
3181 setup_arg_regs(4); // x => rdi, xlen => rsi, y => rdx
3182 // ylen => rcx, z => r8
3183 // r9 and r10 may be used to save non-volatile registers
3184 #ifdef _WIN64
3185 // last argument (#4) is on stack on Win64
3186 __ movptr(z, Address(rsp, 6 * wordSize));
3187 #endif
3188
3189 __ movptr(xlen, rsi);
3190 __ movptr(y, rdx);
3191 __ multiply_to_len(x, xlen, y, ylen, z, tmp0, tmp1, tmp2, tmp3, tmp4, tmp5);
3192
3193 restore_arg_regs();
3194
3195 __ leave(); // required for proper stackwalking of RuntimeStub frame
3196 __ ret(0);
3197
3198 return start;
3199 }
3200
3201 /**
3202 * Arguments:
3203 *
3204 * Input:
3205 * c_rarg0 - obja address
3206 * c_rarg1 - objb address
3207 * c_rarg3 - length length
3208 * c_rarg4 - scale log2_array_indxscale
3209 *
3210 * Output:
3211 * rax - int >= mismatched index, < 0 bitwise complement of tail
3212 */
3213 address StubGenerator::generate_vectorizedMismatch() {
3214 __ align(CodeEntryAlignment);
3215 StubId stub_id = StubId::stubgen_vectorizedMismatch_id;
3216 StubCodeMark mark(this, stub_id);
3217 address start = __ pc();
3218
3219 BLOCK_COMMENT("Entry:");
3220 __ enter();
3221
3222 #ifdef _WIN64 // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
3223 const Register scale = c_rarg0; //rcx, will exchange with r9
3224 const Register objb = c_rarg1; //rdx
3225 const Register length = c_rarg2; //r8
3226 const Register obja = c_rarg3; //r9
3227 __ xchgq(obja, scale); //now obja and scale contains the correct contents
3228
3229 const Register tmp1 = r10;
3230 const Register tmp2 = r11;
3231 #endif
3232 #ifndef _WIN64 // Unix: rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
3233 const Register obja = c_rarg0; //U:rdi
3234 const Register objb = c_rarg1; //U:rsi
3235 const Register length = c_rarg2; //U:rdx
3236 const Register scale = c_rarg3; //U:rcx
3237 const Register tmp1 = r8;
3238 const Register tmp2 = r9;
3239 #endif
3240 const Register result = rax; //return value
3241 const XMMRegister vec0 = xmm0;
3242 const XMMRegister vec1 = xmm1;
3243 const XMMRegister vec2 = xmm2;
3244
3245 __ vectorized_mismatch(obja, objb, length, scale, result, tmp1, tmp2, vec0, vec1, vec2);
3246
3247 __ vzeroupper();
3248 __ leave();
3249 __ ret(0);
3250
3251 return start;
3252 }
3253
3254 /**
3255 * Arguments:
3256 *
3257 // Input:
3258 // c_rarg0 - x address
3259 // c_rarg1 - x length
3260 // c_rarg2 - z address
3261 // c_rarg3 - z length
3262 *
3263 */
3264 address StubGenerator::generate_squareToLen() {
3265
3266 __ align(CodeEntryAlignment);
3267 StubId stub_id = StubId::stubgen_squareToLen_id;
3268 StubCodeMark mark(this, stub_id);
3269 address start = __ pc();
3270
3271 // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
3272 // Unix: rdi, rsi, rdx, rcx (c_rarg0, c_rarg1, ...)
3273 const Register x = rdi;
3274 const Register len = rsi;
3275 const Register z = r8;
3276 const Register zlen = rcx;
3277
3278 const Register tmp1 = r12;
3279 const Register tmp2 = r13;
3280 const Register tmp3 = r14;
3281 const Register tmp4 = r15;
3282 const Register tmp5 = rbx;
3283
3284 BLOCK_COMMENT("Entry:");
3285 __ enter(); // required for proper stackwalking of RuntimeStub frame
3286
3287 setup_arg_regs(4); // x => rdi, len => rsi, z => rdx
3288 // zlen => rcx
3289 // r9 and r10 may be used to save non-volatile registers
3290 __ movptr(r8, rdx);
3291 __ square_to_len(x, len, z, zlen, tmp1, tmp2, tmp3, tmp4, tmp5, rdx, rax);
3292
3293 restore_arg_regs();
3294
3295 __ leave(); // required for proper stackwalking of RuntimeStub frame
3296 __ ret(0);
3297
3298 return start;
3299 }
3300
3301 address StubGenerator::generate_method_entry_barrier() {
3302 __ align(CodeEntryAlignment);
3303 StubId stub_id = StubId::stubgen_method_entry_barrier_id;
3304 StubCodeMark mark(this, stub_id);
3305 address start = __ pc();
3306
3307 Label deoptimize_label;
3308
3309 __ push(-1); // cookie, this is used for writing the new rsp when deoptimizing
3310
3311 BLOCK_COMMENT("Entry:");
3312 __ enter(); // save rbp
3313
3314 // save c_rarg0, because we want to use that value.
3315 // We could do without it but then we depend on the number of slots used by pusha
3316 __ push_ppx(c_rarg0);
3317
3318 __ lea(c_rarg0, Address(rsp, wordSize * 3)); // 1 for cookie, 1 for rbp, 1 for c_rarg0 - this should be the return address
3319
3320 __ pusha();
3321
3322 // The method may have floats as arguments, and we must spill them before calling
3323 // the VM runtime.
3324 assert(Argument::n_float_register_parameters_j == 8, "Assumption");
3325 const int xmm_size = wordSize * 2;
3326 const int xmm_spill_size = xmm_size * Argument::n_float_register_parameters_j;
3327 __ subptr(rsp, xmm_spill_size);
3328 __ movdqu(Address(rsp, xmm_size * 7), xmm7);
3329 __ movdqu(Address(rsp, xmm_size * 6), xmm6);
3330 __ movdqu(Address(rsp, xmm_size * 5), xmm5);
3331 __ movdqu(Address(rsp, xmm_size * 4), xmm4);
3332 __ movdqu(Address(rsp, xmm_size * 3), xmm3);
3333 __ movdqu(Address(rsp, xmm_size * 2), xmm2);
3334 __ movdqu(Address(rsp, xmm_size * 1), xmm1);
3335 __ movdqu(Address(rsp, xmm_size * 0), xmm0);
3336
3337 __ call_VM_leaf(CAST_FROM_FN_PTR(address, static_cast<int (*)(address*)>(BarrierSetNMethod::nmethod_stub_entry_barrier)), 1);
3338
3339 __ movdqu(xmm0, Address(rsp, xmm_size * 0));
3340 __ movdqu(xmm1, Address(rsp, xmm_size * 1));
3341 __ movdqu(xmm2, Address(rsp, xmm_size * 2));
3342 __ movdqu(xmm3, Address(rsp, xmm_size * 3));
3343 __ movdqu(xmm4, Address(rsp, xmm_size * 4));
3344 __ movdqu(xmm5, Address(rsp, xmm_size * 5));
3345 __ movdqu(xmm6, Address(rsp, xmm_size * 6));
3346 __ movdqu(xmm7, Address(rsp, xmm_size * 7));
3347 __ addptr(rsp, xmm_spill_size);
3348
3349 __ cmpl(rax, 1); // 1 means deoptimize
3350 __ jcc(Assembler::equal, deoptimize_label);
3351
3352 __ popa();
3353 __ pop_ppx(c_rarg0);
3354
3355 __ leave();
3356
3357 __ addptr(rsp, 1 * wordSize); // cookie
3358 __ ret(0);
3359
3360
3361 __ BIND(deoptimize_label);
3362
3363 __ popa();
3364 __ pop_ppx(c_rarg0);
3365
3366 __ leave();
3367
3368 // this can be taken out, but is good for verification purposes. getting a SIGSEGV
3369 // here while still having a correct stack is valuable
3370 __ testptr(rsp, Address(rsp, 0));
3371
3372 __ movptr(rsp, Address(rsp, 0)); // new rsp was written in the barrier
3373 __ jmp(Address(rsp, -1 * wordSize)); // jmp target should be callers verified_entry_point
3374
3375 return start;
3376 }
3377
3378 /**
3379 * Arguments:
3380 *
3381 * Input:
3382 * c_rarg0 - out address
3383 * c_rarg1 - in address
3384 * c_rarg2 - offset
3385 * c_rarg3 - len
3386 * not Win64
3387 * c_rarg4 - k
3388 * Win64
3389 * rsp+40 - k
3390 */
3391 address StubGenerator::generate_mulAdd() {
3392 __ align(CodeEntryAlignment);
3393 StubId stub_id = StubId::stubgen_mulAdd_id;
3394 StubCodeMark mark(this, stub_id);
3395 address start = __ pc();
3396
3397 // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
3398 // Unix: rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
3399 const Register out = rdi;
3400 const Register in = rsi;
3401 const Register offset = r11;
3402 const Register len = rcx;
3403 const Register k = r8;
3404
3405 // Next registers will be saved on stack in mul_add().
3406 const Register tmp1 = r12;
3407 const Register tmp2 = r13;
3408 const Register tmp3 = r14;
3409 const Register tmp4 = r15;
3410 const Register tmp5 = rbx;
3411
3412 BLOCK_COMMENT("Entry:");
3413 __ enter(); // required for proper stackwalking of RuntimeStub frame
3414
3415 setup_arg_regs(4); // out => rdi, in => rsi, offset => rdx
3416 // len => rcx, k => r8
3417 // r9 and r10 may be used to save non-volatile registers
3418 #ifdef _WIN64
3419 // last argument is on stack on Win64
3420 __ movl(k, Address(rsp, 6 * wordSize));
3421 #endif
3422 __ movptr(r11, rdx); // move offset in rdx to offset(r11)
3423 __ mul_add(out, in, offset, len, k, tmp1, tmp2, tmp3, tmp4, tmp5, rdx, rax);
3424
3425 restore_arg_regs();
3426
3427 __ leave(); // required for proper stackwalking of RuntimeStub frame
3428 __ ret(0);
3429
3430 return start;
3431 }
3432
3433 address StubGenerator::generate_bigIntegerRightShift() {
3434 __ align(CodeEntryAlignment);
3435 StubId stub_id = StubId::stubgen_bigIntegerRightShiftWorker_id;
3436 StubCodeMark mark(this, stub_id);
3437 address start = __ pc();
3438
3439 Label Shift512Loop, ShiftTwo, ShiftTwoLoop, ShiftOne, Exit;
3440 // For Unix, the arguments are as follows: rdi, rsi, rdx, rcx, r8.
3441 const Register newArr = rdi;
3442 const Register oldArr = rsi;
3443 const Register newIdx = rdx;
3444 const Register shiftCount = rcx; // It was intentional to have shiftCount in rcx since it is used implicitly for shift.
3445 const Register totalNumIter = r8;
3446
3447 // For windows, we use r9 and r10 as temps to save rdi and rsi. Thus we cannot allocate them for our temps.
3448 // For everything else, we prefer using r9 and r10 since we do not have to save them before use.
3449 const Register tmp1 = r11; // Caller save.
3450 const Register tmp2 = rax; // Caller save.
3451 const Register tmp3 = WIN64_ONLY(r12) NOT_WIN64(r9); // Windows: Callee save. Linux: Caller save.
3452 const Register tmp4 = WIN64_ONLY(r13) NOT_WIN64(r10); // Windows: Callee save. Linux: Caller save.
3453 const Register tmp5 = r14; // Callee save.
3454 const Register tmp6 = r15;
3455
3456 const XMMRegister x0 = xmm0;
3457 const XMMRegister x1 = xmm1;
3458 const XMMRegister x2 = xmm2;
3459
3460 BLOCK_COMMENT("Entry:");
3461 __ enter(); // required for proper stackwalking of RuntimeStub frame
3462
3463 #ifdef _WIN64
3464 setup_arg_regs(4);
3465 // For windows, since last argument is on stack, we need to move it to the appropriate register.
3466 __ movl(totalNumIter, Address(rsp, 6 * wordSize));
3467 // Save callee save registers.
3468 __ push_ppx(tmp3);
3469 __ push_ppx(tmp4);
3470 #endif
3471 __ push_ppx(tmp5);
3472
3473 // Rename temps used throughout the code.
3474 const Register idx = tmp1;
3475 const Register nIdx = tmp2;
3476
3477 __ xorl(idx, idx);
3478
3479 // Start right shift from end of the array.
3480 // For example, if #iteration = 4 and newIdx = 1
3481 // then dest[4] = src[4] >> shiftCount | src[3] <<< (shiftCount - 32)
3482 // if #iteration = 4 and newIdx = 0
3483 // then dest[3] = src[4] >> shiftCount | src[3] <<< (shiftCount - 32)
3484 __ movl(idx, totalNumIter);
3485 __ movl(nIdx, idx);
3486 __ addl(nIdx, newIdx);
3487
3488 // If vectorization is enabled, check if the number of iterations is at least 64
3489 // If not, then go to ShifTwo processing 2 iterations
3490 if (VM_Version::supports_avx512_vbmi2()) {
3491 __ cmpptr(totalNumIter, (AVX3Threshold/64));
3492 __ jcc(Assembler::less, ShiftTwo);
3493
3494 if (AVX3Threshold < 16 * 64) {
3495 __ cmpl(totalNumIter, 16);
3496 __ jcc(Assembler::less, ShiftTwo);
3497 }
3498 __ evpbroadcastd(x0, shiftCount, Assembler::AVX_512bit);
3499 __ subl(idx, 16);
3500 __ subl(nIdx, 16);
3501 __ BIND(Shift512Loop);
3502 __ evmovdqul(x2, Address(oldArr, idx, Address::times_4, 4), Assembler::AVX_512bit);
3503 __ evmovdqul(x1, Address(oldArr, idx, Address::times_4), Assembler::AVX_512bit);
3504 __ vpshrdvd(x2, x1, x0, Assembler::AVX_512bit);
3505 __ evmovdqul(Address(newArr, nIdx, Address::times_4), x2, Assembler::AVX_512bit);
3506 __ subl(nIdx, 16);
3507 __ subl(idx, 16);
3508 __ jcc(Assembler::greaterEqual, Shift512Loop);
3509 __ addl(idx, 16);
3510 __ addl(nIdx, 16);
3511 }
3512 __ BIND(ShiftTwo);
3513 __ cmpl(idx, 2);
3514 __ jcc(Assembler::less, ShiftOne);
3515 __ subl(idx, 2);
3516 __ subl(nIdx, 2);
3517 __ BIND(ShiftTwoLoop);
3518 __ movl(tmp5, Address(oldArr, idx, Address::times_4, 8));
3519 __ movl(tmp4, Address(oldArr, idx, Address::times_4, 4));
3520 __ movl(tmp3, Address(oldArr, idx, Address::times_4));
3521 __ shrdl(tmp5, tmp4);
3522 __ shrdl(tmp4, tmp3);
3523 __ movl(Address(newArr, nIdx, Address::times_4, 4), tmp5);
3524 __ movl(Address(newArr, nIdx, Address::times_4), tmp4);
3525 __ subl(nIdx, 2);
3526 __ subl(idx, 2);
3527 __ jcc(Assembler::greaterEqual, ShiftTwoLoop);
3528 __ addl(idx, 2);
3529 __ addl(nIdx, 2);
3530
3531 // Do the last iteration
3532 __ BIND(ShiftOne);
3533 __ cmpl(idx, 1);
3534 __ jcc(Assembler::less, Exit);
3535 __ subl(idx, 1);
3536 __ subl(nIdx, 1);
3537 __ movl(tmp4, Address(oldArr, idx, Address::times_4, 4));
3538 __ movl(tmp3, Address(oldArr, idx, Address::times_4));
3539 __ shrdl(tmp4, tmp3);
3540 __ movl(Address(newArr, nIdx, Address::times_4), tmp4);
3541 __ BIND(Exit);
3542 __ vzeroupper();
3543 // Restore callee save registers.
3544 __ pop_ppx(tmp5);
3545 #ifdef _WIN64
3546 __ pop_ppx(tmp4);
3547 __ pop_ppx(tmp3);
3548 restore_arg_regs();
3549 #endif
3550 __ leave(); // required for proper stackwalking of RuntimeStub frame
3551 __ ret(0);
3552
3553 return start;
3554 }
3555
3556 /**
3557 * Arguments:
3558 *
3559 * Input:
3560 * c_rarg0 - newArr address
3561 * c_rarg1 - oldArr address
3562 * c_rarg2 - newIdx
3563 * c_rarg3 - shiftCount
3564 * not Win64
3565 * c_rarg4 - numIter
3566 * Win64
3567 * rsp40 - numIter
3568 */
3569 address StubGenerator::generate_bigIntegerLeftShift() {
3570 __ align(CodeEntryAlignment);
3571 StubId stub_id = StubId::stubgen_bigIntegerLeftShiftWorker_id;
3572 StubCodeMark mark(this, stub_id);
3573 address start = __ pc();
3574
3575 Label Shift512Loop, ShiftTwo, ShiftTwoLoop, ShiftOne, Exit;
3576 // For Unix, the arguments are as follows: rdi, rsi, rdx, rcx, r8.
3577 const Register newArr = rdi;
3578 const Register oldArr = rsi;
3579 const Register newIdx = rdx;
3580 const Register shiftCount = rcx; // It was intentional to have shiftCount in rcx since it is used implicitly for shift.
3581 const Register totalNumIter = r8;
3582 // For windows, we use r9 and r10 as temps to save rdi and rsi. Thus we cannot allocate them for our temps.
3583 // For everything else, we prefer using r9 and r10 since we do not have to save them before use.
3584 const Register tmp1 = r11; // Caller save.
3585 const Register tmp2 = rax; // Caller save.
3586 const Register tmp3 = WIN64_ONLY(r12) NOT_WIN64(r9); // Windows: Callee save. Linux: Caller save.
3587 const Register tmp4 = WIN64_ONLY(r13) NOT_WIN64(r10); // Windows: Callee save. Linux: Caller save.
3588 const Register tmp5 = r14; // Callee save.
3589
3590 const XMMRegister x0 = xmm0;
3591 const XMMRegister x1 = xmm1;
3592 const XMMRegister x2 = xmm2;
3593 BLOCK_COMMENT("Entry:");
3594 __ enter(); // required for proper stackwalking of RuntimeStub frame
3595
3596 #ifdef _WIN64
3597 setup_arg_regs(4);
3598 // For windows, since last argument is on stack, we need to move it to the appropriate register.
3599 __ movl(totalNumIter, Address(rsp, 6 * wordSize));
3600 // Save callee save registers.
3601 __ push_ppx(tmp3);
3602 __ push_ppx(tmp4);
3603 #endif
3604 __ push_ppx(tmp5);
3605
3606 // Rename temps used throughout the code
3607 const Register idx = tmp1;
3608 const Register numIterTmp = tmp2;
3609
3610 // Start idx from zero.
3611 __ xorl(idx, idx);
3612 // Compute interior pointer for new array. We do this so that we can use same index for both old and new arrays.
3613 __ lea(newArr, Address(newArr, newIdx, Address::times_4));
3614 __ movl(numIterTmp, totalNumIter);
3615
3616 // If vectorization is enabled, check if the number of iterations is at least 64
3617 // If not, then go to ShiftTwo shifting two numbers at a time
3618 if (VM_Version::supports_avx512_vbmi2()) {
3619 __ cmpl(totalNumIter, (AVX3Threshold/64));
3620 __ jcc(Assembler::less, ShiftTwo);
3621
3622 if (AVX3Threshold < 16 * 64) {
3623 __ cmpl(totalNumIter, 16);
3624 __ jcc(Assembler::less, ShiftTwo);
3625 }
3626 __ evpbroadcastd(x0, shiftCount, Assembler::AVX_512bit);
3627 __ subl(numIterTmp, 16);
3628 __ BIND(Shift512Loop);
3629 __ evmovdqul(x1, Address(oldArr, idx, Address::times_4), Assembler::AVX_512bit);
3630 __ evmovdqul(x2, Address(oldArr, idx, Address::times_4, 0x4), Assembler::AVX_512bit);
3631 __ vpshldvd(x1, x2, x0, Assembler::AVX_512bit);
3632 __ evmovdqul(Address(newArr, idx, Address::times_4), x1, Assembler::AVX_512bit);
3633 __ addl(idx, 16);
3634 __ subl(numIterTmp, 16);
3635 __ jcc(Assembler::greaterEqual, Shift512Loop);
3636 __ addl(numIterTmp, 16);
3637 }
3638 __ BIND(ShiftTwo);
3639 __ cmpl(totalNumIter, 1);
3640 __ jcc(Assembler::less, Exit);
3641 __ movl(tmp3, Address(oldArr, idx, Address::times_4));
3642 __ subl(numIterTmp, 2);
3643 __ jcc(Assembler::less, ShiftOne);
3644
3645 __ BIND(ShiftTwoLoop);
3646 __ movl(tmp4, Address(oldArr, idx, Address::times_4, 0x4));
3647 __ movl(tmp5, Address(oldArr, idx, Address::times_4, 0x8));
3648 __ shldl(tmp3, tmp4);
3649 __ shldl(tmp4, tmp5);
3650 __ movl(Address(newArr, idx, Address::times_4), tmp3);
3651 __ movl(Address(newArr, idx, Address::times_4, 0x4), tmp4);
3652 __ movl(tmp3, tmp5);
3653 __ addl(idx, 2);
3654 __ subl(numIterTmp, 2);
3655 __ jcc(Assembler::greaterEqual, ShiftTwoLoop);
3656
3657 // Do the last iteration
3658 __ BIND(ShiftOne);
3659 __ addl(numIterTmp, 2);
3660 __ cmpl(numIterTmp, 1);
3661 __ jcc(Assembler::less, Exit);
3662 __ movl(tmp4, Address(oldArr, idx, Address::times_4, 0x4));
3663 __ shldl(tmp3, tmp4);
3664 __ movl(Address(newArr, idx, Address::times_4), tmp3);
3665
3666 __ BIND(Exit);
3667 __ vzeroupper();
3668 // Restore callee save registers.
3669 __ pop_ppx(tmp5);
3670 #ifdef _WIN64
3671 __ pop_ppx(tmp4);
3672 __ pop_ppx(tmp3);
3673 restore_arg_regs();
3674 #endif
3675 __ leave(); // required for proper stackwalking of RuntimeStub frame
3676 __ ret(0);
3677
3678 return start;
3679 }
3680
3681 void StubGenerator::generate_libm_stubs() {
3682 if (UseLibmIntrinsic && InlineIntrinsics) {
3683 if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dsin)) {
3684 StubRoutines::_dsin = generate_libmSin(); // from stubGenerator_x86_64_sin.cpp
3685 }
3686 if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dcos)) {
3687 StubRoutines::_dcos = generate_libmCos(); // from stubGenerator_x86_64_cos.cpp
3688 }
3689 if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dtan)) {
3690 StubRoutines::_dtan = generate_libmTan(); // from stubGenerator_x86_64_tan.cpp
3691 }
3692 if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dsinh)) {
3693 StubRoutines::_dsinh = generate_libmSinh(); // from stubGenerator_x86_64_sinh.cpp
3694 }
3695 if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dtanh)) {
3696 StubRoutines::_dtanh = generate_libmTanh(); // from stubGenerator_x86_64_tanh.cpp
3697 }
3698 if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dcbrt)) {
3699 StubRoutines::_dcbrt = generate_libmCbrt(); // from stubGenerator_x86_64_cbrt.cpp
3700 }
3701 if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dexp)) {
3702 StubRoutines::_dexp = generate_libmExp(); // from stubGenerator_x86_64_exp.cpp
3703 }
3704 if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dpow)) {
3705 StubRoutines::_dpow = generate_libmPow(); // from stubGenerator_x86_64_pow.cpp
3706 }
3707 if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dlog)) {
3708 StubRoutines::_dlog = generate_libmLog(); // from stubGenerator_x86_64_log.cpp
3709 }
3710 if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dlog10)) {
3711 StubRoutines::_dlog10 = generate_libmLog10(); // from stubGenerator_x86_64_log.cpp
3712 }
3713 }
3714 }
3715
3716 /**
3717 * Arguments:
3718 *
3719 * Input:
3720 * c_rarg0 - float16 jshort
3721 *
3722 * Output:
3723 * xmm0 - float
3724 */
3725 address StubGenerator::generate_float16ToFloat() {
3726 StubId stub_id = StubId::stubgen_hf2f_id;
3727 StubCodeMark mark(this, stub_id);
3728
3729 address start = __ pc();
3730
3731 BLOCK_COMMENT("Entry:");
3732 // No need for RuntimeStub frame since it is called only during JIT compilation
3733
3734 // Load value into xmm0 and convert
3735 __ flt16_to_flt(xmm0, c_rarg0);
3736
3737 __ ret(0);
3738
3739 return start;
3740 }
3741
3742 /**
3743 * Arguments:
3744 *
3745 * Input:
3746 * xmm0 - float
3747 *
3748 * Output:
3749 * rax - float16 jshort
3750 */
3751 address StubGenerator::generate_floatToFloat16() {
3752 StubId stub_id = StubId::stubgen_f2hf_id;
3753 StubCodeMark mark(this, stub_id);
3754
3755 address start = __ pc();
3756
3757 BLOCK_COMMENT("Entry:");
3758 // No need for RuntimeStub frame since it is called only during JIT compilation
3759
3760 // Convert and put result into rax
3761 __ flt_to_flt16(rax, xmm0, xmm1);
3762
3763 __ ret(0);
3764
3765 return start;
3766 }
3767
3768 static void save_return_registers(MacroAssembler* masm) {
3769 masm->push_ppx(rax);
3770 if (InlineTypeReturnedAsFields) {
3771 masm->push(rdi);
3772 masm->push(rsi);
3773 masm->push(rdx);
3774 masm->push(rcx);
3775 masm->push(r8);
3776 masm->push(r9);
3777 }
3778 masm->push_d(xmm0);
3779 if (InlineTypeReturnedAsFields) {
3780 masm->push_d(xmm1);
3781 masm->push_d(xmm2);
3782 masm->push_d(xmm3);
3783 masm->push_d(xmm4);
3784 masm->push_d(xmm5);
3785 masm->push_d(xmm6);
3786 masm->push_d(xmm7);
3787 }
3788 #ifdef ASSERT
3789 masm->movq(rax, 0xBADC0FFE);
3790 masm->movq(rdi, rax);
3791 masm->movq(rsi, rax);
3792 masm->movq(rdx, rax);
3793 masm->movq(rcx, rax);
3794 masm->movq(r8, rax);
3795 masm->movq(r9, rax);
3796 masm->movq(xmm0, rax);
3797 masm->movq(xmm1, rax);
3798 masm->movq(xmm2, rax);
3799 masm->movq(xmm3, rax);
3800 masm->movq(xmm4, rax);
3801 masm->movq(xmm5, rax);
3802 masm->movq(xmm6, rax);
3803 masm->movq(xmm7, rax);
3804 #endif
3805 }
3806
3807 static void restore_return_registers(MacroAssembler* masm) {
3808 if (InlineTypeReturnedAsFields) {
3809 masm->pop_d(xmm7);
3810 masm->pop_d(xmm6);
3811 masm->pop_d(xmm5);
3812 masm->pop_d(xmm4);
3813 masm->pop_d(xmm3);
3814 masm->pop_d(xmm2);
3815 masm->pop_d(xmm1);
3816 }
3817 masm->pop_d(xmm0);
3818 if (InlineTypeReturnedAsFields) {
3819 masm->pop(r9);
3820 masm->pop(r8);
3821 masm->pop(rcx);
3822 masm->pop(rdx);
3823 masm->pop(rsi);
3824 masm->pop(rdi);
3825 }
3826 masm->pop_ppx(rax);
3827 }
3828
3829 address StubGenerator::generate_cont_thaw(StubId stub_id) {
3830 if (!Continuations::enabled()) return nullptr;
3831
3832 bool return_barrier;
3833 bool return_barrier_exception;
3834 Continuation::thaw_kind kind;
3835
3836 switch (stub_id) {
3837 case StubId::stubgen_cont_thaw_id:
3838 return_barrier = false;
3839 return_barrier_exception = false;
3840 kind = Continuation::thaw_top;
3841 break;
3842 case StubId::stubgen_cont_returnBarrier_id:
3843 return_barrier = true;
3844 return_barrier_exception = false;
3845 kind = Continuation::thaw_return_barrier;
3846 break;
3847 case StubId::stubgen_cont_returnBarrierExc_id:
3848 return_barrier = true;
3849 return_barrier_exception = true;
3850 kind = Continuation::thaw_return_barrier_exception;
3851 break;
3852 default:
3853 ShouldNotReachHere();
3854 }
3855 StubCodeMark mark(this, stub_id);
3856 address start = __ pc();
3857
3858 // TODO: Handle Valhalla return types. May require generating different return barriers.
3859
3860 if (!return_barrier) {
3861 // Pop return address. If we don't do this, we get a drift,
3862 // where the bottom-most frozen frame continuously grows.
3863 __ pop(c_rarg3);
3864 } else {
3865 __ movptr(rsp, Address(r15_thread, JavaThread::cont_entry_offset()));
3866 }
3867
3868 #ifdef ASSERT
3869 {
3870 Label L_good_sp;
3871 __ cmpptr(rsp, Address(r15_thread, JavaThread::cont_entry_offset()));
3872 __ jcc(Assembler::equal, L_good_sp);
3873 __ stop("Incorrect rsp at thaw entry");
3874 __ BIND(L_good_sp);
3875 }
3876 #endif // ASSERT
3877
3878 if (return_barrier) {
3879 // Preserve possible return value from a method returning to the return barrier.
3880 save_return_registers(_masm);
3881 }
3882
3883 __ movptr(c_rarg0, r15_thread);
3884 __ movptr(c_rarg1, (return_barrier ? 1 : 0));
3885 __ call_VM_leaf(CAST_FROM_FN_PTR(address, Continuation::prepare_thaw), 2);
3886 __ movptr(rbx, rax);
3887
3888 if (return_barrier) {
3889 // Restore return value from a method returning to the return barrier.
3890 // No safepoint in the call to thaw, so even an oop return value should be OK.
3891 restore_return_registers(_masm);
3892 }
3893
3894 #ifdef ASSERT
3895 {
3896 Label L_good_sp;
3897 __ cmpptr(rsp, Address(r15_thread, JavaThread::cont_entry_offset()));
3898 __ jcc(Assembler::equal, L_good_sp);
3899 __ stop("Incorrect rsp after prepare thaw");
3900 __ BIND(L_good_sp);
3901 }
3902 #endif // ASSERT
3903
3904 // rbx contains the size of the frames to thaw, 0 if overflow or no more frames
3905 Label L_thaw_success;
3906 __ testptr(rbx, rbx);
3907 __ jccb(Assembler::notZero, L_thaw_success);
3908 __ jump(RuntimeAddress(SharedRuntime::throw_StackOverflowError_entry()));
3909 __ bind(L_thaw_success);
3910
3911 // Make room for the thawed frames and align the stack.
3912 __ subptr(rsp, rbx);
3913 __ andptr(rsp, -StackAlignmentInBytes);
3914
3915 if (return_barrier) {
3916 // Preserve possible return value from a method returning to the return barrier. (Again.)
3917 save_return_registers(_masm);
3918 }
3919
3920 // If we want, we can templatize thaw by kind, and have three different entries.
3921 __ movptr(c_rarg0, r15_thread);
3922 __ movptr(c_rarg1, kind);
3923 __ call_VM_leaf(Continuation::thaw_entry(), 2);
3924 __ movptr(rbx, rax);
3925
3926 if (return_barrier) {
3927 // Restore return value from a method returning to the return barrier. (Again.)
3928 // No safepoint in the call to thaw, so even an oop return value should be OK.
3929 restore_return_registers(_masm);
3930 } else {
3931 // Return 0 (success) from doYield.
3932 __ xorptr(rax, rax);
3933 }
3934
3935 // After thawing, rbx is the SP of the yielding frame.
3936 // Move there, and then to saved RBP slot.
3937 __ movptr(rsp, rbx);
3938 __ subptr(rsp, 2*wordSize);
3939
3940 if (return_barrier_exception) {
3941 __ movptr(c_rarg0, r15_thread);
3942 __ movptr(c_rarg1, Address(rsp, wordSize)); // return address
3943
3944 // rax still holds the original exception oop, save it before the call
3945 __ push_ppx(rax);
3946
3947 __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), 2);
3948 __ movptr(rbx, rax);
3949
3950 // Continue at exception handler:
3951 // rax: exception oop
3952 // rbx: exception handler
3953 // rdx: exception pc
3954 __ pop_ppx(rax);
3955 __ verify_oop(rax);
3956 __ pop(rbp); // pop out RBP here too
3957 __ pop(rdx);
3958 __ jmp(rbx);
3959 } else {
3960 // We are "returning" into the topmost thawed frame; see Thaw::push_return_frame
3961 __ pop(rbp);
3962 __ ret(0);
3963 }
3964
3965 return start;
3966 }
3967
3968 address StubGenerator::generate_cont_thaw() {
3969 return generate_cont_thaw(StubId::stubgen_cont_thaw_id);
3970 }
3971
3972 // TODO: will probably need multiple return barriers depending on return type
3973
3974 address StubGenerator::generate_cont_returnBarrier() {
3975 return generate_cont_thaw(StubId::stubgen_cont_returnBarrier_id);
3976 }
3977
3978 address StubGenerator::generate_cont_returnBarrier_exception() {
3979 return generate_cont_thaw(StubId::stubgen_cont_returnBarrierExc_id);
3980 }
3981
3982 address StubGenerator::generate_cont_preempt_stub() {
3983 if (!Continuations::enabled()) return nullptr;
3984 StubId stub_id = StubId::stubgen_cont_preempt_id;
3985 StubCodeMark mark(this, stub_id);
3986 address start = __ pc();
3987
3988 __ reset_last_Java_frame(true);
3989
3990 // Set rsp to enterSpecial frame, i.e. remove all frames copied into the heap.
3991 __ movptr(rsp, Address(r15_thread, JavaThread::cont_entry_offset()));
3992
3993 Label preemption_cancelled;
3994 __ movbool(rscratch1, Address(r15_thread, JavaThread::preemption_cancelled_offset()));
3995 __ testbool(rscratch1);
3996 __ jcc(Assembler::notZero, preemption_cancelled);
3997
3998 // Remove enterSpecial frame from the stack and return to Continuation.run() to unmount.
3999 SharedRuntime::continuation_enter_cleanup(_masm);
4000 __ pop(rbp);
4001 __ ret(0);
4002
4003 // We acquired the monitor after freezing the frames so call thaw to continue execution.
4004 __ bind(preemption_cancelled);
4005 __ movbool(Address(r15_thread, JavaThread::preemption_cancelled_offset()), false);
4006 __ lea(rbp, Address(rsp, checked_cast<int32_t>(ContinuationEntry::size())));
4007 __ movptr(rscratch1, ExternalAddress(ContinuationEntry::thaw_call_pc_address()));
4008 __ jmp(rscratch1);
4009
4010 return start;
4011 }
4012
4013 // exception handler for upcall stubs
4014 address StubGenerator::generate_upcall_stub_exception_handler() {
4015 StubId stub_id = StubId::stubgen_upcall_stub_exception_handler_id;
4016 StubCodeMark mark(this, stub_id);
4017 address start = __ pc();
4018
4019 // native caller has no idea how to handle exceptions
4020 // we just crash here. Up to callee to catch exceptions.
4021 __ verify_oop(rax);
4022 __ vzeroupper();
4023 __ mov(c_rarg0, rax);
4024 __ andptr(rsp, -StackAlignmentInBytes); // align stack as required by ABI
4025 __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
4026 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, UpcallLinker::handle_uncaught_exception)));
4027 __ should_not_reach_here();
4028
4029 return start;
4030 }
4031
4032 // load Method* target of MethodHandle
4033 // j_rarg0 = jobject receiver
4034 // rbx = result
4035 address StubGenerator::generate_upcall_stub_load_target() {
4036 StubId stub_id = StubId::stubgen_upcall_stub_load_target_id;
4037 StubCodeMark mark(this, stub_id);
4038 address start = __ pc();
4039
4040 __ resolve_global_jobject(j_rarg0, rscratch1);
4041 // Load target method from receiver
4042 __ load_heap_oop(rbx, Address(j_rarg0, java_lang_invoke_MethodHandle::form_offset()), rscratch1);
4043 __ load_heap_oop(rbx, Address(rbx, java_lang_invoke_LambdaForm::vmentry_offset()), rscratch1);
4044 __ load_heap_oop(rbx, Address(rbx, java_lang_invoke_MemberName::method_offset()), rscratch1);
4045 __ access_load_at(T_ADDRESS, IN_HEAP, rbx,
4046 Address(rbx, java_lang_invoke_ResolvedMethodName::vmtarget_offset()),
4047 noreg);
4048 __ movptr(Address(r15_thread, JavaThread::callee_target_offset()), rbx); // just in case callee is deoptimized
4049
4050 __ ret(0);
4051
4052 return start;
4053 }
4054
4055 void StubGenerator::generate_lookup_secondary_supers_table_stub() {
4056 StubId stub_id = StubId::stubgen_lookup_secondary_supers_table_id;
4057 StubCodeMark mark(this, stub_id);
4058
4059 const Register
4060 r_super_klass = rax,
4061 r_sub_klass = rsi,
4062 result = rdi;
4063
4064 for (int slot = 0; slot < Klass::SECONDARY_SUPERS_TABLE_SIZE; slot++) {
4065 StubRoutines::_lookup_secondary_supers_table_stubs[slot] = __ pc();
4066 __ lookup_secondary_supers_table_const(r_sub_klass, r_super_klass,
4067 rdx, rcx, rbx, r11, // temps
4068 result,
4069 slot);
4070 __ ret(0);
4071 }
4072 }
4073
4074 // Slow path implementation for UseSecondarySupersTable.
4075 address StubGenerator::generate_lookup_secondary_supers_table_slow_path_stub() {
4076 StubId stub_id = StubId::stubgen_lookup_secondary_supers_table_slow_path_id;
4077 StubCodeMark mark(this, stub_id);
4078
4079 address start = __ pc();
4080
4081 const Register
4082 r_super_klass = rax,
4083 r_array_base = rbx,
4084 r_array_index = rdx,
4085 r_sub_klass = rsi,
4086 r_bitmap = r11,
4087 result = rdi;
4088
4089 Label L_success;
4090 __ lookup_secondary_supers_table_slow_path(r_super_klass, r_array_base, r_array_index, r_bitmap,
4091 rcx, rdi, // temps
4092 &L_success);
4093 // bind(L_failure);
4094 __ movl(result, 1);
4095 __ ret(0);
4096
4097 __ bind(L_success);
4098 __ movl(result, 0);
4099 __ ret(0);
4100
4101 return start;
4102 }
4103
4104 void StubGenerator::create_control_words() {
4105 // Round to nearest, 64-bit mode, exceptions masked, flags specialized
4106 StubRoutines::x86::_mxcsr_std = EnableX86ECoreOpts ? 0x1FBF : 0x1F80;
4107 // Round to zero, 64-bit mode, exceptions masked, flags specialized
4108 StubRoutines::x86::_mxcsr_rz = EnableX86ECoreOpts ? 0x7FBF : 0x7F80;
4109 }
4110
4111 // Initialization
4112 void StubGenerator::generate_preuniverse_stubs() {
4113 // atomic calls
4114 StubRoutines::_fence_entry = generate_orderaccess_fence();
4115 }
4116
4117 void StubGenerator::generate_initial_stubs() {
4118 // Generates all stubs and initializes the entry points
4119
4120 // This platform-specific settings are needed by generate_call_stub()
4121 create_control_words();
4122
4123 // Initialize table for unsafe copy memeory check.
4124 if (UnsafeMemoryAccess::_table == nullptr) {
4125 UnsafeMemoryAccess::create_table(16 + 4); // 16 for copyMemory; 4 for setMemory
4126 }
4127
4128 // entry points that exist in all platforms Note: This is code
4129 // that could be shared among different platforms - however the
4130 // benefit seems to be smaller than the disadvantage of having a
4131 // much more complicated generator structure. See also comment in
4132 // stubRoutines.hpp.
4133
4134 StubRoutines::_forward_exception_entry = generate_forward_exception();
4135
4136 // Generate these first because they are called from other stubs
4137 if (InlineTypeReturnedAsFields) {
4138 StubRoutines::_load_inline_type_fields_in_regs =
4139 generate_return_value_stub(CAST_FROM_FN_PTR(address, SharedRuntime::load_inline_type_fields_in_regs),
4140 "load_inline_type_fields_in_regs", false);
4141 StubRoutines::_store_inline_type_fields_to_buf =
4142 generate_return_value_stub(CAST_FROM_FN_PTR(address, SharedRuntime::store_inline_type_fields_to_buf),
4143 "store_inline_type_fields_to_buf", true);
4144 }
4145
4146 StubRoutines::_call_stub_entry =
4147 generate_call_stub(StubRoutines::_call_stub_return_address);
4148
4149 // is referenced by megamorphic call
4150 StubRoutines::_catch_exception_entry = generate_catch_exception();
4151
4152 // platform dependent
4153 StubRoutines::x86::_verify_mxcsr_entry = generate_verify_mxcsr();
4154
4155 StubRoutines::x86::_f2i_fixup = generate_f2i_fixup();
4156 StubRoutines::x86::_f2l_fixup = generate_f2l_fixup();
4157 StubRoutines::x86::_d2i_fixup = generate_d2i_fixup();
4158 StubRoutines::x86::_d2l_fixup = generate_d2l_fixup();
4159
4160 StubRoutines::x86::_float_sign_mask = generate_fp_mask(StubId::stubgen_float_sign_mask_id, 0x7FFFFFFF7FFFFFFF);
4161 StubRoutines::x86::_float_sign_flip = generate_fp_mask(StubId::stubgen_float_sign_flip_id, 0x8000000080000000);
4162 StubRoutines::x86::_double_sign_mask = generate_fp_mask(StubId::stubgen_double_sign_mask_id, 0x7FFFFFFFFFFFFFFF);
4163 StubRoutines::x86::_double_sign_flip = generate_fp_mask(StubId::stubgen_double_sign_flip_id, 0x8000000000000000);
4164
4165 if (UseCRC32Intrinsics) {
4166 StubRoutines::_updateBytesCRC32 = generate_updateBytesCRC32();
4167 }
4168
4169 if (UseCRC32CIntrinsics) {
4170 bool supports_clmul = VM_Version::supports_clmul();
4171 StubRoutines::_updateBytesCRC32C = generate_updateBytesCRC32C(supports_clmul);
4172 }
4173
4174 if (VM_Version::supports_float16()) {
4175 // For results consistency both intrinsics should be enabled.
4176 // vmIntrinsics checks InlineIntrinsics flag, no need to check it here.
4177 if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_float16ToFloat) &&
4178 vmIntrinsics::is_intrinsic_available(vmIntrinsics::_floatToFloat16)) {
4179 StubRoutines::_hf2f = generate_float16ToFloat();
4180 StubRoutines::_f2hf = generate_floatToFloat16();
4181 }
4182 }
4183
4184 generate_libm_stubs();
4185
4186 StubRoutines::_fmod = generate_libmFmod(); // from stubGenerator_x86_64_fmod.cpp
4187 }
4188
4189 // Call here from the interpreter or compiled code to either load
4190 // multiple returned values from the inline type instance being
4191 // returned to registers or to store returned values to a newly
4192 // allocated inline type instance.
4193 // Register is a class, but it would be assigned numerical value.
4194 // "0" is assigned for xmm0. Thus we need to ignore -Wnonnull.
4195 PRAGMA_DIAG_PUSH
4196 PRAGMA_NONNULL_IGNORED
4197 address StubGenerator::generate_return_value_stub(address destination, const char* name, bool has_res) {
4198 // We need to save all registers the calling convention may use so
4199 // the runtime calls read or update those registers. This needs to
4200 // be in sync with SharedRuntime::java_return_convention().
4201 enum layout {
4202 pad_off = frame::arg_reg_save_area_bytes/BytesPerInt, pad_off_2,
4203 rax_off, rax_off_2,
4204 j_rarg5_off, j_rarg5_2,
4205 j_rarg4_off, j_rarg4_2,
4206 j_rarg3_off, j_rarg3_2,
4207 j_rarg2_off, j_rarg2_2,
4208 j_rarg1_off, j_rarg1_2,
4209 j_rarg0_off, j_rarg0_2,
4210 j_farg0_off, j_farg0_2,
4211 j_farg1_off, j_farg1_2,
4212 j_farg2_off, j_farg2_2,
4213 j_farg3_off, j_farg3_2,
4214 j_farg4_off, j_farg4_2,
4215 j_farg5_off, j_farg5_2,
4216 j_farg6_off, j_farg6_2,
4217 j_farg7_off, j_farg7_2,
4218 rbp_off, rbp_off_2,
4219 return_off, return_off_2,
4220
4221 framesize
4222 };
4223
4224 CodeBuffer buffer(name, 1000, 512);
4225 MacroAssembler* _masm = new MacroAssembler(&buffer);
4226
4227 int frame_size_in_bytes = align_up(framesize*BytesPerInt, 16);
4228 assert(frame_size_in_bytes == framesize*BytesPerInt, "misaligned");
4229 int frame_size_in_slots = frame_size_in_bytes / BytesPerInt;
4230 int frame_size_in_words = frame_size_in_bytes / wordSize;
4231
4232 OopMapSet *oop_maps = new OopMapSet();
4233 OopMap* map = new OopMap(frame_size_in_slots, 0);
4234
4235 map->set_callee_saved(VMRegImpl::stack2reg(rax_off), rax->as_VMReg());
4236 map->set_callee_saved(VMRegImpl::stack2reg(j_rarg5_off), j_rarg5->as_VMReg());
4237 map->set_callee_saved(VMRegImpl::stack2reg(j_rarg4_off), j_rarg4->as_VMReg());
4238 map->set_callee_saved(VMRegImpl::stack2reg(j_rarg3_off), j_rarg3->as_VMReg());
4239 map->set_callee_saved(VMRegImpl::stack2reg(j_rarg2_off), j_rarg2->as_VMReg());
4240 map->set_callee_saved(VMRegImpl::stack2reg(j_rarg1_off), j_rarg1->as_VMReg());
4241 map->set_callee_saved(VMRegImpl::stack2reg(j_rarg0_off), j_rarg0->as_VMReg());
4242 map->set_callee_saved(VMRegImpl::stack2reg(j_farg0_off), j_farg0->as_VMReg());
4243 map->set_callee_saved(VMRegImpl::stack2reg(j_farg1_off), j_farg1->as_VMReg());
4244 map->set_callee_saved(VMRegImpl::stack2reg(j_farg2_off), j_farg2->as_VMReg());
4245 map->set_callee_saved(VMRegImpl::stack2reg(j_farg3_off), j_farg3->as_VMReg());
4246 map->set_callee_saved(VMRegImpl::stack2reg(j_farg4_off), j_farg4->as_VMReg());
4247 map->set_callee_saved(VMRegImpl::stack2reg(j_farg5_off), j_farg5->as_VMReg());
4248 map->set_callee_saved(VMRegImpl::stack2reg(j_farg6_off), j_farg6->as_VMReg());
4249 map->set_callee_saved(VMRegImpl::stack2reg(j_farg7_off), j_farg7->as_VMReg());
4250
4251 int start = __ offset();
4252
4253 __ subptr(rsp, frame_size_in_bytes - 8 /* return address*/);
4254
4255 __ movptr(Address(rsp, rbp_off * BytesPerInt), rbp);
4256 __ movdbl(Address(rsp, j_farg7_off * BytesPerInt), j_farg7);
4257 __ movdbl(Address(rsp, j_farg6_off * BytesPerInt), j_farg6);
4258 __ movdbl(Address(rsp, j_farg5_off * BytesPerInt), j_farg5);
4259 __ movdbl(Address(rsp, j_farg4_off * BytesPerInt), j_farg4);
4260 __ movdbl(Address(rsp, j_farg3_off * BytesPerInt), j_farg3);
4261 __ movdbl(Address(rsp, j_farg2_off * BytesPerInt), j_farg2);
4262 __ movdbl(Address(rsp, j_farg1_off * BytesPerInt), j_farg1);
4263 __ movdbl(Address(rsp, j_farg0_off * BytesPerInt), j_farg0);
4264
4265 __ movptr(Address(rsp, j_rarg0_off * BytesPerInt), j_rarg0);
4266 __ movptr(Address(rsp, j_rarg1_off * BytesPerInt), j_rarg1);
4267 __ movptr(Address(rsp, j_rarg2_off * BytesPerInt), j_rarg2);
4268 __ movptr(Address(rsp, j_rarg3_off * BytesPerInt), j_rarg3);
4269 __ movptr(Address(rsp, j_rarg4_off * BytesPerInt), j_rarg4);
4270 __ movptr(Address(rsp, j_rarg5_off * BytesPerInt), j_rarg5);
4271 __ movptr(Address(rsp, rax_off * BytesPerInt), rax);
4272
4273 int frame_complete = __ offset();
4274
4275 __ set_last_Java_frame(noreg, noreg, nullptr, rscratch1);
4276
4277 __ mov(c_rarg0, r15_thread);
4278 __ mov(c_rarg1, rax);
4279
4280 __ call(RuntimeAddress(destination));
4281
4282 // Set an oopmap for the call site.
4283
4284 oop_maps->add_gc_map( __ offset() - start, map);
4285
4286 // clear last_Java_sp
4287 __ reset_last_Java_frame(false);
4288
4289 __ movptr(rbp, Address(rsp, rbp_off * BytesPerInt));
4290 __ movdbl(j_farg7, Address(rsp, j_farg7_off * BytesPerInt));
4291 __ movdbl(j_farg6, Address(rsp, j_farg6_off * BytesPerInt));
4292 __ movdbl(j_farg5, Address(rsp, j_farg5_off * BytesPerInt));
4293 __ movdbl(j_farg4, Address(rsp, j_farg4_off * BytesPerInt));
4294 __ movdbl(j_farg3, Address(rsp, j_farg3_off * BytesPerInt));
4295 __ movdbl(j_farg2, Address(rsp, j_farg2_off * BytesPerInt));
4296 __ movdbl(j_farg1, Address(rsp, j_farg1_off * BytesPerInt));
4297 __ movdbl(j_farg0, Address(rsp, j_farg0_off * BytesPerInt));
4298
4299 __ movptr(j_rarg0, Address(rsp, j_rarg0_off * BytesPerInt));
4300 __ movptr(j_rarg1, Address(rsp, j_rarg1_off * BytesPerInt));
4301 __ movptr(j_rarg2, Address(rsp, j_rarg2_off * BytesPerInt));
4302 __ movptr(j_rarg3, Address(rsp, j_rarg3_off * BytesPerInt));
4303 __ movptr(j_rarg4, Address(rsp, j_rarg4_off * BytesPerInt));
4304 __ movptr(j_rarg5, Address(rsp, j_rarg5_off * BytesPerInt));
4305 __ movptr(rax, Address(rsp, rax_off * BytesPerInt));
4306
4307 __ addptr(rsp, frame_size_in_bytes-8);
4308
4309 // check for pending exceptions
4310 Label pending;
4311 __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
4312 __ jcc(Assembler::notEqual, pending);
4313
4314 if (has_res) {
4315 __ get_vm_result_oop(rax);
4316 }
4317
4318 __ ret(0);
4319
4320 __ bind(pending);
4321
4322 __ movptr(rax, Address(r15_thread, Thread::pending_exception_offset()));
4323 __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
4324
4325 // -------------
4326 // make sure all code is generated
4327 _masm->flush();
4328
4329 RuntimeStub* stub = RuntimeStub::new_runtime_stub(name, &buffer, frame_complete, frame_size_in_words, oop_maps, false);
4330 return stub->entry_point();
4331 }
4332
4333 void StubGenerator::generate_continuation_stubs() {
4334 // Continuation stubs:
4335 StubRoutines::_cont_thaw = generate_cont_thaw();
4336 StubRoutines::_cont_returnBarrier = generate_cont_returnBarrier();
4337 StubRoutines::_cont_returnBarrierExc = generate_cont_returnBarrier_exception();
4338 StubRoutines::_cont_preempt_stub = generate_cont_preempt_stub();
4339 }
4340
4341 void StubGenerator::generate_final_stubs() {
4342 // Generates the rest of stubs and initializes the entry points
4343
4344 // support for verify_oop (must happen after universe_init)
4345 if (VerifyOops) {
4346 StubRoutines::_verify_oop_subroutine_entry = generate_verify_oop();
4347 }
4348
4349 // arraycopy stubs used by compilers
4350 generate_arraycopy_stubs();
4351
4352 StubRoutines::_method_entry_barrier = generate_method_entry_barrier();
4353
4354 #ifdef COMPILER2
4355 if (UseSecondarySupersTable) {
4356 StubRoutines::_lookup_secondary_supers_table_slow_path_stub = generate_lookup_secondary_supers_table_slow_path_stub();
4357 if (! InlineSecondarySupersTest) {
4358 generate_lookup_secondary_supers_table_stub();
4359 }
4360 }
4361 #endif // COMPILER2
4362
4363 if (UseVectorizedMismatchIntrinsic) {
4364 StubRoutines::_vectorizedMismatch = generate_vectorizedMismatch();
4365 }
4366
4367 StubRoutines::_upcall_stub_exception_handler = generate_upcall_stub_exception_handler();
4368 StubRoutines::_upcall_stub_load_target = generate_upcall_stub_load_target();
4369 }
4370
4371 void StubGenerator::generate_compiler_stubs() {
4372 #if COMPILER2_OR_JVMCI
4373
4374 // Entry points that are C2 compiler specific.
4375
4376 StubRoutines::x86::_vector_float_sign_mask = generate_vector_mask(StubId::stubgen_vector_float_sign_mask_id, 0x7FFFFFFF7FFFFFFF);
4377 StubRoutines::x86::_vector_float_sign_flip = generate_vector_mask(StubId::stubgen_vector_float_sign_flip_id, 0x8000000080000000);
4378 StubRoutines::x86::_vector_double_sign_mask = generate_vector_mask(StubId::stubgen_vector_double_sign_mask_id, 0x7FFFFFFFFFFFFFFF);
4379 StubRoutines::x86::_vector_double_sign_flip = generate_vector_mask(StubId::stubgen_vector_double_sign_flip_id, 0x8000000000000000);
4380 StubRoutines::x86::_vector_all_bits_set = generate_vector_mask(StubId::stubgen_vector_all_bits_set_id, 0xFFFFFFFFFFFFFFFF);
4381 StubRoutines::x86::_vector_int_mask_cmp_bits = generate_vector_mask(StubId::stubgen_vector_int_mask_cmp_bits_id, 0x0000000100000001);
4382 StubRoutines::x86::_vector_short_to_byte_mask = generate_vector_mask(StubId::stubgen_vector_short_to_byte_mask_id, 0x00ff00ff00ff00ff);
4383 StubRoutines::x86::_vector_byte_perm_mask = generate_vector_byte_perm_mask();
4384 StubRoutines::x86::_vector_int_to_byte_mask = generate_vector_mask(StubId::stubgen_vector_int_to_byte_mask_id, 0x000000ff000000ff);
4385 StubRoutines::x86::_vector_int_to_short_mask = generate_vector_mask(StubId::stubgen_vector_int_to_short_mask_id, 0x0000ffff0000ffff);
4386 StubRoutines::x86::_vector_32_bit_mask = generate_vector_custom_i32(StubId::stubgen_vector_32_bit_mask_id, Assembler::AVX_512bit,
4387 0xFFFFFFFF, 0, 0, 0);
4388 StubRoutines::x86::_vector_64_bit_mask = generate_vector_custom_i32(StubId::stubgen_vector_64_bit_mask_id, Assembler::AVX_512bit,
4389 0xFFFFFFFF, 0xFFFFFFFF, 0, 0);
4390 StubRoutines::x86::_vector_int_shuffle_mask = generate_vector_mask(StubId::stubgen_vector_int_shuffle_mask_id, 0x0302010003020100);
4391 StubRoutines::x86::_vector_byte_shuffle_mask = generate_vector_byte_shuffle_mask();
4392 StubRoutines::x86::_vector_short_shuffle_mask = generate_vector_mask(StubId::stubgen_vector_short_shuffle_mask_id, 0x0100010001000100);
4393 StubRoutines::x86::_vector_long_shuffle_mask = generate_vector_mask(StubId::stubgen_vector_long_shuffle_mask_id, 0x0000000100000000);
4394 StubRoutines::x86::_vector_long_sign_mask = generate_vector_mask(StubId::stubgen_vector_long_sign_mask_id, 0x8000000000000000);
4395 StubRoutines::x86::_vector_iota_indices = generate_iota_indices();
4396 StubRoutines::x86::_vector_count_leading_zeros_lut = generate_count_leading_zeros_lut();
4397 StubRoutines::x86::_vector_reverse_bit_lut = generate_vector_reverse_bit_lut();
4398 StubRoutines::x86::_vector_reverse_byte_perm_mask_long = generate_vector_reverse_byte_perm_mask_long();
4399 StubRoutines::x86::_vector_reverse_byte_perm_mask_int = generate_vector_reverse_byte_perm_mask_int();
4400 StubRoutines::x86::_vector_reverse_byte_perm_mask_short = generate_vector_reverse_byte_perm_mask_short();
4401
4402 if (VM_Version::supports_avx2() && !VM_Version::supports_avx512vl()) {
4403 StubRoutines::x86::_compress_perm_table32 = generate_compress_perm_table(StubId::stubgen_compress_perm_table32_id);
4404 StubRoutines::x86::_compress_perm_table64 = generate_compress_perm_table(StubId::stubgen_compress_perm_table64_id);
4405 StubRoutines::x86::_expand_perm_table32 = generate_expand_perm_table(StubId::stubgen_expand_perm_table32_id);
4406 StubRoutines::x86::_expand_perm_table64 = generate_expand_perm_table(StubId::stubgen_expand_perm_table64_id);
4407 }
4408
4409 if (VM_Version::supports_avx2() && !VM_Version::supports_avx512_vpopcntdq()) {
4410 // lut implementation influenced by counting 1s algorithm from section 5-1 of Hackers' Delight.
4411 StubRoutines::x86::_vector_popcount_lut = generate_popcount_avx_lut();
4412 }
4413
4414 generate_aes_stubs();
4415
4416 generate_ghash_stubs();
4417
4418 generate_chacha_stubs();
4419
4420 generate_kyber_stubs();
4421
4422 generate_dilithium_stubs();
4423
4424 generate_sha3_stubs();
4425
4426 // data cache line writeback
4427 StubRoutines::_data_cache_writeback = generate_data_cache_writeback();
4428 StubRoutines::_data_cache_writeback_sync = generate_data_cache_writeback_sync();
4429
4430 #ifdef COMPILER2
4431 if ((UseAVX == 2) && EnableX86ECoreOpts) {
4432 generate_string_indexof(StubRoutines::_string_indexof_array);
4433 }
4434 #endif
4435
4436 if (UseAdler32Intrinsics) {
4437 StubRoutines::_updateBytesAdler32 = generate_updateBytesAdler32();
4438 }
4439
4440 if (UsePoly1305Intrinsics) {
4441 StubRoutines::_poly1305_processBlocks = generate_poly1305_processBlocks();
4442 }
4443
4444 if (UseIntPolyIntrinsics) {
4445 StubRoutines::_intpoly_montgomeryMult_P256 = generate_intpoly_montgomeryMult_P256();
4446 StubRoutines::_intpoly_assign = generate_intpoly_assign();
4447 }
4448
4449 if (UseMD5Intrinsics) {
4450 StubRoutines::_md5_implCompress = generate_md5_implCompress(StubId::stubgen_md5_implCompress_id);
4451 StubRoutines::_md5_implCompressMB = generate_md5_implCompress(StubId::stubgen_md5_implCompressMB_id);
4452 }
4453
4454 if (UseSHA1Intrinsics) {
4455 StubRoutines::x86::_upper_word_mask_addr = generate_upper_word_mask();
4456 StubRoutines::x86::_shuffle_byte_flip_mask_addr = generate_shuffle_byte_flip_mask();
4457 StubRoutines::_sha1_implCompress = generate_sha1_implCompress(StubId::stubgen_sha1_implCompress_id);
4458 StubRoutines::_sha1_implCompressMB = generate_sha1_implCompress(StubId::stubgen_sha1_implCompressMB_id);
4459 }
4460
4461 if (UseSHA256Intrinsics) {
4462 StubRoutines::x86::_k256_adr = (address)StubRoutines::x86::_k256;
4463 char* dst = (char*)StubRoutines::x86::_k256_W;
4464 char* src = (char*)StubRoutines::x86::_k256;
4465 for (int ii = 0; ii < 16; ++ii) {
4466 memcpy(dst + 32 * ii, src + 16 * ii, 16);
4467 memcpy(dst + 32 * ii + 16, src + 16 * ii, 16);
4468 }
4469 StubRoutines::x86::_k256_W_adr = (address)StubRoutines::x86::_k256_W;
4470 StubRoutines::x86::_pshuffle_byte_flip_mask_addr = generate_pshuffle_byte_flip_mask();
4471 StubRoutines::_sha256_implCompress = generate_sha256_implCompress(StubId::stubgen_sha256_implCompress_id);
4472 StubRoutines::_sha256_implCompressMB = generate_sha256_implCompress(StubId::stubgen_sha256_implCompressMB_id);
4473 }
4474
4475 if (UseSHA512Intrinsics) {
4476 StubRoutines::x86::_k512_W_addr = (address)StubRoutines::x86::_k512_W;
4477 StubRoutines::x86::_pshuffle_byte_flip_mask_addr_sha512 = generate_pshuffle_byte_flip_mask_sha512();
4478 StubRoutines::_sha512_implCompress = generate_sha512_implCompress(StubId::stubgen_sha512_implCompress_id);
4479 StubRoutines::_sha512_implCompressMB = generate_sha512_implCompress(StubId::stubgen_sha512_implCompressMB_id);
4480 }
4481
4482 if (UseBASE64Intrinsics) {
4483 if(VM_Version::supports_avx2()) {
4484 StubRoutines::x86::_avx2_shuffle_base64 = base64_avx2_shuffle_addr();
4485 StubRoutines::x86::_avx2_input_mask_base64 = base64_avx2_input_mask_addr();
4486 StubRoutines::x86::_avx2_lut_base64 = base64_avx2_lut_addr();
4487 StubRoutines::x86::_avx2_decode_tables_base64 = base64_AVX2_decode_tables_addr();
4488 StubRoutines::x86::_avx2_decode_lut_tables_base64 = base64_AVX2_decode_LUT_tables_addr();
4489 }
4490 StubRoutines::x86::_encoding_table_base64 = base64_encoding_table_addr();
4491 if (VM_Version::supports_avx512_vbmi()) {
4492 StubRoutines::x86::_shuffle_base64 = base64_shuffle_addr();
4493 StubRoutines::x86::_lookup_lo_base64 = base64_vbmi_lookup_lo_addr();
4494 StubRoutines::x86::_lookup_hi_base64 = base64_vbmi_lookup_hi_addr();
4495 StubRoutines::x86::_lookup_lo_base64url = base64_vbmi_lookup_lo_url_addr();
4496 StubRoutines::x86::_lookup_hi_base64url = base64_vbmi_lookup_hi_url_addr();
4497 StubRoutines::x86::_pack_vec_base64 = base64_vbmi_pack_vec_addr();
4498 StubRoutines::x86::_join_0_1_base64 = base64_vbmi_join_0_1_addr();
4499 StubRoutines::x86::_join_1_2_base64 = base64_vbmi_join_1_2_addr();
4500 StubRoutines::x86::_join_2_3_base64 = base64_vbmi_join_2_3_addr();
4501 }
4502 StubRoutines::x86::_decoding_table_base64 = base64_decoding_table_addr();
4503 StubRoutines::_base64_encodeBlock = generate_base64_encodeBlock();
4504 StubRoutines::_base64_decodeBlock = generate_base64_decodeBlock();
4505 }
4506
4507 #ifdef COMPILER2
4508 if (UseMultiplyToLenIntrinsic) {
4509 StubRoutines::_multiplyToLen = generate_multiplyToLen();
4510 }
4511 if (UseSquareToLenIntrinsic) {
4512 StubRoutines::_squareToLen = generate_squareToLen();
4513 }
4514 if (UseMulAddIntrinsic) {
4515 StubRoutines::_mulAdd = generate_mulAdd();
4516 }
4517 if (VM_Version::supports_avx512_vbmi2()) {
4518 StubRoutines::_bigIntegerRightShiftWorker = generate_bigIntegerRightShift();
4519 StubRoutines::_bigIntegerLeftShiftWorker = generate_bigIntegerLeftShift();
4520 }
4521 if (UseMontgomeryMultiplyIntrinsic) {
4522 StubRoutines::_montgomeryMultiply
4523 = CAST_FROM_FN_PTR(address, SharedRuntime::montgomery_multiply);
4524 }
4525 if (UseMontgomerySquareIntrinsic) {
4526 StubRoutines::_montgomerySquare
4527 = CAST_FROM_FN_PTR(address, SharedRuntime::montgomery_square);
4528 }
4529
4530 // Load x86_64_sort library on supported hardware to enable SIMD sort and partition intrinsics
4531
4532 if (VM_Version::supports_avx512dq() || VM_Version::supports_avx2()) {
4533 void *libsimdsort = nullptr;
4534 char ebuf_[1024];
4535 char dll_name_simd_sort[JVM_MAXPATHLEN];
4536 if (os::dll_locate_lib(dll_name_simd_sort, sizeof(dll_name_simd_sort), Arguments::get_dll_dir(), "simdsort")) {
4537 libsimdsort = os::dll_load(dll_name_simd_sort, ebuf_, sizeof ebuf_);
4538 }
4539 // Get addresses for SIMD sort and partition routines
4540 if (libsimdsort != nullptr) {
4541 log_info(library)("Loaded library %s, handle " INTPTR_FORMAT, JNI_LIB_PREFIX "simdsort" JNI_LIB_SUFFIX, p2i(libsimdsort));
4542
4543 os::snprintf_checked(ebuf_, sizeof(ebuf_), VM_Version::supports_avx512_simd_sort() ? "avx512_sort" : "avx2_sort");
4544 StubRoutines::_array_sort = (address)os::dll_lookup(libsimdsort, ebuf_);
4545
4546 os::snprintf_checked(ebuf_, sizeof(ebuf_), VM_Version::supports_avx512_simd_sort() ? "avx512_partition" : "avx2_partition");
4547 StubRoutines::_array_partition = (address)os::dll_lookup(libsimdsort, ebuf_);
4548 }
4549 }
4550
4551 #endif // COMPILER2
4552 #endif // COMPILER2_OR_JVMCI
4553 }
4554
4555 StubGenerator::StubGenerator(CodeBuffer* code, BlobId blob_id) : StubCodeGenerator(code, blob_id) {
4556 switch(blob_id) {
4557 case BlobId::stubgen_preuniverse_id:
4558 generate_preuniverse_stubs();
4559 break;
4560 case BlobId::stubgen_initial_id:
4561 generate_initial_stubs();
4562 break;
4563 case BlobId::stubgen_continuation_id:
4564 generate_continuation_stubs();
4565 break;
4566 case BlobId::stubgen_compiler_id:
4567 generate_compiler_stubs();
4568 break;
4569 case BlobId::stubgen_final_id:
4570 generate_final_stubs();
4571 break;
4572 default:
4573 fatal("unexpected blob id: %s", StubInfo::name(blob_id));
4574 break;
4575 };
4576 }
4577
4578 void StubGenerator_generate(CodeBuffer* code, BlobId blob_id) {
4579 StubGenerator g(code, blob_id);
4580 }
4581
4582 #undef __