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