28dd193deb2b9e648025907d522e3eb799486283
[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/Mangler.h"
40 #include "llvm/Target/TargetLoweringObjectFile.h"
41 #include "llvm/Target/TargetRegisterInfo.h"
42 #include "llvm/Target/TargetInstrInfo.h"
43 #include "llvm/Target/TargetOptions.h"
44 #include "llvm/Target/TargetRegistry.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->GetSymbolWithGlobalValueBase(GV, "$stub");
74         LazyPtr = Printer->GetSymbolWithGlobalValueBase(GV, "$lazy_ptr");
75         AnonSymbol = Printer->GetSymbolWithGlobalValueBase(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     DenseMap<const MCSymbol*, 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             O << *FnInfo.Stub;
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           O << *FnInfo.Stub;
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       const MCSymbol *Sym = GetGlobalValueSymbol(MO.getGlobal());
337
338       // Map symbol -> label of TOC entry.
339       const MCSymbol *&TOCEntry = TOC[Sym];
340       if (TOCEntry == 0)
341         TOCEntry = OutContext.
342           GetOrCreateSymbol(StringRef(MAI->getPrivateGlobalPrefix()) + "C" +
343                             Twine(LabelID++));
344
345       O << *TOCEntry << "@toc";
346     }
347
348     void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
349                                const char *Modifier);
350
351     virtual bool runOnMachineFunction(MachineFunction &F) = 0;
352   };
353
354   /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
355   class PPCLinuxAsmPrinter : public PPCAsmPrinter {
356   public:
357     explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
358                                 const MCAsmInfo *T, bool V)
359       : PPCAsmPrinter(O, TM, T, V){}
360
361     virtual const char *getPassName() const {
362       return "Linux PPC Assembly Printer";
363     }
364
365     bool runOnMachineFunction(MachineFunction &F);
366     bool doFinalization(Module &M);
367
368     void getAnalysisUsage(AnalysisUsage &AU) const {
369       AU.setPreservesAll();
370       AU.addRequired<MachineModuleInfo>();
371       AU.addRequired<DwarfWriter>();
372       PPCAsmPrinter::getAnalysisUsage(AU);
373     }
374
375     void PrintGlobalVariable(const GlobalVariable *GVar);
376   };
377
378   /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
379   /// OS X
380   class PPCDarwinAsmPrinter : public PPCAsmPrinter {
381     formatted_raw_ostream &OS;
382   public:
383     explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
384                                  const MCAsmInfo *T, bool V)
385       : PPCAsmPrinter(O, TM, T, V), OS(O) {}
386
387     virtual const char *getPassName() const {
388       return "Darwin PPC Assembly Printer";
389     }
390
391     bool runOnMachineFunction(MachineFunction &F);
392     bool doFinalization(Module &M);
393     void EmitStartOfAsmFile(Module &M);
394
395     void getAnalysisUsage(AnalysisUsage &AU) const {
396       AU.setPreservesAll();
397       AU.addRequired<MachineModuleInfo>();
398       AU.addRequired<DwarfWriter>();
399       PPCAsmPrinter::getAnalysisUsage(AU);
400     }
401
402     void PrintGlobalVariable(const GlobalVariable *GVar);
403   };
404 } // end of anonymous namespace
405
406 // Include the auto-generated portion of the assembly writer
407 #include "PPCGenAsmWriter.inc"
408
409 void PPCAsmPrinter::printOp(const MachineOperand &MO) {
410   switch (MO.getType()) {
411   case MachineOperand::MO_Immediate:
412     llvm_unreachable("printOp() does not handle immediate values");
413
414   case MachineOperand::MO_MachineBasicBlock:
415     O << *GetMBBSymbol(MO.getMBB()->getNumber());
416     return;
417   case MachineOperand::MO_JumpTableIndex:
418     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
419       << '_' << MO.getIndex();
420     // FIXME: PIC relocation model
421     return;
422   case MachineOperand::MO_ConstantPoolIndex:
423     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
424       << '_' << MO.getIndex();
425     return;
426   case MachineOperand::MO_BlockAddress:
427     O << *GetBlockAddressSymbol(MO.getBlockAddress());
428     return;
429   case MachineOperand::MO_ExternalSymbol: {
430     // Computing the address of an external symbol, not calling it.
431     const MCSymbol *SymName = GetExternalSymbolSymbol(MO.getSymbolName());
432     if (TM.getRelocationModel() == Reloc::Static) {
433       O << *SymName;
434       return;
435     }
436     const MCSymbol *NLPSym = 
437       OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
438                                    MO.getSymbolName()+"$non_lazy_ptr");
439     GVStubs[SymName] = NLPSym;
440     O << *NLPSym;
441     return;
442   }
443   case MachineOperand::MO_GlobalAddress: {
444     // Computing the address of a global symbol, not calling it.
445     GlobalValue *GV = MO.getGlobal();
446     MCSymbol *SymToPrint;
447
448     // External or weakly linked global variables need non-lazily-resolved stubs
449     if (TM.getRelocationModel() != Reloc::Static &&
450         (GV->isDeclaration() || GV->isWeakForLinker())) {
451       if (!GV->hasHiddenVisibility()) {
452         SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
453         GVStubs[GetGlobalValueSymbol(GV)] = SymToPrint;
454       } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
455                  GV->hasAvailableExternallyLinkage()) {
456         SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
457         HiddenGVStubs[GetGlobalValueSymbol(GV)] = SymToPrint;
458       } else {
459         SymToPrint = GetGlobalValueSymbol(GV);
460       }
461     } else {
462       SymToPrint = GetGlobalValueSymbol(GV);
463     }
464     
465     O << *SymToPrint;
466
467     printOffset(MO.getOffset());
468     return;
469   }
470
471   default:
472     O << "<unknown operand type: " << MO.getType() << ">";
473     return;
474   }
475 }
476
477 /// PrintAsmOperand - Print out an operand for an inline asm expression.
478 ///
479 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
480                                     unsigned AsmVariant,
481                                     const char *ExtraCode) {
482   // Does this asm operand have a single letter operand modifier?
483   if (ExtraCode && ExtraCode[0]) {
484     if (ExtraCode[1] != 0) return true; // Unknown modifier.
485
486     switch (ExtraCode[0]) {
487     default: return true;  // Unknown modifier.
488     case 'c': // Don't print "$" before a global var name or constant.
489       // PPC never has a prefix.
490       printOperand(MI, OpNo);
491       return false;
492     case 'L': // Write second word of DImode reference.
493       // Verify that this operand has two consecutive registers.
494       if (!MI->getOperand(OpNo).isReg() ||
495           OpNo+1 == MI->getNumOperands() ||
496           !MI->getOperand(OpNo+1).isReg())
497         return true;
498       ++OpNo;   // Return the high-part.
499       break;
500     case 'I':
501       // Write 'i' if an integer constant, otherwise nothing.  Used to print
502       // addi vs add, etc.
503       if (MI->getOperand(OpNo).isImm())
504         O << "i";
505       return false;
506     }
507   }
508
509   printOperand(MI, OpNo);
510   return false;
511 }
512
513 // At the moment, all inline asm memory operands are a single register.
514 // In any case, the output of this routine should always be just one
515 // assembler operand.
516
517 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
518                                           unsigned AsmVariant,
519                                           const char *ExtraCode) {
520   if (ExtraCode && ExtraCode[0])
521     return true; // Unknown modifier.
522   assert (MI->getOperand(OpNo).isReg());
523   O << "0(";
524   printOperand(MI, OpNo);
525   O << ")";
526   return false;
527 }
528
529 void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
530                                           const char *Modifier) {
531   assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
532   unsigned Code = MI->getOperand(OpNo).getImm();
533   if (!strcmp(Modifier, "cc")) {
534     switch ((PPC::Predicate)Code) {
535     case PPC::PRED_ALWAYS: return; // Don't print anything for always.
536     case PPC::PRED_LT: O << "lt"; return;
537     case PPC::PRED_LE: O << "le"; return;
538     case PPC::PRED_EQ: O << "eq"; return;
539     case PPC::PRED_GE: O << "ge"; return;
540     case PPC::PRED_GT: O << "gt"; return;
541     case PPC::PRED_NE: O << "ne"; return;
542     case PPC::PRED_UN: O << "un"; return;
543     case PPC::PRED_NU: O << "nu"; return;
544     }
545
546   } else {
547     assert(!strcmp(Modifier, "reg") &&
548            "Need to specify 'cc' or 'reg' as predicate op modifier!");
549     // Don't print the register for 'always'.
550     if (Code == PPC::PRED_ALWAYS) return;
551     printOperand(MI, OpNo+1);
552   }
553 }
554
555
556 /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
557 /// the current output stream.
558 ///
559 void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
560   ++EmittedInsts;
561   
562   processDebugLoc(MI, true);
563
564   // Check for slwi/srwi mnemonics.
565   bool useSubstituteMnemonic = false;
566   if (MI->getOpcode() == PPC::RLWINM) {
567     unsigned char SH = MI->getOperand(2).getImm();
568     unsigned char MB = MI->getOperand(3).getImm();
569     unsigned char ME = MI->getOperand(4).getImm();
570     if (SH <= 31 && MB == 0 && ME == (31-SH)) {
571       O << "\tslwi "; useSubstituteMnemonic = true;
572     }
573     if (SH <= 31 && MB == (32-SH) && ME == 31) {
574       O << "\tsrwi "; useSubstituteMnemonic = true;
575       SH = 32-SH;
576     }
577     if (useSubstituteMnemonic) {
578       printOperand(MI, 0);
579       O << ", ";
580       printOperand(MI, 1);
581       O << ", " << (unsigned int)SH;
582     }
583   } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
584     if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
585       useSubstituteMnemonic = true;
586       O << "\tmr ";
587       printOperand(MI, 0);
588       O << ", ";
589       printOperand(MI, 1);
590     }
591   } else if (MI->getOpcode() == PPC::RLDICR) {
592     unsigned char SH = MI->getOperand(2).getImm();
593     unsigned char ME = MI->getOperand(3).getImm();
594     // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
595     if (63-SH == ME) {
596       useSubstituteMnemonic = true;
597       O << "\tsldi ";
598       printOperand(MI, 0);
599       O << ", ";
600       printOperand(MI, 1);
601       O << ", " << (unsigned int)SH;
602     }
603   }
604
605   if (!useSubstituteMnemonic)
606     printInstruction(MI);
607
608   if (VerboseAsm)
609     EmitComments(*MI);
610   O << '\n';
611
612   processDebugLoc(MI, false);
613 }
614
615 /// runOnMachineFunction - This uses the printMachineInstruction()
616 /// method to print assembly for each instruction.
617 ///
618 bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
619   this->MF = &MF;
620
621   SetupMachineFunction(MF);
622   O << "\n\n";
623
624   // Print out constants referenced by the function
625   EmitConstantPool(MF.getConstantPool());
626
627   // Print out labels for the function.
628   const Function *F = MF.getFunction();
629   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
630
631   switch (F->getLinkage()) {
632   default: llvm_unreachable("Unknown linkage type!");
633   case Function::PrivateLinkage:
634   case Function::InternalLinkage:  // Symbols default to internal.
635     break;
636   case Function::ExternalLinkage:
637     O << "\t.global\t" << *CurrentFnSym << '\n' << "\t.type\t";
638     O << *CurrentFnSym << ", @function\n";
639     break;
640   case Function::LinkerPrivateLinkage:
641   case Function::WeakAnyLinkage:
642   case Function::WeakODRLinkage:
643   case Function::LinkOnceAnyLinkage:
644   case Function::LinkOnceODRLinkage:
645     O << "\t.global\t" << *CurrentFnSym << '\n';
646     O << "\t.weak\t" << *CurrentFnSym << '\n';
647     break;
648   }
649
650   printVisibility(CurrentFnSym, F->getVisibility());
651
652   EmitAlignment(MF.getAlignment(), F);
653
654   if (Subtarget.isPPC64()) {
655     // Emit an official procedure descriptor.
656     // FIXME 64-bit SVR4: Use MCSection here!
657     O << "\t.section\t\".opd\",\"aw\"\n";
658     O << "\t.align 3\n";
659     O << *CurrentFnSym << ":\n";
660     O << "\t.quad .L." << *CurrentFnSym << ",.TOC.@tocbase\n";
661     O << "\t.previous\n";
662     O << ".L." << *CurrentFnSym << ":\n";
663   } else {
664     O << *CurrentFnSym << ":\n";
665   }
666
667   // Emit pre-function debug information.
668   DW->BeginFunction(&MF);
669
670   // Print out code for the function.
671   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
672        I != E; ++I) {
673     // Print a label for the basic block.
674     if (I != MF.begin()) {
675       EmitBasicBlockStart(I);
676     }
677     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
678          II != E; ++II) {
679       // Print the assembly for the instruction.
680       printMachineInstruction(II);
681     }
682   }
683
684   O << "\t.size\t" << *CurrentFnSym << ",.-" << *CurrentFnSym << '\n';
685
686   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
687
688   // Emit post-function debug information.
689   DW->EndFunction(&MF);
690
691   // Print out jump tables referenced by the function.
692   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
693
694   // We didn't modify anything.
695   return false;
696 }
697
698 void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
699   const TargetData *TD = TM.getTargetData();
700
701   if (!GVar->hasInitializer())
702     return;   // External global require no code
703
704   // Check to see if this is a special global used by LLVM, if so, emit it.
705   if (EmitSpecialLLVMGlobal(GVar))
706     return;
707
708   MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
709
710   printVisibility(GVarSym, GVar->getVisibility());
711
712   Constant *C = GVar->getInitializer();
713   const Type *Type = C->getType();
714   unsigned Size = TD->getTypeAllocSize(Type);
715   unsigned Align = TD->getPreferredAlignmentLog(GVar);
716
717   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
718                                                                   TM));
719
720   if (C->isNullValue() && /* FIXME: Verify correct */
721       !GVar->hasSection() &&
722       (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
723        GVar->isWeakForLinker())) {
724     if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
725
726     if (GVar->hasExternalLinkage()) {
727       O << "\t.global " << *GVarSym << '\n';
728       O << "\t.type " << *GVarSym << ", @object\n";
729       O << *GVarSym << ":\n";
730       O << "\t.zero " << Size << '\n';
731     } else if (GVar->hasLocalLinkage()) {
732       O << MAI->getLCOMMDirective() << *GVarSym << ',' << Size;
733     } else {
734       O << ".comm " << *GVarSym << ',' << Size;
735     }
736     if (VerboseAsm) {
737       O << "\t\t" << MAI->getCommentString() << " '";
738       WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
739       O << "'";
740     }
741     O << '\n';
742     return;
743   }
744
745   switch (GVar->getLinkage()) {
746    case GlobalValue::LinkOnceAnyLinkage:
747    case GlobalValue::LinkOnceODRLinkage:
748    case GlobalValue::WeakAnyLinkage:
749    case GlobalValue::WeakODRLinkage:
750    case GlobalValue::CommonLinkage:
751    case GlobalValue::LinkerPrivateLinkage:
752     O << "\t.global " << *GVarSym;
753     O << "\n\t.type " << *GVarSym << ", @object\n\t.weak " << *GVarSym << '\n';
754     break;
755    case GlobalValue::AppendingLinkage:
756     // FIXME: appending linkage variables should go into a section of
757     // their name or something.  For now, just emit them as external.
758    case GlobalValue::ExternalLinkage:
759     // If external or appending, declare as a global symbol
760     O << "\t.global " << *GVarSym;
761     O << "\n\t.type " << *GVarSym << ", @object\n";
762     // FALL THROUGH
763    case GlobalValue::InternalLinkage:
764    case GlobalValue::PrivateLinkage:
765     break;
766    default:
767     llvm_unreachable("Unknown linkage type!");
768   }
769
770   EmitAlignment(Align, GVar);
771   O << *GVarSym << ":";
772   if (VerboseAsm) {
773     O << "\t\t\t\t" << MAI->getCommentString() << " '";
774     WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
775     O << "'";
776   }
777   O << '\n';
778
779   EmitGlobalConstant(C);
780   O << '\n';
781 }
782
783 bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
784   const TargetData *TD = TM.getTargetData();
785
786   bool isPPC64 = TD->getPointerSizeInBits() == 64;
787
788   if (isPPC64 && !TOC.empty()) {
789     // FIXME 64-bit SVR4: Use MCSection here?
790     O << "\t.section\t\".toc\",\"aw\"\n";
791
792     // FIXME: This is nondeterminstic!
793     for (DenseMap<const MCSymbol*, const MCSymbol*>::iterator I = TOC.begin(),
794          E = TOC.end(); I != E; ++I) {
795       O << *I->second << ":\n";
796       O << "\t.tc " << *I->first << "[TC]," << *I->first << '\n';
797     }
798   }
799
800   return AsmPrinter::doFinalization(M);
801 }
802
803 /// runOnMachineFunction - This uses the printMachineInstruction()
804 /// method to print assembly for each instruction.
805 ///
806 bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
807   this->MF = &MF;
808
809   SetupMachineFunction(MF);
810   O << "\n\n";
811
812   // Print out constants referenced by the function
813   EmitConstantPool(MF.getConstantPool());
814
815   // Print out labels for the function.
816   const Function *F = MF.getFunction();
817   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
818
819   switch (F->getLinkage()) {
820   default: llvm_unreachable("Unknown linkage type!");
821   case Function::PrivateLinkage:
822   case Function::InternalLinkage:  // Symbols default to internal.
823     break;
824   case Function::ExternalLinkage:
825     O << "\t.globl\t" << *CurrentFnSym << '\n';
826     break;
827   case Function::WeakAnyLinkage:
828   case Function::WeakODRLinkage:
829   case Function::LinkOnceAnyLinkage:
830   case Function::LinkOnceODRLinkage:
831   case Function::LinkerPrivateLinkage:
832     O << "\t.globl\t" << *CurrentFnSym << '\n';
833     O << "\t.weak_definition\t" << *CurrentFnSym << '\n';
834     break;
835   }
836
837   printVisibility(CurrentFnSym, F->getVisibility());
838
839   EmitAlignment(MF.getAlignment(), F);
840   O << *CurrentFnSym << ":\n";
841
842   // Emit pre-function debug information.
843   DW->BeginFunction(&MF);
844
845   // If the function is empty, then we need to emit *something*. Otherwise, the
846   // function's label might be associated with something that it wasn't meant to
847   // be associated with. We emit a noop in this situation.
848   MachineFunction::iterator I = MF.begin();
849
850   if (++I == MF.end() && MF.front().empty())
851     O << "\tnop\n";
852
853   // Print out code for the function.
854   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
855        I != E; ++I) {
856     // Print a label for the basic block.
857     if (I != MF.begin()) {
858       EmitBasicBlockStart(I);
859     }
860     for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
861          II != IE; ++II) {
862       // Print the assembly for the instruction.
863       printMachineInstruction(II);
864     }
865   }
866
867   // Emit post-function debug information.
868   DW->EndFunction(&MF);
869
870   // Print out jump tables referenced by the function.
871   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
872
873   // We didn't modify anything.
874   return false;
875 }
876
877
878 void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
879   static const char *const CPUDirectives[] = {
880     "",
881     "ppc",
882     "ppc601",
883     "ppc602",
884     "ppc603",
885     "ppc7400",
886     "ppc750",
887     "ppc970",
888     "ppc64"
889   };
890
891   unsigned Directive = Subtarget.getDarwinDirective();
892   if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
893     Directive = PPC::DIR_970;
894   if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
895     Directive = PPC::DIR_7400;
896   if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
897     Directive = PPC::DIR_64;
898   assert(Directive <= PPC::DIR_64 && "Directive out of range.");
899   O << "\t.machine " << CPUDirectives[Directive] << '\n';
900
901   // Prime text sections so they are adjacent.  This reduces the likelihood a
902   // large data or debug section causes a branch to exceed 16M limit.
903   TargetLoweringObjectFileMachO &TLOFMacho = 
904     static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
905   OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
906   if (TM.getRelocationModel() == Reloc::PIC_) {
907     OutStreamer.SwitchSection(
908             TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
909                                       MCSectionMachO::S_SYMBOL_STUBS |
910                                       MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
911                                       32, SectionKind::getText()));
912   } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
913     OutStreamer.SwitchSection(
914             TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
915                                       MCSectionMachO::S_SYMBOL_STUBS |
916                                       MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
917                                       16, SectionKind::getText()));
918   }
919   OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
920 }
921
922 void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
923   const TargetData *TD = TM.getTargetData();
924
925   if (!GVar->hasInitializer())
926     return;   // External global require no code
927
928   // Check to see if this is a special global used by LLVM, if so, emit it.
929   if (EmitSpecialLLVMGlobal(GVar)) {
930     if (TM.getRelocationModel() == Reloc::Static) {
931       if (GVar->getName() == "llvm.global_ctors")
932         O << ".reference .constructors_used\n";
933       else if (GVar->getName() == "llvm.global_dtors")
934         O << ".reference .destructors_used\n";
935     }
936     return;
937   }
938
939   MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
940   printVisibility(GVarSym, GVar->getVisibility());
941
942   Constant *C = GVar->getInitializer();
943   const Type *Type = C->getType();
944   unsigned Size = TD->getTypeAllocSize(Type);
945   unsigned Align = TD->getPreferredAlignmentLog(GVar);
946
947   SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
948   const MCSection *TheSection =
949     getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
950   OutStreamer.SwitchSection(TheSection);
951
952   // Handle the zerofill directive on darwin, which is a special form of BSS
953   // emission.
954   if (GVKind.isBSS() && MAI->hasMachoZeroFillDirective()) {
955     TargetLoweringObjectFileMachO &TLOFMacho = 
956       static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
957     if (TLOFMacho.isDataCommonSection(TheSection)) {
958       // .globl _foo
959       OutStreamer.EmitSymbolAttribute(GVarSym, MCStreamer::Global);
960       // .zerofill __DATA, __common, _foo, 400, 5
961       OutStreamer.EmitZerofill(TheSection, GVarSym, Size, 1 << Align);
962       return;
963     }
964   }
965   
966   /// FIXME: Drive this off the section!
967   if (C->isNullValue() && /* FIXME: Verify correct */
968       !GVar->hasSection() &&
969       (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
970        GVar->isWeakForLinker()) &&
971       // Don't put things that should go in the cstring section into "comm".
972       !TheSection->getKind().isMergeableCString()) {
973     if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
974
975     if (GVar->hasLocalLinkage()) {
976       O << MAI->getLCOMMDirective() << *GVarSym << ',' << Size << ',' << Align;
977     } else if (!GVar->hasCommonLinkage()) {
978       O << "\t.globl " << *GVarSym << '\n' << MAI->getWeakDefDirective();
979       O << *GVarSym << '\n';
980       EmitAlignment(Align, GVar);
981       O << *GVarSym << ":";
982       if (VerboseAsm) {
983         O << "\t\t\t\t" << MAI->getCommentString() << " ";
984         WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
985       }
986       O << '\n';
987       EmitGlobalConstant(C);
988       return;
989     } else {
990       O << ".comm " << *GVarSym << ',' << Size;
991       // Darwin 9 and above support aligned common data.
992       if (Subtarget.isDarwin9())
993         O << ',' << Align;
994     }
995     if (VerboseAsm) {
996       O << "\t\t" << MAI->getCommentString() << " '";
997       WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
998       O << "'";
999     }
1000     O << '\n';
1001     return;
1002   }
1003
1004   switch (GVar->getLinkage()) {
1005   case GlobalValue::LinkOnceAnyLinkage:
1006   case GlobalValue::LinkOnceODRLinkage:
1007   case GlobalValue::WeakAnyLinkage:
1008   case GlobalValue::WeakODRLinkage:
1009   case GlobalValue::CommonLinkage:
1010   case GlobalValue::LinkerPrivateLinkage:
1011     O << "\t.globl " << *GVarSym << "\n\t.weak_definition " << *GVarSym << '\n';
1012     break;
1013   case GlobalValue::AppendingLinkage:
1014     // FIXME: appending linkage variables should go into a section of
1015     // their name or something.  For now, just emit them as external.
1016   case GlobalValue::ExternalLinkage:
1017     // If external or appending, declare as a global symbol
1018     O << "\t.globl " << *GVarSym << '\n';
1019     // FALL THROUGH
1020   case GlobalValue::InternalLinkage:
1021   case GlobalValue::PrivateLinkage:
1022     break;
1023   default:
1024     llvm_unreachable("Unknown linkage type!");
1025   }
1026
1027   EmitAlignment(Align, GVar);
1028   O << *GVarSym << ":";
1029   if (VerboseAsm) {
1030     O << "\t\t\t\t" << MAI->getCommentString() << " '";
1031     WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1032     O << "'";
1033   }
1034   O << '\n';
1035
1036   EmitGlobalConstant(C);
1037   O << '\n';
1038 }
1039
1040 bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
1041   const TargetData *TD = TM.getTargetData();
1042
1043   bool isPPC64 = TD->getPointerSizeInBits() == 64;
1044
1045   // Darwin/PPC always uses mach-o.
1046   TargetLoweringObjectFileMachO &TLOFMacho = 
1047     static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1048
1049   
1050   const MCSection *LSPSection = 0;
1051   if (!FnStubs.empty()) // .lazy_symbol_pointer
1052     LSPSection = TLOFMacho.getLazySymbolPointerSection();
1053     
1054   
1055   // Output stubs for dynamically-linked functions
1056   if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
1057     const MCSection *StubSection = 
1058       TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
1059                                 MCSectionMachO::S_SYMBOL_STUBS |
1060                                 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1061                                 32, SectionKind::getText());
1062     // FIXME: This is emitting in nondeterminstic order!
1063     for (DenseMap<const MCSymbol*, FnStubInfo>::iterator I = 
1064            FnStubs.begin(), E = FnStubs.end(); I != E; ++I) {
1065       OutStreamer.SwitchSection(StubSection);
1066       EmitAlignment(4);
1067       const FnStubInfo &Info = I->second;
1068       O << *Info.Stub << ":\n";
1069       O << "\t.indirect_symbol " << *I->first << '\n';
1070       O << "\tmflr r0\n";
1071       O << "\tbcl 20,31," << *Info.AnonSymbol << '\n';
1072       O << *Info.AnonSymbol << ":\n";
1073       O << "\tmflr r11\n";
1074       O << "\taddis r11,r11,ha16(" << *Info.LazyPtr << '-' << *Info.AnonSymbol
1075         << ")\n";
1076       O << "\tmtlr r0\n";
1077       O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(" << *Info.LazyPtr
1078         << '-' << *Info.AnonSymbol << ")(r11)\n";
1079       O << "\tmtctr r12\n";
1080       O << "\tbctr\n";
1081       
1082       OutStreamer.SwitchSection(LSPSection);
1083       O << *Info.LazyPtr << ":\n";
1084       O << "\t.indirect_symbol " << *I->first << '\n';
1085       O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
1086     }
1087   } else if (!FnStubs.empty()) {
1088     const MCSection *StubSection =
1089       TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1090                                 MCSectionMachO::S_SYMBOL_STUBS |
1091                                 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1092                                 16, SectionKind::getText());
1093     
1094     // FIXME: This is emitting in nondeterminstic order!
1095     for (DenseMap<const MCSymbol*, FnStubInfo>::iterator I = FnStubs.begin(),
1096          E = FnStubs.end(); I != E; ++I) {
1097       OutStreamer.SwitchSection(StubSection);
1098       EmitAlignment(4);
1099       const FnStubInfo &Info = I->second;
1100       O << *Info.Stub << ":\n";
1101       O << "\t.indirect_symbol " << *I->first << '\n';
1102       O << "\tlis r11,ha16(" << *Info.LazyPtr << ")\n";
1103       O << (isPPC64 ? "\tldu" :  "\tlwzu") << " r12,lo16(" << *Info.LazyPtr
1104         << ")(r11)\n";
1105       O << "\tmtctr r12\n";
1106       O << "\tbctr\n";
1107       OutStreamer.SwitchSection(LSPSection);
1108       O << *Info.LazyPtr << ":\n";
1109       O << "\t.indirect_symbol " << *I->first << '\n';
1110       O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
1111     }
1112   }
1113
1114   O << '\n';
1115
1116   if (MAI->doesSupportExceptionHandling() && MMI) {
1117     // Add the (possibly multiple) personalities to the set of global values.
1118     // Only referenced functions get into the Personalities list.
1119     const std::vector<Function *> &Personalities = MMI->getPersonalities();
1120     for (std::vector<Function *>::const_iterator I = Personalities.begin(),
1121          E = Personalities.end(); I != E; ++I) {
1122       if (*I)
1123         GVStubs[GetGlobalValueSymbol(*I)] =
1124           GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
1125     }
1126   }
1127
1128   // Output macho stubs for external and common global variables.
1129   if (!GVStubs.empty()) {
1130     // Switch with ".non_lazy_symbol_pointer" directive.
1131     OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
1132     EmitAlignment(isPPC64 ? 3 : 2);
1133     
1134     // FIXME: This is nondeterminstic.
1135     for (DenseMap<const MCSymbol *, const MCSymbol *>::iterator
1136          I = GVStubs.begin(), E = GVStubs.end(); I != E; ++I) {
1137       O << *I->second << ":\n";
1138       O << "\t.indirect_symbol " << *I->first << '\n';
1139       O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
1140     }
1141   }
1142
1143   if (!HiddenGVStubs.empty()) {
1144     OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
1145     EmitAlignment(isPPC64 ? 3 : 2);
1146     // FIXME: This is nondeterminstic.
1147     for (DenseMap<const MCSymbol *, const MCSymbol *>::iterator
1148          I = HiddenGVStubs.begin(), E = HiddenGVStubs.end(); I != E; ++I) {
1149       O << *I->second << ":\n";
1150       O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << *I->first << '\n';
1151     }
1152   }
1153
1154   // Funny Darwin hack: This flag tells the linker that no global symbols
1155   // contain code that falls through to other global symbols (e.g. the obvious
1156   // implementation of multiple entry points).  If this doesn't occur, the
1157   // linker can safely perform dead code stripping.  Since LLVM never generates
1158   // code that does this, it is always safe to set.
1159   OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
1160
1161   return AsmPrinter::doFinalization(M);
1162 }
1163
1164
1165
1166 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1167 /// for a MachineFunction to the given output stream, in a format that the
1168 /// Darwin assembler can deal with.
1169 ///
1170 static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
1171                                            TargetMachine &tm,
1172                                            const MCAsmInfo *tai,
1173                                            bool verbose) {
1174   const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1175
1176   if (Subtarget->isDarwin())
1177     return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
1178   return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
1179 }
1180
1181 // Force static initialization.
1182 extern "C" void LLVMInitializePowerPCAsmPrinter() { 
1183   TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
1184   TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1185 }