llvm.org GIT mirror llvm / 14ccc90
Added a option to the disassembler to print immediates as hex. This is for the lldb team so most of but not all of the values are to be printed as hex with this option. Some small values like the scale in an X86 address were requested to printed in decimal without the leading 0x. There may be some tweaks need to places that may still be in decimal that they want in hex. Specially for arm. I made my best guess. Any tweaks from here should be simple. I also did the best I know now with help from the C++ gurus creating the cleanest formatImm() utility function and containing the changes. But if someone has a better idea to make something cleaner I'm all ears and game for changing the implementation. rdar://8109283 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169393 91177308-0d34-0410-b5e6-96231b3b80d8 Kevin Enderby 7 years ago
9 changed file(s) with 74 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
88
99 #ifndef LLVM_MC_MCINSTPRINTER_H
1010 #define LLVM_MC_MCINSTPRINTER_H
11
12 #include "llvm/Support/Format.h"
1113
1214 namespace llvm {
1315 class MCInst;
3537 /// True if we are printing marked up assembly.
3638 bool UseMarkup;
3739
40 /// True if we are printing immediates as hex.
41 bool PrintImmHex;
42
3843 /// Utility function for printing annotations.
3944 void printAnnotation(raw_ostream &OS, StringRef Annot);
4045 public:
4146 MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
4247 const MCRegisterInfo &mri)
4348 : CommentStream(0), MAI(mai), MII(mii), MRI(mri), AvailableFeatures(0),
44 UseMarkup(0) {}
49 UseMarkup(0), PrintImmHex(0) {}
4550
4651 virtual ~MCInstPrinter();
4752
6974 /// Utility functions to make adding mark ups simpler.
7075 StringRef markup(StringRef s) const;
7176 StringRef markup(StringRef a, StringRef b) const;
77
78 bool getPrintImmHex() const { return PrintImmHex; }
79 void setPrintImmHex(bool Value) { PrintImmHex = Value; }
80
81 /// Utility function to print immediates in decimal or hex.
82 format_object1 formatImm(const int64_t Value) const;
7283 };
7384
7485 } // namespace llvm
152152
153153 /* The option to produce marked up assembly. */
154154 #define LLVMDisassembler_Option_UseMarkup 1
155 /* The option to print immediates as hex. */
156 #define LLVMDisassembler_Option_PrintImmHex 2
155157
156158 /**
157159 * Dispose of a disassembler context.
181181 IP->setUseMarkup(1);
182182 Options &= ~LLVMDisassembler_Option_UseMarkup;
183183 }
184 if (Options & LLVMDisassembler_Option_PrintImmHex){
185 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
186 MCInstPrinter *IP = DC->getIP();
187 IP->setPrintImmHex(1);
188 Options &= ~LLVMDisassembler_Option_PrintImmHex;
189 }
184190 return (Options == 0);
185191 }
1111 #include "llvm/MC/MCAsmInfo.h"
1212 #include "llvm/MC/MCInstrInfo.h"
1313 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/Format.h"
1415 #include "llvm/Support/raw_ostream.h"
1516 using namespace llvm;
1617
4950 else
5051 return b;
5152 }
53
54 /// Utility function to print immediates in decimal or hex.
55 format_object1 MCInstPrinter::formatImm(const int64_t Value) const {
56 if (getPrintImmHex())
57 return format("0x%llx", Value);
58 else
59 return format("%lld", Value);
60 }
292292 printRegName(O, Reg);
293293 } else if (Op.isImm()) {
294294 O << markup("
295 << '#' << Op.getImm()
295 << '#' << formatImm(Op.getImm())
296296 << markup(">");
297297 } else {
298298 assert(Op.isExpr() && "unknown operand kind in printOperand");
318318 O << *MO1.getExpr();
319319 else if (MO1.isImm()) {
320320 O << markup("
321 << markup("MO1.getImm()
321 << markup("formatImm(MO1.getImm())
322322 << markup(">]>", "]");
323323 }
324324 else
910910 void ARMInstPrinter::printThumbS4ImmOperand(const MCInst *MI, unsigned OpNum,
911911 raw_ostream &O) {
912912 O << markup("
913 << "#" << MI->getOperand(OpNum).getImm() * 4
913 << "#" << formatImm(MI->getOperand(OpNum).getImm() * 4)
914914 << markup(">");
915915 }
916916
918918 raw_ostream &O) {
919919 unsigned Imm = MI->getOperand(OpNum).getImm();
920920 O << markup("
921 << "#" << (Imm == 0 ? 32 : Imm)
921 << "#" << formatImm((Imm == 0 ? 32 : Imm))
922922 << markup(">");
923923 }
924924
975975 if (unsigned ImmOffs = MO2.getImm()) {
976976 O << ", "
977977 << markup("
978 << "#" << ImmOffs * Scale
978 << "#" << formatImm(ImmOffs * Scale)
979979 << markup(">");
980980 }
981981 O << "]" << markup(">");
11261126 if (MO2.getImm()) {
11271127 O << ", "
11281128 << markup("
1129 << "#" << MO2.getImm() * 4
1129 << "#" << formatImm(MO2.getImm() * 4)
11301130 << markup(">");
11311131 }
11321132 O << "]" << markup(">");
12161216 raw_ostream &O) {
12171217 unsigned Imm = MI->getOperand(OpNum).getImm();
12181218 O << markup("
1219 << "#" << Imm + 1
1219 << "#" << formatImm(Imm + 1)
12201220 << markup(">");
12211221 }
12221222
130130 raw_ostream &O) {
131131 const MCOperand &Op = MI->getOperand(OpNo);
132132 if (Op.isImm())
133 O << Op.getImm();
133 O << formatImm(Op.getImm());
134134 else {
135135 assert(Op.isExpr() && "unknown pcrel immediate operand");
136136 // If a symbolic branch target was added as a constant expression then print
156156 } else if (Op.isImm()) {
157157 // Print X86 immediates as signed values.
158158 O << markup("
159 << '$' << (int64_t)Op.getImm()
159 << '$' << formatImm((int64_t)Op.getImm())
160160 << markup(">");
161161
162162 if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256))
188188 if (DispSpec.isImm()) {
189189 int64_t DispVal = DispSpec.getImm();
190190 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
191 O << DispVal;
191 O << formatImm(DispVal);
192192 } else {
193193 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
194194 O << *DispSpec.getExpr();
206206 if (ScaleVal != 1) {
207207 O << ','
208208 << markup("
209 << ScaleVal
209 << ScaleVal // never printed in hex.
210210 << markup(">");
211211 }
212212 }
0 # RUN: llvm-mc -triple=thumbv7-apple-darwin -mcpu=cortex-a8 -hdis < %s | FileCheck %s
1 # CHECK: ldr r4, [pc, #0x20]
2 0x08 0x4c
3 # CHECK: sub sp, #0x84
4 0xa1 0xb0
0 # RUN: llvm-mc --hdis %s -triple=x86_64-apple-darwin9 2>&1 | FileCheck %s
1
2 # CHECK: movabsq $0x7fffffffffffffff, %rcx
3 0x48 0xb9 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0x7f
4 # CHECK: leaq 0x3e2(%rip), %rdi
5 0x48 0x8d 0x3d 0xe2 0x03 0x00 0x00
6 # CHECK: subq $0x40, %rsp
7 0x48 0x83 0xec 0x40
8 # CHECK: leal (,%r14,4), %eax
9 0x42 0x8d 0x04 0xb5 0x00 0x00 0x00 0x00
161161 AC_Assemble,
162162 AC_Disassemble,
163163 AC_EDisassemble,
164 AC_MDisassemble
164 AC_MDisassemble,
165 AC_HDisassemble
165166 };
166167
167168 static cl::opt
177178 "Enhanced disassembly of strings of hex bytes"),
178179 clEnumValN(AC_MDisassemble, "mdis",
179180 "Marked up disassembly of strings of hex bytes"),
181 clEnumValN(AC_HDisassemble, "hdis",
182 "Disassemble strings of hex bytes printing "
183 "immediates as hex"),
180184 clEnumValEnd));
181185
182186 static const Target *GetTarget(const char *ProgName) {
436440 }
437441
438442 int Res = 1;
443 bool disassemble = false;
439444 switch (Action) {
440445 case AC_AsLex:
441446 Res = AsLexInput(SrcMgr, *MAI, Out.get());
445450 break;
446451 case AC_MDisassemble:
447452 IP->setUseMarkup(1);
448 // Fall through to do disassembly.
453 disassemble = true;
454 break;
455 case AC_HDisassemble:
456 IP->setPrintImmHex(1);
457 disassemble = true;
458 break;
449459 case AC_Disassemble:
460 disassemble = true;
461 break;
462 case AC_EDisassemble:
463 Res = Disassembler::disassembleEnhanced(TripleName, *Buffer, SrcMgr, Out->os());
464 break;
465 }
466 if (disassemble)
450467 Res = Disassembler::disassemble(*TheTarget, TripleName, *STI, *Str,
451468 *Buffer, SrcMgr, Out->os());
452 break;
453 case AC_EDisassemble:
454 Res = Disassembler::disassembleEnhanced(TripleName, *Buffer, SrcMgr, Out->os());
455 break;
456 }
457469
458470 // Keep output if no errors.
459471 if (Res == 0) Out->keep();