Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions llvm/include/llvm/MC/MCInstPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
#ifndef LLVM_MC_MCINSTPRINTER_H
#define LLVM_MC_MCINSTPRINTER_H

#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdint>

namespace llvm {
Expand All @@ -24,7 +26,6 @@ class MCRegister;
class MCRegisterInfo;
class MCSubtargetInfo;
class StringRef;
class raw_ostream;

/// Convert `Bytes' to a hex string and output to `OS'
void dumpBytes(ArrayRef<uint8_t> Bytes, raw_ostream &OS);
Expand Down Expand Up @@ -76,6 +77,8 @@ class MCInstPrinter {
/// If true, symbolize branch target and memory reference operands.
bool SymbolizeOperands = false;

SmallVector<raw_ostream::Colors, 4> ColorStack{raw_ostream::Colors::RESET};

/// Utility function for printing annotations.
void printAnnotation(raw_ostream &OS, StringRef Annot);

Expand All @@ -98,8 +101,8 @@ class MCInstPrinter {

class WithMarkup {
public:
LLVM_CTOR_NODISCARD WithMarkup(raw_ostream &OS, Markup M, bool EnableMarkup,
bool EnableColor);
LLVM_CTOR_NODISCARD WithMarkup(MCInstPrinter &IP, raw_ostream &OS, Markup M,
bool EnableMarkup, bool EnableColor);
~WithMarkup();

template <typename T> WithMarkup &operator<<(T &O) {
Expand All @@ -113,6 +116,7 @@ class MCInstPrinter {
}

private:
MCInstPrinter &IP;
raw_ostream &OS;
bool EnableMarkup;
bool EnableColor;
Expand Down
27 changes: 17 additions & 10 deletions llvm/lib/MC/MCInstPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,27 +226,32 @@ format_object<uint64_t> MCInstPrinter::formatHex(uint64_t Value) const {

MCInstPrinter::WithMarkup MCInstPrinter::markup(raw_ostream &OS,
Markup S) const {
return WithMarkup(OS, S, getUseMarkup(), getUseColor());
return WithMarkup(const_cast<MCInstPrinter &>(*this), OS, S, getUseMarkup(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall approach looks fine, but this does feel a bit like an abuse of const. Could const be dropped from the markup method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many const methods transitively call the const markup. If we want to drop const_cast here, many target functions printXXX will need be adjusted...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yuck, okay. I think that's probably still the "right" thing to do (after all you're modifying the state of the class when calling these methods in this manner). But I acknowledge that's probably a bit impractical too, so if you add a TODO comment here instead, I'd be happy with that.

I also considered making the ColorStack member mutable, but that doesn't really feel any better than the const_cast in this context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migrated printRegName users. Dropped const_cast now.

getUseColor());
}

MCInstPrinter::WithMarkup::WithMarkup(raw_ostream &OS, Markup M,
bool EnableMarkup, bool EnableColor)
: OS(OS), EnableMarkup(EnableMarkup), EnableColor(EnableColor) {
MCInstPrinter::WithMarkup::WithMarkup(MCInstPrinter &IP, raw_ostream &OS,
Markup M, bool EnableMarkup,
bool EnableColor)
: IP(IP), OS(OS), EnableMarkup(EnableMarkup), EnableColor(EnableColor) {
if (EnableColor) {
raw_ostream::Colors Color = raw_ostream::Colors::RESET;
switch (M) {
case Markup::Immediate:
OS.changeColor(raw_ostream::RED);
Color = raw_ostream::RED;
break;
case Markup::Register:
OS.changeColor(raw_ostream::CYAN);
Color = raw_ostream::CYAN;
break;
case Markup::Target:
OS.changeColor(raw_ostream::YELLOW);
Color = raw_ostream::YELLOW;
break;
case Markup::Memory:
OS.changeColor(raw_ostream::GREEN);
Color = raw_ostream::GREEN;
break;
}
IP.ColorStack.push_back(Color);
OS.changeColor(Color);
}

if (EnableMarkup) {
Expand All @@ -270,6 +275,8 @@ MCInstPrinter::WithMarkup::WithMarkup(raw_ostream &OS, Markup M,
MCInstPrinter::WithMarkup::~WithMarkup() {
if (EnableMarkup)
OS << '>';
if (EnableColor)
OS.resetColor();
if (!EnableColor)
return;
IP.ColorStack.pop_back();
OS << IP.ColorStack.back();
}
21 changes: 21 additions & 0 deletions llvm/test/tools/llvm-objdump/X86/disassemble-color.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# UNSUPPORTED: system-windows
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t
# RUN: llvm-objdump -d --no-show-raw-insn --disassembler-color=on %t | FileCheck %s --check-prefix=ATT
# RUN: llvm-objdump -d --no-show-raw-insn --disassembler-color=on -M intel %t | FileCheck %s --check-prefix=INTEL

# ATT: <.text>:
# ATT-NEXT: leaq (%rdx,%rax,4), %rbx
# ATT-NEXT: movq (,%rax), %rbx
# ATT-NEXT: leaq 0x3(%rdx,%rax), %rbx
# ATT-NEXT: movq $0x3, %rax

# INTEL: <.text>:
# INTEL-NEXT: lea rbx, [rdx + 4*rax]
# INTEL-NEXT: mov rbx, qword ptr [1*rax]
# INTEL-NEXT: lea rbx, [rdx + rax + 0x3]
# INTEL-NEXT: mov rax, 0x3

leaq (%rdx,%rax,4), %rbx
movq (,%rax), %rbx
leaq 3(%rdx,%rax), %rbx
movq $3, %rax
Loading