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