76e137e75b24f942c3488b3ebec786d025555480
[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 (Subtarget->isTargetDarwin()) {
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 labels for the function.
42   const Function *F = MF.getFunction();
43   switch (F->getLinkage()) {
44   default: assert(0 && "Unknown linkage type!");
45   case Function::InternalLinkage:  // Symbols default to internal.
46     SwitchToTextSection(DefaultTextSection, F);
47     EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
48     break;
49   case Function::ExternalLinkage:
50     SwitchToTextSection(DefaultTextSection, F);
51     EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
52     O << "\t.globl\t" << CurrentFnName << "\n";
53     break;
54   case Function::WeakLinkage:
55   case Function::LinkOnceLinkage:
56     if (Subtarget->isTargetDarwin()) {
57       SwitchToTextSection(
58                 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
59       O << "\t.globl\t" << CurrentFnName << "\n";
60       O << "\t.weak_definition\t" << CurrentFnName << "\n";
61     } else if (Subtarget->TargetType == X86Subtarget::isCygwin) {
62       EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
63       O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
64         << ",\"ax\"\n";
65       SwitchToTextSection("", F);
66       O << "\t.weak " << CurrentFnName << "\n";
67     } else {
68       EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
69       O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
70         << ",\"ax\",@progbits\n";
71       SwitchToTextSection("", F);
72       O << "\t.weak " << CurrentFnName << "\n";
73     }
74     break;
75   }
76   O << CurrentFnName << ":\n";
77
78   if (Subtarget->isTargetDarwin()) {
79     // Emit pre-function debug information.
80     DW.BeginFunction(&MF);
81   }
82
83   // Print out code for the function.
84   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
85        I != E; ++I) {
86     // Print a label for the basic block.
87     if (I->pred_begin() != I->pred_end()) {
88       printBasicBlockLabel(I, true);
89       O << '\n';
90     }
91     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
92          II != E; ++II) {
93       // Print the assembly for the instruction.
94       O << "\t";
95       printMachineInstruction(II);
96     }
97   }
98
99   // Print out jump tables referenced by the function
100   // Mac OS X requires at least one non-local (e.g. L1) labels before local
101   // lables that are used in jump table expressions (e.g. LBB1_1-LJT1_0).
102   EmitJumpTableInfo(MF.getJumpTableInfo());
103   
104   if (HasDotTypeDotSizeDirective)
105     O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
106
107   if (Subtarget->isTargetDarwin()) {
108     // Emit post-function debug information.
109     DW.EndFunction();
110   }
111
112   // We didn't modify anything.
113   return false;
114 }
115
116 void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
117                                     const char *Modifier) {
118   const MachineOperand &MO = MI->getOperand(OpNo);
119   const MRegisterInfo &RI = *TM.getRegisterInfo();
120   switch (MO.getType()) {
121   case MachineOperand::MO_Register: {
122     assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
123            "Virtual registers should not make it this far!");
124     O << '%';
125     unsigned Reg = MO.getReg();
126     if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
127       MVT::ValueType VT = (strcmp(Modifier,"subreg16") == 0)
128         ? MVT::i16 : MVT::i8;
129       Reg = getX86SubSuperRegister(Reg, VT);
130     }
131     for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
132       O << (char)tolower(*Name);
133     return;
134   }
135
136   case MachineOperand::MO_Immediate:
137     if (!Modifier || strcmp(Modifier, "debug") != 0)
138       O << '$';
139     O << MO.getImmedValue();
140     return;
141   case MachineOperand::MO_MachineBasicBlock:
142     printBasicBlockLabel(MO.getMachineBasicBlock());
143     return;
144   case MachineOperand::MO_JumpTableIndex: {
145     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
146     if (!isMemOp) O << '$';
147     O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << "_"
148       << MO.getJumpTableIndex();
149     if (Subtarget->isTargetDarwin() && 
150         TM.getRelocationModel() == Reloc::PIC_)
151       O << "-\"L" << getFunctionNumber() << "$pb\"";
152     return;
153   }
154   case MachineOperand::MO_ConstantPoolIndex: {
155     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
156     if (!isMemOp) O << '$';
157     O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
158       << MO.getConstantPoolIndex();
159     if (Subtarget->isTargetDarwin() && 
160         TM.getRelocationModel() == Reloc::PIC_)
161       O << "-\"L" << getFunctionNumber() << "$pb\"";
162     int Offset = MO.getOffset();
163     if (Offset > 0)
164       O << "+" << Offset;
165     else if (Offset < 0)
166       O << Offset;
167     return;
168   }
169   case MachineOperand::MO_GlobalAddress: {
170     bool isCallOp = Modifier && !strcmp(Modifier, "call");
171     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
172     if (!isMemOp && !isCallOp) O << '$';
173     // Darwin block shameless ripped from PPCAsmPrinter.cpp
174     if (Subtarget->isTargetDarwin() && 
175         TM.getRelocationModel() != Reloc::Static) {
176       GlobalValue *GV = MO.getGlobal();
177       std::string Name = Mang->getValueName(GV);
178       // Link-once, External, or Weakly-linked global variables need
179       // non-lazily-resolved stubs
180       if (GV->isExternal() || GV->hasWeakLinkage() ||
181           GV->hasLinkOnceLinkage()) {
182         // Dynamically-resolved functions need a stub for the function.
183         if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
184           FnStubs.insert(Name);
185           O << "L" << Name << "$stub";
186         } else {
187           GVStubs.insert(Name);
188           O << "L" << Name << "$non_lazy_ptr";
189         }
190       } else {
191         O << Mang->getValueName(GV);
192       } 
193       if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
194         O << "-\"L" << getFunctionNumber() << "$pb\"";
195    } else
196       O << Mang->getValueName(MO.getGlobal());
197     int Offset = MO.getOffset();
198     if (Offset > 0)
199       O << "+" << Offset;
200     else if (Offset < 0)
201       O << Offset;
202     return;
203   }
204   case MachineOperand::MO_ExternalSymbol: {
205     bool isCallOp = Modifier && !strcmp(Modifier, "call");
206     if (isCallOp && 
207         Subtarget->isTargetDarwin() && 
208         TM.getRelocationModel() != Reloc::Static) {
209       std::string Name(GlobalPrefix);
210       Name += MO.getSymbolName();
211       FnStubs.insert(Name);
212       O << "L" << Name << "$stub";
213       return;
214     }
215     if (!isCallOp) O << '$';
216     O << GlobalPrefix << MO.getSymbolName();
217     return;
218   }
219   default:
220     O << "<unknown operand type>"; return;
221   }
222 }
223
224 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
225   unsigned char value = MI->getOperand(Op).getImmedValue();
226   assert(value <= 7 && "Invalid ssecc argument!");
227   switch (value) {
228   case 0: O << "eq"; break;
229   case 1: O << "lt"; break;
230   case 2: O << "le"; break;
231   case 3: O << "unord"; break;
232   case 4: O << "neq"; break;
233   case 5: O << "nlt"; break;
234   case 6: O << "nle"; break;
235   case 7: O << "ord"; break;
236   }
237 }
238
239 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
240   assert(isMem(MI, Op) && "Invalid memory reference!");
241
242   const MachineOperand &BaseReg  = MI->getOperand(Op);
243   int ScaleVal                   = MI->getOperand(Op+1).getImmedValue();
244   const MachineOperand &IndexReg = MI->getOperand(Op+2);
245   const MachineOperand &DispSpec = MI->getOperand(Op+3);
246
247   if (BaseReg.isFrameIndex()) {
248     O << "[frame slot #" << BaseReg.getFrameIndex();
249     if (DispSpec.getImmedValue())
250       O << " + " << DispSpec.getImmedValue();
251     O << "]";
252     return;
253   }
254
255   if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
256     printOperand(MI, Op+3, "mem");
257   } else {
258     int DispVal = DispSpec.getImmedValue();
259     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
260       O << DispVal;
261   }
262
263   if (IndexReg.getReg() || BaseReg.getReg()) {
264     O << "(";
265     if (BaseReg.getReg())
266       printOperand(MI, Op);
267
268     if (IndexReg.getReg()) {
269       O << ",";
270       printOperand(MI, Op+2);
271       if (ScaleVal != 1)
272         O << "," << ScaleVal;
273     }
274
275     O << ")";
276   }
277 }
278
279 void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
280   O << "\"L" << getFunctionNumber() << "$pb\"\n";
281   O << "\"L" << getFunctionNumber() << "$pb\":";
282 }
283
284
285 bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
286                                          const char Mode) {
287   const MRegisterInfo &RI = *TM.getRegisterInfo();
288   unsigned Reg = MO.getReg();
289   switch (Mode) {
290   default: return true;  // Unknown mode.
291   case 'b': // Print QImode register
292     Reg = getX86SubSuperRegister(Reg, MVT::i8);
293     break;
294   case 'h': // Print QImode high register
295     Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
296     break;
297   case 'w': // Print HImode register
298     Reg = getX86SubSuperRegister(Reg, MVT::i16);
299     break;
300   case 'k': // Print SImode register
301     Reg = getX86SubSuperRegister(Reg, MVT::i32);
302     break;
303   }
304
305   O << '%';
306   for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
307     O << (char)tolower(*Name);
308   return false;
309 }
310
311 /// PrintAsmOperand - Print out an operand for an inline asm expression.
312 ///
313 bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
314                                        unsigned AsmVariant, 
315                                        const char *ExtraCode) {
316   // Does this asm operand have a single letter operand modifier?
317   if (ExtraCode && ExtraCode[0]) {
318     if (ExtraCode[1] != 0) return true; // Unknown modifier.
319     
320     switch (ExtraCode[0]) {
321     default: return true;  // Unknown modifier.
322     case 'b': // Print QImode register
323     case 'h': // Print QImode high register
324     case 'w': // Print HImode register
325     case 'k': // Print SImode register
326       return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
327     }
328   }
329   
330   printOperand(MI, OpNo);
331   return false;
332 }
333
334 bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
335                                              unsigned OpNo,
336                                              unsigned AsmVariant, 
337                                              const char *ExtraCode) {
338   if (ExtraCode && ExtraCode[0])
339     return true; // Unknown modifier.
340   printMemReference(MI, OpNo);
341   return false;
342 }
343
344 /// printMachineInstruction -- Print out a single X86 LLVM instruction
345 /// MI in Intel syntax to the current output stream.
346 ///
347 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
348   ++EmittedInsts;
349   // This works around some Darwin assembler bugs.
350   if (Subtarget->isTargetDarwin()) {
351     switch (MI->getOpcode()) {
352     case X86::REP_MOVSB:
353       O << "rep/movsb (%esi),(%edi)\n";
354       return;
355     case X86::REP_MOVSD:
356       O << "rep/movsl (%esi),(%edi)\n";
357       return;
358     case X86::REP_MOVSW:
359       O << "rep/movsw (%esi),(%edi)\n";
360       return;
361     case X86::REP_STOSB:
362       O << "rep/stosb\n";
363       return;
364     case X86::REP_STOSD:
365       O << "rep/stosl\n";
366       return;
367     case X86::REP_STOSW:
368       O << "rep/stosw\n";
369       return;
370     default:
371       break;
372     }
373   }
374
375   // See if a truncate instruction can be turned into a nop.
376   switch (MI->getOpcode()) {
377   default: break;
378   case X86::TRUNC_GR32_GR16:
379   case X86::TRUNC_GR32_GR8:
380   case X86::TRUNC_GR16_GR8: {
381     const MachineOperand &MO0 = MI->getOperand(0);
382     const MachineOperand &MO1 = MI->getOperand(1);
383     unsigned Reg0 = MO0.getReg();
384     unsigned Reg1 = MO1.getReg();
385     if (MI->getOpcode() == X86::TRUNC_GR32_GR16)
386       Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
387     else
388       Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
389     O << CommentString << " TRUNCATE ";
390     if (Reg0 != Reg1)
391       O << "\n\t";
392     break;
393   }
394   }
395
396   // Call the autogenerated instruction printer routines.
397   printInstruction(MI);
398 }
399
400 // Include the auto-generated portion of the assembly writer.
401 #include "X86GenAsmWriter.inc"
402