< prev index next >

src/hotspot/cpu/aarch64/macroAssembler_aarch64.cpp

Print this page

   1 /*
   2  * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, 2024, Red Hat Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "asm/assembler.hpp"
  27 #include "asm/assembler.inline.hpp"
  28 #include "ci/ciEnv.hpp"

  29 #include "code/compiledIC.hpp"
  30 #include "compiler/compileTask.hpp"
  31 #include "compiler/disassembler.hpp"
  32 #include "compiler/oopMap.hpp"
  33 #include "gc/shared/barrierSet.hpp"
  34 #include "gc/shared/barrierSetAssembler.hpp"
  35 #include "gc/shared/cardTableBarrierSet.hpp"
  36 #include "gc/shared/cardTable.hpp"
  37 #include "gc/shared/collectedHeap.hpp"
  38 #include "gc/shared/tlab_globals.hpp"
  39 #include "interpreter/bytecodeHistogram.hpp"
  40 #include "interpreter/interpreter.hpp"
  41 #include "interpreter/interpreterRuntime.hpp"
  42 #include "jvm.h"
  43 #include "memory/resourceArea.hpp"
  44 #include "memory/universe.hpp"
  45 #include "nativeInst_aarch64.hpp"
  46 #include "oops/accessDecorators.hpp"
  47 #include "oops/compressedKlass.inline.hpp"
  48 #include "oops/compressedOops.inline.hpp"
  49 #include "oops/klass.inline.hpp"


  50 #include "runtime/continuation.hpp"

  51 #include "runtime/icache.hpp"
  52 #include "runtime/interfaceSupport.inline.hpp"
  53 #include "runtime/javaThread.hpp"
  54 #include "runtime/jniHandles.inline.hpp"
  55 #include "runtime/sharedRuntime.hpp"

  56 #include "runtime/stubRoutines.hpp"
  57 #include "utilities/globalDefinitions.hpp"
  58 #include "utilities/powerOfTwo.hpp"

  59 #ifdef COMPILER1
  60 #include "c1/c1_LIRAssembler.hpp"
  61 #endif
  62 #ifdef COMPILER2
  63 #include "oops/oop.hpp"
  64 #include "opto/compile.hpp"
  65 #include "opto/node.hpp"
  66 #include "opto/output.hpp"
  67 #endif
  68 
  69 #include <sys/types.h>
  70 
  71 #ifdef PRODUCT
  72 #define BLOCK_COMMENT(str) /* nothing */
  73 #else
  74 #define BLOCK_COMMENT(str) block_comment(str)
  75 #endif
  76 #define STOP(str) stop(str);
  77 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  78 

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 

5397   if (as_raw) {
5398     bs->BarrierSetAssembler::load_at(this, decorators, type, dst, src, tmp1, tmp2);
5399   } else {
5400     bs->load_at(this, decorators, type, dst, src, tmp1, tmp2);
5401   }
5402 }
5403 
5404 void MacroAssembler::access_store_at(BasicType type, DecoratorSet decorators,
5405                                      Address dst, Register val,
5406                                      Register tmp1, Register tmp2, Register tmp3) {
5407   BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
5408   decorators = AccessInternal::decorator_fixup(decorators, type);
5409   bool as_raw = (decorators & AS_RAW) != 0;
5410   if (as_raw) {
5411     bs->BarrierSetAssembler::store_at(this, decorators, type, dst, val, tmp1, tmp2, tmp3);
5412   } else {
5413     bs->store_at(this, decorators, type, dst, val, tmp1, tmp2, tmp3);
5414   }
5415 }
5416 








































5417 void MacroAssembler::load_heap_oop(Register dst, Address src, Register tmp1,
5418                                    Register tmp2, DecoratorSet decorators) {
5419   access_load_at(T_OBJECT, IN_HEAP | decorators, dst, src, tmp1, tmp2);
5420 }
5421 
5422 void MacroAssembler::load_heap_oop_not_null(Register dst, Address src, Register tmp1,
5423                                             Register tmp2, DecoratorSet decorators) {
5424   access_load_at(T_OBJECT, IN_HEAP | IS_NOT_NULL | decorators, dst, src, tmp1, tmp2);
5425 }
5426 
5427 void MacroAssembler::store_heap_oop(Address dst, Register val, Register tmp1,
5428                                     Register tmp2, Register tmp3, DecoratorSet decorators) {
5429   access_store_at(T_OBJECT, IN_HEAP | decorators, dst, val, tmp1, tmp2, tmp3);
5430 }
5431 
5432 // Used for storing nulls.
5433 void MacroAssembler::store_heap_oop_null(Address dst) {
5434   access_store_at(T_OBJECT, IN_HEAP, dst, noreg, noreg, noreg, noreg);
5435 }
5436 

5472     oop_index = oop_recorder()->allocate_metadata_index(obj);
5473   } else {
5474     oop_index = oop_recorder()->find_index(obj);
5475   }
5476   RelocationHolder rspec = metadata_Relocation::spec(oop_index);
5477   mov(dst, Address((address)obj, rspec));
5478 }
5479 
5480 Address MacroAssembler::constant_oop_address(jobject obj) {
5481 #ifdef ASSERT
5482   {
5483     ThreadInVMfromUnknown tiv;
5484     assert(oop_recorder() != nullptr, "this assembler needs an OopRecorder");
5485     assert(Universe::heap()->is_in(JNIHandles::resolve(obj)), "not an oop");
5486   }
5487 #endif
5488   int oop_index = oop_recorder()->find_index(obj);
5489   return Address((address)obj, oop_Relocation::spec(oop_index));
5490 }
5491 






































































































5492 // Defines obj, preserves var_size_in_bytes, okay for t2 == var_size_in_bytes.
5493 void MacroAssembler::tlab_allocate(Register obj,
5494                                    Register var_size_in_bytes,
5495                                    int con_size_in_bytes,
5496                                    Register t1,
5497                                    Register t2,
5498                                    Label& slow_case) {
5499   BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
5500   bs->tlab_allocate(this, obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
5501 }
5502 
5503 void MacroAssembler::verify_tlab() {
5504 #ifdef ASSERT
5505   if (UseTLAB && VerifyOops) {
5506     Label next, ok;
5507 
5508     stp(rscratch2, rscratch1, Address(pre(sp, -16)));
5509 
5510     ldr(rscratch2, Address(rthread, in_bytes(JavaThread::tlab_top_offset())));
5511     ldr(rscratch1, Address(rthread, in_bytes(JavaThread::tlab_start_offset())));
5512     cmp(rscratch2, rscratch1);
5513     br(Assembler::HS, next);
5514     STOP("assert(top >= start)");
5515     should_not_reach_here();
5516 
5517     bind(next);
5518     ldr(rscratch2, Address(rthread, in_bytes(JavaThread::tlab_end_offset())));
5519     ldr(rscratch1, Address(rthread, in_bytes(JavaThread::tlab_top_offset())));
5520     cmp(rscratch2, rscratch1);
5521     br(Assembler::HS, ok);
5522     STOP("assert(top <= end)");
5523     should_not_reach_here();
5524 
5525     bind(ok);
5526     ldp(rscratch2, rscratch1, Address(post(sp, 16)));
5527   }
5528 #endif
5529 }
5530 




















5531 // Writes to stack successive pages until offset reached to check for
5532 // stack overflow + shadow pages.  This clobbers tmp.
5533 void MacroAssembler::bang_stack_size(Register size, Register tmp) {
5534   assert_different_registers(tmp, size, rscratch1);
5535   mov(tmp, sp);
5536   // Bang stack for total size given plus shadow page size.
5537   // Bang one page at a time because large size can bang beyond yellow and
5538   // red zones.
5539   Label loop;
5540   mov(rscratch1, (int)os::vm_page_size());
5541   bind(loop);
5542   lea(tmp, Address(tmp, -(int)os::vm_page_size()));
5543   subsw(size, size, rscratch1);
5544   str(size, Address(tmp));
5545   br(Assembler::GT, loop);
5546 
5547   // Bang down shadow pages too.
5548   // At this point, (tmp-0) is the last address touched, so don't
5549   // touch it again.  (It was touched as (tmp-pagesize) but then tmp
5550   // was post-decremented.)  Skip this address by starting at i=1, and

5597   } else {
5598     uint64_t target = (uint64_t)dest.target();
5599     uint64_t adrp_target
5600       = (target & 0xffffffffULL) | ((uint64_t)pc() & 0xffff00000000ULL);
5601 
5602     _adrp(reg1, (address)adrp_target);
5603     movk(reg1, target >> 32, 32);
5604   }
5605   byte_offset = (uint64_t)dest.target() & 0xfff;
5606 }
5607 
5608 void MacroAssembler::load_byte_map_base(Register reg) {
5609   CardTable::CardValue* byte_map_base =
5610     ((CardTableBarrierSet*)(BarrierSet::barrier_set()))->card_table()->byte_map_base();
5611 
5612   // Strictly speaking the byte_map_base isn't an address at all, and it might
5613   // even be negative. It is thus materialised as a constant.
5614   mov(reg, (uint64_t)byte_map_base);
5615 }
5616 

