Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ReturnTypeSpecializationTest.java @ 9258:07f8d136a05e
Truffle API changes for the Frame API. Introduction of Assumptions class.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Tue, 23 Apr 2013 15:34:06 +0200 |
parents | 0ac3e388445f |
children | cd1a1d92b3e3 |
comparison
equal
deleted
inserted
replaced
9257:542712a4732a | 9258:07f8d136a05e |
---|---|
45 | 45 |
46 @Test | 46 @Test |
47 public void test() { | 47 public void test() { |
48 TruffleRuntime runtime = Truffle.getRuntime(); | 48 TruffleRuntime runtime = Truffle.getRuntime(); |
49 FrameDescriptor frameDescriptor = new FrameDescriptor(); | 49 FrameDescriptor frameDescriptor = new FrameDescriptor(); |
50 FrameSlot slot = frameDescriptor.addFrameSlot("localVar", Integer.class); | 50 FrameSlot slot = frameDescriptor.addFrameSlot("localVar", int.class); |
51 TestRootNode rootNode = new TestRootNode(new IntAssignLocal(slot, new StringTestChildNode()), new IntReadLocal(slot)); | 51 TestRootNode rootNode = new TestRootNode(new IntAssignLocal(slot, new StringTestChildNode()), new IntReadLocal(slot)); |
52 CallTarget target = runtime.createCallTarget(rootNode, frameDescriptor); | 52 CallTarget target = runtime.createCallTarget(rootNode, frameDescriptor); |
53 Assert.assertEquals(Integer.class, slot.getType()); | 53 Assert.assertEquals(int.class, slot.getType()); |
54 Object result = target.call(); | 54 Object result = target.call(); |
55 Assert.assertEquals("42", result); | 55 Assert.assertEquals("42", result); |
56 Assert.assertEquals(Object.class, slot.getType()); | 56 Assert.assertEquals(Object.class, slot.getType()); |
57 } | 57 } |
58 | 58 |
102 return "42"; | 102 return "42"; |
103 } | 103 } |
104 | 104 |
105 } | 105 } |
106 | 106 |
107 class IntAssignLocal extends FrameSlotNode implements FrameSlotTypeListener { | 107 class IntAssignLocal extends FrameSlotNode { |
108 | 108 |
109 @Child private TestChildNode value; | 109 @Child private TestChildNode value; |
110 | 110 |
111 IntAssignLocal(FrameSlot slot, TestChildNode value) { | 111 IntAssignLocal(FrameSlot slot, TestChildNode value) { |
112 super(slot); | 112 super(slot); |
113 this.value = adoptChild(value); | 113 this.value = adoptChild(value); |
114 slot.registerOneShotTypeListener(this); | |
115 } | 114 } |
116 | 115 |
117 @Override | 116 @Override |
118 Object execute(VirtualFrame frame) { | 117 Object execute(VirtualFrame frame) { |
119 try { | 118 try { |
120 frame.setInt(slot, value.executeInt(frame)); | 119 int result = value.executeInt(frame); |
120 try { | |
121 frame.setInt(slot, result); | |
122 } catch (FrameSlotTypeException e) { | |
123 FrameUtil.setObjectSafe(frame, slot, result); | |
124 replace(new ObjectAssignLocal(slot, value)); | |
125 } | |
121 } catch (UnexpectedResultException e) { | 126 } catch (UnexpectedResultException e) { |
122 slot.setType(Object.class); | 127 FrameUtil.setObjectSafe(frame, slot, e.getResult()); |
123 frame.updateToLatestVersion(); | 128 replace(new ObjectAssignLocal(slot, value)); |
124 frame.setObject(slot, e.getResult()); | |
125 } | 129 } |
126 return null; | 130 return null; |
127 } | |
128 | |
129 @Override | |
130 public void typeChanged(FrameSlot changedSlot, Class<?> oldType) { | |
131 if (changedSlot.getType() == Object.class) { | |
132 this.replace(new ObjectAssignLocal(changedSlot, value)); | |
133 } | |
134 } | 131 } |
135 } | 132 } |
136 | 133 |
137 class ObjectAssignLocal extends FrameSlotNode { | 134 class ObjectAssignLocal extends FrameSlotNode { |
138 | 135 |
144 } | 141 } |
145 | 142 |
146 @Override | 143 @Override |
147 Object execute(VirtualFrame frame) { | 144 Object execute(VirtualFrame frame) { |
148 Object o = value.execute(frame); | 145 Object o = value.execute(frame); |
149 frame.setObject(slot, o); | 146 try { |
147 frame.setObject(slot, o); | |
148 } catch (FrameSlotTypeException e) { | |
149 FrameUtil.setObjectSafe(frame, slot, o); | |
150 } | |
150 return null; | 151 return null; |
151 } | 152 } |
152 } | 153 } |
153 | 154 |
154 class IntReadLocal extends FrameSlotNode implements FrameSlotTypeListener { | 155 class IntReadLocal extends FrameSlotNode { |
155 | 156 |
156 IntReadLocal(FrameSlot slot) { | 157 IntReadLocal(FrameSlot slot) { |
157 super(slot); | 158 super(slot); |
158 slot.registerOneShotTypeListener(this); | |
159 } | 159 } |
160 | 160 |
161 @Override | 161 @Override |
162 Object execute(VirtualFrame frame) { | 162 Object execute(VirtualFrame frame) { |
163 return executeInt(frame); | 163 try { |
164 return frame.getInt(slot); | |
165 } catch (FrameSlotTypeException e) { | |
166 return replace(new ObjectReadLocal(slot)).execute(frame); | |
167 } | |
164 } | 168 } |
165 | 169 |
166 @Override | 170 @Override |
167 int executeInt(VirtualFrame frame) { | 171 int executeInt(VirtualFrame frame) throws UnexpectedResultException { |
168 return frame.getInt(slot); | 172 try { |
169 } | 173 return frame.getInt(slot); |
170 | 174 } catch (FrameSlotTypeException e) { |
171 @Override | 175 return replace(new ObjectReadLocal(slot)).executeInt(frame); |
172 public void typeChanged(FrameSlot changedSlot, Class<?> oldType) { | |
173 if (changedSlot.getType() == Object.class) { | |
174 this.replace(new ObjectReadLocal(changedSlot)); | |
175 } | 176 } |
176 } | 177 } |
177 } | 178 } |
178 | 179 |
179 class ObjectReadLocal extends FrameSlotNode { | 180 class ObjectReadLocal extends FrameSlotNode { |
182 super(slot); | 183 super(slot); |
183 } | 184 } |
184 | 185 |
185 @Override | 186 @Override |
186 Object execute(VirtualFrame frame) { | 187 Object execute(VirtualFrame frame) { |
187 return frame.getObject(slot); | 188 try { |
189 return frame.getObject(slot); | |
190 } catch (FrameSlotTypeException e) { | |
191 throw new IllegalStateException(e); | |
192 } | |
188 } | 193 } |
189 } | 194 } |
190 } | 195 } |