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