package com.oracle.truffle.llvm.runtime.nodes.control;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.NodeField;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.GenerateWrapper;
import com.oracle.truffle.api.instrumentation.ProbeNode;
import com.oracle.truffle.llvm.runtime.nodes.api.LLVMControlFlowNode;
import com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode;
import com.oracle.truffle.llvm.runtime.nodes.api.LLVMStatementNode;
import com.oracle.truffle.llvm.runtime.nodes.control.LLVMConditionalBranchNodeFactory.LLVMConditionalBranchNodeImplNodeGen;
@GenerateWrapper
@NodeField(name = "trueSuccessor", type = int.class)
@NodeField(name = "falseSuccessor", type = int.class)
@NodeChild(value = "condition", type = LLVMExpressionNode.class)
public abstract class LLVMConditionalBranchNode extends LLVMControlFlowNode {
public static LLVMConditionalBranchNode create(int trueSuccessor, int falseSuccessor, LLVMStatementNode truePhi, LLVMStatementNode falsePhi, LLVMExpressionNode condition) {
return LLVMConditionalBranchNodeImplNodeGen.create(truePhi, falsePhi, condition, trueSuccessor, falseSuccessor);
}
public static final int TRUE_SUCCESSOR = 0;
public static final int FALSE_SUCCESSOR = 1;
@Override
public WrapperNode createWrapper(ProbeNode probe) {
return new LLVMConditionalBranchNodeWrapper(this, probe);
}
public abstract boolean executeCondition(VirtualFrame frame);
public abstract int getTrueSuccessor();
public abstract int getFalseSuccessor();
abstract static class LLVMConditionalBranchNodeImpl extends LLVMConditionalBranchNode {
@Child private LLVMStatementNode truePhi;
@Child private LLVMStatementNode falsePhi;
LLVMConditionalBranchNodeImpl(LLVMStatementNode truePhi, LLVMStatementNode falsePhi) {
this.truePhi = truePhi;
this.falsePhi = falsePhi;
}
@Override
public String toString() {
return getShortString("trueSuccessor", "falseSuccessor");
}
@Override
public int getSuccessorCount() {
return 2;
}
@Override
public LLVMStatementNode getPhiNode(int successorIndex) {
CompilerAsserts.partialEvaluationConstant(successorIndex);
if (successorIndex == TRUE_SUCCESSOR) {
return truePhi;
} else {
assert successorIndex == FALSE_SUCCESSOR;
return falsePhi;
}
}
@Specialization
public boolean doCondition(boolean condition) {
return condition;
}
@Override
public int[] getSuccessors() {
return new int[]{getTrueSuccessor(), getFalseSuccessor()};
}
}
}