/// <summary> /// Gets a string representation of the instruction block offset that this CFG block logical construct node represents. /// </summary> /// <remarks> /// If the <paramref name="node"/> is partial CFG block logical construct then the string representing its offset is: /// "{offset of the instruction block}_{partial construct index}" /// Where the partial construct index is the index of the partial construct in the <paramref name="context"/>.CFGBlockToLogicalConstructMap. /// </remarks> /// <param name="context"></param> /// <param name="node"></param> /// <returns></returns> protected string NodeILOffset(LogicalFlowBuilderContext context, CFGBlockLogicalConstruct node) { if (node == null) { return("null"); } //If the CFG construct is a partial block, the offset is: "blockOffset_indexOfPartialBlock" PartialCFGBlockLogicalConstruct partialNode = node as PartialCFGBlockLogicalConstruct; if (partialNode != null) { int indexOfPartialConstruct = Array.IndexOf(context.CFGBlockToLogicalConstructMap[partialNode.TheBlock], partialNode); if (indexOfPartialConstruct == -1) { //sanity check throw new Exception("Invalid partial block data."); } return(string.Format("IL_{0}_{1}", partialNode.TheBlock.First.Offset.ToString("x4"), indexOfPartialConstruct)); } else { return(string.Format("IL_{0}", node.TheBlock.First.Offset.ToString("x4"))); } }
public static KeyValuePair <CFGBlockLogicalConstruct, CFGBlockLogicalConstruct> SplitCFGBlockAt(LogicalFlowBuilderContext logicalContext, CFGBlockLogicalConstruct cfgBlock, int expressionIndex) { V_0 = cfgBlock.get_LogicalConstructExpressions(); if (V_0 == null) { throw new ArgumentNullException("blockExpressions"); } if (expressionIndex <= 0 || expressionIndex >= V_0.get_Count()) { throw new ArgumentOutOfRangeException("expressionIndex"); } V_1 = logicalContext.get_CFGBlockToLogicalConstructMap().get_Item(cfgBlock.get_TheBlock()); V_2 = (int)V_1.Length; V_3 = 0; while (V_3 < V_2 && cfgBlock != V_1[V_3]) { V_3 = V_3 + 1; } if (V_3 == V_2) { throw new ArgumentException("cfgBlock"); } V_4 = cfgBlock.get_LogicalConstructExpressions().GetRange(0, expressionIndex); V_5 = new PartialCFGBlockLogicalConstruct(cfgBlock, V_4); V_5.RedirectPredecessors(); V_6 = cfgBlock.get_LogicalConstructExpressions().GetRange(expressionIndex, V_0.get_Count() - expressionIndex); V_7 = new PartialCFGBlockLogicalConstruct(cfgBlock, V_6); V_7.RedirectSuccessors(); V_5.AddToSuccessors(V_7); V_7.AddToPredecessors(V_5); V_8 = new CFGBlockLogicalConstruct[V_2 + 1]; V_9 = 0; V_10 = 0; while (V_9 < V_2) { if (V_9 == V_3) { V_8[V_10] = V_5; stackVariable68 = V_10 + 1; V_10 = stackVariable68; V_8[stackVariable68] = V_7; } else { V_8[V_10] = V_1[V_9]; } V_9 = V_9 + 1; V_10 = V_10 + 1; } logicalContext.get_CFGBlockToLogicalConstructMap().set_Item(cfgBlock.get_TheBlock(), V_8); return(new KeyValuePair <CFGBlockLogicalConstruct, CFGBlockLogicalConstruct>(V_5, V_7)); }
public override int CompareTo(Cil.ISingleEntrySubGraph other) { PartialCFGBlockLogicalConstruct otherPartial = (other as ILogicalConstruct).FirstBlock as PartialCFGBlockLogicalConstruct; if (otherPartial != null) { if (this.Index == otherPartial.Index) { if (this == otherPartial) { return(0); } PartialCFGBlockLogicalConstruct currentPartial = this; while (currentPartial != null && currentPartial.Index == this.Index) { if (currentPartial == otherPartial) { return(-1); } if (currentPartial.CFGSuccessors.Count != 1) { break; } IEnumerator <CFGBlockLogicalConstruct> cfgSuccessorsEnumerator = currentPartial.CFGSuccessors.GetEnumerator(); cfgSuccessorsEnumerator.MoveNext(); currentPartial = cfgSuccessorsEnumerator.Current as PartialCFGBlockLogicalConstruct; } return(1); } } return(base.CompareTo(other)); }
/// <summary> /// Splits the given CFG construct into two partial constructs. The first containing the expressions with indices 0 to expressionIndex - 1, the second - /// expressionIndex to end. This method modifies the logicalContext.CFGBlockToLogicalConstructMap /// </summary> /// <param name="logicalContext"></param> /// <param name="cfgBlock"></param> /// <param name="expressionIndex"></param> /// <returns>A key value pair containing the first partial construct as key and the second as value.</returns> public static KeyValuePair <CFGBlockLogicalConstruct, CFGBlockLogicalConstruct> SplitCFGBlockAt(LogicalFlowBuilderContext logicalContext, CFGBlockLogicalConstruct cfgBlock, int expressionIndex) { List <Expression> blockExpressions = cfgBlock.LogicalConstructExpressions; if (blockExpressions == null) { throw new ArgumentNullException("blockExpressions"); } if (expressionIndex <= 0 || expressionIndex >= blockExpressions.Count) { throw new ArgumentOutOfRangeException("expressionIndex"); } CFGBlockLogicalConstruct[] instructionBlockConstructs = logicalContext.CFGBlockToLogicalConstructMap[cfgBlock.TheBlock]; int cfgBlockArrayLength = instructionBlockConstructs.Length; int cfgBlockIndex; for (cfgBlockIndex = 0; cfgBlockIndex < cfgBlockArrayLength; cfgBlockIndex++) { if (cfgBlock == instructionBlockConstructs[cfgBlockIndex]) { break; } } if (cfgBlockIndex == cfgBlockArrayLength) { throw new ArgumentException("cfgBlock"); } List <Expression> firstExpressionsList = cfgBlock.LogicalConstructExpressions.GetRange(0, expressionIndex); PartialCFGBlockLogicalConstruct firstPartial = new PartialCFGBlockLogicalConstruct(cfgBlock, firstExpressionsList); firstPartial.RedirectPredecessors(); List <Expression> secondExpressionsList = cfgBlock.LogicalConstructExpressions.GetRange(expressionIndex, blockExpressions.Count - expressionIndex); PartialCFGBlockLogicalConstruct secondPartial = new PartialCFGBlockLogicalConstruct(cfgBlock, secondExpressionsList); secondPartial.RedirectSuccessors(); firstPartial.AddToSuccessors(secondPartial); secondPartial.AddToPredecessors(firstPartial); CFGBlockLogicalConstruct[] updatedCollection = new CFGBlockLogicalConstruct[cfgBlockArrayLength + 1]; for (int i = 0, j = 0; i < cfgBlockArrayLength; i++, j++) { if (i != cfgBlockIndex) { updatedCollection[j] = instructionBlockConstructs[i]; } else { updatedCollection[j] = firstPartial; updatedCollection[++j] = secondPartial; } } logicalContext.CFGBlockToLogicalConstructMap[cfgBlock.TheBlock] = updatedCollection; return(new KeyValuePair <CFGBlockLogicalConstruct, CFGBlockLogicalConstruct>(firstPartial, secondPartial)); }
/// <summary> /// Splits the given CFG construct into two partial constructs. The first containing the expressions with indices 0 to expressionIndex - 1, the second - /// expressionIndex to end. This method modifies the logicalContext.CFGBlockToLogicalConstructMap /// </summary> /// <param name="logicalContext"></param> /// <param name="cfgBlock"></param> /// <param name="expressionIndex"></param> /// <returns>A key value pair containing the first partial construct as key and the second as value.</returns> public static KeyValuePair<CFGBlockLogicalConstruct, CFGBlockLogicalConstruct> SplitCFGBlockAt(LogicalFlowBuilderContext logicalContext, CFGBlockLogicalConstruct cfgBlock, int expressionIndex) { List<Expression> blockExpressions = cfgBlock.LogicalConstructExpressions; if(blockExpressions == null) { throw new ArgumentNullException("blockExpressions"); } if(expressionIndex <= 0 || expressionIndex >= blockExpressions.Count) { throw new ArgumentOutOfRangeException("expressionIndex"); } CFGBlockLogicalConstruct[] instructionBlockConstructs = logicalContext.CFGBlockToLogicalConstructMap[cfgBlock.TheBlock]; int cfgBlockArrayLength = instructionBlockConstructs.Length; int cfgBlockIndex; for (cfgBlockIndex = 0; cfgBlockIndex < cfgBlockArrayLength; cfgBlockIndex++) { if(cfgBlock == instructionBlockConstructs[cfgBlockIndex]) { break; } } if(cfgBlockIndex == cfgBlockArrayLength) { throw new ArgumentException("cfgBlock"); } List<Expression> firstExpressionsList = cfgBlock.LogicalConstructExpressions.GetRange(0, expressionIndex); PartialCFGBlockLogicalConstruct firstPartial = new PartialCFGBlockLogicalConstruct(cfgBlock, firstExpressionsList); firstPartial.RedirectPredecessors(); List<Expression> secondExpressionsList = cfgBlock.LogicalConstructExpressions.GetRange(expressionIndex, blockExpressions.Count - expressionIndex); PartialCFGBlockLogicalConstruct secondPartial = new PartialCFGBlockLogicalConstruct(cfgBlock, secondExpressionsList); secondPartial.RedirectSuccessors(); firstPartial.AddToSuccessors(secondPartial); secondPartial.AddToPredecessors(firstPartial); CFGBlockLogicalConstruct[] updatedCollection = new CFGBlockLogicalConstruct[cfgBlockArrayLength + 1]; for (int i = 0, j = 0; i < cfgBlockArrayLength; i++, j++) { if (i != cfgBlockIndex) { updatedCollection[j] = instructionBlockConstructs[i]; } else { updatedCollection[j] = firstPartial; updatedCollection[++j] = secondPartial; } } logicalContext.CFGBlockToLogicalConstructMap[cfgBlock.TheBlock] = updatedCollection; return new KeyValuePair<CFGBlockLogicalConstruct, CFGBlockLogicalConstruct>(firstPartial, secondPartial); }