Skip to content

Commit 77d1032

Browse files
committed
[llvm] Add assembly color highlighting
Add support for syntax highlighting assembly. The patch introduces new RAII helper called WithMarkup that takes care of both emitting colors and markup annotations. It makes adding markup easier and ensures colors and annotations remain consistent. This patch adopts the new helper in the AArch64 backend. If your backend already uses markup annotations, adoption is as easy as using the new MCInstPrinter::markup overload. Differential revision: https://reviews.llvm.org/D159162
1 parent dc2c4fc commit 77d1032

File tree

7 files changed

+204
-85
lines changed

7 files changed

+204
-85
lines changed

llvm/docs/CommandGuide/llvm-mc.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ End-user Options
7575

7676
Marked up disassembly of string of hex bytes.
7777

78+
.. option:: --cdis
79+
80+
Colored disassembly of string of hex bytes.
81+
7882
.. option:: --filetype=[asm,null,obj]
7983

8084
Sets the output filetype. Setting this flag to `asm` will make the tool

llvm/include/llvm/MC/MCInstPrinter.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ class MCInstPrinter {
5555
/// True if we are printing marked up assembly.
5656
bool UseMarkup = false;
5757

58+
/// True if we are printing colored assembly.
59+
bool UseColor = false;
60+
5861
/// True if we prefer aliases (e.g. nop) to raw mnemonics.
5962
bool PrintAliases = true;
6063

@@ -85,6 +88,35 @@ class MCInstPrinter {
8588

8689
virtual ~MCInstPrinter();
8790

91+
enum class Markup {
92+
Immediate,
93+
Register,
94+
Target,
95+
Memory,
96+
};
97+
98+
class WithMarkup {
99+
public:
100+
[[nodiscard]] WithMarkup(raw_ostream &OS, Markup M, bool EnableMarkup,
101+
bool EnableColor);
102+
~WithMarkup();
103+
104+
template <typename T> WithMarkup &operator<<(T &O) {
105+
OS << O;
106+
return *this;
107+
}
108+
109+
template <typename T> WithMarkup &operator<<(const T &O) {
110+
OS << O;
111+
return *this;
112+
}
113+
114+
private:
115+
raw_ostream &OS;
116+
bool EnableMarkup;
117+
bool EnableColor;
118+
};
119+
88120
/// Customize the printer according to a command line option.
89121
/// @return true if the option is recognized and applied.
90122
virtual bool applyTargetSpecificCLOption(StringRef Opt) { return false; }
@@ -116,6 +148,11 @@ class MCInstPrinter {
116148
bool getUseMarkup() const { return UseMarkup; }
117149
void setUseMarkup(bool Value) { UseMarkup = Value; }
118150

151+
bool getUseColor() const { return UseColor; }
152+
void setUseColor(bool Value) { UseColor = Value; }
153+
154+
WithMarkup markup(raw_ostream &OS, Markup M) const;
155+
119156
/// Utility functions to make adding mark ups simpler.
120157
StringRef markup(StringRef s) const;
121158

llvm/lib/MC/MCInstPrinter.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,53 @@ format_object<uint64_t> MCInstPrinter::formatHex(uint64_t Value) const {
231231
}
232232
llvm_unreachable("unsupported print style");
233233
}
234+
235+
MCInstPrinter::WithMarkup MCInstPrinter::markup(raw_ostream &OS,
236+
Markup S) const {
237+
return WithMarkup(OS, S, getUseMarkup(), getUseColor());
238+
}
239+
240+
MCInstPrinter::WithMarkup::WithMarkup(raw_ostream &OS, Markup M,
241+
bool EnableMarkup, bool EnableColor)
242+
: OS(OS), EnableMarkup(EnableMarkup), EnableColor(EnableColor) {
243+
if (EnableColor) {
244+
switch (M) {
245+
case Markup::Immediate:
246+
OS.changeColor(raw_ostream::RED);
247+
break;
248+
case Markup::Register:
249+
OS.changeColor(raw_ostream::CYAN);
250+
break;
251+
case Markup::Target:
252+
OS.changeColor(raw_ostream::YELLOW);
253+
break;
254+
case Markup::Memory:
255+
OS.changeColor(raw_ostream::GREEN);
256+
break;
257+
}
258+
}
259+
260+
if (EnableMarkup) {
261+
switch (M) {
262+
case Markup::Immediate:
263+
OS << "<imm:";
264+
break;
265+
case Markup::Register:
266+
OS << "<reg:";
267+
break;
268+
case Markup::Target:
269+
OS << "<target:";
270+
break;
271+
case Markup::Memory:
272+
OS << "<mem:";
273+
break;
274+
}
275+
}
276+
}
277+
278+
MCInstPrinter::WithMarkup::~WithMarkup() {
279+
if (EnableMarkup)
280+
OS << '>';
281+
if (EnableColor)
282+
OS.resetColor();
283+
}

0 commit comments

Comments
 (0)