Separate target specific asm properties from the asm printers.
[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(TAI->getTextSection(), F);
47     EmitAlignment(4, F);     // FIXME: This should be parameterized somewhere.
48     break;
49   case Function::ExternalLinkage:
50     SwitchToTextSection(TAI->getTextSection(), 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 (TAI->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 << TAI->getPrivateGlobalPrefix() << "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 << TAI->getPrivateGlobalPrefix() << "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(TAI->getGlobalPrefix());
210       Name += MO.getSymbolName();
211       FnStubs.insert(Name);
212       O << "L" << Name << "$stub";
213       return;
214     }
215     if (!isCallOp) O << '$';
216     O << TAI->getGlobalPrefix() << 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() ||
256       DispSpec.isConstantPoolIndex() ||
257       DispSpec.isJumpTableIndex()) {
258     printOperand(MI, Op+3, "mem");
259   } else {
260     int DispVal = DispSpec.getImmedValue();
261     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
262       O << DispVal;
263   }
264
265   if (IndexReg.getReg() || BaseReg.getReg()) {
266     O << "(";
267     if (BaseReg.getReg())
268       printOperand(MI, Op);
269
270     if (IndexReg.getReg()) {
271       O << ",";
272       printOperand(MI, Op+2);
273       if (ScaleVal != 1)
274         O << "," << ScaleVal;
275     }
276
277     O << ")";
278   }
279 }
280
281 void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
282   O << "\"L" << getFunctionNumber() << "$pb\"\n";
283   O << "\"L" << getFunctionNumber() << "$pb\":";
284 }
285
286
287 bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
288                                          const char Mode) {
289   const MRegisterInfo &RI = *TM.getRegisterInfo();
290   unsigned Reg = MO.getReg();
291   switch (Mode) {
292   default: return true;  // Unknown mode.
293   case 'b': // Print QImode register
294     Reg = getX86SubSuperRegister(Reg, MVT::i8);
295     break;
296   case 'h': // Print QImode high register
297     Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
298     break;
299   case 'w': // Print HImode register
300     Reg = getX86SubSuperRegister(Reg, MVT::i16);
301     break;
302   case 'k': // Print SImode register
303     Reg = getX86SubSuperRegister(Reg, MVT::i32);
304     break;
305   }
306
307   O << '%';
308   for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
309     O << (char)tolower(*Name);
310   return false;
311 }
312
313 /// PrintAsmOperand - Print out an operand for an inline asm expression.
314 ///
315 bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
316                                        unsigned AsmVariant, 
317                                        const char *ExtraCode) {
318   // Does this asm operand have a single letter operand modifier?
319   if (ExtraCode && ExtraCode[0]) {
320     if (ExtraCode[1] != 0) return true; // Unknown modifier.
321     
322     switch (ExtraCode[0]) {
323     default: return true;  // Unknown modifier.
324     case 'b': // Print QImode register
325     case 'h': // Print QImode high register
326     case 'w': // Print HImode register
327     case 'k': // Print SImode register
328       return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
329     }
330   }
331   
332   printOperand(MI, OpNo);
333   return false;
334 }
335
336 bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
337                                              unsigned OpNo,
338                                              unsigned AsmVariant, 
339                                              const char *ExtraCode) {
340   if (ExtraCode && ExtraCode[0])
341     return true; // Unknown modifier.
342   printMemReference(MI, OpNo);
343   return false;
344 }
345
346 /// printMachineInstruction -- Print out a single X86 LLVM instruction
347 /// MI in Intel syntax to the current output stream.
348 ///
349 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
350   ++EmittedInsts;
351   // This works around some Darwin assembler bugs.
352   if (Subtarget->isTargetDarwin()) {
353     switch (MI->getOpcode()) {
354     case X86::REP_MOVSB:
355       O << "rep/movsb (%esi),(%edi)\n";
356       return;
357     case X86::REP_MOVSD:
358       O << "rep/movsl (%esi),(%edi)\n";
359       return;
360     case X86::REP_MOVSW:
361       O << "rep/movsw (%esi),(%edi)\n";
362       return;
363     case X86::REP_STOSB:
364       O << "rep/stosb\n";
365       return;
366     case X86::REP_STOSD:
367       O << "rep/stosl\n";
368       return;
369     case X86::REP_STOSW:
370       O << "rep/stosw\n";
371       return;
372     default:
373       break;
374     }
375   }
376
377   // See if a truncate instruction can be turned into a nop.
378   switch (MI->getOpcode()) {
379   default: break;
380   case X86::TRUNC_GR32_GR16:
381   case X86::TRUNC_GR32_GR8:
382   case X86::TRUNC_GR16_GR8: {
383     const MachineOperand &MO0 = MI->getOperand(0);
384     const MachineOperand &MO1 = MI->getOperand(1);
385     unsigned Reg0 = MO0.getReg();
386     unsigned Reg1 = MO1.getReg();
387     if (MI->getOpcode() == X86::TRUNC_GR32_GR16)
388       Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
389     else
390       Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
391     O << TAI->getCommentString() << " TRUNCATE ";
392     if (Reg0 != Reg1)
393       O << "\n\t";
394     break;
395   }
396   }
397
398   // Call the autogenerated instruction printer routines.
399   printInstruction(MI);
400 }
401
402 // Include the auto-generated portion of the assembly writer.
403 #include "X86GenAsmWriter.inc"
404