Percolate the call up to the right superclass
[oota-llvm.git] / lib / Target / X86 / X86IntelAsmPrinter.cpp
1 //===-- X86IntelAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to Intel format assembly language.
12 // This printer is the output mechanism used by `llc'.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "X86IntelAsmPrinter.h"
17 #include "X86.h"
18 #include "llvm/Module.h"
19 #include "llvm/Assembly/Writer.h"
20 #include "llvm/Support/Mangler.h"
21 using namespace llvm;
22 using namespace x86;
23
24 /// runOnMachineFunction - This uses the printMachineInstruction()
25 /// method to print assembly for each instruction.
26 ///
27 bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
28   setupMachineFunction(MF);
29   O << "\n\n";
30
31   // Print out constants referenced by the function
32   printConstantPool(MF.getConstantPool());
33
34   // Print out labels for the function.
35   O << "\t.text\n";
36   emitAlignment(4);
37   O << "\t.globl\t" << CurrentFnName << "\n";
38   if (!forCygwin && !forDarwin)
39     O << "\t.type\t" << CurrentFnName << ", @function\n";
40   O << CurrentFnName << ":\n";
41
42   // Print out code for the function.
43   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
44        I != E; ++I) {
45     // Print a label for the basic block if there are any predecessors.
46     if (I->pred_begin() != I->pred_end())
47       O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
48         << CommentString << " " << I->getBasicBlock()->getName() << "\n";
49     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
50          II != E; ++II) {
51       // Print the assembly for the instruction.
52       O << "\t";
53       printMachineInstruction(II);
54     }
55   }
56
57   // We didn't modify anything.
58   return false;
59 }
60
61 void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
62                                  bool elideOffsetKeyword /* = false */) {
63   const MRegisterInfo &RI = *TM.getRegisterInfo();
64   switch (MO.getType()) {
65   case MachineOperand::MO_VirtualRegister:
66     if (Value *V = MO.getVRegValueOrNull()) {
67       O << "<" << V->getName() << ">";
68       return;
69     }
70     // FALLTHROUGH
71   case MachineOperand::MO_MachineRegister:
72     if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
73       // Bug Workaround: See note in Printer::doInitialization about %.
74       O << "%" << RI.get(MO.getReg()).Name;
75     else
76       O << "%reg" << MO.getReg();
77     return;
78
79   case MachineOperand::MO_SignExtendedImmed:
80   case MachineOperand::MO_UnextendedImmed:
81     O << (int)MO.getImmedValue();
82     return;
83   case MachineOperand::MO_MachineBasicBlock: {
84     MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
85     O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
86       << "_" << MBBOp->getNumber () << "\t# "
87       << MBBOp->getBasicBlock ()->getName ();
88     return;
89   }
90   case MachineOperand::MO_PCRelativeDisp:
91     std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
92     abort ();
93     return;
94   case MachineOperand::MO_GlobalAddress: {
95     if (!elideOffsetKeyword)
96       O << "OFFSET ";
97     O << Mang->getValueName(MO.getGlobal());
98     int Offset = MO.getOffset();
99     if (Offset > 0)
100       O << " + " << Offset;
101     else if (Offset < 0)
102       O << " - " << -Offset;
103     return;
104   }
105   case MachineOperand::MO_ExternalSymbol:
106     O << GlobalPrefix << MO.getSymbolName();
107     return;
108   default:
109     O << "<unknown operand type>"; return;
110   }
111 }
112
113 void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
114   assert(isMem(MI, Op) && "Invalid memory reference!");
115
116   const MachineOperand &BaseReg  = MI->getOperand(Op);
117   int ScaleVal                   = MI->getOperand(Op+1).getImmedValue();
118   const MachineOperand &IndexReg = MI->getOperand(Op+2);
119   const MachineOperand &DispSpec = MI->getOperand(Op+3);
120
121   if (BaseReg.isFrameIndex()) {
122     O << "[frame slot #" << BaseReg.getFrameIndex();
123     if (DispSpec.getImmedValue())
124       O << " + " << DispSpec.getImmedValue();
125     O << "]";
126     return;
127   } else if (BaseReg.isConstantPoolIndex()) {
128     O << "[.CPI" << CurrentFnName << "_"
129       << BaseReg.getConstantPoolIndex();
130
131     if (IndexReg.getReg()) {
132       O << " + ";
133       if (ScaleVal != 1)
134         O << ScaleVal << "*";
135       printOp(IndexReg);
136     }
137
138     if (DispSpec.getImmedValue())
139       O << " + " << DispSpec.getImmedValue();
140     O << "]";
141     return;
142   }
143
144   O << "[";
145   bool NeedPlus = false;
146   if (BaseReg.getReg()) {
147     printOp(BaseReg, true);
148     NeedPlus = true;
149   }
150
151   if (IndexReg.getReg()) {
152     if (NeedPlus) O << " + ";
153     if (ScaleVal != 1)
154       O << ScaleVal << "*";
155     printOp(IndexReg);
156     NeedPlus = true;
157   }
158
159   if (DispSpec.isGlobalAddress()) {
160     if (NeedPlus)
161       O << " + ";
162     printOp(DispSpec, true);
163   } else {
164     int DispVal = DispSpec.getImmedValue();
165     if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
166       if (NeedPlus)
167         if (DispVal > 0)
168           O << " + ";
169         else {
170           O << " - ";
171           DispVal = -DispVal;
172         }
173       O << DispVal;
174     }
175   }
176   O << "]";
177 }
178
179
180 /// printMachineInstruction -- Print out a single X86 LLVM instruction
181 /// MI in Intel syntax to the current output stream.
182 ///
183 void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
184   ++EmittedInsts;
185
186   // Call the autogenerated instruction printer routines.
187   printInstruction(MI);
188 }
189
190 bool X86IntelAsmPrinter::doInitialization(Module &M) {
191   X86SharedAsmPrinter::doInitialization(M);
192   // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
193   //
194   // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
195   // instruction as a reference to the register named sp, and if you try to
196   // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
197   // before being looked up in the symbol table. This creates spurious
198   // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
199   // mode, and decorate all register names with percent signs.
200   O << "\t.intel_syntax\n";
201   return false;
202 }
203
204 // Include the auto-generated portion of the assembly writer.
205 #include "X86GenAsmWriter1.inc"