Mercurial > hg > truffle
comparison src/cpu/x86/vm/jvmciCodeInstaller_x86.cpp @ 21559:be896a1983c0
recast all Graal native code as JVMCI code (JBS:GRAAL-53)
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Thu, 28 May 2015 15:36:48 +0200 |
parents | src/cpu/x86/vm/graalCodeInstaller_x86.cpp@b8d3f84740c7 |
children |
comparison
equal
deleted
inserted
replaced
21558:d563baeca9df | 21559:be896a1983c0 |
---|---|
1 /* | |
2 * Copyright (c) 2013, 2014, 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 "precompiled.hpp" | |
25 #include "compiler/disassembler.hpp" | |
26 #include "runtime/javaCalls.hpp" | |
27 #include "jvmci/jvmciEnv.hpp" | |
28 #include "jvmci/jvmciCodeInstaller.hpp" | |
29 #include "jvmci/jvmciJavaAccess.hpp" | |
30 #include "jvmci/jvmciCompilerToVM.hpp" | |
31 #include "jvmci/jvmciRuntime.hpp" | |
32 #include "asm/register.hpp" | |
33 #include "classfile/vmSymbols.hpp" | |
34 #include "code/vmreg.hpp" | |
35 #include "vmreg_x86.inline.hpp" | |
36 | |
37 jint CodeInstaller::pd_next_offset(NativeInstruction* inst, jint pc_offset, oop method) { | |
38 if (inst->is_call() || inst->is_jump()) { | |
39 assert(NativeCall::instruction_size == (int)NativeJump::instruction_size, "unexpected size"); | |
40 return (pc_offset + NativeCall::instruction_size); | |
41 } else if (inst->is_mov_literal64()) { | |
42 // mov+call instruction pair | |
43 jint offset = pc_offset + NativeMovConstReg::instruction_size; | |
44 u_char* call = (u_char*) (_instructions->start() + offset); | |
45 if (call[0] == Assembler::REX_B) { | |
46 offset += 1; /* prefix byte for extended register R8-R15 */ | |
47 call++; | |
48 } | |
49 assert(call[0] == 0xFF, "expected call"); | |
50 offset += 2; /* opcode byte + modrm byte */ | |
51 return (offset); | |
52 } else if (inst->is_call_reg()) { | |
53 // the inlined vtable stub contains a "call register" instruction | |
54 assert(method != NULL, "only valid for virtual calls"); | |
55 return (pc_offset + ((NativeCallReg *) inst)->next_instruction_offset()); | |
56 } else if (inst->is_cond_jump()) { | |
57 address pc = (address) (inst); | |
58 return pc_offset + (jint) (Assembler::locate_next_instruction(pc) - pc); | |
59 } else { | |
60 fatal("unsupported type of instruction for call site"); | |
61 return 0; | |
62 } | |
63 } | |
64 | |
65 void CodeInstaller::pd_patch_OopConstant(int pc_offset, Handle& constant) { | |
66 address pc = _instructions->start() + pc_offset; | |
67 Handle obj = HotSpotObjectConstantImpl::object(constant); | |
68 jobject value = JNIHandles::make_local(obj()); | |
69 if (HotSpotObjectConstantImpl::compressed(constant)) { | |
70 #ifdef _LP64 | |
71 address operand = Assembler::locate_operand(pc, Assembler::narrow_oop_operand); | |
72 int oop_index = _oop_recorder->find_index(value); | |
73 _instructions->relocate(pc, oop_Relocation::spec(oop_index), Assembler::narrow_oop_operand); | |
74 TRACE_jvmci_3("relocating (narrow oop constant) at %p/%p", pc, operand); | |
75 #else | |
76 fatal("compressed oop on 32bit"); | |
77 #endif | |
78 } else { | |
79 address operand = Assembler::locate_operand(pc, Assembler::imm_operand); | |
80 *((jobject*) operand) = value; | |
81 _instructions->relocate(pc, oop_Relocation::spec_for_immediate(), Assembler::imm_operand); | |
82 TRACE_jvmci_3("relocating (oop constant) at %p/%p", pc, operand); | |
83 } | |
84 } | |
85 | |
86 void CodeInstaller::pd_patch_DataSectionReference(int pc_offset, int data_offset) { | |
87 address pc = _instructions->start() + pc_offset; | |
88 | |
89 address operand = Assembler::locate_operand(pc, Assembler::disp32_operand); | |
90 address next_instruction = Assembler::locate_next_instruction(pc); | |
91 address dest = _constants->start() + data_offset; | |
92 | |
93 long disp = dest - next_instruction; | |
94 assert(disp == (jint) disp, "disp doesn't fit in 32 bits"); | |
95 *((jint*) operand) = (jint) disp; | |
96 | |
97 _instructions->relocate(pc, section_word_Relocation::spec((address) dest, CodeBuffer::SECT_CONSTS), Assembler::disp32_operand); | |
98 TRACE_jvmci_3("relocating at %p/%p with destination at %p (%d)", pc, operand, dest, data_offset); | |
99 } | |
100 | |
101 void CodeInstaller::pd_relocate_CodeBlob(CodeBlob* cb, NativeInstruction* inst) { | |
102 if (cb->is_nmethod()) { | |
103 nmethod* nm = (nmethod*) cb; | |
104 nativeJump_at((address)inst)->set_jump_destination(nm->verified_entry_point()); | |
105 } else { | |
106 nativeJump_at((address)inst)->set_jump_destination(cb->code_begin()); | |
107 } | |
108 _instructions->relocate((address)inst, runtime_call_Relocation::spec(), Assembler::call32_operand); | |
109 } | |
110 | |
111 void CodeInstaller::pd_relocate_ForeignCall(NativeInstruction* inst, jlong foreign_call_destination) { | |
112 address pc = (address) inst; | |
113 if (inst->is_call()) { | |
114 // NOTE: for call without a mov, the offset must fit a 32-bit immediate | |
115 // see also CompilerToVM.getMaxCallTargetOffset() | |
116 NativeCall* call = nativeCall_at(pc); | |
117 call->set_destination((address) foreign_call_destination); | |
118 _instructions->relocate(call->instruction_address(), runtime_call_Relocation::spec(), Assembler::call32_operand); | |
119 } else if (inst->is_mov_literal64()) { | |
120 NativeMovConstReg* mov = nativeMovConstReg_at(pc); | |
121 mov->set_data((intptr_t) foreign_call_destination); | |
122 _instructions->relocate(mov->instruction_address(), runtime_call_Relocation::spec(), Assembler::imm_operand); | |
123 } else if (inst->is_jump()) { | |
124 NativeJump* jump = nativeJump_at(pc); | |
125 jump->set_jump_destination((address) foreign_call_destination); | |
126 _instructions->relocate(jump->instruction_address(), runtime_call_Relocation::spec(), Assembler::call32_operand); | |
127 } else if (inst->is_cond_jump()) { | |
128 address old_dest = nativeGeneralJump_at(pc)->jump_destination(); | |
129 address disp = Assembler::locate_operand(pc, Assembler::call32_operand); | |
130 *(jint*) disp += ((address) foreign_call_destination) - old_dest; | |
131 _instructions->relocate(pc, runtime_call_Relocation::spec(), Assembler::call32_operand); | |
132 } else { | |
133 fatal("unsupported relocation for foreign call"); | |
134 } | |
135 | |
136 TRACE_jvmci_3("relocating (foreign call) at %p", inst); | |
137 } | |
138 | |
139 void CodeInstaller::pd_relocate_JavaMethod(oop hotspot_method, jint pc_offset) { | |
140 #ifdef ASSERT | |
141 Method* method = NULL; | |
142 // we need to check, this might also be an unresolved method | |
143 if (hotspot_method->is_a(HotSpotResolvedJavaMethodImpl::klass())) { | |
144 method = getMethodFromHotSpotMethod(hotspot_method); | |
145 } | |
146 #endif | |
147 switch (_next_call_type) { | |
148 case INLINE_INVOKE: | |
149 break; | |
150 case INVOKEVIRTUAL: | |
151 case INVOKEINTERFACE: { | |
152 assert(method == NULL || !method->is_static(), "cannot call static method with invokeinterface"); | |
153 | |
154 NativeCall* call = nativeCall_at(_instructions->start() + pc_offset); | |
155 call->set_destination(SharedRuntime::get_resolve_virtual_call_stub()); | |
156 _instructions->relocate(call->instruction_address(), | |
157 virtual_call_Relocation::spec(_invoke_mark_pc), | |
158 Assembler::call32_operand); | |
159 break; | |
160 } | |
161 case INVOKESTATIC: { | |
162 assert(method == NULL || method->is_static(), "cannot call non-static method with invokestatic"); | |
163 | |
164 NativeCall* call = nativeCall_at(_instructions->start() + pc_offset); | |
165 call->set_destination(SharedRuntime::get_resolve_static_call_stub()); | |
166 _instructions->relocate(call->instruction_address(), | |
167 relocInfo::static_call_type, Assembler::call32_operand); | |
168 break; | |
169 } | |
170 case INVOKESPECIAL: { | |
171 assert(method == NULL || !method->is_static(), "cannot call static method with invokespecial"); | |
172 NativeCall* call = nativeCall_at(_instructions->start() + pc_offset); | |
173 call->set_destination(SharedRuntime::get_resolve_opt_virtual_call_stub()); | |
174 _instructions->relocate(call->instruction_address(), | |
175 relocInfo::opt_virtual_call_type, Assembler::call32_operand); | |
176 break; | |
177 } | |
178 default: | |
179 break; | |
180 } | |
181 } | |
182 | |
183 static void relocate_poll_near(address pc) { | |
184 NativeInstruction* ni = nativeInstruction_at(pc); | |
185 int32_t* disp = (int32_t*) Assembler::locate_operand(pc, Assembler::disp32_operand); | |
186 int32_t offset = *disp; // The Java code installed the polling page offset into the disp32 operand | |
187 intptr_t new_disp = (intptr_t) (os::get_polling_page() + offset) - (intptr_t) ni; | |
188 *disp = (int32_t)new_disp; | |
189 } | |
190 | |
191 | |
192 void CodeInstaller::pd_relocate_poll(address pc, jint mark) { | |
193 switch (mark) { | |
194 case POLL_NEAR: { | |
195 relocate_poll_near(pc); | |
196 _instructions->relocate(pc, relocInfo::poll_type, Assembler::disp32_operand); | |
197 break; | |
198 } | |
199 case POLL_FAR: | |
200 // This is a load from a register so there is no relocatable operand. | |
201 // We just have to ensure that the format is not disp32_operand | |
202 // so that poll_Relocation::fix_relocation_after_move does the right | |
203 // thing (i.e. ignores this relocation record) | |
204 _instructions->relocate(pc, relocInfo::poll_type, Assembler::imm_operand); | |
205 break; | |
206 case POLL_RETURN_NEAR: { | |
207 relocate_poll_near(pc); | |
208 _instructions->relocate(pc, relocInfo::poll_return_type, Assembler::disp32_operand); | |
209 break; | |
210 } | |
211 case POLL_RETURN_FAR: | |
212 // see comment above for POLL_FAR | |
213 _instructions->relocate(pc, relocInfo::poll_return_type, Assembler::imm_operand); | |
214 break; | |
215 default: | |
216 fatal("invalid mark value"); | |
217 break; | |
218 } | |
219 } | |
220 | |
221 // convert JVMCI register indices (as used in oop maps) to HotSpot registers | |
222 VMReg CodeInstaller::get_hotspot_reg(jint jvmci_reg) { | |
223 if (jvmci_reg < RegisterImpl::number_of_registers) { | |
224 return as_Register(jvmci_reg)->as_VMReg(); | |
225 } else { | |
226 jint floatRegisterNumber = jvmci_reg - RegisterImpl::number_of_registers; | |
227 if (floatRegisterNumber < XMMRegisterImpl::number_of_registers) { | |
228 return as_XMMRegister(floatRegisterNumber)->as_VMReg(); | |
229 } | |
230 ShouldNotReachHere(); | |
231 return NULL; | |
232 } | |
233 } | |
234 | |
235 bool CodeInstaller::is_general_purpose_reg(VMReg hotspotRegister) { | |
236 return !(hotspotRegister->is_FloatRegister() || hotspotRegister->is_XMMRegister()); | |
237 } |