use symbols instead of strings, eliminating a bunch of getMangledName
[oota-llvm.git] / lib / Target / PowerPC / AsmPrinter / PPCAsmPrinter.cpp
1 //===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 PowerPC assembly language. This printer is
12 // the output mechanism used by `llc'.
13 //
14 // Documentation at http://developer.apple.com/documentation/DeveloperTools/
15 // Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
16 //
17 //===----------------------------------------------------------------------===//
18
19 #define DEBUG_TYPE "asmprinter"
20 #include "PPC.h"
21 #include "PPCPredicates.h"
22 #include "PPCTargetMachine.h"
23 #include "PPCSubtarget.h"
24 #include "llvm/Constants.h"
25 #include "llvm/DerivedTypes.h"
26 #include "llvm/Module.h"
27 #include "llvm/Assembly/Writer.h"
28 #include "llvm/CodeGen/AsmPrinter.h"
29 #include "llvm/CodeGen/DwarfWriter.h"
30 #include "llvm/CodeGen/MachineModuleInfo.h"
31 #include "llvm/CodeGen/MachineFunctionPass.h"
32 #include "llvm/CodeGen/MachineInstr.h"
33 #include "llvm/CodeGen/MachineInstrBuilder.h"
34 #include "llvm/MC/MCAsmInfo.h"
35 #include "llvm/MC/MCContext.h"
36 #include "llvm/MC/MCSectionMachO.h"
37 #include "llvm/MC/MCStreamer.h"
38 #include "llvm/MC/MCSymbol.h"
39 #include "llvm/Target/TargetLoweringObjectFile.h"
40 #include "llvm/Target/TargetRegisterInfo.h"
41 #include "llvm/Target/TargetInstrInfo.h"
42 #include "llvm/Target/TargetOptions.h"
43 #include "llvm/Target/TargetRegistry.h"
44 #include "llvm/Support/Mangler.h"
45 #include "llvm/Support/MathExtras.h"
46 #include "llvm/Support/CommandLine.h"
47 #include "llvm/Support/Debug.h"
48 #include "llvm/Support/ErrorHandling.h"
49 #include "llvm/Support/FormattedStream.h"
50 #include "llvm/ADT/Statistic.h"
51 #include "llvm/ADT/StringExtras.h"
52 #include "llvm/ADT/StringSet.h"
53 #include "llvm/ADT/SmallString.h"
54 using namespace llvm;
55
56 STATISTIC(EmittedInsts, "Number of machine instrs printed");
57
58 namespace {
59   class PPCAsmPrinter : public AsmPrinter {
60   protected:
61     struct FnStubInfo {
62       MCSymbol *Stub, *LazyPtr, *AnonSymbol;
63       
64       FnStubInfo() {
65         Stub = LazyPtr = AnonSymbol = 0;
66       }
67       
68       void Init(const GlobalValue *GV, AsmPrinter *Printer) {
69         // Already initialized.
70         if (Stub != 0) return;
71
72         // Get the names.
73         Stub = Printer->GetPrivateGlobalValueSymbolStub(GV, "$stub");
74         LazyPtr = Printer->GetPrivateGlobalValueSymbolStub(GV, "$lazy_ptr");
75         AnonSymbol = Printer->GetPrivateGlobalValueSymbolStub(GV, "$stub$tmp");
76       }
77
78       void Init(StringRef GVName, Mangler *Mang, MCContext &Ctx) {
79         assert(!GVName.empty() && "external symbol name shouldn't be empty");
80         if (Stub != 0) return; // Already initialized.
81         // Get the names for the external symbol name.
82         SmallString<128> TmpStr;
83         Mang->getNameWithPrefix(TmpStr, GVName, Mangler::Private);
84         TmpStr += "$stub";
85         Stub = Ctx.GetOrCreateSymbol(TmpStr.str());
86         TmpStr.erase(TmpStr.end()-5, TmpStr.end()); // Remove $stub
87
88         TmpStr += "$lazy_ptr";
89         LazyPtr = Ctx.GetOrCreateSymbol(TmpStr.str());
90         TmpStr.erase(TmpStr.end()-9, TmpStr.end()); // Remove $lazy_ptr
91         
92         TmpStr += "$stub$tmp";
93         AnonSymbol = Ctx.GetOrCreateSymbol(TmpStr.str());
94       }
95     };
96     
97     DenseMap<const MCSymbol*, FnStubInfo> FnStubs;
98     StringMap<const MCSymbol*> GVStubs, HiddenGVStubs, TOC;
99     const PPCSubtarget &Subtarget;
100     uint64_t LabelID;
101   public:
102     explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
103                            const MCAsmInfo *T, bool V)
104       : AsmPrinter(O, TM, T, V),
105         Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
106
107     virtual const char *getPassName() const {
108       return "PowerPC Assembly Printer";
109     }
110
111     PPCTargetMachine &getTM() {
112       return static_cast<PPCTargetMachine&>(TM);
113     }
114
115     unsigned enumRegToMachineReg(unsigned enumReg) {
116       switch (enumReg) {
117       default: llvm_unreachable("Unhandled register!");
118       case PPC::CR0:  return  0;
119       case PPC::CR1:  return  1;
120       case PPC::CR2:  return  2;
121       case PPC::CR3:  return  3;
122       case PPC::CR4:  return  4;
123       case PPC::CR5:  return  5;
124       case PPC::CR6:  return  6;
125       case PPC::CR7:  return  7;
126       }
127       llvm_unreachable(0);
128     }
129
130     /// printInstruction - This method is automatically generated by tablegen
131     /// from the instruction set description.  This method returns true if the
132     /// machine instruction was sufficiently described to print it, otherwise it
133     /// returns false.
134     void printInstruction(const MachineInstr *MI);
135     static const char *getRegisterName(unsigned RegNo);
136
137
138     void printMachineInstruction(const MachineInstr *MI);
139     void printOp(const MachineOperand &MO);
140
141     /// stripRegisterPrefix - This method strips the character prefix from a
142     /// register name so that only the number is left.  Used by for linux asm.
143     const char *stripRegisterPrefix(const char *RegName) {
144       switch (RegName[0]) {
145       case 'r':
146       case 'f':
147       case 'v': return RegName + 1;
148       case 'c': if (RegName[1] == 'r') return RegName + 2;
149       }
150
151       return RegName;
152     }
153
154     /// printRegister - Print register according to target requirements.
155     ///
156     void printRegister(const MachineOperand &MO, bool R0AsZero) {
157       unsigned RegNo = MO.getReg();
158       assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
159
160       // If we should use 0 for R0.
161       if (R0AsZero && RegNo == PPC::R0) {
162         O << "0";
163         return;
164       }
165
166       const char *RegName = getRegisterName(RegNo);
167       // Linux assembler (Others?) does not take register mnemonics.
168       // FIXME - What about special registers used in mfspr/mtspr?
169       if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
170       O << RegName;
171     }
172
173     void printOperand(const MachineInstr *MI, unsigned OpNo) {
174       const MachineOperand &MO = MI->getOperand(OpNo);
175       if (MO.isReg()) {
176         printRegister(MO, false);
177       } else if (MO.isImm()) {
178         O << MO.getImm();
179       } else {
180         printOp(MO);
181       }
182     }
183
184     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
185                          unsigned AsmVariant, const char *ExtraCode);
186     bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
187                                unsigned AsmVariant, const char *ExtraCode);
188
189
190     void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
191       char value = MI->getOperand(OpNo).getImm();
192       value = (value << (32-5)) >> (32-5);
193       O << (int)value;
194     }
195     void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
196       unsigned char value = MI->getOperand(OpNo).getImm();
197       assert(value <= 31 && "Invalid u5imm argument!");
198       O << (unsigned int)value;
199     }
200     void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
201       unsigned char value = MI->getOperand(OpNo).getImm();
202       assert(value <= 63 && "Invalid u6imm argument!");
203       O << (unsigned int)value;
204     }
205     void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
206       O << (short)MI->getOperand(OpNo).getImm();
207     }
208     void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
209       O << (unsigned short)MI->getOperand(OpNo).getImm();
210     }
211     void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
212       if (MI->getOperand(OpNo).isImm()) {
213         O << (short)(MI->getOperand(OpNo).getImm()*4);
214       } else {
215         O << "lo16(";
216         printOp(MI->getOperand(OpNo));
217         if (TM.getRelocationModel() == Reloc::PIC_)
218           O << "-\"L" << getFunctionNumber() << "$pb\")";
219         else
220           O << ')';
221       }
222     }
223     void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
224       // Branches can take an immediate operand.  This is used by the branch
225       // selection pass to print $+8, an eight byte displacement from the PC.
226       if (MI->getOperand(OpNo).isImm()) {
227         O << "$+" << MI->getOperand(OpNo).getImm()*4;
228       } else {
229         printOp(MI->getOperand(OpNo));
230       }
231     }
232     void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
233       const MachineOperand &MO = MI->getOperand(OpNo);
234       if (TM.getRelocationModel() != Reloc::Static) {
235         if (MO.getType() == MachineOperand::MO_GlobalAddress) {
236           GlobalValue *GV = MO.getGlobal();
237           if (GV->isDeclaration() || GV->isWeakForLinker()) {
238             // Dynamically-resolved functions need a stub for the function.
239             FnStubInfo &FnInfo = FnStubs[GetGlobalValueSymbol(GV)];
240             FnInfo.Init(GV, this);
241             FnInfo.Stub->print(O, MAI);
242             return;
243           }
244         }
245         if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
246           FnStubInfo &FnInfo =
247             FnStubs[GetExternalSymbolSymbol(MO.getSymbolName())];
248           FnInfo.Init(MO.getSymbolName(), Mang, OutContext);
249           FnInfo.Stub->print(O, MAI);
250           return;
251         }
252       }
253
254       printOp(MI->getOperand(OpNo));
255     }
256     void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
257      O << (int)MI->getOperand(OpNo).getImm()*4;
258     }
259     void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
260       O << "\"L" << getFunctionNumber() << "$pb\"\n";
261       O << "\"L" << getFunctionNumber() << "$pb\":";
262     }
263     void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
264       if (MI->getOperand(OpNo).isImm()) {
265         printS16ImmOperand(MI, OpNo);
266       } else {
267         if (Subtarget.isDarwin()) O << "ha16(";
268         printOp(MI->getOperand(OpNo));
269         if (TM.getRelocationModel() == Reloc::PIC_)
270           O << "-\"L" << getFunctionNumber() << "$pb\"";
271         if (Subtarget.isDarwin())
272           O << ')';
273         else
274           O << "@ha";
275       }
276     }
277     void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
278       if (MI->getOperand(OpNo).isImm()) {
279         printS16ImmOperand(MI, OpNo);
280       } else {
281         if (Subtarget.isDarwin()) O << "lo16(";
282         printOp(MI->getOperand(OpNo));
283         if (TM.getRelocationModel() == Reloc::PIC_)
284           O << "-\"L" << getFunctionNumber() << "$pb\"";
285         if (Subtarget.isDarwin())
286           O << ')';
287         else
288           O << "@l";
289       }
290     }
291     void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
292       unsigned CCReg = MI->getOperand(OpNo).getReg();
293       unsigned RegNo = enumRegToMachineReg(CCReg);
294       O << (0x80 >> RegNo);
295     }
296     // The new addressing mode printers.
297     void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
298       printSymbolLo(MI, OpNo);
299       O << '(';
300       if (MI->getOperand(OpNo+1).isReg() &&
301           MI->getOperand(OpNo+1).getReg() == PPC::R0)
302         O << "0";
303       else
304         printOperand(MI, OpNo+1);
305       O << ')';
306     }
307     void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
308       if (MI->getOperand(OpNo).isImm())
309         printS16X4ImmOperand(MI, OpNo);
310       else
311         printSymbolLo(MI, OpNo);
312       O << '(';
313       if (MI->getOperand(OpNo+1).isReg() &&
314           MI->getOperand(OpNo+1).getReg() == PPC::R0)
315         O << "0";
316       else
317         printOperand(MI, OpNo+1);
318       O << ')';
319     }
320
321     void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
322       // When used as the base register, r0 reads constant zero rather than
323       // the value contained in the register.  For this reason, the darwin
324       // assembler requires that we print r0 as 0 (no r) when used as the base.
325       const MachineOperand &MO = MI->getOperand(OpNo);
326       printRegister(MO, true);
327       O << ", ";
328       printOperand(MI, OpNo+1);
329     }
330
331     void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
332       const MachineOperand &MO = MI->getOperand(OpNo);
333
334       assert(MO.getType() == MachineOperand::MO_GlobalAddress);
335
336       GlobalValue *GV = MO.getGlobal();
337
338       std::string Name = Mang->getMangledName(GV);
339
340       // Map symbol -> label of TOC entry.
341       if (TOC.count(Name) == 0)
342         TOC[Name] = OutContext.
343           GetOrCreateSymbol(StringRef(MAI->getPrivateGlobalPrefix()) + "C" +
344                             Twine(LabelID++));
345
346       TOC[Name]->print(O, MAI);
347       O << "@toc";
348     }
349
350     void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
351                                const char *Modifier);
352
353     virtual bool runOnMachineFunction(MachineFunction &F) = 0;
354   };
355
356   /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
357   class PPCLinuxAsmPrinter : public PPCAsmPrinter {
358   public:
359     explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
360                                 const MCAsmInfo *T, bool V)
361       : PPCAsmPrinter(O, TM, T, V){}
362
363     virtual const char *getPassName() const {
364       return "Linux PPC Assembly Printer";
365     }
366
367     bool runOnMachineFunction(MachineFunction &F);
368     bool doFinalization(Module &M);
369
370     void getAnalysisUsage(AnalysisUsage &AU) const {
371       AU.setPreservesAll();
372       AU.addRequired<MachineModuleInfo>();
373       AU.addRequired<DwarfWriter>();
374       PPCAsmPrinter::getAnalysisUsage(AU);
375     }
376
377     void PrintGlobalVariable(const GlobalVariable *GVar);
378   };
379
380   /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
381   /// OS X
382   class PPCDarwinAsmPrinter : public PPCAsmPrinter {
383     formatted_raw_ostream &OS;
384   public:
385     explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
386                                  const MCAsmInfo *T, bool V)
387       : PPCAsmPrinter(O, TM, T, V), OS(O) {}
388
389     virtual const char *getPassName() const {
390       return "Darwin PPC Assembly Printer";
391     }
392
393     bool runOnMachineFunction(MachineFunction &F);
394     bool doFinalization(Module &M);
395     void EmitStartOfAsmFile(Module &M);
396
397     void getAnalysisUsage(AnalysisUsage &AU) const {
398       AU.setPreservesAll();
399       AU.addRequired<MachineModuleInfo>();
400       AU.addRequired<DwarfWriter>();
401       PPCAsmPrinter::getAnalysisUsage(AU);
402     }
403
404     void PrintGlobalVariable(const GlobalVariable *GVar);
405   };
406 } // end of anonymous namespace
407
408 // Include the auto-generated portion of the assembly writer
409 #include "PPCGenAsmWriter.inc"
410
411 void PPCAsmPrinter::printOp(const MachineOperand &MO) {
412   switch (MO.getType()) {
413   case MachineOperand::MO_Immediate:
414     llvm_unreachable("printOp() does not handle immediate values");
415
416   case MachineOperand::MO_MachineBasicBlock:
417     GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
418     return;
419   case MachineOperand::MO_JumpTableIndex:
420     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
421       << '_' << MO.getIndex();
422     // FIXME: PIC relocation model
423     return;
424   case MachineOperand::MO_ConstantPoolIndex:
425     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
426       << '_' << MO.getIndex();
427     return;
428   case MachineOperand::MO_BlockAddress:
429     GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
430     return;
431   case MachineOperand::MO_ExternalSymbol: {
432     // Computing the address of an external symbol, not calling it.
433     std::string Name(MAI->getGlobalPrefix());
434     Name += MO.getSymbolName();
435     
436     if (TM.getRelocationModel() != Reloc::Static) {
437       GVStubs[Name] = 
438         OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
439                                      MO.getSymbolName()+"$non_lazy_ptr");
440       Name += "$non_lazy_ptr";
441     }
442     O << Name;
443     return;
444   }
445   case MachineOperand::MO_GlobalAddress: {
446     // Computing the address of a global symbol, not calling it.
447     GlobalValue *GV = MO.getGlobal();
448     MCSymbol *SymToPrint;
449
450     // External or weakly linked global variables need non-lazily-resolved stubs
451     if (TM.getRelocationModel() != Reloc::Static &&
452         (GV->isDeclaration() || GV->isWeakForLinker())) {
453       if (!GV->hasHiddenVisibility()) {
454         SymToPrint = GetPrivateGlobalValueSymbolStub(GV, "$non_lazy_ptr");
455         GVStubs[Mang->getMangledName(GV)] = SymToPrint;
456       } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
457                  GV->hasAvailableExternallyLinkage()) {
458         SymToPrint = GetPrivateGlobalValueSymbolStub(GV, "$non_lazy_ptr");
459         HiddenGVStubs[Mang->getMangledName(GV)] = SymToPrint;
460         GetGlobalValueSymbol(GV)->print(O, MAI);
461       } else {
462         SymToPrint = GetGlobalValueSymbol(GV);
463       }
464     } else {
465       SymToPrint = GetGlobalValueSymbol(GV);
466     }
467     
468     SymToPrint->print(O, MAI);
469
470     printOffset(MO.getOffset());
471     return;
472   }
473
474   default:
475     O << "<unknown operand type: " << MO.getType() << ">";
476     return;
477   }
478 }
479
480 /// PrintAsmOperand - Print out an operand for an inline asm expression.
481 ///
482 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
483                                     unsigned AsmVariant,
484                                     const char *ExtraCode) {
485   // Does this asm operand have a single letter operand modifier?
486   if (ExtraCode && ExtraCode[0]) {
487     if (ExtraCode[1] != 0) return true; // Unknown modifier.
488
489     switch (ExtraCode[0]) {
490     default: return true;  // Unknown modifier.
491     case 'c': // Don't print "$" before a global var name or constant.
492       // PPC never has a prefix.
493       printOperand(MI, OpNo);
494       return false;
495     case 'L': // Write second word of DImode reference.
496       // Verify that this operand has two consecutive registers.
497       if (!MI->getOperand(OpNo).isReg() ||
498           OpNo+1 == MI->getNumOperands() ||
499           !MI->getOperand(OpNo+1).isReg())
500         return true;
501       ++OpNo;   // Return the high-part.
502       break;
503     case 'I':
504       // Write 'i' if an integer constant, otherwise nothing.  Used to print
505       // addi vs add, etc.
506       if (MI->getOperand(OpNo).isImm())
507         O << "i";
508       return false;
509     }
510   }
511
512   printOperand(MI, OpNo);
513   return false;
514 }
515
516 // At the moment, all inline asm memory operands are a single register.
517 // In any case, the output of this routine should always be just one
518 // assembler operand.
519
520 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
521                                           unsigned AsmVariant,
522                                           const char *ExtraCode) {
523   if (ExtraCode && ExtraCode[0])
524     return true; // Unknown modifier.
525   assert (MI->getOperand(OpNo).isReg());
526   O << "0(";
527   printOperand(MI, OpNo);
528   O << ")";
529   return false;
530 }
531
532 void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
533                                           const char *Modifier) {
534   assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
535   unsigned Code = MI->getOperand(OpNo).getImm();
536   if (!strcmp(Modifier, "cc")) {
537     switch ((PPC::Predicate)Code) {
538     case PPC::PRED_ALWAYS: return; // Don't print anything for always.
539     case PPC::PRED_LT: O << "lt"; return;
540     case PPC::PRED_LE: O << "le"; return;
541     case PPC::PRED_EQ: O << "eq"; return;
542     case PPC::PRED_GE: O << "ge"; return;
543     case PPC::PRED_GT: O << "gt"; return;
544     case PPC::PRED_NE: O << "ne"; return;
545     case PPC::PRED_UN: O << "un"; return;
546     case PPC::PRED_NU: O << "nu"; return;
547     }
548
549   } else {
550     assert(!strcmp(Modifier, "reg") &&
551            "Need to specify 'cc' or 'reg' as predicate op modifier!");
552     // Don't print the register for 'always'.
553     if (Code == PPC::PRED_ALWAYS) return;
554     printOperand(MI, OpNo+1);
555   }
556 }
557
558
559 /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
560 /// the current output stream.
561 ///
562 void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
563   ++EmittedInsts;
564   
565   processDebugLoc(MI, true);
566
567   // Check for slwi/srwi mnemonics.
568   bool useSubstituteMnemonic = false;
569   if (MI->getOpcode() == PPC::RLWINM) {
570     unsigned char SH = MI->getOperand(2).getImm();
571     unsigned char MB = MI->getOperand(3).getImm();
572     unsigned char ME = MI->getOperand(4).getImm();
573     if (SH <= 31 && MB == 0 && ME == (31-SH)) {
574       O << "\tslwi "; useSubstituteMnemonic = true;
575     }
576     if (SH <= 31 && MB == (32-SH) && ME == 31) {
577       O << "\tsrwi "; useSubstituteMnemonic = true;
578       SH = 32-SH;
579     }
580     if (useSubstituteMnemonic) {
581       printOperand(MI, 0);
582       O << ", ";
583       printOperand(MI, 1);
584       O << ", " << (unsigned int)SH;
585     }
586   } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
587     if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
588       useSubstituteMnemonic = true;
589       O << "\tmr ";
590       printOperand(MI, 0);
591       O << ", ";
592       printOperand(MI, 1);
593     }
594   } else if (MI->getOpcode() == PPC::RLDICR) {
595     unsigned char SH = MI->getOperand(2).getImm();
596     unsigned char ME = MI->getOperand(3).getImm();
597     // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
598     if (63-SH == ME) {
599       useSubstituteMnemonic = true;
600       O << "\tsldi ";
601       printOperand(MI, 0);
602       O << ", ";
603       printOperand(MI, 1);
604       O << ", " << (unsigned int)SH;
605     }
606   }
607
608   if (!useSubstituteMnemonic)
609     printInstruction(MI);
610
611   if (VerboseAsm)
612     EmitComments(*MI);
613   O << '\n';
614
615   processDebugLoc(MI, false);
616 }
617
618 /// runOnMachineFunction - This uses the printMachineInstruction()
619 /// method to print assembly for each instruction.
620 ///
621 bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
622   this->MF = &MF;
623
624   SetupMachineFunction(MF);
625   O << "\n\n";
626
627   // Print out constants referenced by the function
628   EmitConstantPool(MF.getConstantPool());
629
630   // Print out labels for the function.
631   const Function *F = MF.getFunction();
632   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
633
634   switch (F->getLinkage()) {
635   default: llvm_unreachable("Unknown linkage type!");
636   case Function::PrivateLinkage:
637   case Function::InternalLinkage:  // Symbols default to internal.
638     break;
639   case Function::ExternalLinkage:
640     O << "\t.global\t";
641     CurrentFnSym->print(O, MAI);
642     O << '\n' << "\t.type\t";
643     CurrentFnSym->print(O, MAI);
644     O << ", @function\n";
645     break;
646   case Function::LinkerPrivateLinkage:
647   case Function::WeakAnyLinkage:
648   case Function::WeakODRLinkage:
649   case Function::LinkOnceAnyLinkage:
650   case Function::LinkOnceODRLinkage:
651     O << "\t.global\t";
652     CurrentFnSym->print(O, MAI);
653     O << '\n';
654     O << "\t.weak\t";
655     CurrentFnSym->print(O, MAI);
656     O << '\n';
657     break;
658   }
659
660   printVisibility(CurrentFnSym, F->getVisibility());
661
662   EmitAlignment(MF.getAlignment(), F);
663
664   if (Subtarget.isPPC64()) {
665     // Emit an official procedure descriptor.
666     // FIXME 64-bit SVR4: Use MCSection here!
667     O << "\t.section\t\".opd\",\"aw\"\n";
668     O << "\t.align 3\n";
669     CurrentFnSym->print(O, MAI);
670     O  << ":\n";
671     O << "\t.quad .L.";
672     CurrentFnSym->print(O, MAI);
673     O << ",.TOC.@tocbase\n";
674     O << "\t.previous\n";
675     O << ".L.";
676     CurrentFnSym->print(O, MAI);
677     O << ":\n";
678   } else {
679     CurrentFnSym->print(O, MAI);
680     O  << ":\n";
681   }
682
683   // Emit pre-function debug information.
684   DW->BeginFunction(&MF);
685
686   // Print out code for the function.
687   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
688        I != E; ++I) {
689     // Print a label for the basic block.
690     if (I != MF.begin()) {
691       EmitBasicBlockStart(I);
692     }
693     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
694          II != E; ++II) {
695       // Print the assembly for the instruction.
696       printMachineInstruction(II);
697     }
698   }
699
700   O << "\t.size\t";
701   CurrentFnSym->print(O, MAI);
702   O << ",.-";
703   CurrentFnSym->print(O, MAI);
704   O << '\n';
705
706   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
707
708   // Emit post-function debug information.
709   DW->EndFunction(&MF);
710
711   // Print out jump tables referenced by the function.
712   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
713
714   // We didn't modify anything.
715   return false;
716 }
717
718 void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
719   const TargetData *TD = TM.getTargetData();
720
721   if (!GVar->hasInitializer())
722     return;   // External global require no code
723
724   // Check to see if this is a special global used by LLVM, if so, emit it.
725   if (EmitSpecialLLVMGlobal(GVar))
726     return;
727
728   MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
729
730   printVisibility(GVarSym, GVar->getVisibility());
731
732   Constant *C = GVar->getInitializer();
733   const Type *Type = C->getType();
734   unsigned Size = TD->getTypeAllocSize(Type);
735   unsigned Align = TD->getPreferredAlignmentLog(GVar);
736
737   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
738                                                                   TM));
739
740   if (C->isNullValue() && /* FIXME: Verify correct */
741       !GVar->hasSection() &&
742       (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
743        GVar->isWeakForLinker())) {
744       if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
745
746       if (GVar->hasExternalLinkage()) {
747         O << "\t.global ";
748         GVarSym->print(O, MAI);
749         O << '\n';
750         O << "\t.type ";
751         GVarSym->print(O, MAI);
752         O << ", @object\n";
753         GVarSym->print(O, MAI);
754         O  << ":\n";
755         O << "\t.zero " << Size << '\n';
756       } else if (GVar->hasLocalLinkage()) {
757         O << MAI->getLCOMMDirective();
758         GVarSym->print(O, MAI);
759         O << ',' << Size;
760       } else {
761         O << ".comm ";
762         GVarSym->print(O, MAI);
763         O  << ',' << Size;
764       }
765       if (VerboseAsm) {
766         O << "\t\t" << MAI->getCommentString() << " '";
767         WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
768         O << "'";
769       }
770       O << '\n';
771       return;
772   }
773
774   switch (GVar->getLinkage()) {
775    case GlobalValue::LinkOnceAnyLinkage:
776    case GlobalValue::LinkOnceODRLinkage:
777    case GlobalValue::WeakAnyLinkage:
778    case GlobalValue::WeakODRLinkage:
779    case GlobalValue::CommonLinkage:
780    case GlobalValue::LinkerPrivateLinkage:
781     O << "\t.global ";
782     GVarSym->print(O, MAI);
783     O << "\n\t.type ";
784     GVarSym->print(O, MAI);
785     O << ", @object\n\t.weak ";
786     GVarSym->print(O, MAI);
787     O << '\n';
788     break;
789    case GlobalValue::AppendingLinkage:
790     // FIXME: appending linkage variables should go into a section of
791     // their name or something.  For now, just emit them as external.
792    case GlobalValue::ExternalLinkage:
793     // If external or appending, declare as a global symbol
794     O << "\t.global ";
795     GVarSym->print(O, MAI);
796     O << "\n\t.type ";
797     GVarSym->print(O, MAI);
798     O << ", @object\n";
799     // FALL THROUGH
800    case GlobalValue::InternalLinkage:
801    case GlobalValue::PrivateLinkage:
802     break;
803    default:
804     llvm_unreachable("Unknown linkage type!");
805   }
806
807   EmitAlignment(Align, GVar);
808   GVarSym->print(O, MAI);
809   O << ":";
810   if (VerboseAsm) {
811     O << "\t\t\t\t" << MAI->getCommentString() << " '";
812     WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
813     O << "'";
814   }
815   O << '\n';
816
817   EmitGlobalConstant(C);
818   O << '\n';
819 }
820
821 bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
822   const TargetData *TD = TM.getTargetData();
823
824   bool isPPC64 = TD->getPointerSizeInBits() == 64;
825
826   if (isPPC64 && !TOC.empty()) {
827     // FIXME 64-bit SVR4: Use MCSection here?
828     O << "\t.section\t\".toc\",\"aw\"\n";
829
830     // FIXME: This is nondeterminstic!
831     for (StringMap<const MCSymbol*>::iterator I = TOC.begin(), E = TOC.end();
832          I != E; ++I) {
833       I->second->print(O, MAI);
834       O << ":\n";
835       O << "\t.tc " << I->getKeyData() << "[TC]," << I->getKeyData() << '\n';
836     }
837   }
838
839   return AsmPrinter::doFinalization(M);
840 }
841
842 /// runOnMachineFunction - This uses the printMachineInstruction()
843 /// method to print assembly for each instruction.
844 ///
845 bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
846   this->MF = &MF;
847
848   SetupMachineFunction(MF);
849   O << "\n\n";
850
851   // Print out constants referenced by the function
852   EmitConstantPool(MF.getConstantPool());
853
854   // Print out labels for the function.
855   const Function *F = MF.getFunction();
856   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
857
858   switch (F->getLinkage()) {
859   default: llvm_unreachable("Unknown linkage type!");
860   case Function::PrivateLinkage:
861   case Function::InternalLinkage:  // Symbols default to internal.
862     break;
863   case Function::ExternalLinkage:
864     O << "\t.globl\t";
865     CurrentFnSym->print(O, MAI);
866     O << '\n';
867     break;
868   case Function::WeakAnyLinkage:
869   case Function::WeakODRLinkage:
870   case Function::LinkOnceAnyLinkage:
871   case Function::LinkOnceODRLinkage:
872   case Function::LinkerPrivateLinkage:
873     O << "\t.globl\t";
874     CurrentFnSym->print(O, MAI);
875     O << '\n';
876     O << "\t.weak_definition\t";
877     CurrentFnSym->print(O, MAI);
878     O << '\n';
879     break;
880   }
881
882   printVisibility(CurrentFnSym, F->getVisibility());
883
884   EmitAlignment(MF.getAlignment(), F);
885   CurrentFnSym->print(O, MAI);
886   O << ":\n";
887
888   // Emit pre-function debug information.
889   DW->BeginFunction(&MF);
890
891   // If the function is empty, then we need to emit *something*. Otherwise, the
892   // function's label might be associated with something that it wasn't meant to
893   // be associated with. We emit a noop in this situation.
894   MachineFunction::iterator I = MF.begin();
895
896   if (++I == MF.end() && MF.front().empty())
897     O << "\tnop\n";
898
899   // Print out code for the function.
900   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
901        I != E; ++I) {
902     // Print a label for the basic block.
903     if (I != MF.begin()) {
904       EmitBasicBlockStart(I);
905     }
906     for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
907          II != IE; ++II) {
908       // Print the assembly for the instruction.
909       printMachineInstruction(II);
910     }
911   }
912
913   // Emit post-function debug information.
914   DW->EndFunction(&MF);
915
916   // Print out jump tables referenced by the function.
917   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
918
919   // We didn't modify anything.
920   return false;
921 }
922
923
924 void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
925   static const char *const CPUDirectives[] = {
926     "",
927     "ppc",
928     "ppc601",
929     "ppc602",
930     "ppc603",
931     "ppc7400",
932     "ppc750",
933     "ppc970",
934     "ppc64"
935   };
936
937   unsigned Directive = Subtarget.getDarwinDirective();
938   if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
939     Directive = PPC::DIR_970;
940   if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
941     Directive = PPC::DIR_7400;
942   if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
943     Directive = PPC::DIR_64;
944   assert(Directive <= PPC::DIR_64 && "Directive out of range.");
945   O << "\t.machine " << CPUDirectives[Directive] << '\n';
946
947   // Prime text sections so they are adjacent.  This reduces the likelihood a
948   // large data or debug section causes a branch to exceed 16M limit.
949   TargetLoweringObjectFileMachO &TLOFMacho = 
950     static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
951   OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
952   if (TM.getRelocationModel() == Reloc::PIC_) {
953     OutStreamer.SwitchSection(
954             TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
955                                       MCSectionMachO::S_SYMBOL_STUBS |
956                                       MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
957                                       32, SectionKind::getText()));
958   } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
959     OutStreamer.SwitchSection(
960             TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
961                                       MCSectionMachO::S_SYMBOL_STUBS |
962                                       MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
963                                       16, SectionKind::getText()));
964   }
965   OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
966 }
967
968 void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
969   const TargetData *TD = TM.getTargetData();
970
971   if (!GVar->hasInitializer())
972     return;   // External global require no code
973
974   // Check to see if this is a special global used by LLVM, if so, emit it.
975   if (EmitSpecialLLVMGlobal(GVar)) {
976     if (TM.getRelocationModel() == Reloc::Static) {
977       if (GVar->getName() == "llvm.global_ctors")
978         O << ".reference .constructors_used\n";
979       else if (GVar->getName() == "llvm.global_dtors")
980         O << ".reference .destructors_used\n";
981     }
982     return;
983   }
984
985   MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
986   printVisibility(GVarSym, GVar->getVisibility());
987
988   Constant *C = GVar->getInitializer();
989   const Type *Type = C->getType();
990   unsigned Size = TD->getTypeAllocSize(Type);
991   unsigned Align = TD->getPreferredAlignmentLog(GVar);
992
993   const MCSection *TheSection =
994     getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
995   OutStreamer.SwitchSection(TheSection);
996
997   /// FIXME: Drive this off the section!
998   if (C->isNullValue() && /* FIXME: Verify correct */
999       !GVar->hasSection() &&
1000       (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
1001        GVar->isWeakForLinker()) &&
1002       // Don't put things that should go in the cstring section into "comm".
1003       !TheSection->getKind().isMergeableCString()) {
1004     if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
1005
1006     if (GVar->hasExternalLinkage()) {
1007       O << "\t.globl ";
1008       GVarSym->print(O, MAI);
1009       O << '\n';
1010       O << "\t.zerofill __DATA, __common, ";
1011       GVarSym->print(O, MAI);
1012       O << ", " << Size << ", " << Align;
1013     } else if (GVar->hasLocalLinkage()) {
1014       O << MAI->getLCOMMDirective();
1015       GVarSym->print(O, MAI);
1016       O << ',' << Size << ',' << Align;
1017     } else if (!GVar->hasCommonLinkage()) {
1018       O << "\t.globl ";
1019       GVarSym->print(O, MAI);
1020       O << '\n' << MAI->getWeakDefDirective();
1021       GVarSym->print(O, MAI);
1022       O << '\n';
1023       EmitAlignment(Align, GVar);
1024       GVarSym->print(O, MAI);
1025       O << ":";
1026       if (VerboseAsm) {
1027         O << "\t\t\t\t" << MAI->getCommentString() << " ";
1028         WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1029       }
1030       O << '\n';
1031       EmitGlobalConstant(C);
1032       return;
1033     } else {
1034       O << ".comm ";
1035       GVarSym->print(O, MAI);
1036       O << ',' << Size;
1037       // Darwin 9 and above support aligned common data.
1038       if (Subtarget.isDarwin9())
1039         O << ',' << Align;
1040     }
1041     if (VerboseAsm) {
1042       O << "\t\t" << MAI->getCommentString() << " '";
1043       WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1044       O << "'";
1045     }
1046     O << '\n';
1047     return;
1048   }
1049
1050   switch (GVar->getLinkage()) {
1051   case GlobalValue::LinkOnceAnyLinkage:
1052   case GlobalValue::LinkOnceODRLinkage:
1053   case GlobalValue::WeakAnyLinkage:
1054   case GlobalValue::WeakODRLinkage:
1055   case GlobalValue::CommonLinkage:
1056   case GlobalValue::LinkerPrivateLinkage:
1057     O << "\t.globl ";
1058     GVarSym->print(O, MAI);
1059     O << "\n\t.weak_definition ";
1060     GVarSym->print(O, MAI);
1061     O << '\n';
1062     break;
1063   case GlobalValue::AppendingLinkage:
1064     // FIXME: appending linkage variables should go into a section of
1065     // their name or something.  For now, just emit them as external.
1066   case GlobalValue::ExternalLinkage:
1067     // If external or appending, declare as a global symbol
1068     O << "\t.globl ";
1069     GVarSym->print(O, MAI);
1070     O << '\n';
1071     // FALL THROUGH
1072   case GlobalValue::InternalLinkage:
1073   case GlobalValue::PrivateLinkage:
1074     break;
1075   default:
1076     llvm_unreachable("Unknown linkage type!");
1077   }
1078
1079   EmitAlignment(Align, GVar);
1080   GVarSym->print(O, MAI);
1081   O << ":";
1082   if (VerboseAsm) {
1083     O << "\t\t\t\t" << MAI->getCommentString() << " '";
1084     WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1085     O << "'";
1086   }
1087   O << '\n';
1088
1089   EmitGlobalConstant(C);
1090   O << '\n';
1091 }
1092
1093 bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
1094   const TargetData *TD = TM.getTargetData();
1095
1096   bool isPPC64 = TD->getPointerSizeInBits() == 64;
1097
1098   // Darwin/PPC always uses mach-o.
1099   TargetLoweringObjectFileMachO &TLOFMacho = 
1100     static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1101
1102   
1103   const MCSection *LSPSection = 0;
1104   if (!FnStubs.empty()) // .lazy_symbol_pointer
1105     LSPSection = TLOFMacho.getLazySymbolPointerSection();
1106     
1107   
1108   // Output stubs for dynamically-linked functions
1109   if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
1110     const MCSection *StubSection = 
1111       TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
1112                                 MCSectionMachO::S_SYMBOL_STUBS |
1113                                 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1114                                 32, SectionKind::getText());
1115     // FIXME: This is emitting in nondeterminstic order!
1116     for (DenseMap<const MCSymbol*, FnStubInfo>::iterator I = 
1117            FnStubs.begin(), E = FnStubs.end(); I != E; ++I) {
1118       OutStreamer.SwitchSection(StubSection);
1119       EmitAlignment(4);
1120       const FnStubInfo &Info = I->second;
1121       Info.Stub->print(O, MAI);
1122       O << ":\n";
1123       O << "\t.indirect_symbol ";
1124       I->first->print(O, MAI);
1125       O << '\n';
1126       O << "\tmflr r0\n";
1127       O << "\tbcl 20,31,";
1128       Info.AnonSymbol->print(O, MAI);
1129       O << '\n';
1130       Info.AnonSymbol->print(O, MAI);
1131       O << ":\n";
1132       O << "\tmflr r11\n";
1133       O << "\taddis r11,r11,ha16(";
1134       Info.LazyPtr->print(O, MAI);
1135       O << '-';
1136       Info.AnonSymbol->print(O, MAI);
1137       O << ")\n";
1138       O << "\tmtlr r0\n";
1139       O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
1140       Info.LazyPtr->print(O, MAI);
1141       O << '-';
1142       Info.AnonSymbol->print(O, MAI);
1143       O << ")(r11)\n";
1144       O << "\tmtctr r12\n";
1145       O << "\tbctr\n";
1146       
1147       OutStreamer.SwitchSection(LSPSection);
1148       Info.LazyPtr->print(O, MAI);
1149       O << ":\n";
1150       O << "\t.indirect_symbol ";
1151       I->first->print(O, MAI);
1152       O << '\n';
1153       O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
1154     }
1155   } else if (!FnStubs.empty()) {
1156     const MCSection *StubSection =
1157       TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1158                                 MCSectionMachO::S_SYMBOL_STUBS |
1159                                 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1160                                 16, SectionKind::getText());
1161     
1162     // FIXME: This is emitting in nondeterminstic order!
1163     for (DenseMap<const MCSymbol*, FnStubInfo>::iterator I = FnStubs.begin(),
1164          E = FnStubs.end(); I != E; ++I) {
1165       OutStreamer.SwitchSection(StubSection);
1166       EmitAlignment(4);
1167       const FnStubInfo &Info = I->second;
1168       Info.Stub->print(O, MAI);
1169       O << ":\n";
1170       O << "\t.indirect_symbol ";
1171       I->first->print(O, MAI);
1172       O << '\n';
1173       O << "\tlis r11,ha16(";
1174       Info.LazyPtr->print(O, MAI);
1175       O << ")\n";
1176       O << (isPPC64 ? "\tldu" :  "\tlwzu") << " r12,lo16(";
1177       Info.LazyPtr->print(O, MAI);
1178       O << ")(r11)\n";
1179       O << "\tmtctr r12\n";
1180       O << "\tbctr\n";
1181       OutStreamer.SwitchSection(LSPSection);
1182       Info.LazyPtr->print(O, MAI);
1183       O << ":\n";
1184       O << "\t.indirect_symbol ";
1185       I->first->print(O, MAI);
1186       O << '\n';
1187       O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
1188     }
1189   }
1190
1191   O << '\n';
1192
1193   if (MAI->doesSupportExceptionHandling() && MMI) {
1194     // Add the (possibly multiple) personalities to the set of global values.
1195     // Only referenced functions get into the Personalities list.
1196     const std::vector<Function *> &Personalities = MMI->getPersonalities();
1197     for (std::vector<Function *>::const_iterator I = Personalities.begin(),
1198          E = Personalities.end(); I != E; ++I) {
1199       if (*I)
1200         GVStubs[Mang->getMangledName(*I)] =
1201           GetPrivateGlobalValueSymbolStub(*I, "$non_lazy_ptr");
1202     }
1203   }
1204
1205   // Output macho stubs for external and common global variables.
1206   if (!GVStubs.empty()) {
1207     // Switch with ".non_lazy_symbol_pointer" directive.
1208     OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
1209     EmitAlignment(isPPC64 ? 3 : 2);
1210     
1211     // FIXME: This is nondeterminstic.
1212     for (StringMap<const MCSymbol *>::iterator I = GVStubs.begin(),
1213          E = GVStubs.end(); I != E; ++I) {
1214       I->second->print(O, MAI);
1215       O << ":\n";
1216       O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1217       O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
1218     }
1219   }
1220
1221   if (!HiddenGVStubs.empty()) {
1222     OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
1223     EmitAlignment(isPPC64 ? 3 : 2);
1224     // FIXME: This is nondeterminstic.
1225     for (StringMap<const MCSymbol *>::iterator I = HiddenGVStubs.begin(),
1226          E = HiddenGVStubs.end(); I != E; ++I) {
1227       I->second->print(O, MAI);
1228       O << ":\n";
1229       O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
1230     }
1231   }
1232
1233   // Funny Darwin hack: This flag tells the linker that no global symbols
1234   // contain code that falls through to other global symbols (e.g. the obvious
1235   // implementation of multiple entry points).  If this doesn't occur, the
1236   // linker can safely perform dead code stripping.  Since LLVM never generates
1237   // code that does this, it is always safe to set.
1238   OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
1239
1240   return AsmPrinter::doFinalization(M);
1241 }
1242
1243
1244
1245 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1246 /// for a MachineFunction to the given output stream, in a format that the
1247 /// Darwin assembler can deal with.
1248 ///
1249 static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
1250                                            TargetMachine &tm,
1251                                            const MCAsmInfo *tai,
1252                                            bool verbose) {
1253   const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1254
1255   if (Subtarget->isDarwin())
1256     return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
1257   return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
1258 }
1259
1260 // Force static initialization.
1261 extern "C" void LLVMInitializePowerPCAsmPrinter() { 
1262   TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
1263   TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1264 }