static public bool Detect(CodeBlock block, out StatementStructure statementStructure) { List <OpCode> brs = new List <OpCode> { OpCodes.Br_S, OpCodes.Br }; statementStructure = null; if (!ConditionalBranch_OpCodes.Contains(block.first.OpCode)) { return(false); } var conditionStatementBlock = Detect(block.first, block.last); if (conditionStatementBlock == null) { return(false); } List <ConditionStatementBlock> blocks = new List <ConditionStatementBlock>(); blocks.Add(conditionStatementBlock); while (true) { var next = conditionStatementBlock.body.last.Next; if (!brs.Contains(next.Previous.OpCode)) { break; } var br = next.Previous as Instruction; var target = br.Operand as Instruction; if (target.OpCode == OpCodes.Ret) { break; } conditionStatementBlock = Detect(next, target.Previous); if (conditionStatementBlock == null) { conditionStatementBlock = new ConditionStatementBlock(null, new CodeBlock(next, target.Previous)); blocks.Add(conditionStatementBlock); break; } var current = blocks[blocks.Count - 1]; blocks[blocks.Count - 1] = new ConditionStatementBlock( current.condition, new CodeBlock(current.body.first, current.body.last.Previous) ); blocks.Add(conditionStatementBlock); } CodeBlock all = new CodeBlock(block.first, blocks[blocks.Count - 1].body.last); statementStructure = new ConditionStatementStructure(all, blocks); return(true); }
static public StatementStructureType Recognize(CodeBlock block, out StatementStructure statementStructure) { if (LoopStatementStructure.Detect(block, out statementStructure) || ConditionStatementStructure.Detect(block, out statementStructure)) { return(statementStructure.type); } statementStructure = null; return(StatementStructureType.Unknown); }