JumpTable support! What this represents is working asm and jit support for
[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 "llvm/Target/TargetOptions.h"
22 #include <iostream>
23 using namespace llvm;
24
25 /// runOnMachineFunction - This uses the printMachineInstruction()
26 /// method to print assembly for each instruction.
27 ///
28 bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
29   //  if (forDarwin) {
30     // Let PassManager know we need debug information and relay
31     // the MachineDebugInfo address on to DwarfWriter.
32     DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
33     //  }
34
35   SetupMachineFunction(MF);
36   O << "\n\n";
37
38   // Print out constants referenced by the function
39   EmitConstantPool(MF.getConstantPool());
40
41   // Print out jump tables referenced by the function
42   EmitJumpTableInfo(MF.getJumpTableInfo());
43   
44   // Print out labels for the function.
45   const Function *F = MF.getFunction();
46   switch (F->getLinkage()) {
47   default: assert(0 && "Unknown linkage type!");
48   case Function::InternalLinkage:  // Symbols default to internal.
49     SwitchSection(".text", F);
50     EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
51     break;
52   case Function::ExternalLinkage:
53     SwitchSection(".text", F);
54     EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
55     O << "\t.globl\t" << CurrentFnName << "\n";
56     break;
57   case Function::WeakLinkage:
58   case Function::LinkOnceLinkage:
59     if (forDarwin) {
60       SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
61                     F);
62       O << "\t.globl\t" << CurrentFnName << "\n";
63       O << "\t.weak_definition\t" << CurrentFnName << "\n";
64     } else {
65       EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
66       O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
67         << ",\"ax\",@progbits\n";
68       O << "\t.weak " << CurrentFnName << "\n";
69     }
70     break;
71   }
72   O << CurrentFnName << ":\n";
73
74   if (forDarwin) {
75     // Emit pre-function debug information.
76     DW.BeginFunction(&MF);
77   }
78
79   // Print out code for the function.
80   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
81        I != E; ++I) {
82     // Print a label for the basic block.
83     if (I->pred_begin() != I->pred_end())
84       O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
85         << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
86         << "\n";
87     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
88          II != E; ++II) {
89       // Print the assembly for the instruction.
90       O << "\t";
91       printMachineInstruction(II);
92     }
93   }
94   if (HasDotTypeDotSizeDirective)
95     O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
96
97   if (forDarwin) {
98     // Emit post-function debug information.
99     DW.EndFunction();
100   }
101
102   // We didn't modify anything.
103   return false;
104 }
105
106 void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
107                                     const char *Modifier) {
108   const MachineOperand &MO = MI->getOperand(OpNo);
109   const MRegisterInfo &RI = *TM.getRegisterInfo();
110   switch (MO.getType()) {
111   case MachineOperand::MO_VirtualRegister:
112   case MachineOperand::MO_MachineRegister:
113     assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
114            "Virtual registers should not make it this far!");
115     O << '%';
116     for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
117       O << (char)tolower(*Name);
118     return;
119
120   case MachineOperand::MO_SignExtendedImmed:
121   case MachineOperand::MO_UnextendedImmed:
122     if (!Modifier || strcmp(Modifier, "debug") != 0)
123       O << '$';
124     O << (int)MO.getImmedValue();
125     return;
126   case MachineOperand::MO_MachineBasicBlock:
127     printBasicBlockLabel(MO.getMachineBasicBlock());
128     return;
129   case MachineOperand::MO_PCRelativeDisp:
130     std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
131     abort ();
132     return;
133   case MachineOperand::MO_JumpTableIndex: {
134     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
135     if (!isMemOp) O << '$';
136     O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << "_"
137       << MO.getJumpTableIndex();
138     // FIXME: PIC relocation model
139     return;
140   }
141   case MachineOperand::MO_ConstantPoolIndex: {
142     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
143     if (!isMemOp) O << '$';
144     O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
145       << MO.getConstantPoolIndex();
146     if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
147       O << "-\"L" << getFunctionNumber() << "$pb\"";
148     int Offset = MO.getOffset();
149     if (Offset > 0)
150       O << "+" << Offset;
151     else if (Offset < 0)
152       O << Offset;
153     return;
154   }
155   case MachineOperand::MO_GlobalAddress: {
156     bool isCallOp = Modifier && !strcmp(Modifier, "call");
157     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
158     if (!isMemOp && !isCallOp) O << '$';
159     // Darwin block shameless ripped from PPCAsmPrinter.cpp
160     if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
161       GlobalValue *GV = MO.getGlobal();
162       std::string Name = Mang->getValueName(GV);
163       // Link-once, External, or Weakly-linked global variables need
164       // non-lazily-resolved stubs
165       if (GV->isExternal() || GV->hasWeakLinkage() ||
166           GV->hasLinkOnceLinkage()) {
167         // Dynamically-resolved functions need a stub for the function.
168         if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
169           FnStubs.insert(Name);
170           O << "L" << Name << "$stub";
171         } else {
172           GVStubs.insert(Name);
173           O << "L" << Name << "$non_lazy_ptr";
174         }
175       } else {
176         O << Mang->getValueName(GV);
177       } 
178       if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
179         O << "-\"L" << getFunctionNumber() << "$pb\"";
180    } else
181       O << Mang->getValueName(MO.getGlobal());
182     int Offset = MO.getOffset();
183     if (Offset > 0)
184       O << "+" << Offset;
185     else if (Offset < 0)
186       O << Offset;
187     return;
188   }
189   case MachineOperand::MO_ExternalSymbol: {
190     bool isCallOp = Modifier && !strcmp(Modifier, "call");
191     if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
192       std::string Name(GlobalPrefix);
193       Name += MO.getSymbolName();
194       FnStubs.insert(Name);
195       O << "L" << Name << "$stub";
196       return;
197     }
198     if (!isCallOp) O << '$';
199     O << GlobalPrefix << MO.getSymbolName();
200     return;
201   }
202   default:
203     O << "<unknown operand type>"; return;
204   }
205 }
206
207 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
208   unsigned char value = MI->getOperand(Op).getImmedValue();
209   assert(value <= 7 && "Invalid ssecc argument!");
210   switch (value) {
211   case 0: O << "eq"; break;
212   case 1: O << "lt"; break;
213   case 2: O << "le"; break;
214   case 3: O << "unord"; break;
215   case 4: O << "neq"; break;
216   case 5: O << "nlt"; break;
217   case 6: O << "nle"; break;
218   case 7: O << "ord"; break;
219   }
220 }
221
222 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
223   assert(isMem(MI, Op) && "Invalid memory reference!");
224
225   const MachineOperand &BaseReg  = MI->getOperand(Op);
226   int ScaleVal                   = MI->getOperand(Op+1).getImmedValue();
227   const MachineOperand &IndexReg = MI->getOperand(Op+2);
228   const MachineOperand &DispSpec = MI->getOperand(Op+3);
229
230   if (BaseReg.isFrameIndex()) {
231     O << "[frame slot #" << BaseReg.getFrameIndex();
232     if (DispSpec.getImmedValue())
233       O << " + " << DispSpec.getImmedValue();
234     O << "]";
235     return;
236   }
237
238   if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
239     printOperand(MI, Op+3, "mem");
240   } else {
241     int DispVal = DispSpec.getImmedValue();
242     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
243       O << DispVal;
244   }
245
246   if (IndexReg.getReg() || BaseReg.getReg()) {
247     O << "(";
248     if (BaseReg.getReg())
249       printOperand(MI, Op);
250
251     if (IndexReg.getReg()) {
252       O << ",";
253       printOperand(MI, Op+2);
254       if (ScaleVal != 1)
255         O << "," << ScaleVal;
256     }
257
258     O << ")";
259   }
260 }
261
262 void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
263   O << "\"L" << getFunctionNumber() << "$pb\"\n";
264   O << "\"L" << getFunctionNumber() << "$pb\":";
265 }
266
267 /// printMachineInstruction -- Print out a single X86 LLVM instruction
268 /// MI in Intel syntax to the current output stream.
269 ///
270 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
271   ++EmittedInsts;
272   // This works around some Darwin assembler bugs.
273   if (forDarwin) {
274     switch (MI->getOpcode()) {
275     case X86::REP_MOVSB:
276       O << "rep/movsb (%esi),(%edi)\n";
277       return;
278     case X86::REP_MOVSD:
279       O << "rep/movsl (%esi),(%edi)\n";
280       return;
281     case X86::REP_MOVSW:
282       O << "rep/movsw (%esi),(%edi)\n";
283       return;
284     case X86::REP_STOSB:
285       O << "rep/stosb\n";
286       return;
287     case X86::REP_STOSD:
288       O << "rep/stosl\n";
289       return;
290     case X86::REP_STOSW:
291       O << "rep/stosw\n";
292       return;
293     default:
294       break;
295     }
296   }
297
298   // Call the autogenerated instruction printer routines.
299   printInstruction(MI);
300 }
301
302 // Include the auto-generated portion of the assembly writer.
303 #include "X86GenAsmWriter.inc"
304