9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #include "asm/assembler.hpp"
27 #include "asm/assembler.inline.hpp"
28 #include "ci/ciEnv.hpp"
29 #include "code/compiledIC.hpp"
30 #include "compiler/compileTask.hpp"
31 #include "compiler/disassembler.hpp"
32 #include "compiler/oopMap.hpp"
33 #include "gc/shared/barrierSet.hpp"
34 #include "gc/shared/barrierSetAssembler.hpp"
35 #include "gc/shared/cardTableBarrierSet.hpp"
36 #include "gc/shared/cardTable.hpp"
37 #include "gc/shared/collectedHeap.hpp"
38 #include "gc/shared/tlab_globals.hpp"
39 #include "interpreter/bytecodeHistogram.hpp"
40 #include "interpreter/interpreter.hpp"
41 #include "interpreter/interpreterRuntime.hpp"
42 #include "jvm.h"
43 #include "memory/resourceArea.hpp"
44 #include "memory/universe.hpp"
45 #include "nativeInst_aarch64.hpp"
46 #include "oops/accessDecorators.hpp"
47 #include "oops/compressedKlass.inline.hpp"
48 #include "oops/compressedOops.inline.hpp"
49 #include "oops/klass.inline.hpp"
50 #include "runtime/continuation.hpp"
51 #include "runtime/icache.hpp"
52 #include "runtime/interfaceSupport.inline.hpp"
53 #include "runtime/javaThread.hpp"
54 #include "runtime/jniHandles.inline.hpp"
55 #include "runtime/sharedRuntime.hpp"
56 #include "runtime/stubRoutines.hpp"
57 #include "utilities/globalDefinitions.hpp"
58 #include "utilities/powerOfTwo.hpp"
59 #ifdef COMPILER1
60 #include "c1/c1_LIRAssembler.hpp"
61 #endif
62 #ifdef COMPILER2
63 #include "oops/oop.hpp"
64 #include "opto/compile.hpp"
65 #include "opto/node.hpp"
66 #include "opto/output.hpp"
67 #endif
68
69 #include <sys/types.h>
70
71 #ifdef PRODUCT
72 #define BLOCK_COMMENT(str) /* nothing */
73 #else
74 #define BLOCK_COMMENT(str) block_comment(str)
75 #endif
76 #define STOP(str) stop(str);
77 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
78
2011 ldarb(scratch, scratch);
2012 cmp(scratch, InstanceKlass::fully_initialized);
2013 br(Assembler::EQ, *L_fast_path);
2014
2015 // Fast path check: current thread is initializer thread
2016 ldr(scratch, Address(klass, InstanceKlass::init_thread_offset()));
2017 cmp(rthread, scratch);
2018
2019 if (L_slow_path == &L_fallthrough) {
2020 br(Assembler::EQ, *L_fast_path);
2021 bind(*L_slow_path);
2022 } else if (L_fast_path == &L_fallthrough) {
2023 br(Assembler::NE, *L_slow_path);
2024 bind(*L_fast_path);
2025 } else {
2026 Unimplemented();
2027 }
2028 }
2029
2030 void MacroAssembler::_verify_oop(Register reg, const char* s, const char* file, int line) {
2031 if (!VerifyOops) return;
2032
2033 // Pass register number to verify_oop_subroutine
2034 const char* b = nullptr;
2035 {
2036 ResourceMark rm;
2037 stringStream ss;
2038 ss.print("verify_oop: %s: %s (%s:%d)", reg->name(), s, file, line);
2039 b = code_string(ss.as_string());
2040 }
2041 BLOCK_COMMENT("verify_oop {");
2042
2043 strip_return_address(); // This might happen within a stack frame.
2044 protect_return_address();
2045 stp(r0, rscratch1, Address(pre(sp, -2 * wordSize)));
2046 stp(rscratch2, lr, Address(pre(sp, -2 * wordSize)));
2047
2048 mov(r0, reg);
2049 movptr(rscratch1, (uintptr_t)(address)b);
2050
2051 // call indirectly to solve generation ordering problem
2052 lea(rscratch2, RuntimeAddress(StubRoutines::verify_oop_subroutine_entry_address()));
2053 ldr(rscratch2, Address(rscratch2));
2054 blr(rscratch2);
2055
2056 ldp(rscratch2, lr, Address(post(sp, 2 * wordSize)));
2057 ldp(r0, rscratch1, Address(post(sp, 2 * wordSize)));
2058 authenticate_return_address();
2059
2060 BLOCK_COMMENT("} verify_oop");
2061 }
2062
2063 void MacroAssembler::_verify_oop_addr(Address addr, const char* s, const char* file, int line) {
2064 if (!VerifyOops) return;
2065
2066 const char* b = nullptr;
2067 {
2068 ResourceMark rm;
2069 stringStream ss;
2070 ss.print("verify_oop_addr: %s (%s:%d)", s, file, line);
2071 b = code_string(ss.as_string());
2072 }
2073 BLOCK_COMMENT("verify_oop_addr {");
2074
2075 strip_return_address(); // This might happen within a stack frame.
2076 protect_return_address();
2077 stp(r0, rscratch1, Address(pre(sp, -2 * wordSize)));
2078 stp(rscratch2, lr, Address(pre(sp, -2 * wordSize)));
2079
2080 // addr may contain sp so we will have to adjust it based on the
2081 // pushes that we just did.
2082 if (addr.uses(sp)) {
2083 lea(r0, addr);
2084 ldr(r0, Address(r0, 4 * wordSize));
2142 call_VM_leaf_base(entry_point, 1);
2143 }
2144
2145 void MacroAssembler::call_VM_leaf(address entry_point, Register arg_0, Register arg_1) {
2146 assert_different_registers(arg_1, c_rarg0);
2147 pass_arg0(this, arg_0);
2148 pass_arg1(this, arg_1);
2149 call_VM_leaf_base(entry_point, 2);
2150 }
2151
2152 void MacroAssembler::call_VM_leaf(address entry_point, Register arg_0,
2153 Register arg_1, Register arg_2) {
2154 assert_different_registers(arg_1, c_rarg0);
2155 assert_different_registers(arg_2, c_rarg0, c_rarg1);
2156 pass_arg0(this, arg_0);
2157 pass_arg1(this, arg_1);
2158 pass_arg2(this, arg_2);
2159 call_VM_leaf_base(entry_point, 3);
2160 }
2161
2162 void MacroAssembler::super_call_VM_leaf(address entry_point, Register arg_0) {
2163 pass_arg0(this, arg_0);
2164 MacroAssembler::call_VM_leaf_base(entry_point, 1);
2165 }
2166
2167 void MacroAssembler::super_call_VM_leaf(address entry_point, Register arg_0, Register arg_1) {
2168
2169 assert_different_registers(arg_0, c_rarg1);
2170 pass_arg1(this, arg_1);
2171 pass_arg0(this, arg_0);
2172 MacroAssembler::call_VM_leaf_base(entry_point, 2);
2173 }
2174
2175 void MacroAssembler::super_call_VM_leaf(address entry_point, Register arg_0, Register arg_1, Register arg_2) {
2176 assert_different_registers(arg_0, c_rarg1, c_rarg2);
2177 assert_different_registers(arg_1, c_rarg2);
2178 pass_arg2(this, arg_2);
2179 pass_arg1(this, arg_1);
2180 pass_arg0(this, arg_0);
2181 MacroAssembler::call_VM_leaf_base(entry_point, 3);
2187 assert_different_registers(arg_2, c_rarg3);
2188 pass_arg3(this, arg_3);
2189 pass_arg2(this, arg_2);
2190 pass_arg1(this, arg_1);
2191 pass_arg0(this, arg_0);
2192 MacroAssembler::call_VM_leaf_base(entry_point, 4);
2193 }
2194
2195 void MacroAssembler::null_check(Register reg, int offset) {
2196 if (needs_explicit_null_check(offset)) {
2197 // provoke OS null exception if reg is null by
2198 // accessing M[reg] w/o changing any registers
2199 // NOTE: this is plenty to provoke a segv
2200 ldr(zr, Address(reg));
2201 } else {
2202 // nothing to do, (later) access of M[reg + offset]
2203 // will provoke OS null exception if reg is null
2204 }
2205 }
2206
2207 // MacroAssembler protected routines needed to implement
2208 // public methods
2209
2210 void MacroAssembler::mov(Register r, Address dest) {
2211 code_section()->relocate(pc(), dest.rspec());
2212 uint64_t imm64 = (uint64_t)dest.target();
2213 movptr(r, imm64);
2214 }
2215
2216 // Move a constant pointer into r. In AArch64 mode the virtual
2217 // address space is 48 bits in size, so we only need three
2218 // instructions to create a patchable instruction sequence that can
2219 // reach anywhere.
2220 void MacroAssembler::movptr(Register r, uintptr_t imm64) {
2221 #ifndef PRODUCT
2222 {
2223 char buffer[64];
2224 os::snprintf_checked(buffer, sizeof(buffer), "0x%" PRIX64, (uint64_t)imm64);
2225 block_comment(buffer);
2226 }
4887 adrp(rscratch1, src2, offset);
4888 ldr(rscratch1, Address(rscratch1, offset));
4889 cmp(src1, rscratch1);
4890 }
4891
4892 void MacroAssembler::cmpoop(Register obj1, Register obj2) {
4893 cmp(obj1, obj2);
4894 }
4895
4896 void MacroAssembler::load_method_holder_cld(Register rresult, Register rmethod) {
4897 load_method_holder(rresult, rmethod);
4898 ldr(rresult, Address(rresult, InstanceKlass::class_loader_data_offset()));
4899 }
4900
4901 void MacroAssembler::load_method_holder(Register holder, Register method) {
4902 ldr(holder, Address(method, Method::const_offset())); // ConstMethod*
4903 ldr(holder, Address(holder, ConstMethod::constants_offset())); // ConstantPool*
4904 ldr(holder, Address(holder, ConstantPool::pool_holder_offset())); // InstanceKlass*
4905 }
4906
4907 // Loads the obj's Klass* into dst.
4908 // Preserves all registers (incl src, rscratch1 and rscratch2).
4909 // Input:
4910 // src - the oop we want to load the klass from.
4911 // dst - output narrow klass.
4912 void MacroAssembler::load_narrow_klass_compact(Register dst, Register src) {
4913 assert(UseCompactObjectHeaders, "expects UseCompactObjectHeaders");
4914 ldr(dst, Address(src, oopDesc::mark_offset_in_bytes()));
4915 lsr(dst, dst, markWord::klass_shift);
4916 }
4917
4918 void MacroAssembler::load_klass(Register dst, Register src) {
4919 if (UseCompactObjectHeaders) {
4920 load_narrow_klass_compact(dst, src);
4921 decode_klass_not_null(dst);
4922 } else if (UseCompressedClassPointers) {
4923 ldrw(dst, Address(src, oopDesc::klass_offset_in_bytes()));
4924 decode_klass_not_null(dst);
4925 } else {
4926 ldr(dst, Address(src, oopDesc::klass_offset_in_bytes()));
4997 }
4998 cmp(klass, tmp);
4999 }
5000
5001 void MacroAssembler::cmp_klasses_from_objects(Register obj1, Register obj2, Register tmp1, Register tmp2) {
5002 if (UseCompactObjectHeaders) {
5003 load_narrow_klass_compact(tmp1, obj1);
5004 load_narrow_klass_compact(tmp2, obj2);
5005 cmpw(tmp1, tmp2);
5006 } else if (UseCompressedClassPointers) {
5007 ldrw(tmp1, Address(obj1, oopDesc::klass_offset_in_bytes()));
5008 ldrw(tmp2, Address(obj2, oopDesc::klass_offset_in_bytes()));
5009 cmpw(tmp1, tmp2);
5010 } else {
5011 ldr(tmp1, Address(obj1, oopDesc::klass_offset_in_bytes()));
5012 ldr(tmp2, Address(obj2, oopDesc::klass_offset_in_bytes()));
5013 cmp(tmp1, tmp2);
5014 }
5015 }
5016
5017 void MacroAssembler::store_klass(Register dst, Register src) {
5018 // FIXME: Should this be a store release? concurrent gcs assumes
5019 // klass length is valid if klass field is not null.
5020 assert(!UseCompactObjectHeaders, "not with compact headers");
5021 if (UseCompressedClassPointers) {
5022 encode_klass_not_null(src);
5023 strw(src, Address(dst, oopDesc::klass_offset_in_bytes()));
5024 } else {
5025 str(src, Address(dst, oopDesc::klass_offset_in_bytes()));
5026 }
5027 }
5028
5029 void MacroAssembler::store_klass_gap(Register dst, Register src) {
5030 assert(!UseCompactObjectHeaders, "not with compact headers");
5031 if (UseCompressedClassPointers) {
5032 // Store to klass gap in destination
5033 strw(src, Address(dst, oopDesc::klass_gap_offset_in_bytes()));
5034 }
5035 }
5036
5398 if (as_raw) {
5399 bs->BarrierSetAssembler::load_at(this, decorators, type, dst, src, tmp1, tmp2);
5400 } else {
5401 bs->load_at(this, decorators, type, dst, src, tmp1, tmp2);
5402 }
5403 }
5404
5405 void MacroAssembler::access_store_at(BasicType type, DecoratorSet decorators,
5406 Address dst, Register val,
5407 Register tmp1, Register tmp2, Register tmp3) {
5408 BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
5409 decorators = AccessInternal::decorator_fixup(decorators, type);
5410 bool as_raw = (decorators & AS_RAW) != 0;
5411 if (as_raw) {
5412 bs->BarrierSetAssembler::store_at(this, decorators, type, dst, val, tmp1, tmp2, tmp3);
5413 } else {
5414 bs->store_at(this, decorators, type, dst, val, tmp1, tmp2, tmp3);
5415 }
5416 }
5417
5418 void MacroAssembler::load_heap_oop(Register dst, Address src, Register tmp1,
5419 Register tmp2, DecoratorSet decorators) {
5420 access_load_at(T_OBJECT, IN_HEAP | decorators, dst, src, tmp1, tmp2);
5421 }
5422
5423 void MacroAssembler::load_heap_oop_not_null(Register dst, Address src, Register tmp1,
5424 Register tmp2, DecoratorSet decorators) {
5425 access_load_at(T_OBJECT, IN_HEAP | IS_NOT_NULL | decorators, dst, src, tmp1, tmp2);
5426 }
5427
5428 void MacroAssembler::store_heap_oop(Address dst, Register val, Register tmp1,
5429 Register tmp2, Register tmp3, DecoratorSet decorators) {
5430 access_store_at(T_OBJECT, IN_HEAP | decorators, dst, val, tmp1, tmp2, tmp3);
5431 }
5432
5433 // Used for storing nulls.
5434 void MacroAssembler::store_heap_oop_null(Address dst) {
5435 access_store_at(T_OBJECT, IN_HEAP, dst, noreg, noreg, noreg, noreg);
5436 }
5437
5473 oop_index = oop_recorder()->allocate_metadata_index(obj);
5474 } else {
5475 oop_index = oop_recorder()->find_index(obj);
5476 }
5477 RelocationHolder rspec = metadata_Relocation::spec(oop_index);
5478 mov(dst, Address((address)obj, rspec));
5479 }
5480
5481 Address MacroAssembler::constant_oop_address(jobject obj) {
5482 #ifdef ASSERT
5483 {
5484 ThreadInVMfromUnknown tiv;
5485 assert(oop_recorder() != nullptr, "this assembler needs an OopRecorder");
5486 assert(Universe::heap()->is_in(JNIHandles::resolve(obj)), "not an oop");
5487 }
5488 #endif
5489 int oop_index = oop_recorder()->find_index(obj);
5490 return Address((address)obj, oop_Relocation::spec(oop_index));
5491 }
5492
5493 // Defines obj, preserves var_size_in_bytes, okay for t2 == var_size_in_bytes.
5494 void MacroAssembler::tlab_allocate(Register obj,
5495 Register var_size_in_bytes,
5496 int con_size_in_bytes,
5497 Register t1,
5498 Register t2,
5499 Label& slow_case) {
5500 BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
5501 bs->tlab_allocate(this, obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
5502 }
5503
5504 void MacroAssembler::verify_tlab() {
5505 #ifdef ASSERT
5506 if (UseTLAB && VerifyOops) {
5507 Label next, ok;
5508
5509 stp(rscratch2, rscratch1, Address(pre(sp, -16)));
5510
5511 ldr(rscratch2, Address(rthread, in_bytes(JavaThread::tlab_top_offset())));
5512 ldr(rscratch1, Address(rthread, in_bytes(JavaThread::tlab_start_offset())));
5513 cmp(rscratch2, rscratch1);
5514 br(Assembler::HS, next);
5515 STOP("assert(top >= start)");
5516 should_not_reach_here();
5517
5518 bind(next);
5519 ldr(rscratch2, Address(rthread, in_bytes(JavaThread::tlab_end_offset())));
5520 ldr(rscratch1, Address(rthread, in_bytes(JavaThread::tlab_top_offset())));
5521 cmp(rscratch2, rscratch1);
5522 br(Assembler::HS, ok);
5523 STOP("assert(top <= end)");
5524 should_not_reach_here();
5525
5526 bind(ok);
5527 ldp(rscratch2, rscratch1, Address(post(sp, 16)));
5528 }
5529 #endif
5530 }
5531
5532 // Writes to stack successive pages until offset reached to check for
5533 // stack overflow + shadow pages. This clobbers tmp.
5534 void MacroAssembler::bang_stack_size(Register size, Register tmp) {
5535 assert_different_registers(tmp, size, rscratch1);
5536 mov(tmp, sp);
5537 // Bang stack for total size given plus shadow page size.
5538 // Bang one page at a time because large size can bang beyond yellow and
5539 // red zones.
5540 Label loop;
5541 mov(rscratch1, (int)os::vm_page_size());
5542 bind(loop);
5543 lea(tmp, Address(tmp, -(int)os::vm_page_size()));
5544 subsw(size, size, rscratch1);
5545 str(size, Address(tmp));
5546 br(Assembler::GT, loop);
5547
5548 // Bang down shadow pages too.
5549 // At this point, (tmp-0) is the last address touched, so don't
5550 // touch it again. (It was touched as (tmp-pagesize) but then tmp
5551 // was post-decremented.) Skip this address by starting at i=1, and
5637 }
5638
5639 void MacroAssembler::remove_frame(int framesize) {
5640 assert(framesize >= 2 * wordSize, "framesize must include space for FP/LR");
5641 assert(framesize % (2*wordSize) == 0, "must preserve 2*wordSize alignment");
5642 if (framesize < ((1 << 9) + 2 * wordSize)) {
5643 ldp(rfp, lr, Address(sp, framesize - 2 * wordSize));
5644 add(sp, sp, framesize);
5645 } else {
5646 if (framesize < ((1 << 12) + 2 * wordSize))
5647 add(sp, sp, framesize - 2 * wordSize);
5648 else {
5649 mov(rscratch1, framesize - 2 * wordSize);
5650 add(sp, sp, rscratch1);
5651 }
5652 ldp(rfp, lr, Address(post(sp, 2 * wordSize)));
5653 }
5654 authenticate_return_address();
5655 }
5656
5657
5658 // This method counts leading positive bytes (highest bit not set) in provided byte array
5659 address MacroAssembler::count_positives(Register ary1, Register len, Register result) {
5660 // Simple and most common case of aligned small array which is not at the
5661 // end of memory page is placed here. All other cases are in stub.
5662 Label LOOP, END, STUB, STUB_LONG, SET_RESULT, DONE;
5663 const uint64_t UPPER_BIT_MASK=0x8080808080808080;
5664 assert_different_registers(ary1, len, result);
5665
5666 mov(result, len);
5667 cmpw(len, 0);
5668 br(LE, DONE);
5669 cmpw(len, 4 * wordSize);
5670 br(GE, STUB_LONG); // size > 32 then go to stub
5671
5672 int shift = 64 - exact_log2(os::vm_page_size());
5673 lsl(rscratch1, ary1, shift);
5674 mov(rscratch2, (size_t)(4 * wordSize) << shift);
5675 adds(rscratch2, rscratch1, rscratch2); // At end of page?
5676 br(CS, STUB); // at the end of page then go to stub
6554 // On other systems, the helper is a usual C function.
6555 //
6556 void MacroAssembler::get_thread(Register dst) {
6557 RegSet saved_regs =
6558 LINUX_ONLY(RegSet::range(r0, r1) + lr - dst)
6559 NOT_LINUX (RegSet::range(r0, r17) + lr - dst);
6560
6561 protect_return_address();
6562 push(saved_regs, sp);
6563
6564 mov(lr, ExternalAddress(CAST_FROM_FN_PTR(address, JavaThread::aarch64_get_thread_helper)));
6565 blr(lr);
6566 if (dst != c_rarg0) {
6567 mov(dst, c_rarg0);
6568 }
6569
6570 pop(saved_regs, sp);
6571 authenticate_return_address();
6572 }
6573
6574 void MacroAssembler::cache_wb(Address line) {
6575 assert(line.getMode() == Address::base_plus_offset, "mode should be base_plus_offset");
6576 assert(line.index() == noreg, "index should be noreg");
6577 assert(line.offset() == 0, "offset should be 0");
6578 // would like to assert this
6579 // assert(line._ext.shift == 0, "shift should be zero");
6580 if (VM_Version::supports_dcpop()) {
6581 // writeback using clear virtual address to point of persistence
6582 dc(Assembler::CVAP, line.base());
6583 } else {
6584 // no need to generate anything as Unsafe.writebackMemory should
6585 // never invoke this stub
6586 }
6587 }
6588
6589 void MacroAssembler::cache_wbsync(bool is_pre) {
6590 // we only need a barrier post sync
6591 if (!is_pre) {
6592 membar(Assembler::AnyAny);
6593 }
6964 }
6965
6966 // Check if the lock-stack is full.
6967 ldrw(top, Address(rthread, JavaThread::lock_stack_top_offset()));
6968 cmpw(top, (unsigned)LockStack::end_offset());
6969 br(Assembler::GE, slow);
6970
6971 // Check for recursion.
6972 subw(t, top, oopSize);
6973 ldr(t, Address(rthread, t));
6974 cmp(obj, t);
6975 br(Assembler::EQ, push);
6976
6977 // Check header for monitor (0b10).
6978 tst(mark, markWord::monitor_value);
6979 br(Assembler::NE, slow);
6980
6981 // Try to lock. Transition lock bits 0b01 => 0b00
6982 assert(oopDesc::mark_offset_in_bytes() == 0, "required to avoid lea");
6983 orr(mark, mark, markWord::unlocked_value);
6984 eor(t, mark, markWord::unlocked_value);
6985 cmpxchg(/*addr*/ obj, /*expected*/ mark, /*new*/ t, Assembler::xword,
6986 /*acquire*/ true, /*release*/ false, /*weak*/ false, noreg);
6987 br(Assembler::NE, slow);
6988
6989 bind(push);
6990 // After successful lock, push object on lock-stack.
6991 str(obj, Address(rthread, top));
6992 addw(top, top, oopSize);
6993 strw(top, Address(rthread, JavaThread::lock_stack_top_offset()));
6994 }
6995
6996 // Implements lightweight-unlocking.
6997 //
6998 // - obj: the object to be unlocked
6999 // - t1, t2, t3: temporary registers
7000 // - slow: branched to if unlocking fails, absolute offset may larger than 32KB (imm14 encoding).
7001 void MacroAssembler::lightweight_unlock(Register obj, Register t1, Register t2, Register t3, Label& slow) {
7002 // cmpxchg clobbers rscratch1.
7003 assert_different_registers(obj, t1, t2, t3, rscratch1);
|
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 "ci/ciInlineKlass.hpp"
30 #include "code/compiledIC.hpp"
31 #include "compiler/compileTask.hpp"
32 #include "compiler/disassembler.hpp"
33 #include "compiler/oopMap.hpp"
34 #include "gc/shared/barrierSet.hpp"
35 #include "gc/shared/barrierSetAssembler.hpp"
36 #include "gc/shared/cardTableBarrierSet.hpp"
37 #include "gc/shared/cardTable.hpp"
38 #include "gc/shared/collectedHeap.hpp"
39 #include "gc/shared/tlab_globals.hpp"
40 #include "interpreter/bytecodeHistogram.hpp"
41 #include "interpreter/interpreter.hpp"
42 #include "interpreter/interpreterRuntime.hpp"
43 #include "jvm.h"
44 #include "memory/resourceArea.hpp"
45 #include "memory/universe.hpp"
46 #include "nativeInst_aarch64.hpp"
47 #include "oops/accessDecorators.hpp"
48 #include "oops/compressedKlass.inline.hpp"
49 #include "oops/compressedOops.inline.hpp"
50 #include "oops/klass.inline.hpp"
51 #include "oops/resolvedFieldEntry.hpp"
52 #include "runtime/continuation.hpp"
53 #include "runtime/globals.hpp"
54 #include "runtime/icache.hpp"
55 #include "runtime/interfaceSupport.inline.hpp"
56 #include "runtime/javaThread.hpp"
57 #include "runtime/jniHandles.inline.hpp"
58 #include "runtime/sharedRuntime.hpp"
59 #include "runtime/signature_cc.hpp"
60 #include "runtime/stubRoutines.hpp"
61 #include "utilities/globalDefinitions.hpp"
62 #include "utilities/powerOfTwo.hpp"
63 #include "vmreg_aarch64.inline.hpp"
64 #ifdef COMPILER1
65 #include "c1/c1_LIRAssembler.hpp"
66 #endif
67 #ifdef COMPILER2
68 #include "oops/oop.hpp"
69 #include "opto/compile.hpp"
70 #include "opto/node.hpp"
71 #include "opto/output.hpp"
72 #endif
73
74 #include <sys/types.h>
75
76 #ifdef PRODUCT
77 #define BLOCK_COMMENT(str) /* nothing */
78 #else
79 #define BLOCK_COMMENT(str) block_comment(str)
80 #endif
81 #define STOP(str) stop(str);
82 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
83
2016 ldarb(scratch, scratch);
2017 cmp(scratch, InstanceKlass::fully_initialized);
2018 br(Assembler::EQ, *L_fast_path);
2019
2020 // Fast path check: current thread is initializer thread
2021 ldr(scratch, Address(klass, InstanceKlass::init_thread_offset()));
2022 cmp(rthread, scratch);
2023
2024 if (L_slow_path == &L_fallthrough) {
2025 br(Assembler::EQ, *L_fast_path);
2026 bind(*L_slow_path);
2027 } else if (L_fast_path == &L_fallthrough) {
2028 br(Assembler::NE, *L_slow_path);
2029 bind(*L_fast_path);
2030 } else {
2031 Unimplemented();
2032 }
2033 }
2034
2035 void MacroAssembler::_verify_oop(Register reg, const char* s, const char* file, int line) {
2036 if (!VerifyOops || VerifyAdapterSharing) {
2037 // Below address of the code string confuses VerifyAdapterSharing
2038 // because it may differ between otherwise equivalent adapters.
2039 return;
2040 }
2041
2042 // Pass register number to verify_oop_subroutine
2043 const char* b = nullptr;
2044 {
2045 ResourceMark rm;
2046 stringStream ss;
2047 ss.print("verify_oop: %s: %s (%s:%d)", reg->name(), s, file, line);
2048 b = code_string(ss.as_string());
2049 }
2050 BLOCK_COMMENT("verify_oop {");
2051
2052 strip_return_address(); // This might happen within a stack frame.
2053 protect_return_address();
2054 stp(r0, rscratch1, Address(pre(sp, -2 * wordSize)));
2055 stp(rscratch2, lr, Address(pre(sp, -2 * wordSize)));
2056
2057 mov(r0, reg);
2058 movptr(rscratch1, (uintptr_t)(address)b);
2059
2060 // call indirectly to solve generation ordering problem
2061 lea(rscratch2, RuntimeAddress(StubRoutines::verify_oop_subroutine_entry_address()));
2062 ldr(rscratch2, Address(rscratch2));
2063 blr(rscratch2);
2064
2065 ldp(rscratch2, lr, Address(post(sp, 2 * wordSize)));
2066 ldp(r0, rscratch1, Address(post(sp, 2 * wordSize)));
2067 authenticate_return_address();
2068
2069 BLOCK_COMMENT("} verify_oop");
2070 }
2071
2072 void MacroAssembler::_verify_oop_addr(Address addr, const char* s, const char* file, int line) {
2073 if (!VerifyOops || VerifyAdapterSharing) {
2074 // Below address of the code string confuses VerifyAdapterSharing
2075 // because it may differ between otherwise equivalent adapters.
2076 return;
2077 }
2078
2079 const char* b = nullptr;
2080 {
2081 ResourceMark rm;
2082 stringStream ss;
2083 ss.print("verify_oop_addr: %s (%s:%d)", s, file, line);
2084 b = code_string(ss.as_string());
2085 }
2086 BLOCK_COMMENT("verify_oop_addr {");
2087
2088 strip_return_address(); // This might happen within a stack frame.
2089 protect_return_address();
2090 stp(r0, rscratch1, Address(pre(sp, -2 * wordSize)));
2091 stp(rscratch2, lr, Address(pre(sp, -2 * wordSize)));
2092
2093 // addr may contain sp so we will have to adjust it based on the
2094 // pushes that we just did.
2095 if (addr.uses(sp)) {
2096 lea(r0, addr);
2097 ldr(r0, Address(r0, 4 * wordSize));
2155 call_VM_leaf_base(entry_point, 1);
2156 }
2157
2158 void MacroAssembler::call_VM_leaf(address entry_point, Register arg_0, Register arg_1) {
2159 assert_different_registers(arg_1, c_rarg0);
2160 pass_arg0(this, arg_0);
2161 pass_arg1(this, arg_1);
2162 call_VM_leaf_base(entry_point, 2);
2163 }
2164
2165 void MacroAssembler::call_VM_leaf(address entry_point, Register arg_0,
2166 Register arg_1, Register arg_2) {
2167 assert_different_registers(arg_1, c_rarg0);
2168 assert_different_registers(arg_2, c_rarg0, c_rarg1);
2169 pass_arg0(this, arg_0);
2170 pass_arg1(this, arg_1);
2171 pass_arg2(this, arg_2);
2172 call_VM_leaf_base(entry_point, 3);
2173 }
2174
2175 void MacroAssembler::super_call_VM_leaf(address entry_point) {
2176 MacroAssembler::call_VM_leaf_base(entry_point, 1);
2177 }
2178
2179 void MacroAssembler::super_call_VM_leaf(address entry_point, Register arg_0) {
2180 pass_arg0(this, arg_0);
2181 MacroAssembler::call_VM_leaf_base(entry_point, 1);
2182 }
2183
2184 void MacroAssembler::super_call_VM_leaf(address entry_point, Register arg_0, Register arg_1) {
2185
2186 assert_different_registers(arg_0, c_rarg1);
2187 pass_arg1(this, arg_1);
2188 pass_arg0(this, arg_0);
2189 MacroAssembler::call_VM_leaf_base(entry_point, 2);
2190 }
2191
2192 void MacroAssembler::super_call_VM_leaf(address entry_point, Register arg_0, Register arg_1, Register arg_2) {
2193 assert_different_registers(arg_0, c_rarg1, c_rarg2);
2194 assert_different_registers(arg_1, c_rarg2);
2195 pass_arg2(this, arg_2);
2196 pass_arg1(this, arg_1);
2197 pass_arg0(this, arg_0);
2198 MacroAssembler::call_VM_leaf_base(entry_point, 3);
2204 assert_different_registers(arg_2, c_rarg3);
2205 pass_arg3(this, arg_3);
2206 pass_arg2(this, arg_2);
2207 pass_arg1(this, arg_1);
2208 pass_arg0(this, arg_0);
2209 MacroAssembler::call_VM_leaf_base(entry_point, 4);
2210 }
2211
2212 void MacroAssembler::null_check(Register reg, int offset) {
2213 if (needs_explicit_null_check(offset)) {
2214 // provoke OS null exception if reg is null by
2215 // accessing M[reg] w/o changing any registers
2216 // NOTE: this is plenty to provoke a segv
2217 ldr(zr, Address(reg));
2218 } else {
2219 // nothing to do, (later) access of M[reg + offset]
2220 // will provoke OS null exception if reg is null
2221 }
2222 }
2223
2224 void MacroAssembler::test_markword_is_inline_type(Register markword, Label& is_inline_type) {
2225 assert_different_registers(markword, rscratch2);
2226 mov(rscratch2, markWord::inline_type_mask_in_place);
2227 andr(markword, markword, rscratch2);
2228 mov(rscratch2, markWord::inline_type_pattern);
2229 cmp(markword, rscratch2);
2230 br(Assembler::EQ, is_inline_type);
2231 }
2232
2233 void MacroAssembler::test_oop_is_not_inline_type(Register object, Register tmp, Label& not_inline_type, bool can_be_null) {
2234 assert_different_registers(tmp, rscratch1);
2235 if (can_be_null) {
2236 cbz(object, not_inline_type);
2237 }
2238 const int is_inline_type_mask = markWord::inline_type_pattern;
2239 ldr(tmp, Address(object, oopDesc::mark_offset_in_bytes()));
2240 mov(rscratch1, is_inline_type_mask);
2241 andr(tmp, tmp, rscratch1);
2242 cmp(tmp, rscratch1);
2243 br(Assembler::NE, not_inline_type);
2244 }
2245
2246 void MacroAssembler::test_field_is_null_free_inline_type(Register flags, Register temp_reg, Label& is_null_free_inline_type) {
2247 assert(temp_reg == noreg, "not needed"); // keep signature uniform with x86
2248 tbnz(flags, ResolvedFieldEntry::is_null_free_inline_type_shift, is_null_free_inline_type);
2249 }
2250
2251 void MacroAssembler::test_field_is_not_null_free_inline_type(Register flags, Register temp_reg, Label& not_null_free_inline_type) {
2252 assert(temp_reg == noreg, "not needed"); // keep signature uniform with x86
2253 tbz(flags, ResolvedFieldEntry::is_null_free_inline_type_shift, not_null_free_inline_type);
2254 }
2255
2256 void MacroAssembler::test_field_is_flat(Register flags, Register temp_reg, Label& is_flat) {
2257 assert(temp_reg == noreg, "not needed"); // keep signature uniform with x86
2258 tbnz(flags, ResolvedFieldEntry::is_flat_shift, is_flat);
2259 }
2260
2261 void MacroAssembler::test_field_has_null_marker(Register flags, Register temp_reg, Label& has_null_marker) {
2262 assert(temp_reg == noreg, "not needed"); // keep signature uniform with x86
2263 tbnz(flags, ResolvedFieldEntry::has_null_marker_shift, has_null_marker);
2264 }
2265
2266 void MacroAssembler::test_oop_prototype_bit(Register oop, Register temp_reg, int32_t test_bit, bool jmp_set, Label& jmp_label) {
2267 Label test_mark_word;
2268 // load mark word
2269 ldr(temp_reg, Address(oop, oopDesc::mark_offset_in_bytes()));
2270 // check displaced
2271 tst(temp_reg, markWord::unlocked_value);
2272 br(Assembler::NE, test_mark_word);
2273 // slow path use klass prototype
2274 load_prototype_header(temp_reg, oop);
2275
2276 bind(test_mark_word);
2277 andr(temp_reg, temp_reg, test_bit);
2278 if (jmp_set) {
2279 cbnz(temp_reg, jmp_label);
2280 } else {
2281 cbz(temp_reg, jmp_label);
2282 }
2283 }
2284
2285 void MacroAssembler::test_flat_array_oop(Register oop, Register temp_reg, Label& is_flat_array) {
2286 test_oop_prototype_bit(oop, temp_reg, markWord::flat_array_bit_in_place, true, is_flat_array);
2287 }
2288
2289 void MacroAssembler::test_non_flat_array_oop(Register oop, Register temp_reg,
2290 Label&is_non_flat_array) {
2291 test_oop_prototype_bit(oop, temp_reg, markWord::flat_array_bit_in_place, false, is_non_flat_array);
2292 }
2293
2294 void MacroAssembler::test_null_free_array_oop(Register oop, Register temp_reg, Label& is_null_free_array) {
2295 test_oop_prototype_bit(oop, temp_reg, markWord::null_free_array_bit_in_place, true, is_null_free_array);
2296 }
2297
2298 void MacroAssembler::test_non_null_free_array_oop(Register oop, Register temp_reg, Label&is_non_null_free_array) {
2299 test_oop_prototype_bit(oop, temp_reg, markWord::null_free_array_bit_in_place, false, is_non_null_free_array);
2300 }
2301
2302 void MacroAssembler::test_flat_array_layout(Register lh, Label& is_flat_array) {
2303 tst(lh, Klass::_lh_array_tag_flat_value_bit_inplace);
2304 br(Assembler::NE, is_flat_array);
2305 }
2306
2307 void MacroAssembler::test_non_flat_array_layout(Register lh, Label& is_non_flat_array) {
2308 tst(lh, Klass::_lh_array_tag_flat_value_bit_inplace);
2309 br(Assembler::EQ, is_non_flat_array);
2310 }
2311
2312 // MacroAssembler protected routines needed to implement
2313 // public methods
2314
2315 void MacroAssembler::mov(Register r, Address dest) {
2316 code_section()->relocate(pc(), dest.rspec());
2317 uint64_t imm64 = (uint64_t)dest.target();
2318 movptr(r, imm64);
2319 }
2320
2321 // Move a constant pointer into r. In AArch64 mode the virtual
2322 // address space is 48 bits in size, so we only need three
2323 // instructions to create a patchable instruction sequence that can
2324 // reach anywhere.
2325 void MacroAssembler::movptr(Register r, uintptr_t imm64) {
2326 #ifndef PRODUCT
2327 {
2328 char buffer[64];
2329 os::snprintf_checked(buffer, sizeof(buffer), "0x%" PRIX64, (uint64_t)imm64);
2330 block_comment(buffer);
2331 }
4992 adrp(rscratch1, src2, offset);
4993 ldr(rscratch1, Address(rscratch1, offset));
4994 cmp(src1, rscratch1);
4995 }
4996
4997 void MacroAssembler::cmpoop(Register obj1, Register obj2) {
4998 cmp(obj1, obj2);
4999 }
5000
5001 void MacroAssembler::load_method_holder_cld(Register rresult, Register rmethod) {
5002 load_method_holder(rresult, rmethod);
5003 ldr(rresult, Address(rresult, InstanceKlass::class_loader_data_offset()));
5004 }
5005
5006 void MacroAssembler::load_method_holder(Register holder, Register method) {
5007 ldr(holder, Address(method, Method::const_offset())); // ConstMethod*
5008 ldr(holder, Address(holder, ConstMethod::constants_offset())); // ConstantPool*
5009 ldr(holder, Address(holder, ConstantPool::pool_holder_offset())); // InstanceKlass*
5010 }
5011
5012 void MacroAssembler::load_metadata(Register dst, Register src) {
5013 if (UseCompactObjectHeaders) {
5014 load_narrow_klass_compact(dst, src);
5015 } else if (UseCompressedClassPointers) {
5016 ldrw(dst, Address(src, oopDesc::klass_offset_in_bytes()));
5017 } else {
5018 ldr(dst, Address(src, oopDesc::klass_offset_in_bytes()));
5019 }
5020 }
5021
5022 // Loads the obj's Klass* into dst.
5023 // Preserves all registers (incl src, rscratch1 and rscratch2).
5024 // Input:
5025 // src - the oop we want to load the klass from.
5026 // dst - output narrow klass.
5027 void MacroAssembler::load_narrow_klass_compact(Register dst, Register src) {
5028 assert(UseCompactObjectHeaders, "expects UseCompactObjectHeaders");
5029 ldr(dst, Address(src, oopDesc::mark_offset_in_bytes()));
5030 lsr(dst, dst, markWord::klass_shift);
5031 }
5032
5033 void MacroAssembler::load_klass(Register dst, Register src) {
5034 if (UseCompactObjectHeaders) {
5035 load_narrow_klass_compact(dst, src);
5036 decode_klass_not_null(dst);
5037 } else if (UseCompressedClassPointers) {
5038 ldrw(dst, Address(src, oopDesc::klass_offset_in_bytes()));
5039 decode_klass_not_null(dst);
5040 } else {
5041 ldr(dst, Address(src, oopDesc::klass_offset_in_bytes()));
5112 }
5113 cmp(klass, tmp);
5114 }
5115
5116 void MacroAssembler::cmp_klasses_from_objects(Register obj1, Register obj2, Register tmp1, Register tmp2) {
5117 if (UseCompactObjectHeaders) {
5118 load_narrow_klass_compact(tmp1, obj1);
5119 load_narrow_klass_compact(tmp2, obj2);
5120 cmpw(tmp1, tmp2);
5121 } else if (UseCompressedClassPointers) {
5122 ldrw(tmp1, Address(obj1, oopDesc::klass_offset_in_bytes()));
5123 ldrw(tmp2, Address(obj2, oopDesc::klass_offset_in_bytes()));
5124 cmpw(tmp1, tmp2);
5125 } else {
5126 ldr(tmp1, Address(obj1, oopDesc::klass_offset_in_bytes()));
5127 ldr(tmp2, Address(obj2, oopDesc::klass_offset_in_bytes()));
5128 cmp(tmp1, tmp2);
5129 }
5130 }
5131
5132 void MacroAssembler::load_prototype_header(Register dst, Register src) {
5133 load_klass(dst, src);
5134 ldr(dst, Address(dst, Klass::prototype_header_offset()));
5135 }
5136
5137 void MacroAssembler::store_klass(Register dst, Register src) {
5138 // FIXME: Should this be a store release? concurrent gcs assumes
5139 // klass length is valid if klass field is not null.
5140 assert(!UseCompactObjectHeaders, "not with compact headers");
5141 if (UseCompressedClassPointers) {
5142 encode_klass_not_null(src);
5143 strw(src, Address(dst, oopDesc::klass_offset_in_bytes()));
5144 } else {
5145 str(src, Address(dst, oopDesc::klass_offset_in_bytes()));
5146 }
5147 }
5148
5149 void MacroAssembler::store_klass_gap(Register dst, Register src) {
5150 assert(!UseCompactObjectHeaders, "not with compact headers");
5151 if (UseCompressedClassPointers) {
5152 // Store to klass gap in destination
5153 strw(src, Address(dst, oopDesc::klass_gap_offset_in_bytes()));
5154 }
5155 }
5156
5518 if (as_raw) {
5519 bs->BarrierSetAssembler::load_at(this, decorators, type, dst, src, tmp1, tmp2);
5520 } else {
5521 bs->load_at(this, decorators, type, dst, src, tmp1, tmp2);
5522 }
5523 }
5524
5525 void MacroAssembler::access_store_at(BasicType type, DecoratorSet decorators,
5526 Address dst, Register val,
5527 Register tmp1, Register tmp2, Register tmp3) {
5528 BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
5529 decorators = AccessInternal::decorator_fixup(decorators, type);
5530 bool as_raw = (decorators & AS_RAW) != 0;
5531 if (as_raw) {
5532 bs->BarrierSetAssembler::store_at(this, decorators, type, dst, val, tmp1, tmp2, tmp3);
5533 } else {
5534 bs->store_at(this, decorators, type, dst, val, tmp1, tmp2, tmp3);
5535 }
5536 }
5537
5538 void MacroAssembler::flat_field_copy(DecoratorSet decorators, Register src, Register dst,
5539 Register inline_layout_info) {
5540 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
5541 bs->flat_field_copy(this, decorators, src, dst, inline_layout_info);
5542 }
5543
5544 void MacroAssembler::payload_offset(Register inline_klass, Register offset) {
5545 ldr(offset, Address(inline_klass, InstanceKlass::adr_inlineklass_fixed_block_offset()));
5546 ldrw(offset, Address(offset, InlineKlass::payload_offset_offset()));
5547 }
5548
5549 void MacroAssembler::payload_address(Register oop, Register data, Register inline_klass) {
5550 // ((address) (void*) o) + vk->payload_offset();
5551 Register offset = (data == oop) ? rscratch1 : data;
5552 payload_offset(inline_klass, offset);
5553 if (data == oop) {
5554 add(data, data, offset);
5555 } else {
5556 lea(data, Address(oop, offset));
5557 }
5558 }
5559
5560 void MacroAssembler::data_for_value_array_index(Register array, Register array_klass,
5561 Register index, Register data) {
5562 assert_different_registers(array, array_klass, index);
5563 assert_different_registers(rscratch1, array, index);
5564
5565 // array->base() + (index << Klass::layout_helper_log2_element_size(lh));
5566 ldrw(rscratch1, Address(array_klass, Klass::layout_helper_offset()));
5567
5568 // Klass::layout_helper_log2_element_size(lh)
5569 // (lh >> _lh_log2_element_size_shift) & _lh_log2_element_size_mask;
5570 lsr(rscratch1, rscratch1, Klass::_lh_log2_element_size_shift);
5571 andr(rscratch1, rscratch1, Klass::_lh_log2_element_size_mask);
5572 lslv(index, index, rscratch1);
5573
5574 add(data, array, index);
5575 add(data, data, arrayOopDesc::base_offset_in_bytes(T_FLAT_ELEMENT));
5576 }
5577
5578 void MacroAssembler::load_heap_oop(Register dst, Address src, Register tmp1,
5579 Register tmp2, DecoratorSet decorators) {
5580 access_load_at(T_OBJECT, IN_HEAP | decorators, dst, src, tmp1, tmp2);
5581 }
5582
5583 void MacroAssembler::load_heap_oop_not_null(Register dst, Address src, Register tmp1,
5584 Register tmp2, DecoratorSet decorators) {
5585 access_load_at(T_OBJECT, IN_HEAP | IS_NOT_NULL | decorators, dst, src, tmp1, tmp2);
5586 }
5587
5588 void MacroAssembler::store_heap_oop(Address dst, Register val, Register tmp1,
5589 Register tmp2, Register tmp3, DecoratorSet decorators) {
5590 access_store_at(T_OBJECT, IN_HEAP | decorators, dst, val, tmp1, tmp2, tmp3);
5591 }
5592
5593 // Used for storing nulls.
5594 void MacroAssembler::store_heap_oop_null(Address dst) {
5595 access_store_at(T_OBJECT, IN_HEAP, dst, noreg, noreg, noreg, noreg);
5596 }
5597
5633 oop_index = oop_recorder()->allocate_metadata_index(obj);
5634 } else {
5635 oop_index = oop_recorder()->find_index(obj);
5636 }
5637 RelocationHolder rspec = metadata_Relocation::spec(oop_index);
5638 mov(dst, Address((address)obj, rspec));
5639 }
5640
5641 Address MacroAssembler::constant_oop_address(jobject obj) {
5642 #ifdef ASSERT
5643 {
5644 ThreadInVMfromUnknown tiv;
5645 assert(oop_recorder() != nullptr, "this assembler needs an OopRecorder");
5646 assert(Universe::heap()->is_in(JNIHandles::resolve(obj)), "not an oop");
5647 }
5648 #endif
5649 int oop_index = oop_recorder()->find_index(obj);
5650 return Address((address)obj, oop_Relocation::spec(oop_index));
5651 }
5652
5653 // Object / value buffer allocation...
5654 void MacroAssembler::allocate_instance(Register klass, Register new_obj,
5655 Register t1, Register t2,
5656 bool clear_fields, Label& alloc_failed)
5657 {
5658 Label done, initialize_header, initialize_object, slow_case, slow_case_no_pop;
5659 Register layout_size = t1;
5660 assert(new_obj == r0, "needs to be r0");
5661 assert_different_registers(klass, new_obj, t1, t2);
5662
5663 // get instance_size in InstanceKlass (scaled to a count of bytes)
5664 ldrw(layout_size, Address(klass, Klass::layout_helper_offset()));
5665 // test to see if it is malformed in some way
5666 tst(layout_size, Klass::_lh_instance_slow_path_bit);
5667 br(Assembler::NE, slow_case_no_pop);
5668
5669 // Allocate the instance:
5670 // If TLAB is enabled:
5671 // Try to allocate in the TLAB.
5672 // If fails, go to the slow path.
5673 // Initialize the allocation.
5674 // Exit.
5675 //
5676 // Go to slow path.
5677
5678 if (UseTLAB) {
5679 push(klass);
5680 tlab_allocate(new_obj, layout_size, 0, klass, t2, slow_case);
5681 if (ZeroTLAB || (!clear_fields)) {
5682 // the fields have been already cleared
5683 b(initialize_header);
5684 } else {
5685 // initialize both the header and fields
5686 b(initialize_object);
5687 }
5688
5689 if (clear_fields) {
5690 // The object is initialized before the header. If the object size is
5691 // zero, go directly to the header initialization.
5692 bind(initialize_object);
5693 int header_size = oopDesc::header_size() * HeapWordSize;
5694 assert(is_aligned(header_size, BytesPerLong), "oop header size must be 8-byte-aligned");
5695 subs(layout_size, layout_size, header_size);
5696 br(Assembler::EQ, initialize_header);
5697
5698 // Initialize topmost object field, divide size by 8, check if odd and
5699 // test if zero.
5700
5701 #ifdef ASSERT
5702 // make sure instance_size was multiple of 8
5703 Label L;
5704 tst(layout_size, 7);
5705 br(Assembler::EQ, L);
5706 stop("object size is not multiple of 8 - adjust this code");
5707 bind(L);
5708 // must be > 0, no extra check needed here
5709 #endif
5710
5711 lsr(layout_size, layout_size, LogBytesPerLong);
5712
5713 // initialize remaining object fields: instance_size was a multiple of 8
5714 {
5715 Label loop;
5716 Register base = t2;
5717
5718 bind(loop);
5719 add(rscratch1, new_obj, layout_size, Assembler::LSL, LogBytesPerLong);
5720 str(zr, Address(rscratch1, header_size - 1*oopSize));
5721 subs(layout_size, layout_size, 1);
5722 br(Assembler::NE, loop);
5723 }
5724 } // clear_fields
5725
5726 // initialize object header only.
5727 bind(initialize_header);
5728 pop(klass);
5729 Register mark_word = t2;
5730 if (UseCompactObjectHeaders || EnableValhalla) {
5731 ldr(mark_word, Address(klass, Klass::prototype_header_offset()));
5732 str(mark_word, Address(new_obj, oopDesc::mark_offset_in_bytes()));
5733 } else {
5734 mov(mark_word, (intptr_t)markWord::prototype().value());
5735 str(mark_word, Address(new_obj, oopDesc::mark_offset_in_bytes()));
5736 }
5737 if (!UseCompactObjectHeaders) {
5738 store_klass_gap(new_obj, zr); // zero klass gap for compressed oops
5739 mov(t2, klass); // preserve klass
5740 store_klass(new_obj, t2); // src klass reg is potentially compressed
5741 }
5742 b(done);
5743 }
5744
5745 if (UseTLAB) {
5746 bind(slow_case);
5747 pop(klass);
5748 }
5749 bind(slow_case_no_pop);
5750 b(alloc_failed);
5751
5752 bind(done);
5753 }
5754
5755 // Defines obj, preserves var_size_in_bytes, okay for t2 == var_size_in_bytes.
5756 void MacroAssembler::tlab_allocate(Register obj,
5757 Register var_size_in_bytes,
5758 int con_size_in_bytes,
5759 Register t1,
5760 Register t2,
5761 Label& slow_case) {
5762 BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
5763 bs->tlab_allocate(this, obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
5764 }
5765
5766 void MacroAssembler::verify_tlab() {
5767 #ifdef ASSERT
5768 if (UseTLAB && VerifyOops) {
5769 Label next, ok;
5770
5771 stp(rscratch2, rscratch1, Address(pre(sp, -16)));
5772
5773 ldr(rscratch2, Address(rthread, in_bytes(JavaThread::tlab_top_offset())));
5774 ldr(rscratch1, Address(rthread, in_bytes(JavaThread::tlab_start_offset())));
5775 cmp(rscratch2, rscratch1);
5776 br(Assembler::HS, next);
5777 STOP("assert(top >= start)");
5778 should_not_reach_here();
5779
5780 bind(next);
5781 ldr(rscratch2, Address(rthread, in_bytes(JavaThread::tlab_end_offset())));
5782 ldr(rscratch1, Address(rthread, in_bytes(JavaThread::tlab_top_offset())));
5783 cmp(rscratch2, rscratch1);
5784 br(Assembler::HS, ok);
5785 STOP("assert(top <= end)");
5786 should_not_reach_here();
5787
5788 bind(ok);
5789 ldp(rscratch2, rscratch1, Address(post(sp, 16)));
5790 }
5791 #endif
5792 }
5793
5794 void MacroAssembler::get_inline_type_field_klass(Register holder_klass, Register index, Register inline_klass) {
5795 inline_layout_info(holder_klass, index, inline_klass);
5796 ldr(inline_klass, Address(inline_klass, InlineLayoutInfo::klass_offset()));
5797 }
5798
5799 void MacroAssembler::inline_layout_info(Register holder_klass, Register index, Register layout_info) {
5800 assert_different_registers(holder_klass, index, layout_info);
5801 InlineLayoutInfo array[2];
5802 int size = (char*)&array[1] - (char*)&array[0]; // computing size of array elements
5803 if (is_power_of_2(size)) {
5804 lsl(index, index, log2i_exact(size)); // Scale index by power of 2
5805 } else {
5806 mov(layout_info, size);
5807 mul(index, index, layout_info); // Scale the index to be the entry index * array_element_size
5808 }
5809 ldr(layout_info, Address(holder_klass, InstanceKlass::inline_layout_info_array_offset()));
5810 add(layout_info, layout_info, Array<InlineLayoutInfo>::base_offset_in_bytes());
5811 lea(layout_info, Address(layout_info, index));
5812 }
5813
5814 // Writes to stack successive pages until offset reached to check for
5815 // stack overflow + shadow pages. This clobbers tmp.
5816 void MacroAssembler::bang_stack_size(Register size, Register tmp) {
5817 assert_different_registers(tmp, size, rscratch1);
5818 mov(tmp, sp);
5819 // Bang stack for total size given plus shadow page size.
5820 // Bang one page at a time because large size can bang beyond yellow and
5821 // red zones.
5822 Label loop;
5823 mov(rscratch1, (int)os::vm_page_size());
5824 bind(loop);
5825 lea(tmp, Address(tmp, -(int)os::vm_page_size()));
5826 subsw(size, size, rscratch1);
5827 str(size, Address(tmp));
5828 br(Assembler::GT, loop);
5829
5830 // Bang down shadow pages too.
5831 // At this point, (tmp-0) is the last address touched, so don't
5832 // touch it again. (It was touched as (tmp-pagesize) but then tmp
5833 // was post-decremented.) Skip this address by starting at i=1, and
5919 }
5920
5921 void MacroAssembler::remove_frame(int framesize) {
5922 assert(framesize >= 2 * wordSize, "framesize must include space for FP/LR");
5923 assert(framesize % (2*wordSize) == 0, "must preserve 2*wordSize alignment");
5924 if (framesize < ((1 << 9) + 2 * wordSize)) {
5925 ldp(rfp, lr, Address(sp, framesize - 2 * wordSize));
5926 add(sp, sp, framesize);
5927 } else {
5928 if (framesize < ((1 << 12) + 2 * wordSize))
5929 add(sp, sp, framesize - 2 * wordSize);
5930 else {
5931 mov(rscratch1, framesize - 2 * wordSize);
5932 add(sp, sp, rscratch1);
5933 }
5934 ldp(rfp, lr, Address(post(sp, 2 * wordSize)));
5935 }
5936 authenticate_return_address();
5937 }
5938
5939 void MacroAssembler::remove_frame(int initial_framesize, bool needs_stack_repair) {
5940 if (needs_stack_repair) {
5941 // Remove the extension of the caller's frame used for inline type unpacking
5942 //
5943 // Right now the stack looks like this:
5944 //
5945 // | Arguments from caller |
5946 // |---------------------------| <-- caller's SP
5947 // | Saved LR #1 |
5948 // | Saved FP #1 |
5949 // |---------------------------|
5950 // | Extension space for |
5951 // | inline arg (un)packing |
5952 // |---------------------------| <-- start of this method's frame
5953 // | Saved LR #2 |
5954 // | Saved FP #2 |
5955 // |---------------------------| <-- FP
5956 // | sp_inc |
5957 // | method locals |
5958 // |---------------------------| <-- SP
5959 //
5960 // There are two copies of FP and LR on the stack. They will be identical at
5961 // first, but that can change.
5962 // If the caller has been deoptimized, LR #1 will be patched to point at the
5963 // deopt blob, and LR #2 will still point into the old method.
5964 // If the saved FP (x29) was not used as the frame pointer, but to store an
5965 // oop, the GC will be aware only of FP #1 as the spilled location of x29 and
5966 // will fix only this one. Overall, FP/LR #2 are not reliable and are simply
5967 // needed to add space between the extension space and the locals, as there
5968 // would be between the real arguments and the locals if we don't need to
5969 // do unpacking.
5970 //
5971 // When restoring, one must then load FP #1 into x29, and LR #1 into x30,
5972 // while keeping in mind that from the scalarized entry point, there will be
5973 // only one copy of each.
5974 //
5975 // The sp_inc stack slot holds the total size of the frame including the
5976 // extension space minus two words for the saved FP and LR. That is how to
5977 // find FP/LR #1. This size is expressed in bytes. Be careful when using it
5978 // from C++ in pointer arithmetic; you might need to divide it by wordSize.
5979 //
5980 // TODO 8371993 store fake values instead of LR/FP#2
5981
5982 int sp_inc_offset = initial_framesize - 3 * wordSize; // Immediately below saved LR and FP
5983
5984 ldr(rscratch1, Address(sp, sp_inc_offset));
5985 add(sp, sp, rscratch1);
5986 ldp(rfp, lr, Address(post(sp, 2 * wordSize)));
5987 } else {
5988 remove_frame(initial_framesize);
5989 }
5990 }
5991
5992 void MacroAssembler::save_stack_increment(int sp_inc, int frame_size) {
5993 int real_frame_size = frame_size + sp_inc;
5994 assert(sp_inc == 0 || sp_inc > 2*wordSize, "invalid sp_inc value");
5995 assert(real_frame_size >= 2*wordSize, "frame size must include FP/LR space");
5996 assert((real_frame_size & (StackAlignmentInBytes-1)) == 0, "frame size not aligned");
5997
5998 int sp_inc_offset = frame_size - 3 * wordSize; // Immediately below saved LR and FP
5999
6000 // Subtract two words for the saved FP and LR as these will be popped
6001 // separately. See remove_frame above.
6002 mov(rscratch1, real_frame_size - 2*wordSize);
6003 str(rscratch1, Address(sp, sp_inc_offset));
6004 }
6005
6006 // This method counts leading positive bytes (highest bit not set) in provided byte array
6007 address MacroAssembler::count_positives(Register ary1, Register len, Register result) {
6008 // Simple and most common case of aligned small array which is not at the
6009 // end of memory page is placed here. All other cases are in stub.
6010 Label LOOP, END, STUB, STUB_LONG, SET_RESULT, DONE;
6011 const uint64_t UPPER_BIT_MASK=0x8080808080808080;
6012 assert_different_registers(ary1, len, result);
6013
6014 mov(result, len);
6015 cmpw(len, 0);
6016 br(LE, DONE);
6017 cmpw(len, 4 * wordSize);
6018 br(GE, STUB_LONG); // size > 32 then go to stub
6019
6020 int shift = 64 - exact_log2(os::vm_page_size());
6021 lsl(rscratch1, ary1, shift);
6022 mov(rscratch2, (size_t)(4 * wordSize) << shift);
6023 adds(rscratch2, rscratch1, rscratch2); // At end of page?
6024 br(CS, STUB); // at the end of page then go to stub
6902 // On other systems, the helper is a usual C function.
6903 //
6904 void MacroAssembler::get_thread(Register dst) {
6905 RegSet saved_regs =
6906 LINUX_ONLY(RegSet::range(r0, r1) + lr - dst)
6907 NOT_LINUX (RegSet::range(r0, r17) + lr - dst);
6908
6909 protect_return_address();
6910 push(saved_regs, sp);
6911
6912 mov(lr, ExternalAddress(CAST_FROM_FN_PTR(address, JavaThread::aarch64_get_thread_helper)));
6913 blr(lr);
6914 if (dst != c_rarg0) {
6915 mov(dst, c_rarg0);
6916 }
6917
6918 pop(saved_regs, sp);
6919 authenticate_return_address();
6920 }
6921
6922 #ifdef COMPILER2
6923 // C2 compiled method's prolog code
6924 // Moved here from aarch64.ad to support Valhalla code belows
6925 void MacroAssembler::verified_entry(Compile* C, int sp_inc) {
6926 if (C->clinit_barrier_on_entry()) {
6927 assert(!C->method()->holder()->is_not_initialized(), "initialization should have been started");
6928
6929 Label L_skip_barrier;
6930
6931 mov_metadata(rscratch2, C->method()->holder()->constant_encoding());
6932 clinit_barrier(rscratch2, rscratch1, &L_skip_barrier);
6933 far_jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub()));
6934 bind(L_skip_barrier);
6935 }
6936
6937 if (C->max_vector_size() > 0) {
6938 reinitialize_ptrue();
6939 }
6940
6941 int bangsize = C->output()->bang_size_in_bytes();
6942 if (C->output()->need_stack_bang(bangsize))
6943 generate_stack_overflow_check(bangsize);
6944
6945 // n.b. frame size includes space for return pc and rfp
6946 const long framesize = C->output()->frame_size_in_bytes();
6947 build_frame(framesize);
6948
6949 if (C->needs_stack_repair()) {
6950 save_stack_increment(sp_inc, framesize);
6951 }
6952
6953 if (VerifyStackAtCalls) {
6954 Unimplemented();
6955 }
6956 }
6957 #endif // COMPILER2
6958
6959 int MacroAssembler::store_inline_type_fields_to_buf(ciInlineKlass* vk, bool from_interpreter) {
6960 assert(InlineTypeReturnedAsFields, "Inline types should never be returned as fields");
6961 // An inline type might be returned. If fields are in registers we
6962 // need to allocate an inline type instance and initialize it with
6963 // the value of the fields.
6964 Label skip;
6965 // We only need a new buffered inline type if a new one is not returned
6966 tbz(r0, 0, skip);
6967 int call_offset = -1;
6968
6969 // Be careful not to clobber r1-7 which hold returned fields
6970 // Also do not use callee-saved registers as these may be live in the interpreter
6971 Register tmp1 = r13, tmp2 = r14, klass = r15, r0_preserved = r12;
6972
6973 // The following code is similar to allocate_instance but has some slight differences,
6974 // e.g. object size is always not zero, sometimes it's constant; storing klass ptr after
6975 // allocating is not necessary if vk != nullptr, etc. allocate_instance is not aware of these.
6976 Label slow_case;
6977 // 1. Try to allocate a new buffered inline instance either from TLAB or eden space
6978 mov(r0_preserved, r0); // save r0 for slow_case since *_allocate may corrupt it when allocation failed
6979
6980 if (vk != nullptr) {
6981 // Called from C1, where the return type is statically known.
6982 movptr(klass, (intptr_t)vk->get_InlineKlass());
6983 jint lh = vk->layout_helper();
6984 assert(lh != Klass::_lh_neutral_value, "inline class in return type must have been resolved");
6985 if (UseTLAB && !Klass::layout_helper_needs_slow_path(lh)) {
6986 tlab_allocate(r0, noreg, lh, tmp1, tmp2, slow_case);
6987 } else {
6988 b(slow_case);
6989 }
6990 } else {
6991 // Call from interpreter. R0 contains ((the InlineKlass* of the return type) | 0x01)
6992 andr(klass, r0, -2);
6993 if (UseTLAB) {
6994 ldrw(tmp2, Address(klass, Klass::layout_helper_offset()));
6995 tst(tmp2, Klass::_lh_instance_slow_path_bit);
6996 br(Assembler::NE, slow_case);
6997 tlab_allocate(r0, tmp2, 0, tmp1, tmp2, slow_case);
6998 } else {
6999 b(slow_case);
7000 }
7001 }
7002 if (UseTLAB) {
7003 // 2. Initialize buffered inline instance header
7004 Register buffer_obj = r0;
7005 if (UseCompactObjectHeaders) {
7006 ldr(rscratch1, Address(klass, Klass::prototype_header_offset()));
7007 str(rscratch1, Address(buffer_obj, oopDesc::mark_offset_in_bytes()));
7008 } else {
7009 mov(rscratch1, (intptr_t)markWord::inline_type_prototype().value());
7010 str(rscratch1, Address(buffer_obj, oopDesc::mark_offset_in_bytes()));
7011 store_klass_gap(buffer_obj, zr);
7012 if (vk == nullptr) {
7013 // store_klass corrupts klass, so save it for later use (interpreter case only).
7014 mov(tmp1, klass);
7015 }
7016 store_klass(buffer_obj, klass);
7017 klass = tmp1;
7018 }
7019 // 3. Initialize its fields with an inline class specific handler
7020 if (vk != nullptr) {
7021 far_call(RuntimeAddress(vk->pack_handler())); // no need for call info as this will not safepoint.
7022 } else {
7023 ldr(tmp1, Address(klass, InstanceKlass::adr_inlineklass_fixed_block_offset()));
7024 ldr(tmp1, Address(tmp1, InlineKlass::pack_handler_offset()));
7025 blr(tmp1);
7026 }
7027
7028 membar(Assembler::StoreStore);
7029 b(skip);
7030 } else {
7031 // Must have already branched to slow_case above.
7032 DEBUG_ONLY(should_not_reach_here());
7033 }
7034 bind(slow_case);
7035 // We failed to allocate a new inline type, fall back to a runtime
7036 // call. Some oop field may be live in some registers but we can't
7037 // tell. That runtime call will take care of preserving them
7038 // across a GC if there's one.
7039 mov(r0, r0_preserved);
7040
7041 if (from_interpreter) {
7042 super_call_VM_leaf(StubRoutines::store_inline_type_fields_to_buf());
7043 } else {
7044 far_call(RuntimeAddress(StubRoutines::store_inline_type_fields_to_buf()));
7045 call_offset = offset();
7046 }
7047 membar(Assembler::StoreStore);
7048
7049 bind(skip);
7050 return call_offset;
7051 }
7052
7053 // Move a value between registers/stack slots and update the reg_state
7054 bool MacroAssembler::move_helper(VMReg from, VMReg to, BasicType bt, RegState reg_state[]) {
7055 assert(from->is_valid() && to->is_valid(), "source and destination must be valid");
7056 if (reg_state[to->value()] == reg_written) {
7057 return true; // Already written
7058 }
7059
7060 if (from != to && bt != T_VOID) {
7061 if (reg_state[to->value()] == reg_readonly) {
7062 return false; // Not yet writable
7063 }
7064 if (from->is_reg()) {
7065 if (to->is_reg()) {
7066 if (from->is_Register() && to->is_Register()) {
7067 mov(to->as_Register(), from->as_Register());
7068 } else if (from->is_FloatRegister() && to->is_FloatRegister()) {
7069 fmovd(to->as_FloatRegister(), from->as_FloatRegister());
7070 } else {
7071 ShouldNotReachHere();
7072 }
7073 } else {
7074 int st_off = to->reg2stack() * VMRegImpl::stack_slot_size;
7075 Address to_addr = Address(sp, st_off);
7076 if (from->is_FloatRegister()) {
7077 if (bt == T_DOUBLE) {
7078 strd(from->as_FloatRegister(), to_addr);
7079 } else {
7080 assert(bt == T_FLOAT, "must be float");
7081 strs(from->as_FloatRegister(), to_addr);
7082 }
7083 } else {
7084 str(from->as_Register(), to_addr);
7085 }
7086 }
7087 } else {
7088 Address from_addr = Address(sp, from->reg2stack() * VMRegImpl::stack_slot_size);
7089 if (to->is_reg()) {
7090 if (to->is_FloatRegister()) {
7091 if (bt == T_DOUBLE) {
7092 ldrd(to->as_FloatRegister(), from_addr);
7093 } else {
7094 assert(bt == T_FLOAT, "must be float");
7095 ldrs(to->as_FloatRegister(), from_addr);
7096 }
7097 } else {
7098 ldr(to->as_Register(), from_addr);
7099 }
7100 } else {
7101 int st_off = to->reg2stack() * VMRegImpl::stack_slot_size;
7102 ldr(rscratch1, from_addr);
7103 str(rscratch1, Address(sp, st_off));
7104 }
7105 }
7106 }
7107
7108 // Update register states
7109 reg_state[from->value()] = reg_writable;
7110 reg_state[to->value()] = reg_written;
7111 return true;
7112 }
7113
7114 // Calculate the extra stack space required for packing or unpacking inline
7115 // args and adjust the stack pointer
7116 int MacroAssembler::extend_stack_for_inline_args(int args_on_stack) {
7117 int sp_inc = args_on_stack * VMRegImpl::stack_slot_size;
7118 sp_inc = align_up(sp_inc, StackAlignmentInBytes);
7119 assert(sp_inc > 0, "sanity");
7120
7121 // Save a copy of the FP and LR here for deoptimization patching and frame walking
7122 stp(rfp, lr, Address(pre(sp, -2 * wordSize)));
7123
7124 // Adjust the stack pointer. This will be repaired on return by MacroAssembler::remove_frame
7125 if (sp_inc < (1 << 9)) {
7126 sub(sp, sp, sp_inc); // Fits in an immediate
7127 } else {
7128 mov(rscratch1, sp_inc);
7129 sub(sp, sp, rscratch1);
7130 }
7131
7132 return sp_inc + 2 * wordSize; // Account for the FP/LR space
7133 }
7134
7135 // Read all fields from an inline type oop and store the values in registers/stack slots
7136 bool MacroAssembler::unpack_inline_helper(const GrowableArray<SigEntry>* sig, int& sig_index,
7137 VMReg from, int& from_index, VMRegPair* to, int to_count, int& to_index,
7138 RegState reg_state[]) {
7139 assert(sig->at(sig_index)._bt == T_VOID, "should be at end delimiter");
7140 assert(from->is_valid(), "source must be valid");
7141 bool progress = false;
7142 #ifdef ASSERT
7143 const int start_offset = offset();
7144 #endif
7145
7146 Label L_null, L_notNull;
7147 // Don't use r14 as tmp because it's used for spilling (see MacroAssembler::spill_reg_for)
7148 // TODO 8366717 We need to make sure that r14 (and potentially other long-life regs) are kept live in slowpath runtime calls in GC barriers
7149 Register tmp1 = r10;
7150 Register tmp2 = r11;
7151 Register fromReg = noreg;
7152 ScalarizedInlineArgsStream stream(sig, sig_index, to, to_count, to_index, -1);
7153 bool done = true;
7154 bool mark_done = true;
7155 VMReg toReg;
7156 BasicType bt;
7157 // Check if argument requires a null check
7158 bool null_check = false;
7159 VMReg nullCheckReg;
7160 while (stream.next(nullCheckReg, bt)) {
7161 if (sig->at(stream.sig_index())._offset == -1) {
7162 null_check = true;
7163 break;
7164 }
7165 }
7166 stream.reset(sig_index, to_index);
7167 while (stream.next(toReg, bt)) {
7168 assert(toReg->is_valid(), "destination must be valid");
7169 int idx = (int)toReg->value();
7170 if (reg_state[idx] == reg_readonly) {
7171 if (idx != from->value()) {
7172 mark_done = false;
7173 }
7174 done = false;
7175 continue;
7176 } else if (reg_state[idx] == reg_written) {
7177 continue;
7178 }
7179 assert(reg_state[idx] == reg_writable, "must be writable");
7180 reg_state[idx] = reg_written;
7181 progress = true;
7182
7183 if (fromReg == noreg) {
7184 if (from->is_reg()) {
7185 fromReg = from->as_Register();
7186 } else {
7187 int st_off = from->reg2stack() * VMRegImpl::stack_slot_size;
7188 ldr(tmp1, Address(sp, st_off));
7189 fromReg = tmp1;
7190 }
7191 if (null_check) {
7192 // Nullable inline type argument, emit null check
7193 cbz(fromReg, L_null);
7194 }
7195 }
7196 int off = sig->at(stream.sig_index())._offset;
7197 if (off == -1) {
7198 assert(null_check, "Missing null check at");
7199 if (toReg->is_stack()) {
7200 int st_off = toReg->reg2stack() * VMRegImpl::stack_slot_size;
7201 mov(tmp2, 1);
7202 str(tmp2, Address(sp, st_off));
7203 } else {
7204 mov(toReg->as_Register(), 1);
7205 }
7206 continue;
7207 }
7208 assert(off > 0, "offset in object should be positive");
7209 Address fromAddr = Address(fromReg, off);
7210 if (!toReg->is_FloatRegister()) {
7211 Register dst = toReg->is_stack() ? tmp2 : toReg->as_Register();
7212 if (is_reference_type(bt)) {
7213 load_heap_oop(dst, fromAddr, rscratch1, rscratch2);
7214 } else {
7215 bool is_signed = (bt != T_CHAR) && (bt != T_BOOLEAN);
7216 load_sized_value(dst, fromAddr, type2aelembytes(bt), is_signed);
7217 }
7218 if (toReg->is_stack()) {
7219 int st_off = toReg->reg2stack() * VMRegImpl::stack_slot_size;
7220 str(dst, Address(sp, st_off));
7221 }
7222 } else if (bt == T_DOUBLE) {
7223 ldrd(toReg->as_FloatRegister(), fromAddr);
7224 } else {
7225 assert(bt == T_FLOAT, "must be float");
7226 ldrs(toReg->as_FloatRegister(), fromAddr);
7227 }
7228 }
7229 if (progress && null_check) {
7230 if (done) {
7231 b(L_notNull);
7232 bind(L_null);
7233 // Set null marker to zero to signal that the argument is null.
7234 // Also set all oop fields to zero to make the GC happy.
7235 stream.reset(sig_index, to_index);
7236 while (stream.next(toReg, bt)) {
7237 if (sig->at(stream.sig_index())._offset == -1 ||
7238 bt == T_OBJECT || bt == T_ARRAY) {
7239 if (toReg->is_stack()) {
7240 int st_off = toReg->reg2stack() * VMRegImpl::stack_slot_size;
7241 str(zr, Address(sp, st_off));
7242 } else {
7243 mov(toReg->as_Register(), zr);
7244 }
7245 }
7246 }
7247 bind(L_notNull);
7248 } else {
7249 bind(L_null);
7250 }
7251 }
7252
7253 // TODO 8366717 This is probably okay but looks fishy because stream is reset in the "Set null marker to zero" case just above. Same on x64.
7254 sig_index = stream.sig_index();
7255 to_index = stream.regs_index();
7256
7257 if (mark_done && reg_state[from->value()] != reg_written) {
7258 // This is okay because no one else will write to that slot
7259 reg_state[from->value()] = reg_writable;
7260 }
7261 from_index--;
7262 assert(progress || (start_offset == offset()), "should not emit code");
7263 return done;
7264 }
7265
7266 // Pack fields back into an inline type oop
7267 bool MacroAssembler::pack_inline_helper(const GrowableArray<SigEntry>* sig, int& sig_index, int vtarg_index,
7268 VMRegPair* from, int from_count, int& from_index, VMReg to,
7269 RegState reg_state[], Register val_array) {
7270 assert(sig->at(sig_index)._bt == T_METADATA, "should be at delimiter");
7271 assert(to->is_valid(), "destination must be valid");
7272
7273 if (reg_state[to->value()] == reg_written) {
7274 skip_unpacked_fields(sig, sig_index, from, from_count, from_index);
7275 return true; // Already written
7276 }
7277
7278 // The GC barrier expanded by store_heap_oop below may call into the
7279 // runtime so use callee-saved registers for any values that need to be
7280 // preserved. The GC barrier assembler should take care of saving the
7281 // Java argument registers.
7282 // TODO 8284443 Isn't it an issue if below code uses r14 as tmp when it contains a spilled value?
7283 // Be careful with r14 because it's used for spilling (see MacroAssembler::spill_reg_for).
7284 Register val_obj_tmp = r21;
7285 Register from_reg_tmp = r22;
7286 Register tmp1 = r14;
7287 Register tmp2 = r13;
7288 Register tmp3 = r12;
7289 Register val_obj = to->is_stack() ? val_obj_tmp : to->as_Register();
7290
7291 assert_different_registers(val_obj_tmp, from_reg_tmp, tmp1, tmp2, tmp3, val_array);
7292
7293 if (reg_state[to->value()] == reg_readonly) {
7294 if (!is_reg_in_unpacked_fields(sig, sig_index, to, from, from_count, from_index)) {
7295 skip_unpacked_fields(sig, sig_index, from, from_count, from_index);
7296 return false; // Not yet writable
7297 }
7298 val_obj = val_obj_tmp;
7299 }
7300
7301 int index = arrayOopDesc::base_offset_in_bytes(T_OBJECT) + vtarg_index * type2aelembytes(T_OBJECT);
7302 load_heap_oop(val_obj, Address(val_array, index), tmp1, tmp2);
7303
7304 ScalarizedInlineArgsStream stream(sig, sig_index, from, from_count, from_index);
7305 VMReg fromReg;
7306 BasicType bt;
7307 Label L_null;
7308 while (stream.next(fromReg, bt)) {
7309 assert(fromReg->is_valid(), "source must be valid");
7310 reg_state[fromReg->value()] = reg_writable;
7311
7312 int off = sig->at(stream.sig_index())._offset;
7313 if (off == -1) {
7314 // Nullable inline type argument, emit null check
7315 Label L_notNull;
7316 if (fromReg->is_stack()) {
7317 int ld_off = fromReg->reg2stack() * VMRegImpl::stack_slot_size;
7318 ldrb(tmp2, Address(sp, ld_off));
7319 cbnz(tmp2, L_notNull);
7320 } else {
7321 cbnz(fromReg->as_Register(), L_notNull);
7322 }
7323 mov(val_obj, 0);
7324 b(L_null);
7325 bind(L_notNull);
7326 continue;
7327 }
7328
7329 assert(off > 0, "offset in object should be positive");
7330 size_t size_in_bytes = is_java_primitive(bt) ? type2aelembytes(bt) : wordSize;
7331
7332 // Pack the scalarized field into the value object.
7333 Address dst(val_obj, off);
7334 if (!fromReg->is_FloatRegister()) {
7335 Register src;
7336 if (fromReg->is_stack()) {
7337 src = from_reg_tmp;
7338 int ld_off = fromReg->reg2stack() * VMRegImpl::stack_slot_size;
7339 load_sized_value(src, Address(sp, ld_off), size_in_bytes, /* is_signed */ false);
7340 } else {
7341 src = fromReg->as_Register();
7342 }
7343 assert_different_registers(dst.base(), src, tmp1, tmp2, tmp3, val_array);
7344 if (is_reference_type(bt)) {
7345 store_heap_oop(dst, src, tmp1, tmp2, tmp3, IN_HEAP | ACCESS_WRITE | IS_DEST_UNINITIALIZED);
7346 } else {
7347 store_sized_value(dst, src, size_in_bytes);
7348 }
7349 } else if (bt == T_DOUBLE) {
7350 strd(fromReg->as_FloatRegister(), dst);
7351 } else {
7352 assert(bt == T_FLOAT, "must be float");
7353 strs(fromReg->as_FloatRegister(), dst);
7354 }
7355 }
7356 bind(L_null);
7357 sig_index = stream.sig_index();
7358 from_index = stream.regs_index();
7359
7360 assert(reg_state[to->value()] == reg_writable, "must have already been read");
7361 bool success = move_helper(val_obj->as_VMReg(), to, T_OBJECT, reg_state);
7362 assert(success, "to register must be writeable");
7363 return true;
7364 }
7365
7366 VMReg MacroAssembler::spill_reg_for(VMReg reg) {
7367 return (reg->is_FloatRegister()) ? v8->as_VMReg() : r14->as_VMReg();
7368 }
7369
7370 void MacroAssembler::cache_wb(Address line) {
7371 assert(line.getMode() == Address::base_plus_offset, "mode should be base_plus_offset");
7372 assert(line.index() == noreg, "index should be noreg");
7373 assert(line.offset() == 0, "offset should be 0");
7374 // would like to assert this
7375 // assert(line._ext.shift == 0, "shift should be zero");
7376 if (VM_Version::supports_dcpop()) {
7377 // writeback using clear virtual address to point of persistence
7378 dc(Assembler::CVAP, line.base());
7379 } else {
7380 // no need to generate anything as Unsafe.writebackMemory should
7381 // never invoke this stub
7382 }
7383 }
7384
7385 void MacroAssembler::cache_wbsync(bool is_pre) {
7386 // we only need a barrier post sync
7387 if (!is_pre) {
7388 membar(Assembler::AnyAny);
7389 }
7760 }
7761
7762 // Check if the lock-stack is full.
7763 ldrw(top, Address(rthread, JavaThread::lock_stack_top_offset()));
7764 cmpw(top, (unsigned)LockStack::end_offset());
7765 br(Assembler::GE, slow);
7766
7767 // Check for recursion.
7768 subw(t, top, oopSize);
7769 ldr(t, Address(rthread, t));
7770 cmp(obj, t);
7771 br(Assembler::EQ, push);
7772
7773 // Check header for monitor (0b10).
7774 tst(mark, markWord::monitor_value);
7775 br(Assembler::NE, slow);
7776
7777 // Try to lock. Transition lock bits 0b01 => 0b00
7778 assert(oopDesc::mark_offset_in_bytes() == 0, "required to avoid lea");
7779 orr(mark, mark, markWord::unlocked_value);
7780 // Mask inline_type bit such that we go to the slow path if object is an inline type
7781 andr(mark, mark, ~((int) markWord::inline_type_bit_in_place));
7782
7783 eor(t, mark, markWord::unlocked_value);
7784 cmpxchg(/*addr*/ obj, /*expected*/ mark, /*new*/ t, Assembler::xword,
7785 /*acquire*/ true, /*release*/ false, /*weak*/ false, noreg);
7786 br(Assembler::NE, slow);
7787
7788 bind(push);
7789 // After successful lock, push object on lock-stack.
7790 str(obj, Address(rthread, top));
7791 addw(top, top, oopSize);
7792 strw(top, Address(rthread, JavaThread::lock_stack_top_offset()));
7793 }
7794
7795 // Implements lightweight-unlocking.
7796 //
7797 // - obj: the object to be unlocked
7798 // - t1, t2, t3: temporary registers
7799 // - slow: branched to if unlocking fails, absolute offset may larger than 32KB (imm14 encoding).
7800 void MacroAssembler::lightweight_unlock(Register obj, Register t1, Register t2, Register t3, Label& slow) {
7801 // cmpxchg clobbers rscratch1.
7802 assert_different_registers(obj, t1, t2, t3, rscratch1);
|