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