|
| 1 | +package trufflesom.interpreter.supernodes; |
| 2 | + |
| 3 | +import com.oracle.truffle.api.CompilerDirectives; |
| 4 | +import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; |
| 5 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 6 | +import com.oracle.truffle.api.nodes.Node; |
| 7 | +import com.oracle.truffle.api.nodes.UnexpectedResultException; |
| 8 | + |
| 9 | +import trufflesom.bdt.inlining.ScopeAdaptationVisitor; |
| 10 | +import trufflesom.bdt.inlining.ScopeAdaptationVisitor.ScopeElement; |
| 11 | +import trufflesom.compiler.Variable.Argument; |
| 12 | +import trufflesom.interpreter.bc.RespecializeException; |
| 13 | +import trufflesom.interpreter.nodes.ArgumentReadNode.LocalArgumentReadNode; |
| 14 | +import trufflesom.interpreter.nodes.ExpressionNode; |
| 15 | +import trufflesom.interpreter.nodes.FieldNode.FieldReadNode; |
| 16 | +import trufflesom.interpreter.nodes.GenericMessageSendNode; |
| 17 | +import trufflesom.interpreter.nodes.MessageSendNode; |
| 18 | +import trufflesom.interpreter.nodes.bc.BytecodeLoopNode; |
| 19 | +import trufflesom.interpreter.nodes.literals.GenericLiteralNode; |
| 20 | +import trufflesom.interpreter.objectstorage.FieldAccessorNode; |
| 21 | +import trufflesom.interpreter.objectstorage.FieldAccessorNode.AbstractReadFieldNode; |
| 22 | +import trufflesom.interpreter.objectstorage.ObjectLayout; |
| 23 | +import trufflesom.interpreter.objectstorage.StorageLocation; |
| 24 | +import trufflesom.vm.SymbolTable; |
| 25 | +import trufflesom.vm.VmSettings; |
| 26 | +import trufflesom.vm.constants.Nil; |
| 27 | +import trufflesom.vmobjects.SObject; |
| 28 | + |
| 29 | + |
| 30 | +public final class LocalFieldStringEqualsNode extends ExpressionNode { |
| 31 | + |
| 32 | + private final int fieldIdx; |
| 33 | + private final String value; |
| 34 | + protected final Argument arg; |
| 35 | + |
| 36 | + @Child private AbstractReadFieldNode readFieldNode; |
| 37 | + |
| 38 | + @CompilationFinal private int state; |
| 39 | + |
| 40 | + public LocalFieldStringEqualsNode(final int fieldIdx, final Argument arg, |
| 41 | + final String value) { |
| 42 | + this.fieldIdx = fieldIdx; |
| 43 | + this.arg = arg; |
| 44 | + this.value = value; |
| 45 | + |
| 46 | + this.state = 0; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Object executeGeneric(final VirtualFrame frame) { |
| 51 | + try { |
| 52 | + SObject rcvr = (SObject) frame.getArguments()[0]; |
| 53 | + return executeEvaluated(frame, rcvr); |
| 54 | + } catch (UnexpectedResultException e) { |
| 55 | + return e.getResult(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Object doPreEvaluated(final VirtualFrame frame, final Object[] args) { |
| 61 | + try { |
| 62 | + return executeEvaluated(frame, (SObject) args[0]); |
| 63 | + } catch (UnexpectedResultException e) { |
| 64 | + return e.getResult(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public boolean executeEvaluated(final VirtualFrame frame, final SObject rcvr) |
| 69 | + throws UnexpectedResultException { |
| 70 | + int currentState = state; |
| 71 | + |
| 72 | + if (state == 0) { |
| 73 | + // uninitialized |
| 74 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 75 | + final ObjectLayout layout = rcvr.getObjectLayout(); |
| 76 | + StorageLocation location = layout.getStorageLocation(fieldIdx); |
| 77 | + |
| 78 | + readFieldNode = |
| 79 | + insert(location.getReadNode(fieldIdx, layout, |
| 80 | + FieldAccessorNode.createRead(fieldIdx))); |
| 81 | + } |
| 82 | + |
| 83 | + Object result = readFieldNode.read(rcvr); |
| 84 | + |
| 85 | + if ((state & 0b1) != 0) { |
| 86 | + // we saw a string before |
| 87 | + if (result instanceof String) { |
| 88 | + return ((String) result).equals(value); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if ((state & 0b10) != 0) { |
| 93 | + // we saw a nil before |
| 94 | + if (result == Nil.nilObject) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 100 | + return specialize(frame, result, currentState); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public boolean executeBoolean(final VirtualFrame frame) throws UnexpectedResultException { |
| 105 | + SObject rcvr = (SObject) frame.getArguments()[0]; |
| 106 | + |
| 107 | + return executeEvaluated(frame, rcvr); |
| 108 | + } |
| 109 | + |
| 110 | + private boolean specialize(final VirtualFrame frame, final Object result, |
| 111 | + final int currentState) throws UnexpectedResultException { |
| 112 | + if (result instanceof String) { |
| 113 | + state = currentState | 0b1; |
| 114 | + return value.equals(result); |
| 115 | + } |
| 116 | + |
| 117 | + if (result == Nil.nilObject) { |
| 118 | + state = currentState | 0b10; |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + Object sendResult = |
| 123 | + makeGenericSend(result).doPreEvaluated(frame, new Object[] {result, value}); |
| 124 | + if (sendResult instanceof Boolean) { |
| 125 | + return (Boolean) sendResult; |
| 126 | + } |
| 127 | + throw new UnexpectedResultException(sendResult); |
| 128 | + } |
| 129 | + |
| 130 | + public GenericMessageSendNode makeGenericSend( |
| 131 | + @SuppressWarnings("unused") final Object receiver) { |
| 132 | + GenericMessageSendNode send = |
| 133 | + MessageSendNode.createGeneric(SymbolTable.symbolFor("="), |
| 134 | + new ExpressionNode[] {new FieldReadNode(new LocalArgumentReadNode(arg), fieldIdx), |
| 135 | + new GenericLiteralNode(value)}, |
| 136 | + sourceCoord); |
| 137 | + |
| 138 | + if (VmSettings.UseAstInterp) { |
| 139 | + replace(send); |
| 140 | + send.notifyDispatchInserted(); |
| 141 | + return send; |
| 142 | + } |
| 143 | + |
| 144 | + assert getParent() instanceof BytecodeLoopNode : "This node was expected to be a direct child of a `BytecodeLoopNode`."; |
| 145 | + throw new RespecializeException(send); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void replaceAfterScopeChange(final ScopeAdaptationVisitor inliner) { |
| 150 | + ScopeElement<? extends Node> se = inliner.getAdaptedVar(arg); |
| 151 | + if (se.var != arg || se.contextLevel < 0) { |
| 152 | + Node newNode; |
| 153 | + if (se.contextLevel == 0) { |
| 154 | + newNode = |
| 155 | + new LocalFieldStringEqualsNode(fieldIdx, (Argument) se.var, value).initialize( |
| 156 | + fieldIdx); |
| 157 | + } else { |
| 158 | + newNode = new NonLocalFieldStringEqualsNode(fieldIdx, (Argument) se.var, |
| 159 | + se.contextLevel, value).initialize(fieldIdx); |
| 160 | + } |
| 161 | + |
| 162 | + replace(newNode); |
| 163 | + } else { |
| 164 | + assert 0 == se.contextLevel; |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments