| 2679 | |
| 2680 | template <typename InstList> |
| 2681 | void Differ::OutputSection( |
| 2682 | const InstList& src_insts, const InstList& dst_insts, |
| 2683 | std::function<void(const opt::Instruction&, const IdInstructions&, |
| 2684 | const opt::Instruction&)> |
| 2685 | write_inst) { |
| 2686 | auto src_iter = src_insts.begin(); |
| 2687 | auto dst_iter = dst_insts.begin(); |
| 2688 | |
| 2689 | // - While src_inst doesn't have a match, output it with - |
| 2690 | // - While dst_inst doesn't have a match, output it with + |
| 2691 | // - Now src_inst and dst_inst both have matches; might not match each other! |
| 2692 | // * If section is unordered, just process src_inst and its match (dst_inst |
| 2693 | // or not), |
| 2694 | // dst_inst will eventually be processed when its match is seen. |
| 2695 | // * If section is ordered, also just process src_inst and its match. Its |
| 2696 | // match must |
| 2697 | // necessarily be dst_inst. |
| 2698 | while (src_iter != src_insts.end() || dst_iter != dst_insts.end()) { |
| 2699 | OutputRed(); |
| 2700 | while (src_iter != src_insts.end() && |
| 2701 | MappedDstInst(IterInst(src_iter)) == nullptr) { |
| 2702 | out_ << "-"; |
| 2703 | write_inst(*IterInst(src_iter), src_id_to_, *IterInst(src_iter)); |
| 2704 | ++src_iter; |
| 2705 | } |
| 2706 | OutputGreen(); |
| 2707 | while (dst_iter != dst_insts.end() && |
| 2708 | MappedSrcInst(IterInst(dst_iter)) == nullptr) { |
| 2709 | out_ << "+"; |
| 2710 | write_inst(ToMappedSrcIds(*IterInst(dst_iter)), dst_id_to_, |
| 2711 | *IterInst(dst_iter)); |
| 2712 | ++dst_iter; |
| 2713 | } |
| 2714 | OutputResetColor(); |
| 2715 | |
| 2716 | if (src_iter != src_insts.end() && dst_iter != dst_insts.end()) { |
| 2717 | const opt::Instruction* src_inst = IterInst(src_iter); |
| 2718 | const opt::Instruction* matched_dst_inst = MappedDstInst(src_inst); |
| 2719 | |
| 2720 | assert(matched_dst_inst != nullptr); |
| 2721 | assert(MappedSrcInst(IterInst(dst_iter)) != nullptr); |
| 2722 | |
| 2723 | OutputLine( |
| 2724 | [this, src_inst, matched_dst_inst]() { |
| 2725 | return DoInstructionsMatch(src_inst, matched_dst_inst); |
| 2726 | }, |
| 2727 | [this, src_inst, &write_inst]() { |
| 2728 | write_inst(*src_inst, src_id_to_, *src_inst); |
| 2729 | }, |
| 2730 | [this, matched_dst_inst, &write_inst]() { |
| 2731 | write_inst(ToMappedSrcIds(*matched_dst_inst), dst_id_to_, |
| 2732 | *matched_dst_inst); |
| 2733 | }); |
| 2734 | |
| 2735 | ++src_iter; |
| 2736 | ++dst_iter; |
| 2737 | } |
| 2738 | } |