JumpTable support! What this represents is working asm and jit support for
[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   // Print out constants referenced by the function
38   EmitConstantPool(MF.getConstantPool());
39
40   // Print out labels for the function.
41   SwitchSection("\t.text\n", MF.getFunction());
42   EmitAlignment(4);
43   O << "\t.globl\t" << CurrentFnName << "\n";
44   if (HasDotTypeDotSizeDirective)
45     O << "\t.type\t" << CurrentFnName << ", @function\n";
46   O << CurrentFnName << ":\n";
47   
48   if (forDarwin) {
49     // Emit pre-function debug information.
50     DW.BeginFunction(&MF);
51   }
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     printBasicBlockLabel(MO.getMachineBasicBlock());
117     return;
118   case MachineOperand::MO_PCRelativeDisp:
119     assert(0 && "Shouldn't use addPCDisp() when building X86 MachineInstrs");
120     abort ();
121     return;
122   case MachineOperand::MO_ConstantPoolIndex: {
123     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
124     if (!isMemOp) O << "OFFSET ";
125     O << "[" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
126       << MO.getConstantPoolIndex();
127     if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
128       O << "-\"L" << getFunctionNumber() << "$pb\"";
129     int Offset = MO.getOffset();
130     if (Offset > 0)
131       O << " + " << Offset;
132     else if (Offset < 0)
133       O << Offset;
134     O << "]";
135     return;
136   }
137   case MachineOperand::MO_GlobalAddress: {
138     bool isCallOp = Modifier && !strcmp(Modifier, "call");
139     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
140     if (!isMemOp && !isCallOp) O << "OFFSET ";
141     if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
142       GlobalValue *GV = MO.getGlobal();
143       std::string Name = Mang->getValueName(GV);
144       if (!isMemOp && !isCallOp) O << '$';
145       // Link-once, External, or Weakly-linked global variables need
146       // non-lazily-resolved stubs
147       if (GV->isExternal() || GV->hasWeakLinkage() ||
148           GV->hasLinkOnceLinkage()) {
149         // Dynamically-resolved functions need a stub for the function.
150         if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
151           FnStubs.insert(Name);
152           O << "L" << Name << "$stub";
153         } else {
154           GVStubs.insert(Name);
155           O << "L" << Name << "$non_lazy_ptr";
156         }
157       } else {
158         O << Mang->getValueName(GV);
159       }
160       if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
161         O << "-\"L" << getFunctionNumber() << "$pb\"";
162     } else
163       O << Mang->getValueName(MO.getGlobal());
164     int Offset = MO.getOffset();
165     if (Offset > 0)
166       O << " + " << Offset;
167     else if (Offset < 0)
168       O << Offset;
169     return;
170   }
171   case MachineOperand::MO_ExternalSymbol: {
172     bool isCallOp = Modifier && !strcmp(Modifier, "call");
173     if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
174       std::string Name(GlobalPrefix);
175       Name += MO.getSymbolName();
176       FnStubs.insert(Name);
177       O << "L" << Name << "$stub";
178       return;
179     }
180     if (!isCallOp) O << "OFFSET ";
181     O << GlobalPrefix << MO.getSymbolName();
182     return;
183   }
184   default:
185     O << "<unknown operand type>"; return;
186   }
187 }
188
189 void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
190   assert(isMem(MI, Op) && "Invalid memory reference!");
191
192   const MachineOperand &BaseReg  = MI->getOperand(Op);
193   int ScaleVal                   = MI->getOperand(Op+1).getImmedValue();
194   const MachineOperand &IndexReg = MI->getOperand(Op+2);
195   const MachineOperand &DispSpec = MI->getOperand(Op+3);
196
197   if (BaseReg.isFrameIndex()) {
198     O << "[frame slot #" << BaseReg.getFrameIndex();
199     if (DispSpec.getImmedValue())
200       O << " + " << DispSpec.getImmedValue();
201     O << "]";
202     return;
203   }
204
205   O << "[";
206   bool NeedPlus = false;
207   if (BaseReg.getReg()) {
208     printOp(BaseReg, "mem");
209     NeedPlus = true;
210   }
211
212   if (IndexReg.getReg()) {
213     if (NeedPlus) O << " + ";
214     if (ScaleVal != 1)
215       O << ScaleVal << "*";
216     printOp(IndexReg);
217     NeedPlus = true;
218   }
219
220   if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
221     if (NeedPlus)
222       O << " + ";
223     printOp(DispSpec, "mem");
224   } else {
225     int DispVal = DispSpec.getImmedValue();
226     if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
227       if (NeedPlus)
228         if (DispVal > 0)
229           O << " + ";
230         else {
231           O << " - ";
232           DispVal = -DispVal;
233         }
234       O << DispVal;
235     }
236   }
237   O << "]";
238 }
239
240 void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
241   O << "\"L" << getFunctionNumber() << "$pb\"\n";
242   O << "\"L" << getFunctionNumber() << "$pb\":";
243 }
244
245 /// printMachineInstruction -- Print out a single X86 LLVM instruction
246 /// MI in Intel syntax to the current output stream.
247 ///
248 void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
249   ++EmittedInsts;
250
251   // Call the autogenerated instruction printer routines.
252   printInstruction(MI);
253 }
254
255 bool X86IntelAsmPrinter::doInitialization(Module &M) {
256   X86SharedAsmPrinter::doInitialization(M);
257   // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
258   //
259   // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
260   // instruction as a reference to the register named sp, and if you try to
261   // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
262   // before being looked up in the symbol table. This creates spurious
263   // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
264   // mode, and decorate all register names with percent signs.
265   O << "\t.intel_syntax\n";
266   return false;
267 }
268
269 // Include the auto-generated portion of the assembly writer.
270 #include "X86GenAsmWriter1.inc"