Added a common about the need for X86ISD::Wrapper.
[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 #include "llvm/Target/TargetOptions.h"
22 using namespace llvm;
23 using namespace x86;
24
25 /// runOnMachineFunction - This uses the printMachineInstruction()
26 /// method to print assembly for each instruction.
27 ///
28 bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
29   SetupMachineFunction(MF);
30   O << "\n\n";
31
32   // Print out constants referenced by the function
33   EmitConstantPool(MF.getConstantPool());
34
35   // Print out labels for the function.
36   SwitchSection("\t.text\n", MF.getFunction());
37   EmitAlignment(4);
38   O << "\t.globl\t" << CurrentFnName << "\n";
39   if (HasDotTypeDotSizeDirective)
40     O << "\t.type\t" << CurrentFnName << ", @function\n";
41   O << CurrentFnName << ":\n";
42
43   // Print out code for the function.
44   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
45        I != E; ++I) {
46     // Print a label for the basic block if there are any predecessors.
47     if (I->pred_begin() != I->pred_end())
48       O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
49         << ":\t"
50         << CommentString << " " << I->getBasicBlock()->getName() << "\n";
51     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
52          II != E; ++II) {
53       // Print the assembly for the instruction.
54       O << "\t";
55       printMachineInstruction(II);
56     }
57   }
58
59   // We didn't modify anything.
60   return false;
61 }
62
63 void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
64   unsigned char value = MI->getOperand(Op).getImmedValue();
65   assert(value <= 7 && "Invalid ssecc argument!");
66   switch (value) {
67   case 0: O << "eq"; break;
68   case 1: O << "lt"; break;
69   case 2: O << "le"; break;
70   case 3: O << "unord"; break;
71   case 4: O << "neq"; break;
72   case 5: O << "nlt"; break;
73   case 6: O << "nle"; break;
74   case 7: O << "ord"; break;
75   }
76 }
77
78 void X86IntelAsmPrinter::printOp(const MachineOperand &MO, 
79                                  const char *Modifier) {
80   const MRegisterInfo &RI = *TM.getRegisterInfo();
81   switch (MO.getType()) {
82   case MachineOperand::MO_VirtualRegister:
83     if (Value *V = MO.getVRegValueOrNull()) {
84       O << "<" << V->getName() << ">";
85       return;
86     }
87     // FALLTHROUGH
88   case MachineOperand::MO_MachineRegister:
89     if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
90       // Bug Workaround: See note in Printer::doInitialization about %.
91       O << "%" << RI.get(MO.getReg()).Name;
92     else
93       O << "%reg" << MO.getReg();
94     return;
95
96   case MachineOperand::MO_SignExtendedImmed:
97   case MachineOperand::MO_UnextendedImmed:
98     O << (int)MO.getImmedValue();
99     return;
100   case MachineOperand::MO_MachineBasicBlock: {
101     MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
102     O << PrivateGlobalPrefix << "BB"
103       << Mang->getValueName(MBBOp->getParent()->getFunction())
104       << "_" << MBBOp->getNumber () << "\t# "
105       << MBBOp->getBasicBlock ()->getName ();
106     return;
107   }
108   case MachineOperand::MO_PCRelativeDisp:
109     assert(0 && "Shouldn't use addPCDisp() when building X86 MachineInstrs");
110     abort ();
111     return;
112   case MachineOperand::MO_GlobalAddress: {
113     bool isCallOp = Modifier && !strcmp(Modifier, "call");
114     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
115     if (!isMemOp && !isCallOp) O << "OFFSET ";
116     if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
117       GlobalValue *GV = MO.getGlobal();
118       std::string Name = Mang->getValueName(GV);
119       if (!isMemOp && !isCallOp) O << '$';
120       // Link-once, External, or Weakly-linked global variables need
121       // non-lazily-resolved stubs
122       if (GV->isExternal() || GV->hasWeakLinkage() ||
123           GV->hasLinkOnceLinkage()) {
124         // Dynamically-resolved functions need a stub for the function.
125         if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
126           FnStubs.insert(Name);
127           O << "L" << Name << "$stub";
128         } else {
129           GVStubs.insert(Name);
130           O << "L" << Name << "$non_lazy_ptr";
131         }
132       } else {
133         O << Mang->getValueName(GV);
134       }
135       if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
136         O << "-\"L" << getFunctionNumber() << "$pb\"";
137     } else
138       O << Mang->getValueName(MO.getGlobal());
139     int Offset = MO.getOffset();
140     if (Offset > 0)
141       O << " + " << Offset;
142     else if (Offset < 0)
143       O << Offset;
144     return;
145   }
146   case MachineOperand::MO_ExternalSymbol: {
147     bool isCallOp = Modifier && !strcmp(Modifier, "call");
148     if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
149       std::string Name(GlobalPrefix);
150       Name += MO.getSymbolName();
151       FnStubs.insert(Name);
152       O << "L" << Name << "$stub";
153       return;
154     }
155     if (!isCallOp) O << "OFFSET ";
156     O << GlobalPrefix << MO.getSymbolName();
157     return;
158   }
159   default:
160     O << "<unknown operand type>"; return;
161   }
162 }
163
164 void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
165   assert(isMem(MI, Op) && "Invalid memory reference!");
166
167   const MachineOperand &BaseReg  = MI->getOperand(Op);
168   int ScaleVal                   = MI->getOperand(Op+1).getImmedValue();
169   const MachineOperand &IndexReg = MI->getOperand(Op+2);
170   const MachineOperand &DispSpec = MI->getOperand(Op+3);
171
172   if (BaseReg.isFrameIndex()) {
173     O << "[frame slot #" << BaseReg.getFrameIndex();
174     if (DispSpec.getImmedValue())
175       O << " + " << DispSpec.getImmedValue();
176     O << "]";
177     return;
178   }
179
180   O << "[";
181   bool NeedPlus = false;
182   if (BaseReg.getReg()) {
183     printOp(BaseReg, "mem");
184     NeedPlus = true;
185   }
186
187   if (IndexReg.getReg()) {
188     if (NeedPlus) O << " + ";
189     if (ScaleVal != 1)
190       O << ScaleVal << "*";
191     printOp(IndexReg);
192     NeedPlus = true;
193   }
194
195   if (DispSpec.isGlobalAddress()) {
196     if (NeedPlus)
197       O << " + ";
198     printOp(DispSpec, "mem");
199   } else if (DispSpec.isConstantPoolIndex()) {
200     O << "[" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
201       << DispSpec.getConstantPoolIndex();
202     if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
203       O << "-\"L" << getFunctionNumber() << "$pb\"";
204     if (DispSpec.getOffset())
205       O << " + " << DispSpec.getOffset();
206     O << "]";
207     return;
208   } else {
209     int DispVal = DispSpec.getImmedValue();
210     if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
211       if (NeedPlus)
212         if (DispVal > 0)
213           O << " + ";
214         else {
215           O << " - ";
216           DispVal = -DispVal;
217         }
218       O << DispVal;
219     }
220   }
221   O << "]";
222 }
223
224 void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
225   O << "\"L" << getFunctionNumber() << "$pb\"\n";
226   O << "\"L" << getFunctionNumber() << "$pb\":";
227 }
228
229 /// printMachineInstruction -- Print out a single X86 LLVM instruction
230 /// MI in Intel syntax to the current output stream.
231 ///
232 void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
233   ++EmittedInsts;
234
235   // Call the autogenerated instruction printer routines.
236   printInstruction(MI);
237 }
238
239 bool X86IntelAsmPrinter::doInitialization(Module &M) {
240   X86SharedAsmPrinter::doInitialization(M);
241   // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
242   //
243   // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
244   // instruction as a reference to the register named sp, and if you try to
245   // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
246   // before being looked up in the symbol table. This creates spurious
247   // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
248   // mode, and decorate all register names with percent signs.
249   O << "\t.intel_syntax\n";
250   return false;
251 }
252
253 // Include the auto-generated portion of the assembly writer.
254 #include "X86GenAsmWriter1.inc"