Added getTargetLowering() to TargetMachine. Refactored targets to support this.
[oota-llvm.git] / lib / Target / X86 / X86ATTAsmPrinter.cpp
1 //===-- X86ATTAsmPrinter.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 AT&T format assembly
12 // language. This printer is the output mechanism used by `llc'.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "X86ATTAsmPrinter.h"
17 #include "X86.h"
18 #include "X86TargetMachine.h"
19 #include "llvm/Module.h"
20 #include "llvm/Support/Mangler.h"
21 #include "llvm/Target/TargetOptions.h"
22 #include <iostream>
23 using namespace llvm;
24
25 /// runOnMachineFunction - This uses the printMachineInstruction()
26 /// method to print assembly for each instruction.
27 ///
28 bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
29   //  if (forDarwin) {
30     // Let PassManager know we need debug information and relay
31     // the MachineDebugInfo address on to DwarfWriter.
32     DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
33     //  }
34
35   SetupMachineFunction(MF);
36   O << "\n\n";
37
38   if (forDarwin) {
39     // Emit pre-function debug information.
40     DW.BeginFunction(MF);
41   }
42
43   // Print out constants referenced by the function
44   EmitConstantPool(MF.getConstantPool());
45
46   // Print out labels for the function.
47   const Function *F = MF.getFunction();
48   switch (F->getLinkage()) {
49   default: assert(0 && "Unknown linkage type!");
50   case Function::InternalLinkage:  // Symbols default to internal.
51     SwitchSection(".text", F);
52     EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
53     break;
54   case Function::ExternalLinkage:
55     SwitchSection(".text", F);
56     EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
57     O << "\t.globl\t" << CurrentFnName << "\n";
58     break;
59   case Function::WeakLinkage:
60   case Function::LinkOnceLinkage:
61     if (forDarwin) {
62       SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
63                     F);
64       O << "\t.globl\t" << CurrentFnName << "\n";
65       O << "\t.weak_definition\t" << CurrentFnName << "\n";
66     } else {
67       EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
68       O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
69         << ",\"ax\",@progbits\n";
70       O << "\t.weak " << CurrentFnName << "\n";
71     }
72     break;
73   }
74   O << CurrentFnName << ":\n";
75
76   // Print out code for the function.
77   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
78        I != E; ++I) {
79     // Print a label for the basic block.
80     if (I->pred_begin() != I->pred_end())
81       O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
82         << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
83         << "\n";
84     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
85          II != E; ++II) {
86       // Print the assembly for the instruction.
87       O << "\t";
88       printMachineInstruction(II);
89     }
90   }
91   if (HasDotTypeDotSizeDirective)
92     O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
93
94   if (forDarwin) {
95     // Emit post-function debug information.
96     DW.EndFunction(MF);
97   }
98
99   // We didn't modify anything.
100   return false;
101 }
102
103 void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
104                                     const char *Modifier) {
105   const MachineOperand &MO = MI->getOperand(OpNo);
106   const MRegisterInfo &RI = *TM.getRegisterInfo();
107   switch (MO.getType()) {
108   case MachineOperand::MO_VirtualRegister:
109   case MachineOperand::MO_MachineRegister:
110     assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
111            "Virtual registers should not make it this far!");
112     O << '%';
113     for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
114       O << (char)tolower(*Name);
115     return;
116
117   case MachineOperand::MO_SignExtendedImmed:
118   case MachineOperand::MO_UnextendedImmed:
119     if (!Modifier || strcmp(Modifier, "debug") != 0)
120       O << '$';
121     O << (int)MO.getImmedValue();
122     return;
123   case MachineOperand::MO_MachineBasicBlock: {
124     MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
125     O << PrivateGlobalPrefix << "BB"
126       << Mang->getValueName(MBBOp->getParent()->getFunction())
127       << "_" << MBBOp->getNumber () << "\t# "
128       << MBBOp->getBasicBlock ()->getName ();
129     return;
130   }
131   case MachineOperand::MO_PCRelativeDisp:
132     std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
133     abort ();
134     return;
135   case MachineOperand::MO_ConstantPoolIndex: {
136     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
137     if (!isMemOp) O << '$';
138     O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
139       << MO.getConstantPoolIndex();
140     if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
141       O << "-\"L" << getFunctionNumber() << "$pb\"";
142     int Offset = MO.getOffset();
143     if (Offset > 0)
144       O << "+" << Offset;
145     else if (Offset < 0)
146       O << Offset;
147     return;
148   }
149   case MachineOperand::MO_GlobalAddress: {
150     bool isCallOp = Modifier && !strcmp(Modifier, "call");
151     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
152     if (!isMemOp && !isCallOp) O << '$';
153     // Darwin block shameless ripped from PPCAsmPrinter.cpp
154     if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
155       GlobalValue *GV = MO.getGlobal();
156       std::string Name = Mang->getValueName(GV);
157       // Link-once, External, or Weakly-linked global variables need
158       // non-lazily-resolved stubs
159       if (GV->isExternal() || GV->hasWeakLinkage() ||
160           GV->hasLinkOnceLinkage()) {
161         // Dynamically-resolved functions need a stub for the function.
162         if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
163           FnStubs.insert(Name);
164           O << "L" << Name << "$stub";
165         } else {
166           GVStubs.insert(Name);
167           O << "L" << Name << "$non_lazy_ptr";
168         }
169       } else {
170         O << Mang->getValueName(GV);
171       } 
172       if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
173         O << "-\"L" << getFunctionNumber() << "$pb\"";
174    } else
175       O << Mang->getValueName(MO.getGlobal());
176     int Offset = MO.getOffset();
177     if (Offset > 0)
178       O << "+" << Offset;
179     else if (Offset < 0)
180       O << Offset;
181     return;
182   }
183   case MachineOperand::MO_ExternalSymbol: {
184     bool isCallOp = Modifier && !strcmp(Modifier, "call");
185     if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
186       std::string Name(GlobalPrefix);
187       Name += MO.getSymbolName();
188       FnStubs.insert(Name);
189       O << "L" << Name << "$stub";
190       return;
191     }
192     if (!isCallOp) O << '$';
193     O << GlobalPrefix << MO.getSymbolName();
194     return;
195   }
196   default:
197     O << "<unknown operand type>"; return;
198   }
199 }
200
201 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
202   unsigned char value = MI->getOperand(Op).getImmedValue();
203   assert(value <= 7 && "Invalid ssecc argument!");
204   switch (value) {
205   case 0: O << "eq"; break;
206   case 1: O << "lt"; break;
207   case 2: O << "le"; break;
208   case 3: O << "unord"; break;
209   case 4: O << "neq"; break;
210   case 5: O << "nlt"; break;
211   case 6: O << "nle"; break;
212   case 7: O << "ord"; break;
213   }
214 }
215
216 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
217   assert(isMem(MI, Op) && "Invalid memory reference!");
218
219   const MachineOperand &BaseReg  = MI->getOperand(Op);
220   int ScaleVal                   = MI->getOperand(Op+1).getImmedValue();
221   const MachineOperand &IndexReg = MI->getOperand(Op+2);
222   const MachineOperand &DispSpec = MI->getOperand(Op+3);
223
224   if (BaseReg.isFrameIndex()) {
225     O << "[frame slot #" << BaseReg.getFrameIndex();
226     if (DispSpec.getImmedValue())
227       O << " + " << DispSpec.getImmedValue();
228     O << "]";
229     return;
230   }
231
232   if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
233     printOperand(MI, Op+3, "mem");
234   } else {
235     int DispVal = DispSpec.getImmedValue();
236     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
237       O << DispVal;
238   }
239
240   if (IndexReg.getReg() || BaseReg.getReg()) {
241     O << "(";
242     if (BaseReg.getReg())
243       printOperand(MI, Op);
244
245     if (IndexReg.getReg()) {
246       O << ",";
247       printOperand(MI, Op+2);
248       if (ScaleVal != 1)
249         O << "," << ScaleVal;
250     }
251
252     O << ")";
253   }
254 }
255
256 void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
257   O << "\"L" << getFunctionNumber() << "$pb\"\n";
258   O << "\"L" << getFunctionNumber() << "$pb\":";
259 }
260
261 /// printMachineInstruction -- Print out a single X86 LLVM instruction
262 /// MI in Intel syntax to the current output stream.
263 ///
264 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
265   ++EmittedInsts;
266   // This works around some Darwin assembler bugs.
267   if (forDarwin) {
268     switch (MI->getOpcode()) {
269     case X86::REP_MOVSB:
270       O << "rep/movsb (%esi),(%edi)\n";
271       return;
272     case X86::REP_MOVSD:
273       O << "rep/movsl (%esi),(%edi)\n";
274       return;
275     case X86::REP_MOVSW:
276       O << "rep/movsw (%esi),(%edi)\n";
277       return;
278     case X86::REP_STOSB:
279       O << "rep/stosb\n";
280       return;
281     case X86::REP_STOSD:
282       O << "rep/stosl\n";
283       return;
284     case X86::REP_STOSW:
285       O << "rep/stosw\n";
286       return;
287     default:
288       break;
289     }
290   }
291
292   // Call the autogenerated instruction printer routines.
293   printInstruction(MI);
294 }
295
296 // Include the auto-generated portion of the assembly writer.
297 #include "X86GenAsmWriter.inc"
298