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