/// <summary> /// Creates new edge between specified basic blocks and adds it into the basic block incoming list and sets direct edge reference. /// </summary> /// <param name="From">Source basic block.</param> /// <param name="To">Target basic block.</param> /// <returns>New edge</returns> internal static void ConnectDirectEdge(BasicBlock From, BasicBlock To) { var edge = new DirectEdge(From, To); From.SetDefaultBranch(edge); To.AddIncommingEdge(edge); }
/// <summary> /// Connects TrueBranch and FalseBranch to From. TrueBranch is followed from From if the condition holds, /// FalseBranch is followed from From if the condition does not hold. /// /// If decompose is true, it decomposes the condition expression using logical operations with respect to /// shortcircuit evaluation. /// Note that analyzer now expects that the condition expressions are decomposed and it no longer supports /// condition expressions that are not decomposed. /// </summary> /// <param name="condition">the condition of the branching.</param> /// <param name="From">the basic block where from which the branching starts.</param> /// <param name="TrueBranch">the branch which is taken if the condition holds.</param> /// <param name="FalseBranch">the branch which is taken if the condition does not hold.</param> /// <param name="decompose"></param> internal static void ConnectConditionalBranching(Expression condition, BasicBlock From, BasicBlock TrueBranch, BasicBlock FalseBranch, bool decompose = true) { var binaryCondition = condition as BinaryEx; if (!decompose || binaryCondition == null || (binaryCondition.PublicOperation != Operations.And && binaryCondition.PublicOperation != Operations.Or && binaryCondition.PublicOperation != Operations.Xor)) { ConditionalEdge.AddConditionalEdge(From, TrueBranch, condition); DirectEdge.ConnectDirectEdge(From, FalseBranch); return; } BasicBlock intermediateBasicBlock = null; switch (binaryCondition.PublicOperation) { case Operations.And: intermediateBasicBlock = new BasicBlock(); ConnectConditionalBranching(binaryCondition.LeftExpr, From, intermediateBasicBlock, FalseBranch); From = intermediateBasicBlock; ConnectConditionalBranching(binaryCondition.RightExpr, From, TrueBranch, FalseBranch); break; case Operations.Or: intermediateBasicBlock = new BasicBlock(); ConnectConditionalBranching(binaryCondition.LeftExpr, From, TrueBranch, intermediateBasicBlock); From = intermediateBasicBlock; ConnectConditionalBranching(binaryCondition.RightExpr, From, TrueBranch, FalseBranch); break; case Operations.Xor: // Expands A xor B to (A and !B) || (!A and B) // Expansion expands A to A and !A and B to B and !B // For A and !A we the AST elements cannot be shared (must be unique) - the same for B and !B // We thus make copies of ast elements of left and right expression and use the copies to represent !A and !B var leftNegation = new UnaryEx(Operations.LogicNegation, CFGVisitor.DeepCopyAstExpressionCopyVisitor(binaryCondition.LeftExpr)); var rightNegation = new UnaryEx(Operations.LogicNegation, CFGVisitor.DeepCopyAstExpressionCopyVisitor(binaryCondition.RightExpr)); var leftExpression = new BinaryEx(Operations.And, binaryCondition.LeftExpr, rightNegation); var rightExpression = new BinaryEx(Operations.And, leftNegation, binaryCondition.RightExpr); var xorExpression = new BinaryEx(Operations.Or, leftExpression, rightExpression); ConnectConditionalBranching(xorExpression, From, TrueBranch, FalseBranch); /* * // Translation of xor in the level of control flow graph. More efficient than expansion of AST (translation in the level of program code). * // Does not work because AST of sharing AST elements * var intermediateBasicBlock1 = new BasicBlock(); * var intermediateBasicBlock2 = new BasicBlock(); * VisitIfStmtRec(binaryCondition.LeftExpr, intermediateBasicBlock1, intermediateBasicBlock2); * FromBlock = intermediateBasicBlock1; * VisitIfStmtRec(binaryCondition.RightExpr, FalseSink, TrueSink); * FromBlock = intermediateBasicBlock2; * VisitIfStmtRec(binaryCondition.RightExpr, TrueSink, FalseSink);*/ break; } }