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