private void InferBasicBlocks() { List <BasicBlock <Ti> > bbs = new List <BasicBlock <Ti> >(); int curIndex = 0; do { int startIndex = curIndex; int lastIndex = curIndex; while (!IsEndOfBB(lastIndex)) { ++lastIndex; } BasicBlock <Ti> bb = CreateBasicBlock(startIndex, lastIndex); if (curIndex == Instructions.Count - 1) { bb.IsExitBlock = true; } else { IEnumerable <int> targets; EBranchBehavior bbh = InstructionInfo.IsBranch(Instructions[lastIndex], out targets); if (bbh == EBranchBehavior.Switch) { // Special case: evil switch instructions bb.IsSwitch = true; } } bbs.Add(bb); curIndex = lastIndex + 1; } while (curIndex < Instructions.Count); BasicBlocks = bbs.ToArray(); }
private void AnalyzeBranches() { MarkAsBranchTarget(Instructions[_entryPoint]); Ti[] term = new Ti[] { _marshal }; MarkAsBranch(_marshal); MarkAsBranchTarget(_marshal); foreach (Ti ili in Instructions) { if (_exitPoints != null && _exitPoints.Contains(ili.Index)) { _successors[ili.Index] = term; MarkAsBranch(ili); } else if (ili.Index == _marshal.Index) { _successors[ili.Index] = new Ti[0]; } else { IEnumerable <int> targets; EBranchBehavior bb = InstructionInfo.IsBranch(ili, out targets); bool includeNext = false; bool isBranch = false; bool isTerm = false; switch (bb) { case EBranchBehavior.NoBranch: includeNext = true; isBranch = false; isTerm = false; break; case EBranchBehavior.UBranch: includeNext = false; isBranch = true; isTerm = false; break; case EBranchBehavior.CBranch: includeNext = true; isBranch = true; isTerm = false; break; case EBranchBehavior.Return: case EBranchBehavior.Throw: includeNext = false; isBranch = true; isTerm = true; break; case EBranchBehavior.Switch: includeNext = true; isBranch = true; isTerm = false; break; default: throw new NotImplementedException(); } if (isBranch) { MarkAsBranch(ili); } foreach (int target in targets) { MarkAsBranchTarget(Instructions[target]); } if (isTerm && ili.Index < Instructions.Count - 1) { _successors[ili.Index] = term; } else if (includeNext) { List <Ti> succs = new List <Ti>(); succs.Add(Instructions[ili.Index + 1]); succs.AddRange(targets.Select(t => Instructions[t])); _successors[ili.Index] = succs.ToArray(); } else { _successors[ili.Index] = targets.Select(t => Instructions[t]).ToArray(); } } } }