-
Notifications
You must be signed in to change notification settings - Fork 392
/
OMRJitCodeRWXObjectFormat.cpp
264 lines (227 loc) · 9.23 KB
/
OMRJitCodeRWXObjectFormat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*******************************************************************************
* Copyright (c) 2021, 2021 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at http://eclipse.org/legal/epl-2.0
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include "codegen/CodeGenerator.hpp"
#include "codegen/MemoryReference.hpp"
#include "codegen/X86Instruction.hpp"
#include "compile/Compilation.hpp"
#include "il/MethodSymbol.hpp"
#include "il/SymbolReference.hpp"
#include "infra/Assert.hpp"
#include "objectfmt/FunctionCallData.hpp"
#include "objectfmt/JitCodeRWXObjectFormat.hpp"
#include "runtime/Runtime.hpp"
#include "omrformatconsts.h"
TR::Instruction *
OMR::X86::AMD64::JitCodeRWXObjectFormat::emitFunctionCall(TR::FunctionCallData &data)
{
TR::SymbolReference *methodSymRef = NULL;
if (data.runtimeHelperIndex > 0)
{
methodSymRef = data.cg->symRefTab()->findOrCreateRuntimeHelper(data.runtimeHelperIndex, false, false, false);
}
else
{
methodSymRef = data.methodSymRef;
}
/**
* If a targetAddress has been provided then use that to override whatever would be
* derived from the symbol reference.
*/
TR_ASSERT_FATAL_WITH_NODE(data.callNode, !(data.runtimeHelperIndex && data.targetAddress),
"a runtime helper (%" OMR_PRId32 ") and target address (%#" OMR_PRIxPTR ") cannot both be provided",
data.runtimeHelperIndex, data.targetAddress);
uintptr_t targetAddress = data.targetAddress ? data.targetAddress : reinterpret_cast<uintptr_t>(methodSymRef->getMethodAddress());
/**
* The targetAddress must be known at this point. The only exception is a recursive
* call to the method being compiled, as that won't be known until binary encoding
* has completed.
*/
TR_ASSERT_FATAL_WITH_NODE(data.callNode,
(targetAddress || data.cg->comp()->isRecursiveMethodTarget(methodSymRef->getSymbol())),
"function address is unknown");
data.cg->resetIsLeafMethod();
TR::Instruction *callInstr = NULL;
if (data.runtimeHelperIndex || methodSymRef->getSymbol()->castToMethodSymbol()->isHelper())
{
// Helper call
//
TR::X86ImmSymInstruction *callImmSym = NULL;
const TR::InstOpCode::Mnemonic op = data.useCall ? CALLImm4 : JMP4;
if (data.prevInstr)
{
callImmSym = generateImmSymInstruction(data.prevInstr, op, targetAddress, methodSymRef, data.regDeps, data.cg);
}
else
{
callImmSym = generateImmSymInstruction(op, data.callNode, targetAddress, methodSymRef, data.regDeps, data.cg);
}
if (data.adjustsFramePointerBy != 0)
{
callImmSym->setAdjustsFramePointerBy(data.adjustsFramePointerBy);
}
callInstr = callImmSym;
}
else
{
// Non-helper call
//
TR_ASSERT_FATAL_WITH_NODE(data.callNode, data.scratchReg, "scratch register is not available");
TR_ASSERT_FATAL_WITH_NODE(data.callNode, (data.adjustsFramePointerBy == 0),
"frame pointer adjustment not supported for CALLReg instructions");
if (targetAddress == 0)
{
/**
* The only situation where targetAddress could be 0 at this point is for
* a recursive call. This is confirmed/protected by the fatal assert above.
* Emit a direct call to the start of the function whose displacement
* will be properly relocated by the binary encoding of the instruction.
*/
if (data.prevInstr)
{
callInstr = generateImmSymInstruction(data.prevInstr, CALLImm4, 0, data.methodSymRef, data.regDeps, data.cg);
}
else
{
callInstr = generateImmSymInstruction(CALLImm4, data.callNode, 0, data.methodSymRef, data.regDeps, data.cg);
}
}
else
{
/**
* Ideally, only the RegImm64Sym instruction form would be used. However, there are
* subtleties in the handling of relocations between these different forms of instructions
* that need to be sorted out first..
*/
if (data.useSymInstruction)
{
TR::AMD64RegImm64SymInstruction *loadInstr;
if (data.prevInstr)
{
loadInstr = generateRegImm64SymInstruction(
data.prevInstr,
MOV8RegImm64,
data.scratchReg,
targetAddress,
data.methodSymRef,
data.cg);
}
else
{
loadInstr = generateRegImm64SymInstruction(
MOV8RegImm64,
data.callNode,
data.scratchReg,
targetAddress,
data.methodSymRef,
data.cg);
}
if (data.reloKind != TR_NoRelocation)
{
loadInstr->setReloKind(data.reloKind);
}
data.out_materializeTargetAddressInstr = loadInstr;
}
else
{
TR::AMD64RegImm64Instruction *loadInstr;
if (data.prevInstr)
{
loadInstr = generateRegImm64Instruction(
data.prevInstr,
MOV8RegImm64,
data.scratchReg,
targetAddress,
data.cg,
data.reloKind);
}
else
{
loadInstr = generateRegImm64Instruction(
MOV8RegImm64,
data.callNode,
data.scratchReg,
targetAddress,
data.cg,
data.reloKind);
}
data.out_materializeTargetAddressInstr = loadInstr;
}
const TR::InstOpCode::Mnemonic op = data.useCall ? CALLReg : JMPReg;
if (data.prevInstr)
{
/**
* An instruction to materialize the function address was already inserted after
* the provided \c prevInstr, so the call or branch instruction must be inserted
* after the materialization instruction.
*/
callInstr = generateRegInstruction(data.out_materializeTargetAddressInstr, op, data.scratchReg, data.regDeps, data.cg);
}
else
{
callInstr = generateRegInstruction(op, data.callNode, data.scratchReg, data.regDeps, data.cg);
}
}
}
data.out_callInstr = callInstr;
return callInstr;
}
uint8_t *
OMR::X86::AMD64::JitCodeRWXObjectFormat::encodeFunctionCall(TR::FunctionCallData &data)
{
const uint8_t op = data.useCall ? 0xe8 : 0xe9; // CALL rel32 | JMP rel32
*data.bufferAddress++ = op;
int32_t disp32 = 0;
if (data.methodSymRef && data.methodSymRef->getSymbol()->castToMethodSymbol()->isHelper())
{
disp32 = data.cg->branchDisplacementToHelperOrTrampoline(data.bufferAddress+4, data.methodSymRef);
}
else
{
intptr_t targetAddress = reinterpret_cast<intptr_t>(data.methodSymRef->getMethodAddress());
intptr_t nextInstructionAddress = reinterpret_cast<intptr_t>(data.bufferAddress + 4);
TR_ASSERT_FATAL(data.cg->comp()->target().cpu.isTargetWithinRIPRange(targetAddress, nextInstructionAddress),
"Target function address %" OMR_PRIxPTR " not reachable from %" OMR_PRIxPTR, targetAddress, data.bufferAddress);
disp32 = static_cast<int32_t>(targetAddress - nextInstructionAddress);
}
*(reinterpret_cast<int32_t *>(data.bufferAddress)) = disp32;
data.out_encodedMethodAddressLocation = data.bufferAddress;
data.bufferAddress += 4;
return data.bufferAddress;
}
void
OMR::X86::AMD64::JitCodeRWXObjectFormat::printEncodedFunctionCall(TR::FILE *pOutFile, TR::FunctionCallData &data, uint8_t *bufferPos)
{
if (!pOutFile)
{
return;
}
TR_Debug *debug = data.cg->getDebug();
debug->printPrefix(pOutFile, NULL, bufferPos, 5);
trfprintf(pOutFile, data.useCall ? "call\t" : "jmp\t");
if (data.methodSymRef && data.methodSymRef->getSymbol()->castToMethodSymbol()->isHelper())
{
trfprintf(pOutFile, "%s", debug->getRuntimeHelperName(data.methodSymRef->getReferenceNumber()));
}
else
{
trfprintf(pOutFile, "%#" OMR_PRIxPTR, data.methodSymRef->getMethodAddress());
}
}