comparison graal/com.oracle.jvmci.asm.amd64/src/com/oracle/jvmci/asm/amd64/AMD64Address.java @ 21708:6df25b1418be

moved com.oracle.asm.** to jvmci-util.jar (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Wed, 03 Jun 2015 18:06:44 +0200
parents graal/com.oracle.graal.asm.amd64/src/com/oracle/graal/asm/amd64/AMD64Address.java@48c1ebd24120
children
comparison
equal deleted inserted replaced
21707:e0f311284930 21708:6df25b1418be
1 /*
2 * Copyright (c) 2010, 2013, 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 */
23 package com.oracle.jvmci.asm.amd64;
24
25 import com.oracle.jvmci.code.Register;
26 import com.oracle.jvmci.code.AbstractAddress;
27
28 /**
29 * Represents an address in target machine memory, specified via some combination of a base
30 * register, an index register, a displacement and a scale. Note that the base and index registers
31 * may be a variable that will get a register assigned later by the register allocator.
32 */
33 public final class AMD64Address extends AbstractAddress {
34
35 private final Register base;
36 private final Register index;
37 private final Scale scale;
38 private final int displacement;
39
40 /**
41 * Creates an {@link AMD64Address} with given base register, no scaling and no displacement.
42 *
43 * @param base the base register
44 */
45 public AMD64Address(Register base) {
46 this(base, Register.None, Scale.Times1, 0);
47 }
48
49 /**
50 * Creates an {@link AMD64Address} with given base register, no scaling and a given
51 * displacement.
52 *
53 * @param base the base register
54 * @param displacement the displacement
55 */
56 public AMD64Address(Register base, int displacement) {
57 this(base, Register.None, Scale.Times1, displacement);
58 }
59
60 /**
61 * Creates an {@link AMD64Address} with given base and index registers, scaling and
62 * displacement. This is the most general constructor.
63 *
64 * @param base the base register
65 * @param index the index register
66 * @param scale the scaling factor
67 * @param displacement the displacement
68 */
69 public AMD64Address(Register base, Register index, Scale scale, int displacement) {
70 this.base = base;
71 this.index = index;
72 this.scale = scale;
73 this.displacement = displacement;
74
75 assert scale != null;
76 }
77
78 /**
79 * A scaling factor used in the SIB addressing mode.
80 */
81 public enum Scale {
82 Times1(1, 0),
83 Times2(2, 1),
84 Times4(4, 2),
85 Times8(8, 3);
86
87 private Scale(int value, int log2) {
88 this.value = value;
89 this.log2 = log2;
90 }
91
92 /**
93 * The value (or multiplier) of this scale.
94 */
95 public final int value;
96
97 /**
98 * The {@linkplain #value value} of this scale log 2.
99 */
100 public final int log2;
101
102 public static Scale fromInt(int scale) {
103 switch (scale) {
104 case 1:
105 return Times1;
106 case 2:
107 return Times2;
108 case 4:
109 return Times4;
110 case 8:
111 return Times8;
112 default:
113 return null;
114 }
115 }
116 }
117
118 @Override
119 public String toString() {
120 StringBuilder s = new StringBuilder();
121 s.append("[");
122 String sep = "";
123 if (!getBase().equals(Register.None)) {
124 s.append(getBase());
125 sep = " + ";
126 }
127 if (!getIndex().equals(Register.None)) {
128 s.append(sep).append(getIndex()).append(" * ").append(getScale().value);
129 sep = " + ";
130 }
131 if (getDisplacement() < 0) {
132 s.append(" - ").append(-getDisplacement());
133 } else if (getDisplacement() > 0) {
134 s.append(sep).append(getDisplacement());
135 }
136 s.append("]");
137 return s.toString();
138 }
139
140 /**
141 * @return Base register that defines the start of the address computation. If not present, is
142 * denoted by {@link Register#None}.
143 */
144 public Register getBase() {
145 return base;
146 }
147
148 /**
149 * @return Index register, the value of which (possibly scaled by {@link #getScale}) is added to
150 * {@link #getBase}. If not present, is denoted by {@link Register#None}.
151 */
152 public Register getIndex() {
153 return index;
154 }
155
156 /**
157 * @return Scaling factor for indexing, dependent on target operand size.
158 */
159 public Scale getScale() {
160 return scale;
161 }
162
163 /**
164 * @return Optional additive displacement.
165 */
166 public int getDisplacement() {
167 return displacement;
168 }
169 }