Eliminate the printCallOperand method, using a 'call' modifier on
[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::printOperand(const MachineInstr *MI, unsigned OpNo,
67                                     const char *Modifier) {
68   const MachineOperand &MO = MI->getOperand(OpNo);
69   const MRegisterInfo &RI = *TM.getRegisterInfo();
70   switch (MO.getType()) {
71   case MachineOperand::MO_VirtualRegister:
72   case MachineOperand::MO_MachineRegister:
73     assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
74            "Virtual registers should not make it this far!");
75     O << '%';
76     for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
77       O << (char)tolower(*Name);
78     return;
79
80   case MachineOperand::MO_SignExtendedImmed:
81   case MachineOperand::MO_UnextendedImmed:
82     O << '$' << (int)MO.getImmedValue();
83     return;
84   case MachineOperand::MO_MachineBasicBlock: {
85     MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
86     O << PrivateGlobalPrefix << "BB"
87       << Mang->getValueName(MBBOp->getParent()->getFunction())
88       << "_" << MBBOp->getNumber () << "\t# "
89       << MBBOp->getBasicBlock ()->getName ();
90     return;
91   }
92   case MachineOperand::MO_PCRelativeDisp:
93     std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
94     abort ();
95     return;
96   case MachineOperand::MO_GlobalAddress: {
97     bool isCallOp = Modifier && !strcmp(Modifier, "call");
98     // Darwin block shameless ripped from PowerPCAsmPrinter.cpp
99     if (forDarwin) {
100       if (!isCallOp) O << '$';
101       GlobalValue *GV = MO.getGlobal();
102       std::string Name = Mang->getValueName(GV);
103
104       // Dynamically-resolved functions need a stub for the function.  Be
105       // wary however not to output $stub for external functions whose addresses
106       // are taken.  Those should be emitted as $non_lazy_ptr below.
107       Function *F = dyn_cast<Function>(GV);
108       if (F && isCallOp && F->isExternal()) {
109         FnStubs.insert(Name);
110         O << "L" << Name << "$stub";
111       } else if (GV->hasLinkOnceLinkage()) {
112         // Link-once, External, or Weakly-linked global variables need
113         // non-lazily-resolved stubs
114         LinkOnceStubs.insert(Name);
115         O << "L" << Name << "$non_lazy_ptr";
116       } else if (GV->isExternal() || GV->hasWeakLinkage()) {
117         GVStubs.insert(Name);
118         O << "L" << Name << "$non_lazy_ptr";
119       } else {
120         O << Mang->getValueName(GV);
121       }
122       int Offset = MO.getOffset();
123       if (Offset > 0)
124         O << "+" << Offset;
125       else if (Offset < 0)
126         O << Offset;
127       return;
128     }
129     if (!isCallOp) O << '$';
130     O << Mang->getValueName(MO.getGlobal());
131     int Offset = MO.getOffset();
132     if (Offset > 0)
133       O << "+" << Offset;
134     else if (Offset < 0)
135       O << Offset;
136     return;
137   }
138   case MachineOperand::MO_ExternalSymbol: {
139     bool isCallOp = Modifier && !strcmp(Modifier, "call");
140     if (isCallOp && forDarwin) {
141       std::string Name(GlobalPrefix); Name += MO.getSymbolName();
142       FnStubs.insert(Name);
143       O << "L" << Name << "$stub";
144       return;
145     }
146     if (!isCallOp) O << '$';
147     O << GlobalPrefix << MO.getSymbolName();
148     return;
149   }
150   default:
151     O << "<unknown operand type>"; return;
152   }
153 }
154
155 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
156   unsigned char value = MI->getOperand(Op).getImmedValue();
157   assert(value <= 7 && "Invalid ssecc argument!");
158   switch (value) {
159   case 0: O << "eq"; break;
160   case 1: O << "lt"; break;
161   case 2: O << "le"; break;
162   case 3: O << "unord"; break;
163   case 4: O << "neq"; break;
164   case 5: O << "nlt"; break;
165   case 6: O << "nle"; break;
166   case 7: O << "ord"; break;
167   }
168 }
169
170 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
171   assert(isMem(MI, Op) && "Invalid memory reference!");
172
173   const MachineOperand &BaseReg  = MI->getOperand(Op);
174   int ScaleVal                   = MI->getOperand(Op+1).getImmedValue();
175   const MachineOperand &IndexReg = MI->getOperand(Op+2);
176   const MachineOperand &DispSpec = MI->getOperand(Op+3);
177
178   if (BaseReg.isFrameIndex()) {
179     O << "[frame slot #" << BaseReg.getFrameIndex();
180     if (DispSpec.getImmedValue())
181       O << " + " << DispSpec.getImmedValue();
182     O << "]";
183     return;
184   } else if (BaseReg.isConstantPoolIndex()) {
185     O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
186       << BaseReg.getConstantPoolIndex();
187     if (DispSpec.getImmedValue())
188       O << "+" << DispSpec.getImmedValue();
189     if (IndexReg.getReg()) {
190       O << "(,";
191       printOperand(MI, Op+2);
192       if (ScaleVal != 1)
193         O << "," << ScaleVal;
194       O << ")";
195     }
196     return;
197   }
198
199   if (DispSpec.isGlobalAddress()) {
200     printOperand(MI, Op+3, "call");
201   } else {
202     int DispVal = DispSpec.getImmedValue();
203     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
204       O << DispVal;
205   }
206
207   if (IndexReg.getReg() || BaseReg.getReg()) {
208     O << "(";
209     if (BaseReg.getReg())
210       printOperand(MI, Op);
211
212     if (IndexReg.getReg()) {
213       O << ",";
214       printOperand(MI, Op+2);
215       if (ScaleVal != 1)
216         O << "," << ScaleVal;
217     }
218
219     O << ")";
220   }
221 }
222
223 /// printMachineInstruction -- Print out a single X86 LLVM instruction
224 /// MI in Intel syntax to the current output stream.
225 ///
226 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
227   ++EmittedInsts;
228   // This works around some Darwin assembler bugs.
229   if (forDarwin) {
230     switch (MI->getOpcode()) {
231     case X86::REP_MOVSB:
232       O << "rep/movsb (%esi),(%edi)\n";
233       return;
234     case X86::REP_MOVSD:
235       O << "rep/movsl (%esi),(%edi)\n";
236       return;
237     case X86::REP_MOVSW:
238       O << "rep/movsw (%esi),(%edi)\n";
239       return;
240     case X86::REP_STOSB:
241       O << "rep/stosb\n";
242       return;
243     case X86::REP_STOSD:
244       O << "rep/stosl\n";
245       return;
246     case X86::REP_STOSW:
247       O << "rep/stosw\n";
248       return;
249     default:
250       break;
251     }
252   }
253
254   // Call the autogenerated instruction printer routines.
255   printInstruction(MI);
256 }
257
258 // Include the auto-generated portion of the assembly writer.
259 #include "X86GenAsmWriter.inc"
260