1 /*
   2  * Copyright (c) 2013, 2017, 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 #include "jvmci/jvmciCodeInstaller.hpp"
  25 #include "jvmci/jvmciRuntime.hpp"
  26 #include "jvmci/jvmciCompilerToVM.hpp"
  27 #include "jvmci/jvmciJavaClasses.hpp"
  28 #include "oops/oop.inline.hpp"
  29 #include "runtime/sharedRuntime.hpp"
  30 #include "utilities/align.hpp"
  31 #include "vmreg_sparc.inline.hpp"
  32 
  33 jint CodeInstaller::pd_next_offset(NativeInstruction* inst, jint pc_offset, Handle method, TRAPS) {
  34   if (inst->is_call() || inst->is_jump()) {
  35     return pc_offset + NativeCall::instruction_size;
  36   } else if (inst->is_call_reg()) {
  37     return pc_offset + NativeCallReg::instruction_size;
  38   } else if (inst->is_sethi()) {
  39     return pc_offset + NativeFarCall::instruction_size;
  40   } else {
  41     JVMCI_ERROR_0("unsupported type of instruction for call site");
  42     return 0;
  43   }
  44 }
  45 
  46 void CodeInstaller::pd_patch_OopConstant(int pc_offset, Handle constant, TRAPS) {
  47   address pc = _instructions->start() + pc_offset;
  48   Handle obj(THREAD, HotSpotObjectConstantImpl::object(constant));
  49   jobject value = JNIHandles::make_local(obj());
  50   if (HotSpotObjectConstantImpl::compressed(constant)) {
  51     int oop_index = _oop_recorder->find_index(value);
  52     RelocationHolder rspec = oop_Relocation::spec(oop_index);
  53     _instructions->relocate(pc, rspec, 1);
  54   } else {
  55     NativeMovConstReg* move = nativeMovConstReg_at(pc);
  56     move->set_data((intptr_t) value);
  57 
  58     // We need two relocations:  one on the sethi and one on the add.
  59     int oop_index = _oop_recorder->find_index(value);
  60     RelocationHolder rspec = oop_Relocation::spec(oop_index);
  61     _instructions->relocate(pc + NativeMovConstReg::sethi_offset, rspec);
  62     _instructions->relocate(pc + NativeMovConstReg::add_offset, rspec);
  63   }
  64 }
  65 
  66 void CodeInstaller::pd_patch_MetaspaceConstant(int pc_offset, Handle constant, TRAPS) {
  67   address pc = _instructions->start() + pc_offset;
  68   if (HotSpotMetaspaceConstantImpl::compressed(constant)) {
  69     NativeMovConstReg32* move = nativeMovConstReg32_at(pc);
  70     narrowKlass narrowOop = record_narrow_metadata_reference(_instructions, pc, constant, CHECK);
  71     move->set_data((intptr_t)narrowOop);
  72     TRACE_jvmci_3("relocating (narrow metaspace constant) at " PTR_FORMAT "/0x%x", p2i(pc), narrowOop);
  73   } else {
  74     NativeMovConstReg* move = nativeMovConstReg_at(pc);
  75     void* reference = record_metadata_reference(_instructions, pc, constant, CHECK);
  76     move->set_data((intptr_t)reference);
  77     TRACE_jvmci_3("relocating (metaspace constant) at " PTR_FORMAT "/" PTR_FORMAT, p2i(pc), p2i(reference));
  78   }
  79 }
  80 
  81 void CodeInstaller::pd_patch_DataSectionReference(int pc_offset, int data_offset, TRAPS) {
  82   address pc = _instructions->start() + pc_offset;
  83   NativeInstruction* inst = nativeInstruction_at(pc);
  84   NativeInstruction* inst1 = nativeInstruction_at(pc + 4);
  85   if(inst->is_sethi() && inst1->is_nop()) {
  86       address const_start = _constants->start();
  87       address dest = _constants->start() + data_offset;
  88       if(_constants_size > 0) {
  89         _instructions->relocate(pc + NativeMovConstReg::sethi_offset, internal_word_Relocation::spec((address) dest));
  90         _instructions->relocate(pc + NativeMovConstReg::add_offset, internal_word_Relocation::spec((address) dest));
  91       }
  92       TRACE_jvmci_3("relocating at " PTR_FORMAT " (+%d) with destination at %d", p2i(pc), pc_offset, data_offset);
  93   }else {
  94     int const_size = align_up(_constants->end()-_constants->start(), CodeEntryAlignment);
  95     NativeMovRegMem* load = nativeMovRegMem_at(pc);
  96     // This offset must match with SPARCLoadConstantTableBaseOp.emitCode
  97     load->set_offset(- (const_size - data_offset + Assembler::min_simm13()));
  98     TRACE_jvmci_3("relocating ld at " PTR_FORMAT " (+%d) with destination at %d", p2i(pc), pc_offset, data_offset);
  99   }
 100 }
 101 
 102 void CodeInstaller::pd_relocate_ForeignCall(NativeInstruction* inst, jlong foreign_call_destination, TRAPS) {
 103   address pc = (address) inst;
 104   if (inst->is_call()) {
 105     NativeCall* call = nativeCall_at(pc);
 106     call->set_destination((address) foreign_call_destination);
 107     _instructions->relocate(call->instruction_address(), runtime_call_Relocation::spec());
 108   } else if (inst->is_sethi()) {
 109     NativeJump* jump = nativeJump_at(pc);
 110     jump->set_jump_destination((address) foreign_call_destination);
 111     _instructions->relocate(jump->instruction_address(), runtime_call_Relocation::spec());
 112   } else {
 113     JVMCI_ERROR("unknown call or jump instruction at " PTR_FORMAT, p2i(pc));
 114   }
 115   TRACE_jvmci_3("relocating (foreign call) at " PTR_FORMAT, p2i(inst));
 116 }
 117 
 118 void CodeInstaller::pd_relocate_JavaMethod(Handle hotspot_method, jint pc_offset, TRAPS) {
 119 #ifdef ASSERT
 120   Method* method = NULL;
 121   // we need to check, this might also be an unresolved method
 122   if (hotspot_method->is_a(HotSpotResolvedJavaMethodImpl::klass())) {
 123     method = getMethodFromHotSpotMethod(hotspot_method());
 124   }
 125 #endif
 126   switch (_next_call_type) {
 127     case INLINE_INVOKE:
 128       break;
 129     case INVOKEVIRTUAL:
 130     case INVOKEINTERFACE: {
 131       assert(method == NULL || !method->is_static(), "cannot call static method with invokeinterface");
 132       NativeCall* call = nativeCall_at(_instructions->start() + pc_offset);
 133       call->set_destination(SharedRuntime::get_resolve_virtual_call_stub());
 134       _instructions->relocate(call->instruction_address(), virtual_call_Relocation::spec(_invoke_mark_pc));
 135       break;
 136     }
 137     case INVOKESTATIC: {
 138       assert(method == NULL || method->is_static(), "cannot call non-static method with invokestatic");
 139       NativeCall* call = nativeCall_at(_instructions->start() + pc_offset);
 140       call->set_destination(SharedRuntime::get_resolve_static_call_stub());
 141       _instructions->relocate(call->instruction_address(), relocInfo::static_call_type);
 142       break;
 143     }
 144     case INVOKESPECIAL: {
 145       assert(method == NULL || !method->is_static(), "cannot call static method with invokespecial");
 146       NativeCall* call = nativeCall_at(_instructions->start() + pc_offset);
 147       call->set_destination(SharedRuntime::get_resolve_opt_virtual_call_stub());
 148       _instructions->relocate(call->instruction_address(), relocInfo::opt_virtual_call_type);
 149       break;
 150     }
 151     default:
 152       JVMCI_ERROR("invalid _next_call_type value");
 153       break;
 154   }
 155 }
 156 
 157 void CodeInstaller::pd_relocate_poll(address pc, jint mark, TRAPS) {
 158   switch (mark) {
 159     case POLL_NEAR:
 160       JVMCI_ERROR("unimplemented");
 161       break;
 162     case POLL_FAR:
 163       _instructions->relocate(pc, relocInfo::poll_type);
 164       break;
 165     case POLL_RETURN_NEAR:
 166       JVMCI_ERROR("unimplemented");
 167       break;
 168     case POLL_RETURN_FAR:
 169       _instructions->relocate(pc, relocInfo::poll_return_type);
 170       break;
 171     default:
 172       JVMCI_ERROR("invalid mark value");
 173       break;
 174   }
 175 }
 176 
 177 // convert JVMCI register indices (as used in oop maps) to HotSpot registers
 178 VMReg CodeInstaller::get_hotspot_reg(jint jvmci_reg, TRAPS) {
 179   // JVMCI Registers are numbered as follows:
 180   //   0..31: Thirty-two General Purpose registers (CPU Registers)
 181   //   32..63: Thirty-two single precision float registers
 182   //   64..95: Thirty-two double precision float registers
 183   //   96..111: Sixteen quad precision float registers
 184   if (jvmci_reg < 32) {
 185     return as_Register(jvmci_reg)->as_VMReg();
 186   } else {
 187     jint floatRegisterNumber;
 188     if(jvmci_reg < 64) { // Single precision
 189       floatRegisterNumber = jvmci_reg - 32;
 190     } else if(jvmci_reg < 96) {
 191       floatRegisterNumber = 2 * (jvmci_reg - 64);
 192     } else if(jvmci_reg < 112) {
 193       floatRegisterNumber = 4 * (jvmci_reg - 96);
 194     } else {
 195       JVMCI_ERROR_NULL("invalid register number: %d", jvmci_reg);
 196     }
 197     return as_FloatRegister(floatRegisterNumber)->as_VMReg();
 198   }
 199 }
 200 
 201 bool CodeInstaller::is_general_purpose_reg(VMReg hotspotRegister) {
 202   return !hotspotRegister->is_FloatRegister();
 203 }