Chris Lattner | fadc83c | 2009-06-19 00:47:59 +0000 | [diff] [blame] | 1 | //===-- X86ATTInstPrinter.cpp - AT&T assembly instruction printing --------===// |
| 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" |
Chris Lattner | cae05cb | 2009-09-13 19:30:11 +0000 | [diff] [blame] | 16 | #include "X86ATTInstPrinter.h" |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 17 | #include "X86InstComments.h" |
Evan Cheng | ed5e355 | 2011-07-06 22:01:53 +0000 | [diff] [blame] | 18 | #include "MCTargetDesc/X86MCTargetDesc.h" |
Chris Lattner | fadc83c | 2009-06-19 00:47:59 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCInst.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCExpr.h" |
Torok Edwin | ab7c09b | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 5d672cf | 2010-02-10 00:10:18 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Format.h" |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FormattedStream.h" |
Bill Wendling | 44dcfd3 | 2011-04-07 21:20:06 +0000 | [diff] [blame] | 25 | #include <map> |
Chris Lattner | fadc83c | 2009-06-19 00:47:59 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 28 | // Include the auto-generated portion of the assembly writer. |
Chris Lattner | 0d7b0aa | 2010-02-11 22:57:32 +0000 | [diff] [blame] | 29 | #define GET_INSTRUCTION_NAME |
Bill Wendling | 44dcfd3 | 2011-04-07 21:20:06 +0000 | [diff] [blame] | 30 | #define PRINT_ALIAS_INSTR |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 31 | #include "X86GenAsmWriter.inc" |
Bill Wendling | 44dcfd3 | 2011-04-07 21:20:06 +0000 | [diff] [blame] | 32 | |
Evan Cheng | b262799 | 2011-07-06 19:45:42 +0000 | [diff] [blame] | 33 | X86ATTInstPrinter::X86ATTInstPrinter(const MCAsmInfo &MAI) |
Bill Wendling | 44dcfd3 | 2011-04-07 21:20:06 +0000 | [diff] [blame] | 34 | : MCInstPrinter(MAI) { |
Bill Wendling | 44dcfd3 | 2011-04-07 21:20:06 +0000 | [diff] [blame] | 35 | } |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 36 | |
Rafael Espindola | cde4ce4 | 2011-06-02 02:34:55 +0000 | [diff] [blame] | 37 | void X86ATTInstPrinter::printRegName(raw_ostream &OS, |
| 38 | unsigned RegNo) const { |
| 39 | OS << '%' << getRegisterName(RegNo); |
Rafael Espindola | 6e03294 | 2011-05-30 20:20:15 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Chris Lattner | d374087 | 2010-04-04 05:04:31 +0000 | [diff] [blame] | 42 | void X86ATTInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) { |
Eric Christopher | 721ef66 | 2011-04-18 21:28:11 +0000 | [diff] [blame] | 43 | // Try to print any aliases first. |
| 44 | if (!printAliasInstr(MI, OS)) |
Bill Wendling | c6df988 | 2011-04-14 01:11:51 +0000 | [diff] [blame] | 45 | printInstruction(MI, OS); |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 46 | |
| 47 | // If verbose assembly is enabled, we can print some informative comments. |
| 48 | if (CommentStream) |
| 49 | EmitAnyX86InstComments(MI, *CommentStream, getRegisterName); |
Chris Lattner | 35c33bd | 2010-04-04 04:47:45 +0000 | [diff] [blame] | 50 | } |
Bill Wendling | 44dcfd3 | 2011-04-07 21:20:06 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 0d7b0aa | 2010-02-11 22:57:32 +0000 | [diff] [blame] | 52 | StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const { |
| 53 | return getInstructionName(Opcode); |
| 54 | } |
| 55 | |
Chris Lattner | 35c33bd | 2010-04-04 04:47:45 +0000 | [diff] [blame] | 56 | void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op, |
| 57 | raw_ostream &O) { |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 58 | switch (MI->getOperand(Op).getImm()) { |
Chris Lattner | 35c33bd | 2010-04-04 04:47:45 +0000 | [diff] [blame] | 59 | default: assert(0 && "Invalid ssecc argument!"); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 60 | case 0: O << "eq"; break; |
| 61 | case 1: O << "lt"; break; |
| 62 | case 2: O << "le"; break; |
| 63 | case 3: O << "unord"; break; |
| 64 | case 4: O << "neq"; break; |
| 65 | case 5: O << "nlt"; break; |
| 66 | case 6: O << "nle"; break; |
| 67 | case 7: O << "ord"; break; |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Chris Lattner | 7680e73 | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 71 | /// print_pcrel_imm - This is used to print an immediate value that ends up |
Chris Lattner | ffc0574 | 2009-12-22 00:44:05 +0000 | [diff] [blame] | 72 | /// being encoded as a pc-relative value (e.g. for jumps and calls). These |
| 73 | /// print slightly differently than normal immediates. For example, a $ is not |
| 74 | /// emitted. |
Chris Lattner | 35c33bd | 2010-04-04 04:47:45 +0000 | [diff] [blame] | 75 | void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo, |
| 76 | raw_ostream &O) { |
Chris Lattner | 7680e73 | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 77 | const MCOperand &Op = MI->getOperand(OpNo); |
Chris Lattner | 7680e73 | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 78 | if (Op.isImm()) |
Chris Lattner | ffc0574 | 2009-12-22 00:44:05 +0000 | [diff] [blame] | 79 | // Print this as a signed 32-bit value. |
| 80 | O << (int)Op.getImm(); |
Chris Lattner | f92c95f | 2009-09-14 01:34:40 +0000 | [diff] [blame] | 81 | else { |
| 82 | assert(Op.isExpr() && "unknown pcrel immediate operand"); |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 83 | O << *Op.getExpr(); |
Chris Lattner | f92c95f | 2009-09-14 01:34:40 +0000 | [diff] [blame] | 84 | } |
Chris Lattner | 7680e73 | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Lattner | 35c33bd | 2010-04-04 04:47:45 +0000 | [diff] [blame] | 87 | void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, |
| 88 | raw_ostream &O) { |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 89 | const MCOperand &Op = MI->getOperand(OpNo); |
| 90 | if (Op.isReg()) { |
Chris Lattner | c510f4c | 2009-09-13 20:15:16 +0000 | [diff] [blame] | 91 | O << '%' << getRegisterName(Op.getReg()); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 92 | } else if (Op.isImm()) { |
Chris Lattner | 3de47b8 | 2009-09-09 00:40:31 +0000 | [diff] [blame] | 93 | O << '$' << Op.getImm(); |
Chris Lattner | 5d672cf | 2010-02-10 00:10:18 +0000 | [diff] [blame] | 94 | |
| 95 | if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256)) |
Dan Gohman | 36b01cb | 2010-02-17 00:37:20 +0000 | [diff] [blame] | 96 | *CommentStream << format("imm = 0x%llX\n", (long long)Op.getImm()); |
Chris Lattner | 5d672cf | 2010-02-10 00:10:18 +0000 | [diff] [blame] | 97 | |
Chris Lattner | f92c95f | 2009-09-14 01:34:40 +0000 | [diff] [blame] | 98 | } else { |
| 99 | assert(Op.isExpr() && "unknown operand kind in printOperand"); |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 100 | O << '$' << *Op.getExpr(); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 101 | } |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chris Lattner | 599b531 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 104 | void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op, |
| 105 | raw_ostream &O) { |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 106 | const MCOperand &BaseReg = MI->getOperand(Op); |
| 107 | const MCOperand &IndexReg = MI->getOperand(Op+2); |
| 108 | const MCOperand &DispSpec = MI->getOperand(Op+3); |
Chris Lattner | 599b531 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 109 | const MCOperand &SegReg = MI->getOperand(Op+4); |
| 110 | |
| 111 | // If this has a segment register, print it. |
| 112 | if (SegReg.getReg()) { |
| 113 | printOperand(MI, Op+4, O); |
| 114 | O << ':'; |
| 115 | } |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 116 | |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 117 | if (DispSpec.isImm()) { |
| 118 | int64_t DispVal = DispSpec.getImm(); |
| 119 | if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) |
| 120 | O << DispVal; |
| 121 | } else { |
Chris Lattner | 3de47b8 | 2009-09-09 00:40:31 +0000 | [diff] [blame] | 122 | assert(DispSpec.isExpr() && "non-immediate displacement for LEA?"); |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 123 | O << *DispSpec.getExpr(); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | if (IndexReg.getReg() || BaseReg.getReg()) { |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 127 | O << '('; |
| 128 | if (BaseReg.getReg()) |
Chris Lattner | 35c33bd | 2010-04-04 04:47:45 +0000 | [diff] [blame] | 129 | printOperand(MI, Op, O); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 130 | |
| 131 | if (IndexReg.getReg()) { |
| 132 | O << ','; |
Chris Lattner | 35c33bd | 2010-04-04 04:47:45 +0000 | [diff] [blame] | 133 | printOperand(MI, Op+2, O); |
Chris Lattner | 7f8217f | 2009-06-20 08:13:12 +0000 | [diff] [blame] | 134 | unsigned ScaleVal = MI->getOperand(Op+1).getImm(); |
| 135 | if (ScaleVal != 1) |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 136 | O << ',' << ScaleVal; |
| 137 | } |
| 138 | O << ')'; |
| 139 | } |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 140 | } |