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