Index: llvm/test/tools/llvm-objdump/ELF/AArch64/disassemble-align.s =================================================================== --- llvm/test/tools/llvm-objdump/ELF/AArch64/disassemble-align.s +++ llvm/test/tools/llvm-objdump/ELF/AArch64/disassemble-align.s @@ -3,7 +3,7 @@ ## Use '|' to show where the tabs line up. # CHECK:0000000000000000 <$x.0>: -# CHECK-NEXT: 0: 62 10 00 91 |add|x2, x3, #4 +# CHECK-NEXT: 0: 62 10 00 91 |add|x2, x3, #4 // =4 # CHECK-EMPTY: # CHECK-NEXT:0000000000000004 <$d.1>: # CHECK-NEXT: 4:|ff ff 00 00|.word|0x0000ffff Index: llvm/test/tools/llvm-objdump/ELF/AArch64/disassemble-print-comments.s =================================================================== --- /dev/null +++ llvm/test/tools/llvm-objdump/ELF/AArch64/disassemble-print-comments.s @@ -0,0 +1,13 @@ +## Check that 'llvm-objdump -d' prints comments generated by the disassembler. + +# RUN: llvm-mc -filetype=obj -triple=aarch64 -mattr=+sve %s -o %t +# RUN: llvm-objdump -d --mattr=+sve --no-show-raw-insn %t | FileCheck %s + +# CHECK: 0000000000000000 : +# CHECK-NEXT: 0: add x0, x2, #2, lsl #12 // =8192 +# CHECK-NEXT: 4: add z31.d, z31.d, #65280 // =0xff00 + + .text +foo: + add x0, x2, 8192 + add z31.d, z31.d, #65280 Index: llvm/test/tools/llvm-objdump/X86/disassemble-print-comments.s =================================================================== --- /dev/null +++ llvm/test/tools/llvm-objdump/X86/disassemble-print-comments.s @@ -0,0 +1,13 @@ +## Check that 'llvm-objdump -d' prints comments generated by the disassembler. + +# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t +# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s + +# CHECK: 0000000000000000 : +# CHECK-NEXT: 0: nop +# CHECK-NEXT: 1: cmpl $305419896, %eax # imm = 0x12345678 + + .text +foo: + nop + cmpl $0x12345678, %eax Index: llvm/tools/llvm-objdump/llvm-objdump.cpp =================================================================== --- llvm/tools/llvm-objdump/llvm-objdump.cpp +++ llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -1039,6 +1039,27 @@ return ""; } +static void emitEOLStuff(formatted_raw_ostream &FOS, const MCAsmInfo &MAI, + const MCSubtargetInfo &STI, StringRef Comments, + LiveVariablePrinter &LVP) { + do { + if (!Comments.empty()) { + // Emit a line of comments. + StringRef Comment; + std::tie(Comment, Comments) = Comments.split('\n'); + // MAI.getCommentColumn() assumes that instructions are printed at the + // position of 8, while getInstStartColumn() returns the actual position. + unsigned CommentColumn = + MAI.getCommentColumn() - 8 + getInstStartColumn(STI); + FOS.PadToColumn(CommentColumn); + FOS << MAI.getCommentString() << ' ' << Comment; + } + LVP.printAfterInst(FOS); + FOS << '\n'; + } while (!Comments.empty()); + FOS.flush(); +} + static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj, MCContext &Ctx, MCDisassembler *PrimaryDisAsm, MCDisassembler *SecondaryDisAsm, @@ -1396,12 +1417,14 @@ LVP.update({Index, Section.getIndex()}, {Index + Size, Section.getIndex()}, Index + Size != End); + IP->setCommentStream(CommentStream); + PIP.printInst( *IP, Disassembled ? &Inst : nullptr, Bytes.slice(Index, Size), {SectionAddr + Index + VMAAdjustment, Section.getIndex()}, FOS, "", *STI, &SP, Obj->getFileName(), &Rels, LVP); - FOS << CommentStream.str(); - Comments.clear(); + + IP->setCommentStream(llvm::nulls()); // If disassembly has failed, avoid analysing invalid/incomplete // instruction information. Otherwise, try to resolve the target @@ -1498,8 +1521,9 @@ } } - LVP.printAfterInst(FOS); - FOS << "\n"; + assert(Ctx.getAsmInfo()); + emitEOLStuff(FOS, *Ctx.getAsmInfo(), *STI, CommentStream.str(), LVP); + Comments.clear(); // Hexagon does this in pretty printer if (Obj->getArch() != Triple::hexagon) {