llvm.org GIT mirror llvm / 44c8e34
Fixed the Intel-syntax X86 disassembler to respect the (existing) option for hexadecimal immediates, to match AT&T syntax. This also brings a new option for C-vs-MASM-style hex. Patch by Richard Mitton Reviewed: http://llvm-reviews.chandlerc.com/D1243 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187614 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Malea 7 years ago
4 changed file(s) with 74 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
2020 class MCRegisterInfo;
2121 class StringRef;
2222
23 namespace HexStyle {
24 enum Style {
25 C, ///< 0xff
26 Asm ///< 0ffh
27 };
28 }
29
2330 /// MCInstPrinter - This is an instance of a target assembly language printer
2431 /// that converts an MCInst to valid target assembly syntax.
2532 class MCInstPrinter {
4148 /// True if we are printing immediates as hex.
4249 bool PrintImmHex;
4350
51 /// Which style to use for printing hexadecimal values.
52 HexStyle::Style PrintHexStyle;
53
4454 /// Utility function for printing annotations.
4555 void printAnnotation(raw_ostream &OS, StringRef Annot);
4656 public:
4757 MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
4858 const MCRegisterInfo &mri)
4959 : CommentStream(0), MAI(mai), MII(mii), MRI(mri), AvailableFeatures(0),
50 UseMarkup(0), PrintImmHex(0) {}
60 UseMarkup(0), PrintImmHex(0), PrintHexStyle(HexStyle::C) {}
5161
5262 virtual ~MCInstPrinter();
5363
7989 bool getPrintImmHex() const { return PrintImmHex; }
8090 void setPrintImmHex(bool Value) { PrintImmHex = Value; }
8191
92 HexStyle::Style getPrintHexStyleHex() const { return PrintHexStyle; }
93 void setPrintImmHex(HexStyle::Style Value) { PrintHexStyle = Value; }
94
8295 /// Utility function to print immediates in decimal or hex.
83 format_object1 formatImm(const int64_t Value) const;
96 format_object1 formatImm(const int64_t Value) const { return PrintImmHex ? formatHex(Value) : formatDec(Value); }
97
98 /// Utility functions to print decimal/hexadecimal values.
99 format_object1 formatDec(const int64_t Value) const;
100 format_object1 formatHex(const int64_t Value) const;
101 format_object1 formatHex(const uint64_t Value) const;
84102 };
85103
86104 } // namespace llvm
5151 return b;
5252 }
5353
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%" PRIx64, Value);
58 else
59 return format("%" PRId64, Value);
54 // For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
55 static bool needsLeadingZero(uint64_t Value)
56 {
57 while(Value)
58 {
59 uint64_t digit = (Value >> 60) & 0xf;
60 if (digit != 0)
61 return (digit >= 0xa);
62 Value <<= 4;
63 }
64 return false;
6065 }
66
67 format_object1 MCInstPrinter::formatDec(const int64_t Value) const {
68 return format("%" PRId64, Value);
69 }
70
71 format_object1 MCInstPrinter::formatHex(const int64_t Value) const {
72 switch(PrintHexStyle) {
73 case HexStyle::C:
74 if (Value < 0)
75 return format("-0x%" PRIx64, -Value);
76 else
77 return format("0x%" PRIx64, Value);
78 case HexStyle::Asm:
79 if (Value < 0) {
80 if (needsLeadingZero((uint64_t)(-Value)))
81 return format("-0%" PRIx64 "h", -Value);
82 else
83 return format("-%" PRIx64 "h", -Value);
84 } else {
85 if (needsLeadingZero((uint64_t)(Value)))
86 return format("0%" PRIx64 "h", Value);
87 else
88 return format("%" PRIx64 "h", Value);
89 }
90 }
91 }
92
93 format_object1 MCInstPrinter::formatHex(const uint64_t Value) const {
94 switch(PrintHexStyle) {
95 case HexStyle::C:
96 return format("0x%" PRIx64, Value);
97 case HexStyle::Asm:
98 if (needsLeadingZero(Value))
99 return format("0%" PRIx64 "h", Value);
100 else
101 return format("%" PRIx64 "h", Value);
102 }
103 }
138138 const MCConstantExpr *BranchTarget = dyn_cast(Op.getExpr());
139139 int64_t Address;
140140 if (BranchTarget && BranchTarget->EvaluateAsAbsolute(Address)) {
141 O << "0x";
142 O.write_hex(Address);
141 O << formatHex((uint64_t)Address);
143142 }
144143 else {
145144 // Otherwise, just print the expression.
118118 raw_ostream &O) {
119119 const MCOperand &Op = MI->getOperand(OpNo);
120120 if (Op.isImm())
121 O << Op.getImm();
121 O << formatImm(Op.getImm());
122122 else {
123123 assert(Op.isExpr() && "unknown pcrel immediate operand");
124124 // If a symbolic branch target was added as a constant expression then print
126126 const MCConstantExpr *BranchTarget = dyn_cast(Op.getExpr());
127127 int64_t Address;
128128 if (BranchTarget && BranchTarget->EvaluateAsAbsolute(Address)) {
129 O << "0x";
130 O.write_hex(Address);
129 O << formatHex((uint64_t)Address);
131130 }
132131 else {
133132 // Otherwise, just print the expression.
142141 if (Op.isReg()) {
143142 printRegName(O, Op.getReg());
144143 } else if (Op.isImm()) {
145 O << Op.getImm();
144 O << formatImm((int64_t)Op.getImm());
146145 } else {
147146 assert(Op.isExpr() && "unknown operand kind in printOperand");
148147 O << *Op.getExpr();
194193 DispVal = -DispVal;
195194 }
196195 }
197 O << DispVal;
196 O << formatImm(DispVal);
198197 }
199198 }
200199