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" |
| 16 | #include "llvm/MC/MCInst.h" |
| 17 | #include "X86ATTAsmPrinter.h" |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetAsmInfo.h" |
Torok Edwin | ab7c09b | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 20 | #include "llvm/Support/FormattedStream.h" |
Chris Lattner | fadc83c | 2009-06-19 00:47:59 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 23 | // Include the auto-generated portion of the assembly writer. |
| 24 | #define MachineInstr MCInst |
| 25 | #define NO_ASM_WRITER_BOILERPLATE |
| 26 | #include "X86GenAsmWriter.inc" |
| 27 | #undef MachineInstr |
| 28 | |
| 29 | void X86ATTAsmPrinter::printSSECC(const MCInst *MI, unsigned Op) { |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 30 | switch (MI->getOperand(Op).getImm()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 31 | default: llvm_unreachable("Invalid ssecc argument!"); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 32 | case 0: O << "eq"; break; |
| 33 | case 1: O << "lt"; break; |
| 34 | case 2: O << "le"; break; |
| 35 | case 3: O << "unord"; break; |
| 36 | case 4: O << "neq"; break; |
| 37 | case 5: O << "nlt"; break; |
| 38 | case 6: O << "nle"; break; |
| 39 | case 7: O << "ord"; break; |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | |
| 44 | void X86ATTAsmPrinter::printPICLabel(const MCInst *MI, unsigned Op) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 45 | llvm_unreachable("This is only used for MOVPC32r," |
Torok Edwin | 481d15a | 2009-07-14 12:22:58 +0000 | [diff] [blame] | 46 | "should lower before asm printing!"); |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | |
Chris Lattner | 7680e73 | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 50 | /// print_pcrel_imm - This is used to print an immediate value that ends up |
| 51 | /// being encoded as a pc-relative value. These print slightly differently, for |
| 52 | /// example, a $ is not emitted. |
| 53 | void X86ATTAsmPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo) { |
| 54 | const MCOperand &Op = MI->getOperand(OpNo); |
| 55 | |
| 56 | if (Op.isImm()) |
| 57 | O << Op.getImm(); |
Daniel Dunbar | 61466c5 | 2009-08-14 03:42:12 +0000 | [diff] [blame^] | 58 | else if (Op.isMCValue()) |
| 59 | Op.getMCValue().print(O); |
Chris Lattner | 7680e73 | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 60 | else if (Op.isMBBLabel()) |
| 61 | // FIXME: Keep in sync with printBasicBlockLabel. printBasicBlockLabel |
| 62 | // should eventually call into this code, not the other way around. |
| 63 | O << TAI->getPrivateGlobalPrefix() << "BB" << Op.getMBBLabelFunction() |
| 64 | << '_' << Op.getMBBLabelBlock(); |
| 65 | else |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 66 | llvm_unreachable("Unknown pcrel immediate operand"); |
Chris Lattner | 7680e73 | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 70 | void X86ATTAsmPrinter::printOperand(const MCInst *MI, unsigned OpNo, |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 71 | const char *Modifier) { |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 72 | assert(Modifier == 0 && "Modifiers should not be used"); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 73 | |
| 74 | const MCOperand &Op = MI->getOperand(OpNo); |
| 75 | if (Op.isReg()) { |
| 76 | O << '%'; |
| 77 | unsigned Reg = Op.getReg(); |
| 78 | #if 0 |
| 79 | if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 80 | EVT VT = (strcmp(Modifier+6,"64") == 0) ? |
| 81 | EVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? EVT::i32 : |
| 82 | ((strcmp(Modifier+6,"16") == 0) ? EVT::i16 : EVT::i8)); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 83 | Reg = getX86SubSuperRegister(Reg, VT); |
| 84 | } |
| 85 | #endif |
| 86 | O << TRI->getAsmName(Reg); |
| 87 | return; |
| 88 | } else if (Op.isImm()) { |
Chris Lattner | 2f429e5 | 2009-06-21 01:48:49 +0000 | [diff] [blame] | 89 | //if (!Modifier || (strcmp(Modifier, "debug") && strcmp(Modifier, "mem"))) |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 90 | O << '$'; |
| 91 | O << Op.getImm(); |
| 92 | return; |
Daniel Dunbar | 61466c5 | 2009-08-14 03:42:12 +0000 | [diff] [blame^] | 93 | } else if (Op.isMCValue()) { |
| 94 | O << '$'; |
| 95 | Op.getMCValue().print(O); |
| 96 | return; |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | O << "<<UNKNOWN OPERAND KIND>>"; |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 102 | void X86ATTAsmPrinter::printLeaMemReference(const MCInst *MI, unsigned Op) { |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 103 | |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 104 | const MCOperand &BaseReg = MI->getOperand(Op); |
| 105 | const MCOperand &IndexReg = MI->getOperand(Op+2); |
| 106 | const MCOperand &DispSpec = MI->getOperand(Op+3); |
| 107 | |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 108 | if (DispSpec.isImm()) { |
| 109 | int64_t DispVal = DispSpec.getImm(); |
| 110 | if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) |
| 111 | O << DispVal; |
Daniel Dunbar | 61466c5 | 2009-08-14 03:42:12 +0000 | [diff] [blame^] | 112 | } else if (DispSpec.isMCValue()) { |
| 113 | DispSpec.getMCValue().print(O); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 114 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 115 | llvm_unreachable("non-immediate displacement for LEA?"); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 116 | //assert(DispSpec.isGlobal() || DispSpec.isCPI() || |
| 117 | // DispSpec.isJTI() || DispSpec.isSymbol()); |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 118 | //printOperand(MI, Op+3, "mem"); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | if (IndexReg.getReg() || BaseReg.getReg()) { |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 122 | // There are cases where we can end up with ESP/RSP in the indexreg slot. |
| 123 | // If this happens, swap the base/index register to support assemblers that |
| 124 | // don't work when the index is *SP. |
| 125 | // FIXME: REMOVE THIS. |
Chris Lattner | dc479f6 | 2009-06-20 07:59:10 +0000 | [diff] [blame] | 126 | assert(IndexReg.getReg() != X86::ESP && IndexReg.getReg() != X86::RSP); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 127 | |
| 128 | O << '('; |
| 129 | if (BaseReg.getReg()) |
Chris Lattner | dc479f6 | 2009-06-20 07:59:10 +0000 | [diff] [blame] | 130 | printOperand(MI, Op); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 131 | |
| 132 | if (IndexReg.getReg()) { |
| 133 | O << ','; |
Chris Lattner | dc479f6 | 2009-06-20 07:59:10 +0000 | [diff] [blame] | 134 | printOperand(MI, Op+2); |
Chris Lattner | 7f8217f | 2009-06-20 08:13:12 +0000 | [diff] [blame] | 135 | unsigned ScaleVal = MI->getOperand(Op+1).getImm(); |
| 136 | if (ScaleVal != 1) |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 137 | O << ',' << ScaleVal; |
| 138 | } |
| 139 | O << ')'; |
| 140 | } |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 143 | void X86ATTAsmPrinter::printMemReference(const MCInst *MI, unsigned Op) { |
Chris Lattner | ad48be0 | 2009-06-20 00:50:32 +0000 | [diff] [blame] | 144 | const MCOperand &Segment = MI->getOperand(Op+4); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 145 | if (Segment.getReg()) { |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 146 | printOperand(MI, Op+4); |
Chris Lattner | f38c03af | 2009-06-20 00:49:26 +0000 | [diff] [blame] | 147 | O << ':'; |
| 148 | } |
Chris Lattner | c124306 | 2009-06-20 07:03:18 +0000 | [diff] [blame] | 149 | printLeaMemReference(MI, Op); |
Chris Lattner | d5fb790 | 2009-06-19 23:59:57 +0000 | [diff] [blame] | 150 | } |