I have manually decoded the imm field of an insertps one too many
[oota-llvm.git] / lib / Target / X86 / AsmPrinter / X86ATTInstPrinter.cpp
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 "X86ATTInstPrinter.h"
17 #include "X86InstComments.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/FormattedStream.h"
24 #include "X86GenInstrNames.inc"
25 using namespace llvm;
26
27 // Include the auto-generated portion of the assembly writer.
28 #define MachineInstr MCInst
29 #define GET_INSTRUCTION_NAME
30 #include "X86GenAsmWriter.inc"
31 #undef MachineInstr
32
33 void X86ATTInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) {
34   printInstruction(MI, OS);
35   
36   // If verbose assembly is enabled, we can print some informative comments.
37   if (CommentStream)
38     EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
39 }
40 StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const {
41   return getInstructionName(Opcode);
42 }
43
44
45 void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
46                                    raw_ostream &O) {
47   switch (MI->getOperand(Op).getImm()) {
48   default: assert(0 && "Invalid ssecc argument!");
49   case 0: O << "eq"; break;
50   case 1: O << "lt"; break;
51   case 2: O << "le"; break;
52   case 3: O << "unord"; break;
53   case 4: O << "neq"; break;
54   case 5: O << "nlt"; break;
55   case 6: O << "nle"; break;
56   case 7: O << "ord"; break;
57   }
58 }
59
60 /// print_pcrel_imm - This is used to print an immediate value that ends up
61 /// being encoded as a pc-relative value (e.g. for jumps and calls).  These
62 /// print slightly differently than normal immediates.  For example, a $ is not
63 /// emitted.
64 void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
65                                         raw_ostream &O) {
66   const MCOperand &Op = MI->getOperand(OpNo);
67   if (Op.isImm())
68     // Print this as a signed 32-bit value.
69     O << (int)Op.getImm();
70   else {
71     assert(Op.isExpr() && "unknown pcrel immediate operand");
72     O << *Op.getExpr();
73   }
74 }
75
76 void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
77                                      raw_ostream &O) {
78   const MCOperand &Op = MI->getOperand(OpNo);
79   if (Op.isReg()) {
80     O << '%' << getRegisterName(Op.getReg());
81   } else if (Op.isImm()) {
82     O << '$' << Op.getImm();
83     
84     if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256))
85       *CommentStream << format("imm = 0x%llX\n", (long long)Op.getImm());
86     
87   } else {
88     assert(Op.isExpr() && "unknown operand kind in printOperand");
89     O << '$' << *Op.getExpr();
90   }
91 }
92
93 void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
94                                           raw_ostream &O) {
95   const MCOperand &BaseReg  = MI->getOperand(Op);
96   const MCOperand &IndexReg = MI->getOperand(Op+2);
97   const MCOperand &DispSpec = MI->getOperand(Op+3);
98   const MCOperand &SegReg = MI->getOperand(Op+4);
99   
100   // If this has a segment register, print it.
101   if (SegReg.getReg()) {
102     printOperand(MI, Op+4, O);
103     O << ':';
104   }
105   
106   if (DispSpec.isImm()) {
107     int64_t DispVal = DispSpec.getImm();
108     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
109       O << DispVal;
110   } else {
111     assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
112     O << *DispSpec.getExpr();
113   }
114   
115   if (IndexReg.getReg() || BaseReg.getReg()) {
116     O << '(';
117     if (BaseReg.getReg())
118       printOperand(MI, Op, O);
119     
120     if (IndexReg.getReg()) {
121       O << ',';
122       printOperand(MI, Op+2, O);
123       unsigned ScaleVal = MI->getOperand(Op+1).getImm();
124       if (ScaleVal != 1)
125         O << ',' << ScaleVal;
126     }
127     O << ')';
128   }
129 }