I can't spell: Register, not Regsiter.
[oota-llvm.git] / lib / Target / X86 / X86IntelAsmPrinter.cpp
1 //===-- X86IntelAsmPrinter.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 Intel format assembly language.
12 // This printer is the output mechanism used by `llc'.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "X86IntelAsmPrinter.h"
17 #include "X86.h"
18 #include "llvm/Module.h"
19 #include "llvm/Assembly/Writer.h"
20 #include "llvm/Support/Mangler.h"
21 #include "llvm/Target/TargetOptions.h"
22 using namespace llvm;
23
24 /// runOnMachineFunction - This uses the printMachineInstruction()
25 /// method to print assembly for each instruction.
26 ///
27 bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
28   if (forDarwin) {
29     // Let PassManager know we need debug information and relay
30     // the MachineDebugInfo address on to DwarfWriter.
31     DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
32   }
33
34   SetupMachineFunction(MF);
35   O << "\n\n";
36
37   // Print out constants referenced by the function
38   EmitConstantPool(MF.getConstantPool());
39
40   // Print out labels for the function.
41   SwitchSection("\t.text\n", MF.getFunction());
42   EmitAlignment(4);
43   O << "\t.globl\t" << CurrentFnName << "\n";
44   if (HasDotTypeDotSizeDirective)
45     O << "\t.type\t" << CurrentFnName << ", @function\n";
46   O << CurrentFnName << ":\n";
47   
48   if (forDarwin) {
49     // Emit pre-function debug information.
50     DW.BeginFunction(&MF);
51   }
52
53   // Print out code for the function.
54   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
55        I != E; ++I) {
56     // Print a label for the basic block if there are any predecessors.
57     if (I->pred_begin() != I->pred_end())
58       O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
59         << ":\t"
60         << CommentString << " " << I->getBasicBlock()->getName() << "\n";
61     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
62          II != E; ++II) {
63       // Print the assembly for the instruction.
64       O << "\t";
65       printMachineInstruction(II);
66     }
67   }
68
69   if (forDarwin) {
70     // Emit post-function debug information.
71     DW.EndFunction();
72   }
73
74   // We didn't modify anything.
75   return false;
76 }
77
78 void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
79   unsigned char value = MI->getOperand(Op).getImmedValue();
80   assert(value <= 7 && "Invalid ssecc argument!");
81   switch (value) {
82   case 0: O << "eq"; break;
83   case 1: O << "lt"; break;
84   case 2: O << "le"; break;
85   case 3: O << "unord"; break;
86   case 4: O << "neq"; break;
87   case 5: O << "nlt"; break;
88   case 6: O << "nle"; break;
89   case 7: O << "ord"; break;
90   }
91 }
92
93 void X86IntelAsmPrinter::printOp(const MachineOperand &MO, 
94                                  const char *Modifier) {
95   const MRegisterInfo &RI = *TM.getRegisterInfo();
96   switch (MO.getType()) {
97   case MachineOperand::MO_VirtualRegister:
98     if (Value *V = MO.getVRegValueOrNull()) {
99       O << "<" << V->getName() << ">";
100       return;
101     }
102     // FALLTHROUGH
103   case MachineOperand::MO_MachineRegister:
104     if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
105       // Bug Workaround: See note in Printer::doInitialization about %.
106       O << "%" << RI.get(MO.getReg()).Name;
107     else
108       O << "%reg" << MO.getReg();
109     return;
110
111   case MachineOperand::MO_SignExtendedImmed:
112   case MachineOperand::MO_UnextendedImmed:
113     O << (int)MO.getImmedValue();
114     return;
115   case MachineOperand::MO_MachineBasicBlock:
116     printBasicBlockLabel(MO.getMachineBasicBlock());
117     return;
118   case MachineOperand::MO_PCRelativeDisp:
119     assert(0 && "Shouldn't use addPCDisp() when building X86 MachineInstrs");
120     abort ();
121     return;
122   case MachineOperand::MO_ConstantPoolIndex: {
123     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
124     if (!isMemOp) O << "OFFSET ";
125     O << "[" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
126       << MO.getConstantPoolIndex();
127     if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
128       O << "-\"L" << getFunctionNumber() << "$pb\"";
129     int Offset = MO.getOffset();
130     if (Offset > 0)
131       O << " + " << Offset;
132     else if (Offset < 0)
133       O << Offset;
134     O << "]";
135     return;
136   }
137   case MachineOperand::MO_GlobalAddress: {
138     bool isCallOp = Modifier && !strcmp(Modifier, "call");
139     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
140     if (!isMemOp && !isCallOp) O << "OFFSET ";
141     if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
142       GlobalValue *GV = MO.getGlobal();
143       std::string Name = Mang->getValueName(GV);
144       if (!isMemOp && !isCallOp) O << '$';
145       // Link-once, External, or Weakly-linked global variables need
146       // non-lazily-resolved stubs
147       if (GV->isExternal() || GV->hasWeakLinkage() ||
148           GV->hasLinkOnceLinkage()) {
149         // Dynamically-resolved functions need a stub for the function.
150         if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
151           FnStubs.insert(Name);
152           O << "L" << Name << "$stub";
153         } else {
154           GVStubs.insert(Name);
155           O << "L" << Name << "$non_lazy_ptr";
156         }
157       } else {
158         O << Mang->getValueName(GV);
159       }
160       if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
161         O << "-\"L" << getFunctionNumber() << "$pb\"";
162     } else
163       O << Mang->getValueName(MO.getGlobal());
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_ExternalSymbol: {
172     bool isCallOp = Modifier && !strcmp(Modifier, "call");
173     if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
174       std::string Name(GlobalPrefix);
175       Name += MO.getSymbolName();
176       FnStubs.insert(Name);
177       O << "L" << Name << "$stub";
178       return;
179     }
180     if (!isCallOp) O << "OFFSET ";
181     O << GlobalPrefix << MO.getSymbolName();
182     return;
183   }
184   default:
185     O << "<unknown operand type>"; return;
186   }
187 }
188
189 void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
190   assert(isMem(MI, Op) && "Invalid memory reference!");
191
192   const MachineOperand &BaseReg  = MI->getOperand(Op);
193   int ScaleVal                   = MI->getOperand(Op+1).getImmedValue();
194   const MachineOperand &IndexReg = MI->getOperand(Op+2);
195   const MachineOperand &DispSpec = MI->getOperand(Op+3);
196
197   if (BaseReg.isFrameIndex()) {
198     O << "[frame slot #" << BaseReg.getFrameIndex();
199     if (DispSpec.getImmedValue())
200       O << " + " << DispSpec.getImmedValue();
201     O << "]";
202     return;
203   }
204
205   O << "[";
206   bool NeedPlus = false;
207   if (BaseReg.getReg()) {
208     printOp(BaseReg, "mem");
209     NeedPlus = true;
210   }
211
212   if (IndexReg.getReg()) {
213     if (NeedPlus) O << " + ";
214     if (ScaleVal != 1)
215       O << ScaleVal << "*";
216     printOp(IndexReg);
217     NeedPlus = true;
218   }
219
220   if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
221     if (NeedPlus)
222       O << " + ";
223     printOp(DispSpec, "mem");
224   } else {
225     int DispVal = DispSpec.getImmedValue();
226     if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
227       if (NeedPlus)
228         if (DispVal > 0)
229           O << " + ";
230         else {
231           O << " - ";
232           DispVal = -DispVal;
233         }
234       O << DispVal;
235     }
236   }
237   O << "]";
238 }
239
240 void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
241   O << "\"L" << getFunctionNumber() << "$pb\"\n";
242   O << "\"L" << getFunctionNumber() << "$pb\":";
243 }
244
245 bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
246                                            const char Mode) {
247   const MRegisterInfo &RI = *TM.getRegisterInfo();
248   unsigned Reg = MO.getReg();
249   const char *Name = RI.get(Reg).Name;
250   switch (Mode) {
251   default: return true;  // Unknown mode.
252   case 'b': // Print QImode register
253     switch (Reg) {
254     default: return true;
255     case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
256       Name = "AL";
257       break;
258     case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
259       Name = "DL";
260       break;
261     case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
262       Name = "CL";
263       break;
264     case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
265       Name = "BL";
266       break;
267     case X86::ESI:
268       Name = "SIL";
269       break;
270     case X86::EDI:
271       Name = "DIL";
272       break;
273     case X86::EBP:
274       Name = "BPL";
275       break;
276     case X86::ESP:
277       Name = "SPL";
278       break;
279     }
280     break;
281   case 'h': // Print QImode high register
282     switch (Reg) {
283     default: return true;
284     case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
285       Name = "AL";
286       break;
287     case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
288       Name = "DL";
289       break;
290     case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
291       Name = "CL";
292       break;
293     case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
294       Name = "BL";
295       break;
296     }
297     break;
298   case 'w': // Print HImode register
299     switch (Reg) {
300     default: return true;
301     case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
302       Name = "AX";
303       break;
304     case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
305       Name = "DX";
306       break;
307     case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
308       Name = "CX";
309       break;
310     case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
311       Name = "BX";
312       break;
313     case X86::ESI:
314       Name = "SI";
315       break;
316     case X86::EDI:
317       Name = "DI";
318       break;
319     case X86::EBP:
320       Name = "BP";
321       break;
322     case X86::ESP:
323       Name = "SP";
324       break;
325     }
326     break;
327   case 'k': // Print SImode register
328     switch (Reg) {
329     default: return true;
330     case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
331       Name = "EAX";
332       break;
333     case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
334       Name = "EDX";
335       break;
336     case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
337       Name = "ECX";
338       break;
339     case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
340       Name = "EBX";
341       break;
342     case X86::ESI:
343       Name = "ESI";
344       break;
345     case X86::EDI:
346       Name = "EDI";
347       break;
348     case X86::EBP:
349       Name = "EBP";
350       break;
351     case X86::ESP:
352       Name = "ESP";
353       break;
354     }
355     break;
356   }
357
358   O << '%' << Name;
359   return false;
360 }
361
362 /// PrintAsmOperand - Print out an operand for an inline asm expression.
363 ///
364 bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
365                                          unsigned AsmVariant, 
366                                          const char *ExtraCode) {
367   // Does this asm operand have a single letter operand modifier?
368   if (ExtraCode && ExtraCode[0]) {
369     if (ExtraCode[1] != 0) return true; // Unknown modifier.
370     
371     switch (ExtraCode[0]) {
372     default: return true;  // Unknown modifier.
373     case 'b': // Print QImode register
374     case 'h': // Print QImode high register
375     case 'w': // Print HImode register
376     case 'k': // Print SImode register
377       return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
378     }
379   }
380   
381   printOperand(MI, OpNo);
382   return false;
383 }
384
385 bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
386                                                unsigned OpNo,
387                                                unsigned AsmVariant, 
388                                                const char *ExtraCode) {
389   if (ExtraCode && ExtraCode[0])
390     return true; // Unknown modifier.
391   printMemReference(MI, OpNo);
392   return false;
393 }
394
395 /// printMachineInstruction -- Print out a single X86 LLVM instruction
396 /// MI in Intel syntax to the current output stream.
397 ///
398 void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
399   ++EmittedInsts;
400
401   // Call the autogenerated instruction printer routines.
402   printInstruction(MI);
403 }
404
405 bool X86IntelAsmPrinter::doInitialization(Module &M) {
406   X86SharedAsmPrinter::doInitialization(M);
407   // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
408   //
409   // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
410   // instruction as a reference to the register named sp, and if you try to
411   // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
412   // before being looked up in the symbol table. This creates spurious
413   // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
414   // mode, and decorate all register names with percent signs.
415   O << "\t.intel_syntax\n";
416   return false;
417 }
418
419 // Include the auto-generated portion of the assembly writer.
420 #include "X86GenAsmWriter1.inc"