5617 void MacroAssembler::build_frame(int framesize) {





5618   assert(framesize >= 2 * wordSize, "framesize must include space for FP/LR");
5619   assert(framesize % (2*wordSize) == 0, "must preserve 2*wordSize alignment");
5620   protect_return_address();
5621   if (framesize < ((1 << 9) + 2 * wordSize)) {
5622     sub(sp, sp, framesize);
5623     stp(rfp, lr, Address(sp, framesize - 2 * wordSize));





5624     if (PreserveFramePointer) add(rfp, sp, framesize - 2 * wordSize);
5625   } else {
5626     stp(rfp, lr, Address(pre(sp, -2 * wordSize)));





5627     if (PreserveFramePointer) mov(rfp, sp);
5628     if (framesize < ((1 << 12) + 2 * wordSize))
5629       sub(sp, sp, framesize - 2 * wordSize);
5630     else {
5631       mov(rscratch1, framesize - 2 * wordSize);
5632       sub(sp, sp, rscratch1);
5633     }
5634   }
5635   verify_cross_modify_fence_not_required();
5636 }
5637 
5638 void MacroAssembler::remove_frame(int framesize) {
5639   assert(framesize >= 2 * wordSize, "framesize must include space for FP/LR");
5640   assert(framesize % (2*wordSize) == 0, "must preserve 2*wordSize alignment");
5641   if (framesize < ((1 << 9) + 2 * wordSize)) {
5642     ldp(rfp, lr, Address(sp, framesize - 2 * wordSize));
5643     add(sp, sp, framesize);
5644   } else {
5645     if (framesize < ((1 << 12) + 2 * wordSize))
5646       add(sp, sp, framesize - 2 * wordSize);
5647     else {
5648       mov(rscratch1, framesize - 2 * wordSize);
5649       add(sp, sp, rscratch1);
5650     }
5651     ldp(rfp, lr, Address(post(sp, 2 * wordSize)));
5652   }
5653   authenticate_return_address();
5654 }
5655 


















































































