43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.lir.amd64;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.ILLEGAL;
|
|
28 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
|
|
29 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.STACK;
|
|
30 |
import static org.graalvm.compiler.lir.LIRValueUtil.differentRegisters;
|
|
31 |
import static jdk.vm.ci.code.ValueUtil.asRegister;
|
|
32 |
import static jdk.vm.ci.code.ValueUtil.isRegister;
|
|
33 |
|
|
34 |
import org.graalvm.compiler.asm.amd64.AMD64Assembler.ConditionFlag;
|
|
35 |
import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
|
|
36 |
import org.graalvm.compiler.core.common.LIRKind;
|
|
37 |
import org.graalvm.compiler.core.common.spi.ForeignCallLinkage;
|
|
38 |
import org.graalvm.compiler.lir.LIRFrameState;
|
|
39 |
import org.graalvm.compiler.lir.LIRInstructionClass;
|
|
40 |
import org.graalvm.compiler.lir.Opcode;
|
|
41 |
import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
|
|
42 |
import org.graalvm.compiler.lir.gen.DiagnosticLIRGeneratorTool.ZapRegistersAfterInstruction;
|
|
43 |
|
|
44 |
import jdk.vm.ci.amd64.AMD64;
|
|
45 |
import jdk.vm.ci.amd64.AMD64Kind;
|
|
46 |
import jdk.vm.ci.code.Register;
|
|
47 |
import jdk.vm.ci.code.RegisterValue;
|
|
48 |
import jdk.vm.ci.meta.AllocatableValue;
|
|
49 |
import jdk.vm.ci.meta.InvokeTarget;
|
|
50 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
51 |
import jdk.vm.ci.meta.Value;
|
|
52 |
|
|
53 |
public class AMD64Call {
|
|
54 |
|
|
55 |
public abstract static class CallOp extends AMD64LIRInstruction {
|
|
56 |
public static final LIRInstructionClass<CallOp> TYPE = LIRInstructionClass.create(CallOp.class);
|
|
57 |
|
|
58 |
@Def({REG, ILLEGAL}) protected Value result;
|
|
59 |
@Use({REG, STACK}) protected Value[] parameters;
|
|
60 |
@Temp({REG, STACK}) protected Value[] temps;
|
|
61 |
@State protected LIRFrameState state;
|
|
62 |
|
|
63 |
protected CallOp(LIRInstructionClass<? extends CallOp> c, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
|
|
64 |
super(c);
|
|
65 |
this.result = result;
|
|
66 |
this.parameters = parameters;
|
|
67 |
this.state = state;
|
|
68 |
this.temps = addStackSlotsToTemporaries(parameters, temps);
|
|
69 |
assert temps != null;
|
|
70 |
}
|
|
71 |
|
|
72 |
@Override
|
|
73 |
public boolean destroysCallerSavedRegisters() {
|
|
74 |
return true;
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
public abstract static class MethodCallOp extends CallOp {
|
|
79 |
public static final LIRInstructionClass<MethodCallOp> TYPE = LIRInstructionClass.create(MethodCallOp.class);
|
|
80 |
|
|
81 |
protected final ResolvedJavaMethod callTarget;
|
|
82 |
|
|
83 |
protected MethodCallOp(LIRInstructionClass<? extends MethodCallOp> c, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
|
|
84 |
super(c, result, parameters, temps, state);
|
|
85 |
this.callTarget = callTarget;
|
|
86 |
}
|
|
87 |
|
|
88 |
}
|
|
89 |
|
|
90 |
@Opcode("CALL_DIRECT")
|
|
91 |
public static class DirectCallOp extends MethodCallOp {
|
|
92 |
public static final LIRInstructionClass<DirectCallOp> TYPE = LIRInstructionClass.create(DirectCallOp.class);
|
|
93 |
|
|
94 |
public DirectCallOp(ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
|
|
95 |
this(TYPE, callTarget, result, parameters, temps, state);
|
|
96 |
}
|
|
97 |
|
|
98 |
protected DirectCallOp(LIRInstructionClass<? extends DirectCallOp> c, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
|
|
99 |
super(c, callTarget, result, parameters, temps, state);
|
|
100 |
}
|
|
101 |
|
|
102 |
@Override
|
|
103 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
104 |
directCall(crb, masm, callTarget, null, true, state);
|
|
105 |
}
|
48861
|
106 |
|
|
107 |
public int emitCall(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
108 |
return directCall(crb, masm, callTarget, null, true, state);
|
|
109 |
}
|
43972
|
110 |
}
|
|
111 |
|
|
112 |
@Opcode("CALL_INDIRECT")
|
|
113 |
public static class IndirectCallOp extends MethodCallOp {
|
|
114 |
public static final LIRInstructionClass<IndirectCallOp> TYPE = LIRInstructionClass.create(IndirectCallOp.class);
|
|
115 |
|
|
116 |
@Use({REG}) protected Value targetAddress;
|
|
117 |
|
|
118 |
public IndirectCallOp(ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, Value targetAddress, LIRFrameState state) {
|
|
119 |
this(TYPE, callTarget, result, parameters, temps, targetAddress, state);
|
|
120 |
}
|
|
121 |
|
|
122 |
protected IndirectCallOp(LIRInstructionClass<? extends IndirectCallOp> c, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, Value targetAddress,
|
|
123 |
LIRFrameState state) {
|
|
124 |
super(c, callTarget, result, parameters, temps, state);
|
|
125 |
this.targetAddress = targetAddress;
|
|
126 |
}
|
|
127 |
|
|
128 |
@Override
|
|
129 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
130 |
indirectCall(crb, masm, asRegister(targetAddress), callTarget, state);
|
|
131 |
}
|
|
132 |
|
|
133 |
@Override
|
|
134 |
public void verify() {
|
|
135 |
super.verify();
|
|
136 |
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";
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
public abstract static class ForeignCallOp extends CallOp implements ZapRegistersAfterInstruction {
|
|
141 |
public static final LIRInstructionClass<ForeignCallOp> TYPE = LIRInstructionClass.create(ForeignCallOp.class);
|
|
142 |
|
|
143 |
protected final ForeignCallLinkage callTarget;
|
|
144 |
|
|
145 |
public ForeignCallOp(LIRInstructionClass<? extends ForeignCallOp> c, ForeignCallLinkage callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
|
|
146 |
super(c, result, parameters, temps, state);
|
|
147 |
this.callTarget = callTarget;
|
|
148 |
}
|
|
149 |
|
|
150 |
@Override
|
|
151 |
public boolean destroysCallerSavedRegisters() {
|
|
152 |
return callTarget.destroysRegisters();
|
|
153 |
}
|
|
154 |
}
|
|
155 |
|
|
156 |
@Opcode("NEAR_FOREIGN_CALL")
|
|
157 |
public static final class DirectNearForeignCallOp extends ForeignCallOp {
|
|
158 |
public static final LIRInstructionClass<DirectNearForeignCallOp> TYPE = LIRInstructionClass.create(DirectNearForeignCallOp.class);
|
|
159 |
|
|
160 |
public DirectNearForeignCallOp(ForeignCallLinkage linkage, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
|
|
161 |
super(TYPE, linkage, result, parameters, temps, state);
|
|
162 |
}
|
|
163 |
|
|
164 |
@Override
|
|
165 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
166 |
directCall(crb, masm, callTarget, null, false, state);
|
|
167 |
}
|
|
168 |
}
|
|
169 |
|
|
170 |
@Opcode("FAR_FOREIGN_CALL")
|
|
171 |
public static final class DirectFarForeignCallOp extends ForeignCallOp {
|
|
172 |
public static final LIRInstructionClass<DirectFarForeignCallOp> TYPE = LIRInstructionClass.create(DirectFarForeignCallOp.class);
|
|
173 |
|
|
174 |
@Temp({REG}) protected AllocatableValue callTemp;
|
|
175 |
|
|
176 |
public DirectFarForeignCallOp(ForeignCallLinkage callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
|
|
177 |
super(TYPE, callTarget, result, parameters, temps, state);
|
|
178 |
/*
|
|
179 |
* The register allocator does not support virtual registers that are used at the call
|
|
180 |
* site, so use a fixed register.
|
|
181 |
*/
|
|
182 |
callTemp = AMD64.rax.asValue(LIRKind.value(AMD64Kind.QWORD));
|
|
183 |
assert differentRegisters(parameters, callTemp);
|
|
184 |
}
|
|
185 |
|
|
186 |
@Override
|
|
187 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
188 |
directCall(crb, masm, callTarget, ((RegisterValue) callTemp).getRegister(), false, state);
|
|
189 |
}
|
|
190 |
}
|
|
191 |
|
48861
|
192 |
public static int directCall(CompilationResultBuilder crb, AMD64MacroAssembler masm, InvokeTarget callTarget, Register scratch, boolean align, LIRFrameState info) {
|
43972
|
193 |
if (align) {
|
|
194 |
emitAlignmentForDirectCall(crb, masm);
|
|
195 |
}
|
|
196 |
int before = masm.position();
|
48861
|
197 |
int callPCOffset;
|
43972
|
198 |
if (scratch != null) {
|
|
199 |
// offset might not fit a 32-bit immediate, generate an
|
|
200 |
// indirect call with a 64-bit immediate
|
|
201 |
masm.movq(scratch, 0L);
|
48861
|
202 |
callPCOffset = masm.position();
|
43972
|
203 |
masm.call(scratch);
|
|
204 |
} else {
|
48861
|
205 |
callPCOffset = masm.position();
|
43972
|
206 |
masm.call();
|
|
207 |
}
|
|
208 |
int after = masm.position();
|
|
209 |
crb.recordDirectCall(before, after, callTarget, info);
|
|
210 |
crb.recordExceptionHandlers(after, info);
|
|
211 |
masm.ensureUniquePC();
|
48861
|
212 |
return callPCOffset;
|
43972
|
213 |
}
|
|
214 |
|
|
215 |
protected static void emitAlignmentForDirectCall(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
216 |
// make sure that the displacement word of the call ends up word aligned
|
|
217 |
int offset = masm.position();
|
|
218 |
offset += crb.target.arch.getMachineCodeCallDisplacementOffset();
|
|
219 |
int modulus = crb.target.wordSize;
|
|
220 |
if (offset % modulus != 0) {
|
|
221 |
masm.nop(modulus - offset % modulus);
|
|
222 |
}
|
|
223 |
}
|
|
224 |
|
|
225 |
public static void directJmp(CompilationResultBuilder crb, AMD64MacroAssembler masm, InvokeTarget target) {
|
|
226 |
int before = masm.position();
|
|
227 |
masm.jmp(0, true);
|
|
228 |
int after = masm.position();
|
|
229 |
crb.recordDirectCall(before, after, target, null);
|
|
230 |
masm.ensureUniquePC();
|
|
231 |
}
|
|
232 |
|
|
233 |
public static void directConditionalJmp(CompilationResultBuilder crb, AMD64MacroAssembler masm, InvokeTarget target, ConditionFlag cond) {
|
|
234 |
int before = masm.position();
|
|
235 |
masm.jcc(cond, 0, true);
|
|
236 |
int after = masm.position();
|
|
237 |
crb.recordDirectCall(before, after, target, null);
|
|
238 |
masm.ensureUniquePC();
|
|
239 |
}
|
|
240 |
|
48861
|
241 |
public static int indirectCall(CompilationResultBuilder crb, AMD64MacroAssembler masm, Register dst, InvokeTarget callTarget, LIRFrameState info) {
|
43972
|
242 |
int before = masm.position();
|
|
243 |
masm.call(dst);
|
|
244 |
int after = masm.position();
|
|
245 |
crb.recordIndirectCall(before, after, callTarget, info);
|
|
246 |
crb.recordExceptionHandlers(after, info);
|
|
247 |
masm.ensureUniquePC();
|
48861
|
248 |
return before;
|
43972
|
249 |
}
|
|
250 |
}
|