package com.oracle.truffle.js.nodes.control;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.InstrumentableNode;
import com.oracle.truffle.api.instrumentation.Tag;
import com.oracle.truffle.api.nodes.NodeInfo;
import com.oracle.truffle.api.nodes.UnexpectedResultException;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.js.nodes.JSNodeUtil;
import com.oracle.truffle.js.nodes.JavaScriptNode;
import com.oracle.truffle.js.nodes.access.JSConstantNode;
import com.oracle.truffle.js.nodes.cast.JSToBooleanNode;
import com.oracle.truffle.js.nodes.instrumentation.JSTaggedExecutionNode;
import com.oracle.truffle.js.nodes.instrumentation.JSTags;
import com.oracle.truffle.js.nodes.instrumentation.JSTags.ControlFlowBlockTag;
import com.oracle.truffle.js.nodes.instrumentation.JSTags.ControlFlowBranchTag;
import com.oracle.truffle.js.nodes.instrumentation.JSTags.ControlFlowRootTag;
import com.oracle.truffle.js.nodes.unary.JSNotNode;
import com.oracle.truffle.js.nodes.unary.JSUnaryNode;
import com.oracle.truffle.js.runtime.objects.Undefined;
@NodeInfo(shortName = "if")
public final class IfNode extends StatementNode implements ResumableNode {
@Child private JavaScriptNode condition;
@Child private JavaScriptNode thenPart;
@Child private JavaScriptNode elsePart;
private final ConditionProfile conditionProfile = ConditionProfile.createCountingProfile();
public static IfNode create(JavaScriptNode condition, JavaScriptNode thenPart, JavaScriptNode elsePart) {
if (condition instanceof JSNotNode) {
JavaScriptNode operand = ((JSNotNode) condition).getOperand();
transferSourceSectionAddExpressionTag(condition, operand);
return new IfNode(operand, elsePart, thenPart);
}
return new IfNode(condition, thenPart, elsePart);
}
@Override
public boolean hasTag(Class<? extends Tag> tag) {
if (tag == ControlFlowRootTag.class) {
return true;
}
return super.hasTag(tag);
}
@Override
public Object getNodeObject() {
return JSTags.createNodeObjectDescriptor("type", ControlFlowRootTag.Type.Conditional.name());
}
@Override
public InstrumentableNode materializeInstrumentableNodes(Set<Class<? extends Tag>> materializedTags) {
if (hasMaterializationTag(materializedTags) && materializationNeeded()) {
JavaScriptNode newCondition = JSTaggedExecutionNode.createForInput(condition, ControlFlowBranchTag.class,
JSTags.createNodeObjectDescriptor("type", ControlFlowBranchTag.Type.Condition.name()), materializedTags);
JavaScriptNode newThenPart = thenPart != null ? JSTaggedExecutionNode.createForInput(thenPart, JSTags.ControlFlowBlockTag.class, materializedTags) : null;
JavaScriptNode newElsePart = elsePart != null ? JSTaggedExecutionNode.createForInput(elsePart, JSTags.ControlFlowBlockTag.class, materializedTags) : null;
if (newCondition == condition && newThenPart == thenPart && newElsePart == elsePart) {
return this;
}
if (newCondition == condition) {
newCondition = cloneUninitialized(condition, materializedTags);
}
if (newThenPart == thenPart) {
newThenPart = cloneUninitialized(thenPart, materializedTags);
}
if (newElsePart == elsePart) {
newElsePart = cloneUninitialized(elsePart, materializedTags);
}
JavaScriptNode newIf = IfNode.create(newCondition, newThenPart, newElsePart);
transferSourceSectionAndTags(this, newIf);
return newIf;
} else {
return this;
}
}
private boolean materializationNeeded() {
return !(JSNodeUtil.isTaggedNode(condition) && (elsePart == null || JSNodeUtil.isTaggedNode(elsePart)) && (thenPart == null || JSNodeUtil.isTaggedNode(thenPart)));
}
private static boolean hasMaterializationTag(Set<Class<? extends Tag>> materializedTags) {
return materializedTags.contains(ControlFlowRootTag.class) || materializedTags.contains(ControlFlowBranchTag.class) ||
materializedTags.contains(ControlFlowBlockTag.class);
}
private IfNode(JavaScriptNode condition, JavaScriptNode thenPart, JavaScriptNode elsePart) {
this.condition = condition;
this.thenPart = thenPart;
this.elsePart = elsePart;
}
public JavaScriptNode getThenPart() {
return thenPart;
}
public JavaScriptNode getElsePart() {
return elsePart;
}
public JavaScriptNode getCondition() {
return condition;
}
@Override
public Object execute(VirtualFrame frame) {
if (conditionProfile.profile(executeCondition(frame))) {
if (thenPart != null) {
return thenPart.execute(frame);
} else {
return EMPTY;
}
} else {
if (elsePart != null) {
return elsePart.execute(frame);
} else {
return EMPTY;
}
}
}
@Override
public void executeVoid(VirtualFrame frame) {
if (conditionProfile.profile(executeCondition(frame))) {
if (thenPart != null) {
thenPart.executeVoid(frame);
}
} else {
if (elsePart != null) {
elsePart.executeVoid(frame);
}
}
}
@Override
public Object resume(VirtualFrame frame) {
int index = getStateAsIntAndReset(frame);
if (index == 0 && conditionProfile.profile(executeCondition(frame)) || index == 1) {
try {
if (thenPart != null) {
return thenPart.execute(frame);
} else {
return EMPTY;
}
} catch (YieldException e) {
setState(frame, 1);
throw e;
}
} else {
assert index == 0 || index == 2;
try {
if (elsePart != null) {
return elsePart.execute(frame);
} else {
return EMPTY;
}
} catch (YieldException e) {
setState(frame, 2);
throw e;
}
}
}
@Override
public boolean isResultAlwaysOfType(Class<?> clazz) {
return isResultAlwaysOfType(thenPart, clazz) && isResultAlwaysOfType(elsePart, clazz);
}
private static boolean isResultAlwaysOfType(JavaScriptNode child, Class<?> clazz) {
if (child == null) {
return clazz == Undefined.class;
} else {
return child.isResultAlwaysOfType(clazz);
}
}
@Override
protected JavaScriptNode copyUninitialized(Set<Class<? extends Tag>> materializedTags) {
return new IfNode(cloneUninitialized(condition, materializedTags), cloneUninitialized(thenPart, materializedTags), cloneUninitialized(elsePart, materializedTags));
}
protected boolean executeCondition(VirtualFrame frame) {
try {
return condition.executeBoolean(frame);
} catch (UnexpectedResultException ex) {
CompilerDirectives.transferToInterpreterAndInvalidate();
JavaScriptNode node = insertToBoolean();
if (node instanceof JSConstantNode) {
try {
return node.executeBoolean(frame);
} catch (UnexpectedResultException e) {
throw CompilerDirectives.shouldNotReachHere(e);
}
} else if (node instanceof JSUnaryNode) {
return (boolean) ((JSUnaryNode) node).execute(frame, ex.getResult());
} else {
throw CompilerDirectives.shouldNotReachHere("Unexpected result node of JSToBooleanNode.create");
}
}
}
private JavaScriptNode insertToBoolean() {
CompilerAsserts.neverPartOfCompilation();
Lock lock = getLock();
lock.lock();
JavaScriptNode cond = condition;
try {
if (!(cond instanceof JSToBooleanNode)) {
condition = cond = insert(JSToBooleanNode.create(cond));
}
} finally {
lock.unlock();
}
return cond;
}
}