1 /*
   2  * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/assembler.inline.hpp"
  27 #include "c1/c1_Compilation.hpp"
  28 #include "c1/c1_Instruction.hpp"
  29 #include "c1/c1_InstructionPrinter.hpp"
  30 #include "c1/c1_LIRAssembler.hpp"
  31 #include "c1/c1_MacroAssembler.hpp"
  32 #include "c1/c1_ValueStack.hpp"
  33 #include "ci/ciInstance.hpp"
  34 #include "ci/ciValueKlass.hpp"
  35 #include "gc/shared/barrierSet.hpp"
  36 #include "runtime/os.hpp"
  37 #include "runtime/sharedRuntime.hpp"
  38 
  39 void LIR_Assembler::patching_epilog(PatchingStub* patch, LIR_PatchCode patch_code, Register obj, CodeEmitInfo* info) {
  40   // We must have enough patching space so that call can be inserted.
  41   // We cannot use fat nops here, since the concurrent code rewrite may transiently
  42   // create the illegal instruction sequence.
  43   while ((intx) _masm->pc() - (intx) patch->pc_start() < NativeGeneralJump::instruction_size) {
  44     _masm->nop();
  45   }
  46   patch->install(_masm, patch_code, obj, info);
  47   append_code_stub(patch);
  48 
  49 #ifdef ASSERT
  50   Bytecodes::Code code = info->scope()->method()->java_code_at_bci(info->stack()->bci());
  51   if (patch->id() == PatchingStub::access_field_id) {
  52     switch (code) {
  53       case Bytecodes::_putstatic:
  54       case Bytecodes::_getstatic:
  55       case Bytecodes::_putfield:
  56       case Bytecodes::_getfield:
  57         break;
  58       default:
  59         ShouldNotReachHere();
  60     }
  61   } else if (patch->id() == PatchingStub::load_klass_id) {
  62     switch (code) {
  63       case Bytecodes::_new:
  64       case Bytecodes::_defaultvalue:
  65       case Bytecodes::_anewarray:
  66       case Bytecodes::_multianewarray:
  67       case Bytecodes::_instanceof:
  68       case Bytecodes::_checkcast:
  69         break;
  70       default:
  71         ShouldNotReachHere();
  72     }
  73   } else if (patch->id() == PatchingStub::load_mirror_id) {
  74     switch (code) {
  75       case Bytecodes::_putstatic:
  76       case Bytecodes::_getstatic:
  77       case Bytecodes::_ldc:
  78       case Bytecodes::_ldc_w:
  79         break;
  80       default:
  81         ShouldNotReachHere();
  82     }
  83   } else if (patch->id() == PatchingStub::load_appendix_id) {
  84     Bytecodes::Code bc_raw = info->scope()->method()->raw_code_at_bci(info->stack()->bci());
  85     assert(Bytecodes::has_optional_appendix(bc_raw), "unexpected appendix resolution");
  86   } else {
  87     ShouldNotReachHere();
  88   }
  89 #endif
  90 }
  91 
  92 PatchingStub::PatchID LIR_Assembler::patching_id(CodeEmitInfo* info) {
  93   IRScope* scope = info->scope();
  94   Bytecodes::Code bc_raw = scope->method()->raw_code_at_bci(info->stack()->bci());
  95   if (Bytecodes::has_optional_appendix(bc_raw)) {
  96     return PatchingStub::load_appendix_id;
  97   }
  98   return PatchingStub::load_mirror_id;
  99 }
 100 
 101 //---------------------------------------------------------------
 102 
 103 
 104 LIR_Assembler::LIR_Assembler(Compilation* c):
 105    _masm(c->masm())
 106  , _bs(BarrierSet::barrier_set())
 107  , _compilation(c)
 108  , _frame_map(c->frame_map())
 109  , _current_block(NULL)
 110  , _pending_non_safepoint(NULL)
 111  , _pending_non_safepoint_offset(0)
 112 {
 113   _slow_case_stubs = new CodeStubList();
 114 }
 115 
 116 
 117 LIR_Assembler::~LIR_Assembler() {
 118   // The unwind handler label may be unnbound if this destructor is invoked because of a bail-out.
 119   // Reset it here to avoid an assertion.
 120   _unwind_handler_entry.reset();
 121   _verified_value_entry.reset();
 122 }
 123 
 124 
 125 void LIR_Assembler::check_codespace() {
 126   CodeSection* cs = _masm->code_section();
 127   if (cs->remaining() < (int)(NOT_LP64(1*K)LP64_ONLY(2*K))) {
 128     BAILOUT("CodeBuffer overflow");
 129   }
 130 }
 131 
 132 
 133 void LIR_Assembler::append_code_stub(CodeStub* stub) {
 134   _slow_case_stubs->append(stub);
 135 }
 136 
 137 void LIR_Assembler::emit_stubs(CodeStubList* stub_list) {
 138   for (int m = 0; m < stub_list->length(); m++) {
 139     CodeStub* s = stub_list->at(m);
 140 
 141     check_codespace();
 142     CHECK_BAILOUT();
 143 
 144 #ifndef PRODUCT
 145     if (CommentedAssembly) {
 146       stringStream st;
 147       s->print_name(&st);
 148       st.print(" slow case");
 149       _masm->block_comment(st.as_string());
 150     }
 151 #endif
 152     s->emit_code(this);
 153 #ifdef ASSERT
 154     s->assert_no_unbound_labels();
 155 #endif
 156   }
 157 }
 158 
 159 
 160 void LIR_Assembler::emit_slow_case_stubs() {
 161   emit_stubs(_slow_case_stubs);
 162 }
 163 
 164 
 165 bool LIR_Assembler::needs_icache(ciMethod* method) const {
 166   return !method->is_static();
 167 }
 168 
 169 
 170 int LIR_Assembler::code_offset() const {
 171   return _masm->offset();
 172 }
 173 
 174 
 175 address LIR_Assembler::pc() const {
 176   return _masm->pc();
 177 }
 178 
 179 // To bang the stack of this compiled method we use the stack size
 180 // that the interpreter would need in case of a deoptimization. This
 181 // removes the need to bang the stack in the deoptimization blob which
 182 // in turn simplifies stack overflow handling.
 183 int LIR_Assembler::bang_size_in_bytes() const {
 184   return MAX2(initial_frame_size_in_bytes() + os::extra_bang_size_in_bytes(), _compilation->interpreter_frame_size());
 185 }
 186 
 187 void LIR_Assembler::emit_exception_entries(ExceptionInfoList* info_list) {
 188   for (int i = 0; i < info_list->length(); i++) {
 189     XHandlers* handlers = info_list->at(i)->exception_handlers();
 190 
 191     for (int j = 0; j < handlers->length(); j++) {
 192       XHandler* handler = handlers->handler_at(j);
 193       assert(handler->lir_op_id() != -1, "handler not processed by LinearScan");
 194       assert(handler->entry_code() == NULL ||
 195              handler->entry_code()->instructions_list()->last()->code() == lir_branch ||
 196              handler->entry_code()->instructions_list()->last()->code() == lir_delay_slot, "last operation must be branch");
 197 
 198       if (handler->entry_pco() == -1) {
 199         // entry code not emitted yet
 200         if (handler->entry_code() != NULL && handler->entry_code()->instructions_list()->length() > 1) {
 201           handler->set_entry_pco(code_offset());
 202           if (CommentedAssembly) {
 203             _masm->block_comment("Exception adapter block");
 204           }
 205           emit_lir_list(handler->entry_code());
 206         } else {
 207           handler->set_entry_pco(handler->entry_block()->exception_handler_pco());
 208         }
 209 
 210         assert(handler->entry_pco() != -1, "must be set now");
 211       }
 212     }
 213   }
 214 }
 215 
 216 
 217 void LIR_Assembler::emit_code(BlockList* hir) {
 218   if (PrintLIR) {
 219     print_LIR(hir);
 220   }
 221 
 222   int n = hir->length();
 223   for (int i = 0; i < n; i++) {
 224     emit_block(hir->at(i));
 225     CHECK_BAILOUT();
 226   }
 227 
 228   flush_debug_info(code_offset());
 229 
 230   DEBUG_ONLY(check_no_unbound_labels());
 231 }
 232 
 233 
 234 void LIR_Assembler::emit_block(BlockBegin* block) {
 235   if (block->is_set(BlockBegin::backward_branch_target_flag)) {
 236     align_backward_branch_target();
 237   }
 238 
 239   // if this block is the start of an exception handler, record the
 240   // PC offset of the first instruction for later construction of
 241   // the ExceptionHandlerTable
 242   if (block->is_set(BlockBegin::exception_entry_flag)) {
 243     block->set_exception_handler_pco(code_offset());
 244   }
 245 
 246 #ifndef PRODUCT
 247   if (PrintLIRWithAssembly) {
 248     // don't print Phi's
 249     InstructionPrinter ip(false);
 250     block->print(ip);
 251   }
 252 #endif /* PRODUCT */
 253 
 254   assert(block->lir() != NULL, "must have LIR");
 255   X86_ONLY(assert(_masm->rsp_offset() == 0, "frame size should be fixed"));
 256 
 257 #ifndef PRODUCT
 258   if (CommentedAssembly) {
 259     stringStream st;
 260     st.print_cr(" block B%d [%d, %d]", block->block_id(), block->bci(), block->end()->printable_bci());
 261     _masm->block_comment(st.as_string());
 262   }
 263 #endif
 264 
 265   emit_lir_list(block->lir());
 266 
 267   X86_ONLY(assert(_masm->rsp_offset() == 0, "frame size should be fixed"));
 268 }
 269 
 270 
 271 void LIR_Assembler::emit_lir_list(LIR_List* list) {
 272   peephole(list);
 273 
 274   int n = list->length();
 275   for (int i = 0; i < n; i++) {
 276     LIR_Op* op = list->at(i);
 277 
 278     check_codespace();
 279     CHECK_BAILOUT();
 280 
 281 #ifndef PRODUCT
 282     if (CommentedAssembly) {
 283       // Don't record out every op since that's too verbose.  Print
 284       // branches since they include block and stub names.  Also print
 285       // patching moves since they generate funny looking code.
 286       if (op->code() == lir_branch ||
 287           (op->code() == lir_move && op->as_Op1()->patch_code() != lir_patch_none) ||
 288           (op->code() == lir_leal && op->as_Op1()->patch_code() != lir_patch_none)) {
 289         stringStream st;
 290         op->print_on(&st);
 291         _masm->block_comment(st.as_string());
 292       }
 293     }
 294     if (PrintLIRWithAssembly) {
 295       // print out the LIR operation followed by the resulting assembly
 296       list->at(i)->print(); tty->cr();
 297     }
 298 #endif /* PRODUCT */
 299 
 300     op->emit_code(this);
 301 
 302     if (compilation()->debug_info_recorder()->recording_non_safepoints()) {
 303       process_debug_info(op);
 304     }
 305 
 306 #ifndef PRODUCT
 307     if (PrintLIRWithAssembly) {
 308       _masm->code()->decode();
 309     }
 310 #endif /* PRODUCT */
 311   }
 312 }
 313 
 314 #ifdef ASSERT
 315 void LIR_Assembler::check_no_unbound_labels() {
 316   CHECK_BAILOUT();
 317 
 318   for (int i = 0; i < _branch_target_blocks.length() - 1; i++) {
 319     if (!_branch_target_blocks.at(i)->label()->is_bound()) {
 320       tty->print_cr("label of block B%d is not bound", _branch_target_blocks.at(i)->block_id());
 321       assert(false, "unbound label");
 322     }
 323   }
 324 }
 325 #endif
 326 
 327 //----------------------------------debug info--------------------------------
 328 
 329 
 330 void LIR_Assembler::add_debug_info_for_branch(CodeEmitInfo* info) {
 331   int pc_offset = code_offset();
 332   flush_debug_info(pc_offset);
 333   info->record_debug_info(compilation()->debug_info_recorder(), pc_offset);
 334   if (info->exception_handlers() != NULL) {
 335     compilation()->add_exception_handlers_for_pco(pc_offset, info->exception_handlers());
 336   }
 337 }
 338 
 339 
 340 void LIR_Assembler::add_call_info(int pc_offset, CodeEmitInfo* cinfo) {
 341   flush_debug_info(pc_offset);
 342   cinfo->record_debug_info(compilation()->debug_info_recorder(), pc_offset);
 343   if (cinfo->exception_handlers() != NULL) {
 344     compilation()->add_exception_handlers_for_pco(pc_offset, cinfo->exception_handlers());
 345   }
 346 }
 347 
 348 static ValueStack* debug_info(Instruction* ins) {
 349   StateSplit* ss = ins->as_StateSplit();
 350   if (ss != NULL) return ss->state();
 351   return ins->state_before();
 352 }
 353 
 354 void LIR_Assembler::process_debug_info(LIR_Op* op) {
 355   Instruction* src = op->source();
 356   if (src == NULL)  return;
 357   int pc_offset = code_offset();
 358   if (_pending_non_safepoint == src) {
 359     _pending_non_safepoint_offset = pc_offset;
 360     return;
 361   }
 362   ValueStack* vstack = debug_info(src);
 363   if (vstack == NULL)  return;
 364   if (_pending_non_safepoint != NULL) {
 365     // Got some old debug info.  Get rid of it.
 366     if (debug_info(_pending_non_safepoint) == vstack) {
 367       _pending_non_safepoint_offset = pc_offset;
 368       return;
 369     }
 370     if (_pending_non_safepoint_offset < pc_offset) {
 371       record_non_safepoint_debug_info();
 372     }
 373     _pending_non_safepoint = NULL;
 374   }
 375   // Remember the debug info.
 376   if (pc_offset > compilation()->debug_info_recorder()->last_pc_offset()) {
 377     _pending_non_safepoint = src;
 378     _pending_non_safepoint_offset = pc_offset;
 379   }
 380 }
 381 
 382 // Index caller states in s, where 0 is the oldest, 1 its callee, etc.
 383 // Return NULL if n is too large.
 384 // Returns the caller_bci for the next-younger state, also.
 385 static ValueStack* nth_oldest(ValueStack* s, int n, int& bci_result) {
 386   ValueStack* t = s;
 387   for (int i = 0; i < n; i++) {
 388     if (t == NULL)  break;
 389     t = t->caller_state();
 390   }
 391   if (t == NULL)  return NULL;
 392   for (;;) {
 393     ValueStack* tc = t->caller_state();
 394     if (tc == NULL)  return s;
 395     t = tc;
 396     bci_result = tc->bci();
 397     s = s->caller_state();
 398   }
 399 }
 400 
 401 void LIR_Assembler::record_non_safepoint_debug_info() {
 402   int         pc_offset = _pending_non_safepoint_offset;
 403   ValueStack* vstack    = debug_info(_pending_non_safepoint);
 404   int         bci       = vstack->bci();
 405 
 406   DebugInformationRecorder* debug_info = compilation()->debug_info_recorder();
 407   assert(debug_info->recording_non_safepoints(), "sanity");
 408 
 409   debug_info->add_non_safepoint(pc_offset);
 410 
 411   // Visit scopes from oldest to youngest.
 412   for (int n = 0; ; n++) {
 413     int s_bci = bci;
 414     ValueStack* s = nth_oldest(vstack, n, s_bci);
 415     if (s == NULL)  break;
 416     IRScope* scope = s->scope();
 417     //Always pass false for reexecute since these ScopeDescs are never used for deopt
 418     methodHandle null_mh;
 419     debug_info->describe_scope(pc_offset, null_mh, scope->method(), s->bci(), false/*reexecute*/);
 420   }
 421 
 422   debug_info->end_non_safepoint(pc_offset);
 423 }
 424 
 425 
 426 ImplicitNullCheckStub* LIR_Assembler::add_debug_info_for_null_check_here(CodeEmitInfo* cinfo) {
 427   return add_debug_info_for_null_check(code_offset(), cinfo);
 428 }
 429 
 430 ImplicitNullCheckStub* LIR_Assembler::add_debug_info_for_null_check(int pc_offset, CodeEmitInfo* cinfo) {
 431   ImplicitNullCheckStub* stub = new ImplicitNullCheckStub(pc_offset, cinfo);
 432   append_code_stub(stub);
 433   return stub;
 434 }
 435 
 436 void LIR_Assembler::add_debug_info_for_div0_here(CodeEmitInfo* info) {
 437   add_debug_info_for_div0(code_offset(), info);
 438 }
 439 
 440 void LIR_Assembler::add_debug_info_for_div0(int pc_offset, CodeEmitInfo* cinfo) {
 441   DivByZeroStub* stub = new DivByZeroStub(pc_offset, cinfo);
 442   append_code_stub(stub);
 443 }
 444 
 445 void LIR_Assembler::emit_rtcall(LIR_OpRTCall* op) {
 446   rt_call(op->result_opr(), op->addr(), op->arguments(), op->tmp(), op->info());
 447 }
 448 
 449 
 450 void LIR_Assembler::emit_call(LIR_OpJavaCall* op) {
 451   verify_oop_map(op->info());
 452 
 453   // must align calls sites, otherwise they can't be updated atomically
 454   align_call(op->code());
 455 
 456   // emit the static call stub stuff out of line
 457   emit_static_call_stub();
 458   CHECK_BAILOUT();
 459 
 460   switch (op->code()) {
 461   case lir_static_call:
 462   case lir_dynamic_call:
 463     call(op, relocInfo::static_call_type);
 464     break;
 465   case lir_optvirtual_call:
 466     call(op, relocInfo::opt_virtual_call_type);
 467     break;
 468   case lir_icvirtual_call:
 469     ic_call(op);
 470     break;
 471   case lir_virtual_call:
 472     vtable_call(op);
 473     break;
 474   default:
 475     fatal("unexpected op code: %s", op->name());
 476     break;
 477   }
 478 
 479   // JSR 292
 480   // Record if this method has MethodHandle invokes.
 481   if (op->is_method_handle_invoke()) {
 482     compilation()->set_has_method_handle_invokes(true);
 483   }
 484 
 485   ciMethod* method = op->method();
 486   if (ValueTypeReturnedAsFields && method->signature()->returns_never_null()) {
 487     ciType* return_type = method->return_type();
 488     if (return_type->is_valuetype()) {
 489       ciValueKlass* vk = return_type->as_value_klass();
 490       if (vk->can_be_returned_as_fields()) {
 491         store_value_type_fields_to_buf(vk);
 492       }
 493     }
 494   }
 495 
 496 #if defined(X86) && defined(TIERED)
 497   // C2 leave fpu stack dirty clean it
 498   if (UseSSE < 2) {
 499     int i;
 500     for ( i = 1; i <= 7 ; i++ ) {
 501       ffree(i);
 502     }
 503     if (!op->result_opr()->is_float_kind()) {
 504       ffree(0);
 505     }
 506   }
 507 #endif // X86 && TIERED
 508 }
 509 
 510 
 511 void LIR_Assembler::emit_opLabel(LIR_OpLabel* op) {
 512   _masm->bind (*(op->label()));
 513 }
 514 
 515 
 516 void LIR_Assembler::emit_op1(LIR_Op1* op) {
 517   switch (op->code()) {
 518     case lir_move:
 519       if (op->move_kind() == lir_move_volatile) {
 520         assert(op->patch_code() == lir_patch_none, "can't patch volatiles");
 521         volatile_move_op(op->in_opr(), op->result_opr(), op->type(), op->info());
 522       } else {
 523         move_op(op->in_opr(), op->result_opr(), op->type(),
 524                 op->patch_code(), op->info(), op->pop_fpu_stack(),
 525                 op->move_kind() == lir_move_unaligned,
 526                 op->move_kind() == lir_move_wide);
 527       }
 528       break;
 529 
 530     case lir_roundfp: {
 531       LIR_OpRoundFP* round_op = op->as_OpRoundFP();
 532       roundfp_op(round_op->in_opr(), round_op->tmp(), round_op->result_opr(), round_op->pop_fpu_stack());
 533       break;
 534     }
 535 
 536     case lir_return:
 537       return_op(op->in_opr());
 538       break;
 539 
 540     case lir_safepoint:
 541       if (compilation()->debug_info_recorder()->last_pc_offset() == code_offset()) {
 542         _masm->nop();
 543       }
 544       safepoint_poll(op->in_opr(), op->info());
 545       break;
 546 
 547     case lir_fxch:
 548       fxch(op->in_opr()->as_jint());
 549       break;
 550 
 551     case lir_fld:
 552       fld(op->in_opr()->as_jint());
 553       break;
 554 
 555     case lir_ffree:
 556       ffree(op->in_opr()->as_jint());
 557       break;
 558 
 559     case lir_branch:
 560       break;
 561 
 562     case lir_push:
 563       push(op->in_opr());
 564       break;
 565 
 566     case lir_pop:
 567       pop(op->in_opr());
 568       break;
 569 
 570     case lir_leal:
 571       leal(op->in_opr(), op->result_opr(), op->patch_code(), op->info());
 572       break;
 573 
 574     case lir_null_check: {
 575       ImplicitNullCheckStub* stub = add_debug_info_for_null_check_here(op->info());
 576 
 577       if (op->in_opr()->is_single_cpu()) {
 578         _masm->null_check(op->in_opr()->as_register(), stub->entry());
 579       } else {
 580         Unimplemented();
 581       }
 582       break;
 583     }
 584 
 585     case lir_monaddr:
 586       monitor_address(op->in_opr()->as_constant_ptr()->as_jint(), op->result_opr());
 587       break;
 588 
 589 #ifdef SPARC
 590     case lir_pack64:
 591       pack64(op->in_opr(), op->result_opr());
 592       break;
 593 
 594     case lir_unpack64:
 595       unpack64(op->in_opr(), op->result_opr());
 596       break;
 597 #endif
 598 
 599     case lir_unwind:
 600       unwind_op(op->in_opr());
 601       break;
 602 
 603     default:
 604       Unimplemented();
 605       break;
 606   }
 607 }
 608 
 609 void LIR_Assembler::add_scalarized_entry_info(int pc_offset) {
 610   flush_debug_info(pc_offset);
 611   DebugInformationRecorder* debug_info = compilation()->debug_info_recorder();
 612   // The VEP and VVEP(RO) of a C1-compiled method call buffer_value_args_xxx()
 613   // before doing any argument shuffling. This call may cause GC. When GC happens,
 614   // all the parameters are still as passed by the caller, so we just use
 615   // map->set_include_argument_oops() inside frame::sender_for_compiled_frame(RegisterMap* map).
 616   // There's no need to build a GC map here.
 617   OopMap* oop_map = new OopMap(0, 0);
 618   debug_info->add_safepoint(pc_offset, oop_map);
 619   DebugToken* locvals = debug_info->create_scope_values(NULL); // FIXME is this needed (for Java debugging to work properly??)
 620   DebugToken* expvals = debug_info->create_scope_values(NULL); // FIXME is this needed (for Java debugging to work properly??)
 621   DebugToken* monvals = debug_info->create_monitor_values(NULL); // FIXME: need testing with synchronized method
 622   bool reexecute = false;
 623   bool return_oop = false; // This flag will be ignored since it used only for C2 with escape analysis.
 624   bool rethrow_exception = false;
 625   bool is_method_handle_invoke = false;
 626   debug_info->describe_scope(pc_offset, methodHandle(), method(), 0, reexecute, rethrow_exception, is_method_handle_invoke, return_oop, false, locvals, expvals, monvals);
 627   debug_info->end_safepoint(pc_offset);
 628 }
 629 
 630 // Emit VEP (and VVEP/VEP_RO if necessary)
 631 void LIR_Assembler::emit_std_entries() {
 632   offsets()->set_value(CodeOffsets::OSR_Entry, _masm->offset());
 633 
 634   const CompiledEntrySignature* ces = compilation()->compiled_entry_signature();
 635 
 636   _masm->align(CodeEntryAlignment);
 637   offsets()->set_value(CodeOffsets::Entry, _masm->offset());
 638   if (needs_icache(compilation()->method())) {
 639     check_icache();
 640   }
 641 
 642   if (ces->has_scalarized_args()) {
 643     assert(ValueTypePassFieldsAsArgs && method()->get_Method()->has_scalarized_args(), "must be");
 644 
 645     bool has_value_ro_entry = false;
 646     if (ces->has_value_recv() && ces->num_value_args() > 1) {
 647       // VVEP(RO) = pack all value parameters, except the <this> object.
 648       has_value_ro_entry = true;
 649       add_scalarized_entry_info(emit_std_entry(CodeOffsets::Verified_Value_Entry_RO, ces));
 650     }
 651 
 652     // VEP = pack all value parameters
 653     _masm->align(CodeEntryAlignment);
 654     add_scalarized_entry_info(emit_std_entry(CodeOffsets::Verified_Entry, ces));
 655 
 656     _masm->align(CodeEntryAlignment);
 657     offsets()->set_value(CodeOffsets::Value_Entry, _masm->offset());
 658     if (needs_icache(compilation()->method())) {
 659       check_icache();
 660     }
 661     // VVEP = all value parameters are already passed as refs, so no need for packing.
 662     emit_std_entry(CodeOffsets::Verified_Value_Entry, NULL);
 663 
 664     if (!has_value_ro_entry) {
 665       if (ces->has_value_recv()) {
 666         assert(ces->num_value_args() == 1, "must be");
 667         offsets()->set_value(CodeOffsets::Verified_Value_Entry_RO,
 668                              offsets()->value(CodeOffsets::Verified_Value_Entry));
 669       } else {
 670         assert(ces->num_value_args() > 0, "must be");
 671         offsets()->set_value(CodeOffsets::Verified_Value_Entry_RO,
 672                              offsets()->value(CodeOffsets::Verified_Entry));
 673       }
 674     }
 675   } else {
 676     // All 3 entries are the same (no value-type packing)
 677     int offset = emit_std_entry(CodeOffsets::Verified_Value_Entry, NULL);
 678     offsets()->set_value(CodeOffsets::Verified_Entry, offset);
 679     offsets()->set_value(CodeOffsets::Verified_Value_Entry_RO, offset);
 680   }
 681 }
 682 
 683 int LIR_Assembler::emit_std_entry(CodeOffsets::Entries entry, const CompiledEntrySignature* ces) {
 684   offsets()->set_value(entry, _masm->offset());
 685   switch (entry) {
 686   case CodeOffsets::Verified_Entry:
 687     return _masm->verified_entry(ces, initial_frame_size_in_bytes(), bang_size_in_bytes(), _verified_value_entry);
 688   case CodeOffsets::Verified_Value_Entry_RO:
 689     return _masm->verified_value_ro_entry(ces, initial_frame_size_in_bytes(), bang_size_in_bytes(), _verified_value_entry);
 690   default:
 691     {
 692       int offset = _masm->offset();
 693       assert(entry == CodeOffsets::Verified_Value_Entry, "must be");
 694       _masm->verified_value_entry();
 695       build_frame();
 696       offsets()->set_value(CodeOffsets::Frame_Complete, _masm->offset());
 697       return offset;
 698     }
 699   }
 700 }
 701 
 702 void LIR_Assembler::emit_op0(LIR_Op0* op) {
 703   switch (op->code()) {
 704     case lir_word_align: {
 705       _masm->align(BytesPerWord);
 706       break;
 707     }
 708 
 709     case lir_nop:
 710       assert(op->info() == NULL, "not supported");
 711       _masm->nop();
 712       break;
 713 
 714     case lir_label:
 715       Unimplemented();
 716       break;
 717 
 718     case lir_build_frame:
 719       build_frame();
 720       break;
 721 
 722     case lir_std_entry:
 723       emit_std_entries();
 724       break;
 725 
 726     case lir_osr_entry:
 727       offsets()->set_value(CodeOffsets::OSR_Entry, _masm->offset());
 728       osr_entry();
 729       break;
 730 
 731     case lir_24bit_FPU:
 732       set_24bit_FPU();
 733       break;
 734 
 735     case lir_reset_FPU:
 736       reset_FPU();
 737       break;
 738 
 739     case lir_breakpoint:
 740       breakpoint();
 741       break;
 742 
 743     case lir_fpop_raw:
 744       fpop();
 745       break;
 746 
 747     case lir_membar:
 748       membar();
 749       break;
 750 
 751     case lir_membar_acquire:
 752       membar_acquire();
 753       break;
 754 
 755     case lir_membar_release:
 756       membar_release();
 757       break;
 758 
 759     case lir_membar_loadload:
 760       membar_loadload();
 761       break;
 762 
 763     case lir_membar_storestore:
 764       membar_storestore();
 765       break;
 766 
 767     case lir_membar_loadstore:
 768       membar_loadstore();
 769       break;
 770 
 771     case lir_membar_storeload:
 772       membar_storeload();
 773       break;
 774 
 775     case lir_get_thread:
 776       get_thread(op->result_opr());
 777       break;
 778 
 779     case lir_on_spin_wait:
 780       on_spin_wait();
 781       break;
 782 
 783     default:
 784       ShouldNotReachHere();
 785       break;
 786   }
 787 }
 788 
 789 
 790 void LIR_Assembler::emit_op2(LIR_Op2* op) {
 791   switch (op->code()) {
 792     case lir_cmp:
 793       if (op->info() != NULL) {
 794         assert(op->in_opr1()->is_address() || op->in_opr2()->is_address(),
 795                "shouldn't be codeemitinfo for non-address operands");
 796         add_debug_info_for_null_check_here(op->info()); // exception possible
 797       }
 798       comp_op(op->condition(), op->in_opr1(), op->in_opr2(), op);
 799       break;
 800 
 801     case lir_cmp_l2i:
 802     case lir_cmp_fd2i:
 803     case lir_ucmp_fd2i:
 804       comp_fl2i(op->code(), op->in_opr1(), op->in_opr2(), op->result_opr(), op);
 805       break;
 806 
 807     case lir_cmove:
 808       cmove(op->condition(), op->in_opr1(), op->in_opr2(), op->result_opr(), op->type());
 809       break;
 810 
 811     case lir_shl:
 812     case lir_shr:
 813     case lir_ushr:
 814       if (op->in_opr2()->is_constant()) {
 815         shift_op(op->code(), op->in_opr1(), op->in_opr2()->as_constant_ptr()->as_jint(), op->result_opr());
 816       } else {
 817         shift_op(op->code(), op->in_opr1(), op->in_opr2(), op->result_opr(), op->tmp1_opr());
 818       }
 819       break;
 820 
 821     case lir_add:
 822     case lir_sub:
 823     case lir_mul:
 824     case lir_mul_strictfp:
 825     case lir_div:
 826     case lir_div_strictfp:
 827     case lir_rem:
 828       assert(op->fpu_pop_count() < 2, "");
 829       arith_op(
 830         op->code(),
 831         op->in_opr1(),
 832         op->in_opr2(),
 833         op->result_opr(),
 834         op->info(),
 835         op->fpu_pop_count() == 1);
 836       break;
 837 
 838     case lir_abs:
 839     case lir_sqrt:
 840     case lir_tan:
 841     case lir_log10:
 842       intrinsic_op(op->code(), op->in_opr1(), op->in_opr2(), op->result_opr(), op);
 843       break;
 844 
 845     case lir_neg:
 846       negate(op->in_opr1(), op->result_opr(), op->in_opr2());
 847       break;
 848 
 849     case lir_logic_and:
 850     case lir_logic_or:
 851     case lir_logic_xor:
 852       logic_op(
 853         op->code(),
 854         op->in_opr1(),
 855         op->in_opr2(),
 856         op->result_opr());
 857       break;
 858 
 859     case lir_throw:
 860       throw_op(op->in_opr1(), op->in_opr2(), op->info());
 861       break;
 862 
 863     case lir_xadd:
 864     case lir_xchg:
 865       atomic_op(op->code(), op->in_opr1(), op->in_opr2(), op->result_opr(), op->tmp1_opr());
 866       break;
 867 
 868     default:
 869       Unimplemented();
 870       break;
 871   }
 872 }
 873 
 874 
 875 void LIR_Assembler::build_frame() {
 876   _masm->build_frame(initial_frame_size_in_bytes(), bang_size_in_bytes(),
 877                      compilation()->compiled_entry_signature()->c1_needs_stack_repair(),
 878                      &_verified_value_entry);
 879 }
 880 
 881 
 882 void LIR_Assembler::roundfp_op(LIR_Opr src, LIR_Opr tmp, LIR_Opr dest, bool pop_fpu_stack) {
 883   assert((src->is_single_fpu() && dest->is_single_stack()) ||
 884          (src->is_double_fpu() && dest->is_double_stack()),
 885          "round_fp: rounds register -> stack location");
 886 
 887   reg2stack (src, dest, src->type(), pop_fpu_stack);
 888 }
 889 
 890 
 891 void LIR_Assembler::move_op(LIR_Opr src, LIR_Opr dest, BasicType type, LIR_PatchCode patch_code, CodeEmitInfo* info, bool pop_fpu_stack, bool unaligned, bool wide) {
 892   if (src->is_register()) {
 893     if (dest->is_register()) {
 894       assert(patch_code == lir_patch_none && info == NULL, "no patching and info allowed here");
 895       reg2reg(src,  dest);
 896     } else if (dest->is_stack()) {
 897       assert(patch_code == lir_patch_none && info == NULL, "no patching and info allowed here");
 898       reg2stack(src, dest, type, pop_fpu_stack);
 899     } else if (dest->is_address()) {
 900       reg2mem(src, dest, type, patch_code, info, pop_fpu_stack, wide, unaligned);
 901     } else {
 902       ShouldNotReachHere();
 903     }
 904 
 905   } else if (src->is_stack()) {
 906     assert(patch_code == lir_patch_none && info == NULL, "no patching and info allowed here");
 907     if (dest->is_register()) {
 908       stack2reg(src, dest, type);
 909     } else if (dest->is_stack()) {
 910       stack2stack(src, dest, type);
 911     } else {
 912       ShouldNotReachHere();
 913     }
 914 
 915   } else if (src->is_constant()) {
 916     if (dest->is_register()) {
 917       const2reg(src, dest, patch_code, info); // patching is possible
 918     } else if (dest->is_stack()) {
 919       assert(patch_code == lir_patch_none && info == NULL, "no patching and info allowed here");
 920       const2stack(src, dest);
 921     } else if (dest->is_address()) {
 922       assert(patch_code == lir_patch_none, "no patching allowed here");
 923       const2mem(src, dest, type, info, wide);
 924     } else {
 925       ShouldNotReachHere();
 926     }
 927 
 928   } else if (src->is_address()) {
 929     mem2reg(src, dest, type, patch_code, info, wide, unaligned);
 930 
 931   } else {
 932     ShouldNotReachHere();
 933   }
 934 }
 935 
 936 
 937 void LIR_Assembler::verify_oop_map(CodeEmitInfo* info) {
 938 #ifndef PRODUCT
 939   if (VerifyOops) {
 940     OopMapStream s(info->oop_map());
 941     while (!s.is_done()) {
 942       OopMapValue v = s.current();
 943       if (v.is_oop()) {
 944         VMReg r = v.reg();
 945         if (!r->is_stack()) {
 946           stringStream st;
 947           st.print("bad oop %s at %d", r->as_Register()->name(), _masm->offset());
 948 #ifdef SPARC
 949           _masm->_verify_oop(r->as_Register(), os::strdup(st.as_string(), mtCompiler), __FILE__, __LINE__);
 950 #else
 951           _masm->verify_oop(r->as_Register());
 952 #endif
 953         } else {
 954           _masm->verify_stack_oop(r->reg2stack() * VMRegImpl::stack_slot_size);
 955         }
 956       }
 957       check_codespace();
 958       CHECK_BAILOUT();
 959 
 960       s.next();
 961     }
 962   }
 963 #endif
 964 }