1 /*
   2  * Copyright (c) 2013, 2015, 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, oop method) {
  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     fatal("unsupported type of instruction for call site");
  41     return 0;
  42   }
  43 }
  44 
  45 void CodeInstaller::pd_patch_OopConstant(int pc_offset, Handle& constant) {
  46   address pc = _instructions->start() + pc_offset;
  47   Handle obj = HotSpotObjectConstantImpl::object(constant);
  48   jobject value = JNIHandles::make_local(obj());
  49   if (HotSpotObjectConstantImpl::compressed(constant)) {
  50 #ifdef _LP64
  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     fatal("compressed oop on 32bit");
  56 #endif
  57   } else {
  58     NativeMovConstReg* move = nativeMovConstReg_at(pc);
  59     move->set_data((intptr_t) value);
  60 
  61     // We need two relocations:  one on the sethi and one on the add.
  62     int oop_index = _oop_recorder->find_index(value);
  63     RelocationHolder rspec = oop_Relocation::spec(oop_index);
  64     _instructions->relocate(pc + NativeMovConstReg::sethi_offset, rspec);
  65     _instructions->relocate(pc + NativeMovConstReg::add_offset, rspec);
  66   }
  67 }
  68 
  69 void CodeInstaller::pd_patch_DataSectionReference(int pc_offset, int data_offset) {
  70   address pc = _instructions->start() + pc_offset;
  71   NativeInstruction* inst = nativeInstruction_at(pc);
  72   NativeInstruction* inst1 = nativeInstruction_at(pc + 4);
  73   if(inst->is_sethi() && inst1->is_nop()) {
  74       address const_start = _constants->start();
  75       address dest = _constants->start() + data_offset;
  76       if(_constants_size > 0) {
  77         _instructions->relocate(pc + NativeMovConstReg::sethi_offset, internal_word_Relocation::spec((address) dest));
  78         _instructions->relocate(pc + NativeMovConstReg::add_offset, internal_word_Relocation::spec((address) dest));
  79       }
  80       TRACE_jvmci_3("relocating at " PTR_FORMAT " (+%d) with destination at %d", p2i(pc), pc_offset, data_offset);
  81   }else {
  82     int const_size = align_size_up(_constants->end()-_constants->start(), CodeEntryAlignment);
  83     NativeMovRegMem* load = nativeMovRegMem_at(pc);
  84     // This offset must match with SPARCLoadConstantTableBaseOp.emitCode
  85     load->set_offset(- (const_size - data_offset + Assembler::min_simm13()));
  86     TRACE_jvmci_3("relocating ld at " PTR_FORMAT " (+%d) with destination at %d", p2i(pc), pc_offset, data_offset);
  87   }
  88 }
  89 
  90 void CodeInstaller::pd_relocate_CodeBlob(CodeBlob* cb, NativeInstruction* inst) {
  91   fatal("CodeInstaller::pd_relocate_CodeBlob - sparc unimp");
  92 }
  93 
  94 void CodeInstaller::pd_relocate_ForeignCall(NativeInstruction* inst, jlong foreign_call_destination) {
  95   address pc = (address) inst;
  96   if (inst->is_call()) {
  97     NativeCall* call = nativeCall_at(pc);
  98     call->set_destination((address) foreign_call_destination);
  99     _instructions->relocate(call->instruction_address(), runtime_call_Relocation::spec());
 100   } else if (inst->is_sethi()) {
 101     NativeJump* jump = nativeJump_at(pc);
 102     jump->set_jump_destination((address) foreign_call_destination);
 103     _instructions->relocate(jump->instruction_address(), runtime_call_Relocation::spec());
 104   } else {
 105     fatal(err_msg("unknown call or jump instruction at " PTR_FORMAT, p2i(pc)));
 106   }
 107   TRACE_jvmci_3("relocating (foreign call) at " PTR_FORMAT, p2i(inst));
 108 }
 109 
 110 void CodeInstaller::pd_relocate_JavaMethod(oop hotspot_method, jint pc_offset) {
 111 #ifdef ASSERT
 112   Method* method = NULL;
 113   // we need to check, this might also be an unresolved method
 114   if (hotspot_method->is_a(HotSpotResolvedJavaMethodImpl::klass())) {
 115     method = getMethodFromHotSpotMethod(hotspot_method);
 116   }
 117 #endif
 118   switch (_next_call_type) {
 119     case INLINE_INVOKE:
 120       break;
 121     case INVOKEVIRTUAL:
 122     case INVOKEINTERFACE: {
 123       assert(method == NULL || !method->is_static(), "cannot call static method with invokeinterface");
 124       NativeCall* call = nativeCall_at(_instructions->start() + pc_offset);
 125       call->set_destination(SharedRuntime::get_resolve_virtual_call_stub());
 126       _instructions->relocate(call->instruction_address(), virtual_call_Relocation::spec(_invoke_mark_pc));
 127       break;
 128     }
 129     case INVOKESTATIC: {
 130       assert(method == NULL || method->is_static(), "cannot call non-static method with invokestatic");
 131       NativeCall* call = nativeCall_at(_instructions->start() + pc_offset);
 132       call->set_destination(SharedRuntime::get_resolve_static_call_stub());
 133       _instructions->relocate(call->instruction_address(), relocInfo::static_call_type);
 134       break;
 135     }
 136     case INVOKESPECIAL: {
 137       assert(method == NULL || !method->is_static(), "cannot call static method with invokespecial");
 138       NativeCall* call = nativeCall_at(_instructions->start() + pc_offset);
 139       call->set_destination(SharedRuntime::get_resolve_opt_virtual_call_stub());
 140       _instructions->relocate(call->instruction_address(), relocInfo::opt_virtual_call_type);
 141       break;
 142     }
 143     default:
 144       fatal("invalid _next_call_type value");
 145       break;
 146   }
 147 }
 148 
 149 void CodeInstaller::pd_relocate_poll(address pc, jint mark) {
 150   switch (mark) {
 151     case POLL_NEAR:
 152       fatal("unimplemented");
 153       break;
 154     case POLL_FAR:
 155       _instructions->relocate(pc, relocInfo::poll_type);
 156       break;
 157     case POLL_RETURN_NEAR:
 158       fatal("unimplemented");
 159       break;
 160     case POLL_RETURN_FAR:
 161       _instructions->relocate(pc, relocInfo::poll_return_type);
 162       break;
 163     default:
 164       fatal("invalid mark value");
 165       break;
 166   }
 167 }
 168 
 169 // convert JVMCI register indices (as used in oop maps) to HotSpot registers
 170 VMReg CodeInstaller::get_hotspot_reg(jint jvmci_reg) {
 171   if (jvmci_reg < RegisterImpl::number_of_registers) {
 172     return as_Register(jvmci_reg)->as_VMReg();
 173   } else {
 174     jint floatRegisterNumber = jvmci_reg - RegisterImpl::number_of_registers;
 175     floatRegisterNumber += MAX2(0, floatRegisterNumber-32); // Beginning with f32, only every second register is going to be addressed
 176     if (floatRegisterNumber < FloatRegisterImpl::number_of_registers) {
 177       return as_FloatRegister(floatRegisterNumber)->as_VMReg();
 178     }
 179     ShouldNotReachHere();
 180     return NULL;
 181   }
 182 }
 183 
 184 bool CodeInstaller::is_general_purpose_reg(VMReg hotspotRegister) {
 185   return !hotspotRegister->is_FloatRegister();
 186 }