43972
|
1 |
/*
|
52910
|
2 |
* Copyright (c) 2013, 2018, 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 |
|
|
27 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
|
|
28 |
import static jdk.vm.ci.code.ValueUtil.isLegal;
|
|
29 |
|
|
30 |
import java.util.EnumSet;
|
|
31 |
|
|
32 |
import org.graalvm.compiler.asm.amd64.AMD64Address;
|
|
33 |
import org.graalvm.compiler.asm.amd64.AMD64Address.Scale;
|
|
34 |
import org.graalvm.compiler.lir.CompositeValue;
|
|
35 |
import org.graalvm.compiler.lir.InstructionValueConsumer;
|
|
36 |
import org.graalvm.compiler.lir.InstructionValueProcedure;
|
|
37 |
import org.graalvm.compiler.lir.LIRInstruction;
|
|
38 |
import org.graalvm.compiler.lir.LIRInstruction.OperandFlag;
|
|
39 |
import org.graalvm.compiler.lir.LIRInstruction.OperandMode;
|
|
40 |
|
|
41 |
import jdk.vm.ci.code.Register;
|
|
42 |
import jdk.vm.ci.code.RegisterValue;
|
|
43 |
import jdk.vm.ci.meta.AllocatableValue;
|
|
44 |
import jdk.vm.ci.meta.Value;
|
|
45 |
import jdk.vm.ci.meta.ValueKind;
|
|
46 |
|
|
47 |
public final class AMD64AddressValue extends CompositeValue {
|
|
48 |
|
|
49 |
@Component({REG, OperandFlag.ILLEGAL}) protected AllocatableValue base;
|
|
50 |
@Component({REG, OperandFlag.ILLEGAL}) protected AllocatableValue index;
|
|
51 |
protected final Scale scale;
|
|
52 |
protected final int displacement;
|
|
53 |
|
|
54 |
private static final EnumSet<OperandFlag> flags = EnumSet.of(OperandFlag.REG, OperandFlag.ILLEGAL);
|
|
55 |
|
|
56 |
public AMD64AddressValue(ValueKind<?> kind, AllocatableValue base, int displacement) {
|
|
57 |
this(kind, base, Value.ILLEGAL, Scale.Times1, displacement);
|
|
58 |
}
|
|
59 |
|
|
60 |
public AMD64AddressValue(ValueKind<?> kind, AllocatableValue base, AllocatableValue index, Scale scale, int displacement) {
|
|
61 |
super(kind);
|
|
62 |
this.base = base;
|
|
63 |
this.index = index;
|
|
64 |
this.scale = scale;
|
|
65 |
this.displacement = displacement;
|
|
66 |
|
|
67 |
assert scale != null;
|
|
68 |
}
|
|
69 |
|
46344
|
70 |
public AllocatableValue getBase() {
|
|
71 |
return base;
|
|
72 |
}
|
|
73 |
|
|
74 |
public AllocatableValue getIndex() {
|
|
75 |
return index;
|
|
76 |
}
|
|
77 |
|
43972
|
78 |
@Override
|
|
79 |
public CompositeValue forEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueProcedure proc) {
|
|
80 |
AllocatableValue newBase = (AllocatableValue) proc.doValue(inst, base, mode, flags);
|
|
81 |
AllocatableValue newIndex = (AllocatableValue) proc.doValue(inst, index, mode, flags);
|
|
82 |
if (!base.identityEquals(newBase) || !index.identityEquals(newIndex)) {
|
|
83 |
return new AMD64AddressValue(getValueKind(), newBase, newIndex, scale, displacement);
|
|
84 |
}
|
|
85 |
return this;
|
|
86 |
}
|
|
87 |
|
|
88 |
@Override
|
|
89 |
protected void visitEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueConsumer proc) {
|
|
90 |
proc.visitValue(inst, base, mode, flags);
|
|
91 |
proc.visitValue(inst, index, mode, flags);
|
|
92 |
}
|
|
93 |
|
46344
|
94 |
public AMD64AddressValue withKind(ValueKind<?> newKind) {
|
|
95 |
return new AMD64AddressValue(newKind, base, index, scale, displacement);
|
|
96 |
}
|
|
97 |
|
43972
|
98 |
private static Register toRegister(AllocatableValue value) {
|
|
99 |
if (value.equals(Value.ILLEGAL)) {
|
|
100 |
return Register.None;
|
|
101 |
} else {
|
|
102 |
RegisterValue reg = (RegisterValue) value;
|
|
103 |
return reg.getRegister();
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
public AMD64Address toAddress() {
|
|
108 |
return new AMD64Address(toRegister(base), toRegister(index), scale, displacement);
|
|
109 |
}
|
|
110 |
|
|
111 |
@Override
|
|
112 |
public String toString() {
|
|
113 |
StringBuilder s = new StringBuilder("[");
|
|
114 |
String sep = "";
|
|
115 |
if (isLegal(base)) {
|
|
116 |
s.append(base);
|
|
117 |
sep = " + ";
|
|
118 |
}
|
|
119 |
if (isLegal(index)) {
|
|
120 |
s.append(sep).append(index).append(" * ").append(scale.value);
|
|
121 |
sep = " + ";
|
|
122 |
}
|
|
123 |
if (displacement < 0) {
|
|
124 |
s.append(" - ").append(-displacement);
|
|
125 |
} else if (displacement > 0) {
|
|
126 |
s.append(sep).append(displacement);
|
|
127 |
}
|
|
128 |
s.append("]");
|
|
129 |
return s.toString();
|
|
130 |
}
|
|
131 |
|
|
132 |
public boolean isValidImplicitNullCheckFor(Value value, int implicitNullCheckLimit) {
|
|
133 |
return value.equals(base) && index.equals(Value.ILLEGAL) && displacement >= 0 && displacement < implicitNullCheckLimit;
|
|
134 |
}
|
|
135 |
|
|
136 |
@Override
|
|
137 |
public boolean equals(Object obj) {
|
|
138 |
if (obj instanceof AMD64AddressValue) {
|
|
139 |
AMD64AddressValue addr = (AMD64AddressValue) obj;
|
|
140 |
return getValueKind().equals(addr.getValueKind()) && displacement == addr.displacement && base.equals(addr.base) && scale == addr.scale && index.equals(addr.index);
|
|
141 |
}
|
|
142 |
return false;
|
|
143 |
}
|
|
144 |
|
|
145 |
@Override
|
|
146 |
public int hashCode() {
|
|
147 |
return base.hashCode() ^ index.hashCode() ^ (displacement << 4) ^ (scale.value << 8) ^ getValueKind().hashCode();
|
|
148 |
}
|
|
149 |
}
|