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