Chris Lattner | b3c8547 | 2009-09-20 07:28:26 +0000 | [diff] [blame] | 1 | //===-- X86IntelInstPrinter.cpp - AT&T assembly instruction printing ------===// |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file includes code for rendering MCInst instances as AT&T-style |
| 11 | // assembly. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "asm-printer" |
| 16 | #include "X86IntelInstPrinter.h" |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 17 | #include "X86InstComments.h" |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCInst.h" |
| 19 | #include "llvm/MC/MCAsmInfo.h" |
| 20 | #include "llvm/MC/MCExpr.h" |
| 21 | #include "llvm/Support/ErrorHandling.h" |
| 22 | #include "llvm/Support/FormattedStream.h" |
| 23 | #include "X86GenInstrNames.inc" |
Douglas Gregor | e735882 | 2011-01-17 19:17:01 +0000 | [diff] [blame^] | 24 | #include <cctype> |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | // Include the auto-generated portion of the assembly writer. |
Chris Lattner | 0d7b0aa | 2010-02-11 22:57:32 +0000 | [diff] [blame] | 28 | #define GET_INSTRUCTION_NAME |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 29 | #include "X86GenAsmWriter1.inc" |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 30 | |
Chris Lattner | d374087 | 2010-04-04 05:04:31 +0000 | [diff] [blame] | 31 | void X86IntelInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) { |
| 32 | printInstruction(MI, OS); |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 33 | |
| 34 | // If verbose assembly is enabled, we can print some informative comments. |
| 35 | if (CommentStream) |
| 36 | EmitAnyX86InstComments(MI, *CommentStream, getRegisterName); |
Chris Lattner | 35c33bd | 2010-04-04 04:47:45 +0000 | [diff] [blame] | 37 | } |
Chris Lattner | 0d7b0aa | 2010-02-11 22:57:32 +0000 | [diff] [blame] | 38 | StringRef X86IntelInstPrinter::getOpcodeName(unsigned Opcode) const { |
| 39 | return getInstructionName(Opcode); |
| 40 | } |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 35c33bd | 2010-04-04 04:47:45 +0000 | [diff] [blame] | 42 | void X86IntelInstPrinter::printSSECC(const MCInst *MI, unsigned Op, |
| 43 | raw_ostream &O) { |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 44 | switch (MI->getOperand(Op).getImm()) { |
Chris Lattner | 35c33bd | 2010-04-04 04:47:45 +0000 | [diff] [blame] | 45 | default: assert(0 && "Invalid ssecc argument!"); |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 46 | case 0: O << "eq"; break; |
| 47 | case 1: O << "lt"; break; |
| 48 | case 2: O << "le"; break; |
| 49 | case 3: O << "unord"; break; |
| 50 | case 4: O << "neq"; break; |
| 51 | case 5: O << "nlt"; break; |
| 52 | case 6: O << "nle"; break; |
| 53 | case 7: O << "ord"; break; |
| 54 | } |
| 55 | } |
| 56 | |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 57 | /// print_pcrel_imm - This is used to print an immediate value that ends up |
Chris Lattner | 7057641 | 2009-09-20 07:47:59 +0000 | [diff] [blame] | 58 | /// being encoded as a pc-relative value. |
Chris Lattner | 35c33bd | 2010-04-04 04:47:45 +0000 | [diff] [blame] | 59 | void X86IntelInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo, |
| 60 | raw_ostream &O) { |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 61 | const MCOperand &Op = MI->getOperand(OpNo); |
| 62 | if (Op.isImm()) |
| 63 | O << Op.getImm(); |
| 64 | else { |
| 65 | assert(Op.isExpr() && "unknown pcrel immediate operand"); |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 66 | O << *Op.getExpr(); |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | static void PrintRegName(raw_ostream &O, StringRef RegName) { |
| 71 | for (unsigned i = 0, e = RegName.size(); i != e; ++i) |
| 72 | O << (char)toupper(RegName[i]); |
| 73 | } |
| 74 | |
| 75 | void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, |
Chris Lattner | 35c33bd | 2010-04-04 04:47:45 +0000 | [diff] [blame] | 76 | raw_ostream &O) { |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 77 | const MCOperand &Op = MI->getOperand(OpNo); |
| 78 | if (Op.isReg()) { |
| 79 | PrintRegName(O, getRegisterName(Op.getReg())); |
| 80 | } else if (Op.isImm()) { |
| 81 | O << Op.getImm(); |
| 82 | } else { |
| 83 | assert(Op.isExpr() && "unknown operand kind in printOperand"); |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 84 | O << *Op.getExpr(); |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Chris Lattner | 599b531 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 88 | void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op, |
| 89 | raw_ostream &O) { |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 90 | const MCOperand &BaseReg = MI->getOperand(Op); |
| 91 | unsigned ScaleVal = MI->getOperand(Op+1).getImm(); |
| 92 | const MCOperand &IndexReg = MI->getOperand(Op+2); |
| 93 | const MCOperand &DispSpec = MI->getOperand(Op+3); |
Chris Lattner | 599b531 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 94 | const MCOperand &SegReg = MI->getOperand(Op+4); |
| 95 | |
| 96 | // If this has a segment register, print it. |
| 97 | if (SegReg.getReg()) { |
| 98 | printOperand(MI, Op+4, O); |
| 99 | O << ':'; |
| 100 | } |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 101 | |
| 102 | O << '['; |
| 103 | |
| 104 | bool NeedPlus = false; |
| 105 | if (BaseReg.getReg()) { |
Chris Lattner | 35c33bd | 2010-04-04 04:47:45 +0000 | [diff] [blame] | 106 | printOperand(MI, Op, O); |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 107 | NeedPlus = true; |
| 108 | } |
| 109 | |
| 110 | if (IndexReg.getReg()) { |
| 111 | if (NeedPlus) O << " + "; |
| 112 | if (ScaleVal != 1) |
| 113 | O << ScaleVal << '*'; |
Chris Lattner | 35c33bd | 2010-04-04 04:47:45 +0000 | [diff] [blame] | 114 | printOperand(MI, Op+2, O); |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 115 | NeedPlus = true; |
| 116 | } |
| 117 | |
Chris Lattner | 599b531 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 118 | |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 119 | if (!DispSpec.isImm()) { |
| 120 | if (NeedPlus) O << " + "; |
| 121 | assert(DispSpec.isExpr() && "non-immediate displacement for LEA?"); |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 122 | O << *DispSpec.getExpr(); |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 123 | } else { |
| 124 | int64_t DispVal = DispSpec.getImm(); |
| 125 | if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) { |
| 126 | if (NeedPlus) { |
| 127 | if (DispVal > 0) |
| 128 | O << " + "; |
| 129 | else { |
| 130 | O << " - "; |
| 131 | DispVal = -DispVal; |
| 132 | } |
| 133 | } |
| 134 | O << DispVal; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | O << ']'; |
| 139 | } |