43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
|
50330
|
3 |
* Copyright (c) 2018, Red Hat Inc. All rights reserved.
|
43972
|
4 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
5 |
*
|
|
6 |
* This code is free software; you can redistribute it and/or modify it
|
|
7 |
* under the terms of the GNU General Public License version 2 only, as
|
|
8 |
* published by the Free Software Foundation.
|
|
9 |
*
|
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
14 |
* accompanied this code).
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License version
|
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
19 |
*
|
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
21 |
* or visit www.oracle.com if you need additional information or have any
|
|
22 |
* questions.
|
|
23 |
*/
|
50858
|
24 |
|
|
25 |
|
43972
|
26 |
package org.graalvm.compiler.lir.aarch64;
|
|
27 |
|
50330
|
28 |
import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
|
43972
|
29 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.ILLEGAL;
|
|
30 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
|
|
31 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.STACK;
|
|
32 |
import static jdk.vm.ci.aarch64.AArch64.r8;
|
|
33 |
import static jdk.vm.ci.code.ValueUtil.asRegister;
|
|
34 |
import static jdk.vm.ci.code.ValueUtil.isRegister;
|
|
35 |
|
|
36 |
import org.graalvm.compiler.asm.Label;
|
|
37 |
import org.graalvm.compiler.asm.aarch64.AArch64Assembler;
|
|
38 |
import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler;
|
|
39 |
import org.graalvm.compiler.core.common.spi.ForeignCallLinkage;
|
|
40 |
import org.graalvm.compiler.lir.LIRFrameState;
|
|
41 |
import org.graalvm.compiler.lir.LIRInstructionClass;
|
|
42 |
import org.graalvm.compiler.lir.Opcode;
|
|
43 |
import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
|
|
44 |
|
|
45 |
import jdk.vm.ci.code.Register;
|
|
46 |
import jdk.vm.ci.meta.InvokeTarget;
|
|
47 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
48 |
import jdk.vm.ci.meta.Value;
|
|
49 |
|
|
50 |
public class AArch64Call {
|
|
51 |
|
|
52 |
public abstract static class CallOp extends AArch64LIRInstruction {
|
|
53 |
@Def({REG, ILLEGAL}) protected Value result;
|
|
54 |
@Use({REG, STACK}) protected Value[] parameters;
|
|
55 |
@Temp({REG, STACK}) protected Value[] temps;
|
|
56 |
@State protected LIRFrameState state;
|
|
57 |
|
|
58 |
protected CallOp(LIRInstructionClass<? extends CallOp> c, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
|
|
59 |
super(c);
|
|
60 |
this.result = result;
|
|
61 |
this.parameters = parameters;
|
|
62 |
this.state = state;
|
|
63 |
this.temps = addStackSlotsToTemporaries(parameters, temps);
|
|
64 |
assert temps != null;
|
|
65 |
}
|
|
66 |
|
|
67 |
@Override
|
|
68 |
public boolean destroysCallerSavedRegisters() {
|
|
69 |
return true;
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
public abstract static class MethodCallOp extends CallOp {
|
|
74 |
protected final ResolvedJavaMethod callTarget;
|
|
75 |
|
|
76 |
protected MethodCallOp(LIRInstructionClass<? extends MethodCallOp> c, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
|
|
77 |
super(c, result, parameters, temps, state);
|
|
78 |
this.callTarget = callTarget;
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
@Opcode("CALL_INDIRECT")
|
|
83 |
public static class IndirectCallOp extends MethodCallOp {
|
|
84 |
public static final LIRInstructionClass<IndirectCallOp> TYPE = LIRInstructionClass.create(IndirectCallOp.class);
|
|
85 |
|
|
86 |
@Use({REG}) protected Value targetAddress;
|
|
87 |
|
|
88 |
public IndirectCallOp(ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, Value targetAddress, LIRFrameState state) {
|
|
89 |
this(TYPE, callTarget, result, parameters, temps, targetAddress, state);
|
|
90 |
}
|
|
91 |
|
|
92 |
protected IndirectCallOp(LIRInstructionClass<? extends IndirectCallOp> c, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, Value targetAddress,
|
|
93 |
LIRFrameState state) {
|
|
94 |
super(c, callTarget, result, parameters, temps, state);
|
|
95 |
this.targetAddress = targetAddress;
|
|
96 |
}
|
|
97 |
|
|
98 |
@Override
|
|
99 |
public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
|
|
100 |
Register target = asRegister(targetAddress);
|
|
101 |
indirectCall(crb, masm, target, callTarget, state);
|
|
102 |
}
|
|
103 |
|
|
104 |
@Override
|
|
105 |
public void verify() {
|
|
106 |
super.verify();
|
|
107 |
assert isRegister(targetAddress) : "The current register allocator cannot handle variables to be used at call sites, " + "it must be in a fixed register for now";
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
@Opcode("CALL_DIRECT")
|
|
112 |
public abstract static class DirectCallOp extends MethodCallOp {
|
|
113 |
public static final LIRInstructionClass<DirectCallOp> TYPE = LIRInstructionClass.create(DirectCallOp.class);
|
|
114 |
|
|
115 |
public DirectCallOp(ResolvedJavaMethod target, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
|
|
116 |
super(TYPE, target, result, parameters, temps, state);
|
|
117 |
}
|
|
118 |
|
|
119 |
protected DirectCallOp(LIRInstructionClass<? extends DirectCallOp> c, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
|
|
120 |
super(c, callTarget, result, parameters, temps, state);
|
|
121 |
}
|
|
122 |
|
|
123 |
@Override
|
|
124 |
public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
|
|
125 |
directCall(crb, masm, callTarget, null, state);
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
public abstract static class ForeignCallOp extends CallOp {
|
|
130 |
protected final ForeignCallLinkage callTarget;
|
|
131 |
protected final Label label;
|
|
132 |
|
|
133 |
protected ForeignCallOp(LIRInstructionClass<? extends ForeignCallOp> c, ForeignCallLinkage callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state, Label label) {
|
|
134 |
super(c, result, parameters, temps, state);
|
|
135 |
this.callTarget = callTarget;
|
|
136 |
this.label = label;
|
|
137 |
}
|
|
138 |
|
|
139 |
@Override
|
|
140 |
public boolean destroysCallerSavedRegisters() {
|
|
141 |
return callTarget.destroysRegisters();
|
|
142 |
}
|
|
143 |
|
|
144 |
@Override
|
|
145 |
public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
|
|
146 |
emitCall(crb, masm);
|
|
147 |
}
|
|
148 |
|
|
149 |
protected abstract void emitCall(CompilationResultBuilder crb, AArch64MacroAssembler masm);
|
|
150 |
}
|
|
151 |
|
|
152 |
@Opcode("NEAR_FOREIGN_CALL")
|
|
153 |
public static class DirectNearForeignCallOp extends ForeignCallOp {
|
|
154 |
public static final LIRInstructionClass<DirectNearForeignCallOp> TYPE = LIRInstructionClass.create(DirectNearForeignCallOp.class);
|
|
155 |
|
|
156 |
public DirectNearForeignCallOp(ForeignCallLinkage callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state, Label label) {
|
|
157 |
super(TYPE, callTarget, result, parameters, temps, state, label);
|
|
158 |
}
|
|
159 |
|
|
160 |
@Override
|
|
161 |
protected void emitCall(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
|
|
162 |
directCall(crb, masm, callTarget, null, state, label);
|
|
163 |
}
|
|
164 |
}
|
|
165 |
|
|
166 |
@Opcode("FAR_FOREIGN_CALL")
|
|
167 |
public static class DirectFarForeignCallOp extends ForeignCallOp {
|
|
168 |
public static final LIRInstructionClass<DirectFarForeignCallOp> TYPE = LIRInstructionClass.create(DirectFarForeignCallOp.class);
|
|
169 |
|
|
170 |
public DirectFarForeignCallOp(ForeignCallLinkage callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state, Label label) {
|
|
171 |
super(TYPE, callTarget, result, parameters, temps, state, label);
|
|
172 |
}
|
|
173 |
|
|
174 |
@Override
|
|
175 |
protected void emitCall(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
|
|
176 |
// We can use any scratch register we want, since we know that they have been saved
|
|
177 |
// before calling.
|
|
178 |
directCall(crb, masm, callTarget, r8, state, label);
|
|
179 |
}
|
|
180 |
}
|
|
181 |
|
|
182 |
/**
|
|
183 |
* Tests whether linkage can be called directly under all circumstances without the need for a
|
|
184 |
* scratch register.
|
|
185 |
*
|
|
186 |
* Note this is a pessimistic assumption: This may return false despite a near call/jump being
|
|
187 |
* adequate.
|
|
188 |
*
|
|
189 |
* @param linkage Foreign call description
|
|
190 |
* @return true if foreign call can be called directly and does not need a scratch register to
|
|
191 |
* load the address into.
|
|
192 |
*/
|
|
193 |
public static boolean isNearCall(ForeignCallLinkage linkage) {
|
|
194 |
long maxOffset = linkage.getMaxCallTargetOffset();
|
|
195 |
return maxOffset != -1 && AArch64MacroAssembler.isBranchImmediateOffset(maxOffset);
|
|
196 |
}
|
|
197 |
|
|
198 |
public static void directCall(CompilationResultBuilder crb, AArch64MacroAssembler masm, InvokeTarget callTarget, Register scratch, LIRFrameState info) {
|
|
199 |
directCall(crb, masm, callTarget, scratch, info, null);
|
|
200 |
}
|
|
201 |
|
|
202 |
public static void directCall(CompilationResultBuilder crb, AArch64MacroAssembler masm, InvokeTarget callTarget, Register scratch, LIRFrameState info, Label label) {
|
|
203 |
int before = masm.position();
|
|
204 |
if (scratch != null) {
|
50330
|
205 |
if (GeneratePIC.getValue(crb.getOptions())) {
|
|
206 |
masm.bl(0);
|
|
207 |
} else {
|
|
208 |
/*
|
|
209 |
* Offset might not fit into a 28-bit immediate, generate an indirect call with a
|
|
210 |
* 64-bit immediate address which is fixed up by HotSpot.
|
|
211 |
*/
|
|
212 |
masm.movNativeAddress(scratch, 0L);
|
|
213 |
masm.blr(scratch);
|
|
214 |
}
|
43972
|
215 |
} else {
|
|
216 |
// Address is fixed up by HotSpot.
|
|
217 |
masm.bl(0);
|
|
218 |
}
|
|
219 |
if (label != null) {
|
|
220 |
// We need this label to be the return address.
|
|
221 |
masm.bind(label);
|
|
222 |
}
|
|
223 |
int after = masm.position();
|
|
224 |
crb.recordDirectCall(before, after, callTarget, info);
|
|
225 |
crb.recordExceptionHandlers(after, info);
|
|
226 |
masm.ensureUniquePC();
|
|
227 |
}
|
|
228 |
|
|
229 |
public static void indirectCall(CompilationResultBuilder crb, AArch64MacroAssembler masm, Register dst, InvokeTarget callTarget, LIRFrameState info) {
|
|
230 |
int before = masm.position();
|
|
231 |
masm.blr(dst);
|
|
232 |
int after = masm.position();
|
|
233 |
crb.recordIndirectCall(before, after, callTarget, info);
|
|
234 |
crb.recordExceptionHandlers(after, info);
|
|
235 |
masm.ensureUniquePC();
|
|
236 |
}
|
|
237 |
|
49451
|
238 |
public static void directJmp(CompilationResultBuilder crb, AArch64MacroAssembler masm, InvokeTarget callTarget) {
|
|
239 |
try (AArch64MacroAssembler.ScratchRegister scratch = masm.getScratchRegister()) {
|
|
240 |
int before = masm.position();
|
50330
|
241 |
if (GeneratePIC.getValue(crb.getOptions())) {
|
|
242 |
masm.jmp();
|
|
243 |
} else {
|
|
244 |
masm.movNativeAddress(scratch.getRegister(), 0L);
|
|
245 |
masm.jmp(scratch.getRegister());
|
|
246 |
}
|
49451
|
247 |
int after = masm.position();
|
|
248 |
crb.recordDirectCall(before, after, callTarget, null);
|
|
249 |
masm.ensureUniquePC();
|
|
250 |
}
|
43972
|
251 |
}
|
|
252 |
|
|
253 |
public static void indirectJmp(CompilationResultBuilder crb, AArch64MacroAssembler masm, Register dst, InvokeTarget target) {
|
|
254 |
int before = masm.position();
|
|
255 |
masm.jmp(dst);
|
|
256 |
int after = masm.position();
|
|
257 |
crb.recordIndirectCall(before, after, target, null);
|
|
258 |
masm.ensureUniquePC();
|
|
259 |
}
|
|
260 |
|
|
261 |
public static void directConditionalJmp(CompilationResultBuilder crb, AArch64MacroAssembler masm, InvokeTarget target, AArch64Assembler.ConditionFlag cond) {
|
|
262 |
int before = masm.position();
|
|
263 |
masm.branchConditionally(cond);
|
|
264 |
int after = masm.position();
|
|
265 |
crb.recordDirectCall(before, after, target, null);
|
|
266 |
masm.ensureUniquePC();
|
|
267 |
}
|
|
268 |
|
|
269 |
}
|