1 /*
2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2014, 2024, Red Hat Inc. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #include "asm/assembler.hpp"
27 #include "asm/assembler.inline.hpp"
28 #include "ci/ciEnv.hpp"
29 #include "code/compiledIC.hpp"
30 #include "compiler/compileTask.hpp"
31 #include "compiler/disassembler.hpp"
32 #include "compiler/oopMap.hpp"
33 #include "gc/shared/barrierSet.hpp"
34 #include "gc/shared/barrierSetAssembler.hpp"
35 #include "gc/shared/cardTableBarrierSet.hpp"
36 #include "gc/shared/cardTable.hpp"
37 #include "gc/shared/collectedHeap.hpp"
38 #include "gc/shared/tlab_globals.hpp"
39 #include "interpreter/bytecodeHistogram.hpp"
40 #include "interpreter/interpreter.hpp"
41 #include "interpreter/interpreterRuntime.hpp"
42 #include "jvm.h"
43 #include "memory/resourceArea.hpp"
44 #include "memory/universe.hpp"
45 #include "nativeInst_aarch64.hpp"
46 #include "oops/accessDecorators.hpp"
47 #include "oops/compressedKlass.inline.hpp"
48 #include "oops/compressedOops.inline.hpp"
49 #include "oops/klass.inline.hpp"
50 #include "runtime/continuation.hpp"
51 #include "runtime/icache.hpp"
52 #include "runtime/interfaceSupport.inline.hpp"
53 #include "runtime/javaThread.hpp"
54 #include "runtime/jniHandles.inline.hpp"
55 #include "runtime/sharedRuntime.hpp"
56 #include "runtime/stubRoutines.hpp"
57 #include "utilities/globalDefinitions.hpp"
58 #include "utilities/powerOfTwo.hpp"
59 #ifdef COMPILER1
60 #include "c1/c1_LIRAssembler.hpp"
61 #endif
62 #ifdef COMPILER2
63 #include "oops/oop.hpp"
64 #include "opto/compile.hpp"
65 #include "opto/node.hpp"
66 #include "opto/output.hpp"
67 #endif
68
69 #include <sys/types.h>
70
71 #ifdef PRODUCT
72 #define BLOCK_COMMENT(str) /* nothing */
73 #else
74 #define BLOCK_COMMENT(str) block_comment(str)
75 #endif
76 #define STOP(str) stop(str);
77 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
78
79 #ifdef ASSERT
80 extern "C" void disnm(intptr_t p);
81 #endif
82 // Target-dependent relocation processing
83 //
84 // Instruction sequences whose target may need to be retrieved or
85 // patched are distinguished by their leading instruction, sorting
86 // them into three main instruction groups and related subgroups.
87 //
88 // 1) Branch, Exception and System (insn count = 1)
89 // 1a) Unconditional branch (immediate):
90 // b/bl imm19
91 // 1b) Compare & branch (immediate):
92 // cbz/cbnz Rt imm19
93 // 1c) Test & branch (immediate):
94 // tbz/tbnz Rt imm14
95 // 1d) Conditional branch (immediate):
96 // b.cond imm19
97 //
98 // 2) Loads and Stores (insn count = 1)
99 // 2a) Load register literal:
100 // ldr Rt imm19
101 //
102 // 3) Data Processing Immediate (insn count = 2 or 3)
103 // 3a) PC-rel. addressing
104 // adr/adrp Rx imm21; ldr/str Ry Rx #imm12
105 // adr/adrp Rx imm21; add Ry Rx #imm12
106 // adr/adrp Rx imm21; movk Rx #imm16<<32; ldr/str Ry, [Rx, #offset_in_page]
107 // adr/adrp Rx imm21
108 // adr/adrp Rx imm21; movk Rx #imm16<<32
109 // adr/adrp Rx imm21; movk Rx #imm16<<32; add Ry, Rx, #offset_in_page
110 // The latter form can only happen when the target is an
111 // ExternalAddress, and (by definition) ExternalAddresses don't
112 // move. Because of that property, there is never any need to
113 // patch the last of the three instructions. However,
114 // MacroAssembler::target_addr_for_insn takes all three
115 // instructions into account and returns the correct address.
116 // 3b) Move wide (immediate)
117 // movz Rx #imm16; movk Rx #imm16 << 16; movk Rx #imm16 << 32;
118 //
119 // A switch on a subset of the instruction's bits provides an
120 // efficient dispatch to these subcases.
121 //
122 // insn[28:26] -> main group ('x' == don't care)
123 // 00x -> UNALLOCATED
124 // 100 -> Data Processing Immediate
125 // 101 -> Branch, Exception and System
126 // x1x -> Loads and Stores
127 //
128 // insn[30:25] -> subgroup ('_' == group, 'x' == don't care).
129 // n.b. in some cases extra bits need to be checked to verify the
130 // instruction is as expected
131 //
132 // 1) ... xx101x Branch, Exception and System
133 // 1a) 00___x Unconditional branch (immediate)
134 // 1b) 01___0 Compare & branch (immediate)
135 // 1c) 01___1 Test & branch (immediate)
136 // 1d) 10___0 Conditional branch (immediate)
137 // other Should not happen
138 //
139 // 2) ... xxx1x0 Loads and Stores
140 // 2a) xx1__00 Load/Store register (insn[28] == 1 && insn[24] == 0)
141 // 2aa) x01__00 Load register literal (i.e. requires insn[29] == 0)
142 // strictly should be 64 bit non-FP/SIMD i.e.
143 // 0101_000 (i.e. requires insn[31:24] == 01011000)
144 //
145 // 3) ... xx100x Data Processing Immediate
146 // 3a) xx___00 PC-rel. addressing (n.b. requires insn[24] == 0)
147 // 3b) xx___101 Move wide (immediate) (n.b. requires insn[24:23] == 01)
148 // strictly should be 64 bit movz #imm16<<0
149 // 110___10100 (i.e. requires insn[31:21] == 11010010100)
150 //
151
152 static uint32_t insn_at(address insn_addr, int n) {
153 return ((uint32_t*)insn_addr)[n];
154 }
155
156 template<typename T>
157 class RelocActions : public AllStatic {
158
159 public:
160
161 static int ALWAYSINLINE run(address insn_addr, address &target) {
162 int instructions = 1;
163 uint32_t insn = insn_at(insn_addr, 0);
164
165 uint32_t dispatch = Instruction_aarch64::extract(insn, 30, 25);
166 switch(dispatch) {
167 case 0b001010:
168 case 0b001011: {
169 instructions = T::unconditionalBranch(insn_addr, target);
170 break;
171 }
172 case 0b101010: // Conditional branch (immediate)
173 case 0b011010: { // Compare & branch (immediate)
174 instructions = T::conditionalBranch(insn_addr, target);
175 break;
176 }
177 case 0b011011: {
178 instructions = T::testAndBranch(insn_addr, target);
179 break;
180 }
181 case 0b001100:
182 case 0b001110:
183 case 0b011100:
184 case 0b011110:
185 case 0b101100:
186 case 0b101110:
187 case 0b111100:
188 case 0b111110: {
189 // load/store
190 if ((Instruction_aarch64::extract(insn, 29, 24) & 0b111011) == 0b011000) {
191 // Load register (literal)
192 instructions = T::loadStore(insn_addr, target);
193 break;
194 } else {
195 // nothing to do
196 assert(target == nullptr, "did not expect to relocate target for polling page load");
197 }
198 break;
199 }
200 case 0b001000:
201 case 0b011000:
202 case 0b101000:
203 case 0b111000: {
204 // adr/adrp
205 assert(Instruction_aarch64::extract(insn, 28, 24) == 0b10000, "must be");
206 int shift = Instruction_aarch64::extract(insn, 31, 31);
207 if (shift) {
208 uint32_t insn2 = insn_at(insn_addr, 1);
209 if (Instruction_aarch64::extract(insn2, 29, 24) == 0b111001 &&
210 Instruction_aarch64::extract(insn, 4, 0) ==
211 Instruction_aarch64::extract(insn2, 9, 5)) {
212 instructions = T::adrp(insn_addr, target, T::adrpMem);
213 } else if (Instruction_aarch64::extract(insn2, 31, 22) == 0b1001000100 &&
214 Instruction_aarch64::extract(insn, 4, 0) ==
215 Instruction_aarch64::extract(insn2, 4, 0)) {
216 instructions = T::adrp(insn_addr, target, T::adrpAdd);
217 } else if (Instruction_aarch64::extract(insn2, 31, 21) == 0b11110010110 &&
218 Instruction_aarch64::extract(insn, 4, 0) ==
219 Instruction_aarch64::extract(insn2, 4, 0)) {
220 instructions = T::adrp(insn_addr, target, T::adrpMovk);
221 } else {
222 ShouldNotReachHere();
223 }
224 } else {
225 instructions = T::adr(insn_addr, target);
226 }
227 break;
228 }
229 case 0b001001:
230 case 0b011001:
231 case 0b101001:
232 case 0b111001: {
233 instructions = T::immediate(insn_addr, target);
234 break;
235 }
236 default: {
237 ShouldNotReachHere();
238 }
239 }
240
241 T::verify(insn_addr, target);
242 return instructions * NativeInstruction::instruction_size;
243 }
244 };
245
246 class Patcher : public AllStatic {
247 public:
248 static int unconditionalBranch(address insn_addr, address &target) {
249 intptr_t offset = (target - insn_addr) >> 2;
250 Instruction_aarch64::spatch(insn_addr, 25, 0, offset);
251 return 1;
252 }
253 static int conditionalBranch(address insn_addr, address &target) {
254 intptr_t offset = (target - insn_addr) >> 2;
255 Instruction_aarch64::spatch(insn_addr, 23, 5, offset);
256 return 1;
257 }
258 static int testAndBranch(address insn_addr, address &target) {
259 intptr_t offset = (target - insn_addr) >> 2;
260 Instruction_aarch64::spatch(insn_addr, 18, 5, offset);
261 return 1;
262 }
263 static int loadStore(address insn_addr, address &target) {
264 intptr_t offset = (target - insn_addr) >> 2;
265 Instruction_aarch64::spatch(insn_addr, 23, 5, offset);
266 return 1;
267 }
268 static int adr(address insn_addr, address &target) {
269 #ifdef ASSERT
270 assert(Instruction_aarch64::extract(insn_at(insn_addr, 0), 28, 24) == 0b10000, "must be");
271 #endif
272 // PC-rel. addressing
273 ptrdiff_t offset = target - insn_addr;
274 int offset_lo = offset & 3;
275 offset >>= 2;
276 Instruction_aarch64::spatch(insn_addr, 23, 5, offset);
277 Instruction_aarch64::patch(insn_addr, 30, 29, offset_lo);
278 return 1;
279 }
280 template<typename U>
281 static int adrp(address insn_addr, address &target, U inner) {
282 int instructions = 1;
283 #ifdef ASSERT
284 assert(Instruction_aarch64::extract(insn_at(insn_addr, 0), 28, 24) == 0b10000, "must be");
285 #endif
286 ptrdiff_t offset = target - insn_addr;
287 instructions = 2;
288 precond(inner != nullptr);
289 // Give the inner reloc a chance to modify the target.
290 address adjusted_target = target;
291 instructions = inner(insn_addr, adjusted_target);
292 uintptr_t pc_page = (uintptr_t)insn_addr >> 12;
293 uintptr_t adr_page = (uintptr_t)adjusted_target >> 12;
294 offset = adr_page - pc_page;
295 int offset_lo = offset & 3;
296 offset >>= 2;
297 Instruction_aarch64::spatch(insn_addr, 23, 5, offset);
298 Instruction_aarch64::patch(insn_addr, 30, 29, offset_lo);
299 return instructions;
300 }
301 static int adrpMem(address insn_addr, address &target) {
302 uintptr_t dest = (uintptr_t)target;
303 int offset_lo = dest & 0xfff;
304 uint32_t insn2 = insn_at(insn_addr, 1);
305 uint32_t size = Instruction_aarch64::extract(insn2, 31, 30);
306 Instruction_aarch64::patch(insn_addr + sizeof (uint32_t), 21, 10, offset_lo >> size);
307 guarantee(((dest >> size) << size) == dest, "misaligned target");
308 return 2;
309 }
310 static int adrpAdd(address insn_addr, address &target) {
311 uintptr_t dest = (uintptr_t)target;
312 int offset_lo = dest & 0xfff;
313 Instruction_aarch64::patch(insn_addr + sizeof (uint32_t), 21, 10, offset_lo);
314 return 2;
315 }
316 static int adrpMovk(address insn_addr, address &target) {
317 uintptr_t dest = uintptr_t(target);
318 Instruction_aarch64::patch(insn_addr + sizeof (uint32_t), 20, 5, (uintptr_t)target >> 32);
319 dest = (dest & 0xffffffffULL) | (uintptr_t(insn_addr) & 0xffff00000000ULL);
320 target = address(dest);
321 return 2;
322 }
323 static int immediate(address insn_addr, address &target) {
324 assert(Instruction_aarch64::extract(insn_at(insn_addr, 0), 31, 21) == 0b11010010100, "must be");
325 uint64_t dest = (uint64_t)target;
326 // Move wide constant
327 assert(nativeInstruction_at(insn_addr+4)->is_movk(), "wrong insns in patch");
328 assert(nativeInstruction_at(insn_addr+8)->is_movk(), "wrong insns in patch");
329 Instruction_aarch64::patch(insn_addr, 20, 5, dest & 0xffff);
330 Instruction_aarch64::patch(insn_addr+4, 20, 5, (dest >>= 16) & 0xffff);
331 Instruction_aarch64::patch(insn_addr+8, 20, 5, (dest >>= 16) & 0xffff);
332 return 3;
333 }
334 static void verify(address insn_addr, address &target) {
335 #ifdef ASSERT
336 address address_is = MacroAssembler::target_addr_for_insn(insn_addr);
337 if (!(address_is == target)) {
338 tty->print_cr("%p at %p should be %p", address_is, insn_addr, target);
339 disnm((intptr_t)insn_addr);
340 assert(address_is == target, "should be");
341 }
342 #endif
343 }
344 };
345
346 // If insn1 and insn2 use the same register to form an address, either
347 // by an offsetted LDR or a simple ADD, return the offset. If the
348 // second instruction is an LDR, the offset may be scaled.
349 static bool offset_for(uint32_t insn1, uint32_t insn2, ptrdiff_t &byte_offset) {
350 if (Instruction_aarch64::extract(insn2, 29, 24) == 0b111001 &&
351 Instruction_aarch64::extract(insn1, 4, 0) ==
352 Instruction_aarch64::extract(insn2, 9, 5)) {
353 // Load/store register (unsigned immediate)
354 byte_offset = Instruction_aarch64::extract(insn2, 21, 10);
355 uint32_t size = Instruction_aarch64::extract(insn2, 31, 30);
356 byte_offset <<= size;
357 return true;
358 } else if (Instruction_aarch64::extract(insn2, 31, 22) == 0b1001000100 &&
359 Instruction_aarch64::extract(insn1, 4, 0) ==
360 Instruction_aarch64::extract(insn2, 4, 0)) {
361 // add (immediate)
362 byte_offset = Instruction_aarch64::extract(insn2, 21, 10);
363 return true;
364 }
365 return false;
366 }
367
368 class AArch64Decoder : public AllStatic {
369 public:
370
371 static int loadStore(address insn_addr, address &target) {
372 intptr_t offset = Instruction_aarch64::sextract(insn_at(insn_addr, 0), 23, 5);
373 target = insn_addr + (offset << 2);
374 return 1;
375 }
376 static int unconditionalBranch(address insn_addr, address &target) {
377 intptr_t offset = Instruction_aarch64::sextract(insn_at(insn_addr, 0), 25, 0);
378 target = insn_addr + (offset << 2);
379 return 1;
380 }
381 static int conditionalBranch(address insn_addr, address &target) {
382 intptr_t offset = Instruction_aarch64::sextract(insn_at(insn_addr, 0), 23, 5);
383 target = address(((uint64_t)insn_addr + (offset << 2)));
384 return 1;
385 }
386 static int testAndBranch(address insn_addr, address &target) {
387 intptr_t offset = Instruction_aarch64::sextract(insn_at(insn_addr, 0), 18, 5);
388 target = address(((uint64_t)insn_addr + (offset << 2)));
389 return 1;
390 }
391 static int adr(address insn_addr, address &target) {
392 // PC-rel. addressing
393 uint32_t insn = insn_at(insn_addr, 0);
394 intptr_t offset = Instruction_aarch64::extract(insn, 30, 29);
395 offset |= Instruction_aarch64::sextract(insn, 23, 5) << 2;
396 target = address((uint64_t)insn_addr + offset);
397 return 1;
398 }
399 template<typename U>
400 static int adrp(address insn_addr, address &target, U inner) {
401 uint32_t insn = insn_at(insn_addr, 0);
402 assert(Instruction_aarch64::extract(insn, 28, 24) == 0b10000, "must be");
403 intptr_t offset = Instruction_aarch64::extract(insn, 30, 29);
404 offset |= Instruction_aarch64::sextract(insn, 23, 5) << 2;
405 int shift = 12;
406 offset <<= shift;
407 uint64_t target_page = ((uint64_t)insn_addr) + offset;
408 target_page &= ((uint64_t)-1) << shift;
409 uint32_t insn2 = insn_at(insn_addr, 1);
410 target = address(target_page);
411 precond(inner != nullptr);
412 inner(insn_addr, target);
413 return 2;
414 }
415 static int adrpMem(address insn_addr, address &target) {
416 uint32_t insn2 = insn_at(insn_addr, 1);
417 // Load/store register (unsigned immediate)
418 ptrdiff_t byte_offset = Instruction_aarch64::extract(insn2, 21, 10);
419 uint32_t size = Instruction_aarch64::extract(insn2, 31, 30);
420 byte_offset <<= size;
421 target += byte_offset;
422 return 2;
423 }
424 static int adrpAdd(address insn_addr, address &target) {
425 uint32_t insn2 = insn_at(insn_addr, 1);
426 // add (immediate)
427 ptrdiff_t byte_offset = Instruction_aarch64::extract(insn2, 21, 10);
428 target += byte_offset;
429 return 2;
430 }
431 static int adrpMovk(address insn_addr, address &target) {
432 uint32_t insn2 = insn_at(insn_addr, 1);
433 uint64_t dest = uint64_t(target);
434 dest = (dest & 0xffff0000ffffffff) |
435 ((uint64_t)Instruction_aarch64::extract(insn2, 20, 5) << 32);
436 target = address(dest);
437
438 // We know the destination 4k page. Maybe we have a third
439 // instruction.
440 uint32_t insn = insn_at(insn_addr, 0);
441 uint32_t insn3 = insn_at(insn_addr, 2);
442 ptrdiff_t byte_offset;
443 if (offset_for(insn, insn3, byte_offset)) {
444 target += byte_offset;
445 return 3;
446 } else {
447 return 2;
448 }
449 }
450 static int immediate(address insn_addr, address &target) {
451 uint32_t *insns = (uint32_t *)insn_addr;
452 assert(Instruction_aarch64::extract(insns[0], 31, 21) == 0b11010010100, "must be");
453 // Move wide constant: movz, movk, movk. See movptr().
454 assert(nativeInstruction_at(insns+1)->is_movk(), "wrong insns in patch");
455 assert(nativeInstruction_at(insns+2)->is_movk(), "wrong insns in patch");
456 target = address(uint64_t(Instruction_aarch64::extract(insns[0], 20, 5))
457 + (uint64_t(Instruction_aarch64::extract(insns[1], 20, 5)) << 16)
458 + (uint64_t(Instruction_aarch64::extract(insns[2], 20, 5)) << 32));
459 assert(nativeInstruction_at(insn_addr+4)->is_movk(), "wrong insns in patch");
460 assert(nativeInstruction_at(insn_addr+8)->is_movk(), "wrong insns in patch");
461 return 3;
462 }
463 static void verify(address insn_addr, address &target) {
464 }
465 };
466
467 address MacroAssembler::target_addr_for_insn(address insn_addr) {
468 address target;
469 RelocActions<AArch64Decoder>::run(insn_addr, target);
470 return target;
471 }
472
473 // Patch any kind of instruction; there may be several instructions.
474 // Return the total length (in bytes) of the instructions.
475 int MacroAssembler::pd_patch_instruction_size(address insn_addr, address target) {
476 return RelocActions<Patcher>::run(insn_addr, target);
477 }
478
479 int MacroAssembler::patch_oop(address insn_addr, address o) {
480 int instructions;
481 unsigned insn = *(unsigned*)insn_addr;
482 assert(nativeInstruction_at(insn_addr+4)->is_movk(), "wrong insns in patch");
483
484 // OOPs are either narrow (32 bits) or wide (48 bits). We encode
485 // narrow OOPs by setting the upper 16 bits in the first
486 // instruction.
487 if (Instruction_aarch64::extract(insn, 31, 21) == 0b11010010101) {
488 // Move narrow OOP
489 uint32_t n = CompressedOops::narrow_oop_value(cast_to_oop(o));
490 Instruction_aarch64::patch(insn_addr, 20, 5, n >> 16);
491 Instruction_aarch64::patch(insn_addr+4, 20, 5, n & 0xffff);
492 instructions = 2;
493 } else {
494 // Move wide OOP
495 assert(nativeInstruction_at(insn_addr+8)->is_movk(), "wrong insns in patch");
496 uintptr_t dest = (uintptr_t)o;
497 Instruction_aarch64::patch(insn_addr, 20, 5, dest & 0xffff);
498 Instruction_aarch64::patch(insn_addr+4, 20, 5, (dest >>= 16) & 0xffff);
499 Instruction_aarch64::patch(insn_addr+8, 20, 5, (dest >>= 16) & 0xffff);
500 instructions = 3;
501 }
502 return instructions * NativeInstruction::instruction_size;
503 }
504
505 int MacroAssembler::patch_narrow_klass(address insn_addr, narrowKlass n) {
506 // Metadata pointers are either narrow (32 bits) or wide (48 bits).
507 // We encode narrow ones by setting the upper 16 bits in the first
508 // instruction.
509 NativeInstruction *insn = nativeInstruction_at(insn_addr);
510 assert(Instruction_aarch64::extract(insn->encoding(), 31, 21) == 0b11010010101 &&
511 nativeInstruction_at(insn_addr+4)->is_movk(), "wrong insns in patch");
512
513 Instruction_aarch64::patch(insn_addr, 20, 5, n >> 16);
514 Instruction_aarch64::patch(insn_addr+4, 20, 5, n & 0xffff);
515 return 2 * NativeInstruction::instruction_size;
516 }
517
518 address MacroAssembler::target_addr_for_insn_or_null(address insn_addr) {
519 if (NativeInstruction::is_ldrw_to_zr(insn_addr)) {
520 return nullptr;
521 }
522 return MacroAssembler::target_addr_for_insn(insn_addr);
523 }
524
525 void MacroAssembler::safepoint_poll(Label& slow_path, bool at_return, bool in_nmethod, Register tmp) {
526 ldr(tmp, Address(rthread, JavaThread::polling_word_offset()));
527 if (at_return) {
528 // Note that when in_nmethod is set, the stack pointer is incremented before the poll. Therefore,
529 // we may safely use the sp instead to perform the stack watermark check.
530 cmp(in_nmethod ? sp : rfp, tmp);
531 br(Assembler::HI, slow_path);
532 } else {
533 tbnz(tmp, log2i_exact(SafepointMechanism::poll_bit()), slow_path);
534 }
535 }
536
537 void MacroAssembler::rt_call(address dest, Register tmp) {
538 CodeBlob *cb = CodeCache::find_blob(dest);
539 if (cb) {
540 far_call(RuntimeAddress(dest));
541 } else {
542 lea(tmp, RuntimeAddress(dest));
543 blr(tmp);
544 }
545 }
546
547 void MacroAssembler::push_cont_fastpath(Register java_thread) {
548 if (!Continuations::enabled()) return;
549 Label done;
550 ldr(rscratch1, Address(java_thread, JavaThread::cont_fastpath_offset()));
551 cmp(sp, rscratch1);
552 br(Assembler::LS, done);
553 mov(rscratch1, sp); // we can't use sp as the source in str
554 str(rscratch1, Address(java_thread, JavaThread::cont_fastpath_offset()));
555 bind(done);
556 }
557
558 void MacroAssembler::pop_cont_fastpath(Register java_thread) {
559 if (!Continuations::enabled()) return;
560 Label done;
561 ldr(rscratch1, Address(java_thread, JavaThread::cont_fastpath_offset()));
562 cmp(sp, rscratch1);
563 br(Assembler::LO, done);
564 str(zr, Address(java_thread, JavaThread::cont_fastpath_offset()));
565 bind(done);
566 }
567
568 void MacroAssembler::reset_last_Java_frame(bool clear_fp) {
569 // we must set sp to zero to clear frame
570 str(zr, Address(rthread, JavaThread::last_Java_sp_offset()));
571
572 // must clear fp, so that compiled frames are not confused; it is
573 // possible that we need it only for debugging
574 if (clear_fp) {
575 str(zr, Address(rthread, JavaThread::last_Java_fp_offset()));
576 }
577
578 // Always clear the pc because it could have been set by make_walkable()
579 str(zr, Address(rthread, JavaThread::last_Java_pc_offset()));
580 }
581
582 // Calls to C land
583 //
584 // When entering C land, the rfp, & resp of the last Java frame have to be recorded
585 // in the (thread-local) JavaThread object. When leaving C land, the last Java fp
586 // has to be reset to 0. This is required to allow proper stack traversal.
587 void MacroAssembler::set_last_Java_frame(Register last_java_sp,
588 Register last_java_fp,
589 Register last_java_pc,
590 Register scratch) {
591
592 if (last_java_pc->is_valid()) {
593 str(last_java_pc, Address(rthread,
594 JavaThread::frame_anchor_offset()
595 + JavaFrameAnchor::last_Java_pc_offset()));
596 }
597
598 // determine last_java_sp register
599 if (last_java_sp == sp) {
600 mov(scratch, sp);
601 last_java_sp = scratch;
602 } else if (!last_java_sp->is_valid()) {
603 last_java_sp = esp;
604 }
605
606 // last_java_fp is optional
607 if (last_java_fp->is_valid()) {
608 str(last_java_fp, Address(rthread, JavaThread::last_Java_fp_offset()));
609 }
610
611 // We must set sp last.
612 str(last_java_sp, Address(rthread, JavaThread::last_Java_sp_offset()));
613 }
614
615 void MacroAssembler::set_last_Java_frame(Register last_java_sp,
616 Register last_java_fp,
617 address last_java_pc,
618 Register scratch) {
619 assert(last_java_pc != nullptr, "must provide a valid PC");
620
621 adr(scratch, last_java_pc);
622 str(scratch, Address(rthread,
623 JavaThread::frame_anchor_offset()
624 + JavaFrameAnchor::last_Java_pc_offset()));
625
626 set_last_Java_frame(last_java_sp, last_java_fp, noreg, scratch);
627 }
628
629 void MacroAssembler::set_last_Java_frame(Register last_java_sp,
630 Register last_java_fp,
631 Label &L,
632 Register scratch) {
633 if (L.is_bound()) {
634 set_last_Java_frame(last_java_sp, last_java_fp, target(L), scratch);
635 } else {
636 InstructionMark im(this);
637 L.add_patch_at(code(), locator());
638 set_last_Java_frame(last_java_sp, last_java_fp, pc() /* Patched later */, scratch);
639 }
640 }
641
642 static inline bool target_needs_far_branch(address addr) {
643 if (AOTCodeCache::is_on_for_dump()) {
644 return true;
645 }
646 // codecache size <= 128M
647 if (!MacroAssembler::far_branches()) {
648 return false;
649 }
650 // codecache size > 240M
651 if (MacroAssembler::codestub_branch_needs_far_jump()) {
652 return true;
653 }
654 // codecache size: 128M..240M
655 return !CodeCache::is_non_nmethod(addr);
656 }
657
658 void MacroAssembler::far_call(Address entry, Register tmp) {
659 assert(ReservedCodeCacheSize < 4*G, "branch out of range");
660 assert(CodeCache::find_blob(entry.target()) != nullptr,
661 "destination of far call not found in code cache");
662 assert(entry.rspec().type() == relocInfo::external_word_type
663 || entry.rspec().type() == relocInfo::runtime_call_type
664 || entry.rspec().type() == relocInfo::none, "wrong entry relocInfo type");
665 if (target_needs_far_branch(entry.target())) {
666 uint64_t offset;
667 // We can use ADRP here because we know that the total size of
668 // the code cache cannot exceed 2Gb (ADRP limit is 4GB).
669 adrp(tmp, entry, offset);
670 add(tmp, tmp, offset);
671 blr(tmp);
672 } else {
673 bl(entry);
674 }
675 }
676
677 int MacroAssembler::far_jump(Address entry, Register tmp) {
678 assert(ReservedCodeCacheSize < 4*G, "branch out of range");
679 assert(CodeCache::find_blob(entry.target()) != nullptr,
680 "destination of far call not found in code cache");
681 assert(entry.rspec().type() == relocInfo::external_word_type
682 || entry.rspec().type() == relocInfo::runtime_call_type
683 || entry.rspec().type() == relocInfo::none, "wrong entry relocInfo type");
684 address start = pc();
685 if (target_needs_far_branch(entry.target())) {
686 uint64_t offset;
687 // We can use ADRP here because we know that the total size of
688 // the code cache cannot exceed 2Gb (ADRP limit is 4GB).
689 adrp(tmp, entry, offset);
690 add(tmp, tmp, offset);
691 br(tmp);
692 } else {
693 b(entry);
694 }
695 return pc() - start;
696 }
697
698 void MacroAssembler::reserved_stack_check() {
699 // testing if reserved zone needs to be enabled
700 Label no_reserved_zone_enabling;
701
702 ldr(rscratch1, Address(rthread, JavaThread::reserved_stack_activation_offset()));
703 cmp(sp, rscratch1);
704 br(Assembler::LO, no_reserved_zone_enabling);
705
706 enter(); // LR and FP are live.
707 lea(rscratch1, RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone)));
708 mov(c_rarg0, rthread);
709 blr(rscratch1);
710 leave();
711
712 // We have already removed our own frame.
713 // throw_delayed_StackOverflowError will think that it's been
714 // called by our caller.
715 lea(rscratch1, RuntimeAddress(SharedRuntime::throw_delayed_StackOverflowError_entry()));
716 br(rscratch1);
717 should_not_reach_here();
718
719 bind(no_reserved_zone_enabling);
720 }
721
722 static void pass_arg0(MacroAssembler* masm, Register arg) {
723 if (c_rarg0 != arg ) {
724 masm->mov(c_rarg0, arg);
725 }
726 }
727
728 static void pass_arg1(MacroAssembler* masm, Register arg) {
729 if (c_rarg1 != arg ) {
730 masm->mov(c_rarg1, arg);
731 }
732 }
733
734 static void pass_arg2(MacroAssembler* masm, Register arg) {
735 if (c_rarg2 != arg ) {
736 masm->mov(c_rarg2, arg);
737 }
738 }
739
740 static void pass_arg3(MacroAssembler* masm, Register arg) {
741 if (c_rarg3 != arg ) {
742 masm->mov(c_rarg3, arg);
743 }
744 }
745
746 void MacroAssembler::call_VM_base(Register oop_result,
747 Register java_thread,
748 Register last_java_sp,
749 Label* return_pc,
750 address entry_point,
751 int number_of_arguments,
752 bool check_exceptions) {
753 // determine java_thread register
754 if (!java_thread->is_valid()) {
755 java_thread = rthread;
756 }
757
758 // determine last_java_sp register
759 if (!last_java_sp->is_valid()) {
760 last_java_sp = esp;
761 }
762
763 // debugging support
764 assert(number_of_arguments >= 0 , "cannot have negative number of arguments");
765 assert(java_thread == rthread, "unexpected register");
766 #ifdef ASSERT
767 // TraceBytecodes does not use r12 but saves it over the call, so don't verify
768 // if ((UseCompressedOops || UseCompressedClassPointers) && !TraceBytecodes) verify_heapbase("call_VM_base: heap base corrupted?");
769 #endif // ASSERT
770
771 assert(java_thread != oop_result , "cannot use the same register for java_thread & oop_result");
772 assert(java_thread != last_java_sp, "cannot use the same register for java_thread & last_java_sp");
773
774 // push java thread (becomes first argument of C function)
775
776 mov(c_rarg0, java_thread);
777
778 // set last Java frame before call
779 assert(last_java_sp != rfp, "can't use rfp");
780
781 Label l;
782 set_last_Java_frame(last_java_sp, rfp, return_pc != nullptr ? *return_pc : l, rscratch1);
783
784 // do the call, remove parameters
785 MacroAssembler::call_VM_leaf_base(entry_point, number_of_arguments, &l);
786
787 // lr could be poisoned with PAC signature during throw_pending_exception
788 // if it was tail-call optimized by compiler, since lr is not callee-saved
789 // reload it with proper value
790 adr(lr, l);
791
792 // reset last Java frame
793 // Only interpreter should have to clear fp
794 reset_last_Java_frame(true);
795
796 // C++ interp handles this in the interpreter
797 check_and_handle_popframe(java_thread);
798 check_and_handle_earlyret(java_thread);
799
800 if (check_exceptions) {
801 // check for pending exceptions (java_thread is set upon return)
802 ldr(rscratch1, Address(java_thread, in_bytes(Thread::pending_exception_offset())));
803 Label ok;
804 cbz(rscratch1, ok);
805 lea(rscratch1, RuntimeAddress(StubRoutines::forward_exception_entry()));
806 br(rscratch1);
807 bind(ok);
808 }
809
810 // get oop result if there is one and reset the value in the thread
811 if (oop_result->is_valid()) {
812 get_vm_result_oop(oop_result, java_thread);
813 }
814 }
815
816 void MacroAssembler::call_VM_helper(Register oop_result, address entry_point, int number_of_arguments, bool check_exceptions) {
817 call_VM_base(oop_result, noreg, noreg, nullptr, entry_point, number_of_arguments, check_exceptions);
818 }
819
820 // Check the entry target is always reachable from any branch.
821 static bool is_always_within_branch_range(Address entry) {
822 if (AOTCodeCache::is_on_for_dump()) {
823 return false;
824 }
825 const address target = entry.target();
826
827 if (!CodeCache::contains(target)) {
828 // We always use trampolines for callees outside CodeCache.
829 assert(entry.rspec().type() == relocInfo::runtime_call_type, "non-runtime call of an external target");
830 return false;
831 }
832
833 if (!MacroAssembler::far_branches()) {
834 return true;
835 }
836
837 if (entry.rspec().type() == relocInfo::runtime_call_type) {
838 // Runtime calls are calls of a non-compiled method (stubs, adapters).
839 // Non-compiled methods stay forever in CodeCache.
840 // We check whether the longest possible branch is within the branch range.
841 assert(CodeCache::find_blob(target) != nullptr &&
842 !CodeCache::find_blob(target)->is_nmethod(),
843 "runtime call of compiled method");
844 const address right_longest_branch_start = CodeCache::high_bound() - NativeInstruction::instruction_size;
845 const address left_longest_branch_start = CodeCache::low_bound();
846 const bool is_reachable = Assembler::reachable_from_branch_at(left_longest_branch_start, target) &&
847 Assembler::reachable_from_branch_at(right_longest_branch_start, target);
848 return is_reachable;
849 }
850
851 return false;
852 }
853
854 // Maybe emit a call via a trampoline. If the code cache is small
855 // trampolines won't be emitted.
856 address MacroAssembler::trampoline_call(Address entry) {
857 assert(entry.rspec().type() == relocInfo::runtime_call_type
858 || entry.rspec().type() == relocInfo::opt_virtual_call_type
859 || entry.rspec().type() == relocInfo::static_call_type
860 || entry.rspec().type() == relocInfo::virtual_call_type, "wrong reloc type");
861
862 address target = entry.target();
863
864 if (!is_always_within_branch_range(entry)) {
865 if (!in_scratch_emit_size()) {
866 // We don't want to emit a trampoline if C2 is generating dummy
867 // code during its branch shortening phase.
868 if (entry.rspec().type() == relocInfo::runtime_call_type) {
869 assert(CodeBuffer::supports_shared_stubs(), "must support shared stubs");
870 code()->share_trampoline_for(entry.target(), offset());
871 } else {
872 address stub = emit_trampoline_stub(offset(), target);
873 if (stub == nullptr) {
874 postcond(pc() == badAddress);
875 return nullptr; // CodeCache is full
876 }
877 }
878 }
879 target = pc();
880 }
881
882 address call_pc = pc();
883 relocate(entry.rspec());
884 bl(target);
885
886 postcond(pc() != badAddress);
887 return call_pc;
888 }
889
890 // Emit a trampoline stub for a call to a target which is too far away.
891 //
892 // code sequences:
893 //
894 // call-site:
895 // branch-and-link to <destination> or <trampoline stub>
896 //
897 // Related trampoline stub for this call site in the stub section:
898 // load the call target from the constant pool
899 // branch (LR still points to the call site above)
900
901 address MacroAssembler::emit_trampoline_stub(int insts_call_instruction_offset,
902 address dest) {
903 // Max stub size: alignment nop, TrampolineStub.
904 address stub = start_a_stub(max_trampoline_stub_size());
905 if (stub == nullptr) {
906 return nullptr; // CodeBuffer::expand failed
907 }
908
909 // Create a trampoline stub relocation which relates this trampoline stub
910 // with the call instruction at insts_call_instruction_offset in the
911 // instructions code-section.
912 align(wordSize);
913 relocate(trampoline_stub_Relocation::spec(code()->insts()->start()
914 + insts_call_instruction_offset));
915 const int stub_start_offset = offset();
916
917 // Now, create the trampoline stub's code:
918 // - load the call
919 // - call
920 Label target;
921 ldr(rscratch1, target);
922 br(rscratch1);
923 bind(target);
924 assert(offset() - stub_start_offset == NativeCallTrampolineStub::data_offset,
925 "should be");
926 emit_int64((int64_t)dest);
927
928 const address stub_start_addr = addr_at(stub_start_offset);
929
930 assert(is_NativeCallTrampolineStub_at(stub_start_addr), "doesn't look like a trampoline");
931
932 end_a_stub();
933 return stub_start_addr;
934 }
935
936 int MacroAssembler::max_trampoline_stub_size() {
937 // Max stub size: alignment nop, TrampolineStub.
938 return NativeInstruction::instruction_size + NativeCallTrampolineStub::instruction_size;
939 }
940
941 void MacroAssembler::emit_static_call_stub() {
942 // CompiledDirectCall::set_to_interpreted knows the
943 // exact layout of this stub.
944
945 isb();
946 mov_metadata(rmethod, nullptr);
947
948 // Jump to the entry point of the c2i stub.
949 if (codestub_branch_needs_far_jump()) {
950 movptr(rscratch1, 0);
951 br(rscratch1);
952 } else {
953 b(pc());
954 }
955 }
956
957 int MacroAssembler::static_call_stub_size() {
958 if (!codestub_branch_needs_far_jump()) {
959 // isb; movk; movz; movz; b
960 return 5 * NativeInstruction::instruction_size;
961 }
962 // isb; movk; movz; movz; movk; movz; movz; br
963 return 8 * NativeInstruction::instruction_size;
964 }
965
966 void MacroAssembler::c2bool(Register x) {
967 // implements x == 0 ? 0 : 1
968 // note: must only look at least-significant byte of x
969 // since C-style booleans are stored in one byte
970 // only! (was bug)
971 tst(x, 0xff);
972 cset(x, Assembler::NE);
973 }
974
975 address MacroAssembler::ic_call(address entry, jint method_index) {
976 RelocationHolder rh = virtual_call_Relocation::spec(pc(), method_index);
977 movptr(rscratch2, (intptr_t)Universe::non_oop_word());
978 return trampoline_call(Address(entry, rh));
979 }
980
981 int MacroAssembler::ic_check_size() {
982 int extra_instructions = UseCompactObjectHeaders ? 1 : 0;
983 if (target_needs_far_branch(CAST_FROM_FN_PTR(address, SharedRuntime::get_ic_miss_stub()))) {
984 return NativeInstruction::instruction_size * (7 + extra_instructions);
985 } else {
986 return NativeInstruction::instruction_size * (5 + extra_instructions);
987 }
988 }
989
990 int MacroAssembler::ic_check(int end_alignment) {
991 Register receiver = j_rarg0;
992 Register data = rscratch2;
993 Register tmp1 = rscratch1;
994 Register tmp2 = r10;
995
996 // The UEP of a code blob ensures that the VEP is padded. However, the padding of the UEP is placed
997 // before the inline cache check, so we don't have to execute any nop instructions when dispatching
998 // through the UEP, yet we can ensure that the VEP is aligned appropriately. That's why we align
999 // before the inline cache check here, and not after
1000 align(end_alignment, offset() + ic_check_size());
1001
1002 int uep_offset = offset();
1003
1004 if (UseCompactObjectHeaders) {
1005 load_narrow_klass_compact(tmp1, receiver);
1006 ldrw(tmp2, Address(data, CompiledICData::speculated_klass_offset()));
1007 cmpw(tmp1, tmp2);
1008 } else if (UseCompressedClassPointers) {
1009 ldrw(tmp1, Address(receiver, oopDesc::klass_offset_in_bytes()));
1010 ldrw(tmp2, Address(data, CompiledICData::speculated_klass_offset()));
1011 cmpw(tmp1, tmp2);
1012 } else {
1013 ldr(tmp1, Address(receiver, oopDesc::klass_offset_in_bytes()));
1014 ldr(tmp2, Address(data, CompiledICData::speculated_klass_offset()));
1015 cmp(tmp1, tmp2);
1016 }
1017
1018 Label dont;
1019 br(Assembler::EQ, dont);
1020 far_jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
1021 bind(dont);
1022 assert((offset() % end_alignment) == 0, "Misaligned verified entry point");
1023
1024 return uep_offset;
1025 }
1026
1027 // Implementation of call_VM versions
1028
1029 void MacroAssembler::call_VM(Register oop_result,
1030 address entry_point,
1031 bool check_exceptions) {
1032 call_VM_helper(oop_result, entry_point, 0, check_exceptions);
1033 }
1034
1035 void MacroAssembler::call_VM(Register oop_result,
1036 address entry_point,
1037 Register arg_1,
1038 bool check_exceptions) {
1039 pass_arg1(this, arg_1);
1040 call_VM_helper(oop_result, entry_point, 1, check_exceptions);
1041 }
1042
1043 void MacroAssembler::call_VM(Register oop_result,
1044 address entry_point,
1045 Register arg_1,
1046 Register arg_2,
1047 bool check_exceptions) {
1048 assert_different_registers(arg_1, c_rarg2);
1049 pass_arg2(this, arg_2);
1050 pass_arg1(this, arg_1);
1051 call_VM_helper(oop_result, entry_point, 2, check_exceptions);
1052 }
1053
1054 void MacroAssembler::call_VM(Register oop_result,
1055 address entry_point,
1056 Register arg_1,
1057 Register arg_2,
1058 Register arg_3,
1059 bool check_exceptions) {
1060 assert_different_registers(arg_1, c_rarg2, c_rarg3);
1061 assert_different_registers(arg_2, c_rarg3);
1062 pass_arg3(this, arg_3);
1063
1064 pass_arg2(this, arg_2);
1065
1066 pass_arg1(this, arg_1);
1067 call_VM_helper(oop_result, entry_point, 3, check_exceptions);
1068 }
1069
1070 void MacroAssembler::call_VM(Register oop_result,
1071 Register last_java_sp,
1072 address entry_point,
1073 int number_of_arguments,
1074 bool check_exceptions) {
1075 call_VM_base(oop_result, rthread, last_java_sp, nullptr, entry_point, number_of_arguments, check_exceptions);
1076 }
1077
1078 void MacroAssembler::call_VM(Register oop_result,
1079 Register last_java_sp,
1080 address entry_point,
1081 Register arg_1,
1082 bool check_exceptions) {
1083 pass_arg1(this, arg_1);
1084 call_VM(oop_result, last_java_sp, entry_point, 1, check_exceptions);
1085 }
1086
1087 void MacroAssembler::call_VM(Register oop_result,
1088 Register last_java_sp,
1089 address entry_point,
1090 Register arg_1,
1091 Register arg_2,
1092 bool check_exceptions) {
1093
1094 assert_different_registers(arg_1, c_rarg2);
1095 pass_arg2(this, arg_2);
1096 pass_arg1(this, arg_1);
1097 call_VM(oop_result, last_java_sp, entry_point, 2, check_exceptions);
1098 }
1099
1100 void MacroAssembler::call_VM(Register oop_result,
1101 Register last_java_sp,
1102 address entry_point,
1103 Register arg_1,
1104 Register arg_2,
1105 Register arg_3,
1106 bool check_exceptions) {
1107 assert_different_registers(arg_1, c_rarg2, c_rarg3);
1108 assert_different_registers(arg_2, c_rarg3);
1109 pass_arg3(this, arg_3);
1110 pass_arg2(this, arg_2);
1111 pass_arg1(this, arg_1);
1112 call_VM(oop_result, last_java_sp, entry_point, 3, check_exceptions);
1113 }
1114
1115
1116 void MacroAssembler::get_vm_result_oop(Register oop_result, Register java_thread) {
1117 ldr(oop_result, Address(java_thread, JavaThread::vm_result_oop_offset()));
1118 str(zr, Address(java_thread, JavaThread::vm_result_oop_offset()));
1119 verify_oop_msg(oop_result, "broken oop in call_VM_base");
1120 }
1121
1122 void MacroAssembler::get_vm_result_metadata(Register metadata_result, Register java_thread) {
1123 ldr(metadata_result, Address(java_thread, JavaThread::vm_result_metadata_offset()));
1124 str(zr, Address(java_thread, JavaThread::vm_result_metadata_offset()));
1125 }
1126
1127 void MacroAssembler::align(int modulus) {
1128 align(modulus, offset());
1129 }
1130
1131 // Ensure that the code at target bytes offset from the current offset() is aligned
1132 // according to modulus.
1133 void MacroAssembler::align(int modulus, int target) {
1134 int delta = target - offset();
1135 while ((offset() + delta) % modulus != 0) nop();
1136 }
1137
1138 void MacroAssembler::post_call_nop() {
1139 if (!Continuations::enabled()) {
1140 return;
1141 }
1142 InstructionMark im(this);
1143 relocate(post_call_nop_Relocation::spec());
1144 InlineSkippedInstructionsCounter skipCounter(this);
1145 nop();
1146 movk(zr, 0);
1147 movk(zr, 0);
1148 }
1149
1150 // these are no-ops overridden by InterpreterMacroAssembler
1151
1152 void MacroAssembler::check_and_handle_earlyret(Register java_thread) { }
1153
1154 void MacroAssembler::check_and_handle_popframe(Register java_thread) { }
1155
1156 // Look up the method for a megamorphic invokeinterface call.
1157 // The target method is determined by <intf_klass, itable_index>.
1158 // The receiver klass is in recv_klass.
1159 // On success, the result will be in method_result, and execution falls through.
1160 // On failure, execution transfers to the given label.
1161 void MacroAssembler::lookup_interface_method(Register recv_klass,
1162 Register intf_klass,
1163 RegisterOrConstant itable_index,
1164 Register method_result,
1165 Register scan_temp,
1166 Label& L_no_such_interface,
1167 bool return_method) {
1168 assert_different_registers(recv_klass, intf_klass, scan_temp);
1169 assert_different_registers(method_result, intf_klass, scan_temp);
1170 assert(recv_klass != method_result || !return_method,
1171 "recv_klass can be destroyed when method isn't needed");
1172 assert(itable_index.is_constant() || itable_index.as_register() == method_result,
1173 "caller must use same register for non-constant itable index as for method");
1174
1175 // Compute start of first itableOffsetEntry (which is at the end of the vtable)
1176 int vtable_base = in_bytes(Klass::vtable_start_offset());
1177 int itentry_off = in_bytes(itableMethodEntry::method_offset());
1178 int scan_step = itableOffsetEntry::size() * wordSize;
1179 int vte_size = vtableEntry::size_in_bytes();
1180 assert(vte_size == wordSize, "else adjust times_vte_scale");
1181
1182 ldrw(scan_temp, Address(recv_klass, Klass::vtable_length_offset()));
1183
1184 // Could store the aligned, prescaled offset in the klass.
1185 // lea(scan_temp, Address(recv_klass, scan_temp, times_vte_scale, vtable_base));
1186 lea(scan_temp, Address(recv_klass, scan_temp, Address::lsl(3)));
1187 add(scan_temp, scan_temp, vtable_base);
1188
1189 if (return_method) {
1190 // Adjust recv_klass by scaled itable_index, so we can free itable_index.
1191 assert(itableMethodEntry::size() * wordSize == wordSize, "adjust the scaling in the code below");
1192 // lea(recv_klass, Address(recv_klass, itable_index, Address::times_ptr, itentry_off));
1193 lea(recv_klass, Address(recv_klass, itable_index, Address::lsl(3)));
1194 if (itentry_off)
1195 add(recv_klass, recv_klass, itentry_off);
1196 }
1197
1198 // for (scan = klass->itable(); scan->interface() != nullptr; scan += scan_step) {
1199 // if (scan->interface() == intf) {
1200 // result = (klass + scan->offset() + itable_index);
1201 // }
1202 // }
1203 Label search, found_method;
1204
1205 ldr(method_result, Address(scan_temp, itableOffsetEntry::interface_offset()));
1206 cmp(intf_klass, method_result);
1207 br(Assembler::EQ, found_method);
1208 bind(search);
1209 // Check that the previous entry is non-null. A null entry means that
1210 // the receiver class doesn't implement the interface, and wasn't the
1211 // same as when the caller was compiled.
1212 cbz(method_result, L_no_such_interface);
1213 if (itableOffsetEntry::interface_offset() != 0) {
1214 add(scan_temp, scan_temp, scan_step);
1215 ldr(method_result, Address(scan_temp, itableOffsetEntry::interface_offset()));
1216 } else {
1217 ldr(method_result, Address(pre(scan_temp, scan_step)));
1218 }
1219 cmp(intf_klass, method_result);
1220 br(Assembler::NE, search);
1221
1222 bind(found_method);
1223
1224 // Got a hit.
1225 if (return_method) {
1226 ldrw(scan_temp, Address(scan_temp, itableOffsetEntry::offset_offset()));
1227 ldr(method_result, Address(recv_klass, scan_temp, Address::uxtw(0)));
1228 }
1229 }
1230
1231 // Look up the method for a megamorphic invokeinterface call in a single pass over itable:
1232 // - check recv_klass (actual object class) is a subtype of resolved_klass from CompiledICData
1233 // - find a holder_klass (class that implements the method) vtable offset and get the method from vtable by index
1234 // The target method is determined by <holder_klass, itable_index>.
1235 // The receiver klass is in recv_klass.
1236 // On success, the result will be in method_result, and execution falls through.
1237 // On failure, execution transfers to the given label.
1238 void MacroAssembler::lookup_interface_method_stub(Register recv_klass,
1239 Register holder_klass,
1240 Register resolved_klass,
1241 Register method_result,
1242 Register temp_itbl_klass,
1243 Register scan_temp,
1244 int itable_index,
1245 Label& L_no_such_interface) {
1246 // 'method_result' is only used as output register at the very end of this method.
1247 // Until then we can reuse it as 'holder_offset'.
1248 Register holder_offset = method_result;
1249 assert_different_registers(resolved_klass, recv_klass, holder_klass, temp_itbl_klass, scan_temp, holder_offset);
1250
1251 int vtable_start_offset = in_bytes(Klass::vtable_start_offset());
1252 int itable_offset_entry_size = itableOffsetEntry::size() * wordSize;
1253 int ioffset = in_bytes(itableOffsetEntry::interface_offset());
1254 int ooffset = in_bytes(itableOffsetEntry::offset_offset());
1255
1256 Label L_loop_search_resolved_entry, L_resolved_found, L_holder_found;
1257
1258 ldrw(scan_temp, Address(recv_klass, Klass::vtable_length_offset()));
1259 add(recv_klass, recv_klass, vtable_start_offset + ioffset);
1260 // itableOffsetEntry[] itable = recv_klass + Klass::vtable_start_offset() + sizeof(vtableEntry) * recv_klass->_vtable_len;
1261 // temp_itbl_klass = itable[0]._interface;
1262 int vtblEntrySize = vtableEntry::size_in_bytes();
1263 assert(vtblEntrySize == wordSize, "ldr lsl shift amount must be 3");
1264 ldr(temp_itbl_klass, Address(recv_klass, scan_temp, Address::lsl(exact_log2(vtblEntrySize))));
1265 mov(holder_offset, zr);
1266 // scan_temp = &(itable[0]._interface)
1267 lea(scan_temp, Address(recv_klass, scan_temp, Address::lsl(exact_log2(vtblEntrySize))));
1268
1269 // Initial checks:
1270 // - if (holder_klass != resolved_klass), go to "scan for resolved"
1271 // - if (itable[0] == holder_klass), shortcut to "holder found"
1272 // - if (itable[0] == 0), no such interface
1273 cmp(resolved_klass, holder_klass);
1274 br(Assembler::NE, L_loop_search_resolved_entry);
1275 cmp(holder_klass, temp_itbl_klass);
1276 br(Assembler::EQ, L_holder_found);
1277 cbz(temp_itbl_klass, L_no_such_interface);
1278
1279 // Loop: Look for holder_klass record in itable
1280 // do {
1281 // temp_itbl_klass = *(scan_temp += itable_offset_entry_size);
1282 // if (temp_itbl_klass == holder_klass) {
1283 // goto L_holder_found; // Found!
1284 // }
1285 // } while (temp_itbl_klass != 0);
1286 // goto L_no_such_interface // Not found.
1287 Label L_search_holder;
1288 bind(L_search_holder);
1289 ldr(temp_itbl_klass, Address(pre(scan_temp, itable_offset_entry_size)));
1290 cmp(holder_klass, temp_itbl_klass);
1291 br(Assembler::EQ, L_holder_found);
1292 cbnz(temp_itbl_klass, L_search_holder);
1293
1294 b(L_no_such_interface);
1295
1296 // Loop: Look for resolved_class record in itable
1297 // while (true) {
1298 // temp_itbl_klass = *(scan_temp += itable_offset_entry_size);
1299 // if (temp_itbl_klass == 0) {
1300 // goto L_no_such_interface;
1301 // }
1302 // if (temp_itbl_klass == resolved_klass) {
1303 // goto L_resolved_found; // Found!
1304 // }
1305 // if (temp_itbl_klass == holder_klass) {
1306 // holder_offset = scan_temp;
1307 // }
1308 // }
1309 //
1310 Label L_loop_search_resolved;
1311 bind(L_loop_search_resolved);
1312 ldr(temp_itbl_klass, Address(pre(scan_temp, itable_offset_entry_size)));
1313 bind(L_loop_search_resolved_entry);
1314 cbz(temp_itbl_klass, L_no_such_interface);
1315 cmp(resolved_klass, temp_itbl_klass);
1316 br(Assembler::EQ, L_resolved_found);
1317 cmp(holder_klass, temp_itbl_klass);
1318 br(Assembler::NE, L_loop_search_resolved);
1319 mov(holder_offset, scan_temp);
1320 b(L_loop_search_resolved);
1321
1322 // See if we already have a holder klass. If not, go and scan for it.
1323 bind(L_resolved_found);
1324 cbz(holder_offset, L_search_holder);
1325 mov(scan_temp, holder_offset);
1326
1327 // Finally, scan_temp contains holder_klass vtable offset
1328 bind(L_holder_found);
1329 ldrw(method_result, Address(scan_temp, ooffset - ioffset));
1330 add(recv_klass, recv_klass, itable_index * wordSize + in_bytes(itableMethodEntry::method_offset())
1331 - vtable_start_offset - ioffset); // substract offsets to restore the original value of recv_klass
1332 ldr(method_result, Address(recv_klass, method_result, Address::uxtw(0)));
1333 }
1334
1335 // virtual method calling
1336 void MacroAssembler::lookup_virtual_method(Register recv_klass,
1337 RegisterOrConstant vtable_index,
1338 Register method_result) {
1339 assert(vtableEntry::size() * wordSize == 8,
1340 "adjust the scaling in the code below");
1341 int64_t vtable_offset_in_bytes = in_bytes(Klass::vtable_start_offset() + vtableEntry::method_offset());
1342
1343 if (vtable_index.is_register()) {
1344 lea(method_result, Address(recv_klass,
1345 vtable_index.as_register(),
1346 Address::lsl(LogBytesPerWord)));
1347 ldr(method_result, Address(method_result, vtable_offset_in_bytes));
1348 } else {
1349 vtable_offset_in_bytes += vtable_index.as_constant() * wordSize;
1350 ldr(method_result,
1351 form_address(rscratch1, recv_klass, vtable_offset_in_bytes, 0));
1352 }
1353 }
1354
1355 void MacroAssembler::check_klass_subtype(Register sub_klass,
1356 Register super_klass,
1357 Register temp_reg,
1358 Label& L_success) {
1359 Label L_failure;
1360 check_klass_subtype_fast_path(sub_klass, super_klass, temp_reg, &L_success, &L_failure, nullptr);
1361 check_klass_subtype_slow_path(sub_klass, super_klass, temp_reg, noreg, &L_success, nullptr);
1362 bind(L_failure);
1363 }
1364
1365
1366 void MacroAssembler::check_klass_subtype_fast_path(Register sub_klass,
1367 Register super_klass,
1368 Register temp_reg,
1369 Label* L_success,
1370 Label* L_failure,
1371 Label* L_slow_path,
1372 Register super_check_offset) {
1373 assert_different_registers(sub_klass, super_klass, temp_reg, super_check_offset);
1374 bool must_load_sco = ! super_check_offset->is_valid();
1375 if (must_load_sco) {
1376 assert(temp_reg != noreg, "supply either a temp or a register offset");
1377 }
1378
1379 Label L_fallthrough;
1380 int label_nulls = 0;
1381 if (L_success == nullptr) { L_success = &L_fallthrough; label_nulls++; }
1382 if (L_failure == nullptr) { L_failure = &L_fallthrough; label_nulls++; }
1383 if (L_slow_path == nullptr) { L_slow_path = &L_fallthrough; label_nulls++; }
1384 assert(label_nulls <= 1, "at most one null in the batch");
1385
1386 int sco_offset = in_bytes(Klass::super_check_offset_offset());
1387 Address super_check_offset_addr(super_klass, sco_offset);
1388
1389 // Hacked jmp, which may only be used just before L_fallthrough.
1390 #define final_jmp(label) \
1391 if (&(label) == &L_fallthrough) { /*do nothing*/ } \
1392 else b(label) /*omit semi*/
1393
1394 // If the pointers are equal, we are done (e.g., String[] elements).
1395 // This self-check enables sharing of secondary supertype arrays among
1396 // non-primary types such as array-of-interface. Otherwise, each such
1397 // type would need its own customized SSA.
1398 // We move this check to the front of the fast path because many
1399 // type checks are in fact trivially successful in this manner,
1400 // so we get a nicely predicted branch right at the start of the check.
1401 cmp(sub_klass, super_klass);
1402 br(Assembler::EQ, *L_success);
1403
1404 // Check the supertype display:
1405 if (must_load_sco) {
1406 ldrw(temp_reg, super_check_offset_addr);
1407 super_check_offset = temp_reg;
1408 }
1409
1410 Address super_check_addr(sub_klass, super_check_offset);
1411 ldr(rscratch1, super_check_addr);
1412 cmp(super_klass, rscratch1); // load displayed supertype
1413 br(Assembler::EQ, *L_success);
1414
1415 // This check has worked decisively for primary supers.
1416 // Secondary supers are sought in the super_cache ('super_cache_addr').
1417 // (Secondary supers are interfaces and very deeply nested subtypes.)
1418 // This works in the same check above because of a tricky aliasing
1419 // between the super_cache and the primary super display elements.
1420 // (The 'super_check_addr' can address either, as the case requires.)
1421 // Note that the cache is updated below if it does not help us find
1422 // what we need immediately.
1423 // So if it was a primary super, we can just fail immediately.
1424 // Otherwise, it's the slow path for us (no success at this point).
1425
1426 sub(rscratch1, super_check_offset, in_bytes(Klass::secondary_super_cache_offset()));
1427 if (L_failure == &L_fallthrough) {
1428 cbz(rscratch1, *L_slow_path);
1429 } else {
1430 cbnz(rscratch1, *L_failure);
1431 final_jmp(*L_slow_path);
1432 }
1433
1434 bind(L_fallthrough);
1435
1436 #undef final_jmp
1437 }
1438
1439 // These two are taken from x86, but they look generally useful
1440
1441 // scans count pointer sized words at [addr] for occurrence of value,
1442 // generic
1443 void MacroAssembler::repne_scan(Register addr, Register value, Register count,
1444 Register scratch) {
1445 Label Lloop, Lexit;
1446 cbz(count, Lexit);
1447 bind(Lloop);
1448 ldr(scratch, post(addr, wordSize));
1449 cmp(value, scratch);
1450 br(EQ, Lexit);
1451 sub(count, count, 1);
1452 cbnz(count, Lloop);
1453 bind(Lexit);
1454 }
1455
1456 // scans count 4 byte words at [addr] for occurrence of value,
1457 // generic
1458 void MacroAssembler::repne_scanw(Register addr, Register value, Register count,
1459 Register scratch) {
1460 Label Lloop, Lexit;
1461 cbz(count, Lexit);
1462 bind(Lloop);
1463 ldrw(scratch, post(addr, wordSize));
1464 cmpw(value, scratch);
1465 br(EQ, Lexit);
1466 sub(count, count, 1);
1467 cbnz(count, Lloop);
1468 bind(Lexit);
1469 }
1470
1471 void MacroAssembler::check_klass_subtype_slow_path_linear(Register sub_klass,
1472 Register super_klass,
1473 Register temp_reg,
1474 Register temp2_reg,
1475 Label* L_success,
1476 Label* L_failure,
1477 bool set_cond_codes) {
1478 // NB! Callers may assume that, when temp2_reg is a valid register,
1479 // this code sets it to a nonzero value.
1480
1481 assert_different_registers(sub_klass, super_klass, temp_reg);
1482 if (temp2_reg != noreg)
1483 assert_different_registers(sub_klass, super_klass, temp_reg, temp2_reg, rscratch1);
1484 #define IS_A_TEMP(reg) ((reg) == temp_reg || (reg) == temp2_reg)
1485
1486 Label L_fallthrough;
1487 int label_nulls = 0;
1488 if (L_success == nullptr) { L_success = &L_fallthrough; label_nulls++; }
1489 if (L_failure == nullptr) { L_failure = &L_fallthrough; label_nulls++; }
1490 assert(label_nulls <= 1, "at most one null in the batch");
1491
1492 // a couple of useful fields in sub_klass:
1493 int ss_offset = in_bytes(Klass::secondary_supers_offset());
1494 int sc_offset = in_bytes(Klass::secondary_super_cache_offset());
1495 Address secondary_supers_addr(sub_klass, ss_offset);
1496 Address super_cache_addr( sub_klass, sc_offset);
1497
1498 BLOCK_COMMENT("check_klass_subtype_slow_path");
1499
1500 // Do a linear scan of the secondary super-klass chain.
1501 // This code is rarely used, so simplicity is a virtue here.
1502 // The repne_scan instruction uses fixed registers, which we must spill.
1503 // Don't worry too much about pre-existing connections with the input regs.
1504
1505 assert(sub_klass != r0, "killed reg"); // killed by mov(r0, super)
1506 assert(sub_klass != r2, "killed reg"); // killed by lea(r2, &pst_counter)
1507
1508 RegSet pushed_registers;
1509 if (!IS_A_TEMP(r2)) pushed_registers += r2;
1510 if (!IS_A_TEMP(r5)) pushed_registers += r5;
1511
1512 if (super_klass != r0) {
1513 if (!IS_A_TEMP(r0)) pushed_registers += r0;
1514 }
1515
1516 push(pushed_registers, sp);
1517
1518 // Get super_klass value into r0 (even if it was in r5 or r2).
1519 if (super_klass != r0) {
1520 mov(r0, super_klass);
1521 }
1522
1523 #ifndef PRODUCT
1524 incrementw(ExternalAddress((address)&SharedRuntime::_partial_subtype_ctr));
1525 #endif //PRODUCT
1526
1527 // We will consult the secondary-super array.
1528 ldr(r5, secondary_supers_addr);
1529 // Load the array length.
1530 ldrw(r2, Address(r5, Array<Klass*>::length_offset_in_bytes()));
1531 // Skip to start of data.
1532 add(r5, r5, Array<Klass*>::base_offset_in_bytes());
1533
1534 cmp(sp, zr); // Clear Z flag; SP is never zero
1535 // Scan R2 words at [R5] for an occurrence of R0.
1536 // Set NZ/Z based on last compare.
1537 repne_scan(r5, r0, r2, rscratch1);
1538
1539 // Unspill the temp. registers:
1540 pop(pushed_registers, sp);
1541
1542 br(Assembler::NE, *L_failure);
1543
1544 // Success. Cache the super we found and proceed in triumph.
1545
1546 if (UseSecondarySupersCache) {
1547 str(super_klass, super_cache_addr);
1548 }
1549
1550 if (L_success != &L_fallthrough) {
1551 b(*L_success);
1552 }
1553
1554 #undef IS_A_TEMP
1555
1556 bind(L_fallthrough);
1557 }
1558
1559 // If Register r is invalid, remove a new register from
1560 // available_regs, and add new register to regs_to_push.
1561 Register MacroAssembler::allocate_if_noreg(Register r,
1562 RegSetIterator<Register> &available_regs,
1563 RegSet ®s_to_push) {
1564 if (!r->is_valid()) {
1565 r = *available_regs++;
1566 regs_to_push += r;
1567 }
1568 return r;
1569 }
1570
1571 // check_klass_subtype_slow_path_table() looks for super_klass in the
1572 // hash table belonging to super_klass, branching to L_success or
1573 // L_failure as appropriate. This is essentially a shim which
1574 // allocates registers as necessary then calls
1575 // lookup_secondary_supers_table() to do the work. Any of the temp
1576 // regs may be noreg, in which case this logic will chooses some
1577 // registers push and pop them from the stack.
1578 void MacroAssembler::check_klass_subtype_slow_path_table(Register sub_klass,
1579 Register super_klass,
1580 Register temp_reg,
1581 Register temp2_reg,
1582 Register temp3_reg,
1583 Register result_reg,
1584 FloatRegister vtemp,
1585 Label* L_success,
1586 Label* L_failure,
1587 bool set_cond_codes) {
1588 RegSet temps = RegSet::of(temp_reg, temp2_reg, temp3_reg);
1589
1590 assert_different_registers(sub_klass, super_klass, temp_reg, temp2_reg, rscratch1);
1591
1592 Label L_fallthrough;
1593 int label_nulls = 0;
1594 if (L_success == nullptr) { L_success = &L_fallthrough; label_nulls++; }
1595 if (L_failure == nullptr) { L_failure = &L_fallthrough; label_nulls++; }
1596 assert(label_nulls <= 1, "at most one null in the batch");
1597
1598 BLOCK_COMMENT("check_klass_subtype_slow_path");
1599
1600 RegSetIterator<Register> available_regs
1601 = (RegSet::range(r0, r15) - temps - sub_klass - super_klass).begin();
1602
1603 RegSet pushed_regs;
1604
1605 temp_reg = allocate_if_noreg(temp_reg, available_regs, pushed_regs);
1606 temp2_reg = allocate_if_noreg(temp2_reg, available_regs, pushed_regs);
1607 temp3_reg = allocate_if_noreg(temp3_reg, available_regs, pushed_regs);
1608 result_reg = allocate_if_noreg(result_reg, available_regs, pushed_regs);
1609
1610 push(pushed_regs, sp);
1611
1612 lookup_secondary_supers_table_var(sub_klass,
1613 super_klass,
1614 temp_reg, temp2_reg, temp3_reg, vtemp, result_reg,
1615 nullptr);
1616 cmp(result_reg, zr);
1617
1618 // Unspill the temp. registers:
1619 pop(pushed_regs, sp);
1620
1621 // NB! Callers may assume that, when set_cond_codes is true, this
1622 // code sets temp2_reg to a nonzero value.
1623 if (set_cond_codes) {
1624 mov(temp2_reg, 1);
1625 }
1626
1627 br(Assembler::NE, *L_failure);
1628
1629 if (L_success != &L_fallthrough) {
1630 b(*L_success);
1631 }
1632
1633 bind(L_fallthrough);
1634 }
1635
1636 void MacroAssembler::check_klass_subtype_slow_path(Register sub_klass,
1637 Register super_klass,
1638 Register temp_reg,
1639 Register temp2_reg,
1640 Label* L_success,
1641 Label* L_failure,
1642 bool set_cond_codes) {
1643 if (UseSecondarySupersTable) {
1644 check_klass_subtype_slow_path_table
1645 (sub_klass, super_klass, temp_reg, temp2_reg, /*temp3*/noreg, /*result*/noreg,
1646 /*vtemp*/fnoreg,
1647 L_success, L_failure, set_cond_codes);
1648 } else {
1649 check_klass_subtype_slow_path_linear
1650 (sub_klass, super_klass, temp_reg, temp2_reg, L_success, L_failure, set_cond_codes);
1651 }
1652 }
1653
1654
1655 // Ensure that the inline code and the stub are using the same registers.
1656 #define LOOKUP_SECONDARY_SUPERS_TABLE_REGISTERS \
1657 do { \
1658 assert(r_super_klass == r0 && \
1659 r_array_base == r1 && \
1660 r_array_length == r2 && \
1661 (r_array_index == r3 || r_array_index == noreg) && \
1662 (r_sub_klass == r4 || r_sub_klass == noreg) && \
1663 (r_bitmap == rscratch2 || r_bitmap == noreg) && \
1664 (result == r5 || result == noreg), "registers must match aarch64.ad"); \
1665 } while(0)
1666
1667 bool MacroAssembler::lookup_secondary_supers_table_const(Register r_sub_klass,
1668 Register r_super_klass,
1669 Register temp1,
1670 Register temp2,
1671 Register temp3,
1672 FloatRegister vtemp,
1673 Register result,
1674 u1 super_klass_slot,
1675 bool stub_is_near) {
1676 assert_different_registers(r_sub_klass, temp1, temp2, temp3, result, rscratch1, rscratch2);
1677
1678 Label L_fallthrough;
1679
1680 BLOCK_COMMENT("lookup_secondary_supers_table {");
1681
1682 const Register
1683 r_array_base = temp1, // r1
1684 r_array_length = temp2, // r2
1685 r_array_index = temp3, // r3
1686 r_bitmap = rscratch2;
1687
1688 LOOKUP_SECONDARY_SUPERS_TABLE_REGISTERS;
1689
1690 u1 bit = super_klass_slot;
1691
1692 // Make sure that result is nonzero if the TBZ below misses.
1693 mov(result, 1);
1694
1695 // We're going to need the bitmap in a vector reg and in a core reg,
1696 // so load both now.
1697 ldr(r_bitmap, Address(r_sub_klass, Klass::secondary_supers_bitmap_offset()));
1698 if (bit != 0) {
1699 ldrd(vtemp, Address(r_sub_klass, Klass::secondary_supers_bitmap_offset()));
1700 }
1701 // First check the bitmap to see if super_klass might be present. If
1702 // the bit is zero, we are certain that super_klass is not one of
1703 // the secondary supers.
1704 tbz(r_bitmap, bit, L_fallthrough);
1705
1706 // Get the first array index that can contain super_klass into r_array_index.
1707 if (bit != 0) {
1708 shld(vtemp, vtemp, Klass::SECONDARY_SUPERS_TABLE_MASK - bit);
1709 cnt(vtemp, T8B, vtemp);
1710 addv(vtemp, T8B, vtemp);
1711 fmovd(r_array_index, vtemp);
1712 } else {
1713 mov(r_array_index, (u1)1);
1714 }
1715 // NB! r_array_index is off by 1. It is compensated by keeping r_array_base off by 1 word.
1716
1717 // We will consult the secondary-super array.
1718 ldr(r_array_base, Address(r_sub_klass, in_bytes(Klass::secondary_supers_offset())));
1719
1720 // The value i in r_array_index is >= 1, so even though r_array_base
1721 // points to the length, we don't need to adjust it to point to the
1722 // data.
1723 assert(Array<Klass*>::base_offset_in_bytes() == wordSize, "Adjust this code");
1724 assert(Array<Klass*>::length_offset_in_bytes() == 0, "Adjust this code");
1725
1726 ldr(result, Address(r_array_base, r_array_index, Address::lsl(LogBytesPerWord)));
1727 eor(result, result, r_super_klass);
1728 cbz(result, L_fallthrough); // Found a match
1729
1730 // Is there another entry to check? Consult the bitmap.
1731 tbz(r_bitmap, (bit + 1) & Klass::SECONDARY_SUPERS_TABLE_MASK, L_fallthrough);
1732
1733 // Linear probe.
1734 if (bit != 0) {
1735 ror(r_bitmap, r_bitmap, bit);
1736 }
1737
1738 // The slot we just inspected is at secondary_supers[r_array_index - 1].
1739 // The next slot to be inspected, by the stub we're about to call,
1740 // is secondary_supers[r_array_index]. Bits 0 and 1 in the bitmap
1741 // have been checked.
1742 Address stub = RuntimeAddress(StubRoutines::lookup_secondary_supers_table_slow_path_stub());
1743 if (stub_is_near) {
1744 bl(stub);
1745 } else {
1746 address call = trampoline_call(stub);
1747 if (call == nullptr) {
1748 return false; // trampoline allocation failed
1749 }
1750 }
1751
1752 BLOCK_COMMENT("} lookup_secondary_supers_table");
1753
1754 bind(L_fallthrough);
1755
1756 if (VerifySecondarySupers) {
1757 verify_secondary_supers_table(r_sub_klass, r_super_klass, // r4, r0
1758 temp1, temp2, result); // r1, r2, r5
1759 }
1760 return true;
1761 }
1762
1763 // At runtime, return 0 in result if r_super_klass is a superclass of
1764 // r_sub_klass, otherwise return nonzero. Use this version of
1765 // lookup_secondary_supers_table() if you don't know ahead of time
1766 // which superclass will be searched for. Used by interpreter and
1767 // runtime stubs. It is larger and has somewhat greater latency than
1768 // the version above, which takes a constant super_klass_slot.
1769 void MacroAssembler::lookup_secondary_supers_table_var(Register r_sub_klass,
1770 Register r_super_klass,
1771 Register temp1,
1772 Register temp2,
1773 Register temp3,
1774 FloatRegister vtemp,
1775 Register result,
1776 Label *L_success) {
1777 assert_different_registers(r_sub_klass, temp1, temp2, temp3, result, rscratch1, rscratch2);
1778
1779 Label L_fallthrough;
1780
1781 BLOCK_COMMENT("lookup_secondary_supers_table {");
1782
1783 const Register
1784 r_array_index = temp3,
1785 slot = rscratch1,
1786 r_bitmap = rscratch2;
1787
1788 ldrb(slot, Address(r_super_klass, Klass::hash_slot_offset()));
1789
1790 // Make sure that result is nonzero if the test below misses.
1791 mov(result, 1);
1792
1793 ldr(r_bitmap, Address(r_sub_klass, Klass::secondary_supers_bitmap_offset()));
1794
1795 // First check the bitmap to see if super_klass might be present. If
1796 // the bit is zero, we are certain that super_klass is not one of
1797 // the secondary supers.
1798
1799 // This next instruction is equivalent to:
1800 // mov(tmp_reg, (u1)(Klass::SECONDARY_SUPERS_TABLE_SIZE - 1));
1801 // sub(temp2, tmp_reg, slot);
1802 eor(temp2, slot, (u1)(Klass::SECONDARY_SUPERS_TABLE_SIZE - 1));
1803 lslv(temp2, r_bitmap, temp2);
1804 tbz(temp2, Klass::SECONDARY_SUPERS_TABLE_SIZE - 1, L_fallthrough);
1805
1806 bool must_save_v0 = (vtemp == fnoreg);
1807 if (must_save_v0) {
1808 // temp1 and result are free, so use them to preserve vtemp
1809 vtemp = v0;
1810 mov(temp1, vtemp, D, 0);
1811 mov(result, vtemp, D, 1);
1812 }
1813
1814 // Get the first array index that can contain super_klass into r_array_index.
1815 mov(vtemp, D, 0, temp2);
1816 cnt(vtemp, T8B, vtemp);
1817 addv(vtemp, T8B, vtemp);
1818 mov(r_array_index, vtemp, D, 0);
1819
1820 if (must_save_v0) {
1821 mov(vtemp, D, 0, temp1 );
1822 mov(vtemp, D, 1, result);
1823 }
1824
1825 // NB! r_array_index is off by 1. It is compensated by keeping r_array_base off by 1 word.
1826
1827 const Register
1828 r_array_base = temp1,
1829 r_array_length = temp2;
1830
1831 // The value i in r_array_index is >= 1, so even though r_array_base
1832 // points to the length, we don't need to adjust it to point to the
1833 // data.
1834 assert(Array<Klass*>::base_offset_in_bytes() == wordSize, "Adjust this code");
1835 assert(Array<Klass*>::length_offset_in_bytes() == 0, "Adjust this code");
1836
1837 // We will consult the secondary-super array.
1838 ldr(r_array_base, Address(r_sub_klass, in_bytes(Klass::secondary_supers_offset())));
1839
1840 ldr(result, Address(r_array_base, r_array_index, Address::lsl(LogBytesPerWord)));
1841 eor(result, result, r_super_klass);
1842 cbz(result, L_success ? *L_success : L_fallthrough); // Found a match
1843
1844 // Is there another entry to check? Consult the bitmap.
1845 rorv(r_bitmap, r_bitmap, slot);
1846 // rol(r_bitmap, r_bitmap, 1);
1847 tbz(r_bitmap, 1, L_fallthrough);
1848
1849 // The slot we just inspected is at secondary_supers[r_array_index - 1].
1850 // The next slot to be inspected, by the logic we're about to call,
1851 // is secondary_supers[r_array_index]. Bits 0 and 1 in the bitmap
1852 // have been checked.
1853 lookup_secondary_supers_table_slow_path(r_super_klass, r_array_base, r_array_index,
1854 r_bitmap, r_array_length, result, /*is_stub*/false);
1855
1856 BLOCK_COMMENT("} lookup_secondary_supers_table");
1857
1858 bind(L_fallthrough);
1859
1860 if (VerifySecondarySupers) {
1861 verify_secondary_supers_table(r_sub_klass, r_super_klass, // r4, r0
1862 temp1, temp2, result); // r1, r2, r5
1863 }
1864
1865 if (L_success) {
1866 cbz(result, *L_success);
1867 }
1868 }
1869
1870 // Called by code generated by check_klass_subtype_slow_path
1871 // above. This is called when there is a collision in the hashed
1872 // lookup in the secondary supers array.
1873 void MacroAssembler::lookup_secondary_supers_table_slow_path(Register r_super_klass,
1874 Register r_array_base,
1875 Register r_array_index,
1876 Register r_bitmap,
1877 Register temp1,
1878 Register result,
1879 bool is_stub) {
1880 assert_different_registers(r_super_klass, r_array_base, r_array_index, r_bitmap, temp1, result, rscratch1);
1881
1882 const Register
1883 r_array_length = temp1,
1884 r_sub_klass = noreg; // unused
1885
1886 if (is_stub) {
1887 LOOKUP_SECONDARY_SUPERS_TABLE_REGISTERS;
1888 }
1889
1890 Label L_fallthrough, L_huge;
1891
1892 // Load the array length.
1893 ldrw(r_array_length, Address(r_array_base, Array<Klass*>::length_offset_in_bytes()));
1894 // And adjust the array base to point to the data.
1895 // NB! Effectively increments current slot index by 1.
1896 assert(Array<Klass*>::base_offset_in_bytes() == wordSize, "");
1897 add(r_array_base, r_array_base, Array<Klass*>::base_offset_in_bytes());
1898
1899 // The bitmap is full to bursting.
1900 // Implicit invariant: BITMAP_FULL implies (length > 0)
1901 assert(Klass::SECONDARY_SUPERS_BITMAP_FULL == ~uintx(0), "");
1902 cmpw(r_array_length, (u1)(Klass::SECONDARY_SUPERS_TABLE_SIZE - 2));
1903 br(GT, L_huge);
1904
1905 // NB! Our caller has checked bits 0 and 1 in the bitmap. The
1906 // current slot (at secondary_supers[r_array_index]) has not yet
1907 // been inspected, and r_array_index may be out of bounds if we
1908 // wrapped around the end of the array.
1909
1910 { // This is conventional linear probing, but instead of terminating
1911 // when a null entry is found in the table, we maintain a bitmap
1912 // in which a 0 indicates missing entries.
1913 // As long as the bitmap is not completely full,
1914 // array_length == popcount(bitmap). The array_length check above
1915 // guarantees there are 0s in the bitmap, so the loop eventually
1916 // terminates.
1917 Label L_loop;
1918 bind(L_loop);
1919
1920 // Check for wraparound.
1921 cmp(r_array_index, r_array_length);
1922 csel(r_array_index, zr, r_array_index, GE);
1923
1924 ldr(rscratch1, Address(r_array_base, r_array_index, Address::lsl(LogBytesPerWord)));
1925 eor(result, rscratch1, r_super_klass);
1926 cbz(result, L_fallthrough);
1927
1928 tbz(r_bitmap, 2, L_fallthrough); // look-ahead check (Bit 2); result is non-zero
1929
1930 ror(r_bitmap, r_bitmap, 1);
1931 add(r_array_index, r_array_index, 1);
1932 b(L_loop);
1933 }
1934
1935 { // Degenerate case: more than 64 secondary supers.
1936 // FIXME: We could do something smarter here, maybe a vectorized
1937 // comparison or a binary search, but is that worth any added
1938 // complexity?
1939 bind(L_huge);
1940 cmp(sp, zr); // Clear Z flag; SP is never zero
1941 repne_scan(r_array_base, r_super_klass, r_array_length, rscratch1);
1942 cset(result, NE); // result == 0 iff we got a match.
1943 }
1944
1945 bind(L_fallthrough);
1946 }
1947
1948 // Make sure that the hashed lookup and a linear scan agree.
1949 void MacroAssembler::verify_secondary_supers_table(Register r_sub_klass,
1950 Register r_super_klass,
1951 Register temp1,
1952 Register temp2,
1953 Register result) {
1954 assert_different_registers(r_sub_klass, r_super_klass, temp1, temp2, result, rscratch1);
1955
1956 const Register
1957 r_array_base = temp1,
1958 r_array_length = temp2,
1959 r_array_index = noreg, // unused
1960 r_bitmap = noreg; // unused
1961
1962 BLOCK_COMMENT("verify_secondary_supers_table {");
1963
1964 // We will consult the secondary-super array.
1965 ldr(r_array_base, Address(r_sub_klass, in_bytes(Klass::secondary_supers_offset())));
1966
1967 // Load the array length.
1968 ldrw(r_array_length, Address(r_array_base, Array<Klass*>::length_offset_in_bytes()));
1969 // And adjust the array base to point to the data.
1970 add(r_array_base, r_array_base, Array<Klass*>::base_offset_in_bytes());
1971
1972 cmp(sp, zr); // Clear Z flag; SP is never zero
1973 // Scan R2 words at [R5] for an occurrence of R0.
1974 // Set NZ/Z based on last compare.
1975 repne_scan(/*addr*/r_array_base, /*value*/r_super_klass, /*count*/r_array_length, rscratch2);
1976 // rscratch1 == 0 iff we got a match.
1977 cset(rscratch1, NE);
1978
1979 Label passed;
1980 cmp(result, zr);
1981 cset(result, NE); // normalize result to 0/1 for comparison
1982
1983 cmp(rscratch1, result);
1984 br(EQ, passed);
1985 {
1986 mov(r0, r_super_klass); // r0 <- r0
1987 mov(r1, r_sub_klass); // r1 <- r4
1988 mov(r2, /*expected*/rscratch1); // r2 <- r8
1989 mov(r3, result); // r3 <- r5
1990 mov(r4, (address)("mismatch")); // r4 <- const
1991 rt_call(CAST_FROM_FN_PTR(address, Klass::on_secondary_supers_verification_failure), rscratch2);
1992 should_not_reach_here();
1993 }
1994 bind(passed);
1995
1996 BLOCK_COMMENT("} verify_secondary_supers_table");
1997 }
1998
1999 void MacroAssembler::clinit_barrier(Register klass, Register scratch, Label* L_fast_path, Label* L_slow_path) {
2000 assert(L_fast_path != nullptr || L_slow_path != nullptr, "at least one is required");
2001 assert_different_registers(klass, rthread, scratch);
2002
2003 Label L_fallthrough, L_tmp;
2004 if (L_fast_path == nullptr) {
2005 L_fast_path = &L_fallthrough;
2006 } else if (L_slow_path == nullptr) {
2007 L_slow_path = &L_fallthrough;
2008 }
2009 // Fast path check: class is fully initialized
2010 lea(scratch, Address(klass, InstanceKlass::init_state_offset()));
2011 ldarb(scratch, scratch);
2012 cmp(scratch, InstanceKlass::fully_initialized);
2013 br(Assembler::EQ, *L_fast_path);
2014
2015 // Fast path check: current thread is initializer thread
2016 ldr(scratch, Address(klass, InstanceKlass::init_thread_offset()));
2017 cmp(rthread, scratch);
2018
2019 if (L_slow_path == &L_fallthrough) {
2020 br(Assembler::EQ, *L_fast_path);
2021 bind(*L_slow_path);
2022 } else if (L_fast_path == &L_fallthrough) {
2023 br(Assembler::NE, *L_slow_path);
2024 bind(*L_fast_path);
2025 } else {
2026 Unimplemented();
2027 }
2028 }
2029
2030 void MacroAssembler::_verify_oop(Register reg, const char* s, const char* file, int line) {
2031 if (!VerifyOops) return;
2032
2033 // Pass register number to verify_oop_subroutine
2034 const char* b = nullptr;
2035 {
2036 ResourceMark rm;
2037 stringStream ss;
2038 ss.print("verify_oop: %s: %s (%s:%d)", reg->name(), s, file, line);
2039 b = code_string(ss.as_string());
2040 }
2041 BLOCK_COMMENT("verify_oop {");
2042
2043 strip_return_address(); // This might happen within a stack frame.
2044 protect_return_address();
2045 stp(r0, rscratch1, Address(pre(sp, -2 * wordSize)));
2046 stp(rscratch2, lr, Address(pre(sp, -2 * wordSize)));
2047
2048 mov(r0, reg);
2049 movptr(rscratch1, (uintptr_t)(address)b);
2050
2051 // call indirectly to solve generation ordering problem
2052 lea(rscratch2, RuntimeAddress(StubRoutines::verify_oop_subroutine_entry_address()));
2053 ldr(rscratch2, Address(rscratch2));
2054 blr(rscratch2);
2055
2056 ldp(rscratch2, lr, Address(post(sp, 2 * wordSize)));
2057 ldp(r0, rscratch1, Address(post(sp, 2 * wordSize)));
2058 authenticate_return_address();
2059
2060 BLOCK_COMMENT("} verify_oop");
2061 }
2062
2063 void MacroAssembler::_verify_oop_addr(Address addr, const char* s, const char* file, int line) {
2064 if (!VerifyOops) return;
2065
2066 const char* b = nullptr;
2067 {
2068 ResourceMark rm;
2069 stringStream ss;
2070 ss.print("verify_oop_addr: %s (%s:%d)", s, file, line);
2071 b = code_string(ss.as_string());
2072 }
2073 BLOCK_COMMENT("verify_oop_addr {");
2074
2075 strip_return_address(); // This might happen within a stack frame.
2076 protect_return_address();
2077 stp(r0, rscratch1, Address(pre(sp, -2 * wordSize)));
2078 stp(rscratch2, lr, Address(pre(sp, -2 * wordSize)));
2079
2080 // addr may contain sp so we will have to adjust it based on the
2081 // pushes that we just did.
2082 if (addr.uses(sp)) {
2083 lea(r0, addr);
2084 ldr(r0, Address(r0, 4 * wordSize));
2085 } else {
2086 ldr(r0, addr);
2087 }
2088 movptr(rscratch1, (uintptr_t)(address)b);
2089
2090 // call indirectly to solve generation ordering problem
2091 lea(rscratch2, RuntimeAddress(StubRoutines::verify_oop_subroutine_entry_address()));
2092 ldr(rscratch2, Address(rscratch2));
2093 blr(rscratch2);
2094
2095 ldp(rscratch2, lr, Address(post(sp, 2 * wordSize)));
2096 ldp(r0, rscratch1, Address(post(sp, 2 * wordSize)));
2097 authenticate_return_address();
2098
2099 BLOCK_COMMENT("} verify_oop_addr");
2100 }
2101
2102 Address MacroAssembler::argument_address(RegisterOrConstant arg_slot,
2103 int extra_slot_offset) {
2104 // cf. TemplateTable::prepare_invoke(), if (load_receiver).
2105 int stackElementSize = Interpreter::stackElementSize;
2106 int offset = Interpreter::expr_offset_in_bytes(extra_slot_offset+0);
2107 #ifdef ASSERT
2108 int offset1 = Interpreter::expr_offset_in_bytes(extra_slot_offset+1);
2109 assert(offset1 - offset == stackElementSize, "correct arithmetic");
2110 #endif
2111 if (arg_slot.is_constant()) {
2112 return Address(esp, arg_slot.as_constant() * stackElementSize
2113 + offset);
2114 } else {
2115 add(rscratch1, esp, arg_slot.as_register(),
2116 ext::uxtx, exact_log2(stackElementSize));
2117 return Address(rscratch1, offset);
2118 }
2119 }
2120
2121 void MacroAssembler::call_VM_leaf_base(address entry_point,
2122 int number_of_arguments,
2123 Label *retaddr) {
2124 Label E, L;
2125
2126 stp(rscratch1, rmethod, Address(pre(sp, -2 * wordSize)));
2127
2128 mov(rscratch1, RuntimeAddress(entry_point));
2129 blr(rscratch1);
2130 if (retaddr)
2131 bind(*retaddr);
2132
2133 ldp(rscratch1, rmethod, Address(post(sp, 2 * wordSize)));
2134 }
2135
2136 void MacroAssembler::call_VM_leaf(address entry_point, int number_of_arguments) {
2137 call_VM_leaf_base(entry_point, number_of_arguments);
2138 }
2139
2140 void MacroAssembler::call_VM_leaf(address entry_point, Register arg_0) {
2141 pass_arg0(this, arg_0);
2142 call_VM_leaf_base(entry_point, 1);
2143 }
2144
2145 void MacroAssembler::call_VM_leaf(address entry_point, Register arg_0, Register arg_1) {
2146 assert_different_registers(arg_1, c_rarg0);
2147 pass_arg0(this, arg_0);
2148 pass_arg1(this, arg_1);
2149 call_VM_leaf_base(entry_point, 2);
2150 }
2151
2152 void MacroAssembler::call_VM_leaf(address entry_point, Register arg_0,
2153 Register arg_1, Register arg_2) {
2154 assert_different_registers(arg_1, c_rarg0);
2155 assert_different_registers(arg_2, c_rarg0, c_rarg1);
2156 pass_arg0(this, arg_0);
2157 pass_arg1(this, arg_1);
2158 pass_arg2(this, arg_2);
2159 call_VM_leaf_base(entry_point, 3);
2160 }
2161
2162 void MacroAssembler::super_call_VM_leaf(address entry_point, Register arg_0) {
2163 pass_arg0(this, arg_0);
2164 MacroAssembler::call_VM_leaf_base(entry_point, 1);
2165 }
2166
2167 void MacroAssembler::super_call_VM_leaf(address entry_point, Register arg_0, Register arg_1) {
2168
2169 assert_different_registers(arg_0, c_rarg1);
2170 pass_arg1(this, arg_1);
2171 pass_arg0(this, arg_0);
2172 MacroAssembler::call_VM_leaf_base(entry_point, 2);
2173 }
2174
2175 void MacroAssembler::super_call_VM_leaf(address entry_point, Register arg_0, Register arg_1, Register arg_2) {
2176 assert_different_registers(arg_0, c_rarg1, c_rarg2);
2177 assert_different_registers(arg_1, c_rarg2);
2178 pass_arg2(this, arg_2);
2179 pass_arg1(this, arg_1);
2180 pass_arg0(this, arg_0);
2181 MacroAssembler::call_VM_leaf_base(entry_point, 3);
2182 }
2183
2184 void MacroAssembler::super_call_VM_leaf(address entry_point, Register arg_0, Register arg_1, Register arg_2, Register arg_3) {
2185 assert_different_registers(arg_0, c_rarg1, c_rarg2, c_rarg3);
2186 assert_different_registers(arg_1, c_rarg2, c_rarg3);
2187 assert_different_registers(arg_2, c_rarg3);
2188 pass_arg3(this, arg_3);
2189 pass_arg2(this, arg_2);
2190 pass_arg1(this, arg_1);
2191 pass_arg0(this, arg_0);
2192 MacroAssembler::call_VM_leaf_base(entry_point, 4);
2193 }
2194
2195 void MacroAssembler::null_check(Register reg, int offset) {
2196 if (needs_explicit_null_check(offset)) {
2197 // provoke OS null exception if reg is null by
2198 // accessing M[reg] w/o changing any registers
2199 // NOTE: this is plenty to provoke a segv
2200 ldr(zr, Address(reg));
2201 } else {
2202 // nothing to do, (later) access of M[reg + offset]
2203 // will provoke OS null exception if reg is null
2204 }
2205 }
2206
2207 // MacroAssembler protected routines needed to implement
2208 // public methods
2209
2210 void MacroAssembler::mov(Register r, Address dest) {
2211 code_section()->relocate(pc(), dest.rspec());
2212 uint64_t imm64 = (uint64_t)dest.target();
2213 movptr(r, imm64);
2214 }
2215
2216 // Move a constant pointer into r. In AArch64 mode the virtual
2217 // address space is 48 bits in size, so we only need three
2218 // instructions to create a patchable instruction sequence that can
2219 // reach anywhere.
2220 void MacroAssembler::movptr(Register r, uintptr_t imm64) {
2221 #ifndef PRODUCT
2222 {
2223 char buffer[64];
2224 os::snprintf_checked(buffer, sizeof(buffer), "0x%" PRIX64, (uint64_t)imm64);
2225 block_comment(buffer);
2226 }
2227 #endif
2228 assert(imm64 < (1ull << 48), "48-bit overflow in address constant");
2229 movz(r, imm64 & 0xffff);
2230 imm64 >>= 16;
2231 movk(r, imm64 & 0xffff, 16);
2232 imm64 >>= 16;
2233 movk(r, imm64 & 0xffff, 32);
2234 }
2235
2236 // Macro to mov replicated immediate to vector register.
2237 // imm64: only the lower 8/16/32 bits are considered for B/H/S type. That is,
2238 // the upper 56/48/32 bits must be zeros for B/H/S type.
2239 // Vd will get the following values for different arrangements in T
2240 // imm64 == hex 000000gh T8B: Vd = ghghghghghghghgh
2241 // imm64 == hex 000000gh T16B: Vd = ghghghghghghghghghghghghghghghgh
2242 // imm64 == hex 0000efgh T4H: Vd = efghefghefghefgh
2243 // imm64 == hex 0000efgh T8H: Vd = efghefghefghefghefghefghefghefgh
2244 // imm64 == hex abcdefgh T2S: Vd = abcdefghabcdefgh
2245 // imm64 == hex abcdefgh T4S: Vd = abcdefghabcdefghabcdefghabcdefgh
2246 // imm64 == hex abcdefgh T1D: Vd = 00000000abcdefgh
2247 // imm64 == hex abcdefgh T2D: Vd = 00000000abcdefgh00000000abcdefgh
2248 // Clobbers rscratch1
2249 void MacroAssembler::mov(FloatRegister Vd, SIMD_Arrangement T, uint64_t imm64) {
2250 assert(T != T1Q, "unsupported");
2251 if (T == T1D || T == T2D) {
2252 int imm = operand_valid_for_movi_immediate(imm64, T);
2253 if (-1 != imm) {
2254 movi(Vd, T, imm);
2255 } else {
2256 mov(rscratch1, imm64);
2257 dup(Vd, T, rscratch1);
2258 }
2259 return;
2260 }
2261
2262 #ifdef ASSERT
2263 if (T == T8B || T == T16B) assert((imm64 & ~0xff) == 0, "extraneous bits (T8B/T16B)");
2264 if (T == T4H || T == T8H) assert((imm64 & ~0xffff) == 0, "extraneous bits (T4H/T8H)");
2265 if (T == T2S || T == T4S) assert((imm64 & ~0xffffffff) == 0, "extraneous bits (T2S/T4S)");
2266 #endif
2267 int shift = operand_valid_for_movi_immediate(imm64, T);
2268 uint32_t imm32 = imm64 & 0xffffffffULL;
2269 if (shift >= 0) {
2270 movi(Vd, T, (imm32 >> shift) & 0xff, shift);
2271 } else {
2272 movw(rscratch1, imm32);
2273 dup(Vd, T, rscratch1);
2274 }
2275 }
2276
2277 void MacroAssembler::mov_immediate64(Register dst, uint64_t imm64)
2278 {
2279 #ifndef PRODUCT
2280 {
2281 char buffer[64];
2282 os::snprintf_checked(buffer, sizeof(buffer), "0x%" PRIX64, imm64);
2283 block_comment(buffer);
2284 }
2285 #endif
2286 if (operand_valid_for_logical_immediate(false, imm64)) {
2287 orr(dst, zr, imm64);
2288 } else {
2289 // we can use a combination of MOVZ or MOVN with
2290 // MOVK to build up the constant
2291 uint64_t imm_h[4];
2292 int zero_count = 0;
2293 int neg_count = 0;
2294 int i;
2295 for (i = 0; i < 4; i++) {
2296 imm_h[i] = ((imm64 >> (i * 16)) & 0xffffL);
2297 if (imm_h[i] == 0) {
2298 zero_count++;
2299 } else if (imm_h[i] == 0xffffL) {
2300 neg_count++;
2301 }
2302 }
2303 if (zero_count == 4) {
2304 // one MOVZ will do
2305 movz(dst, 0);
2306 } else if (neg_count == 4) {
2307 // one MOVN will do
2308 movn(dst, 0);
2309 } else if (zero_count == 3) {
2310 for (i = 0; i < 4; i++) {
2311 if (imm_h[i] != 0L) {
2312 movz(dst, (uint32_t)imm_h[i], (i << 4));
2313 break;
2314 }
2315 }
2316 } else if (neg_count == 3) {
2317 // one MOVN will do
2318 for (int i = 0; i < 4; i++) {
2319 if (imm_h[i] != 0xffffL) {
2320 movn(dst, (uint32_t)imm_h[i] ^ 0xffffL, (i << 4));
2321 break;
2322 }
2323 }
2324 } else if (zero_count == 2) {
2325 // one MOVZ and one MOVK will do
2326 for (i = 0; i < 3; i++) {
2327 if (imm_h[i] != 0L) {
2328 movz(dst, (uint32_t)imm_h[i], (i << 4));
2329 i++;
2330 break;
2331 }
2332 }
2333 for (;i < 4; i++) {
2334 if (imm_h[i] != 0L) {
2335 movk(dst, (uint32_t)imm_h[i], (i << 4));
2336 }
2337 }
2338 } else if (neg_count == 2) {
2339 // one MOVN and one MOVK will do
2340 for (i = 0; i < 4; i++) {
2341 if (imm_h[i] != 0xffffL) {
2342 movn(dst, (uint32_t)imm_h[i] ^ 0xffffL, (i << 4));
2343 i++;
2344 break;
2345 }
2346 }
2347 for (;i < 4; i++) {
2348 if (imm_h[i] != 0xffffL) {
2349 movk(dst, (uint32_t)imm_h[i], (i << 4));
2350 }
2351 }
2352 } else if (zero_count == 1) {
2353 // one MOVZ and two MOVKs will do
2354 for (i = 0; i < 4; i++) {
2355 if (imm_h[i] != 0L) {
2356 movz(dst, (uint32_t)imm_h[i], (i << 4));
2357 i++;
2358 break;
2359 }
2360 }
2361 for (;i < 4; i++) {
2362 if (imm_h[i] != 0x0L) {
2363 movk(dst, (uint32_t)imm_h[i], (i << 4));
2364 }
2365 }
2366 } else if (neg_count == 1) {
2367 // one MOVN and two MOVKs will do
2368 for (i = 0; i < 4; i++) {
2369 if (imm_h[i] != 0xffffL) {
2370 movn(dst, (uint32_t)imm_h[i] ^ 0xffffL, (i << 4));
2371 i++;
2372 break;
2373 }
2374 }
2375 for (;i < 4; i++) {
2376 if (imm_h[i] != 0xffffL) {
2377 movk(dst, (uint32_t)imm_h[i], (i << 4));
2378 }
2379 }
2380 } else {
2381 // use a MOVZ and 3 MOVKs (makes it easier to debug)
2382 movz(dst, (uint32_t)imm_h[0], 0);
2383 for (i = 1; i < 4; i++) {
2384 movk(dst, (uint32_t)imm_h[i], (i << 4));
2385 }
2386 }
2387 }
2388 }
2389
2390 void MacroAssembler::mov_immediate32(Register dst, uint32_t imm32)
2391 {
2392 #ifndef PRODUCT
2393 {
2394 char buffer[64];
2395 os::snprintf_checked(buffer, sizeof(buffer), "0x%" PRIX32, imm32);
2396 block_comment(buffer);
2397 }
2398 #endif
2399 if (operand_valid_for_logical_immediate(true, imm32)) {
2400 orrw(dst, zr, imm32);
2401 } else {
2402 // we can use MOVZ, MOVN or two calls to MOVK to build up the
2403 // constant
2404 uint32_t imm_h[2];
2405 imm_h[0] = imm32 & 0xffff;
2406 imm_h[1] = ((imm32 >> 16) & 0xffff);
2407 if (imm_h[0] == 0) {
2408 movzw(dst, imm_h[1], 16);
2409 } else if (imm_h[0] == 0xffff) {
2410 movnw(dst, imm_h[1] ^ 0xffff, 16);
2411 } else if (imm_h[1] == 0) {
2412 movzw(dst, imm_h[0], 0);
2413 } else if (imm_h[1] == 0xffff) {
2414 movnw(dst, imm_h[0] ^ 0xffff, 0);
2415 } else {
2416 // use a MOVZ and MOVK (makes it easier to debug)
2417 movzw(dst, imm_h[0], 0);
2418 movkw(dst, imm_h[1], 16);
2419 }
2420 }
2421 }
2422
2423 // Form an address from base + offset in Rd. Rd may or may
2424 // not actually be used: you must use the Address that is returned.
2425 // It is up to you to ensure that the shift provided matches the size
2426 // of your data.
2427 Address MacroAssembler::form_address(Register Rd, Register base, int64_t byte_offset, int shift) {
2428 if (Address::offset_ok_for_immed(byte_offset, shift))
2429 // It fits; no need for any heroics
2430 return Address(base, byte_offset);
2431
2432 // Don't do anything clever with negative or misaligned offsets
2433 unsigned mask = (1 << shift) - 1;
2434 if (byte_offset < 0 || byte_offset & mask) {
2435 mov(Rd, byte_offset);
2436 add(Rd, base, Rd);
2437 return Address(Rd);
2438 }
2439
2440 // See if we can do this with two 12-bit offsets
2441 {
2442 uint64_t word_offset = byte_offset >> shift;
2443 uint64_t masked_offset = word_offset & 0xfff000;
2444 if (Address::offset_ok_for_immed(word_offset - masked_offset, 0)
2445 && Assembler::operand_valid_for_add_sub_immediate(masked_offset << shift)) {
2446 add(Rd, base, masked_offset << shift);
2447 word_offset -= masked_offset;
2448 return Address(Rd, word_offset << shift);
2449 }
2450 }
2451
2452 // Do it the hard way
2453 mov(Rd, byte_offset);
2454 add(Rd, base, Rd);
2455 return Address(Rd);
2456 }
2457
2458 int MacroAssembler::corrected_idivl(Register result, Register ra, Register rb,
2459 bool want_remainder, Register scratch)
2460 {
2461 // Full implementation of Java idiv and irem. The function
2462 // returns the (pc) offset of the div instruction - may be needed
2463 // for implicit exceptions.
2464 //
2465 // constraint : ra/rb =/= scratch
2466 // normal case
2467 //
2468 // input : ra: dividend
2469 // rb: divisor
2470 //
2471 // result: either
2472 // quotient (= ra idiv rb)
2473 // remainder (= ra irem rb)
2474
2475 assert(ra != scratch && rb != scratch, "reg cannot be scratch");
2476
2477 int idivl_offset = offset();
2478 if (! want_remainder) {
2479 sdivw(result, ra, rb);
2480 } else {
2481 sdivw(scratch, ra, rb);
2482 Assembler::msubw(result, scratch, rb, ra);
2483 }
2484
2485 return idivl_offset;
2486 }
2487
2488 int MacroAssembler::corrected_idivq(Register result, Register ra, Register rb,
2489 bool want_remainder, Register scratch)
2490 {
2491 // Full implementation of Java ldiv and lrem. The function
2492 // returns the (pc) offset of the div instruction - may be needed
2493 // for implicit exceptions.
2494 //
2495 // constraint : ra/rb =/= scratch
2496 // normal case
2497 //
2498 // input : ra: dividend
2499 // rb: divisor
2500 //
2501 // result: either
2502 // quotient (= ra idiv rb)
2503 // remainder (= ra irem rb)
2504
2505 assert(ra != scratch && rb != scratch, "reg cannot be scratch");
2506
2507 int idivq_offset = offset();
2508 if (! want_remainder) {
2509 sdiv(result, ra, rb);
2510 } else {
2511 sdiv(scratch, ra, rb);
2512 Assembler::msub(result, scratch, rb, ra);
2513 }
2514
2515 return idivq_offset;
2516 }
2517
2518 void MacroAssembler::membar(Membar_mask_bits order_constraint) {
2519 address prev = pc() - NativeMembar::instruction_size;
2520 address last = code()->last_insn();
2521 if (last != nullptr && nativeInstruction_at(last)->is_Membar() && prev == last) {
2522 NativeMembar *bar = NativeMembar_at(prev);
2523 if (AlwaysMergeDMB) {
2524 bar->set_kind(bar->get_kind() | order_constraint);
2525 BLOCK_COMMENT("merged membar(always)");
2526 return;
2527 }
2528 // Don't promote DMB ST|DMB LD to DMB (a full barrier) because
2529 // doing so would introduce a StoreLoad which the caller did not
2530 // intend
2531 if (bar->get_kind() == order_constraint
2532 || bar->get_kind() == AnyAny
2533 || order_constraint == AnyAny) {
2534 // We are merging two memory barrier instructions. On AArch64 we
2535 // can do this simply by ORing them together.
2536 bar->set_kind(bar->get_kind() | order_constraint);
2537 BLOCK_COMMENT("merged membar");
2538 return;
2539 } else {
2540 // A special case like "DMB ST;DMB LD;DMB ST", the last DMB can be skipped
2541 // We need check the last 2 instructions
2542 address prev2 = prev - NativeMembar::instruction_size;
2543 if (last != code()->last_label() && nativeInstruction_at(prev2)->is_Membar()) {
2544 NativeMembar *bar2 = NativeMembar_at(prev2);
2545 assert(bar2->get_kind() == order_constraint, "it should be merged before");
2546 BLOCK_COMMENT("merged membar(elided)");
2547 return;
2548 }
2549 }
2550 }
2551 code()->set_last_insn(pc());
2552 dmb(Assembler::barrier(order_constraint));
2553 }
2554
2555 bool MacroAssembler::try_merge_ldst(Register rt, const Address &adr, size_t size_in_bytes, bool is_store) {
2556 if (ldst_can_merge(rt, adr, size_in_bytes, is_store)) {
2557 merge_ldst(rt, adr, size_in_bytes, is_store);
2558 code()->clear_last_insn();
2559 return true;
2560 } else {
2561 assert(size_in_bytes == 8 || size_in_bytes == 4, "only 8 bytes or 4 bytes load/store is supported.");
2562 const uint64_t mask = size_in_bytes - 1;
2563 if (adr.getMode() == Address::base_plus_offset &&
2564 (adr.offset() & mask) == 0) { // only supports base_plus_offset.
2565 code()->set_last_insn(pc());
2566 }
2567 return false;
2568 }
2569 }
2570
2571 void MacroAssembler::ldr(Register Rx, const Address &adr) {
2572 // We always try to merge two adjacent loads into one ldp.
2573 if (!try_merge_ldst(Rx, adr, 8, false)) {
2574 Assembler::ldr(Rx, adr);
2575 }
2576 }
2577
2578 void MacroAssembler::ldrw(Register Rw, const Address &adr) {
2579 // We always try to merge two adjacent loads into one ldp.
2580 if (!try_merge_ldst(Rw, adr, 4, false)) {
2581 Assembler::ldrw(Rw, adr);
2582 }
2583 }
2584
2585 void MacroAssembler::str(Register Rx, const Address &adr) {
2586 // We always try to merge two adjacent stores into one stp.
2587 if (!try_merge_ldst(Rx, adr, 8, true)) {
2588 Assembler::str(Rx, adr);
2589 }
2590 }
2591
2592 void MacroAssembler::strw(Register Rw, const Address &adr) {
2593 // We always try to merge two adjacent stores into one stp.
2594 if (!try_merge_ldst(Rw, adr, 4, true)) {
2595 Assembler::strw(Rw, adr);
2596 }
2597 }
2598
2599 // MacroAssembler routines found actually to be needed
2600
2601 void MacroAssembler::push(Register src)
2602 {
2603 str(src, Address(pre(esp, -1 * wordSize)));
2604 }
2605
2606 void MacroAssembler::pop(Register dst)
2607 {
2608 ldr(dst, Address(post(esp, 1 * wordSize)));
2609 }
2610
2611 // Note: load_unsigned_short used to be called load_unsigned_word.
2612 int MacroAssembler::load_unsigned_short(Register dst, Address src) {
2613 int off = offset();
2614 ldrh(dst, src);
2615 return off;
2616 }
2617
2618 int MacroAssembler::load_unsigned_byte(Register dst, Address src) {
2619 int off = offset();
2620 ldrb(dst, src);
2621 return off;
2622 }
2623
2624 int MacroAssembler::load_signed_short(Register dst, Address src) {
2625 int off = offset();
2626 ldrsh(dst, src);
2627 return off;
2628 }
2629
2630 int MacroAssembler::load_signed_byte(Register dst, Address src) {
2631 int off = offset();
2632 ldrsb(dst, src);
2633 return off;
2634 }
2635
2636 int MacroAssembler::load_signed_short32(Register dst, Address src) {
2637 int off = offset();
2638 ldrshw(dst, src);
2639 return off;
2640 }
2641
2642 int MacroAssembler::load_signed_byte32(Register dst, Address src) {
2643 int off = offset();
2644 ldrsbw(dst, src);
2645 return off;
2646 }
2647
2648 void MacroAssembler::load_sized_value(Register dst, Address src, size_t size_in_bytes, bool is_signed) {
2649 switch (size_in_bytes) {
2650 case 8: ldr(dst, src); break;
2651 case 4: ldrw(dst, src); break;
2652 case 2: is_signed ? load_signed_short(dst, src) : load_unsigned_short(dst, src); break;
2653 case 1: is_signed ? load_signed_byte( dst, src) : load_unsigned_byte( dst, src); break;
2654 default: ShouldNotReachHere();
2655 }
2656 }
2657
2658 void MacroAssembler::store_sized_value(Address dst, Register src, size_t size_in_bytes) {
2659 switch (size_in_bytes) {
2660 case 8: str(src, dst); break;
2661 case 4: strw(src, dst); break;
2662 case 2: strh(src, dst); break;
2663 case 1: strb(src, dst); break;
2664 default: ShouldNotReachHere();
2665 }
2666 }
2667
2668 void MacroAssembler::decrementw(Register reg, int value)
2669 {
2670 if (value < 0) { incrementw(reg, -value); return; }
2671 if (value == 0) { return; }
2672 if (value < (1 << 12)) { subw(reg, reg, value); return; }
2673 /* else */ {
2674 guarantee(reg != rscratch2, "invalid dst for register decrement");
2675 movw(rscratch2, (unsigned)value);
2676 subw(reg, reg, rscratch2);
2677 }
2678 }
2679
2680 void MacroAssembler::decrement(Register reg, int value)
2681 {
2682 if (value < 0) { increment(reg, -value); return; }
2683 if (value == 0) { return; }
2684 if (value < (1 << 12)) { sub(reg, reg, value); return; }
2685 /* else */ {
2686 assert(reg != rscratch2, "invalid dst for register decrement");
2687 mov(rscratch2, (uint64_t)value);
2688 sub(reg, reg, rscratch2);
2689 }
2690 }
2691
2692 void MacroAssembler::decrementw(Address dst, int value)
2693 {
2694 assert(!dst.uses(rscratch1), "invalid dst for address decrement");
2695 if (dst.getMode() == Address::literal) {
2696 assert(abs(value) < (1 << 12), "invalid value and address mode combination");
2697 lea(rscratch2, dst);
2698 dst = Address(rscratch2);
2699 }
2700 ldrw(rscratch1, dst);
2701 decrementw(rscratch1, value);
2702 strw(rscratch1, dst);
2703 }
2704
2705 void MacroAssembler::decrement(Address dst, int value)
2706 {
2707 assert(!dst.uses(rscratch1), "invalid address for decrement");
2708 if (dst.getMode() == Address::literal) {
2709 assert(abs(value) < (1 << 12), "invalid value and address mode combination");
2710 lea(rscratch2, dst);
2711 dst = Address(rscratch2);
2712 }
2713 ldr(rscratch1, dst);
2714 decrement(rscratch1, value);
2715 str(rscratch1, dst);
2716 }
2717
2718 void MacroAssembler::incrementw(Register reg, int value)
2719 {
2720 if (value < 0) { decrementw(reg, -value); return; }
2721 if (value == 0) { return; }
2722 if (value < (1 << 12)) { addw(reg, reg, value); return; }
2723 /* else */ {
2724 assert(reg != rscratch2, "invalid dst for register increment");
2725 movw(rscratch2, (unsigned)value);
2726 addw(reg, reg, rscratch2);
2727 }
2728 }
2729
2730 void MacroAssembler::increment(Register reg, int value)
2731 {
2732 if (value < 0) { decrement(reg, -value); return; }
2733 if (value == 0) { return; }
2734 if (value < (1 << 12)) { add(reg, reg, value); return; }
2735 /* else */ {
2736 assert(reg != rscratch2, "invalid dst for register increment");
2737 movw(rscratch2, (unsigned)value);
2738 add(reg, reg, rscratch2);
2739 }
2740 }
2741
2742 void MacroAssembler::incrementw(Address dst, int value)
2743 {
2744 assert(!dst.uses(rscratch1), "invalid dst for address increment");
2745 if (dst.getMode() == Address::literal) {
2746 assert(abs(value) < (1 << 12), "invalid value and address mode combination");
2747 lea(rscratch2, dst);
2748 dst = Address(rscratch2);
2749 }
2750 ldrw(rscratch1, dst);
2751 incrementw(rscratch1, value);
2752 strw(rscratch1, dst);
2753 }
2754
2755 void MacroAssembler::increment(Address dst, int value)
2756 {
2757 assert(!dst.uses(rscratch1), "invalid dst for address increment");
2758 if (dst.getMode() == Address::literal) {
2759 assert(abs(value) < (1 << 12), "invalid value and address mode combination");
2760 lea(rscratch2, dst);
2761 dst = Address(rscratch2);
2762 }
2763 ldr(rscratch1, dst);
2764 increment(rscratch1, value);
2765 str(rscratch1, dst);
2766 }
2767
2768 // Push lots of registers in the bit set supplied. Don't push sp.
2769 // Return the number of words pushed
2770 int MacroAssembler::push(unsigned int bitset, Register stack) {
2771 int words_pushed = 0;
2772
2773 // Scan bitset to accumulate register pairs
2774 unsigned char regs[32];
2775 int count = 0;
2776 for (int reg = 0; reg <= 30; reg++) {
2777 if (1 & bitset)
2778 regs[count++] = reg;
2779 bitset >>= 1;
2780 }
2781 regs[count++] = zr->raw_encoding();
2782 count &= ~1; // Only push an even number of regs
2783
2784 if (count) {
2785 stp(as_Register(regs[0]), as_Register(regs[1]),
2786 Address(pre(stack, -count * wordSize)));
2787 words_pushed += 2;
2788 }
2789 for (int i = 2; i < count; i += 2) {
2790 stp(as_Register(regs[i]), as_Register(regs[i+1]),
2791 Address(stack, i * wordSize));
2792 words_pushed += 2;
2793 }
2794
2795 assert(words_pushed == count, "oops, pushed != count");
2796
2797 return count;
2798 }
2799
2800 int MacroAssembler::pop(unsigned int bitset, Register stack) {
2801 int words_pushed = 0;
2802
2803 // Scan bitset to accumulate register pairs
2804 unsigned char regs[32];
2805 int count = 0;
2806 for (int reg = 0; reg <= 30; reg++) {
2807 if (1 & bitset)
2808 regs[count++] = reg;
2809 bitset >>= 1;
2810 }
2811 regs[count++] = zr->raw_encoding();
2812 count &= ~1;
2813
2814 for (int i = 2; i < count; i += 2) {
2815 ldp(as_Register(regs[i]), as_Register(regs[i+1]),
2816 Address(stack, i * wordSize));
2817 words_pushed += 2;
2818 }
2819 if (count) {
2820 ldp(as_Register(regs[0]), as_Register(regs[1]),
2821 Address(post(stack, count * wordSize)));
2822 words_pushed += 2;
2823 }
2824
2825 assert(words_pushed == count, "oops, pushed != count");
2826
2827 return count;
2828 }
2829
2830 // Push lots of registers in the bit set supplied. Don't push sp.
2831 // Return the number of dwords pushed
2832 int MacroAssembler::push_fp(unsigned int bitset, Register stack, FpPushPopMode mode) {
2833 int words_pushed = 0;
2834 bool use_sve = false;
2835 int sve_vector_size_in_bytes = 0;
2836
2837 #ifdef COMPILER2
2838 use_sve = Matcher::supports_scalable_vector();
2839 sve_vector_size_in_bytes = Matcher::scalable_vector_reg_size(T_BYTE);
2840 #endif
2841
2842 // Scan bitset to accumulate register pairs
2843 unsigned char regs[32];
2844 int count = 0;
2845 for (int reg = 0; reg <= 31; reg++) {
2846 if (1 & bitset)
2847 regs[count++] = reg;
2848 bitset >>= 1;
2849 }
2850
2851 if (count == 0) {
2852 return 0;
2853 }
2854
2855 if (mode == PushPopFull) {
2856 if (use_sve && sve_vector_size_in_bytes > 16) {
2857 mode = PushPopSVE;
2858 } else {
2859 mode = PushPopNeon;
2860 }
2861 }
2862
2863 #ifndef PRODUCT
2864 {
2865 char buffer[48];
2866 if (mode == PushPopSVE) {
2867 os::snprintf_checked(buffer, sizeof(buffer), "push_fp: %d SVE registers", count);
2868 } else if (mode == PushPopNeon) {
2869 os::snprintf_checked(buffer, sizeof(buffer), "push_fp: %d Neon registers", count);
2870 } else {
2871 os::snprintf_checked(buffer, sizeof(buffer), "push_fp: %d fp registers", count);
2872 }
2873 block_comment(buffer);
2874 }
2875 #endif
2876
2877 if (mode == PushPopSVE) {
2878 sub(stack, stack, sve_vector_size_in_bytes * count);
2879 for (int i = 0; i < count; i++) {
2880 sve_str(as_FloatRegister(regs[i]), Address(stack, i));
2881 }
2882 return count * sve_vector_size_in_bytes / 8;
2883 }
2884
2885 if (mode == PushPopNeon) {
2886 if (count == 1) {
2887 strq(as_FloatRegister(regs[0]), Address(pre(stack, -wordSize * 2)));
2888 return 2;
2889 }
2890
2891 bool odd = (count & 1) == 1;
2892 int push_slots = count + (odd ? 1 : 0);
2893
2894 // Always pushing full 128 bit registers.
2895 stpq(as_FloatRegister(regs[0]), as_FloatRegister(regs[1]), Address(pre(stack, -push_slots * wordSize * 2)));
2896 words_pushed += 2;
2897
2898 for (int i = 2; i + 1 < count; i += 2) {
2899 stpq(as_FloatRegister(regs[i]), as_FloatRegister(regs[i+1]), Address(stack, i * wordSize * 2));
2900 words_pushed += 2;
2901 }
2902
2903 if (odd) {
2904 strq(as_FloatRegister(regs[count - 1]), Address(stack, (count - 1) * wordSize * 2));
2905 words_pushed++;
2906 }
2907
2908 assert(words_pushed == count, "oops, pushed(%d) != count(%d)", words_pushed, count);
2909 return count * 2;
2910 }
2911
2912 if (mode == PushPopFp) {
2913 bool odd = (count & 1) == 1;
2914 int push_slots = count + (odd ? 1 : 0);
2915
2916 if (count == 1) {
2917 // Stack pointer must be 16 bytes aligned
2918 strd(as_FloatRegister(regs[0]), Address(pre(stack, -push_slots * wordSize)));
2919 return 1;
2920 }
2921
2922 stpd(as_FloatRegister(regs[0]), as_FloatRegister(regs[1]), Address(pre(stack, -push_slots * wordSize)));
2923 words_pushed += 2;
2924
2925 for (int i = 2; i + 1 < count; i += 2) {
2926 stpd(as_FloatRegister(regs[i]), as_FloatRegister(regs[i+1]), Address(stack, i * wordSize));
2927 words_pushed += 2;
2928 }
2929
2930 if (odd) {
2931 // Stack pointer must be 16 bytes aligned
2932 strd(as_FloatRegister(regs[count - 1]), Address(stack, (count - 1) * wordSize));
2933 words_pushed++;
2934 }
2935
2936 assert(words_pushed == count, "oops, pushed != count");
2937
2938 return count;
2939 }
2940
2941 return 0;
2942 }
2943
2944 // Return the number of dwords popped
2945 int MacroAssembler::pop_fp(unsigned int bitset, Register stack, FpPushPopMode mode) {
2946 int words_pushed = 0;
2947 bool use_sve = false;
2948 int sve_vector_size_in_bytes = 0;
2949
2950 #ifdef COMPILER2
2951 use_sve = Matcher::supports_scalable_vector();
2952 sve_vector_size_in_bytes = Matcher::scalable_vector_reg_size(T_BYTE);
2953 #endif
2954 // Scan bitset to accumulate register pairs
2955 unsigned char regs[32];
2956 int count = 0;
2957 for (int reg = 0; reg <= 31; reg++) {
2958 if (1 & bitset)
2959 regs[count++] = reg;
2960 bitset >>= 1;
2961 }
2962
2963 if (count == 0) {
2964 return 0;
2965 }
2966
2967 if (mode == PushPopFull) {
2968 if (use_sve && sve_vector_size_in_bytes > 16) {
2969 mode = PushPopSVE;
2970 } else {
2971 mode = PushPopNeon;
2972 }
2973 }
2974
2975 #ifndef PRODUCT
2976 {
2977 char buffer[48];
2978 if (mode == PushPopSVE) {
2979 os::snprintf_checked(buffer, sizeof(buffer), "pop_fp: %d SVE registers", count);
2980 } else if (mode == PushPopNeon) {
2981 os::snprintf_checked(buffer, sizeof(buffer), "pop_fp: %d Neon registers", count);
2982 } else {
2983 os::snprintf_checked(buffer, sizeof(buffer), "pop_fp: %d fp registers", count);
2984 }
2985 block_comment(buffer);
2986 }
2987 #endif
2988
2989 if (mode == PushPopSVE) {
2990 for (int i = count - 1; i >= 0; i--) {
2991 sve_ldr(as_FloatRegister(regs[i]), Address(stack, i));
2992 }
2993 add(stack, stack, sve_vector_size_in_bytes * count);
2994 return count * sve_vector_size_in_bytes / 8;
2995 }
2996
2997 if (mode == PushPopNeon) {
2998 if (count == 1) {
2999 ldrq(as_FloatRegister(regs[0]), Address(post(stack, wordSize * 2)));
3000 return 2;
3001 }
3002
3003 bool odd = (count & 1) == 1;
3004 int push_slots = count + (odd ? 1 : 0);
3005
3006 if (odd) {
3007 ldrq(as_FloatRegister(regs[count - 1]), Address(stack, (count - 1) * wordSize * 2));
3008 words_pushed++;
3009 }
3010
3011 for (int i = 2; i + 1 < count; i += 2) {
3012 ldpq(as_FloatRegister(regs[i]), as_FloatRegister(regs[i+1]), Address(stack, i * wordSize * 2));
3013 words_pushed += 2;
3014 }
3015
3016 ldpq(as_FloatRegister(regs[0]), as_FloatRegister(regs[1]), Address(post(stack, push_slots * wordSize * 2)));
3017 words_pushed += 2;
3018
3019 assert(words_pushed == count, "oops, pushed(%d) != count(%d)", words_pushed, count);
3020
3021 return count * 2;
3022 }
3023
3024 if (mode == PushPopFp) {
3025 bool odd = (count & 1) == 1;
3026 int push_slots = count + (odd ? 1 : 0);
3027
3028 if (count == 1) {
3029 ldrd(as_FloatRegister(regs[0]), Address(post(stack, push_slots * wordSize)));
3030 return 1;
3031 }
3032
3033 if (odd) {
3034 ldrd(as_FloatRegister(regs[count - 1]), Address(stack, (count - 1) * wordSize));
3035 words_pushed++;
3036 }
3037
3038 for (int i = 2; i + 1 < count; i += 2) {
3039 ldpd(as_FloatRegister(regs[i]), as_FloatRegister(regs[i+1]), Address(stack, i * wordSize));
3040 words_pushed += 2;
3041 }
3042
3043 ldpd(as_FloatRegister(regs[0]), as_FloatRegister(regs[1]), Address(post(stack, push_slots * wordSize)));
3044 words_pushed += 2;
3045
3046 assert(words_pushed == count, "oops, pushed != count");
3047
3048 return count;
3049 }
3050
3051 return 0;
3052 }
3053
3054 // Return the number of dwords pushed
3055 int MacroAssembler::push_p(unsigned int bitset, Register stack) {
3056 bool use_sve = false;
3057 int sve_predicate_size_in_slots = 0;
3058
3059 #ifdef COMPILER2
3060 use_sve = Matcher::supports_scalable_vector();
3061 if (use_sve) {
3062 sve_predicate_size_in_slots = Matcher::scalable_predicate_reg_slots();
3063 }
3064 #endif
3065
3066 if (!use_sve) {
3067 return 0;
3068 }
3069
3070 unsigned char regs[PRegister::number_of_registers];
3071 int count = 0;
3072 for (int reg = 0; reg < PRegister::number_of_registers; reg++) {
3073 if (1 & bitset)
3074 regs[count++] = reg;
3075 bitset >>= 1;
3076 }
3077
3078 if (count == 0) {
3079 return 0;
3080 }
3081
3082 int total_push_bytes = align_up(sve_predicate_size_in_slots *
3083 VMRegImpl::stack_slot_size * count, 16);
3084 sub(stack, stack, total_push_bytes);
3085 for (int i = 0; i < count; i++) {
3086 sve_str(as_PRegister(regs[i]), Address(stack, i));
3087 }
3088 return total_push_bytes / 8;
3089 }
3090
3091 // Return the number of dwords popped
3092 int MacroAssembler::pop_p(unsigned int bitset, Register stack) {
3093 bool use_sve = false;
3094 int sve_predicate_size_in_slots = 0;
3095
3096 #ifdef COMPILER2
3097 use_sve = Matcher::supports_scalable_vector();
3098 if (use_sve) {
3099 sve_predicate_size_in_slots = Matcher::scalable_predicate_reg_slots();
3100 }
3101 #endif
3102
3103 if (!use_sve) {
3104 return 0;
3105 }
3106
3107 unsigned char regs[PRegister::number_of_registers];
3108 int count = 0;
3109 for (int reg = 0; reg < PRegister::number_of_registers; reg++) {
3110 if (1 & bitset)
3111 regs[count++] = reg;
3112 bitset >>= 1;
3113 }
3114
3115 if (count == 0) {
3116 return 0;
3117 }
3118
3119 int total_pop_bytes = align_up(sve_predicate_size_in_slots *
3120 VMRegImpl::stack_slot_size * count, 16);
3121 for (int i = count - 1; i >= 0; i--) {
3122 sve_ldr(as_PRegister(regs[i]), Address(stack, i));
3123 }
3124 add(stack, stack, total_pop_bytes);
3125 return total_pop_bytes / 8;
3126 }
3127
3128 #ifdef ASSERT
3129 void MacroAssembler::verify_heapbase(const char* msg) {
3130 #if 0
3131 assert (UseCompressedOops || UseCompressedClassPointers, "should be compressed");
3132 assert (Universe::heap() != nullptr, "java heap should be initialized");
3133 if (!UseCompressedOops || Universe::ptr_base() == nullptr) {
3134 // rheapbase is allocated as general register
3135 return;
3136 }
3137 if (CheckCompressedOops) {
3138 Label ok;
3139 push(1 << rscratch1->encoding(), sp); // cmpptr trashes rscratch1
3140 cmpptr(rheapbase, ExternalAddress(CompressedOops::base_addr()));
3141 br(Assembler::EQ, ok);
3142 stop(msg);
3143 bind(ok);
3144 pop(1 << rscratch1->encoding(), sp);
3145 }
3146 #endif
3147 }
3148 #endif
3149
3150 void MacroAssembler::resolve_jobject(Register value, Register tmp1, Register tmp2) {
3151 assert_different_registers(value, tmp1, tmp2);
3152 Label done, tagged, weak_tagged;
3153
3154 cbz(value, done); // Use null as-is.
3155 tst(value, JNIHandles::tag_mask); // Test for tag.
3156 br(Assembler::NE, tagged);
3157
3158 // Resolve local handle
3159 access_load_at(T_OBJECT, IN_NATIVE | AS_RAW, value, Address(value, 0), tmp1, tmp2);
3160 verify_oop(value);
3161 b(done);
3162
3163 bind(tagged);
3164 STATIC_ASSERT(JNIHandles::TypeTag::weak_global == 0b1);
3165 tbnz(value, 0, weak_tagged); // Test for weak tag.
3166
3167 // Resolve global handle
3168 access_load_at(T_OBJECT, IN_NATIVE, value, Address(value, -JNIHandles::TypeTag::global), tmp1, tmp2);
3169 verify_oop(value);
3170 b(done);
3171
3172 bind(weak_tagged);
3173 // Resolve jweak.
3174 access_load_at(T_OBJECT, IN_NATIVE | ON_PHANTOM_OOP_REF,
3175 value, Address(value, -JNIHandles::TypeTag::weak_global), tmp1, tmp2);
3176 verify_oop(value);
3177
3178 bind(done);
3179 }
3180
3181 void MacroAssembler::resolve_global_jobject(Register value, Register tmp1, Register tmp2) {
3182 assert_different_registers(value, tmp1, tmp2);
3183 Label done;
3184
3185 cbz(value, done); // Use null as-is.
3186
3187 #ifdef ASSERT
3188 {
3189 STATIC_ASSERT(JNIHandles::TypeTag::global == 0b10);
3190 Label valid_global_tag;
3191 tbnz(value, 1, valid_global_tag); // Test for global tag
3192 stop("non global jobject using resolve_global_jobject");
3193 bind(valid_global_tag);
3194 }
3195 #endif
3196
3197 // Resolve global handle
3198 access_load_at(T_OBJECT, IN_NATIVE, value, Address(value, -JNIHandles::TypeTag::global), tmp1, tmp2);
3199 verify_oop(value);
3200
3201 bind(done);
3202 }
3203
3204 void MacroAssembler::stop(const char* msg) {
3205 // Skip AOT caching C strings in scratch buffer.
3206 const char* str = (code_section()->scratch_emit()) ? msg : AOTCodeCache::add_C_string(msg);
3207 BLOCK_COMMENT(str);
3208 // load msg into r0 so we can access it from the signal handler
3209 // ExternalAddress enables saving and restoring via the code cache
3210 lea(c_rarg0, ExternalAddress((address) str));
3211 dcps1(0xdeae);
3212 }
3213
3214 void MacroAssembler::unimplemented(const char* what) {
3215 const char* buf = nullptr;
3216 {
3217 ResourceMark rm;
3218 stringStream ss;
3219 ss.print("unimplemented: %s", what);
3220 buf = code_string(ss.as_string());
3221 }
3222 stop(buf);
3223 }
3224
3225 void MacroAssembler::_assert_asm(Assembler::Condition cc, const char* msg) {
3226 #ifdef ASSERT
3227 Label OK;
3228 br(cc, OK);
3229 stop(msg);
3230 bind(OK);
3231 #endif
3232 }
3233
3234 // If a constant does not fit in an immediate field, generate some
3235 // number of MOV instructions and then perform the operation.
3236 void MacroAssembler::wrap_add_sub_imm_insn(Register Rd, Register Rn, uint64_t imm,
3237 add_sub_imm_insn insn1,
3238 add_sub_reg_insn insn2,
3239 bool is32) {
3240 assert(Rd != zr, "Rd = zr and not setting flags?");
3241 bool fits = operand_valid_for_add_sub_immediate(is32 ? (int32_t)imm : imm);
3242 if (fits) {
3243 (this->*insn1)(Rd, Rn, imm);
3244 } else {
3245 if (g_uabs(imm) < (1 << 24)) {
3246 (this->*insn1)(Rd, Rn, imm & -(1 << 12));
3247 (this->*insn1)(Rd, Rd, imm & ((1 << 12)-1));
3248 } else {
3249 assert_different_registers(Rd, Rn);
3250 mov(Rd, imm);
3251 (this->*insn2)(Rd, Rn, Rd, LSL, 0);
3252 }
3253 }
3254 }
3255
3256 // Separate vsn which sets the flags. Optimisations are more restricted
3257 // because we must set the flags correctly.
3258 void MacroAssembler::wrap_adds_subs_imm_insn(Register Rd, Register Rn, uint64_t imm,
3259 add_sub_imm_insn insn1,
3260 add_sub_reg_insn insn2,
3261 bool is32) {
3262 bool fits = operand_valid_for_add_sub_immediate(is32 ? (int32_t)imm : imm);
3263 if (fits) {
3264 (this->*insn1)(Rd, Rn, imm);
3265 } else {
3266 assert_different_registers(Rd, Rn);
3267 assert(Rd != zr, "overflow in immediate operand");
3268 mov(Rd, imm);
3269 (this->*insn2)(Rd, Rn, Rd, LSL, 0);
3270 }
3271 }
3272
3273
3274 void MacroAssembler::add(Register Rd, Register Rn, RegisterOrConstant increment) {
3275 if (increment.is_register()) {
3276 add(Rd, Rn, increment.as_register());
3277 } else {
3278 add(Rd, Rn, increment.as_constant());
3279 }
3280 }
3281
3282 void MacroAssembler::addw(Register Rd, Register Rn, RegisterOrConstant increment) {
3283 if (increment.is_register()) {
3284 addw(Rd, Rn, increment.as_register());
3285 } else {
3286 addw(Rd, Rn, increment.as_constant());
3287 }
3288 }
3289
3290 void MacroAssembler::sub(Register Rd, Register Rn, RegisterOrConstant decrement) {
3291 if (decrement.is_register()) {
3292 sub(Rd, Rn, decrement.as_register());
3293 } else {
3294 sub(Rd, Rn, decrement.as_constant());
3295 }
3296 }
3297
3298 void MacroAssembler::subw(Register Rd, Register Rn, RegisterOrConstant decrement) {
3299 if (decrement.is_register()) {
3300 subw(Rd, Rn, decrement.as_register());
3301 } else {
3302 subw(Rd, Rn, decrement.as_constant());
3303 }
3304 }
3305
3306 void MacroAssembler::reinit_heapbase()
3307 {
3308 if (UseCompressedOops) {
3309 if (Universe::is_fully_initialized()) {
3310 mov(rheapbase, CompressedOops::base());
3311 } else {
3312 lea(rheapbase, ExternalAddress(CompressedOops::base_addr()));
3313 ldr(rheapbase, Address(rheapbase));
3314 }
3315 }
3316 }
3317
3318 // A generic CAS; success or failure is in the EQ flag. A weak CAS
3319 // doesn't retry and may fail spuriously. If the oldval is wanted,
3320 // Pass a register for the result, otherwise pass noreg.
3321
3322 // Clobbers rscratch1
3323 void MacroAssembler::cmpxchg(Register addr, Register expected,
3324 Register new_val,
3325 enum operand_size size,
3326 bool acquire, bool release,
3327 bool weak,
3328 Register result) {
3329 if (result == noreg) result = rscratch1;
3330 BLOCK_COMMENT("cmpxchg {");
3331 if (UseLSE) {
3332 mov(result, expected);
3333 lse_cas(result, new_val, addr, size, acquire, release, /*not_pair*/ true);
3334 compare_eq(result, expected, size);
3335 #ifdef ASSERT
3336 // Poison rscratch1 which is written on !UseLSE branch
3337 mov(rscratch1, 0x1f1f1f1f1f1f1f1f);
3338 #endif
3339 } else {
3340 Label retry_load, done;
3341 prfm(Address(addr), PSTL1STRM);
3342 bind(retry_load);
3343 load_exclusive(result, addr, size, acquire);
3344 compare_eq(result, expected, size);
3345 br(Assembler::NE, done);
3346 store_exclusive(rscratch1, new_val, addr, size, release);
3347 if (weak) {
3348 cmpw(rscratch1, 0u); // If the store fails, return NE to our caller.
3349 } else {
3350 cbnzw(rscratch1, retry_load);
3351 }
3352 bind(done);
3353 }
3354 BLOCK_COMMENT("} cmpxchg");
3355 }
3356
3357 // A generic comparison. Only compares for equality, clobbers rscratch1.
3358 void MacroAssembler::compare_eq(Register rm, Register rn, enum operand_size size) {
3359 if (size == xword) {
3360 cmp(rm, rn);
3361 } else if (size == word) {
3362 cmpw(rm, rn);
3363 } else if (size == halfword) {
3364 eorw(rscratch1, rm, rn);
3365 ands(zr, rscratch1, 0xffff);
3366 } else if (size == byte) {
3367 eorw(rscratch1, rm, rn);
3368 ands(zr, rscratch1, 0xff);
3369 } else {
3370 ShouldNotReachHere();
3371 }
3372 }
3373
3374
3375 static bool different(Register a, RegisterOrConstant b, Register c) {
3376 if (b.is_constant())
3377 return a != c;
3378 else
3379 return a != b.as_register() && a != c && b.as_register() != c;
3380 }
3381
3382 #define ATOMIC_OP(NAME, LDXR, OP, IOP, AOP, STXR, sz) \
3383 void MacroAssembler::atomic_##NAME(Register prev, RegisterOrConstant incr, Register addr) { \
3384 if (UseLSE) { \
3385 prev = prev->is_valid() ? prev : zr; \
3386 if (incr.is_register()) { \
3387 AOP(sz, incr.as_register(), prev, addr); \
3388 } else { \
3389 mov(rscratch2, incr.as_constant()); \
3390 AOP(sz, rscratch2, prev, addr); \
3391 } \
3392 return; \
3393 } \
3394 Register result = rscratch2; \
3395 if (prev->is_valid()) \
3396 result = different(prev, incr, addr) ? prev : rscratch2; \
3397 \
3398 Label retry_load; \
3399 prfm(Address(addr), PSTL1STRM); \
3400 bind(retry_load); \
3401 LDXR(result, addr); \
3402 OP(rscratch1, result, incr); \
3403 STXR(rscratch2, rscratch1, addr); \
3404 cbnzw(rscratch2, retry_load); \
3405 if (prev->is_valid() && prev != result) { \
3406 IOP(prev, rscratch1, incr); \
3407 } \
3408 }
3409
3410 ATOMIC_OP(add, ldxr, add, sub, ldadd, stxr, Assembler::xword)
3411 ATOMIC_OP(addw, ldxrw, addw, subw, ldadd, stxrw, Assembler::word)
3412 ATOMIC_OP(addal, ldaxr, add, sub, ldaddal, stlxr, Assembler::xword)
3413 ATOMIC_OP(addalw, ldaxrw, addw, subw, ldaddal, stlxrw, Assembler::word)
3414
3415 #undef ATOMIC_OP
3416
3417 #define ATOMIC_XCHG(OP, AOP, LDXR, STXR, sz) \
3418 void MacroAssembler::atomic_##OP(Register prev, Register newv, Register addr) { \
3419 if (UseLSE) { \
3420 prev = prev->is_valid() ? prev : zr; \
3421 AOP(sz, newv, prev, addr); \
3422 return; \
3423 } \
3424 Register result = rscratch2; \
3425 if (prev->is_valid()) \
3426 result = different(prev, newv, addr) ? prev : rscratch2; \
3427 \
3428 Label retry_load; \
3429 prfm(Address(addr), PSTL1STRM); \
3430 bind(retry_load); \
3431 LDXR(result, addr); \
3432 STXR(rscratch1, newv, addr); \
3433 cbnzw(rscratch1, retry_load); \
3434 if (prev->is_valid() && prev != result) \
3435 mov(prev, result); \
3436 }
3437
3438 ATOMIC_XCHG(xchg, swp, ldxr, stxr, Assembler::xword)
3439 ATOMIC_XCHG(xchgw, swp, ldxrw, stxrw, Assembler::word)
3440 ATOMIC_XCHG(xchgl, swpl, ldxr, stlxr, Assembler::xword)
3441 ATOMIC_XCHG(xchglw, swpl, ldxrw, stlxrw, Assembler::word)
3442 ATOMIC_XCHG(xchgal, swpal, ldaxr, stlxr, Assembler::xword)
3443 ATOMIC_XCHG(xchgalw, swpal, ldaxrw, stlxrw, Assembler::word)
3444
3445 #undef ATOMIC_XCHG
3446
3447 #ifndef PRODUCT
3448 extern "C" void findpc(intptr_t x);
3449 #endif
3450
3451 void MacroAssembler::debug64(char* msg, int64_t pc, int64_t regs[])
3452 {
3453 // In order to get locks to work, we need to fake a in_VM state
3454 if (ShowMessageBoxOnError ) {
3455 JavaThread* thread = JavaThread::current();
3456 JavaThreadState saved_state = thread->thread_state();
3457 thread->set_thread_state(_thread_in_vm);
3458 #ifndef PRODUCT
3459 if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
3460 ttyLocker ttyl;
3461 BytecodeCounter::print();
3462 }
3463 #endif
3464 if (os::message_box(msg, "Execution stopped, print registers?")) {
3465 ttyLocker ttyl;
3466 tty->print_cr(" pc = 0x%016" PRIx64, pc);
3467 #ifndef PRODUCT
3468 tty->cr();
3469 findpc(pc);
3470 tty->cr();
3471 #endif
3472 tty->print_cr(" r0 = 0x%016" PRIx64, regs[0]);
3473 tty->print_cr(" r1 = 0x%016" PRIx64, regs[1]);
3474 tty->print_cr(" r2 = 0x%016" PRIx64, regs[2]);
3475 tty->print_cr(" r3 = 0x%016" PRIx64, regs[3]);
3476 tty->print_cr(" r4 = 0x%016" PRIx64, regs[4]);
3477 tty->print_cr(" r5 = 0x%016" PRIx64, regs[5]);
3478 tty->print_cr(" r6 = 0x%016" PRIx64, regs[6]);
3479 tty->print_cr(" r7 = 0x%016" PRIx64, regs[7]);
3480 tty->print_cr(" r8 = 0x%016" PRIx64, regs[8]);
3481 tty->print_cr(" r9 = 0x%016" PRIx64, regs[9]);
3482 tty->print_cr("r10 = 0x%016" PRIx64, regs[10]);
3483 tty->print_cr("r11 = 0x%016" PRIx64, regs[11]);
3484 tty->print_cr("r12 = 0x%016" PRIx64, regs[12]);
3485 tty->print_cr("r13 = 0x%016" PRIx64, regs[13]);
3486 tty->print_cr("r14 = 0x%016" PRIx64, regs[14]);
3487 tty->print_cr("r15 = 0x%016" PRIx64, regs[15]);
3488 tty->print_cr("r16 = 0x%016" PRIx64, regs[16]);
3489 tty->print_cr("r17 = 0x%016" PRIx64, regs[17]);
3490 tty->print_cr("r18 = 0x%016" PRIx64, regs[18]);
3491 tty->print_cr("r19 = 0x%016" PRIx64, regs[19]);
3492 tty->print_cr("r20 = 0x%016" PRIx64, regs[20]);
3493 tty->print_cr("r21 = 0x%016" PRIx64, regs[21]);
3494 tty->print_cr("r22 = 0x%016" PRIx64, regs[22]);
3495 tty->print_cr("r23 = 0x%016" PRIx64, regs[23]);
3496 tty->print_cr("r24 = 0x%016" PRIx64, regs[24]);
3497 tty->print_cr("r25 = 0x%016" PRIx64, regs[25]);
3498 tty->print_cr("r26 = 0x%016" PRIx64, regs[26]);
3499 tty->print_cr("r27 = 0x%016" PRIx64, regs[27]);
3500 tty->print_cr("r28 = 0x%016" PRIx64, regs[28]);
3501 tty->print_cr("r30 = 0x%016" PRIx64, regs[30]);
3502 tty->print_cr("r31 = 0x%016" PRIx64, regs[31]);
3503 BREAKPOINT;
3504 }
3505 }
3506 fatal("DEBUG MESSAGE: %s", msg);
3507 }
3508
3509 RegSet MacroAssembler::call_clobbered_gp_registers() {
3510 RegSet regs = RegSet::range(r0, r17) - RegSet::of(rscratch1, rscratch2);
3511 #ifndef R18_RESERVED
3512 regs += r18_tls;
3513 #endif
3514 return regs;
3515 }
3516
3517 void MacroAssembler::push_call_clobbered_registers_except(RegSet exclude) {
3518 int step = 4 * wordSize;
3519 push(call_clobbered_gp_registers() - exclude, sp);
3520 sub(sp, sp, step);
3521 mov(rscratch1, -step);
3522 // Push v0-v7, v16-v31.
3523 for (int i = 31; i>= 4; i -= 4) {
3524 if (i <= v7->encoding() || i >= v16->encoding())
3525 st1(as_FloatRegister(i-3), as_FloatRegister(i-2), as_FloatRegister(i-1),
3526 as_FloatRegister(i), T1D, Address(post(sp, rscratch1)));
3527 }
3528 st1(as_FloatRegister(0), as_FloatRegister(1), as_FloatRegister(2),
3529 as_FloatRegister(3), T1D, Address(sp));
3530 }
3531
3532 void MacroAssembler::pop_call_clobbered_registers_except(RegSet exclude) {
3533 for (int i = 0; i < 32; i += 4) {
3534 if (i <= v7->encoding() || i >= v16->encoding())
3535 ld1(as_FloatRegister(i), as_FloatRegister(i+1), as_FloatRegister(i+2),
3536 as_FloatRegister(i+3), T1D, Address(post(sp, 4 * wordSize)));
3537 }
3538
3539 reinitialize_ptrue();
3540
3541 pop(call_clobbered_gp_registers() - exclude, sp);
3542 }
3543
3544 void MacroAssembler::push_CPU_state(bool save_vectors, bool use_sve,
3545 int sve_vector_size_in_bytes, int total_predicate_in_bytes) {
3546 push(RegSet::range(r0, r29), sp); // integer registers except lr & sp
3547 if (save_vectors && use_sve && sve_vector_size_in_bytes > 16) {
3548 sub(sp, sp, sve_vector_size_in_bytes * FloatRegister::number_of_registers);
3549 for (int i = 0; i < FloatRegister::number_of_registers; i++) {
3550 sve_str(as_FloatRegister(i), Address(sp, i));
3551 }
3552 } else {
3553 int step = (save_vectors ? 8 : 4) * wordSize;
3554 mov(rscratch1, -step);
3555 sub(sp, sp, step);
3556 for (int i = 28; i >= 4; i -= 4) {
3557 st1(as_FloatRegister(i), as_FloatRegister(i+1), as_FloatRegister(i+2),
3558 as_FloatRegister(i+3), save_vectors ? T2D : T1D, Address(post(sp, rscratch1)));
3559 }
3560 st1(v0, v1, v2, v3, save_vectors ? T2D : T1D, sp);
3561 }
3562 if (save_vectors && use_sve && total_predicate_in_bytes > 0) {
3563 sub(sp, sp, total_predicate_in_bytes);
3564 for (int i = 0; i < PRegister::number_of_registers; i++) {
3565 sve_str(as_PRegister(i), Address(sp, i));
3566 }
3567 }
3568 }
3569
3570 void MacroAssembler::pop_CPU_state(bool restore_vectors, bool use_sve,
3571 int sve_vector_size_in_bytes, int total_predicate_in_bytes) {
3572 if (restore_vectors && use_sve && total_predicate_in_bytes > 0) {
3573 for (int i = PRegister::number_of_registers - 1; i >= 0; i--) {
3574 sve_ldr(as_PRegister(i), Address(sp, i));
3575 }
3576 add(sp, sp, total_predicate_in_bytes);
3577 }
3578 if (restore_vectors && use_sve && sve_vector_size_in_bytes > 16) {
3579 for (int i = FloatRegister::number_of_registers - 1; i >= 0; i--) {
3580 sve_ldr(as_FloatRegister(i), Address(sp, i));
3581 }
3582 add(sp, sp, sve_vector_size_in_bytes * FloatRegister::number_of_registers);
3583 } else {
3584 int step = (restore_vectors ? 8 : 4) * wordSize;
3585 for (int i = 0; i <= 28; i += 4)
3586 ld1(as_FloatRegister(i), as_FloatRegister(i+1), as_FloatRegister(i+2),
3587 as_FloatRegister(i+3), restore_vectors ? T2D : T1D, Address(post(sp, step)));
3588 }
3589
3590 // We may use predicate registers and rely on ptrue with SVE,
3591 // regardless of wide vector (> 8 bytes) used or not.
3592 if (use_sve) {
3593 reinitialize_ptrue();
3594 }
3595
3596 // integer registers except lr & sp
3597 pop(RegSet::range(r0, r17), sp);
3598 #ifdef R18_RESERVED
3599 ldp(zr, r19, Address(post(sp, 2 * wordSize)));
3600 pop(RegSet::range(r20, r29), sp);
3601 #else
3602 pop(RegSet::range(r18_tls, r29), sp);
3603 #endif
3604 }
3605
3606 /**
3607 * Helpers for multiply_to_len().
3608 */
3609 void MacroAssembler::add2_with_carry(Register final_dest_hi, Register dest_hi, Register dest_lo,
3610 Register src1, Register src2) {
3611 adds(dest_lo, dest_lo, src1);
3612 adc(dest_hi, dest_hi, zr);
3613 adds(dest_lo, dest_lo, src2);
3614 adc(final_dest_hi, dest_hi, zr);
3615 }
3616
3617 // Generate an address from (r + r1 extend offset). "size" is the
3618 // size of the operand. The result may be in rscratch2.
3619 Address MacroAssembler::offsetted_address(Register r, Register r1,
3620 Address::extend ext, int offset, int size) {
3621 if (offset || (ext.shift() % size != 0)) {
3622 lea(rscratch2, Address(r, r1, ext));
3623 return Address(rscratch2, offset);
3624 } else {
3625 return Address(r, r1, ext);
3626 }
3627 }
3628
3629 Address MacroAssembler::spill_address(int size, int offset, Register tmp)
3630 {
3631 assert(offset >= 0, "spill to negative address?");
3632 // Offset reachable ?
3633 // Not aligned - 9 bits signed offset
3634 // Aligned - 12 bits unsigned offset shifted
3635 Register base = sp;
3636 if ((offset & (size-1)) && offset >= (1<<8)) {
3637 add(tmp, base, offset & ((1<<12)-1));
3638 base = tmp;
3639 offset &= -1u<<12;
3640 }
3641
3642 if (offset >= (1<<12) * size) {
3643 add(tmp, base, offset & (((1<<12)-1)<<12));
3644 base = tmp;
3645 offset &= ~(((1<<12)-1)<<12);
3646 }
3647
3648 return Address(base, offset);
3649 }
3650
3651 Address MacroAssembler::sve_spill_address(int sve_reg_size_in_bytes, int offset, Register tmp) {
3652 assert(offset >= 0, "spill to negative address?");
3653
3654 Register base = sp;
3655
3656 // An immediate offset in the range 0 to 255 which is multiplied
3657 // by the current vector or predicate register size in bytes.
3658 if (offset % sve_reg_size_in_bytes == 0 && offset < ((1<<8)*sve_reg_size_in_bytes)) {
3659 return Address(base, offset / sve_reg_size_in_bytes);
3660 }
3661
3662 add(tmp, base, offset);
3663 return Address(tmp);
3664 }
3665
3666 // Checks whether offset is aligned.
3667 // Returns true if it is, else false.
3668 bool MacroAssembler::merge_alignment_check(Register base,
3669 size_t size,
3670 int64_t cur_offset,
3671 int64_t prev_offset) const {
3672 if (AvoidUnalignedAccesses) {
3673 if (base == sp) {
3674 // Checks whether low offset if aligned to pair of registers.
3675 int64_t pair_mask = size * 2 - 1;
3676 int64_t offset = prev_offset > cur_offset ? cur_offset : prev_offset;
3677 return (offset & pair_mask) == 0;
3678 } else { // If base is not sp, we can't guarantee the access is aligned.
3679 return false;
3680 }
3681 } else {
3682 int64_t mask = size - 1;
3683 // Load/store pair instruction only supports element size aligned offset.
3684 return (cur_offset & mask) == 0 && (prev_offset & mask) == 0;
3685 }
3686 }
3687
3688 // Checks whether current and previous loads/stores can be merged.
3689 // Returns true if it can be merged, else false.
3690 bool MacroAssembler::ldst_can_merge(Register rt,
3691 const Address &adr,
3692 size_t cur_size_in_bytes,
3693 bool is_store) const {
3694 address prev = pc() - NativeInstruction::instruction_size;
3695 address last = code()->last_insn();
3696
3697 if (last == nullptr || !nativeInstruction_at(last)->is_Imm_LdSt()) {
3698 return false;
3699 }
3700
3701 if (adr.getMode() != Address::base_plus_offset || prev != last) {
3702 return false;
3703 }
3704
3705 NativeLdSt* prev_ldst = NativeLdSt_at(prev);
3706 size_t prev_size_in_bytes = prev_ldst->size_in_bytes();
3707
3708 assert(prev_size_in_bytes == 4 || prev_size_in_bytes == 8, "only supports 64/32bit merging.");
3709 assert(cur_size_in_bytes == 4 || cur_size_in_bytes == 8, "only supports 64/32bit merging.");
3710
3711 if (cur_size_in_bytes != prev_size_in_bytes || is_store != prev_ldst->is_store()) {
3712 return false;
3713 }
3714
3715 int64_t max_offset = 63 * prev_size_in_bytes;
3716 int64_t min_offset = -64 * prev_size_in_bytes;
3717
3718 assert(prev_ldst->is_not_pre_post_index(), "pre-index or post-index is not supported to be merged.");
3719
3720 // Only same base can be merged.
3721 if (adr.base() != prev_ldst->base()) {
3722 return false;
3723 }
3724
3725 int64_t cur_offset = adr.offset();
3726 int64_t prev_offset = prev_ldst->offset();
3727 size_t diff = abs(cur_offset - prev_offset);
3728 if (diff != prev_size_in_bytes) {
3729 return false;
3730 }
3731
3732 // Following cases can not be merged:
3733 // ldr x2, [x2, #8]
3734 // ldr x3, [x2, #16]
3735 // or:
3736 // ldr x2, [x3, #8]
3737 // ldr x2, [x3, #16]
3738 // If t1 and t2 is the same in "ldp t1, t2, [xn, #imm]", we'll get SIGILL.
3739 if (!is_store && (adr.base() == prev_ldst->target() || rt == prev_ldst->target())) {
3740 return false;
3741 }
3742
3743 int64_t low_offset = prev_offset > cur_offset ? cur_offset : prev_offset;
3744 // Offset range must be in ldp/stp instruction's range.
3745 if (low_offset > max_offset || low_offset < min_offset) {
3746 return false;
3747 }
3748
3749 if (merge_alignment_check(adr.base(), prev_size_in_bytes, cur_offset, prev_offset)) {
3750 return true;
3751 }
3752
3753 return false;
3754 }
3755
3756 // Merge current load/store with previous load/store into ldp/stp.
3757 void MacroAssembler::merge_ldst(Register rt,
3758 const Address &adr,
3759 size_t cur_size_in_bytes,
3760 bool is_store) {
3761
3762 assert(ldst_can_merge(rt, adr, cur_size_in_bytes, is_store) == true, "cur and prev must be able to be merged.");
3763
3764 Register rt_low, rt_high;
3765 address prev = pc() - NativeInstruction::instruction_size;
3766 NativeLdSt* prev_ldst = NativeLdSt_at(prev);
3767
3768 int64_t offset;
3769
3770 if (adr.offset() < prev_ldst->offset()) {
3771 offset = adr.offset();
3772 rt_low = rt;
3773 rt_high = prev_ldst->target();
3774 } else {
3775 offset = prev_ldst->offset();
3776 rt_low = prev_ldst->target();
3777 rt_high = rt;
3778 }
3779
3780 Address adr_p = Address(prev_ldst->base(), offset);
3781 // Overwrite previous generated binary.
3782 code_section()->set_end(prev);
3783
3784 const size_t sz = prev_ldst->size_in_bytes();
3785 assert(sz == 8 || sz == 4, "only supports 64/32bit merging.");
3786 if (!is_store) {
3787 BLOCK_COMMENT("merged ldr pair");
3788 if (sz == 8) {
3789 ldp(rt_low, rt_high, adr_p);
3790 } else {
3791 ldpw(rt_low, rt_high, adr_p);
3792 }
3793 } else {
3794 BLOCK_COMMENT("merged str pair");
3795 if (sz == 8) {
3796 stp(rt_low, rt_high, adr_p);
3797 } else {
3798 stpw(rt_low, rt_high, adr_p);
3799 }
3800 }
3801 }
3802
3803 /**
3804 * Multiply 64 bit by 64 bit first loop.
3805 */
3806 void MacroAssembler::multiply_64_x_64_loop(Register x, Register xstart, Register x_xstart,
3807 Register y, Register y_idx, Register z,
3808 Register carry, Register product,
3809 Register idx, Register kdx) {
3810 //
3811 // jlong carry, x[], y[], z[];
3812 // for (int idx=ystart, kdx=ystart+1+xstart; idx >= 0; idx-, kdx--) {
3813 // huge_128 product = y[idx] * x[xstart] + carry;
3814 // z[kdx] = (jlong)product;
3815 // carry = (jlong)(product >>> 64);
3816 // }
3817 // z[xstart] = carry;
3818 //
3819
3820 Label L_first_loop, L_first_loop_exit;
3821 Label L_one_x, L_one_y, L_multiply;
3822
3823 subsw(xstart, xstart, 1);
3824 br(Assembler::MI, L_one_x);
3825
3826 lea(rscratch1, Address(x, xstart, Address::lsl(LogBytesPerInt)));
3827 ldr(x_xstart, Address(rscratch1));
3828 ror(x_xstart, x_xstart, 32); // convert big-endian to little-endian
3829
3830 bind(L_first_loop);
3831 subsw(idx, idx, 1);
3832 br(Assembler::MI, L_first_loop_exit);
3833 subsw(idx, idx, 1);
3834 br(Assembler::MI, L_one_y);
3835 lea(rscratch1, Address(y, idx, Address::uxtw(LogBytesPerInt)));
3836 ldr(y_idx, Address(rscratch1));
3837 ror(y_idx, y_idx, 32); // convert big-endian to little-endian
3838 bind(L_multiply);
3839
3840 // AArch64 has a multiply-accumulate instruction that we can't use
3841 // here because it has no way to process carries, so we have to use
3842 // separate add and adc instructions. Bah.
3843 umulh(rscratch1, x_xstart, y_idx); // x_xstart * y_idx -> rscratch1:product
3844 mul(product, x_xstart, y_idx);
3845 adds(product, product, carry);
3846 adc(carry, rscratch1, zr); // x_xstart * y_idx + carry -> carry:product
3847
3848 subw(kdx, kdx, 2);
3849 ror(product, product, 32); // back to big-endian
3850 str(product, offsetted_address(z, kdx, Address::uxtw(LogBytesPerInt), 0, BytesPerLong));
3851
3852 b(L_first_loop);
3853
3854 bind(L_one_y);
3855 ldrw(y_idx, Address(y, 0));
3856 b(L_multiply);
3857
3858 bind(L_one_x);
3859 ldrw(x_xstart, Address(x, 0));
3860 b(L_first_loop);
3861
3862 bind(L_first_loop_exit);
3863 }
3864
3865 /**
3866 * Multiply 128 bit by 128. Unrolled inner loop.
3867 *
3868 */
3869 void MacroAssembler::multiply_128_x_128_loop(Register y, Register z,
3870 Register carry, Register carry2,
3871 Register idx, Register jdx,
3872 Register yz_idx1, Register yz_idx2,
3873 Register tmp, Register tmp3, Register tmp4,
3874 Register tmp6, Register product_hi) {
3875
3876 // jlong carry, x[], y[], z[];
3877 // int kdx = ystart+1;
3878 // for (int idx=ystart-2; idx >= 0; idx -= 2) { // Third loop
3879 // huge_128 tmp3 = (y[idx+1] * product_hi) + z[kdx+idx+1] + carry;
3880 // jlong carry2 = (jlong)(tmp3 >>> 64);
3881 // huge_128 tmp4 = (y[idx] * product_hi) + z[kdx+idx] + carry2;
3882 // carry = (jlong)(tmp4 >>> 64);
3883 // z[kdx+idx+1] = (jlong)tmp3;
3884 // z[kdx+idx] = (jlong)tmp4;
3885 // }
3886 // idx += 2;
3887 // if (idx > 0) {
3888 // yz_idx1 = (y[idx] * product_hi) + z[kdx+idx] + carry;
3889 // z[kdx+idx] = (jlong)yz_idx1;
3890 // carry = (jlong)(yz_idx1 >>> 64);
3891 // }
3892 //
3893
3894 Label L_third_loop, L_third_loop_exit, L_post_third_loop_done;
3895
3896 lsrw(jdx, idx, 2);
3897
3898 bind(L_third_loop);
3899
3900 subsw(jdx, jdx, 1);
3901 br(Assembler::MI, L_third_loop_exit);
3902 subw(idx, idx, 4);
3903
3904 lea(rscratch1, Address(y, idx, Address::uxtw(LogBytesPerInt)));
3905
3906 ldp(yz_idx2, yz_idx1, Address(rscratch1, 0));
3907
3908 lea(tmp6, Address(z, idx, Address::uxtw(LogBytesPerInt)));
3909
3910 ror(yz_idx1, yz_idx1, 32); // convert big-endian to little-endian
3911 ror(yz_idx2, yz_idx2, 32);
3912
3913 ldp(rscratch2, rscratch1, Address(tmp6, 0));
3914
3915 mul(tmp3, product_hi, yz_idx1); // yz_idx1 * product_hi -> tmp4:tmp3
3916 umulh(tmp4, product_hi, yz_idx1);
3917
3918 ror(rscratch1, rscratch1, 32); // convert big-endian to little-endian
3919 ror(rscratch2, rscratch2, 32);
3920
3921 mul(tmp, product_hi, yz_idx2); // yz_idx2 * product_hi -> carry2:tmp
3922 umulh(carry2, product_hi, yz_idx2);
3923
3924 // propagate sum of both multiplications into carry:tmp4:tmp3
3925 adds(tmp3, tmp3, carry);
3926 adc(tmp4, tmp4, zr);
3927 adds(tmp3, tmp3, rscratch1);
3928 adcs(tmp4, tmp4, tmp);
3929 adc(carry, carry2, zr);
3930 adds(tmp4, tmp4, rscratch2);
3931 adc(carry, carry, zr);
3932
3933 ror(tmp3, tmp3, 32); // convert little-endian to big-endian
3934 ror(tmp4, tmp4, 32);
3935 stp(tmp4, tmp3, Address(tmp6, 0));
3936
3937 b(L_third_loop);
3938 bind (L_third_loop_exit);
3939
3940 andw (idx, idx, 0x3);
3941 cbz(idx, L_post_third_loop_done);
3942
3943 Label L_check_1;
3944 subsw(idx, idx, 2);
3945 br(Assembler::MI, L_check_1);
3946
3947 lea(rscratch1, Address(y, idx, Address::uxtw(LogBytesPerInt)));
3948 ldr(yz_idx1, Address(rscratch1, 0));
3949 ror(yz_idx1, yz_idx1, 32);
3950 mul(tmp3, product_hi, yz_idx1); // yz_idx1 * product_hi -> tmp4:tmp3
3951 umulh(tmp4, product_hi, yz_idx1);
3952 lea(rscratch1, Address(z, idx, Address::uxtw(LogBytesPerInt)));
3953 ldr(yz_idx2, Address(rscratch1, 0));
3954 ror(yz_idx2, yz_idx2, 32);
3955
3956 add2_with_carry(carry, tmp4, tmp3, carry, yz_idx2);
3957
3958 ror(tmp3, tmp3, 32);
3959 str(tmp3, Address(rscratch1, 0));
3960
3961 bind (L_check_1);
3962
3963 andw (idx, idx, 0x1);
3964 subsw(idx, idx, 1);
3965 br(Assembler::MI, L_post_third_loop_done);
3966 ldrw(tmp4, Address(y, idx, Address::uxtw(LogBytesPerInt)));
3967 mul(tmp3, tmp4, product_hi); // tmp4 * product_hi -> carry2:tmp3
3968 umulh(carry2, tmp4, product_hi);
3969 ldrw(tmp4, Address(z, idx, Address::uxtw(LogBytesPerInt)));
3970
3971 add2_with_carry(carry2, tmp3, tmp4, carry);
3972
3973 strw(tmp3, Address(z, idx, Address::uxtw(LogBytesPerInt)));
3974 extr(carry, carry2, tmp3, 32);
3975
3976 bind(L_post_third_loop_done);
3977 }
3978
3979 /**
3980 * Code for BigInteger::multiplyToLen() intrinsic.
3981 *
3982 * r0: x
3983 * r1: xlen
3984 * r2: y
3985 * r3: ylen
3986 * r4: z
3987 * r5: tmp0
3988 * r10: tmp1
3989 * r11: tmp2
3990 * r12: tmp3
3991 * r13: tmp4
3992 * r14: tmp5
3993 * r15: tmp6
3994 * r16: tmp7
3995 *
3996 */
3997 void MacroAssembler::multiply_to_len(Register x, Register xlen, Register y, Register ylen,
3998 Register z, Register tmp0,
3999 Register tmp1, Register tmp2, Register tmp3, Register tmp4,
4000 Register tmp5, Register tmp6, Register product_hi) {
4001
4002 assert_different_registers(x, xlen, y, ylen, z, tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, product_hi);
4003
4004 const Register idx = tmp1;
4005 const Register kdx = tmp2;
4006 const Register xstart = tmp3;
4007
4008 const Register y_idx = tmp4;
4009 const Register carry = tmp5;
4010 const Register product = xlen;
4011 const Register x_xstart = tmp0;
4012
4013 // First Loop.
4014 //
4015 // final static long LONG_MASK = 0xffffffffL;
4016 // int xstart = xlen - 1;
4017 // int ystart = ylen - 1;
4018 // long carry = 0;
4019 // for (int idx=ystart, kdx=ystart+1+xstart; idx >= 0; idx-, kdx--) {
4020 // long product = (y[idx] & LONG_MASK) * (x[xstart] & LONG_MASK) + carry;
4021 // z[kdx] = (int)product;
4022 // carry = product >>> 32;
4023 // }
4024 // z[xstart] = (int)carry;
4025 //
4026
4027 movw(idx, ylen); // idx = ylen;
4028 addw(kdx, xlen, ylen); // kdx = xlen+ylen;
4029 mov(carry, zr); // carry = 0;
4030
4031 Label L_done;
4032
4033 movw(xstart, xlen);
4034 subsw(xstart, xstart, 1);
4035 br(Assembler::MI, L_done);
4036
4037 multiply_64_x_64_loop(x, xstart, x_xstart, y, y_idx, z, carry, product, idx, kdx);
4038
4039 Label L_second_loop;
4040 cbzw(kdx, L_second_loop);
4041
4042 Label L_carry;
4043 subw(kdx, kdx, 1);
4044 cbzw(kdx, L_carry);
4045
4046 strw(carry, Address(z, kdx, Address::uxtw(LogBytesPerInt)));
4047 lsr(carry, carry, 32);
4048 subw(kdx, kdx, 1);
4049
4050 bind(L_carry);
4051 strw(carry, Address(z, kdx, Address::uxtw(LogBytesPerInt)));
4052
4053 // Second and third (nested) loops.
4054 //
4055 // for (int i = xstart-1; i >= 0; i--) { // Second loop
4056 // carry = 0;
4057 // for (int jdx=ystart, k=ystart+1+i; jdx >= 0; jdx--, k--) { // Third loop
4058 // long product = (y[jdx] & LONG_MASK) * (x[i] & LONG_MASK) +
4059 // (z[k] & LONG_MASK) + carry;
4060 // z[k] = (int)product;
4061 // carry = product >>> 32;
4062 // }
4063 // z[i] = (int)carry;
4064 // }
4065 //
4066 // i = xlen, j = tmp1, k = tmp2, carry = tmp5, x[i] = product_hi
4067
4068 const Register jdx = tmp1;
4069
4070 bind(L_second_loop);
4071 mov(carry, zr); // carry = 0;
4072 movw(jdx, ylen); // j = ystart+1
4073
4074 subsw(xstart, xstart, 1); // i = xstart-1;
4075 br(Assembler::MI, L_done);
4076
4077 str(z, Address(pre(sp, -4 * wordSize)));
4078
4079 Label L_last_x;
4080 lea(z, offsetted_address(z, xstart, Address::uxtw(LogBytesPerInt), 4, BytesPerInt)); // z = z + k - j
4081 subsw(xstart, xstart, 1); // i = xstart-1;
4082 br(Assembler::MI, L_last_x);
4083
4084 lea(rscratch1, Address(x, xstart, Address::uxtw(LogBytesPerInt)));
4085 ldr(product_hi, Address(rscratch1));
4086 ror(product_hi, product_hi, 32); // convert big-endian to little-endian
4087
4088 Label L_third_loop_prologue;
4089 bind(L_third_loop_prologue);
4090
4091 str(ylen, Address(sp, wordSize));
4092 stp(x, xstart, Address(sp, 2 * wordSize));
4093 multiply_128_x_128_loop(y, z, carry, x, jdx, ylen, product,
4094 tmp2, x_xstart, tmp3, tmp4, tmp6, product_hi);
4095 ldp(z, ylen, Address(post(sp, 2 * wordSize)));
4096 ldp(x, xlen, Address(post(sp, 2 * wordSize))); // copy old xstart -> xlen
4097
4098 addw(tmp3, xlen, 1);
4099 strw(carry, Address(z, tmp3, Address::uxtw(LogBytesPerInt)));
4100 subsw(tmp3, tmp3, 1);
4101 br(Assembler::MI, L_done);
4102
4103 lsr(carry, carry, 32);
4104 strw(carry, Address(z, tmp3, Address::uxtw(LogBytesPerInt)));
4105 b(L_second_loop);
4106
4107 // Next infrequent code is moved outside loops.
4108 bind(L_last_x);
4109 ldrw(product_hi, Address(x, 0));
4110 b(L_third_loop_prologue);
4111
4112 bind(L_done);
4113 }
4114
4115 // Code for BigInteger::mulAdd intrinsic
4116 // out = r0
4117 // in = r1
4118 // offset = r2 (already out.length-offset)
4119 // len = r3
4120 // k = r4
4121 //
4122 // pseudo code from java implementation:
4123 // carry = 0;
4124 // offset = out.length-offset - 1;
4125 // for (int j=len-1; j >= 0; j--) {
4126 // product = (in[j] & LONG_MASK) * kLong + (out[offset] & LONG_MASK) + carry;
4127 // out[offset--] = (int)product;
4128 // carry = product >>> 32;
4129 // }
4130 // return (int)carry;
4131 void MacroAssembler::mul_add(Register out, Register in, Register offset,
4132 Register len, Register k) {
4133 Label LOOP, END;
4134 // pre-loop
4135 cmp(len, zr); // cmp, not cbz/cbnz: to use condition twice => less branches
4136 csel(out, zr, out, Assembler::EQ);
4137 br(Assembler::EQ, END);
4138 add(in, in, len, LSL, 2); // in[j+1] address
4139 add(offset, out, offset, LSL, 2); // out[offset + 1] address
4140 mov(out, zr); // used to keep carry now
4141 BIND(LOOP);
4142 ldrw(rscratch1, Address(pre(in, -4)));
4143 madd(rscratch1, rscratch1, k, out);
4144 ldrw(rscratch2, Address(pre(offset, -4)));
4145 add(rscratch1, rscratch1, rscratch2);
4146 strw(rscratch1, Address(offset));
4147 lsr(out, rscratch1, 32);
4148 subs(len, len, 1);
4149 br(Assembler::NE, LOOP);
4150 BIND(END);
4151 }
4152
4153 /**
4154 * Emits code to update CRC-32 with a byte value according to constants in table
4155 *
4156 * @param [in,out]crc Register containing the crc.
4157 * @param [in]val Register containing the byte to fold into the CRC.
4158 * @param [in]table Register containing the table of crc constants.
4159 *
4160 * uint32_t crc;
4161 * val = crc_table[(val ^ crc) & 0xFF];
4162 * crc = val ^ (crc >> 8);
4163 *
4164 */
4165 void MacroAssembler::update_byte_crc32(Register crc, Register val, Register table) {
4166 eor(val, val, crc);
4167 andr(val, val, 0xff);
4168 ldrw(val, Address(table, val, Address::lsl(2)));
4169 eor(crc, val, crc, Assembler::LSR, 8);
4170 }
4171
4172 /**
4173 * Emits code to update CRC-32 with a 32-bit value according to tables 0 to 3
4174 *
4175 * @param [in,out]crc Register containing the crc.
4176 * @param [in]v Register containing the 32-bit to fold into the CRC.
4177 * @param [in]table0 Register containing table 0 of crc constants.
4178 * @param [in]table1 Register containing table 1 of crc constants.
4179 * @param [in]table2 Register containing table 2 of crc constants.
4180 * @param [in]table3 Register containing table 3 of crc constants.
4181 *
4182 * uint32_t crc;
4183 * v = crc ^ v
4184 * crc = table3[v&0xff]^table2[(v>>8)&0xff]^table1[(v>>16)&0xff]^table0[v>>24]
4185 *
4186 */
4187 void MacroAssembler::update_word_crc32(Register crc, Register v, Register tmp,
4188 Register table0, Register table1, Register table2, Register table3,
4189 bool upper) {
4190 eor(v, crc, v, upper ? LSR:LSL, upper ? 32:0);
4191 uxtb(tmp, v);
4192 ldrw(crc, Address(table3, tmp, Address::lsl(2)));
4193 ubfx(tmp, v, 8, 8);
4194 ldrw(tmp, Address(table2, tmp, Address::lsl(2)));
4195 eor(crc, crc, tmp);
4196 ubfx(tmp, v, 16, 8);
4197 ldrw(tmp, Address(table1, tmp, Address::lsl(2)));
4198 eor(crc, crc, tmp);
4199 ubfx(tmp, v, 24, 8);
4200 ldrw(tmp, Address(table0, tmp, Address::lsl(2)));
4201 eor(crc, crc, tmp);
4202 }
4203
4204 void MacroAssembler::kernel_crc32_using_crypto_pmull(Register crc, Register buf,
4205 Register len, Register tmp0, Register tmp1, Register tmp2, Register tmp3) {
4206 Label CRC_by4_loop, CRC_by1_loop, CRC_less128, CRC_by128_pre, CRC_by32_loop, CRC_less32, L_exit;
4207 assert_different_registers(crc, buf, len, tmp0, tmp1, tmp2);
4208
4209 subs(tmp0, len, 384);
4210 mvnw(crc, crc);
4211 br(Assembler::GE, CRC_by128_pre);
4212 BIND(CRC_less128);
4213 subs(len, len, 32);
4214 br(Assembler::GE, CRC_by32_loop);
4215 BIND(CRC_less32);
4216 adds(len, len, 32 - 4);
4217 br(Assembler::GE, CRC_by4_loop);
4218 adds(len, len, 4);
4219 br(Assembler::GT, CRC_by1_loop);
4220 b(L_exit);
4221
4222 BIND(CRC_by32_loop);
4223 ldp(tmp0, tmp1, Address(buf));
4224 crc32x(crc, crc, tmp0);
4225 ldp(tmp2, tmp3, Address(buf, 16));
4226 crc32x(crc, crc, tmp1);
4227 add(buf, buf, 32);
4228 crc32x(crc, crc, tmp2);
4229 subs(len, len, 32);
4230 crc32x(crc, crc, tmp3);
4231 br(Assembler::GE, CRC_by32_loop);
4232 cmn(len, (u1)32);
4233 br(Assembler::NE, CRC_less32);
4234 b(L_exit);
4235
4236 BIND(CRC_by4_loop);
4237 ldrw(tmp0, Address(post(buf, 4)));
4238 subs(len, len, 4);
4239 crc32w(crc, crc, tmp0);
4240 br(Assembler::GE, CRC_by4_loop);
4241 adds(len, len, 4);
4242 br(Assembler::LE, L_exit);
4243 BIND(CRC_by1_loop);
4244 ldrb(tmp0, Address(post(buf, 1)));
4245 subs(len, len, 1);
4246 crc32b(crc, crc, tmp0);
4247 br(Assembler::GT, CRC_by1_loop);
4248 b(L_exit);
4249
4250 BIND(CRC_by128_pre);
4251 kernel_crc32_common_fold_using_crypto_pmull(crc, buf, len, tmp0, tmp1, tmp2,
4252 4*256*sizeof(juint) + 8*sizeof(juint));
4253 mov(crc, 0);
4254 crc32x(crc, crc, tmp0);
4255 crc32x(crc, crc, tmp1);
4256
4257 cbnz(len, CRC_less128);
4258
4259 BIND(L_exit);
4260 mvnw(crc, crc);
4261 }
4262
4263 void MacroAssembler::kernel_crc32_using_crc32(Register crc, Register buf,
4264 Register len, Register tmp0, Register tmp1, Register tmp2,
4265 Register tmp3) {
4266 Label CRC_by64_loop, CRC_by4_loop, CRC_by1_loop, CRC_less64, CRC_by64_pre, CRC_by32_loop, CRC_less32, L_exit;
4267 assert_different_registers(crc, buf, len, tmp0, tmp1, tmp2, tmp3);
4268
4269 mvnw(crc, crc);
4270
4271 subs(len, len, 128);
4272 br(Assembler::GE, CRC_by64_pre);
4273 BIND(CRC_less64);
4274 adds(len, len, 128-32);
4275 br(Assembler::GE, CRC_by32_loop);
4276 BIND(CRC_less32);
4277 adds(len, len, 32-4);
4278 br(Assembler::GE, CRC_by4_loop);
4279 adds(len, len, 4);
4280 br(Assembler::GT, CRC_by1_loop);
4281 b(L_exit);
4282
4283 BIND(CRC_by32_loop);
4284 ldp(tmp0, tmp1, Address(post(buf, 16)));
4285 subs(len, len, 32);
4286 crc32x(crc, crc, tmp0);
4287 ldr(tmp2, Address(post(buf, 8)));
4288 crc32x(crc, crc, tmp1);
4289 ldr(tmp3, Address(post(buf, 8)));
4290 crc32x(crc, crc, tmp2);
4291 crc32x(crc, crc, tmp3);
4292 br(Assembler::GE, CRC_by32_loop);
4293 cmn(len, (u1)32);
4294 br(Assembler::NE, CRC_less32);
4295 b(L_exit);
4296
4297 BIND(CRC_by4_loop);
4298 ldrw(tmp0, Address(post(buf, 4)));
4299 subs(len, len, 4);
4300 crc32w(crc, crc, tmp0);
4301 br(Assembler::GE, CRC_by4_loop);
4302 adds(len, len, 4);
4303 br(Assembler::LE, L_exit);
4304 BIND(CRC_by1_loop);
4305 ldrb(tmp0, Address(post(buf, 1)));
4306 subs(len, len, 1);
4307 crc32b(crc, crc, tmp0);
4308 br(Assembler::GT, CRC_by1_loop);
4309 b(L_exit);
4310
4311 BIND(CRC_by64_pre);
4312 sub(buf, buf, 8);
4313 ldp(tmp0, tmp1, Address(buf, 8));
4314 crc32x(crc, crc, tmp0);
4315 ldr(tmp2, Address(buf, 24));
4316 crc32x(crc, crc, tmp1);
4317 ldr(tmp3, Address(buf, 32));
4318 crc32x(crc, crc, tmp2);
4319 ldr(tmp0, Address(buf, 40));
4320 crc32x(crc, crc, tmp3);
4321 ldr(tmp1, Address(buf, 48));
4322 crc32x(crc, crc, tmp0);
4323 ldr(tmp2, Address(buf, 56));
4324 crc32x(crc, crc, tmp1);
4325 ldr(tmp3, Address(pre(buf, 64)));
4326
4327 b(CRC_by64_loop);
4328
4329 align(CodeEntryAlignment);
4330 BIND(CRC_by64_loop);
4331 subs(len, len, 64);
4332 crc32x(crc, crc, tmp2);
4333 ldr(tmp0, Address(buf, 8));
4334 crc32x(crc, crc, tmp3);
4335 ldr(tmp1, Address(buf, 16));
4336 crc32x(crc, crc, tmp0);
4337 ldr(tmp2, Address(buf, 24));
4338 crc32x(crc, crc, tmp1);
4339 ldr(tmp3, Address(buf, 32));
4340 crc32x(crc, crc, tmp2);
4341 ldr(tmp0, Address(buf, 40));
4342 crc32x(crc, crc, tmp3);
4343 ldr(tmp1, Address(buf, 48));
4344 crc32x(crc, crc, tmp0);
4345 ldr(tmp2, Address(buf, 56));
4346 crc32x(crc, crc, tmp1);
4347 ldr(tmp3, Address(pre(buf, 64)));
4348 br(Assembler::GE, CRC_by64_loop);
4349
4350 // post-loop
4351 crc32x(crc, crc, tmp2);
4352 crc32x(crc, crc, tmp3);
4353
4354 sub(len, len, 64);
4355 add(buf, buf, 8);
4356 cmn(len, (u1)128);
4357 br(Assembler::NE, CRC_less64);
4358 BIND(L_exit);
4359 mvnw(crc, crc);
4360 }
4361
4362 /**
4363 * @param crc register containing existing CRC (32-bit)
4364 * @param buf register pointing to input byte buffer (byte*)
4365 * @param len register containing number of bytes
4366 * @param table register that will contain address of CRC table
4367 * @param tmp scratch register
4368 */
4369 void MacroAssembler::kernel_crc32(Register crc, Register buf, Register len,
4370 Register table0, Register table1, Register table2, Register table3,
4371 Register tmp, Register tmp2, Register tmp3) {
4372 Label L_by16, L_by16_loop, L_by4, L_by4_loop, L_by1, L_by1_loop, L_exit;
4373
4374 if (UseCryptoPmullForCRC32) {
4375 kernel_crc32_using_crypto_pmull(crc, buf, len, table0, table1, table2, table3);
4376 return;
4377 }
4378
4379 if (UseCRC32) {
4380 kernel_crc32_using_crc32(crc, buf, len, table0, table1, table2, table3);
4381 return;
4382 }
4383
4384 mvnw(crc, crc);
4385
4386 {
4387 uint64_t offset;
4388 adrp(table0, ExternalAddress(StubRoutines::crc_table_addr()), offset);
4389 add(table0, table0, offset);
4390 }
4391 add(table1, table0, 1*256*sizeof(juint));
4392 add(table2, table0, 2*256*sizeof(juint));
4393 add(table3, table0, 3*256*sizeof(juint));
4394
4395 { // Neon code start
4396 cmp(len, (u1)64);
4397 br(Assembler::LT, L_by16);
4398 eor(v16, T16B, v16, v16);
4399
4400 Label L_fold;
4401
4402 add(tmp, table0, 4*256*sizeof(juint)); // Point at the Neon constants
4403
4404 ld1(v0, v1, T2D, post(buf, 32));
4405 ld1r(v4, T2D, post(tmp, 8));
4406 ld1r(v5, T2D, post(tmp, 8));
4407 ld1r(v6, T2D, post(tmp, 8));
4408 ld1r(v7, T2D, post(tmp, 8));
4409 mov(v16, S, 0, crc);
4410
4411 eor(v0, T16B, v0, v16);
4412 sub(len, len, 64);
4413
4414 BIND(L_fold);
4415 pmull(v22, T8H, v0, v5, T8B);
4416 pmull(v20, T8H, v0, v7, T8B);
4417 pmull(v23, T8H, v0, v4, T8B);
4418 pmull(v21, T8H, v0, v6, T8B);
4419
4420 pmull2(v18, T8H, v0, v5, T16B);
4421 pmull2(v16, T8H, v0, v7, T16B);
4422 pmull2(v19, T8H, v0, v4, T16B);
4423 pmull2(v17, T8H, v0, v6, T16B);
4424
4425 uzp1(v24, T8H, v20, v22);
4426 uzp2(v25, T8H, v20, v22);
4427 eor(v20, T16B, v24, v25);
4428
4429 uzp1(v26, T8H, v16, v18);
4430 uzp2(v27, T8H, v16, v18);
4431 eor(v16, T16B, v26, v27);
4432
4433 ushll2(v22, T4S, v20, T8H, 8);
4434 ushll(v20, T4S, v20, T4H, 8);
4435
4436 ushll2(v18, T4S, v16, T8H, 8);
4437 ushll(v16, T4S, v16, T4H, 8);
4438
4439 eor(v22, T16B, v23, v22);
4440 eor(v18, T16B, v19, v18);
4441 eor(v20, T16B, v21, v20);
4442 eor(v16, T16B, v17, v16);
4443
4444 uzp1(v17, T2D, v16, v20);
4445 uzp2(v21, T2D, v16, v20);
4446 eor(v17, T16B, v17, v21);
4447
4448 ushll2(v20, T2D, v17, T4S, 16);
4449 ushll(v16, T2D, v17, T2S, 16);
4450
4451 eor(v20, T16B, v20, v22);
4452 eor(v16, T16B, v16, v18);
4453
4454 uzp1(v17, T2D, v20, v16);
4455 uzp2(v21, T2D, v20, v16);
4456 eor(v28, T16B, v17, v21);
4457
4458 pmull(v22, T8H, v1, v5, T8B);
4459 pmull(v20, T8H, v1, v7, T8B);
4460 pmull(v23, T8H, v1, v4, T8B);
4461 pmull(v21, T8H, v1, v6, T8B);
4462
4463 pmull2(v18, T8H, v1, v5, T16B);
4464 pmull2(v16, T8H, v1, v7, T16B);
4465 pmull2(v19, T8H, v1, v4, T16B);
4466 pmull2(v17, T8H, v1, v6, T16B);
4467
4468 ld1(v0, v1, T2D, post(buf, 32));
4469
4470 uzp1(v24, T8H, v20, v22);
4471 uzp2(v25, T8H, v20, v22);
4472 eor(v20, T16B, v24, v25);
4473
4474 uzp1(v26, T8H, v16, v18);
4475 uzp2(v27, T8H, v16, v18);
4476 eor(v16, T16B, v26, v27);
4477
4478 ushll2(v22, T4S, v20, T8H, 8);
4479 ushll(v20, T4S, v20, T4H, 8);
4480
4481 ushll2(v18, T4S, v16, T8H, 8);
4482 ushll(v16, T4S, v16, T4H, 8);
4483
4484 eor(v22, T16B, v23, v22);
4485 eor(v18, T16B, v19, v18);
4486 eor(v20, T16B, v21, v20);
4487 eor(v16, T16B, v17, v16);
4488
4489 uzp1(v17, T2D, v16, v20);
4490 uzp2(v21, T2D, v16, v20);
4491 eor(v16, T16B, v17, v21);
4492
4493 ushll2(v20, T2D, v16, T4S, 16);
4494 ushll(v16, T2D, v16, T2S, 16);
4495
4496 eor(v20, T16B, v22, v20);
4497 eor(v16, T16B, v16, v18);
4498
4499 uzp1(v17, T2D, v20, v16);
4500 uzp2(v21, T2D, v20, v16);
4501 eor(v20, T16B, v17, v21);
4502
4503 shl(v16, T2D, v28, 1);
4504 shl(v17, T2D, v20, 1);
4505
4506 eor(v0, T16B, v0, v16);
4507 eor(v1, T16B, v1, v17);
4508
4509 subs(len, len, 32);
4510 br(Assembler::GE, L_fold);
4511
4512 mov(crc, 0);
4513 mov(tmp, v0, D, 0);
4514 update_word_crc32(crc, tmp, tmp2, table0, table1, table2, table3, false);
4515 update_word_crc32(crc, tmp, tmp2, table0, table1, table2, table3, true);
4516 mov(tmp, v0, D, 1);
4517 update_word_crc32(crc, tmp, tmp2, table0, table1, table2, table3, false);
4518 update_word_crc32(crc, tmp, tmp2, table0, table1, table2, table3, true);
4519 mov(tmp, v1, D, 0);
4520 update_word_crc32(crc, tmp, tmp2, table0, table1, table2, table3, false);
4521 update_word_crc32(crc, tmp, tmp2, table0, table1, table2, table3, true);
4522 mov(tmp, v1, D, 1);
4523 update_word_crc32(crc, tmp, tmp2, table0, table1, table2, table3, false);
4524 update_word_crc32(crc, tmp, tmp2, table0, table1, table2, table3, true);
4525
4526 add(len, len, 32);
4527 } // Neon code end
4528
4529 BIND(L_by16);
4530 subs(len, len, 16);
4531 br(Assembler::GE, L_by16_loop);
4532 adds(len, len, 16-4);
4533 br(Assembler::GE, L_by4_loop);
4534 adds(len, len, 4);
4535 br(Assembler::GT, L_by1_loop);
4536 b(L_exit);
4537
4538 BIND(L_by4_loop);
4539 ldrw(tmp, Address(post(buf, 4)));
4540 update_word_crc32(crc, tmp, tmp2, table0, table1, table2, table3);
4541 subs(len, len, 4);
4542 br(Assembler::GE, L_by4_loop);
4543 adds(len, len, 4);
4544 br(Assembler::LE, L_exit);
4545 BIND(L_by1_loop);
4546 subs(len, len, 1);
4547 ldrb(tmp, Address(post(buf, 1)));
4548 update_byte_crc32(crc, tmp, table0);
4549 br(Assembler::GT, L_by1_loop);
4550 b(L_exit);
4551
4552 align(CodeEntryAlignment);
4553 BIND(L_by16_loop);
4554 subs(len, len, 16);
4555 ldp(tmp, tmp3, Address(post(buf, 16)));
4556 update_word_crc32(crc, tmp, tmp2, table0, table1, table2, table3, false);
4557 update_word_crc32(crc, tmp, tmp2, table0, table1, table2, table3, true);
4558 update_word_crc32(crc, tmp3, tmp2, table0, table1, table2, table3, false);
4559 update_word_crc32(crc, tmp3, tmp2, table0, table1, table2, table3, true);
4560 br(Assembler::GE, L_by16_loop);
4561 adds(len, len, 16-4);
4562 br(Assembler::GE, L_by4_loop);
4563 adds(len, len, 4);
4564 br(Assembler::GT, L_by1_loop);
4565 BIND(L_exit);
4566 mvnw(crc, crc);
4567 }
4568
4569 void MacroAssembler::kernel_crc32c_using_crypto_pmull(Register crc, Register buf,
4570 Register len, Register tmp0, Register tmp1, Register tmp2, Register tmp3) {
4571 Label CRC_by4_loop, CRC_by1_loop, CRC_less128, CRC_by128_pre, CRC_by32_loop, CRC_less32, L_exit;
4572 assert_different_registers(crc, buf, len, tmp0, tmp1, tmp2);
4573
4574 subs(tmp0, len, 384);
4575 br(Assembler::GE, CRC_by128_pre);
4576 BIND(CRC_less128);
4577 subs(len, len, 32);
4578 br(Assembler::GE, CRC_by32_loop);
4579 BIND(CRC_less32);
4580 adds(len, len, 32 - 4);
4581 br(Assembler::GE, CRC_by4_loop);
4582 adds(len, len, 4);
4583 br(Assembler::GT, CRC_by1_loop);
4584 b(L_exit);
4585
4586 BIND(CRC_by32_loop);
4587 ldp(tmp0, tmp1, Address(buf));
4588 crc32cx(crc, crc, tmp0);
4589 ldr(tmp2, Address(buf, 16));
4590 crc32cx(crc, crc, tmp1);
4591 ldr(tmp3, Address(buf, 24));
4592 crc32cx(crc, crc, tmp2);
4593 add(buf, buf, 32);
4594 subs(len, len, 32);
4595 crc32cx(crc, crc, tmp3);
4596 br(Assembler::GE, CRC_by32_loop);
4597 cmn(len, (u1)32);
4598 br(Assembler::NE, CRC_less32);
4599 b(L_exit);
4600
4601 BIND(CRC_by4_loop);
4602 ldrw(tmp0, Address(post(buf, 4)));
4603 subs(len, len, 4);
4604 crc32cw(crc, crc, tmp0);
4605 br(Assembler::GE, CRC_by4_loop);
4606 adds(len, len, 4);
4607 br(Assembler::LE, L_exit);
4608 BIND(CRC_by1_loop);
4609 ldrb(tmp0, Address(post(buf, 1)));
4610 subs(len, len, 1);
4611 crc32cb(crc, crc, tmp0);
4612 br(Assembler::GT, CRC_by1_loop);
4613 b(L_exit);
4614
4615 BIND(CRC_by128_pre);
4616 kernel_crc32_common_fold_using_crypto_pmull(crc, buf, len, tmp0, tmp1, tmp2,
4617 4*256*sizeof(juint) + 8*sizeof(juint) + 0x50);
4618 mov(crc, 0);
4619 crc32cx(crc, crc, tmp0);
4620 crc32cx(crc, crc, tmp1);
4621
4622 cbnz(len, CRC_less128);
4623
4624 BIND(L_exit);
4625 }
4626
4627 void MacroAssembler::kernel_crc32c_using_crc32c(Register crc, Register buf,
4628 Register len, Register tmp0, Register tmp1, Register tmp2,
4629 Register tmp3) {
4630 Label CRC_by64_loop, CRC_by4_loop, CRC_by1_loop, CRC_less64, CRC_by64_pre, CRC_by32_loop, CRC_less32, L_exit;
4631 assert_different_registers(crc, buf, len, tmp0, tmp1, tmp2, tmp3);
4632
4633 subs(len, len, 128);
4634 br(Assembler::GE, CRC_by64_pre);
4635 BIND(CRC_less64);
4636 adds(len, len, 128-32);
4637 br(Assembler::GE, CRC_by32_loop);
4638 BIND(CRC_less32);
4639 adds(len, len, 32-4);
4640 br(Assembler::GE, CRC_by4_loop);
4641 adds(len, len, 4);
4642 br(Assembler::GT, CRC_by1_loop);
4643 b(L_exit);
4644
4645 BIND(CRC_by32_loop);
4646 ldp(tmp0, tmp1, Address(post(buf, 16)));
4647 subs(len, len, 32);
4648 crc32cx(crc, crc, tmp0);
4649 ldr(tmp2, Address(post(buf, 8)));
4650 crc32cx(crc, crc, tmp1);
4651 ldr(tmp3, Address(post(buf, 8)));
4652 crc32cx(crc, crc, tmp2);
4653 crc32cx(crc, crc, tmp3);
4654 br(Assembler::GE, CRC_by32_loop);
4655 cmn(len, (u1)32);
4656 br(Assembler::NE, CRC_less32);
4657 b(L_exit);
4658
4659 BIND(CRC_by4_loop);
4660 ldrw(tmp0, Address(post(buf, 4)));
4661 subs(len, len, 4);
4662 crc32cw(crc, crc, tmp0);
4663 br(Assembler::GE, CRC_by4_loop);
4664 adds(len, len, 4);
4665 br(Assembler::LE, L_exit);
4666 BIND(CRC_by1_loop);
4667 ldrb(tmp0, Address(post(buf, 1)));
4668 subs(len, len, 1);
4669 crc32cb(crc, crc, tmp0);
4670 br(Assembler::GT, CRC_by1_loop);
4671 b(L_exit);
4672
4673 BIND(CRC_by64_pre);
4674 sub(buf, buf, 8);
4675 ldp(tmp0, tmp1, Address(buf, 8));
4676 crc32cx(crc, crc, tmp0);
4677 ldr(tmp2, Address(buf, 24));
4678 crc32cx(crc, crc, tmp1);
4679 ldr(tmp3, Address(buf, 32));
4680 crc32cx(crc, crc, tmp2);
4681 ldr(tmp0, Address(buf, 40));
4682 crc32cx(crc, crc, tmp3);
4683 ldr(tmp1, Address(buf, 48));
4684 crc32cx(crc, crc, tmp0);
4685 ldr(tmp2, Address(buf, 56));
4686 crc32cx(crc, crc, tmp1);
4687 ldr(tmp3, Address(pre(buf, 64)));
4688
4689 b(CRC_by64_loop);
4690
4691 align(CodeEntryAlignment);
4692 BIND(CRC_by64_loop);
4693 subs(len, len, 64);
4694 crc32cx(crc, crc, tmp2);
4695 ldr(tmp0, Address(buf, 8));
4696 crc32cx(crc, crc, tmp3);
4697 ldr(tmp1, Address(buf, 16));
4698 crc32cx(crc, crc, tmp0);
4699 ldr(tmp2, Address(buf, 24));
4700 crc32cx(crc, crc, tmp1);
4701 ldr(tmp3, Address(buf, 32));
4702 crc32cx(crc, crc, tmp2);
4703 ldr(tmp0, Address(buf, 40));
4704 crc32cx(crc, crc, tmp3);
4705 ldr(tmp1, Address(buf, 48));
4706 crc32cx(crc, crc, tmp0);
4707 ldr(tmp2, Address(buf, 56));
4708 crc32cx(crc, crc, tmp1);
4709 ldr(tmp3, Address(pre(buf, 64)));
4710 br(Assembler::GE, CRC_by64_loop);
4711
4712 // post-loop
4713 crc32cx(crc, crc, tmp2);
4714 crc32cx(crc, crc, tmp3);
4715
4716 sub(len, len, 64);
4717 add(buf, buf, 8);
4718 cmn(len, (u1)128);
4719 br(Assembler::NE, CRC_less64);
4720 BIND(L_exit);
4721 }
4722
4723 /**
4724 * @param crc register containing existing CRC (32-bit)
4725 * @param buf register pointing to input byte buffer (byte*)
4726 * @param len register containing number of bytes
4727 * @param table register that will contain address of CRC table
4728 * @param tmp scratch register
4729 */
4730 void MacroAssembler::kernel_crc32c(Register crc, Register buf, Register len,
4731 Register table0, Register table1, Register table2, Register table3,
4732 Register tmp, Register tmp2, Register tmp3) {
4733 if (UseCryptoPmullForCRC32) {
4734 kernel_crc32c_using_crypto_pmull(crc, buf, len, table0, table1, table2, table3);
4735 } else {
4736 kernel_crc32c_using_crc32c(crc, buf, len, table0, table1, table2, table3);
4737 }
4738 }
4739
4740 void MacroAssembler::kernel_crc32_common_fold_using_crypto_pmull(Register crc, Register buf,
4741 Register len, Register tmp0, Register tmp1, Register tmp2, size_t table_offset) {
4742 Label CRC_by128_loop;
4743 assert_different_registers(crc, buf, len, tmp0, tmp1, tmp2);
4744
4745 sub(len, len, 256);
4746 Register table = tmp0;
4747 {
4748 uint64_t offset;
4749 adrp(table, ExternalAddress(StubRoutines::crc_table_addr()), offset);
4750 add(table, table, offset);
4751 }
4752 add(table, table, table_offset);
4753
4754 // Registers v0..v7 are used as data registers.
4755 // Registers v16..v31 are used as tmp registers.
4756 sub(buf, buf, 0x10);
4757 ldrq(v0, Address(buf, 0x10));
4758 ldrq(v1, Address(buf, 0x20));
4759 ldrq(v2, Address(buf, 0x30));
4760 ldrq(v3, Address(buf, 0x40));
4761 ldrq(v4, Address(buf, 0x50));
4762 ldrq(v5, Address(buf, 0x60));
4763 ldrq(v6, Address(buf, 0x70));
4764 ldrq(v7, Address(pre(buf, 0x80)));
4765
4766 movi(v31, T4S, 0);
4767 mov(v31, S, 0, crc);
4768 eor(v0, T16B, v0, v31);
4769
4770 // Register v16 contains constants from the crc table.
4771 ldrq(v16, Address(table));
4772 b(CRC_by128_loop);
4773
4774 align(OptoLoopAlignment);
4775 BIND(CRC_by128_loop);
4776 pmull (v17, T1Q, v0, v16, T1D);
4777 pmull2(v18, T1Q, v0, v16, T2D);
4778 ldrq(v0, Address(buf, 0x10));
4779 eor3(v0, T16B, v17, v18, v0);
4780
4781 pmull (v19, T1Q, v1, v16, T1D);
4782 pmull2(v20, T1Q, v1, v16, T2D);
4783 ldrq(v1, Address(buf, 0x20));
4784 eor3(v1, T16B, v19, v20, v1);
4785
4786 pmull (v21, T1Q, v2, v16, T1D);
4787 pmull2(v22, T1Q, v2, v16, T2D);
4788 ldrq(v2, Address(buf, 0x30));
4789 eor3(v2, T16B, v21, v22, v2);
4790
4791 pmull (v23, T1Q, v3, v16, T1D);
4792 pmull2(v24, T1Q, v3, v16, T2D);
4793 ldrq(v3, Address(buf, 0x40));
4794 eor3(v3, T16B, v23, v24, v3);
4795
4796 pmull (v25, T1Q, v4, v16, T1D);
4797 pmull2(v26, T1Q, v4, v16, T2D);
4798 ldrq(v4, Address(buf, 0x50));
4799 eor3(v4, T16B, v25, v26, v4);
4800
4801 pmull (v27, T1Q, v5, v16, T1D);
4802 pmull2(v28, T1Q, v5, v16, T2D);
4803 ldrq(v5, Address(buf, 0x60));
4804 eor3(v5, T16B, v27, v28, v5);
4805
4806 pmull (v29, T1Q, v6, v16, T1D);
4807 pmull2(v30, T1Q, v6, v16, T2D);
4808 ldrq(v6, Address(buf, 0x70));
4809 eor3(v6, T16B, v29, v30, v6);
4810
4811 // Reuse registers v23, v24.
4812 // Using them won't block the first instruction of the next iteration.
4813 pmull (v23, T1Q, v7, v16, T1D);
4814 pmull2(v24, T1Q, v7, v16, T2D);
4815 ldrq(v7, Address(pre(buf, 0x80)));
4816 eor3(v7, T16B, v23, v24, v7);
4817
4818 subs(len, len, 0x80);
4819 br(Assembler::GE, CRC_by128_loop);
4820
4821 // fold into 512 bits
4822 // Use v31 for constants because v16 can be still in use.
4823 ldrq(v31, Address(table, 0x10));
4824
4825 pmull (v17, T1Q, v0, v31, T1D);
4826 pmull2(v18, T1Q, v0, v31, T2D);
4827 eor3(v0, T16B, v17, v18, v4);
4828
4829 pmull (v19, T1Q, v1, v31, T1D);
4830 pmull2(v20, T1Q, v1, v31, T2D);
4831 eor3(v1, T16B, v19, v20, v5);
4832
4833 pmull (v21, T1Q, v2, v31, T1D);
4834 pmull2(v22, T1Q, v2, v31, T2D);
4835 eor3(v2, T16B, v21, v22, v6);
4836
4837 pmull (v23, T1Q, v3, v31, T1D);
4838 pmull2(v24, T1Q, v3, v31, T2D);
4839 eor3(v3, T16B, v23, v24, v7);
4840
4841 // fold into 128 bits
4842 // Use v17 for constants because v31 can be still in use.
4843 ldrq(v17, Address(table, 0x20));
4844 pmull (v25, T1Q, v0, v17, T1D);
4845 pmull2(v26, T1Q, v0, v17, T2D);
4846 eor3(v3, T16B, v3, v25, v26);
4847
4848 // Use v18 for constants because v17 can be still in use.
4849 ldrq(v18, Address(table, 0x30));
4850 pmull (v27, T1Q, v1, v18, T1D);
4851 pmull2(v28, T1Q, v1, v18, T2D);
4852 eor3(v3, T16B, v3, v27, v28);
4853
4854 // Use v19 for constants because v18 can be still in use.
4855 ldrq(v19, Address(table, 0x40));
4856 pmull (v29, T1Q, v2, v19, T1D);
4857 pmull2(v30, T1Q, v2, v19, T2D);
4858 eor3(v0, T16B, v3, v29, v30);
4859
4860 add(len, len, 0x80);
4861 add(buf, buf, 0x10);
4862
4863 mov(tmp0, v0, D, 0);
4864 mov(tmp1, v0, D, 1);
4865 }
4866
4867 void MacroAssembler::addptr(const Address &dst, int32_t src) {
4868 Address adr;
4869 switch(dst.getMode()) {
4870 case Address::base_plus_offset:
4871 // This is the expected mode, although we allow all the other
4872 // forms below.
4873 adr = form_address(rscratch2, dst.base(), dst.offset(), LogBytesPerWord);
4874 break;
4875 default:
4876 lea(rscratch2, dst);
4877 adr = Address(rscratch2);
4878 break;
4879 }
4880 ldr(rscratch1, adr);
4881 add(rscratch1, rscratch1, src);
4882 str(rscratch1, adr);
4883 }
4884
4885 void MacroAssembler::cmpptr(Register src1, Address src2) {
4886 uint64_t offset;
4887 adrp(rscratch1, src2, offset);
4888 ldr(rscratch1, Address(rscratch1, offset));
4889 cmp(src1, rscratch1);
4890 }
4891
4892 void MacroAssembler::cmpoop(Register obj1, Register obj2) {
4893 cmp(obj1, obj2);
4894 }
4895
4896 void MacroAssembler::load_method_holder_cld(Register rresult, Register rmethod) {
4897 load_method_holder(rresult, rmethod);
4898 ldr(rresult, Address(rresult, InstanceKlass::class_loader_data_offset()));
4899 }
4900
4901 void MacroAssembler::load_method_holder(Register holder, Register method) {
4902 ldr(holder, Address(method, Method::const_offset())); // ConstMethod*
4903 ldr(holder, Address(holder, ConstMethod::constants_offset())); // ConstantPool*
4904 ldr(holder, Address(holder, ConstantPool::pool_holder_offset())); // InstanceKlass*
4905 }
4906
4907 // Loads the obj's Klass* into dst.
4908 // Preserves all registers (incl src, rscratch1 and rscratch2).
4909 // Input:
4910 // src - the oop we want to load the klass from.
4911 // dst - output narrow klass.
4912 void MacroAssembler::load_narrow_klass_compact(Register dst, Register src) {
4913 assert(UseCompactObjectHeaders, "expects UseCompactObjectHeaders");
4914 ldr(dst, Address(src, oopDesc::mark_offset_in_bytes()));
4915 lsr(dst, dst, markWord::klass_shift);
4916 }
4917
4918 void MacroAssembler::load_klass(Register dst, Register src) {
4919 if (UseCompactObjectHeaders) {
4920 load_narrow_klass_compact(dst, src);
4921 decode_klass_not_null(dst);
4922 } else if (UseCompressedClassPointers) {
4923 ldrw(dst, Address(src, oopDesc::klass_offset_in_bytes()));
4924 decode_klass_not_null(dst);
4925 } else {
4926 ldr(dst, Address(src, oopDesc::klass_offset_in_bytes()));
4927 }
4928 }
4929
4930 void MacroAssembler::restore_cpu_control_state_after_jni(Register tmp1, Register tmp2) {
4931 if (RestoreMXCSROnJNICalls) {
4932 Label OK;
4933 get_fpcr(tmp1);
4934 mov(tmp2, tmp1);
4935 // Set FPCR to the state we need. We do want Round to Nearest. We
4936 // don't want non-IEEE rounding modes or floating-point traps.
4937 bfi(tmp1, zr, 22, 4); // Clear DN, FZ, and Rmode
4938 bfi(tmp1, zr, 8, 5); // Clear exception-control bits (8-12)
4939 bfi(tmp1, zr, 0, 2); // Clear AH:FIZ
4940 eor(tmp2, tmp1, tmp2);
4941 cbz(tmp2, OK); // Only reset FPCR if it's wrong
4942 set_fpcr(tmp1);
4943 bind(OK);
4944 }
4945 }
4946
4947 // ((OopHandle)result).resolve();
4948 void MacroAssembler::resolve_oop_handle(Register result, Register tmp1, Register tmp2) {
4949 // OopHandle::resolve is an indirection.
4950 access_load_at(T_OBJECT, IN_NATIVE, result, Address(result, 0), tmp1, tmp2);
4951 }
4952
4953 // ((WeakHandle)result).resolve();
4954 void MacroAssembler::resolve_weak_handle(Register result, Register tmp1, Register tmp2) {
4955 assert_different_registers(result, tmp1, tmp2);
4956 Label resolved;
4957
4958 // A null weak handle resolves to null.
4959 cbz(result, resolved);
4960
4961 // Only 64 bit platforms support GCs that require a tmp register
4962 // WeakHandle::resolve is an indirection like jweak.
4963 access_load_at(T_OBJECT, IN_NATIVE | ON_PHANTOM_OOP_REF,
4964 result, Address(result), tmp1, tmp2);
4965 bind(resolved);
4966 }
4967
4968 void MacroAssembler::load_mirror(Register dst, Register method, Register tmp1, Register tmp2) {
4969 const int mirror_offset = in_bytes(Klass::java_mirror_offset());
4970 ldr(dst, Address(rmethod, Method::const_offset()));
4971 ldr(dst, Address(dst, ConstMethod::constants_offset()));
4972 ldr(dst, Address(dst, ConstantPool::pool_holder_offset()));
4973 ldr(dst, Address(dst, mirror_offset));
4974 resolve_oop_handle(dst, tmp1, tmp2);
4975 }
4976
4977 void MacroAssembler::cmp_klass(Register obj, Register klass, Register tmp) {
4978 assert_different_registers(obj, klass, tmp);
4979 if (UseCompressedClassPointers) {
4980 if (UseCompactObjectHeaders) {
4981 load_narrow_klass_compact(tmp, obj);
4982 } else {
4983 ldrw(tmp, Address(obj, oopDesc::klass_offset_in_bytes()));
4984 }
4985 if (CompressedKlassPointers::base() == nullptr) {
4986 cmp(klass, tmp, LSL, CompressedKlassPointers::shift());
4987 return;
4988 } else if (((uint64_t)CompressedKlassPointers::base() & 0xffffffff) == 0
4989 && CompressedKlassPointers::shift() == 0) {
4990 // Only the bottom 32 bits matter
4991 cmpw(klass, tmp);
4992 return;
4993 }
4994 decode_klass_not_null(tmp);
4995 } else {
4996 ldr(tmp, Address(obj, oopDesc::klass_offset_in_bytes()));
4997 }
4998 cmp(klass, tmp);
4999 }
5000
5001 void MacroAssembler::cmp_klasses_from_objects(Register obj1, Register obj2, Register tmp1, Register tmp2) {
5002 if (UseCompactObjectHeaders) {
5003 load_narrow_klass_compact(tmp1, obj1);
5004 load_narrow_klass_compact(tmp2, obj2);
5005 cmpw(tmp1, tmp2);
5006 } else if (UseCompressedClassPointers) {
5007 ldrw(tmp1, Address(obj1, oopDesc::klass_offset_in_bytes()));
5008 ldrw(tmp2, Address(obj2, oopDesc::klass_offset_in_bytes()));
5009 cmpw(tmp1, tmp2);
5010 } else {
5011 ldr(tmp1, Address(obj1, oopDesc::klass_offset_in_bytes()));
5012 ldr(tmp2, Address(obj2, oopDesc::klass_offset_in_bytes()));
5013 cmp(tmp1, tmp2);
5014 }
5015 }
5016
5017 void MacroAssembler::store_klass(Register dst, Register src) {
5018 // FIXME: Should this be a store release? concurrent gcs assumes
5019 // klass length is valid if klass field is not null.
5020 assert(!UseCompactObjectHeaders, "not with compact headers");
5021 if (UseCompressedClassPointers) {
5022 encode_klass_not_null(src);
5023 strw(src, Address(dst, oopDesc::klass_offset_in_bytes()));
5024 } else {
5025 str(src, Address(dst, oopDesc::klass_offset_in_bytes()));
5026 }
5027 }
5028
5029 void MacroAssembler::store_klass_gap(Register dst, Register src) {
5030 assert(!UseCompactObjectHeaders, "not with compact headers");
5031 if (UseCompressedClassPointers) {
5032 // Store to klass gap in destination
5033 strw(src, Address(dst, oopDesc::klass_gap_offset_in_bytes()));
5034 }
5035 }
5036
5037 // Algorithm must match CompressedOops::encode.
5038 void MacroAssembler::encode_heap_oop(Register d, Register s) {
5039 #ifdef ASSERT
5040 verify_heapbase("MacroAssembler::encode_heap_oop: heap base corrupted?");
5041 #endif
5042 verify_oop_msg(s, "broken oop in encode_heap_oop");
5043 if (CompressedOops::base() == nullptr) {
5044 if (CompressedOops::shift() != 0) {
5045 assert (LogMinObjAlignmentInBytes == CompressedOops::shift(), "decode alg wrong");
5046 lsr(d, s, LogMinObjAlignmentInBytes);
5047 } else {
5048 mov(d, s);
5049 }
5050 } else {
5051 subs(d, s, rheapbase);
5052 csel(d, d, zr, Assembler::HS);
5053 lsr(d, d, LogMinObjAlignmentInBytes);
5054
5055 /* Old algorithm: is this any worse?
5056 Label nonnull;
5057 cbnz(r, nonnull);
5058 sub(r, r, rheapbase);
5059 bind(nonnull);
5060 lsr(r, r, LogMinObjAlignmentInBytes);
5061 */
5062 }
5063 }
5064
5065 void MacroAssembler::encode_heap_oop_not_null(Register r) {
5066 #ifdef ASSERT
5067 verify_heapbase("MacroAssembler::encode_heap_oop_not_null: heap base corrupted?");
5068 if (CheckCompressedOops) {
5069 Label ok;
5070 cbnz(r, ok);
5071 stop("null oop passed to encode_heap_oop_not_null");
5072 bind(ok);
5073 }
5074 #endif
5075 verify_oop_msg(r, "broken oop in encode_heap_oop_not_null");
5076 if (CompressedOops::base() != nullptr) {
5077 sub(r, r, rheapbase);
5078 }
5079 if (CompressedOops::shift() != 0) {
5080 assert (LogMinObjAlignmentInBytes == CompressedOops::shift(), "decode alg wrong");
5081 lsr(r, r, LogMinObjAlignmentInBytes);
5082 }
5083 }
5084
5085 void MacroAssembler::encode_heap_oop_not_null(Register dst, Register src) {
5086 #ifdef ASSERT
5087 verify_heapbase("MacroAssembler::encode_heap_oop_not_null2: heap base corrupted?");
5088 if (CheckCompressedOops) {
5089 Label ok;
5090 cbnz(src, ok);
5091 stop("null oop passed to encode_heap_oop_not_null2");
5092 bind(ok);
5093 }
5094 #endif
5095 verify_oop_msg(src, "broken oop in encode_heap_oop_not_null2");
5096
5097 Register data = src;
5098 if (CompressedOops::base() != nullptr) {
5099 sub(dst, src, rheapbase);
5100 data = dst;
5101 }
5102 if (CompressedOops::shift() != 0) {
5103 assert (LogMinObjAlignmentInBytes == CompressedOops::shift(), "decode alg wrong");
5104 lsr(dst, data, LogMinObjAlignmentInBytes);
5105 data = dst;
5106 }
5107 if (data == src)
5108 mov(dst, src);
5109 }
5110
5111 void MacroAssembler::decode_heap_oop(Register d, Register s) {
5112 #ifdef ASSERT
5113 verify_heapbase("MacroAssembler::decode_heap_oop: heap base corrupted?");
5114 #endif
5115 if (CompressedOops::base() == nullptr) {
5116 if (CompressedOops::shift() != 0) {
5117 lsl(d, s, CompressedOops::shift());
5118 } else if (d != s) {
5119 mov(d, s);
5120 }
5121 } else {
5122 Label done;
5123 if (d != s)
5124 mov(d, s);
5125 cbz(s, done);
5126 add(d, rheapbase, s, Assembler::LSL, LogMinObjAlignmentInBytes);
5127 bind(done);
5128 }
5129 verify_oop_msg(d, "broken oop in decode_heap_oop");
5130 }
5131
5132 void MacroAssembler::decode_heap_oop_not_null(Register r) {
5133 assert (UseCompressedOops, "should only be used for compressed headers");
5134 assert (Universe::heap() != nullptr, "java heap should be initialized");
5135 // Cannot assert, unverified entry point counts instructions (see .ad file)
5136 // vtableStubs also counts instructions in pd_code_size_limit.
5137 // Also do not verify_oop as this is called by verify_oop.
5138 if (CompressedOops::shift() != 0) {
5139 assert(LogMinObjAlignmentInBytes == CompressedOops::shift(), "decode alg wrong");
5140 if (CompressedOops::base() != nullptr) {
5141 add(r, rheapbase, r, Assembler::LSL, LogMinObjAlignmentInBytes);
5142 } else {
5143 add(r, zr, r, Assembler::LSL, LogMinObjAlignmentInBytes);
5144 }
5145 } else {
5146 assert (CompressedOops::base() == nullptr, "sanity");
5147 }
5148 }
5149
5150 void MacroAssembler::decode_heap_oop_not_null(Register dst, Register src) {
5151 assert (UseCompressedOops, "should only be used for compressed headers");
5152 assert (Universe::heap() != nullptr, "java heap should be initialized");
5153 // Cannot assert, unverified entry point counts instructions (see .ad file)
5154 // vtableStubs also counts instructions in pd_code_size_limit.
5155 // Also do not verify_oop as this is called by verify_oop.
5156 if (CompressedOops::shift() != 0) {
5157 assert(LogMinObjAlignmentInBytes == CompressedOops::shift(), "decode alg wrong");
5158 if (CompressedOops::base() != nullptr) {
5159 add(dst, rheapbase, src, Assembler::LSL, LogMinObjAlignmentInBytes);
5160 } else {
5161 add(dst, zr, src, Assembler::LSL, LogMinObjAlignmentInBytes);
5162 }
5163 } else {
5164 assert (CompressedOops::base() == nullptr, "sanity");
5165 if (dst != src) {
5166 mov(dst, src);
5167 }
5168 }
5169 }
5170
5171 MacroAssembler::KlassDecodeMode MacroAssembler::_klass_decode_mode(KlassDecodeNone);
5172
5173 MacroAssembler::KlassDecodeMode MacroAssembler::klass_decode_mode() {
5174 assert(Metaspace::initialized(), "metaspace not initialized yet");
5175 assert(_klass_decode_mode != KlassDecodeNone, "should be initialized");
5176 return _klass_decode_mode;
5177 }
5178
5179 MacroAssembler::KlassDecodeMode MacroAssembler::klass_decode_mode(address base, int shift, const size_t range) {
5180 assert(UseCompressedClassPointers, "not using compressed class pointers");
5181
5182 // KlassDecodeMode shouldn't be set already.
5183 assert(_klass_decode_mode == KlassDecodeNone, "set once");
5184
5185 if (base == nullptr) {
5186 return KlassDecodeZero;
5187 }
5188
5189 if (operand_valid_for_logical_immediate(
5190 /*is32*/false, (uint64_t)base)) {
5191 const uint64_t range_mask = right_n_bits(log2i_ceil(range));
5192 if (((uint64_t)base & range_mask) == 0) {
5193 return KlassDecodeXor;
5194 }
5195 }
5196
5197 const uint64_t shifted_base =
5198 (uint64_t)base >> shift;
5199 if ((shifted_base & 0xffff0000ffffffff) == 0) {
5200 return KlassDecodeMovk;
5201 }
5202
5203 // No valid encoding.
5204 return KlassDecodeNone;
5205 }
5206
5207 // Check if one of the above decoding modes will work for given base, shift and range.
5208 bool MacroAssembler::check_klass_decode_mode(address base, int shift, const size_t range) {
5209 return klass_decode_mode(base, shift, range) != KlassDecodeNone;
5210 }
5211
5212 bool MacroAssembler::set_klass_decode_mode(address base, int shift, const size_t range) {
5213 _klass_decode_mode = klass_decode_mode(base, shift, range);
5214 return _klass_decode_mode != KlassDecodeNone;
5215 }
5216
5217 static Register pick_different_tmp(Register dst, Register src) {
5218 auto tmps = RegSet::of(r0, r1, r2) - RegSet::of(src, dst);
5219 return *tmps.begin();
5220 }
5221
5222 void MacroAssembler::encode_klass_not_null_for_aot(Register dst, Register src) {
5223 // we have to load the klass base from the AOT constants area but
5224 // not the shift because it is not allowed to change
5225 int shift = CompressedKlassPointers::shift();
5226 assert(shift >= 0 && shift <= CompressedKlassPointers::max_shift(), "unexpected compressed klass shift!");
5227 if (dst != src) {
5228 // we can load the base into dst, subtract it formthe src and shift down
5229 lea(dst, ExternalAddress(CompressedKlassPointers::base_addr()));
5230 ldr(dst, dst);
5231 sub(dst, src, dst);
5232 lsr(dst, dst, shift);
5233 } else {
5234 // we need an extra register in order to load the coop base
5235 Register tmp = pick_different_tmp(dst, src);
5236 RegSet regs = RegSet::of(tmp);
5237 push(regs, sp);
5238 lea(tmp, ExternalAddress(CompressedKlassPointers::base_addr()));
5239 ldr(tmp, tmp);
5240 sub(dst, src, tmp);
5241 lsr(dst, dst, shift);
5242 pop(regs, sp);
5243 }
5244 }
5245
5246 void MacroAssembler::encode_klass_not_null(Register dst, Register src) {
5247 if (AOTCodeCache::is_on_for_dump()) {
5248 encode_klass_not_null_for_aot(dst, src);
5249 return;
5250 }
5251
5252 switch (klass_decode_mode()) {
5253 case KlassDecodeZero:
5254 if (CompressedKlassPointers::shift() != 0) {
5255 lsr(dst, src, CompressedKlassPointers::shift());
5256 } else {
5257 if (dst != src) mov(dst, src);
5258 }
5259 break;
5260
5261 case KlassDecodeXor:
5262 if (CompressedKlassPointers::shift() != 0) {
5263 eor(dst, src, (uint64_t)CompressedKlassPointers::base());
5264 lsr(dst, dst, CompressedKlassPointers::shift());
5265 } else {
5266 eor(dst, src, (uint64_t)CompressedKlassPointers::base());
5267 }
5268 break;
5269
5270 case KlassDecodeMovk:
5271 if (CompressedKlassPointers::shift() != 0) {
5272 ubfx(dst, src, CompressedKlassPointers::shift(), 32);
5273 } else {
5274 movw(dst, src);
5275 }
5276 break;
5277
5278 case KlassDecodeNone:
5279 ShouldNotReachHere();
5280 break;
5281 }
5282 }
5283
5284 void MacroAssembler::encode_klass_not_null(Register r) {
5285 encode_klass_not_null(r, r);
5286 }
5287
5288 void MacroAssembler::decode_klass_not_null_for_aot(Register dst, Register src) {
5289 // we have to load the klass base from the AOT constants area but
5290 // not the shift because it is not allowed to change
5291 int shift = CompressedKlassPointers::shift();
5292 assert(shift >= 0 && shift <= CompressedKlassPointers::max_shift(), "unexpected compressed klass shift!");
5293 if (dst != src) {
5294 // we can load the base into dst then add the offset with a suitable shift
5295 lea(dst, ExternalAddress(CompressedKlassPointers::base_addr()));
5296 ldr(dst, dst);
5297 add(dst, dst, src, LSL, shift);
5298 } else {
5299 // we need an extra register in order to load the coop base
5300 Register tmp = pick_different_tmp(dst, src);
5301 RegSet regs = RegSet::of(tmp);
5302 push(regs, sp);
5303 lea(tmp, ExternalAddress(CompressedKlassPointers::base_addr()));
5304 ldr(tmp, tmp);
5305 add(dst, tmp, src, LSL, shift);
5306 pop(regs, sp);
5307 }
5308 }
5309
5310 void MacroAssembler::decode_klass_not_null(Register dst, Register src) {
5311 assert (UseCompressedClassPointers, "should only be used for compressed headers");
5312
5313 if (AOTCodeCache::is_on_for_dump()) {
5314 decode_klass_not_null_for_aot(dst, src);
5315 return;
5316 }
5317
5318 switch (klass_decode_mode()) {
5319 case KlassDecodeZero:
5320 if (CompressedKlassPointers::shift() != 0) {
5321 lsl(dst, src, CompressedKlassPointers::shift());
5322 } else {
5323 if (dst != src) mov(dst, src);
5324 }
5325 break;
5326
5327 case KlassDecodeXor:
5328 if (CompressedKlassPointers::shift() != 0) {
5329 lsl(dst, src, CompressedKlassPointers::shift());
5330 eor(dst, dst, (uint64_t)CompressedKlassPointers::base());
5331 } else {
5332 eor(dst, src, (uint64_t)CompressedKlassPointers::base());
5333 }
5334 break;
5335
5336 case KlassDecodeMovk: {
5337 const uint64_t shifted_base =
5338 (uint64_t)CompressedKlassPointers::base() >> CompressedKlassPointers::shift();
5339
5340 if (dst != src) movw(dst, src);
5341 movk(dst, shifted_base >> 32, 32);
5342
5343 if (CompressedKlassPointers::shift() != 0) {
5344 lsl(dst, dst, CompressedKlassPointers::shift());
5345 }
5346
5347 break;
5348 }
5349
5350 case KlassDecodeNone:
5351 ShouldNotReachHere();
5352 break;
5353 }
5354 }
5355
5356 void MacroAssembler::decode_klass_not_null(Register r) {
5357 decode_klass_not_null(r, r);
5358 }
5359
5360 void MacroAssembler::set_narrow_oop(Register dst, jobject obj) {
5361 #ifdef ASSERT
5362 {
5363 ThreadInVMfromUnknown tiv;
5364 assert (UseCompressedOops, "should only be used for compressed oops");
5365 assert (Universe::heap() != nullptr, "java heap should be initialized");
5366 assert (oop_recorder() != nullptr, "this assembler needs an OopRecorder");
5367 assert(Universe::heap()->is_in(JNIHandles::resolve(obj)), "should be real oop");
5368 }
5369 #endif
5370 int oop_index = oop_recorder()->find_index(obj);
5371 InstructionMark im(this);
5372 RelocationHolder rspec = oop_Relocation::spec(oop_index);
5373 code_section()->relocate(inst_mark(), rspec);
5374 movz(dst, 0xDEAD, 16);
5375 movk(dst, 0xBEEF);
5376 }
5377
5378 void MacroAssembler::set_narrow_klass(Register dst, Klass* k) {
5379 assert (UseCompressedClassPointers, "should only be used for compressed headers");
5380 assert (oop_recorder() != nullptr, "this assembler needs an OopRecorder");
5381 int index = oop_recorder()->find_index(k);
5382 assert(! Universe::heap()->is_in(k), "should not be an oop");
5383
5384 InstructionMark im(this);
5385 RelocationHolder rspec = metadata_Relocation::spec(index);
5386 code_section()->relocate(inst_mark(), rspec);
5387 narrowKlass nk = CompressedKlassPointers::encode(k);
5388 movz(dst, (nk >> 16), 16);
5389 movk(dst, nk & 0xffff);
5390 }
5391
5392 void MacroAssembler::access_load_at(BasicType type, DecoratorSet decorators,
5393 Register dst, Address src,
5394 Register tmp1, Register tmp2) {
5395 BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
5396 decorators = AccessInternal::decorator_fixup(decorators, type);
5397 bool as_raw = (decorators & AS_RAW) != 0;
5398 if (as_raw) {
5399 bs->BarrierSetAssembler::load_at(this, decorators, type, dst, src, tmp1, tmp2);
5400 } else {
5401 bs->load_at(this, decorators, type, dst, src, tmp1, tmp2);
5402 }
5403 }
5404
5405 void MacroAssembler::access_store_at(BasicType type, DecoratorSet decorators,
5406 Address dst, Register val,
5407 Register tmp1, Register tmp2, Register tmp3) {
5408 BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
5409 decorators = AccessInternal::decorator_fixup(decorators, type);
5410 bool as_raw = (decorators & AS_RAW) != 0;
5411 if (as_raw) {
5412 bs->BarrierSetAssembler::store_at(this, decorators, type, dst, val, tmp1, tmp2, tmp3);
5413 } else {
5414 bs->store_at(this, decorators, type, dst, val, tmp1, tmp2, tmp3);
5415 }
5416 }
5417
5418 void MacroAssembler::load_heap_oop(Register dst, Address src, Register tmp1,
5419 Register tmp2, DecoratorSet decorators) {
5420 access_load_at(T_OBJECT, IN_HEAP | decorators, dst, src, tmp1, tmp2);
5421 }
5422
5423 void MacroAssembler::load_heap_oop_not_null(Register dst, Address src, Register tmp1,
5424 Register tmp2, DecoratorSet decorators) {
5425 access_load_at(T_OBJECT, IN_HEAP | IS_NOT_NULL | decorators, dst, src, tmp1, tmp2);
5426 }
5427
5428 void MacroAssembler::store_heap_oop(Address dst, Register val, Register tmp1,
5429 Register tmp2, Register tmp3, DecoratorSet decorators) {
5430 access_store_at(T_OBJECT, IN_HEAP | decorators, dst, val, tmp1, tmp2, tmp3);
5431 }
5432
5433 // Used for storing nulls.
5434 void MacroAssembler::store_heap_oop_null(Address dst) {
5435 access_store_at(T_OBJECT, IN_HEAP, dst, noreg, noreg, noreg, noreg);
5436 }
5437
5438 Address MacroAssembler::allocate_metadata_address(Metadata* obj) {
5439 assert(oop_recorder() != nullptr, "this assembler needs a Recorder");
5440 int index = oop_recorder()->allocate_metadata_index(obj);
5441 RelocationHolder rspec = metadata_Relocation::spec(index);
5442 return Address((address)obj, rspec);
5443 }
5444
5445 // Move an oop into a register.
5446 void MacroAssembler::movoop(Register dst, jobject obj) {
5447 int oop_index;
5448 if (obj == nullptr) {
5449 oop_index = oop_recorder()->allocate_oop_index(obj);
5450 } else {
5451 #ifdef ASSERT
5452 {
5453 ThreadInVMfromUnknown tiv;
5454 assert(Universe::heap()->is_in(JNIHandles::resolve(obj)), "should be real oop");
5455 }
5456 #endif
5457 oop_index = oop_recorder()->find_index(obj);
5458 }
5459 RelocationHolder rspec = oop_Relocation::spec(oop_index);
5460
5461 if (BarrierSet::barrier_set()->barrier_set_assembler()->supports_instruction_patching()) {
5462 mov(dst, Address((address)obj, rspec));
5463 } else {
5464 address dummy = address(uintptr_t(pc()) & -wordSize); // A nearby aligned address
5465 ldr(dst, Address(dummy, rspec));
5466 }
5467 }
5468
5469 // Move a metadata address into a register.
5470 void MacroAssembler::mov_metadata(Register dst, Metadata* obj) {
5471 int oop_index;
5472 if (obj == nullptr) {
5473 oop_index = oop_recorder()->allocate_metadata_index(obj);
5474 } else {
5475 oop_index = oop_recorder()->find_index(obj);
5476 }
5477 RelocationHolder rspec = metadata_Relocation::spec(oop_index);
5478 mov(dst, Address((address)obj, rspec));
5479 }
5480
5481 Address MacroAssembler::constant_oop_address(jobject obj) {
5482 #ifdef ASSERT
5483 {
5484 ThreadInVMfromUnknown tiv;
5485 assert(oop_recorder() != nullptr, "this assembler needs an OopRecorder");
5486 assert(Universe::heap()->is_in(JNIHandles::resolve(obj)), "not an oop");
5487 }
5488 #endif
5489 int oop_index = oop_recorder()->find_index(obj);
5490 return Address((address)obj, oop_Relocation::spec(oop_index));
5491 }
5492
5493 // Defines obj, preserves var_size_in_bytes, okay for t2 == var_size_in_bytes.
5494 void MacroAssembler::tlab_allocate(Register obj,
5495 Register var_size_in_bytes,
5496 int con_size_in_bytes,
5497 Register t1,
5498 Register t2,
5499 Label& slow_case) {
5500 BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
5501 bs->tlab_allocate(this, obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
5502 }
5503
5504 void MacroAssembler::verify_tlab() {
5505 #ifdef ASSERT
5506 if (UseTLAB && VerifyOops) {
5507 Label next, ok;
5508
5509 stp(rscratch2, rscratch1, Address(pre(sp, -16)));
5510
5511 ldr(rscratch2, Address(rthread, in_bytes(JavaThread::tlab_top_offset())));
5512 ldr(rscratch1, Address(rthread, in_bytes(JavaThread::tlab_start_offset())));
5513 cmp(rscratch2, rscratch1);
5514 br(Assembler::HS, next);
5515 STOP("assert(top >= start)");
5516 should_not_reach_here();
5517
5518 bind(next);
5519 ldr(rscratch2, Address(rthread, in_bytes(JavaThread::tlab_end_offset())));
5520 ldr(rscratch1, Address(rthread, in_bytes(JavaThread::tlab_top_offset())));
5521 cmp(rscratch2, rscratch1);
5522 br(Assembler::HS, ok);
5523 STOP("assert(top <= end)");
5524 should_not_reach_here();
5525
5526 bind(ok);
5527 ldp(rscratch2, rscratch1, Address(post(sp, 16)));
5528 }
5529 #endif
5530 }
5531
5532 // Writes to stack successive pages until offset reached to check for
5533 // stack overflow + shadow pages. This clobbers tmp.
5534 void MacroAssembler::bang_stack_size(Register size, Register tmp) {
5535 assert_different_registers(tmp, size, rscratch1);
5536 mov(tmp, sp);
5537 // Bang stack for total size given plus shadow page size.
5538 // Bang one page at a time because large size can bang beyond yellow and
5539 // red zones.
5540 Label loop;
5541 mov(rscratch1, (int)os::vm_page_size());
5542 bind(loop);
5543 lea(tmp, Address(tmp, -(int)os::vm_page_size()));
5544 subsw(size, size, rscratch1);
5545 str(size, Address(tmp));
5546 br(Assembler::GT, loop);
5547
5548 // Bang down shadow pages too.
5549 // At this point, (tmp-0) is the last address touched, so don't
5550 // touch it again. (It was touched as (tmp-pagesize) but then tmp
5551 // was post-decremented.) Skip this address by starting at i=1, and
5552 // touch a few more pages below. N.B. It is important to touch all
5553 // the way down to and including i=StackShadowPages.
5554 for (int i = 0; i < (int)(StackOverflow::stack_shadow_zone_size() / (int)os::vm_page_size()) - 1; i++) {
5555 // this could be any sized move but this is can be a debugging crumb
5556 // so the bigger the better.
5557 lea(tmp, Address(tmp, -(int)os::vm_page_size()));
5558 str(size, Address(tmp));
5559 }
5560 }
5561
5562 // Move the address of the polling page into dest.
5563 void MacroAssembler::get_polling_page(Register dest, relocInfo::relocType rtype) {
5564 ldr(dest, Address(rthread, JavaThread::polling_page_offset()));
5565 }
5566
5567 // Read the polling page. The address of the polling page must
5568 // already be in r.
5569 address MacroAssembler::read_polling_page(Register r, relocInfo::relocType rtype) {
5570 address mark;
5571 {
5572 InstructionMark im(this);
5573 code_section()->relocate(inst_mark(), rtype);
5574 ldrw(zr, Address(r, 0));
5575 mark = inst_mark();
5576 }
5577 verify_cross_modify_fence_not_required();
5578 return mark;
5579 }
5580
5581 void MacroAssembler::adrp(Register reg1, const Address &dest, uint64_t &byte_offset) {
5582 relocInfo::relocType rtype = dest.rspec().reloc()->type();
5583 uint64_t low_page = (uint64_t)CodeCache::low_bound() >> 12;
5584 uint64_t high_page = (uint64_t)(CodeCache::high_bound()-1) >> 12;
5585 uint64_t dest_page = (uint64_t)dest.target() >> 12;
5586 int64_t offset_low = dest_page - low_page;
5587 int64_t offset_high = dest_page - high_page;
5588
5589 assert(is_valid_AArch64_address(dest.target()), "bad address");
5590 assert(dest.getMode() == Address::literal, "ADRP must be applied to a literal address");
5591
5592 InstructionMark im(this);
5593 code_section()->relocate(inst_mark(), dest.rspec());
5594 // 8143067: Ensure that the adrp can reach the dest from anywhere within
5595 // the code cache so that if it is relocated we know it will still reach
5596 if (offset_high >= -(1<<20) && offset_low < (1<<20)) {
5597 _adrp(reg1, dest.target());
5598 } else {
5599 uint64_t target = (uint64_t)dest.target();
5600 uint64_t adrp_target
5601 = (target & 0xffffffffULL) | ((uint64_t)pc() & 0xffff00000000ULL);
5602
5603 _adrp(reg1, (address)adrp_target);
5604 movk(reg1, target >> 32, 32);
5605 }
5606 byte_offset = (uint64_t)dest.target() & 0xfff;
5607 }
5608
5609 void MacroAssembler::load_byte_map_base(Register reg) {
5610 CardTable::CardValue* byte_map_base =
5611 ((CardTableBarrierSet*)(BarrierSet::barrier_set()))->card_table()->byte_map_base();
5612
5613 // Strictly speaking the byte_map_base isn't an address at all, and it might
5614 // even be negative. It is thus materialised as a constant.
5615 mov(reg, (uint64_t)byte_map_base);
5616 }
5617
5618 void MacroAssembler::build_frame(int framesize) {
5619 assert(framesize >= 2 * wordSize, "framesize must include space for FP/LR");
5620 assert(framesize % (2*wordSize) == 0, "must preserve 2*wordSize alignment");
5621 protect_return_address();
5622 if (framesize < ((1 << 9) + 2 * wordSize)) {
5623 sub(sp, sp, framesize);
5624 stp(rfp, lr, Address(sp, framesize - 2 * wordSize));
5625 if (PreserveFramePointer) add(rfp, sp, framesize - 2 * wordSize);
5626 } else {
5627 stp(rfp, lr, Address(pre(sp, -2 * wordSize)));
5628 if (PreserveFramePointer) mov(rfp, sp);
5629 if (framesize < ((1 << 12) + 2 * wordSize))
5630 sub(sp, sp, framesize - 2 * wordSize);
5631 else {
5632 mov(rscratch1, framesize - 2 * wordSize);
5633 sub(sp, sp, rscratch1);
5634 }
5635 }
5636 verify_cross_modify_fence_not_required();
5637 }
5638
5639 void MacroAssembler::remove_frame(int framesize) {
5640 assert(framesize >= 2 * wordSize, "framesize must include space for FP/LR");
5641 assert(framesize % (2*wordSize) == 0, "must preserve 2*wordSize alignment");
5642 if (framesize < ((1 << 9) + 2 * wordSize)) {
5643 ldp(rfp, lr, Address(sp, framesize - 2 * wordSize));
5644 add(sp, sp, framesize);
5645 } else {
5646 if (framesize < ((1 << 12) + 2 * wordSize))
5647 add(sp, sp, framesize - 2 * wordSize);
5648 else {
5649 mov(rscratch1, framesize - 2 * wordSize);
5650 add(sp, sp, rscratch1);
5651 }
5652 ldp(rfp, lr, Address(post(sp, 2 * wordSize)));
5653 }
5654 authenticate_return_address();
5655 }
5656
5657
5658 // This method counts leading positive bytes (highest bit not set) in provided byte array
5659 address MacroAssembler::count_positives(Register ary1, Register len, Register result) {
5660 // Simple and most common case of aligned small array which is not at the
5661 // end of memory page is placed here. All other cases are in stub.
5662 Label LOOP, END, STUB, STUB_LONG, SET_RESULT, DONE;
5663 const uint64_t UPPER_BIT_MASK=0x8080808080808080;
5664 assert_different_registers(ary1, len, result);
5665
5666 mov(result, len);
5667 cmpw(len, 0);
5668 br(LE, DONE);
5669 cmpw(len, 4 * wordSize);
5670 br(GE, STUB_LONG); // size > 32 then go to stub
5671
5672 int shift = 64 - exact_log2(os::vm_page_size());
5673 lsl(rscratch1, ary1, shift);
5674 mov(rscratch2, (size_t)(4 * wordSize) << shift);
5675 adds(rscratch2, rscratch1, rscratch2); // At end of page?
5676 br(CS, STUB); // at the end of page then go to stub
5677 subs(len, len, wordSize);
5678 br(LT, END);
5679
5680 BIND(LOOP);
5681 ldr(rscratch1, Address(post(ary1, wordSize)));
5682 tst(rscratch1, UPPER_BIT_MASK);
5683 br(NE, SET_RESULT);
5684 subs(len, len, wordSize);
5685 br(GE, LOOP);
5686 cmpw(len, -wordSize);
5687 br(EQ, DONE);
5688
5689 BIND(END);
5690 ldr(rscratch1, Address(ary1));
5691 sub(rscratch2, zr, len, LSL, 3); // LSL 3 is to get bits from bytes
5692 lslv(rscratch1, rscratch1, rscratch2);
5693 tst(rscratch1, UPPER_BIT_MASK);
5694 br(NE, SET_RESULT);
5695 b(DONE);
5696
5697 BIND(STUB);
5698 RuntimeAddress count_pos = RuntimeAddress(StubRoutines::aarch64::count_positives());
5699 assert(count_pos.target() != nullptr, "count_positives stub has not been generated");
5700 address tpc1 = trampoline_call(count_pos);
5701 if (tpc1 == nullptr) {
5702 DEBUG_ONLY(reset_labels(STUB_LONG, SET_RESULT, DONE));
5703 postcond(pc() == badAddress);
5704 return nullptr;
5705 }
5706 b(DONE);
5707
5708 BIND(STUB_LONG);
5709 RuntimeAddress count_pos_long = RuntimeAddress(StubRoutines::aarch64::count_positives_long());
5710 assert(count_pos_long.target() != nullptr, "count_positives_long stub has not been generated");
5711 address tpc2 = trampoline_call(count_pos_long);
5712 if (tpc2 == nullptr) {
5713 DEBUG_ONLY(reset_labels(SET_RESULT, DONE));
5714 postcond(pc() == badAddress);
5715 return nullptr;
5716 }
5717 b(DONE);
5718
5719 BIND(SET_RESULT);
5720
5721 add(len, len, wordSize);
5722 sub(result, result, len);
5723
5724 BIND(DONE);
5725 postcond(pc() != badAddress);
5726 return pc();
5727 }
5728
5729 // Clobbers: rscratch1, rscratch2, rflags
5730 // May also clobber v0-v7 when (!UseSimpleArrayEquals && UseSIMDForArrayEquals)
5731 address MacroAssembler::arrays_equals(Register a1, Register a2, Register tmp3,
5732 Register tmp4, Register tmp5, Register result,
5733 Register cnt1, int elem_size) {
5734 Label DONE, SAME;
5735 Register tmp1 = rscratch1;
5736 Register tmp2 = rscratch2;
5737 int elem_per_word = wordSize/elem_size;
5738 int log_elem_size = exact_log2(elem_size);
5739 int klass_offset = arrayOopDesc::klass_offset_in_bytes();
5740 int length_offset = arrayOopDesc::length_offset_in_bytes();
5741 int base_offset
5742 = arrayOopDesc::base_offset_in_bytes(elem_size == 2 ? T_CHAR : T_BYTE);
5743 // When the length offset is not aligned to 8 bytes,
5744 // then we align it down. This is valid because the new
5745 // offset will always be the klass which is the same
5746 // for type arrays.
5747 int start_offset = align_down(length_offset, BytesPerWord);
5748 int extra_length = base_offset - start_offset;
5749 assert(start_offset == length_offset || start_offset == klass_offset,
5750 "start offset must be 8-byte-aligned or be the klass offset");
5751 assert(base_offset != start_offset, "must include the length field");
5752 extra_length = extra_length / elem_size; // We count in elements, not bytes.
5753 int stubBytesThreshold = 3 * 64 + (UseSIMDForArrayEquals ? 0 : 16);
5754
5755 assert(elem_size == 1 || elem_size == 2, "must be char or byte");
5756 assert_different_registers(a1, a2, result, cnt1, rscratch1, rscratch2);
5757
5758 #ifndef PRODUCT
5759 {
5760 const char kind = (elem_size == 2) ? 'U' : 'L';
5761 char comment[64];
5762 os::snprintf_checked(comment, sizeof comment, "array_equals%c{", kind);
5763 BLOCK_COMMENT(comment);
5764 }
5765 #endif
5766
5767 // if (a1 == a2)
5768 // return true;
5769 cmpoop(a1, a2); // May have read barriers for a1 and a2.
5770 br(EQ, SAME);
5771
5772 if (UseSimpleArrayEquals) {
5773 Label NEXT_WORD, SHORT, TAIL03, TAIL01, A_MIGHT_BE_NULL, A_IS_NOT_NULL;
5774 // if (a1 == nullptr || a2 == nullptr)
5775 // return false;
5776 // a1 & a2 == 0 means (some-pointer is null) or
5777 // (very-rare-or-even-probably-impossible-pointer-values)
5778 // so, we can save one branch in most cases
5779 tst(a1, a2);
5780 mov(result, false);
5781 br(EQ, A_MIGHT_BE_NULL);
5782 // if (a1.length != a2.length)
5783 // return false;
5784 bind(A_IS_NOT_NULL);
5785 ldrw(cnt1, Address(a1, length_offset));
5786 // Increase loop counter by diff between base- and actual start-offset.
5787 addw(cnt1, cnt1, extra_length);
5788 lea(a1, Address(a1, start_offset));
5789 lea(a2, Address(a2, start_offset));
5790 // Check for short strings, i.e. smaller than wordSize.
5791 subs(cnt1, cnt1, elem_per_word);
5792 br(Assembler::LT, SHORT);
5793 // Main 8 byte comparison loop.
5794 bind(NEXT_WORD); {
5795 ldr(tmp1, Address(post(a1, wordSize)));
5796 ldr(tmp2, Address(post(a2, wordSize)));
5797 subs(cnt1, cnt1, elem_per_word);
5798 eor(tmp5, tmp1, tmp2);
5799 cbnz(tmp5, DONE);
5800 } br(GT, NEXT_WORD);
5801 // Last longword. In the case where length == 4 we compare the
5802 // same longword twice, but that's still faster than another
5803 // conditional branch.
5804 // cnt1 could be 0, -1, -2, -3, -4 for chars; -4 only happens when
5805 // length == 4.
5806 if (log_elem_size > 0)
5807 lsl(cnt1, cnt1, log_elem_size);
5808 ldr(tmp3, Address(a1, cnt1));
5809 ldr(tmp4, Address(a2, cnt1));
5810 eor(tmp5, tmp3, tmp4);
5811 cbnz(tmp5, DONE);
5812 b(SAME);
5813 bind(A_MIGHT_BE_NULL);
5814 // in case both a1 and a2 are not-null, proceed with loads
5815 cbz(a1, DONE);
5816 cbz(a2, DONE);
5817 b(A_IS_NOT_NULL);
5818 bind(SHORT);
5819
5820 tbz(cnt1, 2 - log_elem_size, TAIL03); // 0-7 bytes left.
5821 {
5822 ldrw(tmp1, Address(post(a1, 4)));
5823 ldrw(tmp2, Address(post(a2, 4)));
5824 eorw(tmp5, tmp1, tmp2);
5825 cbnzw(tmp5, DONE);
5826 }
5827 bind(TAIL03);
5828 tbz(cnt1, 1 - log_elem_size, TAIL01); // 0-3 bytes left.
5829 {
5830 ldrh(tmp3, Address(post(a1, 2)));
5831 ldrh(tmp4, Address(post(a2, 2)));
5832 eorw(tmp5, tmp3, tmp4);
5833 cbnzw(tmp5, DONE);
5834 }
5835 bind(TAIL01);
5836 if (elem_size == 1) { // Only needed when comparing byte arrays.
5837 tbz(cnt1, 0, SAME); // 0-1 bytes left.
5838 {
5839 ldrb(tmp1, a1);
5840 ldrb(tmp2, a2);
5841 eorw(tmp5, tmp1, tmp2);
5842 cbnzw(tmp5, DONE);
5843 }
5844 }
5845 } else {
5846 Label NEXT_DWORD, SHORT, TAIL, TAIL2, STUB,
5847 CSET_EQ, LAST_CHECK;
5848 mov(result, false);
5849 cbz(a1, DONE);
5850 ldrw(cnt1, Address(a1, length_offset));
5851 cbz(a2, DONE);
5852 // Increase loop counter by diff between base- and actual start-offset.
5853 addw(cnt1, cnt1, extra_length);
5854
5855 // on most CPUs a2 is still "locked"(surprisingly) in ldrw and it's
5856 // faster to perform another branch before comparing a1 and a2
5857 cmp(cnt1, (u1)elem_per_word);
5858 br(LE, SHORT); // short or same
5859 ldr(tmp3, Address(pre(a1, start_offset)));
5860 subs(zr, cnt1, stubBytesThreshold);
5861 br(GE, STUB);
5862 ldr(tmp4, Address(pre(a2, start_offset)));
5863 sub(tmp5, zr, cnt1, LSL, 3 + log_elem_size);
5864
5865 // Main 16 byte comparison loop with 2 exits
5866 bind(NEXT_DWORD); {
5867 ldr(tmp1, Address(pre(a1, wordSize)));
5868 ldr(tmp2, Address(pre(a2, wordSize)));
5869 subs(cnt1, cnt1, 2 * elem_per_word);
5870 br(LE, TAIL);
5871 eor(tmp4, tmp3, tmp4);
5872 cbnz(tmp4, DONE);
5873 ldr(tmp3, Address(pre(a1, wordSize)));
5874 ldr(tmp4, Address(pre(a2, wordSize)));
5875 cmp(cnt1, (u1)elem_per_word);
5876 br(LE, TAIL2);
5877 cmp(tmp1, tmp2);
5878 } br(EQ, NEXT_DWORD);
5879 b(DONE);
5880
5881 bind(TAIL);
5882 eor(tmp4, tmp3, tmp4);
5883 eor(tmp2, tmp1, tmp2);
5884 lslv(tmp2, tmp2, tmp5);
5885 orr(tmp5, tmp4, tmp2);
5886 cmp(tmp5, zr);
5887 b(CSET_EQ);
5888
5889 bind(TAIL2);
5890 eor(tmp2, tmp1, tmp2);
5891 cbnz(tmp2, DONE);
5892 b(LAST_CHECK);
5893
5894 bind(STUB);
5895 ldr(tmp4, Address(pre(a2, start_offset)));
5896 if (elem_size == 2) { // convert to byte counter
5897 lsl(cnt1, cnt1, 1);
5898 }
5899 eor(tmp5, tmp3, tmp4);
5900 cbnz(tmp5, DONE);
5901 RuntimeAddress stub = RuntimeAddress(StubRoutines::aarch64::large_array_equals());
5902 assert(stub.target() != nullptr, "array_equals_long stub has not been generated");
5903 address tpc = trampoline_call(stub);
5904 if (tpc == nullptr) {
5905 DEBUG_ONLY(reset_labels(SHORT, LAST_CHECK, CSET_EQ, SAME, DONE));
5906 postcond(pc() == badAddress);
5907 return nullptr;
5908 }
5909 b(DONE);
5910
5911 // (a1 != null && a2 == null) || (a1 != null && a2 != null && a1 == a2)
5912 // so, if a2 == null => return false(0), else return true, so we can return a2
5913 mov(result, a2);
5914 b(DONE);
5915 bind(SHORT);
5916 sub(tmp5, zr, cnt1, LSL, 3 + log_elem_size);
5917 ldr(tmp3, Address(a1, start_offset));
5918 ldr(tmp4, Address(a2, start_offset));
5919 bind(LAST_CHECK);
5920 eor(tmp4, tmp3, tmp4);
5921 lslv(tmp5, tmp4, tmp5);
5922 cmp(tmp5, zr);
5923 bind(CSET_EQ);
5924 cset(result, EQ);
5925 b(DONE);
5926 }
5927
5928 bind(SAME);
5929 mov(result, true);
5930 // That's it.
5931 bind(DONE);
5932
5933 BLOCK_COMMENT("} array_equals");
5934 postcond(pc() != badAddress);
5935 return pc();
5936 }
5937
5938 // Compare Strings
5939
5940 // For Strings we're passed the address of the first characters in a1
5941 // and a2 and the length in cnt1.
5942 // There are two implementations. For arrays >= 8 bytes, all
5943 // comparisons (including the final one, which may overlap) are
5944 // performed 8 bytes at a time. For strings < 8 bytes, we compare a
5945 // halfword, then a short, and then a byte.
5946
5947 void MacroAssembler::string_equals(Register a1, Register a2,
5948 Register result, Register cnt1)
5949 {
5950 Label SAME, DONE, SHORT, NEXT_WORD;
5951 Register tmp1 = rscratch1;
5952 Register tmp2 = rscratch2;
5953 Register cnt2 = tmp2; // cnt2 only used in array length compare
5954
5955 assert_different_registers(a1, a2, result, cnt1, rscratch1, rscratch2);
5956
5957 #ifndef PRODUCT
5958 {
5959 char comment[64];
5960 os::snprintf_checked(comment, sizeof comment, "{string_equalsL");
5961 BLOCK_COMMENT(comment);
5962 }
5963 #endif
5964
5965 mov(result, false);
5966
5967 // Check for short strings, i.e. smaller than wordSize.
5968 subs(cnt1, cnt1, wordSize);
5969 br(Assembler::LT, SHORT);
5970 // Main 8 byte comparison loop.
5971 bind(NEXT_WORD); {
5972 ldr(tmp1, Address(post(a1, wordSize)));
5973 ldr(tmp2, Address(post(a2, wordSize)));
5974 subs(cnt1, cnt1, wordSize);
5975 eor(tmp1, tmp1, tmp2);
5976 cbnz(tmp1, DONE);
5977 } br(GT, NEXT_WORD);
5978 // Last longword. In the case where length == 4 we compare the
5979 // same longword twice, but that's still faster than another
5980 // conditional branch.
5981 // cnt1 could be 0, -1, -2, -3, -4 for chars; -4 only happens when
5982 // length == 4.
5983 ldr(tmp1, Address(a1, cnt1));
5984 ldr(tmp2, Address(a2, cnt1));
5985 eor(tmp2, tmp1, tmp2);
5986 cbnz(tmp2, DONE);
5987 b(SAME);
5988
5989 bind(SHORT);
5990 Label TAIL03, TAIL01;
5991
5992 tbz(cnt1, 2, TAIL03); // 0-7 bytes left.
5993 {
5994 ldrw(tmp1, Address(post(a1, 4)));
5995 ldrw(tmp2, Address(post(a2, 4)));
5996 eorw(tmp1, tmp1, tmp2);
5997 cbnzw(tmp1, DONE);
5998 }
5999 bind(TAIL03);
6000 tbz(cnt1, 1, TAIL01); // 0-3 bytes left.
6001 {
6002 ldrh(tmp1, Address(post(a1, 2)));
6003 ldrh(tmp2, Address(post(a2, 2)));
6004 eorw(tmp1, tmp1, tmp2);
6005 cbnzw(tmp1, DONE);
6006 }
6007 bind(TAIL01);
6008 tbz(cnt1, 0, SAME); // 0-1 bytes left.
6009 {
6010 ldrb(tmp1, a1);
6011 ldrb(tmp2, a2);
6012 eorw(tmp1, tmp1, tmp2);
6013 cbnzw(tmp1, DONE);
6014 }
6015 // Arrays are equal.
6016 bind(SAME);
6017 mov(result, true);
6018
6019 // That's it.
6020 bind(DONE);
6021 BLOCK_COMMENT("} string_equals");
6022 }
6023
6024
6025 // The size of the blocks erased by the zero_blocks stub. We must
6026 // handle anything smaller than this ourselves in zero_words().
6027 const int MacroAssembler::zero_words_block_size = 8;
6028
6029 // zero_words() is used by C2 ClearArray patterns and by
6030 // C1_MacroAssembler. It is as small as possible, handling small word
6031 // counts locally and delegating anything larger to the zero_blocks
6032 // stub. It is expanded many times in compiled code, so it is
6033 // important to keep it short.
6034
6035 // ptr: Address of a buffer to be zeroed.
6036 // cnt: Count in HeapWords.
6037 //
6038 // ptr, cnt, rscratch1, and rscratch2 are clobbered.
6039 address MacroAssembler::zero_words(Register ptr, Register cnt)
6040 {
6041 assert(is_power_of_2(zero_words_block_size), "adjust this");
6042
6043 BLOCK_COMMENT("zero_words {");
6044 assert(ptr == r10 && cnt == r11, "mismatch in register usage");
6045 RuntimeAddress zero_blocks = RuntimeAddress(StubRoutines::aarch64::zero_blocks());
6046 assert(zero_blocks.target() != nullptr, "zero_blocks stub has not been generated");
6047
6048 subs(rscratch1, cnt, zero_words_block_size);
6049 Label around;
6050 br(LO, around);
6051 {
6052 RuntimeAddress zero_blocks = RuntimeAddress(StubRoutines::aarch64::zero_blocks());
6053 assert(zero_blocks.target() != nullptr, "zero_blocks stub has not been generated");
6054 // Make sure this is a C2 compilation. C1 allocates space only for
6055 // trampoline stubs generated by Call LIR ops, and in any case it
6056 // makes sense for a C1 compilation task to proceed as quickly as
6057 // possible.
6058 CompileTask* task;
6059 if (StubRoutines::aarch64::complete()
6060 && Thread::current()->is_Compiler_thread()
6061 && (task = ciEnv::current()->task())
6062 && is_c2_compile(task->comp_level())) {
6063 address tpc = trampoline_call(zero_blocks);
6064 if (tpc == nullptr) {
6065 DEBUG_ONLY(reset_labels(around));
6066 return nullptr;
6067 }
6068 } else {
6069 far_call(zero_blocks);
6070 }
6071 }
6072 bind(around);
6073
6074 // We have a few words left to do. zero_blocks has adjusted r10 and r11
6075 // for us.
6076 for (int i = zero_words_block_size >> 1; i > 1; i >>= 1) {
6077 Label l;
6078 tbz(cnt, exact_log2(i), l);
6079 for (int j = 0; j < i; j += 2) {
6080 stp(zr, zr, post(ptr, 2 * BytesPerWord));
6081 }
6082 bind(l);
6083 }
6084 {
6085 Label l;
6086 tbz(cnt, 0, l);
6087 str(zr, Address(ptr));
6088 bind(l);
6089 }
6090
6091 BLOCK_COMMENT("} zero_words");
6092 return pc();
6093 }
6094
6095 // base: Address of a buffer to be zeroed, 8 bytes aligned.
6096 // cnt: Immediate count in HeapWords.
6097 //
6098 // r10, r11, rscratch1, and rscratch2 are clobbered.
6099 address MacroAssembler::zero_words(Register base, uint64_t cnt)
6100 {
6101 assert(wordSize <= BlockZeroingLowLimit,
6102 "increase BlockZeroingLowLimit");
6103 address result = nullptr;
6104 if (cnt <= (uint64_t)BlockZeroingLowLimit / BytesPerWord) {
6105 #ifndef PRODUCT
6106 {
6107 char buf[64];
6108 os::snprintf_checked(buf, sizeof buf, "zero_words (count = %" PRIu64 ") {", cnt);
6109 BLOCK_COMMENT(buf);
6110 }
6111 #endif
6112 if (cnt >= 16) {
6113 uint64_t loops = cnt/16;
6114 if (loops > 1) {
6115 mov(rscratch2, loops - 1);
6116 }
6117 {
6118 Label loop;
6119 bind(loop);
6120 for (int i = 0; i < 16; i += 2) {
6121 stp(zr, zr, Address(base, i * BytesPerWord));
6122 }
6123 add(base, base, 16 * BytesPerWord);
6124 if (loops > 1) {
6125 subs(rscratch2, rscratch2, 1);
6126 br(GE, loop);
6127 }
6128 }
6129 }
6130 cnt %= 16;
6131 int i = cnt & 1; // store any odd word to start
6132 if (i) str(zr, Address(base));
6133 for (; i < (int)cnt; i += 2) {
6134 stp(zr, zr, Address(base, i * wordSize));
6135 }
6136 BLOCK_COMMENT("} zero_words");
6137 result = pc();
6138 } else {
6139 mov(r10, base); mov(r11, cnt);
6140 result = zero_words(r10, r11);
6141 }
6142 return result;
6143 }
6144
6145 // Zero blocks of memory by using DC ZVA.
6146 //
6147 // Aligns the base address first sufficiently for DC ZVA, then uses
6148 // DC ZVA repeatedly for every full block. cnt is the size to be
6149 // zeroed in HeapWords. Returns the count of words left to be zeroed
6150 // in cnt.
6151 //
6152 // NOTE: This is intended to be used in the zero_blocks() stub. If
6153 // you want to use it elsewhere, note that cnt must be >= 2*zva_length.
6154 void MacroAssembler::zero_dcache_blocks(Register base, Register cnt) {
6155 Register tmp = rscratch1;
6156 Register tmp2 = rscratch2;
6157 int zva_length = VM_Version::zva_length();
6158 Label initial_table_end, loop_zva;
6159 Label fini;
6160
6161 // Base must be 16 byte aligned. If not just return and let caller handle it
6162 tst(base, 0x0f);
6163 br(Assembler::NE, fini);
6164 // Align base with ZVA length.
6165 neg(tmp, base);
6166 andr(tmp, tmp, zva_length - 1);
6167
6168 // tmp: the number of bytes to be filled to align the base with ZVA length.
6169 add(base, base, tmp);
6170 sub(cnt, cnt, tmp, Assembler::ASR, 3);
6171 adr(tmp2, initial_table_end);
6172 sub(tmp2, tmp2, tmp, Assembler::LSR, 2);
6173 br(tmp2);
6174
6175 for (int i = -zva_length + 16; i < 0; i += 16)
6176 stp(zr, zr, Address(base, i));
6177 bind(initial_table_end);
6178
6179 sub(cnt, cnt, zva_length >> 3);
6180 bind(loop_zva);
6181 dc(Assembler::ZVA, base);
6182 subs(cnt, cnt, zva_length >> 3);
6183 add(base, base, zva_length);
6184 br(Assembler::GE, loop_zva);
6185 add(cnt, cnt, zva_length >> 3); // count not zeroed by DC ZVA
6186 bind(fini);
6187 }
6188
6189 // base: Address of a buffer to be filled, 8 bytes aligned.
6190 // cnt: Count in 8-byte unit.
6191 // value: Value to be filled with.
6192 // base will point to the end of the buffer after filling.
6193 void MacroAssembler::fill_words(Register base, Register cnt, Register value)
6194 {
6195 // Algorithm:
6196 //
6197 // if (cnt == 0) {
6198 // return;
6199 // }
6200 // if ((p & 8) != 0) {
6201 // *p++ = v;
6202 // }
6203 //
6204 // scratch1 = cnt & 14;
6205 // cnt -= scratch1;
6206 // p += scratch1;
6207 // switch (scratch1 / 2) {
6208 // do {
6209 // cnt -= 16;
6210 // p[-16] = v;
6211 // p[-15] = v;
6212 // case 7:
6213 // p[-14] = v;
6214 // p[-13] = v;
6215 // case 6:
6216 // p[-12] = v;
6217 // p[-11] = v;
6218 // // ...
6219 // case 1:
6220 // p[-2] = v;
6221 // p[-1] = v;
6222 // case 0:
6223 // p += 16;
6224 // } while (cnt);
6225 // }
6226 // if ((cnt & 1) == 1) {
6227 // *p++ = v;
6228 // }
6229
6230 assert_different_registers(base, cnt, value, rscratch1, rscratch2);
6231
6232 Label fini, skip, entry, loop;
6233 const int unroll = 8; // Number of stp instructions we'll unroll
6234
6235 cbz(cnt, fini);
6236 tbz(base, 3, skip);
6237 str(value, Address(post(base, 8)));
6238 sub(cnt, cnt, 1);
6239 bind(skip);
6240
6241 andr(rscratch1, cnt, (unroll-1) * 2);
6242 sub(cnt, cnt, rscratch1);
6243 add(base, base, rscratch1, Assembler::LSL, 3);
6244 adr(rscratch2, entry);
6245 sub(rscratch2, rscratch2, rscratch1, Assembler::LSL, 1);
6246 br(rscratch2);
6247
6248 bind(loop);
6249 add(base, base, unroll * 16);
6250 for (int i = -unroll; i < 0; i++)
6251 stp(value, value, Address(base, i * 16));
6252 bind(entry);
6253 subs(cnt, cnt, unroll * 2);
6254 br(Assembler::GE, loop);
6255
6256 tbz(cnt, 0, fini);
6257 str(value, Address(post(base, 8)));
6258 bind(fini);
6259 }
6260
6261 // Intrinsic for
6262 //
6263 // - sun.nio.cs.ISO_8859_1.Encoder#encodeISOArray0(byte[] sa, int sp, byte[] da, int dp, int len)
6264 // Encodes char[] to byte[] in ISO-8859-1
6265 //
6266 // - java.lang.StringCoding#encodeISOArray0(byte[] sa, int sp, byte[] da, int dp, int len)
6267 // Encodes byte[] (containing UTF-16) to byte[] in ISO-8859-1
6268 //
6269 // - java.lang.StringCoding#encodeAsciiArray0(char[] sa, int sp, byte[] da, int dp, int len)
6270 // Encodes char[] to byte[] in ASCII
6271 //
6272 // This version always returns the number of characters copied, and does not
6273 // clobber the 'len' register. A successful copy will complete with the post-
6274 // condition: 'res' == 'len', while an unsuccessful copy will exit with the
6275 // post-condition: 0 <= 'res' < 'len'.
6276 //
6277 // NOTE: Attempts to use 'ld2' (and 'umaxv' in the ISO part) has proven to
6278 // degrade performance (on Ampere Altra - Neoverse N1), to an extent
6279 // beyond the acceptable, even though the footprint would be smaller.
6280 // Using 'umaxv' in the ASCII-case comes with a small penalty but does
6281 // avoid additional bloat.
6282 //
6283 // Clobbers: src, dst, res, rscratch1, rscratch2, rflags
6284 void MacroAssembler::encode_iso_array(Register src, Register dst,
6285 Register len, Register res, bool ascii,
6286 FloatRegister vtmp0, FloatRegister vtmp1,
6287 FloatRegister vtmp2, FloatRegister vtmp3,
6288 FloatRegister vtmp4, FloatRegister vtmp5)
6289 {
6290 Register cnt = res;
6291 Register max = rscratch1;
6292 Register chk = rscratch2;
6293
6294 prfm(Address(src), PLDL1STRM);
6295 movw(cnt, len);
6296
6297 #define ASCII(insn) do { if (ascii) { insn; } } while (0)
6298
6299 Label LOOP_32, DONE_32, FAIL_32;
6300
6301 BIND(LOOP_32);
6302 {
6303 cmpw(cnt, 32);
6304 br(LT, DONE_32);
6305 ld1(vtmp0, vtmp1, vtmp2, vtmp3, T8H, Address(post(src, 64)));
6306 // Extract lower bytes.
6307 FloatRegister vlo0 = vtmp4;
6308 FloatRegister vlo1 = vtmp5;
6309 uzp1(vlo0, T16B, vtmp0, vtmp1);
6310 uzp1(vlo1, T16B, vtmp2, vtmp3);
6311 // Merge bits...
6312 orr(vtmp0, T16B, vtmp0, vtmp1);
6313 orr(vtmp2, T16B, vtmp2, vtmp3);
6314 // Extract merged upper bytes.
6315 FloatRegister vhix = vtmp0;
6316 uzp2(vhix, T16B, vtmp0, vtmp2);
6317 // ISO-check on hi-parts (all zero).
6318 // ASCII-check on lo-parts (no sign).
6319 FloatRegister vlox = vtmp1; // Merge lower bytes.
6320 ASCII(orr(vlox, T16B, vlo0, vlo1));
6321 umov(chk, vhix, D, 1); ASCII(cm(LT, vlox, T16B, vlox));
6322 fmovd(max, vhix); ASCII(umaxv(vlox, T16B, vlox));
6323 orr(chk, chk, max); ASCII(umov(max, vlox, B, 0));
6324 ASCII(orr(chk, chk, max));
6325 cbnz(chk, FAIL_32);
6326 subw(cnt, cnt, 32);
6327 st1(vlo0, vlo1, T16B, Address(post(dst, 32)));
6328 b(LOOP_32);
6329 }
6330 BIND(FAIL_32);
6331 sub(src, src, 64);
6332 BIND(DONE_32);
6333
6334 Label LOOP_8, SKIP_8;
6335
6336 BIND(LOOP_8);
6337 {
6338 cmpw(cnt, 8);
6339 br(LT, SKIP_8);
6340 FloatRegister vhi = vtmp0;
6341 FloatRegister vlo = vtmp1;
6342 ld1(vtmp3, T8H, src);
6343 uzp1(vlo, T16B, vtmp3, vtmp3);
6344 uzp2(vhi, T16B, vtmp3, vtmp3);
6345 // ISO-check on hi-parts (all zero).
6346 // ASCII-check on lo-parts (no sign).
6347 ASCII(cm(LT, vtmp2, T16B, vlo));
6348 fmovd(chk, vhi); ASCII(umaxv(vtmp2, T16B, vtmp2));
6349 ASCII(umov(max, vtmp2, B, 0));
6350 ASCII(orr(chk, chk, max));
6351 cbnz(chk, SKIP_8);
6352
6353 strd(vlo, Address(post(dst, 8)));
6354 subw(cnt, cnt, 8);
6355 add(src, src, 16);
6356 b(LOOP_8);
6357 }
6358 BIND(SKIP_8);
6359
6360 #undef ASCII
6361
6362 Label LOOP, DONE;
6363
6364 cbz(cnt, DONE);
6365 BIND(LOOP);
6366 {
6367 Register chr = rscratch1;
6368 ldrh(chr, Address(post(src, 2)));
6369 tst(chr, ascii ? 0xff80 : 0xff00);
6370 br(NE, DONE);
6371 strb(chr, Address(post(dst, 1)));
6372 subs(cnt, cnt, 1);
6373 br(GT, LOOP);
6374 }
6375 BIND(DONE);
6376 // Return index where we stopped.
6377 subw(res, len, cnt);
6378 }
6379
6380 // Inflate byte[] array to char[].
6381 // Clobbers: src, dst, len, rflags, rscratch1, v0-v6
6382 address MacroAssembler::byte_array_inflate(Register src, Register dst, Register len,
6383 FloatRegister vtmp1, FloatRegister vtmp2,
6384 FloatRegister vtmp3, Register tmp4) {
6385 Label big, done, after_init, to_stub;
6386
6387 assert_different_registers(src, dst, len, tmp4, rscratch1);
6388
6389 fmovd(vtmp1, 0.0);
6390 lsrw(tmp4, len, 3);
6391 bind(after_init);
6392 cbnzw(tmp4, big);
6393 // Short string: less than 8 bytes.
6394 {
6395 Label loop, tiny;
6396
6397 cmpw(len, 4);
6398 br(LT, tiny);
6399 // Use SIMD to do 4 bytes.
6400 ldrs(vtmp2, post(src, 4));
6401 zip1(vtmp3, T8B, vtmp2, vtmp1);
6402 subw(len, len, 4);
6403 strd(vtmp3, post(dst, 8));
6404
6405 cbzw(len, done);
6406
6407 // Do the remaining bytes by steam.
6408 bind(loop);
6409 ldrb(tmp4, post(src, 1));
6410 strh(tmp4, post(dst, 2));
6411 subw(len, len, 1);
6412
6413 bind(tiny);
6414 cbnz(len, loop);
6415
6416 b(done);
6417 }
6418
6419 if (SoftwarePrefetchHintDistance >= 0) {
6420 bind(to_stub);
6421 RuntimeAddress stub = RuntimeAddress(StubRoutines::aarch64::large_byte_array_inflate());
6422 assert(stub.target() != nullptr, "large_byte_array_inflate stub has not been generated");
6423 address tpc = trampoline_call(stub);
6424 if (tpc == nullptr) {
6425 DEBUG_ONLY(reset_labels(big, done));
6426 postcond(pc() == badAddress);
6427 return nullptr;
6428 }
6429 b(after_init);
6430 }
6431
6432 // Unpack the bytes 8 at a time.
6433 bind(big);
6434 {
6435 Label loop, around, loop_last, loop_start;
6436
6437 if (SoftwarePrefetchHintDistance >= 0) {
6438 const int large_loop_threshold = (64 + 16)/8;
6439 ldrd(vtmp2, post(src, 8));
6440 andw(len, len, 7);
6441 cmp(tmp4, (u1)large_loop_threshold);
6442 br(GE, to_stub);
6443 b(loop_start);
6444
6445 bind(loop);
6446 ldrd(vtmp2, post(src, 8));
6447 bind(loop_start);
6448 subs(tmp4, tmp4, 1);
6449 br(EQ, loop_last);
6450 zip1(vtmp2, T16B, vtmp2, vtmp1);
6451 ldrd(vtmp3, post(src, 8));
6452 st1(vtmp2, T8H, post(dst, 16));
6453 subs(tmp4, tmp4, 1);
6454 zip1(vtmp3, T16B, vtmp3, vtmp1);
6455 st1(vtmp3, T8H, post(dst, 16));
6456 br(NE, loop);
6457 b(around);
6458 bind(loop_last);
6459 zip1(vtmp2, T16B, vtmp2, vtmp1);
6460 st1(vtmp2, T8H, post(dst, 16));
6461 bind(around);
6462 cbz(len, done);
6463 } else {
6464 andw(len, len, 7);
6465 bind(loop);
6466 ldrd(vtmp2, post(src, 8));
6467 sub(tmp4, tmp4, 1);
6468 zip1(vtmp3, T16B, vtmp2, vtmp1);
6469 st1(vtmp3, T8H, post(dst, 16));
6470 cbnz(tmp4, loop);
6471 }
6472 }
6473
6474 // Do the tail of up to 8 bytes.
6475 add(src, src, len);
6476 ldrd(vtmp3, Address(src, -8));
6477 add(dst, dst, len, ext::uxtw, 1);
6478 zip1(vtmp3, T16B, vtmp3, vtmp1);
6479 strq(vtmp3, Address(dst, -16));
6480
6481 bind(done);
6482 postcond(pc() != badAddress);
6483 return pc();
6484 }
6485
6486 // Compress char[] array to byte[].
6487 // Intrinsic for java.lang.StringUTF16.compress(char[] src, int srcOff, byte[] dst, int dstOff, int len)
6488 // Return the array length if every element in array can be encoded,
6489 // otherwise, the index of first non-latin1 (> 0xff) character.
6490 void MacroAssembler::char_array_compress(Register src, Register dst, Register len,
6491 Register res,
6492 FloatRegister tmp0, FloatRegister tmp1,
6493 FloatRegister tmp2, FloatRegister tmp3,
6494 FloatRegister tmp4, FloatRegister tmp5) {
6495 encode_iso_array(src, dst, len, res, false, tmp0, tmp1, tmp2, tmp3, tmp4, tmp5);
6496 }
6497
6498 // java.math.round(double a)
6499 // Returns the closest long to the argument, with ties rounding to
6500 // positive infinity. This requires some fiddling for corner
6501 // cases. We take care to avoid double rounding in e.g. (jlong)(a + 0.5).
6502 void MacroAssembler::java_round_double(Register dst, FloatRegister src,
6503 FloatRegister ftmp) {
6504 Label DONE;
6505 BLOCK_COMMENT("java_round_double: { ");
6506 fmovd(rscratch1, src);
6507 // Use RoundToNearestTiesAway unless src small and -ve.
6508 fcvtasd(dst, src);
6509 // Test if src >= 0 || abs(src) >= 0x1.0p52
6510 eor(rscratch1, rscratch1, UCONST64(1) << 63); // flip sign bit
6511 mov(rscratch2, julong_cast(0x1.0p52));
6512 cmp(rscratch1, rscratch2);
6513 br(HS, DONE); {
6514 // src < 0 && abs(src) < 0x1.0p52
6515 // src may have a fractional part, so add 0.5
6516 fmovd(ftmp, 0.5);
6517 faddd(ftmp, src, ftmp);
6518 // Convert double to jlong, use RoundTowardsNegative
6519 fcvtmsd(dst, ftmp);
6520 }
6521 bind(DONE);
6522 BLOCK_COMMENT("} java_round_double");
6523 }
6524
6525 void MacroAssembler::java_round_float(Register dst, FloatRegister src,
6526 FloatRegister ftmp) {
6527 Label DONE;
6528 BLOCK_COMMENT("java_round_float: { ");
6529 fmovs(rscratch1, src);
6530 // Use RoundToNearestTiesAway unless src small and -ve.
6531 fcvtassw(dst, src);
6532 // Test if src >= 0 || abs(src) >= 0x1.0p23
6533 eor(rscratch1, rscratch1, 0x80000000); // flip sign bit
6534 mov(rscratch2, jint_cast(0x1.0p23f));
6535 cmp(rscratch1, rscratch2);
6536 br(HS, DONE); {
6537 // src < 0 && |src| < 0x1.0p23
6538 // src may have a fractional part, so add 0.5
6539 fmovs(ftmp, 0.5f);
6540 fadds(ftmp, src, ftmp);
6541 // Convert float to jint, use RoundTowardsNegative
6542 fcvtmssw(dst, ftmp);
6543 }
6544 bind(DONE);
6545 BLOCK_COMMENT("} java_round_float");
6546 }
6547
6548 // get_thread() can be called anywhere inside generated code so we
6549 // need to save whatever non-callee save context might get clobbered
6550 // by the call to JavaThread::aarch64_get_thread_helper() or, indeed,
6551 // the call setup code.
6552 //
6553 // On Linux, aarch64_get_thread_helper() clobbers only r0, r1, and flags.
6554 // On other systems, the helper is a usual C function.
6555 //
6556 void MacroAssembler::get_thread(Register dst) {
6557 RegSet saved_regs =
6558 LINUX_ONLY(RegSet::range(r0, r1) + lr - dst)
6559 NOT_LINUX (RegSet::range(r0, r17) + lr - dst);
6560
6561 protect_return_address();
6562 push(saved_regs, sp);
6563
6564 mov(lr, ExternalAddress(CAST_FROM_FN_PTR(address, JavaThread::aarch64_get_thread_helper)));
6565 blr(lr);
6566 if (dst != c_rarg0) {
6567 mov(dst, c_rarg0);
6568 }
6569
6570 pop(saved_regs, sp);
6571 authenticate_return_address();
6572 }
6573
6574 void MacroAssembler::cache_wb(Address line) {
6575 assert(line.getMode() == Address::base_plus_offset, "mode should be base_plus_offset");
6576 assert(line.index() == noreg, "index should be noreg");
6577 assert(line.offset() == 0, "offset should be 0");
6578 // would like to assert this
6579 // assert(line._ext.shift == 0, "shift should be zero");
6580 if (VM_Version::supports_dcpop()) {
6581 // writeback using clear virtual address to point of persistence
6582 dc(Assembler::CVAP, line.base());
6583 } else {
6584 // no need to generate anything as Unsafe.writebackMemory should
6585 // never invoke this stub
6586 }
6587 }
6588
6589 void MacroAssembler::cache_wbsync(bool is_pre) {
6590 // we only need a barrier post sync
6591 if (!is_pre) {
6592 membar(Assembler::AnyAny);
6593 }
6594 }
6595
6596 void MacroAssembler::verify_sve_vector_length(Register tmp) {
6597 if (!UseSVE || VM_Version::get_max_supported_sve_vector_length() == FloatRegister::sve_vl_min) {
6598 return;
6599 }
6600 // Make sure that native code does not change SVE vector length.
6601 Label verify_ok;
6602 movw(tmp, zr);
6603 sve_inc(tmp, B);
6604 subsw(zr, tmp, VM_Version::get_initial_sve_vector_length());
6605 br(EQ, verify_ok);
6606 stop("Error: SVE vector length has changed since jvm startup");
6607 bind(verify_ok);
6608 }
6609
6610 void MacroAssembler::verify_ptrue() {
6611 Label verify_ok;
6612 if (!UseSVE) {
6613 return;
6614 }
6615 sve_cntp(rscratch1, B, ptrue, ptrue); // get true elements count.
6616 sve_dec(rscratch1, B);
6617 cbz(rscratch1, verify_ok);
6618 stop("Error: the preserved predicate register (p7) elements are not all true");
6619 bind(verify_ok);
6620 }
6621
6622 void MacroAssembler::safepoint_isb() {
6623 isb();
6624 #ifndef PRODUCT
6625 if (VerifyCrossModifyFence) {
6626 // Clear the thread state.
6627 strb(zr, Address(rthread, in_bytes(JavaThread::requires_cross_modify_fence_offset())));
6628 }
6629 #endif
6630 }
6631
6632 #ifndef PRODUCT
6633 void MacroAssembler::verify_cross_modify_fence_not_required() {
6634 if (VerifyCrossModifyFence) {
6635 // Check if thread needs a cross modify fence.
6636 ldrb(rscratch1, Address(rthread, in_bytes(JavaThread::requires_cross_modify_fence_offset())));
6637 Label fence_not_required;
6638 cbz(rscratch1, fence_not_required);
6639 // If it does then fail.
6640 lea(rscratch1, RuntimeAddress(CAST_FROM_FN_PTR(address, JavaThread::verify_cross_modify_fence_failure)));
6641 mov(c_rarg0, rthread);
6642 blr(rscratch1);
6643 bind(fence_not_required);
6644 }
6645 }
6646 #endif
6647
6648 void MacroAssembler::spin_wait() {
6649 block_comment("spin_wait {");
6650 for (int i = 0; i < VM_Version::spin_wait_desc().inst_count(); ++i) {
6651 switch (VM_Version::spin_wait_desc().inst()) {
6652 case SpinWait::NOP:
6653 nop();
6654 break;
6655 case SpinWait::ISB:
6656 isb();
6657 break;
6658 case SpinWait::YIELD:
6659 yield();
6660 break;
6661 case SpinWait::SB:
6662 assert(VM_Version::supports_sb(), "current CPU does not support SB instruction");
6663 sb();
6664 break;
6665 default:
6666 ShouldNotReachHere();
6667 }
6668 }
6669 block_comment("}");
6670 }
6671
6672 // Stack frame creation/removal
6673
6674 void MacroAssembler::enter(bool strip_ret_addr) {
6675 if (strip_ret_addr) {
6676 // Addresses can only be signed once. If there are multiple nested frames being created
6677 // in the same function, then the return address needs stripping first.
6678 strip_return_address();
6679 }
6680 protect_return_address();
6681 stp(rfp, lr, Address(pre(sp, -2 * wordSize)));
6682 mov(rfp, sp);
6683 }
6684
6685 void MacroAssembler::leave() {
6686 mov(sp, rfp);
6687 ldp(rfp, lr, Address(post(sp, 2 * wordSize)));
6688 authenticate_return_address();
6689 }
6690
6691 // ROP Protection
6692 // Use the AArch64 PAC feature to add ROP protection for generated code. Use whenever creating/
6693 // destroying stack frames or whenever directly loading/storing the LR to memory.
6694 // If ROP protection is not set then these functions are no-ops.
6695 // For more details on PAC see pauth_aarch64.hpp.
6696
6697 // Sign the LR. Use during construction of a stack frame, before storing the LR to memory.
6698 // Uses value zero as the modifier.
6699 //
6700 void MacroAssembler::protect_return_address() {
6701 if (VM_Version::use_rop_protection()) {
6702 check_return_address();
6703 paciaz();
6704 }
6705 }
6706
6707 // Sign the return value in the given register. Use before updating the LR in the existing stack
6708 // frame for the current function.
6709 // Uses value zero as the modifier.
6710 //
6711 void MacroAssembler::protect_return_address(Register return_reg) {
6712 if (VM_Version::use_rop_protection()) {
6713 check_return_address(return_reg);
6714 paciza(return_reg);
6715 }
6716 }
6717
6718 // Authenticate the LR. Use before function return, after restoring FP and loading LR from memory.
6719 // Uses value zero as the modifier.
6720 //
6721 void MacroAssembler::authenticate_return_address() {
6722 if (VM_Version::use_rop_protection()) {
6723 autiaz();
6724 check_return_address();
6725 }
6726 }
6727
6728 // Authenticate the return value in the given register. Use before updating the LR in the existing
6729 // stack frame for the current function.
6730 // Uses value zero as the modifier.
6731 //
6732 void MacroAssembler::authenticate_return_address(Register return_reg) {
6733 if (VM_Version::use_rop_protection()) {
6734 autiza(return_reg);
6735 check_return_address(return_reg);
6736 }
6737 }
6738
6739 // Strip any PAC data from LR without performing any authentication. Use with caution - only if
6740 // there is no guaranteed way of authenticating the LR.
6741 //
6742 void MacroAssembler::strip_return_address() {
6743 if (VM_Version::use_rop_protection()) {
6744 xpaclri();
6745 }
6746 }
6747
6748 #ifndef PRODUCT
6749 // PAC failures can be difficult to debug. After an authentication failure, a segfault will only
6750 // occur when the pointer is used - ie when the program returns to the invalid LR. At this point
6751 // it is difficult to debug back to the callee function.
6752 // This function simply loads from the address in the given register.
6753 // Use directly after authentication to catch authentication failures.
6754 // Also use before signing to check that the pointer is valid and hasn't already been signed.
6755 //
6756 void MacroAssembler::check_return_address(Register return_reg) {
6757 if (VM_Version::use_rop_protection()) {
6758 ldr(zr, Address(return_reg));
6759 }
6760 }
6761 #endif
6762
6763 // The java_calling_convention describes stack locations as ideal slots on
6764 // a frame with no abi restrictions. Since we must observe abi restrictions
6765 // (like the placement of the register window) the slots must be biased by
6766 // the following value.
6767 static int reg2offset_in(VMReg r) {
6768 // Account for saved rfp and lr
6769 // This should really be in_preserve_stack_slots
6770 return (r->reg2stack() + 4) * VMRegImpl::stack_slot_size;
6771 }
6772
6773 static int reg2offset_out(VMReg r) {
6774 return (r->reg2stack() + SharedRuntime::out_preserve_stack_slots()) * VMRegImpl::stack_slot_size;
6775 }
6776
6777 // On 64bit we will store integer like items to the stack as
6778 // 64bits items (AArch64 ABI) even though java would only store
6779 // 32bits for a parameter. On 32bit it will simply be 32bits
6780 // So this routine will do 32->32 on 32bit and 32->64 on 64bit
6781 void MacroAssembler::move32_64(VMRegPair src, VMRegPair dst, Register tmp) {
6782 if (src.first()->is_stack()) {
6783 if (dst.first()->is_stack()) {
6784 // stack to stack
6785 ldr(tmp, Address(rfp, reg2offset_in(src.first())));
6786 str(tmp, Address(sp, reg2offset_out(dst.first())));
6787 } else {
6788 // stack to reg
6789 ldrsw(dst.first()->as_Register(), Address(rfp, reg2offset_in(src.first())));
6790 }
6791 } else if (dst.first()->is_stack()) {
6792 // reg to stack
6793 str(src.first()->as_Register(), Address(sp, reg2offset_out(dst.first())));
6794 } else {
6795 if (dst.first() != src.first()) {
6796 sxtw(dst.first()->as_Register(), src.first()->as_Register());
6797 }
6798 }
6799 }
6800
6801 // An oop arg. Must pass a handle not the oop itself
6802 void MacroAssembler::object_move(
6803 OopMap* map,
6804 int oop_handle_offset,
6805 int framesize_in_slots,
6806 VMRegPair src,
6807 VMRegPair dst,
6808 bool is_receiver,
6809 int* receiver_offset) {
6810
6811 // must pass a handle. First figure out the location we use as a handle
6812
6813 Register rHandle = dst.first()->is_stack() ? rscratch2 : dst.first()->as_Register();
6814
6815 // See if oop is null if it is we need no handle
6816
6817 if (src.first()->is_stack()) {
6818
6819 // Oop is already on the stack as an argument
6820 int offset_in_older_frame = src.first()->reg2stack() + SharedRuntime::out_preserve_stack_slots();
6821 map->set_oop(VMRegImpl::stack2reg(offset_in_older_frame + framesize_in_slots));
6822 if (is_receiver) {
6823 *receiver_offset = (offset_in_older_frame + framesize_in_slots) * VMRegImpl::stack_slot_size;
6824 }
6825
6826 ldr(rscratch1, Address(rfp, reg2offset_in(src.first())));
6827 lea(rHandle, Address(rfp, reg2offset_in(src.first())));
6828 // conditionally move a null
6829 cmp(rscratch1, zr);
6830 csel(rHandle, zr, rHandle, Assembler::EQ);
6831 } else {
6832
6833 // Oop is in an a register we must store it to the space we reserve
6834 // on the stack for oop_handles and pass a handle if oop is non-null
6835
6836 const Register rOop = src.first()->as_Register();
6837 int oop_slot;
6838 if (rOop == j_rarg0)
6839 oop_slot = 0;
6840 else if (rOop == j_rarg1)
6841 oop_slot = 1;
6842 else if (rOop == j_rarg2)
6843 oop_slot = 2;
6844 else if (rOop == j_rarg3)
6845 oop_slot = 3;
6846 else if (rOop == j_rarg4)
6847 oop_slot = 4;
6848 else if (rOop == j_rarg5)
6849 oop_slot = 5;
6850 else if (rOop == j_rarg6)
6851 oop_slot = 6;
6852 else {
6853 assert(rOop == j_rarg7, "wrong register");
6854 oop_slot = 7;
6855 }
6856
6857 oop_slot = oop_slot * VMRegImpl::slots_per_word + oop_handle_offset;
6858 int offset = oop_slot*VMRegImpl::stack_slot_size;
6859
6860 map->set_oop(VMRegImpl::stack2reg(oop_slot));
6861 // Store oop in handle area, may be null
6862 str(rOop, Address(sp, offset));
6863 if (is_receiver) {
6864 *receiver_offset = offset;
6865 }
6866
6867 cmp(rOop, zr);
6868 lea(rHandle, Address(sp, offset));
6869 // conditionally move a null
6870 csel(rHandle, zr, rHandle, Assembler::EQ);
6871 }
6872
6873 // If arg is on the stack then place it otherwise it is already in correct reg.
6874 if (dst.first()->is_stack()) {
6875 str(rHandle, Address(sp, reg2offset_out(dst.first())));
6876 }
6877 }
6878
6879 // A float arg may have to do float reg int reg conversion
6880 void MacroAssembler::float_move(VMRegPair src, VMRegPair dst, Register tmp) {
6881 if (src.first()->is_stack()) {
6882 if (dst.first()->is_stack()) {
6883 ldrw(tmp, Address(rfp, reg2offset_in(src.first())));
6884 strw(tmp, Address(sp, reg2offset_out(dst.first())));
6885 } else {
6886 ldrs(dst.first()->as_FloatRegister(), Address(rfp, reg2offset_in(src.first())));
6887 }
6888 } else if (src.first() != dst.first()) {
6889 if (src.is_single_phys_reg() && dst.is_single_phys_reg())
6890 fmovs(dst.first()->as_FloatRegister(), src.first()->as_FloatRegister());
6891 else
6892 strs(src.first()->as_FloatRegister(), Address(sp, reg2offset_out(dst.first())));
6893 }
6894 }
6895
6896 // A long move
6897 void MacroAssembler::long_move(VMRegPair src, VMRegPair dst, Register tmp) {
6898 if (src.first()->is_stack()) {
6899 if (dst.first()->is_stack()) {
6900 // stack to stack
6901 ldr(tmp, Address(rfp, reg2offset_in(src.first())));
6902 str(tmp, Address(sp, reg2offset_out(dst.first())));
6903 } else {
6904 // stack to reg
6905 ldr(dst.first()->as_Register(), Address(rfp, reg2offset_in(src.first())));
6906 }
6907 } else if (dst.first()->is_stack()) {
6908 // reg to stack
6909 // Do we really have to sign extend???
6910 // __ movslq(src.first()->as_Register(), src.first()->as_Register());
6911 str(src.first()->as_Register(), Address(sp, reg2offset_out(dst.first())));
6912 } else {
6913 if (dst.first() != src.first()) {
6914 mov(dst.first()->as_Register(), src.first()->as_Register());
6915 }
6916 }
6917 }
6918
6919
6920 // A double move
6921 void MacroAssembler::double_move(VMRegPair src, VMRegPair dst, Register tmp) {
6922 if (src.first()->is_stack()) {
6923 if (dst.first()->is_stack()) {
6924 ldr(tmp, Address(rfp, reg2offset_in(src.first())));
6925 str(tmp, Address(sp, reg2offset_out(dst.first())));
6926 } else {
6927 ldrd(dst.first()->as_FloatRegister(), Address(rfp, reg2offset_in(src.first())));
6928 }
6929 } else if (src.first() != dst.first()) {
6930 if (src.is_single_phys_reg() && dst.is_single_phys_reg())
6931 fmovd(dst.first()->as_FloatRegister(), src.first()->as_FloatRegister());
6932 else
6933 strd(src.first()->as_FloatRegister(), Address(sp, reg2offset_out(dst.first())));
6934 }
6935 }
6936
6937 // Implements fast-locking.
6938 //
6939 // - obj: the object to be locked
6940 // - t1, t2, t3: temporary registers, will be destroyed
6941 // - slow: branched to if locking fails, absolute offset may larger than 32KB (imm14 encoding).
6942 void MacroAssembler::fast_lock(Register basic_lock, Register obj, Register t1, Register t2, Register t3, Label& slow) {
6943 assert_different_registers(basic_lock, obj, t1, t2, t3, rscratch1);
6944
6945 Label push;
6946 const Register top = t1;
6947 const Register mark = t2;
6948 const Register t = t3;
6949
6950 // Preload the markWord. It is important that this is the first
6951 // instruction emitted as it is part of C1's null check semantics.
6952 ldr(mark, Address(obj, oopDesc::mark_offset_in_bytes()));
6953
6954 if (UseObjectMonitorTable) {
6955 // Clear cache in case fast locking succeeds or we need to take the slow-path.
6956 str(zr, Address(basic_lock, BasicObjectLock::lock_offset() + in_ByteSize((BasicLock::object_monitor_cache_offset_in_bytes()))));
6957 }
6958
6959 if (DiagnoseSyncOnValueBasedClasses != 0) {
6960 load_klass(t1, obj);
6961 ldrb(t1, Address(t1, Klass::misc_flags_offset()));
6962 tst(t1, KlassFlags::_misc_is_value_based_class);
6963 br(Assembler::NE, slow);
6964 }
6965
6966 // Check if the lock-stack is full.
6967 ldrw(top, Address(rthread, JavaThread::lock_stack_top_offset()));
6968 cmpw(top, (unsigned)LockStack::end_offset());
6969 br(Assembler::GE, slow);
6970
6971 // Check for recursion.
6972 subw(t, top, oopSize);
6973 ldr(t, Address(rthread, t));
6974 cmp(obj, t);
6975 br(Assembler::EQ, push);
6976
6977 // Check header for monitor (0b10).
6978 tst(mark, markWord::monitor_value);
6979 br(Assembler::NE, slow);
6980
6981 // Try to lock. Transition lock bits 0b01 => 0b00
6982 assert(oopDesc::mark_offset_in_bytes() == 0, "required to avoid lea");
6983 orr(mark, mark, markWord::unlocked_value);
6984 eor(t, mark, markWord::unlocked_value);
6985 cmpxchg(/*addr*/ obj, /*expected*/ mark, /*new*/ t, Assembler::xword,
6986 /*acquire*/ true, /*release*/ false, /*weak*/ false, noreg);
6987 br(Assembler::NE, slow);
6988
6989 bind(push);
6990 // After successful lock, push object on lock-stack.
6991 str(obj, Address(rthread, top));
6992 addw(top, top, oopSize);
6993 strw(top, Address(rthread, JavaThread::lock_stack_top_offset()));
6994 }
6995
6996 // Implements fast-unlocking.
6997 //
6998 // - obj: the object to be unlocked
6999 // - t1, t2, t3: temporary registers
7000 // - slow: branched to if unlocking fails, absolute offset may larger than 32KB (imm14 encoding).
7001 void MacroAssembler::fast_unlock(Register obj, Register t1, Register t2, Register t3, Label& slow) {
7002 // cmpxchg clobbers rscratch1.
7003 assert_different_registers(obj, t1, t2, t3, rscratch1);
7004
7005 #ifdef ASSERT
7006 {
7007 // Check for lock-stack underflow.
7008 Label stack_ok;
7009 ldrw(t1, Address(rthread, JavaThread::lock_stack_top_offset()));
7010 cmpw(t1, (unsigned)LockStack::start_offset());
7011 br(Assembler::GE, stack_ok);
7012 STOP("Lock-stack underflow");
7013 bind(stack_ok);
7014 }
7015 #endif
7016
7017 Label unlocked, push_and_slow;
7018 const Register top = t1;
7019 const Register mark = t2;
7020 const Register t = t3;
7021
7022 // Check if obj is top of lock-stack.
7023 ldrw(top, Address(rthread, JavaThread::lock_stack_top_offset()));
7024 subw(top, top, oopSize);
7025 ldr(t, Address(rthread, top));
7026 cmp(obj, t);
7027 br(Assembler::NE, slow);
7028
7029 // Pop lock-stack.
7030 DEBUG_ONLY(str(zr, Address(rthread, top));)
7031 strw(top, Address(rthread, JavaThread::lock_stack_top_offset()));
7032
7033 // Check if recursive.
7034 subw(t, top, oopSize);
7035 ldr(t, Address(rthread, t));
7036 cmp(obj, t);
7037 br(Assembler::EQ, unlocked);
7038
7039 // Not recursive. Check header for monitor (0b10).
7040 ldr(mark, Address(obj, oopDesc::mark_offset_in_bytes()));
7041 tbnz(mark, log2i_exact(markWord::monitor_value), push_and_slow);
7042
7043 #ifdef ASSERT
7044 // Check header not unlocked (0b01).
7045 Label not_unlocked;
7046 tbz(mark, log2i_exact(markWord::unlocked_value), not_unlocked);
7047 stop("fast_unlock already unlocked");
7048 bind(not_unlocked);
7049 #endif
7050
7051 // Try to unlock. Transition lock bits 0b00 => 0b01
7052 assert(oopDesc::mark_offset_in_bytes() == 0, "required to avoid lea");
7053 orr(t, mark, markWord::unlocked_value);
7054 cmpxchg(obj, mark, t, Assembler::xword,
7055 /*acquire*/ false, /*release*/ true, /*weak*/ false, noreg);
7056 br(Assembler::EQ, unlocked);
7057
7058 bind(push_and_slow);
7059 // Restore lock-stack and handle the unlock in runtime.
7060 DEBUG_ONLY(str(obj, Address(rthread, top));)
7061 addw(top, top, oopSize);
7062 strw(top, Address(rthread, JavaThread::lock_stack_top_offset()));
7063 b(slow);
7064
7065 bind(unlocked);
7066 }