Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/ConstantNode.java @ 5061:e808627bd16f
Renamed projects.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Thu, 08 Mar 2012 19:24:17 +0100 |
parents | graal/com.oracle.max.graal.nodes/src/com/oracle/graal/nodes/ConstantNode.java@4ed4295ce15f |
children | 5bdaa08ba96b |
comparison
equal
deleted
inserted
replaced
5060:4ed4295ce15f | 5061:e808627bd16f |
---|---|
1 /* | |
2 * Copyright (c) 2009, 2011, 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.graal.nodes; | |
24 | |
25 import com.oracle.max.cri.ci.*; | |
26 import com.oracle.max.cri.ri.*; | |
27 import com.oracle.graal.graph.*; | |
28 import com.oracle.graal.graph.iterators.*; | |
29 import com.oracle.graal.nodes.spi.*; | |
30 import com.oracle.graal.nodes.type.*; | |
31 | |
32 /** | |
33 * The {@code ConstantNode} represents a constant such as an integer value, | |
34 * long, float, object reference, address, etc. | |
35 */ | |
36 @NodeInfo(shortName = "Const") | |
37 public class ConstantNode extends BooleanNode implements LIRLowerable { | |
38 | |
39 @Data public final CiConstant value; | |
40 | |
41 protected ConstantNode(CiConstant value) { | |
42 this(value, null); | |
43 } | |
44 | |
45 /** | |
46 * Constructs a new ConstantNode representing the specified constant. | |
47 * @param value the constant | |
48 */ | |
49 protected ConstantNode(CiConstant value, RiRuntime runtime) { | |
50 super(StampFactory.forConstant(value, runtime)); | |
51 this.value = value; | |
52 } | |
53 | |
54 @Override | |
55 public void generate(LIRGeneratorTool gen) { | |
56 if (gen.canInlineConstant(value) || onlyUsedInFrameState()) { | |
57 gen.setResult(this, value); | |
58 } else { | |
59 gen.setResult(this, gen.emitMove(value)); | |
60 } | |
61 } | |
62 | |
63 private boolean onlyUsedInFrameState() { | |
64 return usages().filter(NodePredicates.isNotA(FrameState.class)).isEmpty(); | |
65 } | |
66 | |
67 public static ConstantNode forCiConstant(CiConstant constant, RiRuntime runtime, Graph graph) { | |
68 return graph.unique(new ConstantNode(constant, runtime)); | |
69 } | |
70 | |
71 /** | |
72 * Returns a node for a double constant. | |
73 * @param d the double value for which to create the instruction | |
74 * @param graph | |
75 * @return a node for a double constant | |
76 */ | |
77 public static ConstantNode forDouble(double d, Graph graph) { | |
78 return graph.unique(new ConstantNode(CiConstant.forDouble(d))); | |
79 } | |
80 | |
81 /** | |
82 * Returns a node for a float constant. | |
83 * @param f the float value for which to create the instruction | |
84 * @param graph | |
85 * @return a node for a float constant | |
86 */ | |
87 public static ConstantNode forFloat(float f, Graph graph) { | |
88 return graph.unique(new ConstantNode(CiConstant.forFloat(f))); | |
89 } | |
90 | |
91 /** | |
92 * Returns a node for an long constant. | |
93 * @param i the long value for which to create the instruction | |
94 * @param graph | |
95 * @return a node for an long constant | |
96 */ | |
97 public static ConstantNode forLong(long i, Graph graph) { | |
98 return graph.unique(new ConstantNode(CiConstant.forLong(i))); | |
99 } | |
100 | |
101 /** | |
102 * Returns a node for an integer constant. | |
103 * @param i the integer value for which to create the instruction | |
104 * @param graph | |
105 * @return a node for an integer constant | |
106 */ | |
107 public static ConstantNode forInt(int i, Graph graph) { | |
108 return graph.unique(new ConstantNode(CiConstant.forInt(i))); | |
109 } | |
110 | |
111 /** | |
112 * Returns a node for a boolean constant. | |
113 * @param i the boolean value for which to create the instruction | |
114 * @param graph | |
115 * @return a node representing the boolean | |
116 */ | |
117 public static ConstantNode forBoolean(boolean i, Graph graph) { | |
118 return graph.unique(new ConstantNode(CiConstant.forBoolean(i))); | |
119 } | |
120 | |
121 /** | |
122 * Returns a node for a byte constant. | |
123 * @param i the byte value for which to create the instruction | |
124 * @param graph | |
125 * @return a node representing the byte | |
126 */ | |
127 public static ConstantNode forByte(byte i, Graph graph) { | |
128 return graph.unique(new ConstantNode(CiConstant.forByte(i))); | |
129 } | |
130 | |
131 /** | |
132 * Returns a node for a char constant. | |
133 * @param i the char value for which to create the instruction | |
134 * @param graph | |
135 * @return a node representing the char | |
136 */ | |
137 public static ConstantNode forChar(char i, Graph graph) { | |
138 return graph.unique(new ConstantNode(CiConstant.forChar(i))); | |
139 } | |
140 | |
141 /** | |
142 * Returns a node for a short constant. | |
143 * @param i the short value for which to create the instruction | |
144 * @param graph | |
145 * @return a node representing the short | |
146 */ | |
147 public static ConstantNode forShort(short i, Graph graph) { | |
148 return graph.unique(new ConstantNode(CiConstant.forShort(i))); | |
149 } | |
150 | |
151 /** | |
152 * Returns a node for an address (jsr/ret address) constant. | |
153 * @param i the address value for which to create the instruction | |
154 * @param graph | |
155 * @return a node representing the address | |
156 */ | |
157 public static ConstantNode forJsr(int i, Graph graph) { | |
158 return graph.unique(new ConstantNode(CiConstant.forJsr(i))); | |
159 } | |
160 | |
161 /** | |
162 * Returns a node for an object constant. | |
163 * @param o the object value for which to create the instruction | |
164 * @param graph | |
165 * @return a node representing the object | |
166 */ | |
167 public static ConstantNode forObject(Object o, RiRuntime runtime, Graph graph) { | |
168 return graph.unique(new ConstantNode(CiConstant.forObject(o), runtime)); | |
169 } | |
170 | |
171 public static ConstantNode forIntegerKind(CiKind kind, long value, Graph graph) { | |
172 switch (kind) { | |
173 case Byte: | |
174 case Short: | |
175 case Int: | |
176 return ConstantNode.forInt((int) value, graph); | |
177 case Long: | |
178 return ConstantNode.forLong(value, graph); | |
179 default: | |
180 throw new InternalError("Should not reach here"); | |
181 } | |
182 } | |
183 | |
184 public static ConstantNode forFloatingKind(CiKind kind, double value, Graph graph) { | |
185 switch (kind) { | |
186 case Float: | |
187 return ConstantNode.forFloat((float) value, graph); | |
188 case Double: | |
189 return ConstantNode.forDouble(value, graph); | |
190 default: | |
191 throw new InternalError("Should not reach here"); | |
192 } | |
193 } | |
194 | |
195 public static ConstantNode defaultForKind(CiKind kind, Graph graph) { | |
196 switch(kind) { | |
197 case Boolean: | |
198 return ConstantNode.forBoolean(false, graph); | |
199 case Byte: | |
200 case Char: | |
201 case Short: | |
202 case Int: | |
203 return ConstantNode.forInt(0, graph); | |
204 case Double: | |
205 return ConstantNode.forDouble(0.0, graph); | |
206 case Float: | |
207 return ConstantNode.forFloat(0.0f, graph); | |
208 case Long: | |
209 return ConstantNode.forLong(0L, graph); | |
210 case Object: | |
211 return ConstantNode.forObject(null, null, graph); | |
212 default: | |
213 return null; | |
214 } | |
215 } | |
216 | |
217 @Override | |
218 public String toString(Verbosity verbosity) { | |
219 if (verbosity == Verbosity.Name) { | |
220 return super.toString(Verbosity.Name) + "(" + value.kind.format(value.boxedValue()) + ")"; | |
221 } else { | |
222 return super.toString(verbosity); | |
223 } | |
224 } | |
225 | |
226 @Override | |
227 public BooleanNode negate() { | |
228 return ConstantNode.forBoolean(!value.asBoolean(), graph()); | |
229 } | |
230 } |