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); }
static public bool Detect(CodeBlock block, out StatementStructure statementStructure) { statementStructure = null; var opCode = block.first.OpCode; if (opCode != OpCodes.Br && opCode != OpCodes.Br_S) { return(false); } // maybe loop condition var target = block.first.Operand as Instruction; // find next branch instruction (Brtrue, Brtrue_S), which may be a loop tag and an end Instruction last = null; { var tmp = target; while (tmp != null && !ConditionalBranch_OpCodes.Contains(tmp.OpCode)) { tmp = tmp.Next; } if (tmp == null) { return(false); } last = tmp; } // loop body if ((last.Operand as Instruction).Previous != block.first) { return(false); } CodeBlock all = new CodeBlock(block.first, last); CodeBlock init = null; CodeBlock condition = new CodeBlock(target, last); CodeBlock step = null; CodeBlock body = new CodeBlock(block.first.Next, target.Previous); statementStructure = new LoopStatementStructure(all, init, condition, step, body); return(true); }