Eliminate all remaining tabs and trailing spaces.
[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 using namespace llvm;
22 using namespace x86;
23
24 /// runOnMachineFunction - This uses the printMachineInstruction()
25 /// method to print assembly for each instruction.
26 ///
27 bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
28   setupMachineFunction(MF);
29   O << "\n\n";
30
31   // Print out constants referenced by the function
32   printConstantPool(MF.getConstantPool());
33
34   // Print out labels for the function.
35   O << "\t.text\n";
36   emitAlignment(4);     // FIXME: This should be parameterized somewhere.
37   O << "\t.globl\t" << CurrentFnName << "\n";
38   if (!forCygwin && !forDarwin)
39     O << "\t.type\t" << CurrentFnName << ", @function\n";
40   O << CurrentFnName << ":\n";
41
42   // Print out code for the function.
43   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
44        I != E; ++I) {
45     // Print a label for the basic block.
46     if (I->pred_begin() != I->pred_end())
47       O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
48         << CommentString << " " << I->getBasicBlock()->getName() << "\n";
49     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
50          II != E; ++II) {
51       // Print the assembly for the instruction.
52       O << "\t";
53       printMachineInstruction(II);
54     }
55   }
56   if (!forDarwin)
57     O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
58
59   // We didn't modify anything.
60   return false;
61 }
62
63 void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
64   const MRegisterInfo &RI = *TM.getRegisterInfo();
65   switch (MO.getType()) {
66   case MachineOperand::MO_VirtualRegister:
67   case MachineOperand::MO_MachineRegister:
68     assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
69            "Virtual registers should not make it this far!");
70     O << '%';
71     for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
72       O << (char)tolower(*Name);
73     return;
74
75   case MachineOperand::MO_SignExtendedImmed:
76   case MachineOperand::MO_UnextendedImmed:
77     O << '$' << (int)MO.getImmedValue();
78     return;
79   case MachineOperand::MO_MachineBasicBlock: {
80     MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
81     O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
82       << "_" << MBBOp->getNumber () << "\t# "
83       << MBBOp->getBasicBlock ()->getName ();
84     return;
85   }
86   case MachineOperand::MO_PCRelativeDisp:
87     std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
88     abort ();
89     return;
90   case MachineOperand::MO_GlobalAddress: {
91     // Darwin block shameless ripped from PowerPCAsmPrinter.cpp
92     if (forDarwin) {
93       if (!isCallOp) O << '$';
94       GlobalValue *GV = MO.getGlobal();
95       std::string Name = Mang->getValueName(GV);
96
97       // Dynamically-resolved functions need a stub for the function.  Be
98       // wary however not to output $stub for external functions whose addresses
99       // are taken.  Those should be emitted as $non_lazy_ptr below.
100       Function *F = dyn_cast<Function>(GV);
101       if (F && isCallOp && F->isExternal()) {
102         FnStubs.insert(Name);
103         O << "L" << Name << "$stub";
104       } else if (GV->hasLinkOnceLinkage()) {
105         // Link-once, External, or Weakly-linked global variables need
106         // non-lazily-resolved stubs
107         LinkOnceStubs.insert(Name);
108         O << "L" << Name << "$non_lazy_ptr";
109       } else if (GV->isExternal() || GV->hasWeakLinkage()) {
110         GVStubs.insert(Name);
111         O << "L" << Name << "$non_lazy_ptr";
112       } else {
113         O << Mang->getValueName(GV);
114       }
115       int Offset = MO.getOffset();
116       if (Offset > 0)
117         O << "+" << Offset;
118       else if (Offset < 0)
119         O << Offset;
120       return;
121     }
122     if (!isCallOp) O << '$';
123     O << Mang->getValueName(MO.getGlobal());
124     int Offset = MO.getOffset();
125     if (Offset > 0)
126       O << "+" << Offset;
127     else if (Offset < 0)
128       O << Offset;
129     return;
130   }
131   case MachineOperand::MO_ExternalSymbol:
132     if (isCallOp && forDarwin) {
133       std::string Name(GlobalPrefix); Name += MO.getSymbolName();
134       FnStubs.insert(Name);
135       O << "L" << Name << "$stub";
136       return;
137     }
138     if (!isCallOp) O << '$';
139     O << GlobalPrefix << MO.getSymbolName();
140     return;
141   default:
142     O << "<unknown operand type>"; return;
143   }
144 }
145
146 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op,
147                                   MVT::ValueType VT) {
148   unsigned char value = MI->getOperand(Op).getImmedValue();
149   assert(value <= 7 && "Invalid ssecc argument!");
150   switch (value) {
151   case 0: O << "eq"; break;
152   case 1: O << "lt"; break;
153   case 2: O << "le"; break;
154   case 3: O << "unord"; break;
155   case 4: O << "neq"; break;
156   case 5: O << "nlt"; break;
157   case 6: O << "nle"; break;
158   case 7: O << "ord"; break;
159   }
160 }
161
162 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
163   assert(isMem(MI, Op) && "Invalid memory reference!");
164
165   const MachineOperand &BaseReg  = MI->getOperand(Op);
166   int ScaleVal                   = MI->getOperand(Op+1).getImmedValue();
167   const MachineOperand &IndexReg = MI->getOperand(Op+2);
168   const MachineOperand &DispSpec = MI->getOperand(Op+3);
169
170   if (BaseReg.isFrameIndex()) {
171     O << "[frame slot #" << BaseReg.getFrameIndex();
172     if (DispSpec.getImmedValue())
173       O << " + " << DispSpec.getImmedValue();
174     O << "]";
175     return;
176   } else if (BaseReg.isConstantPoolIndex()) {
177     O << ".CPI" << CurrentFnName << "_"
178       << BaseReg.getConstantPoolIndex();
179     if (DispSpec.getImmedValue())
180       O << "+" << DispSpec.getImmedValue();
181     if (IndexReg.getReg()) {
182       O << "(,";
183       printOp(IndexReg);
184       if (ScaleVal != 1)
185         O << "," << ScaleVal;
186       O << ")";
187     }
188     return;
189   }
190
191   if (DispSpec.isGlobalAddress()) {
192     printOp(DispSpec, true);
193   } else {
194     int DispVal = DispSpec.getImmedValue();
195     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
196       O << DispVal;
197   }
198
199   if (IndexReg.getReg() || BaseReg.getReg()) {
200     O << "(";
201     if (BaseReg.getReg())
202       printOp(BaseReg);
203
204     if (IndexReg.getReg()) {
205       O << ",";
206       printOp(IndexReg);
207       if (ScaleVal != 1)
208         O << "," << ScaleVal;
209     }
210
211     O << ")";
212   }
213 }
214
215 /// printMachineInstruction -- Print out a single X86 LLVM instruction
216 /// MI in Intel syntax to the current output stream.
217 ///
218 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
219   ++EmittedInsts;
220   // Call the autogenerated instruction printer routines.
221   printInstruction(MI);
222 }
223
224 // Include the auto-generated portion of the assembly writer.
225 #include "X86GenAsmWriter.inc"
226