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" |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FormattedStream.h" |
Chris Lattner | cae05cb | 2009-09-13 19:30:11 +0000 | [diff] [blame^] | 22 | #include "llvm/Target/TargetRegisterInfo.h" // FIXME: REMOVE. |
Chris Lattner | fadc83c | 2009-06-19 00:47:59 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 25 | // Include the auto-generated portion of the assembly writer. |
| 26 | #define MachineInstr MCInst |
| 27 | #define NO_ASM_WRITER_BOILERPLATE |
| 28 | #include "X86GenAsmWriter.inc" |
| 29 | #undef MachineInstr |
| 30 | |
Chris Lattner | cae05cb | 2009-09-13 19:30:11 +0000 | [diff] [blame^] | 31 | void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op) { |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 32 | switch (MI->getOperand(Op).getImm()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 33 | default: llvm_unreachable("Invalid ssecc argument!"); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 34 | case 0: O << "eq"; break; |
| 35 | case 1: O << "lt"; break; |
| 36 | case 2: O << "le"; break; |
| 37 | case 3: O << "unord"; break; |
| 38 | case 4: O << "neq"; break; |
| 39 | case 5: O << "nlt"; break; |
| 40 | case 6: O << "nle"; break; |
| 41 | case 7: O << "ord"; break; |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
| 45 | |
Chris Lattner | cae05cb | 2009-09-13 19:30:11 +0000 | [diff] [blame^] | 46 | void X86ATTInstPrinter::printPICLabel(const MCInst *MI, unsigned Op) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 47 | llvm_unreachable("This is only used for MOVPC32r," |
Chris Lattner | cae05cb | 2009-09-13 19:30:11 +0000 | [diff] [blame^] | 48 | "should lower before instruction printing!"); |
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 |
| 53 | /// being encoded as a pc-relative value. These print slightly differently, for |
| 54 | /// example, a $ is not emitted. |
Chris Lattner | cae05cb | 2009-09-13 19:30:11 +0000 | [diff] [blame^] | 55 | void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo) { |
Chris Lattner | 7680e73 | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 56 | const MCOperand &Op = MI->getOperand(OpNo); |
| 57 | |
| 58 | if (Op.isImm()) |
| 59 | O << Op.getImm(); |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 60 | else if (Op.isExpr()) |
Chris Lattner | 684c593d | 2009-09-03 05:46:51 +0000 | [diff] [blame] | 61 | Op.getExpr()->print(O, MAI); |
Chris Lattner | 7680e73 | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 62 | else |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 63 | llvm_unreachable("Unknown pcrel immediate operand"); |
Chris Lattner | 7680e73 | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | |
Chris Lattner | cae05cb | 2009-09-13 19:30:11 +0000 | [diff] [blame^] | 67 | void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 68 | const char *Modifier) { |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 69 | assert(Modifier == 0 && "Modifiers should not be used"); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 70 | |
| 71 | const MCOperand &Op = MI->getOperand(OpNo); |
| 72 | if (Op.isReg()) { |
| 73 | O << '%'; |
| 74 | unsigned Reg = Op.getReg(); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 75 | O << TRI->getAsmName(Reg); |
| 76 | return; |
| 77 | } else if (Op.isImm()) { |
Chris Lattner | 3de47b8 | 2009-09-09 00:40:31 +0000 | [diff] [blame] | 78 | O << '$' << Op.getImm(); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 79 | return; |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 80 | } else if (Op.isExpr()) { |
Daniel Dunbar | 61466c5 | 2009-08-14 03:42:12 +0000 | [diff] [blame] | 81 | O << '$'; |
Chris Lattner | 684c593d | 2009-09-03 05:46:51 +0000 | [diff] [blame] | 82 | Op.getExpr()->print(O, MAI); |
Daniel Dunbar | 61466c5 | 2009-08-14 03:42:12 +0000 | [diff] [blame] | 83 | return; |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | O << "<<UNKNOWN OPERAND KIND>>"; |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Chris Lattner | cae05cb | 2009-09-13 19:30:11 +0000 | [diff] [blame^] | 89 | void X86ATTInstPrinter::printLeaMemReference(const MCInst *MI, unsigned Op) { |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 90 | const MCOperand &BaseReg = MI->getOperand(Op); |
| 91 | const MCOperand &IndexReg = MI->getOperand(Op+2); |
| 92 | const MCOperand &DispSpec = MI->getOperand(Op+3); |
| 93 | |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 94 | if (DispSpec.isImm()) { |
| 95 | int64_t DispVal = DispSpec.getImm(); |
| 96 | if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) |
| 97 | O << DispVal; |
| 98 | } else { |
Chris Lattner | 3de47b8 | 2009-09-09 00:40:31 +0000 | [diff] [blame] | 99 | assert(DispSpec.isExpr() && "non-immediate displacement for LEA?"); |
| 100 | DispSpec.getExpr()->print(O, MAI); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | if (IndexReg.getReg() || BaseReg.getReg()) { |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 104 | O << '('; |
| 105 | if (BaseReg.getReg()) |
Chris Lattner | dc479f6 | 2009-06-20 07:59:10 +0000 | [diff] [blame] | 106 | printOperand(MI, Op); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 107 | |
| 108 | if (IndexReg.getReg()) { |
| 109 | O << ','; |
Chris Lattner | dc479f6 | 2009-06-20 07:59:10 +0000 | [diff] [blame] | 110 | printOperand(MI, Op+2); |
Chris Lattner | 7f8217f | 2009-06-20 08:13:12 +0000 | [diff] [blame] | 111 | unsigned ScaleVal = MI->getOperand(Op+1).getImm(); |
| 112 | if (ScaleVal != 1) |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 113 | O << ',' << ScaleVal; |
| 114 | } |
| 115 | O << ')'; |
| 116 | } |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chris Lattner | cae05cb | 2009-09-13 19:30:11 +0000 | [diff] [blame^] | 119 | void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op) { |
Chris Lattner | ad48be0 | 2009-06-20 00:50:32 +0000 | [diff] [blame] | 120 | const MCOperand &Segment = MI->getOperand(Op+4); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 121 | if (Segment.getReg()) { |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 122 | printOperand(MI, Op+4); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 123 | O << ':'; |
| 124 | } |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 125 | printLeaMemReference(MI, Op); |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 126 | } |