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