Skip to content

Commit 4497c53

Browse files
authored
[clang][bytecode] Accept current PC argument in Function::dump() (llvm#170449)
This is useful since we can highlight the opcode that OpPC points to.
1 parent dd9a516 commit 4497c53

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

clang/lib/AST/ByteCode/Disasm.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,16 @@ static size_t getNumDisplayWidth(size_t N) {
138138
return L;
139139
}
140140

141-
LLVM_DUMP_METHOD void Function::dump() const { dump(llvm::errs()); }
141+
LLVM_DUMP_METHOD void Function::dump(CodePtr PC) const {
142+
dump(llvm::errs(), PC);
143+
}
142144

143-
LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
145+
LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS,
146+
CodePtr OpPC) const {
147+
if (OpPC) {
148+
assert(OpPC >= getCodeBegin());
149+
assert(OpPC <= getCodeEnd());
150+
}
144151
{
145152
ColorScope SC(OS, true, {llvm::raw_ostream::BRIGHT_GREEN, true});
146153
OS << getName() << " " << (const void *)this << "\n";
@@ -154,6 +161,7 @@ LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
154161
size_t Addr;
155162
std::string Op;
156163
bool IsJump;
164+
bool CurrentOp = false;
157165
llvm::SmallVector<std::string> Args;
158166
};
159167

@@ -171,6 +179,7 @@ LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
171179
auto Op = PC.read<Opcode>();
172180
Text.Addr = Addr;
173181
Text.IsJump = isJumpOpcode(Op);
182+
Text.CurrentOp = (PC == OpPC);
174183
switch (Op) {
175184
#define GET_DISASM
176185
#include "Opcodes.inc"
@@ -198,9 +207,15 @@ LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
198207
Text.reserve(Code.size());
199208
size_t LongestLine = 0;
200209
// Print code to a string, one at a time.
201-
for (auto C : Code) {
210+
for (const auto &C : Code) {
202211
std::string Line;
203212
llvm::raw_string_ostream LS(Line);
213+
if (OpPC) {
214+
if (C.CurrentOp)
215+
LS << " * ";
216+
else
217+
LS << " ";
218+
}
204219
LS << C.Addr;
205220
LS.indent(LongestAddr - getNumDisplayWidth(C.Addr) + 4);
206221
LS << C.Op;

clang/lib/AST/ByteCode/Function.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ class Function final {
312312

313313
public:
314314
/// Dumps the disassembled bytecode to \c llvm::errs().
315-
void dump() const;
316-
void dump(llvm::raw_ostream &OS) const;
315+
void dump(CodePtr PC = {}) const;
316+
void dump(llvm::raw_ostream &OS, CodePtr PC = {}) const;
317317
};
318318

319319
} // namespace interp

clang/lib/AST/ByteCode/Source.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class CodePtr final {
5151
explicit operator bool() const { return Ptr; }
5252
bool operator<=(const CodePtr &RHS) const { return Ptr <= RHS.Ptr; }
5353
bool operator>=(const CodePtr &RHS) const { return Ptr >= RHS.Ptr; }
54+
bool operator==(const CodePtr RHS) const { return Ptr == RHS.Ptr; }
5455

5556
/// Reads data and advances the pointer.
5657
template <typename T> std::enable_if_t<!std::is_pointer<T>::value, T> read() {

0 commit comments

Comments
 (0)