comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ReturnTypeSpecializationTest.java @ 7530:5e3d1a68664e

applied mx eclipseformat to all Java files
author Doug Simon <doug.simon@oracle.com>
date Wed, 23 Jan 2013 16:34:57 +0100
parents a4b84ba6dc2e
children 0ac3e388445f
comparison
equal deleted inserted replaced
7529:4a11124a3563 7530:5e3d1a68664e
28 import com.oracle.truffle.api.frame.*; 28 import com.oracle.truffle.api.frame.*;
29 import com.oracle.truffle.api.nodes.*; 29 import com.oracle.truffle.api.nodes.*;
30 30
31 /** 31 /**
32 * <h3>Specializing Return Types</h3> 32 * <h3>Specializing Return Types</h3>
33 * 33 *
34 * <p> 34 * <p>
35 * In order to avoid boxing and/or type casts on the return value of a node, the return value the method for executing a 35 * In order to avoid boxing and/or type casts on the return value of a node, the return value the
36 * node can have a specific type and need not be of type {@link java.lang.Object}. For dynamically typed languages, this 36 * method for executing a node can have a specific type and need not be of type
37 * return type is something that should be speculated on. When the speculation fails and the child node cannot return 37 * {@link java.lang.Object}. For dynamically typed languages, this return type is something that
38 * the appropriate type of value, it can use an {@link UnexpectedResultException} to still pass the result to the 38 * should be speculated on. When the speculation fails and the child node cannot return the
39 * caller. In such a case, the caller must rewrite itself to a more general version in oder to avoid future failures of 39 * appropriate type of value, it can use an {@link UnexpectedResultException} to still pass the
40 * this kind. 40 * result to the caller. In such a case, the caller must rewrite itself to a more general version in
41 * oder to avoid future failures of this kind.
41 * </p> 42 * </p>
42 */ 43 */
43 public class ReturnTypeSpecializationTest { 44 public class ReturnTypeSpecializationTest {
44 45
45 @Test 46 @Test
71 return right.execute(frame); 72 return right.execute(frame);
72 } 73 }
73 } 74 }
74 75
75 abstract class TestChildNode extends Node { 76 abstract class TestChildNode extends Node {
77
76 abstract Object execute(VirtualFrame frame); 78 abstract Object execute(VirtualFrame frame);
77 79
78 int executeInt(VirtualFrame frame) throws UnexpectedResultException { 80 int executeInt(VirtualFrame frame) throws UnexpectedResultException {
79 Object result = execute(frame); 81 Object result = execute(frame);
80 if (result instanceof Integer) { 82 if (result instanceof Integer) {
83 throw new UnexpectedResultException(result); 85 throw new UnexpectedResultException(result);
84 } 86 }
85 } 87 }
86 88
87 abstract class FrameSlotNode extends TestChildNode { 89 abstract class FrameSlotNode extends TestChildNode {
90
88 protected final FrameSlot slot; 91 protected final FrameSlot slot;
89 92
90 public FrameSlotNode(FrameSlot slot) { 93 public FrameSlotNode(FrameSlot slot) {
91 this.slot = slot; 94 this.slot = slot;
92 } 95 }
100 } 103 }
101 104
102 } 105 }
103 106
104 class IntAssignLocal extends FrameSlotNode implements FrameSlotTypeListener { 107 class IntAssignLocal extends FrameSlotNode implements FrameSlotTypeListener {
108
105 @Child private TestChildNode value; 109 @Child private TestChildNode value;
106 110
107 IntAssignLocal(FrameSlot slot, TestChildNode value) { 111 IntAssignLocal(FrameSlot slot, TestChildNode value) {
108 super(slot); 112 super(slot);
109 this.value = adoptChild(value); 113 this.value = adoptChild(value);
121 } 125 }
122 return null; 126 return null;
123 } 127 }
124 128
125 @Override 129 @Override
126 public void typeChanged(FrameSlot changedSlot, Class< ? > oldType) { 130 public void typeChanged(FrameSlot changedSlot, Class<?> oldType) {
127 if (changedSlot.getType() == Object.class) { 131 if (changedSlot.getType() == Object.class) {
128 this.replace(new ObjectAssignLocal(changedSlot, value)); 132 this.replace(new ObjectAssignLocal(changedSlot, value));
129 } 133 }
130 } 134 }
131 } 135 }
132 136
133 class ObjectAssignLocal extends FrameSlotNode { 137 class ObjectAssignLocal extends FrameSlotNode {
138
134 @Child private TestChildNode value; 139 @Child private TestChildNode value;
135 140
136 ObjectAssignLocal(FrameSlot slot, TestChildNode value) { 141 ObjectAssignLocal(FrameSlot slot, TestChildNode value) {
137 super(slot); 142 super(slot);
138 this.value = adoptChild(value); 143 this.value = adoptChild(value);
145 return null; 150 return null;
146 } 151 }
147 } 152 }
148 153
149 class IntReadLocal extends FrameSlotNode implements FrameSlotTypeListener { 154 class IntReadLocal extends FrameSlotNode implements FrameSlotTypeListener {
155
150 IntReadLocal(FrameSlot slot) { 156 IntReadLocal(FrameSlot slot) {
151 super(slot); 157 super(slot);
152 slot.registerOneShotTypeListener(this); 158 slot.registerOneShotTypeListener(this);
153 } 159 }
154 160
161 int executeInt(VirtualFrame frame) { 167 int executeInt(VirtualFrame frame) {
162 return frame.getInt(slot); 168 return frame.getInt(slot);
163 } 169 }
164 170
165 @Override 171 @Override
166 public void typeChanged(FrameSlot changedSlot, Class< ? > oldType) { 172 public void typeChanged(FrameSlot changedSlot, Class<?> oldType) {
167 if (changedSlot.getType() == Object.class) { 173 if (changedSlot.getType() == Object.class) {
168 this.replace(new ObjectReadLocal(changedSlot)); 174 this.replace(new ObjectReadLocal(changedSlot));
169 } 175 }
170 } 176 }
171 } 177 }
172 178
173 class ObjectReadLocal extends FrameSlotNode { 179 class ObjectReadLocal extends FrameSlotNode {
180
174 ObjectReadLocal(FrameSlot slot) { 181 ObjectReadLocal(FrameSlot slot) {
175 super(slot); 182 super(slot);
176 } 183 }
177 184
178 @Override 185 @Override
179 Object execute(VirtualFrame frame) { 186 Object execute(VirtualFrame frame) {
180 return frame.getObject(slot); 187 return frame.getObject(slot);
181 } 188 }
182 } 189 }
183 } 190 }
184