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