Eliminate the printCallOperand method, using a 'call' modifier on
[oota-llvm.git] / lib / Target / X86 / X86IntelAsmPrinter.cpp
1 //===-- X86IntelAsmPrinter.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 Intel format assembly language.
12 // This printer is the output mechanism used by `llc'.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "X86IntelAsmPrinter.h"
17 #include "X86.h"
18 #include "llvm/Module.h"
19 #include "llvm/Assembly/Writer.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 X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
28   SetupMachineFunction(MF);
29   O << "\n\n";
30
31   // Print out constants referenced by the function
32   EmitConstantPool(MF.getConstantPool());
33
34   // Print out labels for the function.
35   SwitchSection("\t.text\n", MF.getFunction());
36   EmitAlignment(4);
37   O << "\t.globl\t" << CurrentFnName << "\n";
38   if (HasDotTypeDotSizeDirective)
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 if there are any predecessors.
46     if (I->pred_begin() != I->pred_end())
47       O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
48         << ":\t"
49         << CommentString << " " << I->getBasicBlock()->getName() << "\n";
50     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
51          II != E; ++II) {
52       // Print the assembly for the instruction.
53       O << "\t";
54       printMachineInstruction(II);
55     }
56   }
57
58   // We didn't modify anything.
59   return false;
60 }
61
62 void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
63   unsigned char value = MI->getOperand(Op).getImmedValue();
64   assert(value <= 7 && "Invalid ssecc argument!");
65   switch (value) {
66   case 0: O << "eq"; break;
67   case 1: O << "lt"; break;
68   case 2: O << "le"; break;
69   case 3: O << "unord"; break;
70   case 4: O << "neq"; break;
71   case 5: O << "nlt"; break;
72   case 6: O << "nle"; break;
73   case 7: O << "ord"; break;
74   }
75 }
76
77 void X86IntelAsmPrinter::printOp(const MachineOperand &MO, 
78                                  const char *Modifier) {
79   const MRegisterInfo &RI = *TM.getRegisterInfo();
80   switch (MO.getType()) {
81   case MachineOperand::MO_VirtualRegister:
82     if (Value *V = MO.getVRegValueOrNull()) {
83       O << "<" << V->getName() << ">";
84       return;
85     }
86     // FALLTHROUGH
87   case MachineOperand::MO_MachineRegister:
88     if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
89       // Bug Workaround: See note in Printer::doInitialization about %.
90       O << "%" << RI.get(MO.getReg()).Name;
91     else
92       O << "%reg" << MO.getReg();
93     return;
94
95   case MachineOperand::MO_SignExtendedImmed:
96   case MachineOperand::MO_UnextendedImmed:
97     O << (int)MO.getImmedValue();
98     return;
99   case MachineOperand::MO_MachineBasicBlock: {
100     MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
101     O << PrivateGlobalPrefix << "BB"
102       << Mang->getValueName(MBBOp->getParent()->getFunction())
103       << "_" << MBBOp->getNumber () << "\t# "
104       << MBBOp->getBasicBlock ()->getName ();
105     return;
106   }
107   case MachineOperand::MO_PCRelativeDisp:
108     assert(0 && "Shouldn't use addPCDisp() when building X86 MachineInstrs");
109     abort ();
110     return;
111   case MachineOperand::MO_GlobalAddress: {
112     if (!Modifier || strcmp(Modifier, "call"))
113       O << "OFFSET ";
114     O << Mang->getValueName(MO.getGlobal());
115     int Offset = MO.getOffset();
116     if (Offset > 0)
117       O << " + " << Offset;
118     else if (Offset < 0)
119       O << Offset;
120     return;
121   }
122   case MachineOperand::MO_ExternalSymbol:
123     O << GlobalPrefix << MO.getSymbolName();
124     return;
125   default:
126     O << "<unknown operand type>"; return;
127   }
128 }
129
130 void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
131   assert(isMem(MI, Op) && "Invalid memory reference!");
132
133   const MachineOperand &BaseReg  = MI->getOperand(Op);
134   int ScaleVal                   = MI->getOperand(Op+1).getImmedValue();
135   const MachineOperand &IndexReg = MI->getOperand(Op+2);
136   const MachineOperand &DispSpec = MI->getOperand(Op+3);
137
138   if (BaseReg.isFrameIndex()) {
139     O << "[frame slot #" << BaseReg.getFrameIndex();
140     if (DispSpec.getImmedValue())
141       O << " + " << DispSpec.getImmedValue();
142     O << "]";
143     return;
144   } else if (BaseReg.isConstantPoolIndex()) {
145     O << "[" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
146       << BaseReg.getConstantPoolIndex();
147
148     if (IndexReg.getReg()) {
149       O << " + ";
150       if (ScaleVal != 1)
151         O << ScaleVal << "*";
152       printOp(IndexReg);
153     }
154
155     if (DispSpec.getImmedValue())
156       O << " + " << DispSpec.getImmedValue();
157     O << "]";
158     return;
159   }
160
161   O << "[";
162   bool NeedPlus = false;
163   if (BaseReg.getReg()) {
164     printOp(BaseReg, "call");
165     NeedPlus = true;
166   }
167
168   if (IndexReg.getReg()) {
169     if (NeedPlus) O << " + ";
170     if (ScaleVal != 1)
171       O << ScaleVal << "*";
172     printOp(IndexReg);
173     NeedPlus = true;
174   }
175
176   if (DispSpec.isGlobalAddress()) {
177     if (NeedPlus)
178       O << " + ";
179     printOp(DispSpec, "call");
180   } else {
181     int DispVal = DispSpec.getImmedValue();
182     if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
183       if (NeedPlus)
184         if (DispVal > 0)
185           O << " + ";
186         else {
187           O << " - ";
188           DispVal = -DispVal;
189         }
190       O << DispVal;
191     }
192   }
193   O << "]";
194 }
195
196
197 /// printMachineInstruction -- Print out a single X86 LLVM instruction
198 /// MI in Intel syntax to the current output stream.
199 ///
200 void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
201   ++EmittedInsts;
202
203   // Call the autogenerated instruction printer routines.
204   printInstruction(MI);
205 }
206
207 bool X86IntelAsmPrinter::doInitialization(Module &M) {
208   X86SharedAsmPrinter::doInitialization(M);
209   // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
210   //
211   // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
212   // instruction as a reference to the register named sp, and if you try to
213   // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
214   // before being looked up in the symbol table. This creates spurious
215   // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
216   // mode, and decorate all register names with percent signs.
217   O << "\t.intel_syntax\n";
218   return false;
219 }
220
221 // Include the auto-generated portion of the assembly writer.
222 #include "X86GenAsmWriter1.inc"