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