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