1 /*
   2  * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
   4  * Copyright (c) 2020, 2024, Huawei Technologies Co., Ltd. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #ifndef CPU_RISCV_MACROASSEMBLER_RISCV_HPP
  28 #define CPU_RISCV_MACROASSEMBLER_RISCV_HPP
  29 
  30 #include "asm/assembler.inline.hpp"
  31 #include "code/vmreg.hpp"
  32 #include "metaprogramming/enableIf.hpp"
  33 #include "oops/compressedOops.hpp"
  34 #include "utilities/powerOfTwo.hpp"
  35 
  36 // MacroAssembler extends Assembler by frequently used macros.
  37 //
  38 // Instructions for which a 'better' code sequence exists depending
  39 // on arguments should also go in here.
  40 
  41 class MacroAssembler: public Assembler {
  42 
  43  public:
  44 
  45   MacroAssembler(CodeBuffer* code) : Assembler(code) {}
  46 
  47   void safepoint_poll(Label& slow_path, bool at_return, bool acquire, bool in_nmethod);
  48 
  49   // Alignment
  50   int align(int modulus, int extra_offset = 0);
  51 
  52   static inline void assert_alignment(address pc, int alignment = MacroAssembler::instruction_size) {
  53     assert(is_aligned(pc, alignment), "bad alignment");
  54   }
  55 
  56   // nop
  57   void post_call_nop();
  58 
  59   // Stack frame creation/removal
  60   // Note that SP must be updated to the right place before saving/restoring RA and FP
  61   // because signal based thread suspend/resume could happen asynchronously.
  62   void enter() {
  63     addi(sp, sp, - 2 * wordSize);
  64     sd(ra, Address(sp, wordSize));
  65     sd(fp, Address(sp));
  66     addi(fp, sp, 2 * wordSize);
  67   }
  68 
  69   void leave() {
  70     addi(sp, fp, - 2 * wordSize);
  71     ld(fp, Address(sp));
  72     ld(ra, Address(sp, wordSize));
  73     addi(sp, sp, 2 * wordSize);
  74   }
  75 
  76 
  77   // Support for getting the JavaThread pointer (i.e.; a reference to thread-local information)
  78   // The pointer will be loaded into the thread register.
  79   void get_thread(Register thread);
  80 
  81   // Support for VM calls
  82   //
  83   // It is imperative that all calls into the VM are handled via the call_VM macros.
  84   // They make sure that the stack linkage is setup correctly. call_VM's correspond
  85   // to ENTRY/ENTRY_X entry points while call_VM_leaf's correspond to LEAF entry points.
  86 
  87   void call_VM(Register oop_result,
  88                address entry_point,
  89                bool check_exceptions = true);
  90   void call_VM(Register oop_result,
  91                address entry_point,
  92                Register arg_1,
  93                bool check_exceptions = true);
  94   void call_VM(Register oop_result,
  95                address entry_point,
  96                Register arg_1, Register arg_2,
  97                bool check_exceptions = true);
  98   void call_VM(Register oop_result,
  99                address entry_point,
 100                Register arg_1, Register arg_2, Register arg_3,
 101                bool check_exceptions = true);
 102 
 103   // Overloadings with last_Java_sp
 104   void call_VM(Register oop_result,
 105                Register last_java_sp,
 106                address entry_point,
 107                int number_of_arguments = 0,
 108                bool check_exceptions = true);
 109   void call_VM(Register oop_result,
 110                Register last_java_sp,
 111                address entry_point,
 112                Register arg_1,
 113                bool check_exceptions = true);
 114   void call_VM(Register oop_result,
 115                Register last_java_sp,
 116                address entry_point,
 117                Register arg_1, Register arg_2,
 118                bool check_exceptions = true);
 119   void call_VM(Register oop_result,
 120                Register last_java_sp,
 121                address entry_point,
 122                Register arg_1, Register arg_2, Register arg_3,
 123                bool check_exceptions = true);
 124 
 125   void get_vm_result(Register oop_result, Register java_thread);
 126   void get_vm_result_2(Register metadata_result, Register java_thread);
 127 
 128   // These always tightly bind to MacroAssembler::call_VM_leaf_base
 129   // bypassing the virtual implementation
 130   void call_VM_leaf(address entry_point,
 131                     int number_of_arguments = 0);
 132   void call_VM_leaf(address entry_point,
 133                     Register arg_0);
 134   void call_VM_leaf(address entry_point,
 135                     Register arg_0, Register arg_1);
 136   void call_VM_leaf(address entry_point,
 137                     Register arg_0, Register arg_1, Register arg_2);
 138 
 139   // These always tightly bind to MacroAssembler::call_VM_base
 140   // bypassing the virtual implementation
 141   void super_call_VM_leaf(address entry_point, Register arg_0);
 142   void super_call_VM_leaf(address entry_point, Register arg_0, Register arg_1);
 143   void super_call_VM_leaf(address entry_point, Register arg_0, Register arg_1, Register arg_2);
 144   void super_call_VM_leaf(address entry_point, Register arg_0, Register arg_1, Register arg_2, Register arg_3);
 145 
 146   // last Java Frame (fills frame anchor)
 147   void set_last_Java_frame(Register last_java_sp, Register last_java_fp, address last_java_pc, Register tmp);
 148   void set_last_Java_frame(Register last_java_sp, Register last_java_fp, Label &last_java_pc, Register tmp);
 149   void set_last_Java_frame(Register last_java_sp, Register last_java_fp, Register last_java_pc);
 150 
 151   // thread in the default location (xthread)
 152   void reset_last_Java_frame(bool clear_fp);
 153 
 154   virtual void call_VM_leaf_base(
 155     address entry_point,                // the entry point
 156     int     number_of_arguments,        // the number of arguments to pop after the call
 157     Label*  retaddr = nullptr
 158   );
 159 
 160   virtual void call_VM_leaf_base(
 161     address entry_point,                // the entry point
 162     int     number_of_arguments,        // the number of arguments to pop after the call
 163     Label&  retaddr) {
 164     call_VM_leaf_base(entry_point, number_of_arguments, &retaddr);
 165   }
 166 
 167   virtual void call_VM_base(           // returns the register containing the thread upon return
 168     Register oop_result,               // where an oop-result ends up if any; use noreg otherwise
 169     Register java_thread,              // the thread if computed before     ; use noreg otherwise
 170     Register last_java_sp,             // to set up last_Java_frame in stubs; use noreg otherwise
 171     address  entry_point,              // the entry point
 172     int      number_of_arguments,      // the number of arguments (w/o thread) to pop after the call
 173     bool     check_exceptions          // whether to check for pending exceptions after return
 174   );
 175 
 176   void call_VM_helper(Register oop_result, address entry_point, int number_of_arguments, bool check_exceptions);
 177 
 178   virtual void check_and_handle_earlyret(Register java_thread);
 179   virtual void check_and_handle_popframe(Register java_thread);
 180 
 181   void resolve_weak_handle(Register result, Register tmp1, Register tmp2);
 182   void resolve_oop_handle(Register result, Register tmp1, Register tmp2);
 183   void resolve_jobject(Register value, Register tmp1, Register tmp2);
 184   void resolve_global_jobject(Register value, Register tmp1, Register tmp2);
 185 
 186   void movoop(Register dst, jobject obj);
 187   void mov_metadata(Register dst, Metadata* obj);
 188   void bang_stack_size(Register size, Register tmp);
 189   void set_narrow_oop(Register dst, jobject obj);
 190   void set_narrow_klass(Register dst, Klass* k);
 191 
 192   void load_mirror(Register dst, Register method, Register tmp1, Register tmp2);
 193   void access_load_at(BasicType type, DecoratorSet decorators, Register dst,
 194                       Address src, Register tmp1, Register tmp2);
 195   void access_store_at(BasicType type, DecoratorSet decorators, Address dst,
 196                        Register val, Register tmp1, Register tmp2, Register tmp3);
 197   void load_klass(Register dst, Register src, Register tmp = t0);
 198   void store_klass(Register dst, Register src, Register tmp = t0);
 199   void cmp_klass(Register oop, Register trial_klass, Register tmp1, Register tmp2, Label &L);
 200 
 201   void encode_klass_not_null(Register r, Register tmp = t0);
 202   void decode_klass_not_null(Register r, Register tmp = t0);
 203   void encode_klass_not_null(Register dst, Register src, Register tmp);
 204   void decode_klass_not_null(Register dst, Register src, Register tmp);
 205   void decode_heap_oop_not_null(Register r);
 206   void decode_heap_oop_not_null(Register dst, Register src);
 207   void decode_heap_oop(Register d, Register s);
 208   void decode_heap_oop(Register r) { decode_heap_oop(r, r); }
 209   void encode_heap_oop_not_null(Register r);
 210   void encode_heap_oop_not_null(Register dst, Register src);
 211   void encode_heap_oop(Register d, Register s);
 212   void encode_heap_oop(Register r) { encode_heap_oop(r, r); };
 213   void load_heap_oop(Register dst, Address src, Register tmp1,
 214                      Register tmp2, DecoratorSet decorators = 0);
 215   void load_heap_oop_not_null(Register dst, Address src, Register tmp1,
 216                               Register tmp2, DecoratorSet decorators = 0);
 217   void store_heap_oop(Address dst, Register val, Register tmp1,
 218                       Register tmp2, Register tmp3, DecoratorSet decorators = 0);
 219 
 220   void store_klass_gap(Register dst, Register src);
 221 
 222   // currently unimplemented
 223   // Used for storing null. All other oop constants should be
 224   // stored using routines that take a jobject.
 225   void store_heap_oop_null(Address dst);
 226 
 227   // This dummy is to prevent a call to store_heap_oop from
 228   // converting a zero (linked null) into a Register by giving
 229   // the compiler two choices it can't resolve
 230 
 231   void store_heap_oop(Address dst, void* dummy);
 232 
 233   // Support for null-checks
 234   //
 235   // Generates code that causes a null OS exception if the content of reg is null.
 236   // If the accessed location is M[reg + offset] and the offset is known, provide the
 237   // offset. No explicit code generateion is needed if the offset is within a certain
 238   // range (0 <= offset <= page_size).
 239 
 240   virtual void null_check(Register reg, int offset = -1);
 241   static bool needs_explicit_null_check(intptr_t offset);
 242   static bool uses_implicit_null_check(void* address);
 243 
 244   // idiv variant which deals with MINLONG as dividend and -1 as divisor
 245   int corrected_idivl(Register result, Register rs1, Register rs2,
 246                       bool want_remainder, bool is_signed);
 247   int corrected_idivq(Register result, Register rs1, Register rs2,
 248                       bool want_remainder, bool is_signed);
 249 
 250   // interface method calling
 251   void lookup_interface_method(Register recv_klass,
 252                                Register intf_klass,
 253                                RegisterOrConstant itable_index,
 254                                Register method_result,
 255                                Register scan_tmp,
 256                                Label& no_such_interface,
 257                                bool return_method = true);
 258 
 259   void lookup_interface_method_stub(Register recv_klass,
 260                                     Register holder_klass,
 261                                     Register resolved_klass,
 262                                     Register method_result,
 263                                     Register temp_reg,
 264                                     Register temp_reg2,
 265                                     int itable_index,
 266                                     Label& L_no_such_interface);
 267 
 268   // virtual method calling
 269   // n.n. x86 allows RegisterOrConstant for vtable_index
 270   void lookup_virtual_method(Register recv_klass,
 271                              RegisterOrConstant vtable_index,
 272                              Register method_result);
 273 
 274   // Form an address from base + offset in Rd. Rd my or may not
 275   // actually be used: you must use the Address that is returned. It
 276   // is up to you to ensure that the shift provided matches the size
 277   // of your data.
 278   Address form_address(Register Rd, Register base, int64_t byte_offset);
 279 
 280   // Sometimes we get misaligned loads and stores, usually from Unsafe
 281   // accesses, and these can exceed the offset range.
 282   Address legitimize_address(Register Rd, const Address &adr) {
 283     if (adr.getMode() == Address::base_plus_offset) {
 284       if (!is_simm12(adr.offset())) {
 285         return form_address(Rd, adr.base(), adr.offset());
 286       }
 287     }
 288     return adr;
 289   }
 290 
 291   // allocation
 292   void tlab_allocate(
 293     Register obj,                   // result: pointer to object after successful allocation
 294     Register var_size_in_bytes,     // object size in bytes if unknown at compile time; invalid otherwise
 295     int      con_size_in_bytes,     // object size in bytes if   known at compile time
 296     Register tmp1,                  // temp register
 297     Register tmp2,                  // temp register
 298     Label&   slow_case,             // continuation point of fast allocation fails
 299     bool     is_far = false
 300   );
 301 
 302   // Test sub_klass against super_klass, with fast and slow paths.
 303 
 304   // The fast path produces a tri-state answer: yes / no / maybe-slow.
 305   // One of the three labels can be null, meaning take the fall-through.
 306   // If super_check_offset is -1, the value is loaded up from super_klass.
 307   // No registers are killed, except tmp_reg
 308   void check_klass_subtype_fast_path(Register sub_klass,
 309                                      Register super_klass,
 310                                      Register tmp_reg,
 311                                      Label* L_success,
 312                                      Label* L_failure,
 313                                      Label* L_slow_path,
 314                                      Register super_check_offset = noreg);
 315 
 316   // The reset of the type check; must be wired to a corresponding fast path.
 317   // It does not repeat the fast path logic, so don't use it standalone.
 318   // The tmp1_reg and tmp2_reg can be noreg, if no temps are available.
 319   // Updates the sub's secondary super cache as necessary.
 320   void check_klass_subtype_slow_path(Register sub_klass,
 321                                      Register super_klass,
 322                                      Register tmp1_reg,
 323                                      Register tmp2_reg,
 324                                      Label* L_success,
 325                                      Label* L_failure);
 326 
 327   void population_count(Register dst, Register src, Register tmp1, Register tmp2);
 328 
 329   // As above, but with a constant super_klass.
 330   // The result is in Register result, not the condition codes.
 331   bool lookup_secondary_supers_table(Register r_sub_klass,
 332                                      Register r_super_klass,
 333                                      Register result,
 334                                      Register tmp1,
 335                                      Register tmp2,
 336                                      Register tmp3,
 337                                      Register tmp4,
 338                                      u1 super_klass_slot,
 339                                      bool stub_is_near = false);
 340 
 341   void verify_secondary_supers_table(Register r_sub_klass,
 342                                      Register r_super_klass,
 343                                      Register result,
 344                                      Register tmp1,
 345                                      Register tmp2,
 346                                      Register tmp3);
 347 
 348   void lookup_secondary_supers_table_slow_path(Register r_super_klass,
 349                                                Register r_array_base,
 350                                                Register r_array_index,
 351                                                Register r_bitmap,
 352                                                Register result,
 353                                                Register tmp1);
 354 
 355   void check_klass_subtype(Register sub_klass,
 356                            Register super_klass,
 357                            Register tmp_reg,
 358                            Label& L_success);
 359 
 360   Address argument_address(RegisterOrConstant arg_slot, int extra_slot_offset = 0);
 361 
 362   // only if +VerifyOops
 363   void _verify_oop(Register reg, const char* s, const char* file, int line);
 364   void _verify_oop_addr(Address addr, const char* s, const char* file, int line);
 365 
 366   void _verify_oop_checked(Register reg, const char* s, const char* file, int line) {
 367     if (VerifyOops) {
 368       _verify_oop(reg, s, file, line);
 369     }
 370   }
 371   void _verify_oop_addr_checked(Address reg, const char* s, const char* file, int line) {
 372     if (VerifyOops) {
 373       _verify_oop_addr(reg, s, file, line);
 374     }
 375   }
 376 
 377   void _verify_method_ptr(Register reg, const char* msg, const char* file, int line) {}
 378   void _verify_klass_ptr(Register reg, const char* msg, const char* file, int line) {}
 379 
 380 #define verify_oop(reg) _verify_oop_checked(reg, "broken oop " #reg, __FILE__, __LINE__)
 381 #define verify_oop_msg(reg, msg) _verify_oop_checked(reg, "broken oop " #reg ", " #msg, __FILE__, __LINE__)
 382 #define verify_oop_addr(addr) _verify_oop_addr_checked(addr, "broken oop addr " #addr, __FILE__, __LINE__)
 383 #define verify_method_ptr(reg) _verify_method_ptr(reg, "broken method " #reg, __FILE__, __LINE__)
 384 #define verify_klass_ptr(reg) _verify_method_ptr(reg, "broken klass " #reg, __FILE__, __LINE__)
 385 
 386   // A more convenient access to fence for our purposes
 387   // We used four bit to indicate the read and write bits in the predecessors and successors,
 388   // and extended i for r, o for w if UseConservativeFence enabled.
 389   enum Membar_mask_bits {
 390     StoreStore = 0b0101,               // (pred = ow   + succ =   ow)
 391     LoadStore  = 0b1001,               // (pred = ir   + succ =   ow)
 392     StoreLoad  = 0b0110,               // (pred = ow   + succ =   ir)
 393     LoadLoad   = 0b1010,               // (pred = ir   + succ =   ir)
 394     AnyAny     = LoadStore | StoreLoad // (pred = iorw + succ = iorw)
 395   };
 396 
 397   void membar(uint32_t order_constraint);
 398 
 399   static void membar_mask_to_pred_succ(uint32_t order_constraint,
 400                                        uint32_t& predecessor, uint32_t& successor) {
 401     predecessor = (order_constraint >> 2) & 0x3;
 402     successor = order_constraint & 0x3;
 403 
 404     // extend rw -> iorw:
 405     // 01(w) -> 0101(ow)
 406     // 10(r) -> 1010(ir)
 407     // 11(rw)-> 1111(iorw)
 408     if (UseConservativeFence) {
 409       predecessor |= predecessor << 2;
 410       successor |= successor << 2;
 411     }
 412   }
 413 
 414   static int pred_succ_to_membar_mask(uint32_t predecessor, uint32_t successor) {
 415     return ((predecessor & 0x3) << 2) | (successor & 0x3);
 416   }
 417 
 418   void fence(uint32_t predecessor, uint32_t successor) {
 419     if (UseZtso) {
 420       if ((pred_succ_to_membar_mask(predecessor, successor) & StoreLoad) == StoreLoad) {
 421         // TSO allows for stores to be reordered after loads. When the compiler
 422         // generates a fence to disallow that, we are required to generate the
 423         // fence for correctness.
 424         Assembler::fence(predecessor, successor);
 425       } else {
 426         // TSO guarantees other fences already.
 427       }
 428     } else {
 429       // always generate fence for RVWMO
 430       Assembler::fence(predecessor, successor);
 431     }
 432   }
 433 
 434   void pause() {
 435     Assembler::fence(w, 0);
 436   }
 437 
 438   // prints msg, dumps registers and stops execution
 439   void stop(const char* msg);
 440 
 441   static void debug64(char* msg, int64_t pc, int64_t regs[]);
 442 
 443   void unimplemented(const char* what = "");
 444 
 445   void should_not_reach_here() { stop("should not reach here"); }
 446 
 447   static address target_addr_for_insn(address insn_addr);
 448 
 449   // Required platform-specific helpers for Label::patch_instructions.
 450   // They _shadow_ the declarations in AbstractAssembler, which are undefined.
 451   static int pd_patch_instruction_size(address branch, address target);
 452   static void pd_patch_instruction(address branch, address target, const char* file = nullptr, int line = 0) {
 453     pd_patch_instruction_size(branch, target);
 454   }
 455   static address pd_call_destination(address branch) {
 456     return target_addr_for_insn(branch);
 457   }
 458 
 459   static int patch_oop(address insn_addr, address o);
 460 
 461   static address get_target_of_li32(address insn_addr);
 462   static int patch_imm_in_li32(address branch, int32_t target);
 463 
 464   // Return whether code is emitted to a scratch blob.
 465   virtual bool in_scratch_emit_size() {
 466     return false;
 467   }
 468 
 469   address emit_address_stub(int insts_call_instruction_offset, address target);
 470   address emit_trampoline_stub(int insts_call_instruction_offset, address target);
 471   static int max_reloc_call_stub_size();
 472 
 473   void emit_static_call_stub();
 474   static int static_call_stub_size();
 475 
 476   // The following 4 methods return the offset of the appropriate move instruction
 477 
 478   // Support for fast byte/short loading with zero extension (depending on particular CPU)
 479   int load_unsigned_byte(Register dst, Address src);
 480   int load_unsigned_short(Register dst, Address src);
 481 
 482   // Support for fast byte/short loading with sign extension (depending on particular CPU)
 483   int load_signed_byte(Register dst, Address src);
 484   int load_signed_short(Register dst, Address src);
 485 
 486   // Load and store values by size and signed-ness
 487   void load_sized_value(Register dst, Address src, size_t size_in_bytes, bool is_signed);
 488   void store_sized_value(Address dst, Register src, size_t size_in_bytes);
 489 
 490   // Misaligned loads, will use the best way, according to the AvoidUnalignedAccess flag
 491   void load_short_misaligned(Register dst, Address src, Register tmp, bool is_signed, int granularity = 1);
 492   void load_int_misaligned(Register dst, Address src, Register tmp, bool is_signed, int granularity = 1);
 493   void load_long_misaligned(Register dst, Address src, Register tmp, int granularity = 1);
 494 
 495  public:
 496   // Standard pseudo instructions
 497   inline void nop() {
 498     addi(x0, x0, 0);
 499   }
 500 
 501   inline void mv(Register Rd, Register Rs) {
 502     if (Rd != Rs) {
 503       addi(Rd, Rs, 0);
 504     }
 505   }
 506 
 507   inline void notr(Register Rd, Register Rs) {
 508     if (do_compress_zcb(Rd, Rs) && (Rd == Rs)) {
 509       c_not(Rd);
 510     } else {
 511       xori(Rd, Rs, -1);
 512     }
 513   }
 514 
 515   inline void neg(Register Rd, Register Rs) {
 516     sub(Rd, x0, Rs);
 517   }
 518 
 519   inline void negw(Register Rd, Register Rs) {
 520     subw(Rd, x0, Rs);
 521   }
 522 
 523   inline void sext_w(Register Rd, Register Rs) {
 524     addiw(Rd, Rs, 0);
 525   }
 526 
 527   inline void zext_b(Register Rd, Register Rs) {
 528     if (do_compress_zcb(Rd, Rs) && (Rd == Rs)) {
 529       c_zext_b(Rd);
 530     } else {
 531       andi(Rd, Rs, 0xFF);
 532     }
 533   }
 534 
 535   inline void seqz(Register Rd, Register Rs) {
 536     sltiu(Rd, Rs, 1);
 537   }
 538 
 539   inline void snez(Register Rd, Register Rs) {
 540     sltu(Rd, x0, Rs);
 541   }
 542 
 543   inline void sltz(Register Rd, Register Rs) {
 544     slt(Rd, Rs, x0);
 545   }
 546 
 547   inline void sgtz(Register Rd, Register Rs) {
 548     slt(Rd, x0, Rs);
 549   }
 550 
 551   // Bit-manipulation extension pseudo instructions
 552   // zero extend word
 553   inline void zext_w(Register Rd, Register Rs) {
 554     assert(UseZba, "must be");
 555     if (do_compress_zcb(Rd, Rs) && (Rd == Rs)) {
 556       c_zext_w(Rd);
 557     } else {
 558       add_uw(Rd, Rs, zr);
 559     }
 560   }
 561 
 562   // Floating-point data-processing pseudo instructions
 563   inline void fmv_s(FloatRegister Rd, FloatRegister Rs) {
 564     if (Rd != Rs) {
 565       fsgnj_s(Rd, Rs, Rs);
 566     }
 567   }
 568 
 569   inline void fabs_s(FloatRegister Rd, FloatRegister Rs) {
 570     fsgnjx_s(Rd, Rs, Rs);
 571   }
 572 
 573   inline void fneg_s(FloatRegister Rd, FloatRegister Rs) {
 574     fsgnjn_s(Rd, Rs, Rs);
 575   }
 576 
 577   inline void fmv_d(FloatRegister Rd, FloatRegister Rs) {
 578     if (Rd != Rs) {
 579       fsgnj_d(Rd, Rs, Rs);
 580     }
 581   }
 582 
 583   inline void fabs_d(FloatRegister Rd, FloatRegister Rs) {
 584     fsgnjx_d(Rd, Rs, Rs);
 585   }
 586 
 587   inline void fneg_d(FloatRegister Rd, FloatRegister Rs) {
 588     fsgnjn_d(Rd, Rs, Rs);
 589   }
 590 
 591   // Control and status pseudo instructions
 592   void rdinstret(Register Rd);                  // read instruction-retired counter
 593   void rdcycle(Register Rd);                    // read cycle counter
 594   void rdtime(Register Rd);                     // read time
 595   void csrr(Register Rd, unsigned csr);         // read csr
 596   void csrw(unsigned csr, Register Rs);         // write csr
 597   void csrs(unsigned csr, Register Rs);         // set bits in csr
 598   void csrc(unsigned csr, Register Rs);         // clear bits in csr
 599   void csrwi(unsigned csr, unsigned imm);
 600   void csrsi(unsigned csr, unsigned imm);
 601   void csrci(unsigned csr, unsigned imm);
 602   void frcsr(Register Rd);                      // read float-point csr
 603   void fscsr(Register Rd, Register Rs);         // swap float-point csr
 604   void fscsr(Register Rs);                      // write float-point csr
 605   void frrm(Register Rd);                       // read float-point rounding mode
 606   void fsrm(Register Rd, Register Rs);          // swap float-point rounding mode
 607   void fsrm(Register Rs);                       // write float-point rounding mode
 608   void fsrmi(Register Rd, unsigned imm);
 609   void fsrmi(unsigned imm);
 610   void frflags(Register Rd);                    // read float-point exception flags
 611   void fsflags(Register Rd, Register Rs);       // swap float-point exception flags
 612   void fsflags(Register Rs);                    // write float-point exception flags
 613   void fsflagsi(Register Rd, unsigned imm);
 614   void fsflagsi(unsigned imm);
 615 
 616   // Restore cpu control state after JNI call
 617   void restore_cpu_control_state_after_jni(Register tmp);
 618 
 619   // Control transfer pseudo instructions
 620   void beqz(Register Rs, const address dest);
 621   void bnez(Register Rs, const address dest);
 622   void blez(Register Rs, const address dest);
 623   void bgez(Register Rs, const address dest);
 624   void bltz(Register Rs, const address dest);
 625   void bgtz(Register Rs, const address dest);
 626 
 627  private:
 628   void load_link_jump(const address source, Register temp = t0);
 629   void jump_link(const address dest, Register temp);
 630  public:
 631   // We try to follow risc-v asm menomics.
 632   // But as we don't layout a reachable GOT,
 633   // we often need to resort to movptr, li <48imm>.
 634   // https://github.com/riscv-non-isa/riscv-asm-manual/blob/master/riscv-asm.md
 635 
 636   // jump: jal x0, offset
 637   // For long reach uses temp register for:
 638   // la + jr
 639   void j(const address dest, Register temp = t0);
 640   void j(const Address &adr, Register temp = t0);
 641   void j(Label &l, Register temp = t0);
 642 
 643   // jump register: jalr x0, offset(rs)
 644   void jr(Register Rd, int32_t offset = 0);
 645 
 646   // call: la + jalr x1
 647   void call(const address dest, Register temp = t0);
 648 
 649   // jalr: jalr x1, offset(rs)
 650   void jalr(Register Rs, int32_t offset = 0);
 651 
 652   // Emit a runtime call. Only invalidates the tmp register which
 653   // is used to keep the entry address for jalr/movptr.
 654   // Uses call() for intra code cache, else movptr + jalr.
 655   void rt_call(address dest, Register tmp = t0);
 656 
 657   // ret: jalr x0, 0(x1)
 658   inline void ret() {
 659     Assembler::jalr(x0, x1, 0);
 660   }
 661 
 662   //label
 663   void beqz(Register Rs, Label &l, bool is_far = false);
 664   void bnez(Register Rs, Label &l, bool is_far = false);
 665   void blez(Register Rs, Label &l, bool is_far = false);
 666   void bgez(Register Rs, Label &l, bool is_far = false);
 667   void bltz(Register Rs, Label &l, bool is_far = false);
 668   void bgtz(Register Rs, Label &l, bool is_far = false);
 669 
 670   void beq (Register Rs1, Register Rs2, Label &L, bool is_far = false);
 671   void bne (Register Rs1, Register Rs2, Label &L, bool is_far = false);
 672   void blt (Register Rs1, Register Rs2, Label &L, bool is_far = false);
 673   void bge (Register Rs1, Register Rs2, Label &L, bool is_far = false);
 674   void bltu(Register Rs1, Register Rs2, Label &L, bool is_far = false);
 675   void bgeu(Register Rs1, Register Rs2, Label &L, bool is_far = false);
 676 
 677   void bgt (Register Rs, Register Rt, const address dest);
 678   void ble (Register Rs, Register Rt, const address dest);
 679   void bgtu(Register Rs, Register Rt, const address dest);
 680   void bleu(Register Rs, Register Rt, const address dest);
 681 
 682   void bgt (Register Rs, Register Rt, Label &l, bool is_far = false);
 683   void ble (Register Rs, Register Rt, Label &l, bool is_far = false);
 684   void bgtu(Register Rs, Register Rt, Label &l, bool is_far = false);
 685   void bleu(Register Rs, Register Rt, Label &l, bool is_far = false);
 686 
 687 #define INSN_ENTRY_RELOC(result_type, header)                               \
 688   result_type header {                                                      \
 689     guarantee(rtype == relocInfo::internal_word_type,                       \
 690               "only internal_word_type relocs make sense here");            \
 691     relocate(InternalAddress(dest).rspec());                                \
 692     IncompressibleRegion ir(this);  /* relocations */
 693 
 694 #define INSN(NAME)                                                                                       \
 695   void NAME(Register Rs1, Register Rs2, const address dest) {                                            \
 696     assert_cond(dest != nullptr);                                                                        \
 697     int64_t offset = dest - pc();                                                                        \
 698     guarantee(is_simm13(offset) && is_even(offset),                                                      \
 699               "offset is invalid: is_simm_13: %s offset: " INT64_FORMAT,                                 \
 700               BOOL_TO_STR(is_simm13(offset)), offset);                                                   \
 701     Assembler::NAME(Rs1, Rs2, offset);                                                                   \
 702   }                                                                                                      \
 703   INSN_ENTRY_RELOC(void, NAME(Register Rs1, Register Rs2, address dest, relocInfo::relocType rtype))     \
 704     NAME(Rs1, Rs2, dest);                                                                                \
 705   }
 706 
 707   INSN(beq);
 708   INSN(bne);
 709   INSN(bge);
 710   INSN(bgeu);
 711   INSN(blt);
 712   INSN(bltu);
 713 
 714 #undef INSN
 715 
 716 #undef INSN_ENTRY_RELOC
 717 
 718   void float_beq(FloatRegister Rs1, FloatRegister Rs2, Label &l, bool is_far = false, bool is_unordered = false);
 719   void float_bne(FloatRegister Rs1, FloatRegister Rs2, Label &l, bool is_far = false, bool is_unordered = false);
 720   void float_ble(FloatRegister Rs1, FloatRegister Rs2, Label &l, bool is_far = false, bool is_unordered = false);
 721   void float_bge(FloatRegister Rs1, FloatRegister Rs2, Label &l, bool is_far = false, bool is_unordered = false);
 722   void float_blt(FloatRegister Rs1, FloatRegister Rs2, Label &l, bool is_far = false, bool is_unordered = false);
 723   void float_bgt(FloatRegister Rs1, FloatRegister Rs2, Label &l, bool is_far = false, bool is_unordered = false);
 724 
 725   void double_beq(FloatRegister Rs1, FloatRegister Rs2, Label &l, bool is_far = false, bool is_unordered = false);
 726   void double_bne(FloatRegister Rs1, FloatRegister Rs2, Label &l, bool is_far = false, bool is_unordered = false);
 727   void double_ble(FloatRegister Rs1, FloatRegister Rs2, Label &l, bool is_far = false, bool is_unordered = false);
 728   void double_bge(FloatRegister Rs1, FloatRegister Rs2, Label &l, bool is_far = false, bool is_unordered = false);
 729   void double_blt(FloatRegister Rs1, FloatRegister Rs2, Label &l, bool is_far = false, bool is_unordered = false);
 730   void double_bgt(FloatRegister Rs1, FloatRegister Rs2, Label &l, bool is_far = false, bool is_unordered = false);
 731 
 732 private:
 733   int push_reg(unsigned int bitset, Register stack);
 734   int pop_reg(unsigned int bitset, Register stack);
 735   int push_fp(unsigned int bitset, Register stack);
 736   int pop_fp(unsigned int bitset, Register stack);
 737 #ifdef COMPILER2
 738   int push_v(unsigned int bitset, Register stack);
 739   int pop_v(unsigned int bitset, Register stack);
 740 #endif // COMPILER2
 741 
 742   // The signed 20-bit upper imm can materialize at most negative 0xF...F80000000, two G.
 743   // The following signed 12-bit imm can at max subtract 0x800, two K, from that previously loaded two G.
 744   bool is_valid_32bit_offset(int64_t x) {
 745     constexpr int64_t twoG = (2 * G);
 746     constexpr int64_t twoK = (2 * K);
 747     return x < (twoG - twoK) && x >= (-twoG - twoK);
 748   }
 749 
 750   // Ensure that the auipc can reach the destination at x from anywhere within
 751   // the code cache so that if it is relocated we know it will still reach.
 752   bool is_32bit_offset_from_codecache(int64_t x) {
 753     int64_t low  = (int64_t)CodeCache::low_bound();
 754     int64_t high = (int64_t)CodeCache::high_bound();
 755     return is_valid_32bit_offset(x - low) && is_valid_32bit_offset(x - high);
 756   }
 757 
 758 public:
 759   void push_reg(Register Rs);
 760   void pop_reg(Register Rd);
 761   void push_reg(RegSet regs, Register stack) { if (regs.bits()) push_reg(regs.bits(), stack); }
 762   void pop_reg(RegSet regs, Register stack)  { if (regs.bits()) pop_reg(regs.bits(), stack); }
 763   void push_fp(FloatRegSet regs, Register stack) { if (regs.bits()) push_fp(regs.bits(), stack); }
 764   void pop_fp(FloatRegSet regs, Register stack)  { if (regs.bits()) pop_fp(regs.bits(), stack); }
 765 #ifdef COMPILER2
 766   void push_v(VectorRegSet regs, Register stack) { if (regs.bits()) push_v(regs.bits(), stack); }
 767   void pop_v(VectorRegSet regs, Register stack)  { if (regs.bits()) pop_v(regs.bits(), stack); }
 768 #endif // COMPILER2
 769 
 770   // Push and pop everything that might be clobbered by a native
 771   // runtime call except t0 and t1. (They are always
 772   // temporary registers, so we don't have to protect them.)
 773   // Additional registers can be excluded in a passed RegSet.
 774   void push_call_clobbered_registers_except(RegSet exclude);
 775   void pop_call_clobbered_registers_except(RegSet exclude);
 776 
 777   void push_call_clobbered_registers() {
 778     push_call_clobbered_registers_except(RegSet());
 779   }
 780   void pop_call_clobbered_registers() {
 781     pop_call_clobbered_registers_except(RegSet());
 782   }
 783 
 784   void push_CPU_state(bool save_vectors = false, int vector_size_in_bytes = 0);
 785   void pop_CPU_state(bool restore_vectors = false, int vector_size_in_bytes = 0);
 786 
 787   void push_cont_fastpath(Register java_thread = xthread);
 788   void pop_cont_fastpath(Register java_thread = xthread);
 789 
 790   void inc_held_monitor_count(Register tmp = t0);
 791   void dec_held_monitor_count(Register tmp = t0);
 792 
 793   // if heap base register is used - reinit it with the correct value
 794   void reinit_heapbase();
 795 
 796   void bind(Label& L) {
 797     Assembler::bind(L);
 798     // fences across basic blocks should not be merged
 799     code()->clear_last_insn();
 800   }
 801 
 802   typedef void (MacroAssembler::* compare_and_branch_insn)(Register Rs1, Register Rs2, const address dest);
 803   typedef void (MacroAssembler::* compare_and_branch_label_insn)(Register Rs1, Register Rs2, Label &L, bool is_far);
 804   typedef void (MacroAssembler::* jal_jalr_insn)(Register Rt, address dest);
 805 
 806   void wrap_label(Register r, Label &L, jal_jalr_insn insn);
 807   void wrap_label(Register r1, Register r2, Label &L,
 808                   compare_and_branch_insn insn,
 809                   compare_and_branch_label_insn neg_insn, bool is_far = false);
 810 
 811   // la will use movptr instead of GOT when not in reach for auipc.
 812   void la(Register Rd, Label &label);
 813   void la(Register Rd, const address addr);
 814   void la(Register Rd, const address addr, int32_t &offset);
 815   void la(Register Rd, const Address &adr);
 816 
 817   void li16u(Register Rd, uint16_t imm);
 818   void li32(Register Rd, int32_t imm);
 819   void li  (Register Rd, int64_t imm);  // optimized load immediate
 820 
 821   // mv
 822   void mv(Register Rd, address addr)                  { li(Rd, (int64_t)addr); }
 823   void mv(Register Rd, address addr, int32_t &offset) {
 824     // Split address into a lower 12-bit sign-extended offset and the remainder,
 825     // so that the offset could be encoded in jalr or load/store instruction.
 826     offset = ((int32_t)(int64_t)addr << 20) >> 20;
 827     li(Rd, (int64_t)addr - offset);
 828   }
 829 
 830   template<typename T, ENABLE_IF(std::is_integral<T>::value)>
 831   inline void mv(Register Rd, T o)                    { li(Rd, (int64_t)o); }
 832 
 833   void mv(Register Rd, RegisterOrConstant src) {
 834     if (src.is_register()) {
 835       mv(Rd, src.as_register());
 836     } else {
 837       mv(Rd, src.as_constant());
 838     }
 839   }
 840 
 841   // Generates a load of a 48-bit constant which can be
 842   // patched to any 48-bit constant, i.e. address.
 843   // If common case supply additional temp register
 844   // to shorten the instruction sequence.
 845   void movptr(Register Rd, address addr, Register tmp = noreg);
 846   void movptr(Register Rd, address addr, int32_t &offset, Register tmp = noreg);
 847  private:
 848   void movptr1(Register Rd, uintptr_t addr, int32_t &offset);
 849   void movptr2(Register Rd, uintptr_t addr, int32_t &offset, Register tmp);
 850  public:
 851 
 852   // arith
 853   void add (Register Rd, Register Rn, int64_t increment, Register temp = t0);
 854   void addw(Register Rd, Register Rn, int32_t increment, Register temp = t0);
 855   void sub (Register Rd, Register Rn, int64_t decrement, Register temp = t0);
 856   void subw(Register Rd, Register Rn, int32_t decrement, Register temp = t0);
 857 
 858 #define INSN(NAME)                                               \
 859   inline void NAME(Register Rd, Register Rs1, Register Rs2) {    \
 860     Assembler::NAME(Rd, Rs1, Rs2);                               \
 861   }
 862 
 863   INSN(add);
 864   INSN(addw);
 865   INSN(sub);
 866   INSN(subw);
 867 
 868 #undef INSN
 869 
 870   // logic
 871   void andrw(Register Rd, Register Rs1, Register Rs2);
 872   void orrw(Register Rd, Register Rs1, Register Rs2);
 873   void xorrw(Register Rd, Register Rs1, Register Rs2);
 874 
 875   // logic with negate
 876   void andn(Register Rd, Register Rs1, Register Rs2);
 877   void orn(Register Rd, Register Rs1, Register Rs2);
 878 
 879   // revb
 880   void revb_h_h(Register Rd, Register Rs, Register tmp = t0);                           // reverse bytes in halfword in lower 16 bits, sign-extend
 881   void revb_w_w(Register Rd, Register Rs, Register tmp1 = t0, Register tmp2 = t1);      // reverse bytes in lower word, sign-extend
 882   void revb_h_h_u(Register Rd, Register Rs, Register tmp = t0);                         // reverse bytes in halfword in lower 16 bits, zero-extend
 883   void revb_h_w_u(Register Rd, Register Rs, Register tmp1 = t0, Register tmp2 = t1);    // reverse bytes in halfwords in lower 32 bits, zero-extend
 884   void revb_h_helper(Register Rd, Register Rs, Register tmp1 = t0, Register tmp2= t1);  // reverse bytes in upper 16 bits (48:63) and move to lower
 885   void revb_h(Register Rd, Register Rs, Register tmp1 = t0, Register tmp2= t1);         // reverse bytes in each halfword
 886   void revb_w(Register Rd, Register Rs, Register tmp1 = t0, Register tmp2= t1);         // reverse bytes in each word
 887   void revb(Register Rd, Register Rs, Register tmp1 = t0, Register tmp2 = t1);          // reverse bytes in doubleword
 888 
 889   void ror_imm(Register dst, Register src, uint32_t shift, Register tmp = t0);
 890   void rolw_imm(Register dst, Register src, uint32_t, Register tmp = t0);
 891   void andi(Register Rd, Register Rn, int64_t imm, Register tmp = t0);
 892   void orptr(Address adr, RegisterOrConstant src, Register tmp1 = t0, Register tmp2 = t1);
 893 
 894 // Load and Store Instructions
 895 #define INSN_ENTRY_RELOC(result_type, header)                               \
 896   result_type header {                                                      \
 897     guarantee(rtype == relocInfo::internal_word_type,                       \
 898               "only internal_word_type relocs make sense here");            \
 899     relocate(InternalAddress(dest).rspec());                                \
 900     IncompressibleRegion ir(this);  /* relocations */
 901 
 902 #define INSN(NAME)                                                                                 \
 903   void NAME(Register Rd, address dest) {                                                           \
 904     assert_cond(dest != nullptr);                                                                  \
 905     int64_t distance = dest - pc();                                                                \
 906     if (is_valid_32bit_offset(distance)) {                                                         \
 907       auipc(Rd, (int32_t)distance + 0x800);                                                        \
 908       Assembler::NAME(Rd, Rd, ((int32_t)distance << 20) >> 20);                                    \
 909     } else {                                                                                       \
 910       int32_t offset = 0;                                                                          \
 911       movptr(Rd, dest, offset);                                                                    \
 912       Assembler::NAME(Rd, Rd, offset);                                                             \
 913     }                                                                                              \
 914   }                                                                                                \
 915   INSN_ENTRY_RELOC(void, NAME(Register Rd, address dest, relocInfo::relocType rtype))              \
 916     NAME(Rd, dest);                                                                                \
 917   }                                                                                                \
 918   void NAME(Register Rd, const Address &adr, Register temp = t0) {                                 \
 919     switch (adr.getMode()) {                                                                       \
 920       case Address::literal: {                                                                     \
 921         relocate(adr.rspec(), [&] {                                                                \
 922           NAME(Rd, adr.target());                                                                  \
 923         });                                                                                        \
 924         break;                                                                                     \
 925       }                                                                                            \
 926       case Address::base_plus_offset: {                                                            \
 927         if (is_simm12(adr.offset())) {                                                             \
 928           Assembler::NAME(Rd, adr.base(), adr.offset());                                           \
 929         } else {                                                                                   \
 930           int32_t offset = ((int32_t)adr.offset() << 20) >> 20;                                    \
 931           if (Rd == adr.base()) {                                                                  \
 932             la(temp, Address(adr.base(), adr.offset() - offset));                                  \
 933             Assembler::NAME(Rd, temp, offset);                                                     \
 934           } else {                                                                                 \
 935             la(Rd, Address(adr.base(), adr.offset() - offset));                                    \
 936             Assembler::NAME(Rd, Rd, offset);                                                       \
 937           }                                                                                        \
 938         }                                                                                          \
 939         break;                                                                                     \
 940       }                                                                                            \
 941       default:                                                                                     \
 942         ShouldNotReachHere();                                                                      \
 943     }                                                                                              \
 944   }                                                                                                \
 945   void NAME(Register Rd, Label &L) {                                                               \
 946     wrap_label(Rd, L, &MacroAssembler::NAME);                                                      \
 947   }
 948 
 949   INSN(lb);
 950   INSN(lbu);
 951   INSN(lh);
 952   INSN(lhu);
 953   INSN(lw);
 954   INSN(lwu);
 955   INSN(ld);
 956 
 957 #undef INSN
 958 
 959 #define INSN(NAME)                                                                                 \
 960   void NAME(FloatRegister Rd, address dest, Register temp = t0) {                                  \
 961     assert_cond(dest != nullptr);                                                                  \
 962     int64_t distance = dest - pc();                                                                \
 963     if (is_valid_32bit_offset(distance)) {                                                         \
 964       auipc(temp, (int32_t)distance + 0x800);                                                      \
 965       Assembler::NAME(Rd, temp, ((int32_t)distance << 20) >> 20);                                  \
 966     } else {                                                                                       \
 967       int32_t offset = 0;                                                                          \
 968       movptr(temp, dest, offset);                                                                  \
 969       Assembler::NAME(Rd, temp, offset);                                                           \
 970     }                                                                                              \
 971   }                                                                                                \
 972   INSN_ENTRY_RELOC(void, NAME(FloatRegister Rd, address dest,                                      \
 973                               relocInfo::relocType rtype, Register temp = t0))                     \
 974     NAME(Rd, dest, temp);                                                                          \
 975   }                                                                                                \
 976   void NAME(FloatRegister Rd, const Address &adr, Register temp = t0) {                            \
 977     switch (adr.getMode()) {                                                                       \
 978       case Address::literal: {                                                                     \
 979         relocate(adr.rspec(), [&] {                                                                \
 980           NAME(Rd, adr.target(), temp);                                                            \
 981         });                                                                                        \
 982         break;                                                                                     \
 983       }                                                                                            \
 984       case Address::base_plus_offset: {                                                            \
 985         if (is_simm12(adr.offset())) {                                                             \
 986           Assembler::NAME(Rd, adr.base(), adr.offset());                                           \
 987         } else {                                                                                   \
 988           int32_t offset = ((int32_t)adr.offset() << 20) >> 20;                                    \
 989           la(temp, Address(adr.base(), adr.offset() - offset));                                    \
 990           Assembler::NAME(Rd, temp, offset);                                                       \
 991         }                                                                                          \
 992         break;                                                                                     \
 993       }                                                                                            \
 994       default:                                                                                     \
 995         ShouldNotReachHere();                                                                      \
 996     }                                                                                              \
 997   }
 998 
 999   INSN(flw);