5656 
5657 // This method counts leading positive bytes (highest bit not set) in provided byte array
5658 address MacroAssembler::count_positives(Register ary1, Register len, Register result) {
5659     // Simple and most common case of aligned small array which is not at the
5660     // end of memory page is placed here. All other cases are in stub.
5661     Label LOOP, END, STUB, STUB_LONG, SET_RESULT, DONE;
5662     const uint64_t UPPER_BIT_MASK=0x8080808080808080;
5663     assert_different_registers(ary1, len, result);
5664 
5665     mov(result, len);
5666     cmpw(len, 0);
5667     br(LE, DONE);
5668     cmpw(len, 4 * wordSize);
5669     br(GE, STUB_LONG); // size > 32 then go to stub
5670 
5671     int shift = 64 - exact_log2(os::vm_page_size());
5672     lsl(rscratch1, ary1, shift);
5673     mov(rscratch2, (size_t)(4 * wordSize) << shift);
5674     adds(rscratch2, rscratch1, rscratch2);  // At end of page?
5675     br(CS, STUB); // at the end of page then go to stub

6553 // On other systems, the helper is a usual C function.
6554 //
6555 void MacroAssembler::get_thread(Register dst) {
6556   RegSet saved_regs =
6557     LINUX_ONLY(RegSet::range(r0, r1)  + lr - dst)
6558     NOT_LINUX (RegSet::range(r0, r17) + lr - dst);
6559 
6560   protect_return_address();
6561   push(saved_regs, sp);
6562 
6563   mov(lr, ExternalAddress(CAST_FROM_FN_PTR(address, JavaThread::aarch64_get_thread_helper)));
6564   blr(lr);
6565   if (dst != c_rarg0) {
6566     mov(dst, c_rarg0);
6567   }
6568 
6569   pop(saved_regs, sp);
6570   authenticate_return_address();
6571 }
6572 









































































































































































































































































































































































































































































6573 void MacroAssembler::cache_wb(Address line) {
6574   assert(line.getMode() == Address::base_plus_offset, "mode should be base_plus_offset");
6575   assert(line.index() == noreg, "index should be noreg");
6576   assert(line.offset() == 0, "offset should be 0");
6577   // would like to assert this
6578   // assert(line._ext.shift == 0, "shift should be zero");
6579   if (VM_Version::supports_dcpop()) {
6580     // writeback using clear virtual address to point of persistence
6581     dc(Assembler::CVAP, line.base());
6582   } else {
6583     // no need to generate anything as Unsafe.writebackMemory should
6584     // never invoke this stub
6585   }
6586 }
6587 
6588 void MacroAssembler::cache_wbsync(bool is_pre) {
6589   // we only need a barrier post sync
6590   if (!is_pre) {
6591     membar(Assembler::AnyAny);
6592   }

6963   }
6964 
6965   // Check if the lock-stack is full.
6966   ldrw(top, Address(rthread, JavaThread::lock_stack_top_offset()));
6967   cmpw(top, (unsigned)LockStack::end_offset());
6968   br(Assembler::GE, slow);
6969 
6970   // Check for recursion.
6971   subw(t, top, oopSize);
6972   ldr(t, Address(rthread, t));
6973   cmp(obj, t);
6974   br(Assembler::EQ, push);
6975 
6976   // Check header for monitor (0b10).
6977   tst(mark, markWord::monitor_value);
6978   br(Assembler::NE, slow);
6979 
6980   // Try to lock. Transition lock bits 0b01 => 0b00
6981   assert(oopDesc::mark_offset_in_bytes() == 0, "required to avoid lea");
6982   orr(mark, mark, markWord::unlocked_value);



6983   eor(t, mark, markWord::unlocked_value);
6984   cmpxchg(/*addr*/ obj, /*expected*/ mark, /*new*/ t, Assembler::xword,
6985           /*acquire*/ true, /*release*/ false, /*weak*/ false, noreg);
6986   br(Assembler::NE, slow);
6987 
6988   bind(push);
6989   // After successful lock, push object on lock-stack.
6990   str(obj, Address(rthread, top));
6991   addw(top, top, oopSize);
6992   strw(top, Address(rthread, JavaThread::lock_stack_top_offset()));
6993 }
6994 
6995 // Implements fast-unlocking.
6996 //
6997 // - obj: the object to be unlocked
6998 // - t1, t2, t3: temporary registers
6999 // - slow: branched to if unlocking fails, absolute offset may larger than 32KB (imm14 encoding).
7000 void MacroAssembler::fast_unlock(Register obj, Register t1, Register t2, Register t3, Label& slow) {
7001   // cmpxchg clobbers rscratch1.
7002   assert_different_registers(obj, t1, t2, t3, rscratch1);

   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 "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/arguments.hpp"
  53 #include "runtime/continuation.hpp"
  54 #include "runtime/globals.hpp"
  55 #include "runtime/icache.hpp"
  56 #include "runtime/interfaceSupport.inline.hpp"
  57 #include "runtime/javaThread.hpp"
  58 #include "runtime/jniHandles.inline.hpp"
  59 #include "runtime/sharedRuntime.hpp"
  60 #include "runtime/signature_cc.hpp"
  61 #include "runtime/stubRoutines.hpp"
  62 #include "utilities/globalDefinitions.hpp"
  63 #include "utilities/powerOfTwo.hpp"
  64 #include "vmreg_aarch64.inline.hpp"
  65 #ifdef COMPILER1
  66 #include "c1/c1_LIRAssembler.hpp"
  67 #endif
  68 #ifdef COMPILER2
  69 #include "oops/oop.hpp"
  70 #include "opto/compile.hpp"
  71 #include "opto/node.hpp"
  72 #include "opto/output.hpp"
  73 #endif
  74 
  75 #include <sys/types.h>
  76 
  77 #ifdef PRODUCT
  78 #define BLOCK_COMMENT(str) /* nothing */
  79 #else
  80 #define BLOCK_COMMENT(str) block_comment(str)
  81 #endif
  82 #define STOP(str) stop(str);
  83 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  84 

2017   ldarb(scratch, scratch);
2018   cmp(scratch, InstanceKlass::fully_initialized);
2019   br(Assembler::EQ, *L_fast_path);
2020 
2021   // Fast path check: current thread is initializer thread
2022   ldr(scratch, Address(klass, InstanceKlass::init_thread_offset()));
2023   cmp(rthread, scratch);
2024 
2025   if (L_slow_path == &L_fallthrough) {
2026     br(Assembler::EQ, *L_fast_path);
2027     bind(*L_slow_path);
2028   } else if (L_fast_path == &L_fallthrough) {
2029     br(Assembler::NE, *L_slow_path);
2030     bind(*L_fast_path);
2031   } else {
2032     Unimplemented();
2033   }
2034 }
2035 
2036 void MacroAssembler::_verify_oop(Register reg, const char* s, const char* file, int line) {
2037   if (!VerifyOops || VerifyAdapterSharing) {
2038     // Below address of the code string confuses VerifyAdapterSharing
2039     // because it may differ between otherwise equivalent adapters.
2040     return;
2041   }
2042 
2043   // Pass register number to verify_oop_subroutine
2044   const char* b = nullptr;
2045   {
2046     ResourceMark rm;
2047     stringStream ss;
2048     ss.print("verify_oop: %s: %s (%s:%d)", reg->name(), s, file, line);
2049     b = code_string(ss.as_string());
2050   }
2051   BLOCK_COMMENT("verify_oop {");
2052 
2053   strip_return_address(); // This might happen within a stack frame.
2054   protect_return_address();
2055   stp(r0, rscratch1, Address(pre(sp, -2 * wordSize)));
2056   stp(rscratch2, lr, Address(pre(sp, -2 * wordSize)));
2057 
2058   mov(r0, reg);
2059   movptr(rscratch1, (uintptr_t)(address)b);
2060 
2061   // call indirectly to solve generation ordering problem
2062   lea(rscratch2, RuntimeAddress(StubRoutines::verify_oop_subroutine_entry_address()));
2063   ldr(rscratch2, Address(rscratch2));
2064   blr(rscratch2);
2065 
2066   ldp(rscratch2, lr, Address(post(sp, 2 * wordSize)));
2067   ldp(r0, rscratch1, Address(post(sp, 2 * wordSize)));
2068   authenticate_return_address();
2069 
2070   BLOCK_COMMENT("} verify_oop");
2071 }
2072 
2073 void MacroAssembler::_verify_oop_addr(Address addr, const char* s, const char* file, int line) {
2074   if (!VerifyOops || VerifyAdapterSharing) {
2075     // Below address of the code string confuses VerifyAdapterSharing
2076     // because it may differ between otherwise equivalent adapters.
2077     return;
2078   }
2079 
2080   const char* b = nullptr;
2081   {
2082     ResourceMark rm;
2083     stringStream ss;
2084     ss.print("verify_oop_addr: %s (%s:%d)", s, file, line);
2085     b = code_string(ss.as_string());
2086   }
2087   BLOCK_COMMENT("verify_oop_addr {");
2088 
2089   strip_return_address(); // This might happen within a stack frame.
2090   protect_return_address();
2091   stp(r0, rscratch1, Address(pre(sp, -2 * wordSize)));
2092   stp(rscratch2, lr, Address(pre(sp, -2 * wordSize)));
2093 
2094   // addr may contain sp so we will have to adjust it based on the
2095   // pushes that we just did.
2096   if (addr.uses(sp)) {
2097     lea(r0, addr);
2098     ldr(r0, Address(r0, 4 * wordSize));

2156   call_VM_leaf_base(entry_point, 1);
2157 }
2158 
2159 void MacroAssembler::call_VM_leaf(address entry_point, Register arg_0, Register arg_1) {
2160   assert_different_registers(arg_1, c_rarg0);
2161   pass_arg0(this, arg_0);
2162   pass_arg1(this, arg_1);
2163   call_VM_leaf_base(entry_point, 2);
2164 }
2165 
2166 void MacroAssembler::call_VM_leaf(address entry_point, Register arg_0,
2167                                   Register arg_1, Register arg_2) {
2168   assert_different_registers(arg_1, c_rarg0);
2169   assert_different_registers(arg_2, c_rarg0, c_rarg1);
2170   pass_arg0(this, arg_0);
2171   pass_arg1(this, arg_1);
2172   pass_arg2(this, arg_2);
2173   call_VM_leaf_base(entry_point, 3);
2174 }
2175 
2176 void MacroAssembler::super_call_VM_leaf(address entry_point) {
2177   MacroAssembler::call_VM_leaf_base(entry_point, 1);
2178 }
2179 
2180 void MacroAssembler::super_call_VM_leaf(address entry_point, Register arg_0) {
2181   pass_arg0(this, arg_0);
2182   MacroAssembler::call_VM_leaf_base(entry_point, 1);
2183 }
2184 
2185 void MacroAssembler::super_call_VM_leaf(address entry_point, Register arg_0, Register arg_1) {
2186 
2187   assert_different_registers(arg_0, c_rarg1);
2188   pass_arg1(this, arg_1);
2189   pass_arg0(this, arg_0);
2190   MacroAssembler::call_VM_leaf_base(entry_point, 2);
2191 }
2192 
2193 void MacroAssembler::super_call_VM_leaf(address entry_point, Register arg_0, Register arg_1, Register arg_2) {
2194   assert_different_registers(arg_0, c_rarg1, c_rarg2);
2195   assert_different_registers(arg_1, c_rarg2);
2196   pass_arg2(this, arg_2);
2197   pass_arg1(this, arg_1);
2198   pass_arg0(this, arg_0);
2199   MacroAssembler::call_VM_leaf_base(entry_point, 3);

2205   assert_different_registers(arg_2, c_rarg3);
2206   pass_arg3(this, arg_3);
2207   pass_arg2(this, arg_2);
2208   pass_arg1(this, arg_1);
2209   pass_arg0(this, arg_0);
2210   MacroAssembler::call_VM_leaf_base(entry_point, 4);
2211 }
2212 
2213 void MacroAssembler::null_check(Register reg, int offset) {
2214   if (needs_explicit_null_check(offset)) {
2215     // provoke OS null exception if reg is null by
2216     // accessing M[reg] w/o changing any registers
2217     // NOTE: this is plenty to provoke a segv
2218     ldr(zr, Address(reg));
2219   } else {
2220     // nothing to do, (later) access of M[reg + offset]
2221     // will provoke OS null exception if reg is null
2222   }
2223 }
2224 
2225 void MacroAssembler::test_markword_is_inline_type(Register markword, Label& is_inline_type) {
2226   assert_different_registers(markword, rscratch2);
2227   mov(rscratch2, markWord::inline_type_mask_in_place);
2228   andr(markword, markword, rscratch2);
2229   mov(rscratch2, markWord::inline_type_pattern);
2230   cmp(markword, rscratch2);
2231   br(Assembler::EQ, is_inline_type);
2232 }
2233 
2234 void MacroAssembler::test_oop_is_not_inline_type(Register object, Register tmp, Label& not_inline_type, bool can_be_null) {
2235   assert_different_registers(tmp, rscratch1);
2236   if (can_be_null) {
2237     cbz(object, not_inline_type);
2238   }
2239   const int is_inline_type_mask = markWord::inline_type_pattern;
2240   ldr(tmp, Address(object, oopDesc::mark_offset_in_bytes()));
2241   mov(rscratch1, is_inline_type_mask);
2242   andr(tmp, tmp, rscratch1);
2243   cmp(tmp, rscratch1);
2244   br(Assembler::NE, not_inline_type);
2245 }
2246 
2247 void MacroAssembler::test_field_is_null_free_inline_type(Register flags, Register temp_reg, Label& is_null_free_inline_type) {
2248   assert(temp_reg == noreg, "not needed"); // keep signature uniform with x86
2249   tbnz(flags, ResolvedFieldEntry::is_null_free_inline_type_shift, is_null_free_inline_type);
2250 }
2251 
2252 void MacroAssembler::test_field_is_not_null_free_inline_type(Register flags, Register temp_reg, Label& not_null_free_inline_type) {
2253   assert(temp_reg == noreg, "not needed"); // keep signature uniform with x86
2254   tbz(flags, ResolvedFieldEntry::is_null_free_inline_type_shift, not_null_free_inline_type);
2255 }
2256 
2257 void MacroAssembler::test_field_is_flat(Register flags, Register temp_reg, Label& is_flat) {
2258   assert(temp_reg == noreg, "not needed"); // keep signature uniform with x86
2259   tbnz(flags, ResolvedFieldEntry::is_flat_shift, is_flat);
2260 }
2261 
2262 void MacroAssembler::test_field_has_null_marker(Register flags, Register temp_reg, Label& has_null_marker) {
2263   assert(temp_reg == noreg, "not needed"); // keep signature uniform with x86
2264   tbnz(flags, ResolvedFieldEntry::has_null_marker_shift, has_null_marker);
2265 }
2266 
2267 void MacroAssembler::test_oop_prototype_bit(Register oop, Register temp_reg, int32_t test_bit, bool jmp_set, Label& jmp_label) {
2268   Label test_mark_word;
2269   // load mark word
2270   ldr(temp_reg, Address(oop, oopDesc::mark_offset_in_bytes()));
2271   // check displaced
2272   tst(temp_reg, markWord::unlocked_value);
2273   br(Assembler::NE, test_mark_word);
2274   // slow path use klass prototype
2275   load_prototype_header(temp_reg, oop);
2276 
2277   bind(test_mark_word);
2278   andr(temp_reg, temp_reg, test_bit);
2279   if (jmp_set) {
2280     cbnz(temp_reg, jmp_label);
2281   } else {
2282     cbz(temp_reg, jmp_label);
2283   }
2284 }
2285 
2286 void MacroAssembler::test_flat_array_oop(Register oop, Register temp_reg, Label& is_flat_array) {
2287   test_oop_prototype_bit(oop, temp_reg, markWord::flat_array_bit_in_place, true, is_flat_array);
2288 }
2289 
2290 void MacroAssembler::test_non_flat_array_oop(Register oop, Register temp_reg,
2291                                                   Label&is_non_flat_array) {
2292   test_oop_prototype_bit(oop, temp_reg, markWord::flat_array_bit_in_place, false, is_non_flat_array);
2293 }
2294 
2295 void MacroAssembler::test_null_free_array_oop(Register oop, Register temp_reg, Label& is_null_free_array) {
2296   test_oop_prototype_bit(oop, temp_reg, markWord::null_free_array_bit_in_place, true, is_null_free_array);
2297 }
2298 
2299 void MacroAssembler::test_non_null_free_array_oop(Register oop, Register temp_reg, Label&is_non_null_free_array) {
2300   test_oop_prototype_bit(oop, temp_reg, markWord::null_free_array_bit_in_place, false, is_non_null_free_array);
2301 }
2302 
2303 void MacroAssembler::test_flat_array_layout(Register lh, Label& is_flat_array) {
2304   tst(lh, Klass::_lh_array_tag_flat_value_bit_inplace);
2305   br(Assembler::NE, is_flat_array);
2306 }
2307 
2308 void MacroAssembler::test_non_flat_array_layout(Register lh, Label& is_non_flat_array) {
2309   tst(lh, Klass::_lh_array_tag_flat_value_bit_inplace);
2310   br(Assembler::EQ, is_non_flat_array);
2311 }
2312 
2313 // MacroAssembler protected routines needed to implement
2314 // public methods
2315 
2316 void MacroAssembler::mov(Register r, Address dest) {
2317   code_section()->relocate(pc(), dest.rspec());
2318   uint64_t imm64 = (uint64_t)dest.target();
2319   movptr(r, imm64);
2320 }
2321 
2322 // Move a constant pointer into r.  In AArch64 mode the virtual
2323 // address space is 48 bits in size, so we only need three
2324 // instructions to create a patchable instruction sequence that can
2325 // reach anywhere.
2326 void MacroAssembler::movptr(Register r, uintptr_t imm64) {
2327 #ifndef PRODUCT
2328   {
2329     char buffer[64];
2330     os::snprintf_checked(buffer, sizeof(buffer), "0x%" PRIX64, (uint64_t)imm64);
2331     block_comment(buffer);
2332   }

4993   adrp(rscratch1, src2, offset);
4994   ldr(rscratch1, Address(rscratch1, offset));
4995   cmp(src1, rscratch1);
4996 }
4997 
4998 void MacroAssembler::cmpoop(Register obj1, Register obj2) {
4999   cmp(obj1, obj2);
5000 }
5001 
5002 void MacroAssembler::load_method_holder_cld(Register rresult, Register rmethod) {
5003   load_method_holder(rresult, rmethod);
5004   ldr(rresult, Address(rresult, InstanceKlass::class_loader_data_offset()));
5005 }
5006 
5007 void MacroAssembler::load_method_holder(Register holder, Register method) {
5008   ldr(holder, Address(method, Method::const_offset()));                      // ConstMethod*
5009   ldr(holder, Address(holder, ConstMethod::constants_offset()));             // ConstantPool*
5010   ldr(holder, Address(holder, ConstantPool::pool_holder_offset()));          // InstanceKlass*
5011 }
5012 
5013 void MacroAssembler::load_metadata(Register dst, Register src) {
5014   if (UseCompactObjectHeaders) {
5015     load_narrow_klass_compact(dst, src);
5016   } else if (UseCompressedClassPointers) {
5017     ldrw(dst, Address(src, oopDesc::klass_offset_in_bytes()));
5018   } else {
5019     ldr(dst, Address(src, oopDesc::klass_offset_in_bytes()));
5020   }
5021 }
5022 
5023 // Loads the obj's Klass* into dst.
5024 // Preserves all registers (incl src, rscratch1 and rscratch2).
5025 // Input:
5026 // src - the oop we want to load the klass from.
5027 // dst - output narrow klass.
5028 void MacroAssembler::load_narrow_klass_compact(Register dst, Register src) {
5029   assert(UseCompactObjectHeaders, "expects UseCompactObjectHeaders");
5030   ldr(dst, Address(src, oopDesc::mark_offset_in_bytes()));
5031   lsr(dst, dst, markWord::klass_shift);
5032 }
5033 
5034 void MacroAssembler::load_klass(Register dst, Register src) {
5035   if (UseCompactObjectHeaders) {
5036     load_narrow_klass_compact(dst, src);
5037     decode_klass_not_null(dst);
5038   } else if (UseCompressedClassPointers) {
5039     ldrw(dst, Address(src, oopDesc::klass_offset_in_bytes()));
5040     decode_klass_not_null(dst);
5041   } else {
5042     ldr(dst, Address(src, oopDesc::klass_offset_in_bytes()));

5113   }
5114   cmp(klass, tmp);
5115 }
5116 
5117 void MacroAssembler::cmp_klasses_from_objects(Register obj1, Register obj2, Register tmp1, Register tmp2) {
5118   if (UseCompactObjectHeaders) {
5119     load_narrow_klass_compact(tmp1, obj1);
5120     load_narrow_klass_compact(tmp2,  obj2);
5121     cmpw(tmp1, tmp2);
5122   } else if (UseCompressedClassPointers) {
5123     ldrw(tmp1, Address(obj1, oopDesc::klass_offset_in_bytes()));
5124     ldrw(tmp2, Address(obj2, oopDesc::klass_offset_in_bytes()));
5125     cmpw(tmp1, tmp2);
5126   } else {
5127     ldr(tmp1, Address(obj1, oopDesc::klass_offset_in_bytes()));
5128     ldr(tmp2, Address(obj2, oopDesc::klass_offset_in_bytes()));
5129     cmp(tmp1, tmp2);
5130   }
5131 }
5132 
5133 void MacroAssembler::load_prototype_header(Register dst, Register src) {
5134   load_klass(dst, src);
5135   ldr(dst, Address(dst, Klass::prototype_header_offset()));
5136 }
5137 
5138 void MacroAssembler::store_klass(Register dst, Register src) {
5139   // FIXME: Should this be a store release?  concurrent gcs assumes
5140   // klass length is valid if klass field is not null.
5141   assert(!UseCompactObjectHeaders, "not with compact headers");
5142   if (UseCompressedClassPointers) {
5143     encode_klass_not_null(src);
5144     strw(src, Address(dst, oopDesc::klass_offset_in_bytes()));
5145   } else {
5146     str(src, Address(dst, oopDesc::klass_offset_in_bytes()));
5147   }
5148 }
5149 
5150 void MacroAssembler::store_klass_gap(Register dst, Register src) {
5151   assert(!UseCompactObjectHeaders, "not with compact headers");
5152   if (UseCompressedClassPointers) {
5153     // Store to klass gap in destination
5154     strw(src, Address(dst, oopDesc::klass_gap_offset_in_bytes()));
5155   }
5156 }
5157 

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, InlineKlass::adr_members_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 || Arguments::is_valhalla_enabled()) {
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

5880   } else {
5881     uint64_t target = (uint64_t)dest.target();
5882     uint64_t adrp_target
5883       = (target & 0xffffffffULL) | ((uint64_t)pc() & 0xffff00000000ULL);
5884 
5885     _adrp(reg1, (address)adrp_target);
5886     movk(reg1, target >> 32, 32);
5887   }
5888   byte_offset = (uint64_t)dest.target() & 0xfff;
5889 }
5890 
5891 void MacroAssembler::load_byte_map_base(Register reg) {
5892   CardTable::CardValue* byte_map_base =
5893     ((CardTableBarrierSet*)(BarrierSet::barrier_set()))->card_table()->byte_map_base();
5894 
5895   // Strictly speaking the byte_map_base isn't an address at all, and it might
5896   // even be negative. It is thus materialised as a constant.
5897   mov(reg, (uint64_t)byte_map_base);
5898 }
5899 
5900 #ifdef ASSERT
5901 void MacroAssembler::build_frame(int framesize) {
5902   build_frame(framesize, false);
5903 }
5904 #endif
5905 
5906 void MacroAssembler::build_frame(int framesize DEBUG_ONLY(COMMA bool zap_rfp_lr_spills)) {
5907   assert(framesize >= 2 * wordSize, "framesize must include space for FP/LR");
5908   assert(framesize % (2*wordSize) == 0, "must preserve 2*wordSize alignment");
5909   protect_return_address();
5910   if (framesize < ((1 << 9) + 2 * wordSize)) {
5911     sub(sp, sp, framesize);
5912     if (DEBUG_ONLY(zap_rfp_lr_spills ||) false) {
5913       mov_immediate64(rscratch1, ((uint64_t)badRegWordVal) << 32 | (uint64_t)badRegWordVal);
5914       stp(rscratch1, rscratch1, Address(sp, framesize - 2 * wordSize));
5915     } else {
5916       stp(rfp, lr, Address(sp, framesize - 2 * wordSize));
5917     }
5918     if (PreserveFramePointer) add(rfp, sp, framesize - 2 * wordSize);
5919   } else {
5920     if (DEBUG_ONLY(zap_rfp_lr_spills ||) false) {
5921       mov_immediate64(rscratch1, ((uint64_t)badRegWordVal) << 32 | (uint64_t)badRegWordVal);
5922       stp(rscratch1, rscratch1, Address(pre(sp, -2 * wordSize)));
5923     } else {
5924       stp(rfp, lr, Address(pre(sp, -2 * wordSize)));
5925     }
5926     if (PreserveFramePointer) mov(rfp, sp);
5927     if (framesize < ((1 << 12) + 2 * wordSize))
5928       sub(sp, sp, framesize - 2 * wordSize);
5929     else {
5930       mov(rscratch1, framesize - 2 * wordSize);
5931       sub(sp, sp, rscratch1);
5932     }
5933   }
5934   verify_cross_modify_fence_not_required();
5935 }
5936 
5937 void MacroAssembler::remove_frame(int framesize) {
5938   assert(framesize >= 2 * wordSize, "framesize must include space for FP/LR");
5939   assert(framesize % (2*wordSize) == 0, "must preserve 2*wordSize alignment");
5940   if (framesize < ((1 << 9) + 2 * wordSize)) {
5941     ldp(rfp, lr, Address(sp, framesize - 2 * wordSize));
5942     add(sp, sp, framesize);
5943   } else {
5944     if (framesize < ((1 << 12) + 2 * wordSize))
5945       add(sp, sp, framesize - 2 * wordSize);
5946     else {
5947       mov(rscratch1, framesize - 2 * wordSize);
5948       add(sp, sp, rscratch1);
5949     }
5950     ldp(rfp, lr, Address(post(sp, 2 * wordSize)));
5951   }
5952   authenticate_return_address();
5953 }
5954 
5955 void MacroAssembler::remove_frame(int initial_framesize, bool needs_stack_repair) {
5956   if (needs_stack_repair) {
5957     // The method has a scalarized entry point (where fields of value object arguments
5958     // are passed through registers and stack), and a non-scalarized entry point (where
5959     // value object arguments are given as oops). The non-scalarized entry point will
5960     // first load each field of value object arguments and store them in registers and on
5961     // the stack in a way compatible with the scalarized entry point. To do so, some extra
5962     // stack space might be reserved (if argument registers are not enough). On leaving the
5963     // method, this space must be freed.
5964     //
5965     // In case we used the non-scalarized entry point the stack looks like this:
5966     //
5967     // | Arguments from caller     |
5968     // |---------------------------|  <-- caller's SP
5969     // | Saved LR #1               |
5970     // | Saved FP #1               |
5971     // |---------------------------|
5972     // | Extension space for       |
5973     // |   inline arg (un)packing  |
5974     // |---------------------------|  <-- start of this method's frame
5975     // | Saved LR #2               |
5976     // | Saved FP #2               |
5977     // |---------------------------|  <-- FP
5978     // | sp_inc                    |
5979     // | method locals             |
5980     // |---------------------------|  <-- SP
5981     //
5982     // There are two copies of FP and LR on the stack. They will be identical at
5983     // first, but that can change.
5984     // If the caller has been deoptimized, LR #1 will be patched to point at the
5985     // deopt blob, and LR #2 will still point into the old method.
5986     // If the saved FP (x29) was not used as the frame pointer, but to store an
5987     // oop, the GC will be aware only of FP #1 as the spilled location of x29 and
5988     // will fix only this one. Overall, FP/LR #2 are not reliable and are simply
5989     // needed to add space between the extension space and the locals, as there
5990     // would be between the real arguments and the locals if we don't need to
5991     // do unpacking (from the scalarized entry point).
5992     //
5993     // When restoring, one must then load FP #1 into x29, and LR #1 into x30,
5994     // while keeping in mind that from the scalarized entry point, there will be
5995     // only one copy of each. Indeed, in the case we used the scalarized calling
5996     // convention, the stack looks like this:
5997     //
5998     // | Arguments from caller     |
5999     // |---------------------------|  <-- caller's SP / start of this method's frame
6000     // | Saved LR                  |
6001     // | Saved FP                  |
6002     // |---------------------------|  <-- FP
6003     // | sp_inc                    |
6004     // | method locals             |
6005     // |---------------------------|  <-- SP
6006     //
6007     // The sp_inc stack slot holds the total size of the frame including the
6008     // extension space minus two words for the saved FP and LR. That is how to
6009     // find FP/LR #1. This size is expressed in bytes. Be careful when using it
6010     // from C++ in pointer arithmetic; you might need to divide it by wordSize.
6011     //
6012     // One can find sp_inc since the start the method's frame is SP + initial_framesize.
6013 
6014     int sp_inc_offset = initial_framesize - 3 * wordSize;  // Immediately below saved LR and FP
6015 
6016     ldr(rscratch1, Address(sp, sp_inc_offset));
6017     add(sp, sp, rscratch1);
6018     ldp(rfp, lr, Address(post(sp, 2 * wordSize)));
6019   } else {
6020     remove_frame(initial_framesize);
6021   }
6022 }
6023 
6024 void MacroAssembler::save_stack_increment(int sp_inc, int frame_size) {
6025   int real_frame_size = frame_size + sp_inc;
6026   assert(sp_inc == 0 || sp_inc > 2*wordSize, "invalid sp_inc value");
6027   assert(real_frame_size >= 2*wordSize, "frame size must include FP/LR space");
6028   assert((real_frame_size & (StackAlignmentInBytes-1)) == 0, "frame size not aligned");
6029 
6030   int sp_inc_offset = frame_size - 3 * wordSize;  // Immediately below saved LR and FP
6031 
6032   // Subtract two words for the saved FP and LR as these will be popped
6033   // separately. See remove_frame above.
6034   mov(rscratch1, real_frame_size - 2*wordSize);
6035   str(rscratch1, Address(sp, sp_inc_offset));
6036 }
6037 
6038 // This method counts leading positive bytes (highest bit not set) in provided byte array
6039 address MacroAssembler::count_positives(Register ary1, Register len, Register result) {
6040     // Simple and most common case of aligned small array which is not at the
6041     // end of memory page is placed here. All other cases are in stub.
6042     Label LOOP, END, STUB, STUB_LONG, SET_RESULT, DONE;
6043     const uint64_t UPPER_BIT_MASK=0x8080808080808080;
6044     assert_different_registers(ary1, len, result);
6045 
6046     mov(result, len);
6047     cmpw(len, 0);
6048     br(LE, DONE);
6049     cmpw(len, 4 * wordSize);
6050     br(GE, STUB_LONG); // size > 32 then go to stub
6051 
6052     int shift = 64 - exact_log2(os::vm_page_size());
6053     lsl(rscratch1, ary1, shift);
6054     mov(rscratch2, (size_t)(4 * wordSize) << shift);
6055     adds(rscratch2, rscratch1, rscratch2);  // At end of page?
6056     br(CS, STUB); // at the end of page then go to stub

6934 // On other systems, the helper is a usual C function.
6935 //
6936 void MacroAssembler::get_thread(Register dst) {
6937   RegSet saved_regs =
6938     LINUX_ONLY(RegSet::range(r0, r1)  + lr - dst)
6939     NOT_LINUX (RegSet::range(r0, r17) + lr - dst);
6940 
6941   protect_return_address();
6942   push(saved_regs, sp);
6943 
6944   mov(lr, ExternalAddress(CAST_FROM_FN_PTR(address, JavaThread::aarch64_get_thread_helper)));
6945   blr(lr);
6946   if (dst != c_rarg0) {
6947     mov(dst, c_rarg0);
6948   }
6949 
6950   pop(saved_regs, sp);
6951   authenticate_return_address();
6952 }
6953 
6954 #ifdef COMPILER2
6955 // C2 compiled method's prolog code
6956 // Moved here from aarch64.ad to support Valhalla code belows
6957 void MacroAssembler::verified_entry(Compile* C, int sp_inc) {
6958   if (C->clinit_barrier_on_entry()) {
6959     assert(!C->method()->holder()->is_not_initialized(), "initialization should have been started");
6960 
6961     Label L_skip_barrier;
6962 
6963     mov_metadata(rscratch2, C->method()->holder()->constant_encoding());
6964     clinit_barrier(rscratch2, rscratch1, &L_skip_barrier);
6965     far_jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub()));
6966     bind(L_skip_barrier);
6967   }
6968 
6969   if (C->max_vector_size() > 0) {
6970     reinitialize_ptrue();
6971   }
6972 
6973   int bangsize = C->output()->bang_size_in_bytes();
6974   if (C->output()->need_stack_bang(bangsize))
6975     generate_stack_overflow_check(bangsize);
6976 
6977   // n.b. frame size includes space for return pc and rfp
6978   const long framesize = C->output()->frame_size_in_bytes();
6979   build_frame(framesize DEBUG_ONLY(COMMA sp_inc != 0));
6980 
6981   if (C->needs_stack_repair()) {
6982     save_stack_increment(sp_inc, framesize);
6983   }
6984 
6985   if (VerifyStackAtCalls) {
6986     Unimplemented();
6987   }
6988 }
6989 #endif // COMPILER2
6990 
6991 int MacroAssembler::store_inline_type_fields_to_buf(ciInlineKlass* vk, bool from_interpreter) {
6992   assert(InlineTypeReturnedAsFields, "Inline types should never be returned as fields");
6993   // An inline type might be returned. If fields are in registers we
6994   // need to allocate an inline type instance and initialize it with
6995   // the value of the fields.
6996   Label skip;
6997   // We only need a new buffered inline type if a new one is not returned
6998   tbz(r0, 0, skip);
6999   int call_offset = -1;
7000 
7001   // Be careful not to clobber r1-7 which hold returned fields
7002   // Also do not use callee-saved registers as these may be live in the interpreter
7003   Register tmp1 = r13, tmp2 = r14, klass = r15, r0_preserved = r12;
7004 
7005   // The following code is similar to allocate_instance but has some slight differences,
7006   // e.g. object size is always not zero, sometimes it's constant; storing klass ptr after
7007   // allocating is not necessary if vk != nullptr, etc. allocate_instance is not aware of these.
7008   Label slow_case;
7009   // 1. Try to allocate a new buffered inline instance either from TLAB or eden space
7010   mov(r0_preserved, r0); // save r0 for slow_case since *_allocate may corrupt it when allocation failed
7011 
7012   if (vk != nullptr) {
7013     // Called from C1, where the return type is statically known.
7014     movptr(klass, (intptr_t)vk->get_InlineKlass());
7015     jint lh = vk->layout_helper();
7016     assert(lh != Klass::_lh_neutral_value, "inline class in return type must have been resolved");
7017     if (UseTLAB && !Klass::layout_helper_needs_slow_path(lh)) {
7018       tlab_allocate(r0, noreg, lh, tmp1, tmp2, slow_case);
7019     } else {
7020       b(slow_case);
7021     }
7022   } else {
7023     // Call from interpreter. R0 contains ((the InlineKlass* of the return type) | 0x01)
7024     andr(klass, r0, -2);
7025     if (UseTLAB) {
7026       ldrw(tmp2, Address(klass, Klass::layout_helper_offset()));
7027       tst(tmp2, Klass::_lh_instance_slow_path_bit);
7028       br(Assembler::NE, slow_case);
7029       tlab_allocate(r0, tmp2, 0, tmp1, tmp2, slow_case);
7030     } else {
7031       b(slow_case);
7032     }
7033   }
7034   if (UseTLAB) {
7035     // 2. Initialize buffered inline instance header
7036     Register buffer_obj = r0;
7037     if (UseCompactObjectHeaders) {
7038       ldr(rscratch1, Address(klass, Klass::prototype_header_offset()));
7039       str(rscratch1, Address(buffer_obj, oopDesc::mark_offset_in_bytes()));
7040     } else {
7041       mov(rscratch1, (intptr_t)markWord::inline_type_prototype().value());
7042       str(rscratch1, Address(buffer_obj, oopDesc::mark_offset_in_bytes()));
7043       store_klass_gap(buffer_obj, zr);
7044       if (vk == nullptr) {
7045         // store_klass corrupts klass, so save it for later use (interpreter case only).
7046         mov(tmp1, klass);
7047       }
7048       store_klass(buffer_obj, klass);
7049       klass = tmp1;
7050     }
7051     // 3. Initialize its fields with an inline class specific handler
7052     if (vk != nullptr) {
7053       far_call(RuntimeAddress(vk->pack_handler())); // no need for call info as this will not safepoint.
7054     } else {
7055       ldr(tmp1, Address(klass, InlineKlass::adr_members_offset()));
7056       ldr(tmp1, Address(tmp1, InlineKlass::pack_handler_offset()));
7057       blr(tmp1);
7058     }
7059 
7060     membar(Assembler::StoreStore);
7061     b(skip);
7062   } else {
7063     // Must have already branched to slow_case above.
7064     DEBUG_ONLY(should_not_reach_here());
7065   }
7066   bind(slow_case);
7067   // We failed to allocate a new inline type, fall back to a runtime
7068   // call. Some oop field may be live in some registers but we can't
7069   // tell. That runtime call will take care of preserving them
7070   // across a GC if there's one.
7071   mov(r0, r0_preserved);
7072 
7073   if (from_interpreter) {
7074     super_call_VM_leaf(StubRoutines::store_inline_type_fields_to_buf());
7075   } else {
7076     far_call(RuntimeAddress(StubRoutines::store_inline_type_fields_to_buf()));
7077     call_offset = offset();
7078   }
7079   membar(Assembler::StoreStore);
7080 
7081   bind(skip);
7082   return call_offset;
7083 }
7084 
7085 // Move a value between registers/stack slots and update the reg_state
7086 bool MacroAssembler::move_helper(VMReg from, VMReg to, BasicType bt, RegState reg_state[]) {
7087   assert(from->is_valid() && to->is_valid(), "source and destination must be valid");
7088   if (reg_state[to->value()] == reg_written) {
7089     return true; // Already written
7090   }
7091 
7092   if (from != to && bt != T_VOID) {
7093     if (reg_state[to->value()] == reg_readonly) {
7094       return false; // Not yet writable
7095     }
7096     if (from->is_reg()) {
7097       if (to->is_reg()) {
7098         if (from->is_Register() && to->is_Register()) {
7099           mov(to->as_Register(), from->as_Register());
7100         } else if (from->is_FloatRegister() && to->is_FloatRegister()) {
7101           fmovd(to->as_FloatRegister(), from->as_FloatRegister());
7102         } else {
7103           ShouldNotReachHere();
7104         }
7105       } else {
7106         int st_off = to->reg2stack() * VMRegImpl::stack_slot_size;
7107         Address to_addr = Address(sp, st_off);
7108         if (from->is_FloatRegister()) {
7109           if (bt == T_DOUBLE) {
7110              strd(from->as_FloatRegister(), to_addr);
7111           } else {
7112              assert(bt == T_FLOAT, "must be float");
7113              strs(from->as_FloatRegister(), to_addr);
7114           }
7115         } else {
7116           str(from->as_Register(), to_addr);
7117         }
7118       }
7119     } else {
7120       Address from_addr = Address(sp, from->reg2stack() * VMRegImpl::stack_slot_size);
7121       if (to->is_reg()) {
7122         if (to->is_FloatRegister()) {
7123           if (bt == T_DOUBLE) {
7124             ldrd(to->as_FloatRegister(), from_addr);
7125           } else {
7126             assert(bt == T_FLOAT, "must be float");
7127             ldrs(to->as_FloatRegister(), from_addr);
7128           }
7129         } else {
7130           ldr(to->as_Register(), from_addr);
7131         }
7132       } else {
7133         int st_off = to->reg2stack() * VMRegImpl::stack_slot_size;
7134         ldr(rscratch1, from_addr);
7135         str(rscratch1, Address(sp, st_off));
7136       }
7137     }
7138   }
7139 
7140   // Update register states
7141   reg_state[from->value()] = reg_writable;
7142   reg_state[to->value()] = reg_written;
7143   return true;
7144 }
7145 
7146 // Calculate the extra stack space required for packing or unpacking inline
7147 // args and adjust the stack pointer
7148 int MacroAssembler::extend_stack_for_inline_args(int args_on_stack) {
7149   int sp_inc = args_on_stack * VMRegImpl::stack_slot_size;
7150   sp_inc = align_up(sp_inc, StackAlignmentInBytes);
7151   assert(sp_inc > 0, "sanity");
7152 
7153   // Save a copy of the FP and LR here for deoptimization patching and frame walking
7154   stp(rfp, lr, Address(pre(sp, -2 * wordSize)));
7155 
7156   // Adjust the stack pointer. This will be repaired on return by MacroAssembler::remove_frame
7157   if (sp_inc < (1 << 9)) {
7158     sub(sp, sp, sp_inc);   // Fits in an immediate
7159   } else {
7160     mov(rscratch1, sp_inc);
7161     sub(sp, sp, rscratch1);
7162   }
7163 
7164   return sp_inc + 2 * wordSize;  // Account for the FP/LR space
7165 }
7166 
7167 // Read all fields from an inline type oop and store the values in registers/stack slots
7168 bool MacroAssembler::unpack_inline_helper(const GrowableArray<SigEntry>* sig, int& sig_index,
7169                                           VMReg from, int& from_index, VMRegPair* to, int to_count, int& to_index,
7170                                           RegState reg_state[]) {
7171   assert(sig->at(sig_index)._bt == T_VOID, "should be at end delimiter");
7172   assert(from->is_valid(), "source must be valid");
7173   bool progress = false;
7174 #ifdef ASSERT
7175   const int start_offset = offset();
7176 #endif
7177 
7178   Label L_null, L_notNull;
7179   // Don't use r14 as tmp because it's used for spilling (see MacroAssembler::spill_reg_for)
7180   Register tmp1 = r10;
7181   Register tmp2 = r11;
7182 
7183 #ifndef ASSERT
7184   RegSet clobbered_gp_regs = MacroAssembler::call_clobbered_gp_registers();
7185   assert(clobbered_gp_regs.contains(tmp1), "tmp1 must be saved explicitly if it's not a clobber");
7186   assert(clobbered_gp_regs.contains(tmp2), "tmp2 must be saved explicitly if it's not a clobber");
7187   assert(clobbered_gp_regs.contains(r14), "r14 must be saved explicitly if it's not a clobber");
7188 #endif
7189 
7190   Register fromReg = noreg;
7191   ScalarizedInlineArgsStream stream(sig, sig_index, to, to_count, to_index, true);
7192   bool done = true;
7193   bool mark_done = true;
7194   VMReg toReg;
7195   BasicType bt;
7196   // Check if argument requires a null check
7197   bool null_check = false;
7198   VMReg nullCheckReg;
7199   while (stream.next(nullCheckReg, bt)) {
7200     if (sig->at(stream.sig_index())._offset == -1) {
7201       null_check = true;
7202       break;
7203     }
7204   }
7205   stream.reset(sig_index, to_index);
7206   while (stream.next(toReg, bt)) {
7207     assert(toReg->is_valid(), "destination must be valid");
7208     int idx = (int)toReg->value();
7209     if (reg_state[idx] == reg_readonly) {
7210       if (idx != from->value()) {
7211         mark_done = false;
7212       }
7213       done = false;
7214       continue;
7215     } else if (reg_state[idx] == reg_written) {
7216       continue;
7217     }
7218     assert(reg_state[idx] == reg_writable, "must be writable");
7219     reg_state[idx] = reg_written;
7220     progress = true;
7221 
7222     if (fromReg == noreg) {
7223       if (from->is_reg()) {
7224         fromReg = from->as_Register();
7225       } else {
7226         int st_off = from->reg2stack() * VMRegImpl::stack_slot_size;
7227         ldr(tmp1, Address(sp, st_off));
7228         fromReg = tmp1;
7229       }
7230       if (null_check) {
7231         // Nullable inline type argument, emit null check
7232         cbz(fromReg, L_null);
7233       }
7234     }
7235     int off = sig->at(stream.sig_index())._offset;
7236     if (off == -1) {
7237       assert(null_check, "Missing null check at");
7238       if (toReg->is_stack()) {
7239         int st_off = toReg->reg2stack() * VMRegImpl::stack_slot_size;
7240         mov(tmp2, 1);
7241         str(tmp2, Address(sp, st_off));
7242       } else {
7243         mov(toReg->as_Register(), 1);
7244       }
7245       continue;
7246     }
7247     assert(off > 0, "offset in object should be positive");
7248     Address fromAddr = Address(fromReg, off);
7249     if (!toReg->is_FloatRegister()) {
7250       Register dst = toReg->is_stack() ? tmp2 : toReg->as_Register();
7251       if (is_reference_type(bt)) {
7252         load_heap_oop(dst, fromAddr, rscratch1, rscratch2);
7253       } else {
7254         bool is_signed = (bt != T_CHAR) && (bt != T_BOOLEAN);
7255         load_sized_value(dst, fromAddr, type2aelembytes(bt), is_signed);
7256       }
7257       if (toReg->is_stack()) {
7258         int st_off = toReg->reg2stack() * VMRegImpl::stack_slot_size;
7259         str(dst, Address(sp, st_off));
7260       }
7261     } else if (bt == T_DOUBLE) {
7262       ldrd(toReg->as_FloatRegister(), fromAddr);
7263     } else {
7264       assert(bt == T_FLOAT, "must be float");
7265       ldrs(toReg->as_FloatRegister(), fromAddr);
7266     }
7267   }
7268   if (progress && null_check) {
7269     if (done) {
7270       b(L_notNull);
7271       bind(L_null);
7272       // Set null marker to zero to signal that the argument is null.
7273       // Also set all oop fields to zero to make the GC happy.
7274       stream.reset(sig_index, to_index);
7275       while (stream.next(toReg, bt)) {
7276         if (sig->at(stream.sig_index())._offset == -1 ||
7277             bt == T_OBJECT || bt == T_ARRAY) {
7278           if (toReg->is_stack()) {
7279             int st_off = toReg->reg2stack() * VMRegImpl::stack_slot_size;
7280             str(zr, Address(sp, st_off));
7281           } else {
7282             mov(toReg->as_Register(), zr);
7283           }
7284         }
7285       }
7286       bind(L_notNull);
7287     } else {
7288       bind(L_null);
7289     }
7290   }
7291 
7292   sig_index = stream.sig_index();
7293   to_index = stream.regs_index();
7294 
7295   if (mark_done && reg_state[from->value()] != reg_written) {
7296     // This is okay because no one else will write to that slot
7297     reg_state[from->value()] = reg_writable;
7298   }
7299   from_index--;
7300   assert(progress || (start_offset == offset()), "should not emit code");
7301   return done;
7302 }
7303 
7304 // Pack fields back into an inline type oop
7305 bool MacroAssembler::pack_inline_helper(const GrowableArray<SigEntry>* sig, int& sig_index, int vtarg_index,
7306                                         VMRegPair* from, int from_count, int& from_index, VMReg to,
7307                                         RegState reg_state[], Register val_array) {
7308   assert(sig->at(sig_index)._bt == T_METADATA, "should be at delimiter");
7309   assert(to->is_valid(), "destination must be valid");
7310 
7311   if (reg_state[to->value()] == reg_written) {
7312     skip_unpacked_fields(sig, sig_index, from, from_count, from_index);
7313     return true; // Already written
7314   }
7315 
7316   // The GC barrier expanded by store_heap_oop below may call into the
7317   // runtime so use callee-saved registers for any values that need to be
7318   // preserved. The GC barrier assembler should take care of saving the
7319   // Java argument registers.
7320   // TODO 8284443 Isn't it an issue if below code uses r14 as tmp when it contains a spilled value?
7321   // Be careful with r14 because it's used for spilling (see MacroAssembler::spill_reg_for).
7322   Register val_obj_tmp = r21;
7323   Register from_reg_tmp = r22;
7324   Register tmp1 = r14;
7325   Register tmp2 = r13;
7326   Register tmp3 = r12;
7327   Register val_obj = to->is_stack() ? val_obj_tmp : to->as_Register();
7328 
7329   assert_different_registers(val_obj_tmp, from_reg_tmp, tmp1, tmp2, tmp3, val_array);
7330 
7331   if (reg_state[to->value()] == reg_readonly) {
7332     if (!is_reg_in_unpacked_fields(sig, sig_index, to, from, from_count, from_index)) {
7333       skip_unpacked_fields(sig, sig_index, from, from_count, from_index);
7334       return false; // Not yet writable
7335     }
7336     val_obj = val_obj_tmp;
7337   }
7338 
7339   int index = arrayOopDesc::base_offset_in_bytes(T_OBJECT) + vtarg_index * type2aelembytes(T_OBJECT);
7340   load_heap_oop(val_obj, Address(val_array, index), tmp1, tmp2);
7341 
7342   ScalarizedInlineArgsStream stream(sig, sig_index, from, from_count, from_index);
7343   VMReg fromReg;
7344   BasicType bt;
7345   Label L_null;
7346   while (stream.next(fromReg, bt)) {
7347     assert(fromReg->is_valid(), "source must be valid");
7348     reg_state[fromReg->value()] = reg_writable;
7349 
7350     int off = sig->at(stream.sig_index())._offset;
7351     if (off == -1) {
7352       // Nullable inline type argument, emit null check
7353       Label L_notNull;
7354       if (fromReg->is_stack()) {
7355         int ld_off = fromReg->reg2stack() * VMRegImpl::stack_slot_size;
7356         ldrb(tmp2, Address(sp, ld_off));
7357         cbnz(tmp2, L_notNull);
7358       } else {
7359         cbnz(fromReg->as_Register(), L_notNull);
7360       }
7361       mov(val_obj, 0);
7362       b(L_null);
7363       bind(L_notNull);
7364       continue;
7365     }
7366 
7367     assert(off > 0, "offset in object should be positive");
7368     size_t size_in_bytes = is_java_primitive(bt) ? type2aelembytes(bt) : wordSize;
7369 
7370     // Pack the scalarized field into the value object.
7371     Address dst(val_obj, off);
7372     if (!fromReg->is_FloatRegister()) {
7373       Register src;
7374       if (fromReg->is_stack()) {
7375         src = from_reg_tmp;
7376         int ld_off = fromReg->reg2stack() * VMRegImpl::stack_slot_size;
7377         load_sized_value(src, Address(sp, ld_off), size_in_bytes, /* is_signed */ false);
7378       } else {
7379         src = fromReg->as_Register();
7380       }
7381       assert_different_registers(dst.base(), src, tmp1, tmp2, tmp3, val_array);
7382       if (is_reference_type(bt)) {
7383         // store_heap_oop transitively calls oop_store_at which corrupts to.base(). We need to keep val_obj valid.
7384         mov(tmp3, val_obj);
7385         Address dst_with_tmp3(tmp3, off);
7386         store_heap_oop(dst_with_tmp3, src, tmp1, tmp2, tmp3, IN_HEAP | ACCESS_WRITE | IS_DEST_UNINITIALIZED);
7387       } else {
7388         store_sized_value(dst, src, size_in_bytes);
7389       }
7390     } else if (bt == T_DOUBLE) {
7391       strd(fromReg->as_FloatRegister(), dst);
7392     } else {
7393       assert(bt == T_FLOAT, "must be float");
7394       strs(fromReg->as_FloatRegister(), dst);
7395     }
7396   }
7397   bind(L_null);
7398   sig_index = stream.sig_index();
7399   from_index = stream.regs_index();
7400 
7401   assert(reg_state[to->value()] == reg_writable, "must have already been read");
7402   bool success = move_helper(val_obj->as_VMReg(), to, T_OBJECT, reg_state);
7403   assert(success, "to register must be writeable");
7404   return true;
7405 }
7406 
7407 VMReg MacroAssembler::spill_reg_for(VMReg reg) {
7408   return (reg->is_FloatRegister()) ? v8->as_VMReg() : r14->as_VMReg();
7409 }
7410 
7411 void MacroAssembler::cache_wb(Address line) {
7412   assert(line.getMode() == Address::base_plus_offset, "mode should be base_plus_offset");
7413   assert(line.index() == noreg, "index should be noreg");
7414   assert(line.offset() == 0, "offset should be 0");
7415   // would like to assert this
7416   // assert(line._ext.shift == 0, "shift should be zero");
7417   if (VM_Version::supports_dcpop()) {
7418     // writeback using clear virtual address to point of persistence
7419     dc(Assembler::CVAP, line.base());
7420   } else {
7421     // no need to generate anything as Unsafe.writebackMemory should
7422     // never invoke this stub
7423   }
7424 }
7425 
7426 void MacroAssembler::cache_wbsync(bool is_pre) {
7427   // we only need a barrier post sync
7428   if (!is_pre) {
7429     membar(Assembler::AnyAny);
7430   }

7801   }
7802 
7803   // Check if the lock-stack is full.
7804   ldrw(top, Address(rthread, JavaThread::lock_stack_top_offset()));
7805   cmpw(top, (unsigned)LockStack::end_offset());
7806   br(Assembler::GE, slow);
7807 
7808   // Check for recursion.
7809   subw(t, top, oopSize);
7810   ldr(t, Address(rthread, t));
7811   cmp(obj, t);
7812   br(Assembler::EQ, push);
7813 
7814   // Check header for monitor (0b10).
7815   tst(mark, markWord::monitor_value);
7816   br(Assembler::NE, slow);
7817 
7818   // Try to lock. Transition lock bits 0b01 => 0b00
7819   assert(oopDesc::mark_offset_in_bytes() == 0, "required to avoid lea");
7820   orr(mark, mark, markWord::unlocked_value);
7821   // Mask inline_type bit such that we go to the slow path if object is an inline type
7822   andr(mark, mark, ~((int) markWord::inline_type_bit_in_place));
7823 
7824   eor(t, mark, markWord::unlocked_value);
7825   cmpxchg(/*addr*/ obj, /*expected*/ mark, /*new*/ t, Assembler::xword,
7826           /*acquire*/ true, /*release*/ false, /*weak*/ false, noreg);
7827   br(Assembler::NE, slow);
7828 
7829   bind(push);
7830   // After successful lock, push object on lock-stack.
7831   str(obj, Address(rthread, top));
7832   addw(top, top, oopSize);
7833   strw(top, Address(rthread, JavaThread::lock_stack_top_offset()));
7834 }
7835 
7836 // Implements fast-unlocking.
7837 //
7838 // - obj: the object to be unlocked
7839 // - t1, t2, t3: temporary registers
7840 // - slow: branched to if unlocking fails, absolute offset may larger than 32KB (imm14 encoding).
7841 void MacroAssembler::fast_unlock(Register obj, Register t1, Register t2, Register t3, Label& slow) {
7842   // cmpxchg clobbers rscratch1.
7843   assert_different_registers(obj, t1, t2, t3, rscratch1);
< prev index next >