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,10 +3,12 @@ ## 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-NEXT: 4: 1f 20 03 d5 |nop # CHECK-EMPTY: -# CHECK-NEXT:0000000000000004 <$d.1>: -# CHECK-NEXT: 4:|ff ff 00 00|.word|0x0000ffff +# CHECK-NEXT:0000000000000008 <$d.1>: +# CHECK-NEXT: 8:|ff ff 00 00|.word|0x0000ffff add x2, x3, #4 + nop .word 0xffff 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-align.s =================================================================== --- llvm/test/tools/llvm-objdump/X86/disassemble-align.s +++ llvm/test/tools/llvm-objdump/X86/disassemble-align.s @@ -1,26 +1,27 @@ # RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t # Use '|' to show where the tabs line up. -# RUN: llvm-objdump -d --print-imm-hex %t | tr '\t' '|' | FileCheck --strict-whitespace %s +# RUN: llvm-objdump -d --print-imm-hex %t | tr '\t' '|' | \ +# RUN: FileCheck --match-full-lines --strict-whitespace %s # RUN: llvm-objdump -d --print-imm-hex --no-show-raw-insn %t | tr '\t' '|' | \ -# RUN: FileCheck -check-prefix=NORAW -strict-whitespace %s +# RUN: FileCheck -check-prefix=NORAW --match-full-lines -strict-whitespace %s # Instructions are expected to be aligned if the instruction in hex is not too long. -# CHECK: 0: c3 |retq -# CHECK-NEXT: 1: 48 8b 05 56 34 12 00 |movq|0x123456(%rip), %rax -# CHECK-NEXT: 8: 48 b8 54 55 55 55 55 55 55 55|movabsq|$0x5555555555555554, %rax -# CHECK-NEXT: 12: 8f ea 00 12 4c 02 40 00 00 00 00 |lwpval|$0x0, 0x40(%rdx,%rax), %r15d -# CHECK-NEXT: 1d: 8f ea 00 12 04 25 f0 1c f0 1c 00 00 00 00 |lwpins|$0x0, 0x1cf01cf0, %r15d -# CHECK-NEXT: 2b: ff ff | +# CHECK: 0: c3 |retq +# CHECK-NEXT: 1: 48 8b 05 56 34 12 00 |movq|0x123456(%rip), %rax # 12345e <.text+0x12345e> +# CHECK-NEXT: 8: 48 b8 54 55 55 55 55 55 55 55|movabsq|$0x5555555555555554, %rax # imm = 0x5555555555555554 +# CHECK-NEXT: 12: 8f ea 00 12 4c 02 40 00 00 00 00 |lwpval|$0x0, 0x40(%rdx,%rax), %r15d +# CHECK-NEXT: 1d: 8f ea 00 12 04 25 f0 1c f0 1c 00 00 00 00 |lwpins|$0x0, 0x1cf01cf0, %r15d +# CHECK-NEXT: 2b: ff ff | -# NORAW: 0: |retq -# NORAW-NEXT: 1: |movq|0x123456(%rip), %rax -# NORAW-NEXT: 8: |movabsq|$0x5555555555555554, %rax -# NORAW-NEXT: 12: |lwpval|$0x0, 0x40(%rdx,%rax), %r15d -# NORAW-NEXT: 1d: |lwpins|$0x0, 0x1cf01cf0, %r15d -# NORAW-NEXT: 2b: | +# NORAW: 0: |retq +# NORAW-NEXT: 1: |movq|0x123456(%rip), %rax # 12345e <.text+0x12345e> +# NORAW-NEXT: 8: |movabsq|$0x5555555555555554, %rax # imm = 0x5555555555555554 +# NORAW-NEXT: 12: |lwpval|$0x0, 0x40(%rdx,%rax), %r15d +# NORAW-NEXT: 1d: |lwpins|$0x0, 0x1cf01cf0, %r15d +# NORAW-NEXT: 2b: | .text retq 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,29 @@ return ""; } +static void emitPostInstructionInfo(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 +1419,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 +1523,10 @@ } } - LVP.printAfterInst(FOS); - FOS << "\n"; + assert(Ctx.getAsmInfo()); + emitPostInstructionInfo(FOS, *Ctx.getAsmInfo(), *STI, + CommentStream.str(), LVP); + Comments.clear(); // Hexagon does this in pretty printer if (Obj->getArch() != Triple::hexagon) {