1000   INSN(fld);
1001 
1002 #undef INSN
1003 
1004 #define INSN(NAME, REGISTER)                                                                       \
1005   INSN_ENTRY_RELOC(void, NAME(REGISTER Rs, address dest,                                           \
1006                               relocInfo::relocType rtype, Register temp = t0))                     \
1007     NAME(Rs, dest, temp);                                                                          \
1008   }
1009 
1010   INSN(sb,  Register);
1011   INSN(sh,  Register);
1012   INSN(sw,  Register);
1013   INSN(sd,  Register);
1014   INSN(fsw, FloatRegister);
1015   INSN(fsd, FloatRegister);
1016 
1017 #undef INSN
1018 
1019 #define INSN(NAME)                                                                                 \
1020   void NAME(Register Rs, address dest, Register temp = t0) {                                       \
1021     assert_cond(dest != nullptr);                                                                  \
1022     assert_different_registers(Rs, temp);                                                          \
1023     int64_t distance = dest - pc();                                                                \
1024     if (is_valid_32bit_offset(distance)) {                                                         \
1025       auipc(temp, (int32_t)distance + 0x800);                                                      \
1026       Assembler::NAME(Rs, temp, ((int32_t)distance << 20) >> 20);                                  \
1027     } else {                                                                                       \
1028       int32_t offset = 0;                                                                          \
1029       movptr(temp, dest, offset);                                                                  \
1030       Assembler::NAME(Rs, temp, offset);                                                           \
1031     }                                                                                              \
1032   }                                                                                                \
1033   void NAME(Register Rs, const Address &adr, Register temp = t0) {                                 \
1034     switch (adr.getMode()) {                                                                       \
1035       case Address::literal: {                                                                     \
1036         assert_different_registers(Rs, temp);                                                      \
1037         relocate(adr.rspec(), [&] {                                                                \
1038           NAME(Rs, adr.target(), temp);                                                            \
1039         });                                                                                        \
1040         break;                                                                                     \
1041       }                                                                                            \
1042       case Address::base_plus_offset: {                                                            \
1043         if (is_simm12(adr.offset())) {                                                             \
1044           Assembler::NAME(Rs, adr.base(), adr.offset());                                           \
1045         } else {                                                                                   \
1046           assert_different_registers(Rs, temp);                                                    \
1047           int32_t offset = ((int32_t)adr.offset() << 20) >> 20;                                    \
1048           la(temp, Address(adr.base(), adr.offset() - offset));                                    \
1049           Assembler::NAME(Rs, temp, offset);                                                       \
1050         }                                                                                          \
1051         break;                                                                                     \
1052       }                                                                                            \
1053       default:                                                                                     \
1054         ShouldNotReachHere();                                                                      \
1055     }                                                                                              \
1056   }
1057 
1058   INSN(sb);
1059   INSN(sh);
1060   INSN(sw);
1061   INSN(sd);
1062 
1063 #undef INSN
1064 
1065 #define INSN(NAME)                                                                                 \
1066   void NAME(FloatRegister Rs, address dest, Register temp = t0) {                                  \
1067     assert_cond(dest != nullptr);                                                                  \
1068     int64_t distance = dest - pc();                                                                \
1069     if (is_valid_32bit_offset(distance)) {                                                         \
1070       auipc(temp, (int32_t)distance + 0x800);                                                      \
1071       Assembler::NAME(Rs, temp, ((int32_t)distance << 20) >> 20);                                  \
1072     } else {                                                                                       \
1073       int32_t offset = 0;                                                                          \
1074       movptr(temp, dest, offset);                                                                  \
1075       Assembler::NAME(Rs, temp, offset);                                                           \
1076     }                                                                                              \
1077   }                                                                                                \
1078   void NAME(FloatRegister Rs, const Address &adr, Register temp = t0) {                            \
1079     switch (adr.getMode()) {                                                                       \
1080       case Address::literal: {                                                                     \
1081         relocate(adr.rspec(), [&] {                                                                \
1082           NAME(Rs, adr.target(), temp);                                                            \
1083         });                                                                                        \
1084         break;                                                                                     \
1085       }                                                                                            \
1086       case Address::base_plus_offset: {                                                            \
1087         if (is_simm12(adr.offset())) {                                                             \
1088           Assembler::NAME(Rs, adr.base(), adr.offset());                                           \
1089         } else {                                                                                   \
1090           int32_t offset = ((int32_t)adr.offset() << 20) >> 20;                                    \
1091           la(temp, Address(adr.base(), adr.offset() - offset));                                    \
1092           Assembler::NAME(Rs, temp, offset);                                                       \
1093         }                                                                                          \
1094         break;                                                                                     \
1095       }                                                                                            \
1096       default:                                                                                     \
1097         ShouldNotReachHere();                                                                      \
1098     }                                                                                              \
1099   }
1100 
1101   INSN(fsw);
1102   INSN(fsd);
1103 
1104 #undef INSN
1105 
1106 #undef INSN_ENTRY_RELOC
1107 
1108   void cmpxchg_obj_header(Register oldv, Register newv, Register obj, Register tmp, Label &succeed, Label *fail);
1109   void cmpxchgptr(Register oldv, Register newv, Register addr, Register tmp, Label &succeed, Label *fail);
1110   void cmpxchg(Register addr, Register expected,
1111                Register new_val,
1112                enum operand_size size,
1113                Assembler::Aqrl acquire, Assembler::Aqrl release,
1114                Register result, bool result_as_bool = false);
1115   void cmpxchg_weak(Register addr, Register expected,
1116                     Register new_val,
1117                     enum operand_size size,
1118                     Assembler::Aqrl acquire, Assembler::Aqrl release,
1119                     Register result);
1120   void cmpxchg_narrow_value_helper(Register addr, Register expected,
1121                                    Register new_val,
1122                                    enum operand_size size,
1123                                    Register tmp1, Register tmp2, Register tmp3);
1124   void cmpxchg_narrow_value(Register addr, Register expected,
1125                             Register new_val,
1126                             enum operand_size size,
1127                             Assembler::Aqrl acquire, Assembler::Aqrl release,
1128                             Register result, bool result_as_bool,
1129                             Register tmp1, Register tmp2, Register tmp3);
1130   void weak_cmpxchg_narrow_value(Register addr, Register expected,
1131                                  Register new_val,
1132                                  enum operand_size size,
1133                                  Assembler::Aqrl acquire, Assembler::Aqrl release,
1134                                  Register result,
1135                                  Register tmp1, Register tmp2, Register tmp3);
1136 
1137   void atomic_add(Register prev, RegisterOrConstant incr, Register addr);
1138   void atomic_addw(Register prev, RegisterOrConstant incr, Register addr);
1139   void atomic_addal(Register prev, RegisterOrConstant incr, Register addr);
1140   void atomic_addalw(Register prev, RegisterOrConstant incr, Register addr);
1141 
1142   void atomic_xchg(Register prev, Register newv, Register addr);
1143   void atomic_xchgw(Register prev, Register newv, Register addr);
1144   void atomic_xchgal(Register prev, Register newv, Register addr);
1145   void atomic_xchgalw(Register prev, Register newv, Register addr);
1146   void atomic_xchgwu(Register prev, Register newv, Register addr);
1147   void atomic_xchgalwu(Register prev, Register newv, Register addr);
1148 
1149   void atomic_cas(Register prev, Register newv, Register addr);
1150   void atomic_casw(Register prev, Register newv, Register addr);
1151   void atomic_casl(Register prev, Register newv, Register addr);
1152   void atomic_caslw(Register prev, Register newv, Register addr);
1153   void atomic_casal(Register prev, Register newv, Register addr);
1154   void atomic_casalw(Register prev, Register newv, Register addr);
1155   void atomic_caswu(Register prev, Register newv, Register addr);
1156   void atomic_caslwu(Register prev, Register newv, Register addr);
1157   void atomic_casalwu(Register prev, Register newv, Register addr);
1158 
1159   void atomic_cas(Register prev, Register newv, Register addr, enum operand_size size,
1160               Assembler::Aqrl acquire = Assembler::relaxed, Assembler::Aqrl release = Assembler::relaxed);
1161 
1162   // Emit a far call/jump. Only invalidates the tmp register which
1163   // is used to keep the entry address for jalr.
1164   // The address must be inside the code cache.
1165   // Supported entry.rspec():
1166   // - relocInfo::external_word_type
1167   // - relocInfo::runtime_call_type
1168   // - relocInfo::none
1169   void far_call(const Address &entry, Register tmp = t0);
1170   void far_jump(const Address &entry, Register tmp = t0);
1171 
1172   static int far_branch_size() {
1173       return 2 * 4;  // auipc + jalr, see far_call() & far_jump()
1174   }
1175 
1176   void load_byte_map_base(Register reg);
1177 
1178   void bang_stack_with_offset(int offset) {
1179     // stack grows down, caller passes positive offset
1180     assert(offset > 0, "must bang with negative offset");
1181     sub(t0, sp, offset);
1182     sd(zr, Address(t0));
1183   }
1184 
1185   virtual void _call_Unimplemented(address call_site) {
1186     mv(t1, call_site);
1187   }
1188 
1189   #define call_Unimplemented() _call_Unimplemented((address)__PRETTY_FUNCTION__)
1190 
1191   // Frame creation and destruction shared between JITs.
1192   void build_frame(int framesize);
1193   void remove_frame(int framesize);
1194 
1195   void reserved_stack_check();
1196 
1197   void get_polling_page(Register dest, relocInfo::relocType rtype);
1198   void read_polling_page(Register r, int32_t offset, relocInfo::relocType rtype);
1199 
1200   // RISCV64 OpenJDK uses four different types of calls:
1201   //   - direct call: jal pc_relative_offset
1202   //     This is the shortest and the fastest, but the offset has the range: +/-1MB.
1203   //
1204   //   - far call: auipc reg, pc_relative_offset; jalr ra, reg, offset
1205   //     This is longer than a direct call. The offset has
1206   //     the range [-(2G + 2K), 2G - 2K). Addresses out of the range in the code cache
1207   //     requires indirect call.
1208   //     If a jump is needed rather than a call, a far jump 'jalr x0, reg, offset' can
1209   //     be used instead.
1210   //     All instructions are embedded at a call site.
1211   //
1212   //   - indirect call: movptr + jalr
1213   //     This too can reach anywhere in the address space, but it cannot be
1214   //     patched while code is running, so it must only be modified at a safepoint.
1215   //     This form of call is most suitable for targets at fixed addresses, which
1216   //     will never be patched.
1217   //
1218   //   - reloc call:
1219   //     This is only available in C1/C2-generated code (nmethod).
1220   //
1221   //     [Main code section]
1222   //       auipc
1223   //       ld <address_from_stub_section>
1224   //       jalr
1225   //     [Stub section]
1226   //     trampoline:
1227   //       <64-bit destination address>
1228   //
1229   //    To change the destination we simply atomically store the new
1230   //    address in the stub section.
1231   //
1232   // - trampoline call (old reloc call / -XX:+UseTrampolines):
1233   //     This is only available in C1/C2-generated code (nmethod). It is a combination
1234   //     of a direct call, which is used if the destination of a call is in range,
1235   //     and a register-indirect call. It has the advantages of reaching anywhere in
1236   //     the RISCV address space and being patchable at runtime when the generated
1237   //     code is being executed by other threads.
1238   //
1239   //     [Main code section]
1240   //       jal trampoline
1241   //     [Stub code section]
1242   //     trampoline:
1243   //       ld    reg, pc + 8 (auipc + ld)
1244   //       jr    reg
1245   //       <64-bit destination address>
1246   //
1247   //     If the destination is in range when the generated code is moved to the code
1248   //     cache, 'jal trampoline' is replaced with 'jal destination' and the trampoline
1249   //     is not used.
1250   //     The optimization does not remove the trampoline from the stub section.
1251   //
1252   //     This is necessary because the trampoline may well be redirected later when
1253   //     code is patched, and the new destination may not be reachable by a simple JAL
1254   //     instruction.
1255   //
1256   // To patch a trampoline call when the JAL can't reach, we first modify
1257   // the 64-bit destination address in the trampoline, then modify the
1258   // JAL to point to the trampoline, then flush the instruction cache to
1259   // broadcast the change to all executing threads. See
1260   // NativeCall::set_destination_mt_safe for the details.
1261   //
1262   // There is a benign race in that the other thread might observe the
1263   // modified JAL before it observes the modified 64-bit destination
1264   // address. That does not matter because the destination method has been
1265   // invalidated, so there will be a trap at its start.
1266   // For this to work, the destination address in the trampoline is
1267   // always updated, even if we're not using the trampoline.
1268   // --
1269 
1270   // Emit a direct call if the entry address will always be in range,
1271   // otherwise a reloc call.
1272   // Supported entry.rspec():
1273   // - relocInfo::runtime_call_type
1274   // - relocInfo::opt_virtual_call_type
1275   // - relocInfo::static_call_type
1276   // - relocInfo::virtual_call_type
1277   //
1278   // Return: the call PC or null if CodeCache is full.
1279   address reloc_call(Address entry) {
1280     return UseTrampolines ? trampoline_call(entry) : load_and_call(entry);
1281   }
1282  private:
1283   address trampoline_call(Address entry);
1284   address load_and_call(Address entry);
1285  public:
1286 
1287   address ic_call(address entry, jint method_index = 0);
1288   static int ic_check_size();
1289   int ic_check(int end_alignment = MacroAssembler::instruction_size);
1290 
1291   // Support for memory inc/dec
1292   // n.b. increment/decrement calls with an Address destination will
1293   // need to use a scratch register to load the value to be
1294   // incremented. increment/decrement calls which add or subtract a
1295   // constant value other than sign-extended 12-bit immediate will need
1296   // to use a 2nd scratch register to hold the constant. so, an address
1297   // increment/decrement may trash both t0 and t1.
1298 
1299   void increment(const Address dst, int64_t value = 1, Register tmp1 = t0, Register tmp2 = t1);
1300   void incrementw(const Address dst, int32_t value = 1, Register tmp1 = t0, Register tmp2 = t1);
1301 
1302   void decrement(const Address dst, int64_t value = 1, Register tmp1 = t0, Register tmp2 = t1);
1303   void decrementw(const Address dst, int32_t value = 1, Register tmp1 = t0, Register tmp2 = t1);
1304 
1305   void cmpptr(Register src1, Address src2, Label& equal);
1306 
1307   void clinit_barrier(Register klass, Register tmp, Label* L_fast_path = nullptr, Label* L_slow_path = nullptr);
1308   void load_method_holder_cld(Register result, Register method);
1309   void load_method_holder(Register holder, Register method);
1310 
1311   void compute_index(Register str1, Register trailing_zeros, Register match_mask,
1312                      Register result, Register char_tmp, Register tmp,
1313                      bool haystack_isL);
1314   void compute_match_mask(Register src, Register pattern, Register match_mask,
1315                           Register mask1, Register mask2);
1316 
1317   // CRC32 code for java.util.zip.CRC32::updateBytes() intrinsic.
1318   void kernel_crc32(Register crc, Register buf, Register len,
1319         Register table0, Register table1, Register table2, Register table3,
1320         Register tmp1, Register tmp2, Register tmp3, Register tmp4, Register tmp5, Register tmp6);
1321   void update_word_crc32(Register crc, Register v, Register tmp1, Register tmp2, Register tmp3,
1322         Register table0, Register table1, Register table2, Register table3,
1323         bool upper);
1324   void update_byte_crc32(Register crc, Register val, Register table);
1325 
1326 #ifdef COMPILER2
1327   void mul_add(Register out, Register in, Register offset,
1328                Register len, Register k, Register tmp);
1329   void wide_mul(Register prod_lo, Register prod_hi, Register n, Register m);
1330   void wide_madd(Register sum_lo, Register sum_hi, Register n,
1331                  Register m, Register tmp1, Register tmp2);
1332   void cad(Register dst, Register src1, Register src2, Register carry);
1333   void cadc(Register dst, Register src1, Register src2, Register carry);
1334   void adc(Register dst, Register src1, Register src2, Register carry);
1335   void add2_with_carry(Register final_dest_hi, Register dest_hi, Register dest_lo,
1336                        Register src1, Register src2, Register carry);
1337   void multiply_32_x_32_loop(Register x, Register xstart, Register x_xstart,
1338                              Register y, Register y_idx, Register z,
1339                              Register carry, Register product,
1340                              Register idx, Register kdx);
1341   void multiply_64_x_64_loop(Register x, Register xstart, Register x_xstart,
1342                              Register y, Register y_idx, Register z,
1343                              Register carry, Register product,
1344                              Register idx, Register kdx);
1345   void multiply_128_x_128_loop(Register y, Register z,
1346                                Register carry, Register carry2,
1347                                Register idx, Register jdx,
1348                                Register yz_idx1, Register yz_idx2,
1349                                Register tmp, Register tmp3, Register tmp4,
1350                                Register tmp6, Register product_hi);
1351   void multiply_to_len(Register x, Register xlen, Register y, Register ylen,
1352                        Register z, Register tmp0,
1353                        Register tmp1, Register tmp2, Register tmp3, Register tmp4,
1354                        Register tmp5, Register tmp6, Register product_hi);
1355 
1356 #endif
1357 
1358   void inflate_lo32(Register Rd, Register Rs, Register tmp1 = t0, Register tmp2 = t1);
1359   void inflate_hi32(Register Rd, Register Rs, Register tmp1 = t0, Register tmp2 = t1);
1360 
1361   void ctzc_bit(Register Rd, Register Rs, bool isLL = false, Register tmp1 = t0, Register tmp2 = t1);
1362 
1363   void zero_words(Register base, uint64_t cnt);
1364   address zero_words(Register ptr, Register cnt);
1365   void fill_words(Register base, Register cnt, Register value);
1366   void zero_memory(Register addr, Register len, Register tmp);
1367   void zero_dcache_blocks(Register base, Register cnt, Register tmp1, Register tmp2);
1368 
1369   // shift left by shamt and add
1370   void shadd(Register Rd, Register Rs1, Register Rs2, Register tmp, int shamt);
1371 
1372   // test single bit in Rs, result is set to Rd
1373   void test_bit(Register Rd, Register Rs, uint32_t bit_pos);
1374 
1375   // Here the float instructions with safe deal with some exceptions.
1376   // e.g. convert from NaN, +Inf, -Inf to int, float, double
1377   // will trigger exception, we need to deal with these situations
1378   // to get correct results.
1379   void fcvt_w_s_safe(Register dst, FloatRegister src, Register tmp = t0);
1380   void fcvt_l_s_safe(Register dst, FloatRegister src, Register tmp = t0);
1381   void fcvt_w_d_safe(Register dst, FloatRegister src, Register tmp = t0);
1382   void fcvt_l_d_safe(Register dst, FloatRegister src, Register tmp = t0);
1383 
1384   void java_round_float(Register dst, FloatRegister src, FloatRegister ftmp);
1385   void java_round_double(Register dst, FloatRegister src, FloatRegister ftmp);
1386 
1387   // vector load/store unit-stride instructions
1388   void vlex_v(VectorRegister vd, Register base, Assembler::SEW sew, VectorMask vm = unmasked) {
1389     switch (sew) {
1390       case Assembler::e64:
1391         vle64_v(vd, base, vm);
1392         break;
1393       case Assembler::e32:
1394         vle32_v(vd, base, vm);
1395         break;
1396       case Assembler::e16:
1397         vle16_v(vd, base, vm);
1398         break;
1399       case Assembler::e8: // fall through
1400       default:
1401         vle8_v(vd, base, vm);
1402         break;
1403     }
1404   }
1405 
1406   void vsex_v(VectorRegister store_data, Register base, Assembler::SEW sew, VectorMask vm = unmasked) {
1407     switch (sew) {
1408       case Assembler::e64:
1409         vse64_v(store_data, base, vm);
1410         break;
1411       case Assembler::e32:
1412         vse32_v(store_data, base, vm);
1413         break;
1414       case Assembler::e16:
1415         vse16_v(store_data, base, vm);
1416         break;
1417       case Assembler::e8: // fall through
1418       default:
1419         vse8_v(store_data, base, vm);
1420         break;
1421     }
1422   }
1423 
1424   // vector pseudo instructions
1425   // rotate vector register left with shift bits, 32-bit version
1426   inline void vrole32_vi(VectorRegister vd, uint32_t shift, VectorRegister tmp_vr) {
1427     vsrl_vi(tmp_vr, vd, 32 - shift);
1428     vsll_vi(vd, vd, shift);
1429     vor_vv(vd, vd, tmp_vr);
1430   }
1431 
1432   inline void vl1r_v(VectorRegister vd, Register rs) {
1433     vl1re8_v(vd, rs);
1434   }
1435 
1436   inline void vmnot_m(VectorRegister vd, VectorRegister vs) {
1437     vmnand_mm(vd, vs, vs);
1438   }
1439 
1440   inline void vncvt_x_x_w(VectorRegister vd, VectorRegister vs, VectorMask vm = unmasked) {
1441     vnsrl_wx(vd, vs, x0, vm);
1442   }
1443 
1444   inline void vneg_v(VectorRegister vd, VectorRegister vs, VectorMask vm = unmasked) {
1445     vrsub_vx(vd, vs, x0, vm);
1446   }
1447 
1448   inline void vfneg_v(VectorRegister vd, VectorRegister vs, VectorMask vm = unmasked) {
1449     vfsgnjn_vv(vd, vs, vs, vm);
1450   }
1451 
1452   inline void vfabs_v(VectorRegister vd, VectorRegister vs, VectorMask vm = unmasked) {
1453     vfsgnjx_vv(vd, vs, vs, vm);
1454   }
1455 
1456   inline void vmsgt_vv(VectorRegister vd, VectorRegister vs2, VectorRegister vs1, VectorMask vm = unmasked) {
1457     vmslt_vv(vd, vs1, vs2, vm);
1458   }
1459 
1460   inline void vmsgtu_vv(VectorRegister vd, VectorRegister vs2, VectorRegister vs1, VectorMask vm = unmasked) {
1461     vmsltu_vv(vd, vs1, vs2, vm);
1462   }
1463 
1464   inline void vmsge_vv(VectorRegister vd, VectorRegister vs2, VectorRegister vs1, VectorMask vm = unmasked) {
1465     vmsle_vv(vd, vs1, vs2, vm);
1466   }
1467 
1468   inline void vmsgeu_vv(VectorRegister vd, VectorRegister vs2, VectorRegister vs1, VectorMask vm = unmasked) {
1469     vmsleu_vv(vd, vs1, vs2, vm);
1470   }
1471 
1472   inline void vmfgt_vv(VectorRegister vd, VectorRegister vs2, VectorRegister vs1, VectorMask vm = unmasked) {
1473     vmflt_vv(vd, vs1, vs2, vm);
1474   }
1475 
1476   inline void vmfge_vv(VectorRegister vd, VectorRegister vs2, VectorRegister vs1, VectorMask vm = unmasked) {
1477     vmfle_vv(vd, vs1, vs2, vm);
1478   }
1479 
1480   inline void vmsltu_vi(VectorRegister Vd, VectorRegister Vs2, uint32_t imm, VectorMask vm = unmasked) {
1481     guarantee(imm >= 1 && imm <= 16, "imm is invalid");
1482     vmsleu_vi(Vd, Vs2, imm-1, vm);
1483   }
1484 
1485   inline void vmsgeu_vi(VectorRegister Vd, VectorRegister Vs2, uint32_t imm, VectorMask vm = unmasked) {
1486     guarantee(imm >= 1 && imm <= 16, "imm is invalid");
1487     vmsgtu_vi(Vd, Vs2, imm-1, vm);
1488   }
1489 
1490   // Copy mask register
1491   inline void vmmv_m(VectorRegister vd, VectorRegister vs) {
1492     vmand_mm(vd, vs, vs);
1493   }
1494 
1495   // Clear mask register
1496   inline void vmclr_m(VectorRegister vd) {
1497     vmxor_mm(vd, vd, vd);
1498   }
1499 
1500   // Set mask register
1501   inline void vmset_m(VectorRegister vd) {
1502     vmxnor_mm(vd, vd, vd);
1503   }
1504 
1505   inline void vnot_v(VectorRegister Vd, VectorRegister Vs, VectorMask vm = unmasked) {
1506     vxor_vi(Vd, Vs, -1, vm);
1507   }
1508 
1509   static const int zero_words_block_size;
1510 
1511   void cast_primitive_type(BasicType type, Register Rt) {
1512     switch (type) {
1513       case T_BOOLEAN:
1514         sltu(Rt, zr, Rt);
1515         break;
1516       case T_CHAR   :
1517         zero_extend(Rt, Rt, 16);
1518         break;
1519       case T_BYTE   :
1520         sign_extend(Rt, Rt, 8);
1521         break;
1522       case T_SHORT  :
1523         sign_extend(Rt, Rt, 16);
1524         break;
1525       case T_INT    :
1526         sign_extend(Rt, Rt, 32);
1527         break;
1528       case T_LONG   : /* nothing to do */        break;
1529       case T_VOID   : /* nothing to do */        break;
1530       case T_FLOAT  : /* nothing to do */        break;
1531       case T_DOUBLE : /* nothing to do */        break;
1532       default: ShouldNotReachHere();
1533     }
1534   }
1535 
1536   // float cmp with unordered_result
1537   void float_compare(Register result, FloatRegister Rs1, FloatRegister Rs2, int unordered_result);
1538   void double_compare(Register result, FloatRegister Rs1, FloatRegister Rs2, int unordered_result);
1539 
1540   // Zero/Sign-extend
1541   void zero_extend(Register dst, Register src, int bits);
1542   void sign_extend(Register dst, Register src, int bits);
1543 
1544 private:
1545   void cmp_x2i(Register dst, Register src1, Register src2, Register tmp, bool is_signed = true);
1546 
1547 public:
1548   // compare src1 and src2 and get -1/0/1 in dst.
1549   // if [src1 > src2], dst = 1;
1550   // if [src1 == src2], dst = 0;
1551   // if [src1 < src2], dst = -1;
1552   void cmp_l2i(Register dst, Register src1, Register src2, Register tmp = t0);
1553   void cmp_ul2i(Register dst, Register src1, Register src2, Register tmp = t0);
1554   void cmp_uw2i(Register dst, Register src1, Register src2, Register tmp = t0);
1555 
1556   // support for argument shuffling
1557   void move32_64(VMRegPair src, VMRegPair dst, Register tmp = t0);
1558   void float_move(VMRegPair src, VMRegPair dst, Register tmp = t0);
1559   void long_move(VMRegPair src, VMRegPair dst, Register tmp = t0);
1560   void double_move(VMRegPair src, VMRegPair dst, Register tmp = t0);
1561   void object_move(OopMap* map,
1562                    int oop_handle_offset,
1563                    int framesize_in_slots,
1564                    VMRegPair src,
1565                    VMRegPair dst,
1566                    bool is_receiver,
1567                    int* receiver_offset);
1568 
1569 #ifdef ASSERT
1570   // Template short-hand support to clean-up after a failed call to trampoline
1571   // call generation (see trampoline_call() below), when a set of Labels must
1572   // be reset (before returning).
1573   template<typename Label, typename... More>
1574   void reset_labels(Label& lbl, More&... more) {
1575     lbl.reset(); reset_labels(more...);
1576   }
1577   template<typename Label>
1578   void reset_labels(Label& lbl) {
1579     lbl.reset();
1580   }
1581 #endif
1582 
1583 private:
1584 
1585   void repne_scan(Register addr, Register value, Register count, Register tmp);
1586 
1587   void ld_constant(Register dest, const Address &const_addr) {
1588     if (NearCpool) {
1589       ld(dest, const_addr);
1590     } else {
1591       InternalAddress target(const_addr.target());
1592       relocate(target.rspec(), [&] {
1593         int32_t offset;
1594         la(dest, target.target(), offset);
1595         ld(dest, Address(dest, offset));
1596       });
1597     }
1598   }
1599 
1600   int bitset_to_regs(unsigned int bitset, unsigned char* regs);
1601   Address add_memory_helper(const Address dst, Register tmp);
1602 
1603   void load_reserved(Register dst, Register addr, enum operand_size size, Assembler::Aqrl acquire);
1604   void store_conditional(Register dst, Register new_val, Register addr, enum operand_size size, Assembler::Aqrl release);
1605 
1606 public:
1607   void lightweight_lock(Register basic_lock, Register obj, Register tmp1, Register tmp2, Register tmp3, Label& slow);
1608   void lightweight_unlock(Register obj, Register tmp1, Register tmp2, Register tmp3, Label& slow);
1609 
1610 public:
1611   enum {
1612     // movptr
1613     movptr1_instruction_size = 6 * instruction_size, // lui, addi, slli, addi, slli, addi.  See movptr1().
1614     movptr2_instruction_size = 5 * instruction_size, // lui, lui, slli, add, addi.  See movptr2().
1615     load_pc_relative_instruction_size = 2 * instruction_size // auipc, ld
1616   };
1617 
1618   enum NativeShortCall {
1619     trampoline_size        = 3 * instruction_size + wordSize,
1620     trampoline_data_offset = 3 * instruction_size
1621   };
1622 
1623   static bool is_load_pc_relative_at(address branch);
1624   static bool is_li16u_at(address instr);
1625 
1626   static bool is_jal_at(address instr)        { assert_cond(instr != nullptr); return extract_opcode(instr) == 0b1101111; }
1627   static bool is_jalr_at(address instr)       { assert_cond(instr != nullptr); return extract_opcode(instr) == 0b1100111 && extract_funct3(instr) == 0b000; }
1628   static bool is_branch_at(address instr)     { assert_cond(instr != nullptr); return extract_opcode(instr) == 0b1100011; }
1629   static bool is_ld_at(address instr)         { assert_cond(instr != nullptr); return is_load_at(instr) && extract_funct3(instr) == 0b011; }
1630   static bool is_load_at(address instr)       { assert_cond(instr != nullptr); return extract_opcode(instr) == 0b0000011; }
1631   static bool is_float_load_at(address instr) { assert_cond(instr != nullptr); return extract_opcode(instr) == 0b0000111; }
1632   static bool is_auipc_at(address instr)      { assert_cond(instr != nullptr); return extract_opcode(instr) == 0b0010111; }
1633   static bool is_jump_at(address instr)       { assert_cond(instr != nullptr); return is_branch_at(instr) || is_jal_at(instr) || is_jalr_at(instr); }
1634   static bool is_add_at(address instr)        { assert_cond(instr != nullptr); return extract_opcode(instr) == 0b0110011 && extract_funct3(instr) == 0b000; }
1635   static bool is_addi_at(address instr)       { assert_cond(instr != nullptr); return extract_opcode(instr) == 0b0010011 && extract_funct3(instr) == 0b000; }
1636   static bool is_addiw_at(address instr)      { assert_cond(instr != nullptr); return extract_opcode(instr) == 0b0011011 && extract_funct3(instr) == 0b000; }
1637   static bool is_addiw_to_zr_at(address instr){ assert_cond(instr != nullptr); return is_addiw_at(instr) && extract_rd(instr) == zr; }
1638   static bool is_lui_at(address instr)        { assert_cond(instr != nullptr); return extract_opcode(instr) == 0b0110111; }
1639   static bool is_lui_to_zr_at(address instr)  { assert_cond(instr != nullptr); return is_lui_at(instr) && extract_rd(instr) == zr; }
1640 
1641   static bool is_srli_at(address instr) {
1642     assert_cond(instr != nullptr);
1643     return extract_opcode(instr) == 0b0010011 &&
1644            extract_funct3(instr) == 0b101 &&
1645            Assembler::extract(((unsigned*)instr)[0], 31, 26) == 0b000000;
1646   }
1647 
1648   static bool is_slli_shift_at(address instr, uint32_t shift) {
1649     assert_cond(instr != nullptr);
1650     return (extract_opcode(instr) == 0b0010011 && // opcode field
1651             extract_funct3(instr) == 0b001 &&     // funct3 field, select the type of operation
1652             Assembler::extract(Assembler::ld_instr(instr), 25, 20) == shift);    // shamt field
1653   }
1654 
1655   static bool is_movptr1_at(address instr);
1656   static bool is_movptr2_at(address instr);
1657 
1658   static bool is_lwu_to_zr(address instr);
1659 
1660   static Register extract_rs1(address instr);
1661   static Register extract_rs2(address instr);
1662   static Register extract_rd(address instr);
1663   static uint32_t extract_opcode(address instr);
1664   static uint32_t extract_funct3(address instr);
1665 
1666   // the instruction sequence of movptr is as below:
1667   //     lui
1668   //     addi
1669   //     slli
1670   //     addi
1671   //     slli
1672   //     addi/jalr/load
1673   static bool check_movptr1_data_dependency(address instr) {
1674     address lui = instr;
1675     address addi1 = lui + instruction_size;
1676     address slli1 = addi1 + instruction_size;
1677     address addi2 = slli1 + instruction_size;
1678     address slli2 = addi2 + instruction_size;
1679     address last_instr = slli2 + instruction_size;
1680     return extract_rs1(addi1) == extract_rd(lui) &&
1681            extract_rs1(addi1) == extract_rd(addi1) &&
1682            extract_rs1(slli1) == extract_rd(addi1) &&
1683            extract_rs1(slli1) == extract_rd(slli1) &&
1684            extract_rs1(addi2) == extract_rd(slli1) &&
1685            extract_rs1(addi2) == extract_rd(addi2) &&
1686            extract_rs1(slli2) == extract_rd(addi2) &&
1687            extract_rs1(slli2) == extract_rd(slli2) &&
1688            extract_rs1(last_instr) == extract_rd(slli2);
1689   }
1690 
1691   // the instruction sequence of movptr2 is as below:
1692   //     lui
1693   //     lui
1694   //     slli
1695   //     add
1696   //     addi/jalr/load
1697   static bool check_movptr2_data_dependency(address instr) {
1698     address lui1 = instr;
1699     address lui2 = lui1 + instruction_size;
1700     address slli = lui2 + instruction_size;
1701     address add  = slli + instruction_size;
1702     address last_instr = add + instruction_size;
1703     return extract_rd(add) == extract_rd(lui2) &&
1704            extract_rs1(add) == extract_rd(lui2) &&
1705            extract_rs2(add) == extract_rd(slli) &&
1706            extract_rs1(slli) == extract_rd(lui1) &&
1707            extract_rd(slli) == extract_rd(lui1) &&
1708            extract_rs1(last_instr) == extract_rd(add);
1709   }
1710 
1711   // the instruction sequence of li16u is as below:
1712   //     lui
1713   //     srli
1714   static bool check_li16u_data_dependency(address instr) {
1715     address lui = instr;
1716     address srli = lui + instruction_size;
1717 
1718     return extract_rs1(srli) == extract_rd(lui) &&
1719            extract_rs1(srli) == extract_rd(srli);
1720   }
1721 
1722   // the instruction sequence of li32 is as below:
1723   //     lui
1724   //     addiw
1725   static bool check_li32_data_dependency(address instr) {
1726     address lui = instr;
1727     address addiw = lui + instruction_size;
1728 
1729     return extract_rs1(addiw) == extract_rd(lui) &&
1730            extract_rs1(addiw) == extract_rd(addiw);
1731   }
1732 
1733   // the instruction sequence of pc-relative is as below:
1734   //     auipc
1735   //     jalr/addi/load/float_load
1736   static bool check_pc_relative_data_dependency(address instr) {
1737     address auipc = instr;
1738     address last_instr = auipc + instruction_size;
1739 
1740     return extract_rs1(last_instr) == extract_rd(auipc);
1741   }
1742 
1743   // the instruction sequence of load_label is as below:
1744   //     auipc
1745   //     load
1746   static bool check_load_pc_relative_data_dependency(address instr) {
1747     address auipc = instr;
1748     address load = auipc + instruction_size;
1749 
1750     return extract_rd(load) == extract_rd(auipc) &&
1751            extract_rs1(load) == extract_rd(load);
1752   }
1753 
1754   static bool is_li32_at(address instr);
1755   static bool is_pc_relative_at(address branch);
1756 
1757   static bool is_membar(address addr) {
1758     return (Bytes::get_native_u4(addr) & 0x7f) == 0b1111 && extract_funct3(addr) == 0;
1759   }
1760   static uint32_t get_membar_kind(address addr);
1761   static void set_membar_kind(address addr, uint32_t order_kind);
1762 };
1763 
1764 #ifdef ASSERT
1765 inline bool AbstractAssembler::pd_check_instruction_mark() { return false; }
1766 #endif
1767 
1768 /**
1769  * class SkipIfEqual:
1770  *
1771  * Instantiating this class will result in assembly code being output that will
1772  * jump around any code emitted between the creation of the instance and it's
1773  * automatic destruction at the end of a scope block, depending on the value of
1774  * the flag passed to the constructor, which will be checked at run-time.
1775  */
1776 class SkipIfEqual {
1777  private:
1778   MacroAssembler* _masm;
1779   Label _label;
1780 
1781  public:
1782    SkipIfEqual(MacroAssembler*, const bool* flag_addr, bool value);
1783    ~SkipIfEqual();
1784 };
1785 
1786 #endif // CPU_RISCV_MACROASSEMBLER_RISCV_HPP