[llvm] [llvm-objdump] Add the --visualize-jumps option (PR #74858)
Jonathan Thackray via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 3 07:39:20 PST 2024
================
@@ -503,5 +506,188 @@ SourcePrinter::SourcePrinter(const object::ObjectFile *Obj,
Symbolizer.reset(new symbolize::LLVMSymbolizer(SymbolizerOpts));
}
+// TODO Light/dark shades? 256-color terminals?
+const raw_ostream::Colors LineColors[] = {
+ raw_ostream::RED, raw_ostream::GREEN, raw_ostream::YELLOW,
+ raw_ostream::BLUE, raw_ostream::MAGENTA, raw_ostream::CYAN,
+};
+
+raw_ostream::Colors ControlFlowPrinter::PickColor() {
+ if (!OutputMode.color_enabled())
+ return raw_ostream::RESET;
+ auto Ret = LineColors[NextColorIdx];
+ NextColorIdx =
+ (NextColorIdx + 1) % (sizeof(LineColors) / sizeof(LineColors[0]));
+ return Ret;
+}
+
+void ControlFlowPrinter::addEdge(uint64_t From, uint64_t To) {
+ auto It = Targets.find(To);
+ if (It == Targets.end())
+ It = Targets.insert(std::make_pair(To, ControlFlowTarget(To, PickColor())))
+ .first;
+ It->second.addSource(From);
+}
+
+void ControlFlowPrinter::finalise() {
+ if (!OutputMode.enabled()) {
+ setControlFlowColumnWidth(0);
+ return;
+ }
+
+ SmallVector<ControlFlowTarget *> SortedTargets;
+ for (auto &[Addr, Info] : Targets) {
+ SortedTargets.push_back(&Info);
+ }
+ std::sort(SortedTargets.begin(), SortedTargets.end(),
+ [](ControlFlowTarget *LHS, ControlFlowTarget *RHS) {
+ return LHS->Length() < RHS->Length();
+ });
+
+ // FIXME This is O(n^3) in the worst case, can we do better?
+ for (auto &T : SortedTargets) {
+ int Column = 0;
+ for (;; ++Column)
+ if (!std::any_of(SortedTargets.begin(), SortedTargets.end(),
+ [T, Column](ControlFlowTarget *T2) {
+ return T != T2 && T2->Column == Column &&
+ T->Overlaps(*T2);
+ }))
+ break;
+ T->Column = Column;
+ MaxColumn = std::max(MaxColumn, Column);
+ }
+
+ setControlFlowColumnWidth(MaxColumn * 2 + 4);
+}
+
+const char *ControlFlowPrinter::getLineChar(LineChar C) const {
+ bool IsASCII = !OutputMode.unicode_enabled();
+ switch (C) {
+ case LineChar::Horiz:
+ return IsASCII ? "-" : u8"\u2500";
+ case LineChar::Vert:
+ return IsASCII ? "|" : u8"\u2502";
+ case LineChar::TopCorner:
+ return IsASCII ? "/" : u8"\u256d";
+ case LineChar::BottomCorner:
+ return IsASCII ? "\\" : u8"\u2570";
+ case LineChar::Tee:
+ return IsASCII ? "+" : u8"\u251c";
+ case LineChar::Arrow:
+ return ">";
+ }
+ llvm_unreachable("Unhandled LineChar enum");
+}
+
+#define C(id) getLineChar(LineChar::id)
----------------
jthackray wrote:
Nit: would prefer not to have a #define if possible
https://github.com/llvm/llvm-project/pull/74858
More information about the llvm-commits
mailing list