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 | fadc83c | 2009-06-19 00:47:59 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCInst.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCExpr.h" |
Torok Edwin | ab7c09b | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 5d672cf | 2010-02-10 00:10:18 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Format.h" |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FormattedStream.h" |
Shantonu Sen | 558b79a | 2009-09-18 20:35:59 +0000 | [diff] [blame] | 23 | #include "X86GenInstrNames.inc" |
Chris Lattner | fadc83c | 2009-06-19 00:47:59 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 26 | // Include the auto-generated portion of the assembly writer. |
| 27 | #define MachineInstr MCInst |
Chris Lattner | 0d7b0aa | 2010-02-11 22:57:32 +0000 | [diff] [blame^] | 28 | #define GET_INSTRUCTION_NAME |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 29 | #include "X86GenAsmWriter.inc" |
| 30 | #undef MachineInstr |
| 31 | |
Chris Lattner | c493fb2 | 2009-09-14 01:49:26 +0000 | [diff] [blame] | 32 | void X86ATTInstPrinter::printInst(const MCInst *MI) { printInstruction(MI); } |
Chris Lattner | 0d7b0aa | 2010-02-11 22:57:32 +0000 | [diff] [blame^] | 33 | StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const { |
| 34 | return getInstructionName(Opcode); |
| 35 | } |
| 36 | |
Chris Lattner | c493fb2 | 2009-09-14 01:49:26 +0000 | [diff] [blame] | 37 | |
Chris Lattner | cae05cb | 2009-09-13 19:30:11 +0000 | [diff] [blame] | 38 | void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op) { |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 39 | switch (MI->getOperand(Op).getImm()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 40 | default: llvm_unreachable("Invalid ssecc argument!"); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 41 | case 0: O << "eq"; break; |
| 42 | case 1: O << "lt"; break; |
| 43 | case 2: O << "le"; break; |
| 44 | case 3: O << "unord"; break; |
| 45 | case 4: O << "neq"; break; |
| 46 | case 5: O << "nlt"; break; |
| 47 | case 6: O << "nle"; break; |
| 48 | case 7: O << "ord"; break; |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Chris Lattner | 7680e73 | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 52 | /// 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] | 53 | /// being encoded as a pc-relative value (e.g. for jumps and calls). These |
| 54 | /// print slightly differently than normal immediates. For example, a $ is not |
| 55 | /// emitted. |
Chris Lattner | cae05cb | 2009-09-13 19:30:11 +0000 | [diff] [blame] | 56 | void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo) { |
Chris Lattner | 7680e73 | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 57 | const MCOperand &Op = MI->getOperand(OpNo); |
Chris Lattner | 7680e73 | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 58 | if (Op.isImm()) |
Chris Lattner | ffc0574 | 2009-12-22 00:44:05 +0000 | [diff] [blame] | 59 | // Print this as a signed 32-bit value. |
| 60 | O << (int)Op.getImm(); |
Chris Lattner | f92c95f | 2009-09-14 01:34:40 +0000 | [diff] [blame] | 61 | else { |
| 62 | assert(Op.isExpr() && "unknown pcrel immediate operand"); |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 63 | O << *Op.getExpr(); |
Chris Lattner | f92c95f | 2009-09-14 01:34:40 +0000 | [diff] [blame] | 64 | } |
Chris Lattner | 7680e73 | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Chris Lattner | 172862a | 2009-10-19 19:51:42 +0000 | [diff] [blame] | 67 | void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo) { |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 68 | |
| 69 | const MCOperand &Op = MI->getOperand(OpNo); |
| 70 | if (Op.isReg()) { |
Chris Lattner | c510f4c | 2009-09-13 20:15:16 +0000 | [diff] [blame] | 71 | O << '%' << getRegisterName(Op.getReg()); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 72 | } else if (Op.isImm()) { |
Chris Lattner | 3de47b8 | 2009-09-09 00:40:31 +0000 | [diff] [blame] | 73 | O << '$' << Op.getImm(); |
Chris Lattner | 5d672cf | 2010-02-10 00:10:18 +0000 | [diff] [blame] | 74 | |
| 75 | if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256)) |
| 76 | *CommentStream << format("imm = 0x%X\n", Op.getImm()); |
| 77 | |
Chris Lattner | f92c95f | 2009-09-14 01:34:40 +0000 | [diff] [blame] | 78 | } else { |
| 79 | assert(Op.isExpr() && "unknown operand kind in printOperand"); |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 80 | O << '$' << *Op.getExpr(); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 81 | } |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Chris Lattner | cae05cb | 2009-09-13 19:30:11 +0000 | [diff] [blame] | 84 | void X86ATTInstPrinter::printLeaMemReference(const MCInst *MI, unsigned Op) { |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 85 | const MCOperand &BaseReg = MI->getOperand(Op); |
| 86 | const MCOperand &IndexReg = MI->getOperand(Op+2); |
| 87 | const MCOperand &DispSpec = MI->getOperand(Op+3); |
| 88 | |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 89 | if (DispSpec.isImm()) { |
| 90 | int64_t DispVal = DispSpec.getImm(); |
| 91 | if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) |
| 92 | O << DispVal; |
| 93 | } else { |
Chris Lattner | 3de47b8 | 2009-09-09 00:40:31 +0000 | [diff] [blame] | 94 | assert(DispSpec.isExpr() && "non-immediate displacement for LEA?"); |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 95 | O << *DispSpec.getExpr(); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | if (IndexReg.getReg() || BaseReg.getReg()) { |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 99 | O << '('; |
| 100 | if (BaseReg.getReg()) |
Chris Lattner | dc479f6 | 2009-06-20 07:59:10 +0000 | [diff] [blame] | 101 | printOperand(MI, Op); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 102 | |
| 103 | if (IndexReg.getReg()) { |
| 104 | O << ','; |
Chris Lattner | dc479f6 | 2009-06-20 07:59:10 +0000 | [diff] [blame] | 105 | printOperand(MI, Op+2); |
Chris Lattner | 7f8217f | 2009-06-20 08:13:12 +0000 | [diff] [blame] | 106 | unsigned ScaleVal = MI->getOperand(Op+1).getImm(); |
| 107 | if (ScaleVal != 1) |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 108 | O << ',' << ScaleVal; |
| 109 | } |
| 110 | O << ')'; |
| 111 | } |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | cae05cb | 2009-09-13 19:30:11 +0000 | [diff] [blame] | 114 | void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op) { |
Chris Lattner | f92c95f | 2009-09-14 01:34:40 +0000 | [diff] [blame] | 115 | // If this has a segment register, print it. |
| 116 | if (MI->getOperand(Op+4).getReg()) { |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 117 | printOperand(MI, Op+4); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 118 | O << ':'; |
| 119 | } |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 120 | printLeaMemReference(MI, Op); |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 121 | } |