Mercurial > hg > graal-compiler
diff graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/FrameSlotTypeSpecializationTest.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 | 5e3d1a68664e |
children | cd1a1d92b3e3 |
line wrap: on
line diff
--- a/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/FrameSlotTypeSpecializationTest.java Tue Apr 23 15:08:11 2013 +0200 +++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/FrameSlotTypeSpecializationTest.java Tue Apr 23 15:34:06 2013 +0200 @@ -34,12 +34,7 @@ * <p> * Dynamically typed languages can speculate on the type of a frame slot and only fall back at run * time to a more generic type if necessary. The new type of a frame slot can be set using the - * {@link FrameSlot#setType(Class)} method. It is the responsibility of the language implementor to - * update the content of currently active frames (using {@link Frame#updateToLatestVersion()}). - * Also, nodes that depend a specific type of a frame slot must be replaced. Such node can register - * a listener that implements {@link FrameSlotTypeListener} using - * {@link FrameSlot#registerOneShotTypeListener(FrameSlotTypeListener)}. The event of a type change - * on the frame slot will fire only once for the next upcoming change. + * {@link FrameSlot#setType(Class)} method. * </p> * * <p> @@ -53,10 +48,10 @@ public void test() { TruffleRuntime runtime = Truffle.getRuntime(); FrameDescriptor frameDescriptor = new FrameDescriptor(); - FrameSlot slot = frameDescriptor.addFrameSlot("localVar", Integer.class); + FrameSlot slot = frameDescriptor.addFrameSlot("localVar", int.class); TestRootNode rootNode = new TestRootNode(new IntAssignLocal(slot, new StringTestChildNode()), new IntReadLocal(slot)); CallTarget target = runtime.createCallTarget(rootNode, frameDescriptor); - Assert.assertEquals(Integer.class, slot.getType()); + Assert.assertEquals(int.class, slot.getType()); Object result = target.call(); Assert.assertEquals("42", result); Assert.assertEquals(Object.class, slot.getType()); @@ -102,35 +97,29 @@ } - class IntAssignLocal extends FrameSlotNode implements FrameSlotTypeListener { + class IntAssignLocal extends FrameSlotNode { @Child private TestChildNode value; IntAssignLocal(FrameSlot slot, TestChildNode value) { super(slot); this.value = adoptChild(value); - slot.registerOneShotTypeListener(this); } @Override Object execute(VirtualFrame frame) { Object o = value.execute(frame); if (o instanceof Integer) { - frame.setInt(slot, (Integer) o); - } else { - slot.setType(Object.class); - frame.updateToLatestVersion(); - frame.setObject(slot, o); + try { + frame.setInt(slot, (Integer) o); + } catch (FrameSlotTypeException e) { + // fall through + } } + FrameUtil.setObjectSafe(frame, slot, o); + this.replace(new ObjectAssignLocal(slot, value)); return null; } - - @Override - public void typeChanged(FrameSlot changedSlot, Class<?> oldType) { - if (changedSlot.getType() == Object.class) { - this.replace(new ObjectAssignLocal(changedSlot, value)); - } - } } class ObjectAssignLocal extends FrameSlotNode { @@ -145,27 +134,27 @@ @Override Object execute(VirtualFrame frame) { Object o = value.execute(frame); - frame.setObject(slot, o); + try { + frame.setObject(slot, o); + } catch (FrameSlotTypeException e) { + FrameUtil.setObjectSafe(frame, slot, o); + } return null; } } - class IntReadLocal extends FrameSlotNode implements FrameSlotTypeListener { + class IntReadLocal extends FrameSlotNode { IntReadLocal(FrameSlot slot) { super(slot); - slot.registerOneShotTypeListener(this); } @Override Object execute(VirtualFrame frame) { - return frame.getInt(slot); - } - - @Override - public void typeChanged(FrameSlot changedSlot, Class<?> oldType) { - if (changedSlot.getType() == Object.class) { - this.replace(new ObjectReadLocal(changedSlot)); + try { + return frame.getInt(slot); + } catch (FrameSlotTypeException e) { + return this.replace(new ObjectReadLocal(slot)).execute(frame); } } } @@ -178,7 +167,11 @@ @Override Object execute(VirtualFrame frame) { - return frame.getObject(slot); + try { + return frame.getObject(slot); + } catch (FrameSlotTypeException e) { + throw new IllegalStateException(e); + } } } }