fix GetOrCreateTemporarySymbol to require a name, clients
[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) { printInstruction(MI); }
33 StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const {
34   return getInstructionName(Opcode);
35 }
36
37
38 void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op) {
39   switch (MI->getOperand(Op).getImm()) {
40   default: llvm_unreachable("Invalid ssecc argument!");
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;
49   }
50 }
51
52 /// print_pcrel_imm - This is used to print an immediate value that ends up
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.
56 void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo) {
57   const MCOperand &Op = MI->getOperand(OpNo);
58   if (Op.isImm())
59     // Print this as a signed 32-bit value.
60     O << (int)Op.getImm();
61   else {
62     assert(Op.isExpr() && "unknown pcrel immediate operand");
63     O << *Op.getExpr();
64   }
65 }
66
67 void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo) {
68   
69   const MCOperand &Op = MI->getOperand(OpNo);
70   if (Op.isReg()) {
71     O << '%' << getRegisterName(Op.getReg());
72   } else if (Op.isImm()) {
73     O << '$' << Op.getImm();
74     
75     if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256))
76       *CommentStream << format("imm = 0x%llX\n", (long long)Op.getImm());
77     
78   } else {
79     assert(Op.isExpr() && "unknown operand kind in printOperand");
80     O << '$' << *Op.getExpr();
81   }
82 }
83
84 void X86ATTInstPrinter::printLeaMemReference(const MCInst *MI, unsigned Op) {
85   const MCOperand &BaseReg  = MI->getOperand(Op);
86   const MCOperand &IndexReg = MI->getOperand(Op+2);
87   const MCOperand &DispSpec = MI->getOperand(Op+3);
88   
89   if (DispSpec.isImm()) {
90     int64_t DispVal = DispSpec.getImm();
91     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
92       O << DispVal;
93   } else {
94     assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
95     O << *DispSpec.getExpr();
96   }
97   
98   if (IndexReg.getReg() || BaseReg.getReg()) {
99     O << '(';
100     if (BaseReg.getReg())
101       printOperand(MI, Op);
102     
103     if (IndexReg.getReg()) {
104       O << ',';
105       printOperand(MI, Op+2);
106       unsigned ScaleVal = MI->getOperand(Op+1).getImm();
107       if (ScaleVal != 1)
108         O << ',' << ScaleVal;
109     }
110     O << ')';
111   }
112 }
113
114 void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op) {
115   // If this has a segment register, print it.
116   if (MI->getOperand(Op+4).getReg()) {
117     printOperand(MI, Op+4);
118     O << ':';
119   }
120   printLeaMemReference(MI, Op);
121 }