gcc often inserts it's own names for sections (e.g.
[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 #define DEBUG_TYPE "asm-printer"
17 #include "X86ATTAsmPrinter.h"
18 #include "X86.h"
19 #include "X86MachineFunctionInfo.h"
20 #include "X86TargetMachine.h"
21 #include "X86TargetAsmInfo.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/Module.h"
24 #include "llvm/Support/Mangler.h"
25 #include "llvm/Target/TargetAsmInfo.h"
26 #include "llvm/Target/TargetOptions.h"
27 #include "llvm/ADT/Statistic.h"
28 using namespace llvm;
29
30 STATISTIC(EmittedInsts, "Number of machine instrs printed");
31
32 /// getSectionForFunction - Return the section that we should emit the
33 /// specified function body into.
34 std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
35   switch (F.getLinkage()) {
36   default: assert(0 && "Unknown linkage type!");
37   case Function::InternalLinkage: 
38   case Function::DLLExportLinkage:
39   case Function::ExternalLinkage:
40     return TAI->getTextSection();
41   case Function::WeakLinkage:
42   case Function::LinkOnceLinkage:
43     if (Subtarget->isTargetDarwin()) {
44       return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
45     } else if (Subtarget->isTargetCygMing()) {
46       return "\t.section\t.text$linkonce." + CurrentFnName + ",\"ax\"\n";
47     } else {
48       return "\t.section\t.llvm.linkonce.t." + CurrentFnName +
49              ",\"ax\",@progbits\n";
50     }
51   }
52 }
53
54 /// runOnMachineFunction - This uses the printMachineInstruction()
55 /// method to print assembly for each instruction.
56 ///
57 bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
58   if (Subtarget->isTargetDarwin() ||
59       Subtarget->isTargetELF() ||
60       Subtarget->isTargetCygMing()) {
61     // Let PassManager know we need debug information and relay
62     // the MachineDebugInfo address on to DwarfWriter.
63     DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
64   }
65
66   SetupMachineFunction(MF);
67   O << "\n\n";
68
69   // Print out constants referenced by the function
70   EmitConstantPool(MF.getConstantPool());
71
72   // Print out labels for the function.
73   const Function *F = MF.getFunction();
74   unsigned CC = F->getCallingConv();
75
76   // Populate function information map.  Actually, We don't want to populate
77   // non-stdcall or non-fastcall functions' information right now.
78   if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
79     FunctionInfoMap[F] = *MF.getInfo<X86FunctionInfo>();
80
81   X86SharedAsmPrinter::decorateName(CurrentFnName, F);
82
83   // Change GNU linkonce to LLVM linkonce name
84   if (F->hasSection() &&
85       (F->getSection().find(".gnu.linkonce.t") != std::string::npos))
86     SwitchToTextSection(getSectionForFunction(*F).c_str(), NULL);
87   else
88     SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
89     
90   switch (F->getLinkage()) {
91   default: assert(0 && "Unknown linkage type!");
92   case Function::InternalLinkage:  // Symbols default to internal.
93     EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
94     break;
95   case Function::DLLExportLinkage:
96     DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
97     //FALLS THROUGH
98   case Function::ExternalLinkage:
99     EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
100     O << "\t.globl\t" << CurrentFnName << "\n";    
101     break;
102   case Function::LinkOnceLinkage:
103   case Function::WeakLinkage:
104     if (Subtarget->isTargetDarwin()) {
105       O << "\t.globl\t" << CurrentFnName << "\n";
106       O << "\t.weak_definition\t" << CurrentFnName << "\n";
107     } else if (Subtarget->isTargetCygMing()) {
108       EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
109       O << "\t.linkonce discard\n";
110       O << "\t.globl " << CurrentFnName << "\n";
111     } else {
112       EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
113       O << "\t.weak " << CurrentFnName << "\n";
114     }
115     break;
116   }
117   O << CurrentFnName << ":\n";
118   // Add some workaround for linkonce linkage on Cygwin\MinGW
119   if (Subtarget->isTargetCygMing() &&
120       (F->getLinkage() == Function::LinkOnceLinkage ||
121        F->getLinkage() == Function::WeakLinkage))
122     O << "_llvm$workaround$fake$stub_" << CurrentFnName << ":\n";
123
124   if (Subtarget->isTargetDarwin() ||
125       Subtarget->isTargetELF() ||
126       Subtarget->isTargetCygMing()) {
127     // Emit pre-function debug information.
128     DW.BeginFunction(&MF);
129   }
130
131   // Print out code for the function.
132   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
133        I != E; ++I) {
134     // Print a label for the basic block.
135     if (I->pred_begin() != I->pred_end()) {
136       printBasicBlockLabel(I, true);
137       O << '\n';
138     }
139     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
140          II != E; ++II) {
141       // Print the assembly for the instruction.
142       O << "\t";
143       printMachineInstruction(II);
144     }
145   }
146
147   // Print out jump tables referenced by the function.
148   
149   // Mac OS X requires that the jump table follow the function, so that the jump
150   // table is part of the same atom that the function is in.
151   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
152   
153   if (TAI->hasDotTypeDotSizeDirective())
154     O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
155
156   if (Subtarget->isTargetDarwin() ||
157       Subtarget->isTargetELF() ||
158       Subtarget->isTargetCygMing()) {
159     // Emit post-function debug information.
160     DW.EndFunction();
161   }
162
163   // We didn't modify anything.
164   return false;
165 }
166
167 void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
168                                     const char *Modifier, bool NotRIPRel) {
169   const MachineOperand &MO = MI->getOperand(OpNo);
170   const MRegisterInfo &RI = *TM.getRegisterInfo();
171   switch (MO.getType()) {
172   case MachineOperand::MO_Register: {
173     assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
174            "Virtual registers should not make it this far!");
175     O << '%';
176     unsigned Reg = MO.getReg();
177     if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
178       MVT::ValueType VT = (strcmp(Modifier+6,"64") == 0) ?
179         MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
180                     ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
181       Reg = getX86SubSuperRegister(Reg, VT);
182     }
183     for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
184       O << (char)tolower(*Name);
185     return;
186   }
187
188   case MachineOperand::MO_Immediate:
189     if (!Modifier || strcmp(Modifier, "debug") != 0)
190       O << '$';
191     O << MO.getImmedValue();
192     return;
193   case MachineOperand::MO_MachineBasicBlock:
194     printBasicBlockLabel(MO.getMachineBasicBlock());
195     return;
196   case MachineOperand::MO_JumpTableIndex: {
197     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
198     if (!isMemOp) O << '$';
199     O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
200       << MO.getJumpTableIndex();
201     if (X86PICStyle == PICStyle::Stub &&
202         TM.getRelocationModel() == Reloc::PIC_)
203       O << "-\"L" << getFunctionNumber() << "$pb\"";
204     if (isMemOp && Subtarget->is64Bit() && !NotRIPRel)
205       O << "(%rip)";
206     return;
207   }
208   case MachineOperand::MO_ConstantPoolIndex: {
209     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
210     if (!isMemOp) O << '$';
211     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
212       << MO.getConstantPoolIndex();
213     if (X86PICStyle == PICStyle::Stub &&
214         TM.getRelocationModel() == Reloc::PIC_)
215       O << "-\"L" << getFunctionNumber() << "$pb\"";
216     int Offset = MO.getOffset();
217     if (Offset > 0)
218       O << "+" << Offset;
219     else if (Offset < 0)
220       O << Offset;
221
222     if (isMemOp && Subtarget->is64Bit() && !NotRIPRel)
223       O << "(%rip)";
224     return;
225   }
226   case MachineOperand::MO_GlobalAddress: {
227     bool isCallOp = Modifier && !strcmp(Modifier, "call");
228     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
229     if (!isMemOp && !isCallOp) O << '$';
230
231     GlobalValue *GV = MO.getGlobal();
232     std::string Name = Mang->getValueName(GV);
233     
234     bool isExt = (GV->isExternal() || GV->hasWeakLinkage() ||
235                   GV->hasLinkOnceLinkage());
236     
237     X86SharedAsmPrinter::decorateName(Name, GV);
238     
239     if (X86PICStyle == PICStyle::Stub &&
240         TM.getRelocationModel() != Reloc::Static) {
241       // Link-once, External, or Weakly-linked global variables need
242       // non-lazily-resolved stubs
243       if (isExt) {
244         // Dynamically-resolved functions need a stub for the function.
245         if (isCallOp && isa<Function>(GV)) {
246           FnStubs.insert(Name);
247           O << "L" << Name << "$stub";
248         } else {
249           GVStubs.insert(Name);
250           O << "L" << Name << "$non_lazy_ptr";
251         }
252       } else {
253         if (GV->hasDLLImportLinkage()) {
254           O << "__imp_";          
255         } 
256         O << Name;
257       }
258       
259       if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
260         O << "-\"L" << getFunctionNumber() << "$pb\"";
261     } else {
262       if (GV->hasDLLImportLinkage()) {
263         O << "__imp_";          
264       }       
265       O << Name;
266     }
267
268     if (GV->hasExternalWeakLinkage())
269       ExtWeakSymbols.insert(GV);
270     
271     int Offset = MO.getOffset();
272     if (Offset > 0)
273       O << "+" << Offset;
274     else if (Offset < 0)
275       O << Offset;
276
277     if (isMemOp && Subtarget->is64Bit()) {
278       if (isExt && TM.getRelocationModel() != Reloc::Static)
279         O << "@GOTPCREL(%rip)";
280       else if (!NotRIPRel)
281         // Use rip when possible to reduce code size, except when index or
282         // base register are also part of the address. e.g.
283         // foo(%rip)(%rcx,%rax,4) is not legal
284         O << "(%rip)";        
285     }
286
287     return;
288   }
289   case MachineOperand::MO_ExternalSymbol: {
290     bool isCallOp = Modifier && !strcmp(Modifier, "call");
291     if (isCallOp && 
292         X86PICStyle == PICStyle::Stub &&
293         TM.getRelocationModel() != Reloc::Static) {
294       std::string Name(TAI->getGlobalPrefix());
295       Name += MO.getSymbolName();
296       FnStubs.insert(Name);
297       O << "L" << Name << "$stub";
298       return;
299     }
300     if (!isCallOp) O << '$';
301     O << TAI->getGlobalPrefix() << MO.getSymbolName();
302
303     if (!isCallOp && Subtarget->is64Bit())
304       O << "(%rip)";
305
306     return;
307   }
308   default:
309     O << "<unknown operand type>"; return;
310   }
311 }
312
313 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
314   unsigned char value = MI->getOperand(Op).getImmedValue();
315   assert(value <= 7 && "Invalid ssecc argument!");
316   switch (value) {
317   case 0: O << "eq"; break;
318   case 1: O << "lt"; break;
319   case 2: O << "le"; break;
320   case 3: O << "unord"; break;
321   case 4: O << "neq"; break;
322   case 5: O << "nlt"; break;
323   case 6: O << "nle"; break;
324   case 7: O << "ord"; break;
325   }
326 }
327
328 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
329                                          const char *Modifier){
330   assert(isMem(MI, Op) && "Invalid memory reference!");
331
332   const MachineOperand &BaseReg  = MI->getOperand(Op);
333   int ScaleVal                   = MI->getOperand(Op+1).getImmedValue();
334   const MachineOperand &IndexReg = MI->getOperand(Op+2);
335   const MachineOperand &DispSpec = MI->getOperand(Op+3);
336
337   if (BaseReg.isFrameIndex()) {
338     O << "[frame slot #" << BaseReg.getFrameIndex();
339     if (DispSpec.getImmedValue())
340       O << " + " << DispSpec.getImmedValue();
341     O << "]";
342     return;
343   }
344
345   bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
346   if (DispSpec.isGlobalAddress() ||
347       DispSpec.isConstantPoolIndex() ||
348       DispSpec.isJumpTableIndex()) {
349     printOperand(MI, Op+3, "mem", NotRIPRel);
350   } else {
351     int DispVal = DispSpec.getImmedValue();
352     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
353       O << DispVal;
354   }
355
356   if (IndexReg.getReg() || BaseReg.getReg()) {
357     O << "(";
358     if (BaseReg.getReg()) {
359       printOperand(MI, Op, Modifier);
360     }
361
362     if (IndexReg.getReg()) {
363       O << ",";
364       printOperand(MI, Op+2, Modifier);
365       if (ScaleVal != 1)
366         O << "," << ScaleVal;
367     }
368
369     O << ")";
370   }
371 }
372
373 void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
374   O << "\"L" << getFunctionNumber() << "$pb\"\n";
375   O << "\"L" << getFunctionNumber() << "$pb\":";
376 }
377
378
379 bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
380                                          const char Mode) {
381   const MRegisterInfo &RI = *TM.getRegisterInfo();
382   unsigned Reg = MO.getReg();
383   switch (Mode) {
384   default: return true;  // Unknown mode.
385   case 'b': // Print QImode register
386     Reg = getX86SubSuperRegister(Reg, MVT::i8);
387     break;
388   case 'h': // Print QImode high register
389     Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
390     break;
391   case 'w': // Print HImode register
392     Reg = getX86SubSuperRegister(Reg, MVT::i16);
393     break;
394   case 'k': // Print SImode register
395     Reg = getX86SubSuperRegister(Reg, MVT::i32);
396     break;
397   }
398
399   O << '%';
400   for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
401     O << (char)tolower(*Name);
402   return false;
403 }
404
405 /// PrintAsmOperand - Print out an operand for an inline asm expression.
406 ///
407 bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
408                                        unsigned AsmVariant, 
409                                        const char *ExtraCode) {
410   // Does this asm operand have a single letter operand modifier?
411   if (ExtraCode && ExtraCode[0]) {
412     if (ExtraCode[1] != 0) return true; // Unknown modifier.
413     
414     switch (ExtraCode[0]) {
415     default: return true;  // Unknown modifier.
416     case 'c': // Don't print "$" before a global var name.
417       printOperand(MI, OpNo, "mem");
418       return false;
419     case 'b': // Print QImode register
420     case 'h': // Print QImode high register
421     case 'w': // Print HImode register
422     case 'k': // Print SImode register
423       return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
424     }
425   }
426   
427   printOperand(MI, OpNo);
428   return false;
429 }
430
431 bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
432                                              unsigned OpNo,
433                                              unsigned AsmVariant, 
434                                              const char *ExtraCode) {
435   if (ExtraCode && ExtraCode[0])
436     return true; // Unknown modifier.
437   printMemReference(MI, OpNo);
438   return false;
439 }
440
441 /// printMachineInstruction -- Print out a single X86 LLVM instruction
442 /// MI in Intel syntax to the current output stream.
443 ///
444 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
445   ++EmittedInsts;
446
447   // See if a truncate instruction can be turned into a nop.
448   switch (MI->getOpcode()) {
449   default: break;
450   case X86::TRUNC_64to32:
451   case X86::TRUNC_64to16:
452   case X86::TRUNC_32to16:
453   case X86::TRUNC_32to8:
454   case X86::TRUNC_16to8:
455   case X86::TRUNC_32_to8:
456   case X86::TRUNC_16_to8: {
457     const MachineOperand &MO0 = MI->getOperand(0);
458     const MachineOperand &MO1 = MI->getOperand(1);
459     unsigned Reg0 = MO0.getReg();
460     unsigned Reg1 = MO1.getReg();
461     unsigned Opc = MI->getOpcode();
462     if (Opc == X86::TRUNC_64to32)
463       Reg1 = getX86SubSuperRegister(Reg1, MVT::i32);
464     else if (Opc == X86::TRUNC_32to16 || Opc == X86::TRUNC_64to16)
465       Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
466     else
467       Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
468     O << TAI->getCommentString() << " TRUNCATE ";
469     if (Reg0 != Reg1)
470       O << "\n\t";
471     break;
472   }
473   case X86::PsMOVZX64rr32:
474     O << TAI->getCommentString() << " ZERO-EXTEND " << "\n\t";
475     break;
476   }
477
478   // Call the autogenerated instruction printer routines.
479   printInstruction(MI);
480 }
481
482 // Include the auto-generated portion of the assembly writer.
483 #include "X86GenAsmWriter.inc"
484