Add all of the data stream intrinsics and instructions. woo
[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 #include "llvm/Target/TargetOptions.h"
22 using namespace llvm;
23
24 /// runOnMachineFunction - This uses the printMachineInstruction()
25 /// method to print assembly for each instruction.
26 ///
27 bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
28   if (forDarwin) {
29     // Let PassManager know we need debug information and relay
30     // the MachineDebugInfo address on to DwarfWriter.
31     DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
32   }
33
34   SetupMachineFunction(MF);
35   O << "\n\n";
36
37   if (forDarwin) {
38     // Emit pre-function debug information.
39     DW.BeginFunction(&MF);
40   }
41
42   // Print out constants referenced by the function
43   EmitConstantPool(MF.getConstantPool());
44
45   // Print out labels for the function.
46   SwitchSection("\t.text\n", MF.getFunction());
47   EmitAlignment(4);
48   O << "\t.globl\t" << CurrentFnName << "\n";
49   if (HasDotTypeDotSizeDirective)
50     O << "\t.type\t" << CurrentFnName << ", @function\n";
51   O << CurrentFnName << ":\n";
52
53   // Print out code for the function.
54   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
55        I != E; ++I) {
56     // Print a label for the basic block if there are any predecessors.
57     if (I->pred_begin() != I->pred_end())
58       O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
59         << ":\t"
60         << CommentString << " " << I->getBasicBlock()->getName() << "\n";
61     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
62          II != E; ++II) {
63       // Print the assembly for the instruction.
64       O << "\t";
65       printMachineInstruction(II);
66     }
67   }
68
69   if (forDarwin) {
70     // Emit post-function debug information.
71     DW.EndFunction();
72   }
73
74   // We didn't modify anything.
75   return false;
76 }
77
78 void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
79   unsigned char value = MI->getOperand(Op).getImmedValue();
80   assert(value <= 7 && "Invalid ssecc argument!");
81   switch (value) {
82   case 0: O << "eq"; break;
83   case 1: O << "lt"; break;
84   case 2: O << "le"; break;
85   case 3: O << "unord"; break;
86   case 4: O << "neq"; break;
87   case 5: O << "nlt"; break;
88   case 6: O << "nle"; break;
89   case 7: O << "ord"; break;
90   }
91 }
92
93 void X86IntelAsmPrinter::printOp(const MachineOperand &MO, 
94                                  const char *Modifier) {
95   const MRegisterInfo &RI = *TM.getRegisterInfo();
96   switch (MO.getType()) {
97   case MachineOperand::MO_VirtualRegister:
98     if (Value *V = MO.getVRegValueOrNull()) {
99       O << "<" << V->getName() << ">";
100       return;
101     }
102     // FALLTHROUGH
103   case MachineOperand::MO_MachineRegister:
104     if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
105       // Bug Workaround: See note in Printer::doInitialization about %.
106       O << "%" << RI.get(MO.getReg()).Name;
107     else
108       O << "%reg" << MO.getReg();
109     return;
110
111   case MachineOperand::MO_SignExtendedImmed:
112   case MachineOperand::MO_UnextendedImmed:
113     O << (int)MO.getImmedValue();
114     return;
115   case MachineOperand::MO_MachineBasicBlock: {
116     MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
117     O << PrivateGlobalPrefix << "BB"
118       << Mang->getValueName(MBBOp->getParent()->getFunction())
119       << "_" << MBBOp->getNumber () << "\t# "
120       << MBBOp->getBasicBlock ()->getName ();
121     return;
122   }
123   case MachineOperand::MO_PCRelativeDisp:
124     assert(0 && "Shouldn't use addPCDisp() when building X86 MachineInstrs");
125     abort ();
126     return;
127   case MachineOperand::MO_ConstantPoolIndex: {
128     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
129     if (!isMemOp) O << "OFFSET ";
130     O << "[" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
131       << MO.getConstantPoolIndex();
132     if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
133       O << "-\"L" << getFunctionNumber() << "$pb\"";
134     int Offset = MO.getOffset();
135     if (Offset > 0)
136       O << " + " << Offset;
137     else if (Offset < 0)
138       O << Offset;
139     O << "]";
140     return;
141   }
142   case MachineOperand::MO_GlobalAddress: {
143     bool isCallOp = Modifier && !strcmp(Modifier, "call");
144     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
145     if (!isMemOp && !isCallOp) O << "OFFSET ";
146     if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
147       GlobalValue *GV = MO.getGlobal();
148       std::string Name = Mang->getValueName(GV);
149       if (!isMemOp && !isCallOp) O << '$';
150       // Link-once, External, or Weakly-linked global variables need
151       // non-lazily-resolved stubs
152       if (GV->isExternal() || GV->hasWeakLinkage() ||
153           GV->hasLinkOnceLinkage()) {
154         // Dynamically-resolved functions need a stub for the function.
155         if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
156           FnStubs.insert(Name);
157           O << "L" << Name << "$stub";
158         } else {
159           GVStubs.insert(Name);
160           O << "L" << Name << "$non_lazy_ptr";
161         }
162       } else {
163         O << Mang->getValueName(GV);
164       }
165       if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
166         O << "-\"L" << getFunctionNumber() << "$pb\"";
167     } else
168       O << Mang->getValueName(MO.getGlobal());
169     int Offset = MO.getOffset();
170     if (Offset > 0)
171       O << " + " << Offset;
172     else if (Offset < 0)
173       O << Offset;
174     return;
175   }
176   case MachineOperand::MO_ExternalSymbol: {
177     bool isCallOp = Modifier && !strcmp(Modifier, "call");
178     if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
179       std::string Name(GlobalPrefix);
180       Name += MO.getSymbolName();
181       FnStubs.insert(Name);
182       O << "L" << Name << "$stub";
183       return;
184     }
185     if (!isCallOp) O << "OFFSET ";
186     O << GlobalPrefix << MO.getSymbolName();
187     return;
188   }
189   default:
190     O << "<unknown operand type>"; return;
191   }
192 }
193
194 void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
195   assert(isMem(MI, Op) && "Invalid memory reference!");
196
197   const MachineOperand &BaseReg  = MI->getOperand(Op);
198   int ScaleVal                   = MI->getOperand(Op+1).getImmedValue();
199   const MachineOperand &IndexReg = MI->getOperand(Op+2);
200   const MachineOperand &DispSpec = MI->getOperand(Op+3);
201
202   if (BaseReg.isFrameIndex()) {
203     O << "[frame slot #" << BaseReg.getFrameIndex();
204     if (DispSpec.getImmedValue())
205       O << " + " << DispSpec.getImmedValue();
206     O << "]";
207     return;
208   }
209
210   O << "[";
211   bool NeedPlus = false;
212   if (BaseReg.getReg()) {
213     printOp(BaseReg, "mem");
214     NeedPlus = true;
215   }
216
217   if (IndexReg.getReg()) {
218     if (NeedPlus) O << " + ";
219     if (ScaleVal != 1)
220       O << ScaleVal << "*";
221     printOp(IndexReg);
222     NeedPlus = true;
223   }
224
225   if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
226     if (NeedPlus)
227       O << " + ";
228     printOp(DispSpec, "mem");
229   } else {
230     int DispVal = DispSpec.getImmedValue();
231     if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
232       if (NeedPlus)
233         if (DispVal > 0)
234           O << " + ";
235         else {
236           O << " - ";
237           DispVal = -DispVal;
238         }
239       O << DispVal;
240     }
241   }
242   O << "]";
243 }
244
245 void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
246   O << "\"L" << getFunctionNumber() << "$pb\"\n";
247   O << "\"L" << getFunctionNumber() << "$pb\":";
248 }
249
250 /// printMachineInstruction -- Print out a single X86 LLVM instruction
251 /// MI in Intel syntax to the current output stream.
252 ///
253 void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
254   ++EmittedInsts;
255
256   // Call the autogenerated instruction printer routines.
257   printInstruction(MI);
258 }
259
260 bool X86IntelAsmPrinter::doInitialization(Module &M) {
261   X86SharedAsmPrinter::doInitialization(M);
262   // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
263   //
264   // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
265   // instruction as a reference to the register named sp, and if you try to
266   // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
267   // before being looked up in the symbol table. This creates spurious
268   // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
269   // mode, and decorate all register names with percent signs.
270   O << "\t.intel_syntax\n";
271   return false;
272 }
273
274 // Include the auto-generated portion of the assembly writer.
275 #include "X86GenAsmWriter1.inc"