PPCAsmPrinter.cpp revision 37dfa02788711b72618497ce5acf39596e3a0211
1//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to 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 "PPCTargetMachine.h"
22#include "PPCSubtarget.h"
23#include "llvm/Constants.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Module.h"
26#include "llvm/Assembly/Writer.h"
27#include "llvm/CodeGen/AsmPrinter.h"
28#include "llvm/CodeGen/DwarfWriter.h"
29#include "llvm/CodeGen/MachineDebugInfo.h"
30#include "llvm/CodeGen/MachineFunctionPass.h"
31#include "llvm/CodeGen/MachineInstr.h"
32#include "llvm/Support/Mangler.h"
33#include "llvm/Support/MathExtras.h"
34#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/Debug.h"
36#include "llvm/Support/Compiler.h"
37#include "llvm/Target/TargetAsmInfo.h"
38#include "llvm/Target/MRegisterInfo.h"
39#include "llvm/Target/TargetInstrInfo.h"
40#include "llvm/Target/TargetOptions.h"
41#include "llvm/ADT/Statistic.h"
42#include "llvm/ADT/StringExtras.h"
43#include <iostream>
44#include <set>
45using namespace llvm;
46
47namespace {
48  Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
49
50  struct VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
51    std::set<std::string> FnStubs, GVStubs;
52    const PPCSubtarget &Subtarget;
53
54    PPCAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
55      : AsmPrinter(O, TM, T), Subtarget(TM.getSubtarget<PPCSubtarget>()) {
56    }
57
58    virtual const char *getPassName() const {
59      return "PowerPC Assembly Printer";
60    }
61
62    PPCTargetMachine &getTM() {
63      return static_cast<PPCTargetMachine&>(TM);
64    }
65
66    unsigned enumRegToMachineReg(unsigned enumReg) {
67      switch (enumReg) {
68      default: assert(0 && "Unhandled register!"); break;
69      case PPC::CR0:  return  0;
70      case PPC::CR1:  return  1;
71      case PPC::CR2:  return  2;
72      case PPC::CR3:  return  3;
73      case PPC::CR4:  return  4;
74      case PPC::CR5:  return  5;
75      case PPC::CR6:  return  6;
76      case PPC::CR7:  return  7;
77      }
78      abort();
79    }
80
81    /// printInstruction - This method is automatically generated by tablegen
82    /// from the instruction set description.  This method returns true if the
83    /// machine instruction was sufficiently described to print it, otherwise it
84    /// returns false.
85    bool printInstruction(const MachineInstr *MI);
86
87    void printMachineInstruction(const MachineInstr *MI);
88    void printOp(const MachineOperand &MO);
89
90    void printOperand(const MachineInstr *MI, unsigned OpNo) {
91      const MachineOperand &MO = MI->getOperand(OpNo);
92      if (MO.isRegister()) {
93        assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
94        O << TM.getRegisterInfo()->get(MO.getReg()).Name;
95      } else if (MO.isImmediate()) {
96        O << MO.getImmedValue();
97      } else {
98        printOp(MO);
99      }
100    }
101
102    bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
103                         unsigned AsmVariant, const char *ExtraCode);
104    bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
105                               unsigned AsmVariant, const char *ExtraCode);
106
107
108    void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
109      char value = MI->getOperand(OpNo).getImmedValue();
110      value = (value << (32-5)) >> (32-5);
111      O << (int)value;
112    }
113    void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
114      unsigned char value = MI->getOperand(OpNo).getImmedValue();
115      assert(value <= 31 && "Invalid u5imm argument!");
116      O << (unsigned int)value;
117    }
118    void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
119      unsigned char value = MI->getOperand(OpNo).getImmedValue();
120      assert(value <= 63 && "Invalid u6imm argument!");
121      O << (unsigned int)value;
122    }
123    void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
124      O << (short)MI->getOperand(OpNo).getImmedValue();
125    }
126    void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
127      O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
128    }
129    void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
130      O << (short)(MI->getOperand(OpNo).getImmedValue()*4);
131    }
132    void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
133      // Branches can take an immediate operand.  This is used by the branch
134      // selection pass to print $+8, an eight byte displacement from the PC.
135      if (MI->getOperand(OpNo).isImmediate()) {
136        O << "$+" << MI->getOperand(OpNo).getImmedValue()*4;
137      } else {
138        printOp(MI->getOperand(OpNo));
139      }
140    }
141    void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
142      const MachineOperand &MO = MI->getOperand(OpNo);
143      if (TM.getRelocationModel() != Reloc::Static) {
144        if (MO.getType() == MachineOperand::MO_GlobalAddress) {
145          GlobalValue *GV = MO.getGlobal();
146          if (((GV->isExternal() || GV->hasWeakLinkage() ||
147                GV->hasLinkOnceLinkage()))) {
148            // Dynamically-resolved functions need a stub for the function.
149            std::string Name = Mang->getValueName(GV);
150            FnStubs.insert(Name);
151            O << "L" << Name << "$stub";
152            return;
153          }
154        }
155        if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
156          std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
157          FnStubs.insert(Name);
158          O << "L" << Name << "$stub";
159          return;
160        }
161      }
162
163      printOp(MI->getOperand(OpNo));
164    }
165    void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
166     O << (int)MI->getOperand(OpNo).getImmedValue()*4;
167    }
168    void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
169      O << "\"L" << getFunctionNumber() << "$pb\"\n";
170      O << "\"L" << getFunctionNumber() << "$pb\":";
171    }
172    void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
173      if (MI->getOperand(OpNo).isImmediate()) {
174        printS16ImmOperand(MI, OpNo);
175      } else {
176        O << "ha16(";
177        printOp(MI->getOperand(OpNo));
178        if (TM.getRelocationModel() == Reloc::PIC_)
179          O << "-\"L" << getFunctionNumber() << "$pb\")";
180        else
181          O << ')';
182      }
183    }
184    void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
185      if (MI->getOperand(OpNo).isImmediate()) {
186        printS16ImmOperand(MI, OpNo);
187      } else {
188        O << "lo16(";
189        printOp(MI->getOperand(OpNo));
190        if (TM.getRelocationModel() == Reloc::PIC_)
191          O << "-\"L" << getFunctionNumber() << "$pb\")";
192        else
193          O << ')';
194      }
195    }
196    void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
197      unsigned CCReg = MI->getOperand(OpNo).getReg();
198      unsigned RegNo = enumRegToMachineReg(CCReg);
199      O << (0x80 >> RegNo);
200    }
201    // The new addressing mode printers.
202    void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
203      printSymbolLo(MI, OpNo);
204      O << '(';
205      if (MI->getOperand(OpNo+1).isRegister() &&
206          MI->getOperand(OpNo+1).getReg() == PPC::R0)
207        O << "0";
208      else
209        printOperand(MI, OpNo+1);
210      O << ')';
211    }
212    void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
213      if (MI->getOperand(OpNo).isImmediate())
214        printS16X4ImmOperand(MI, OpNo);
215      else
216        printSymbolLo(MI, OpNo);
217      O << '(';
218      if (MI->getOperand(OpNo+1).isRegister() &&
219          MI->getOperand(OpNo+1).getReg() == PPC::R0)
220        O << "0";
221      else
222        printOperand(MI, OpNo+1);
223      O << ')';
224    }
225
226    void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
227      // When used as the base register, r0 reads constant zero rather than
228      // the value contained in the register.  For this reason, the darwin
229      // assembler requires that we print r0 as 0 (no r) when used as the base.
230      const MachineOperand &MO = MI->getOperand(OpNo);
231      if (MO.getReg() == PPC::R0)
232        O << '0';
233      else
234        O << TM.getRegisterInfo()->get(MO.getReg()).Name;
235      O << ", ";
236      printOperand(MI, OpNo+1);
237    }
238
239    virtual bool runOnMachineFunction(MachineFunction &F) = 0;
240    virtual bool doFinalization(Module &M) = 0;
241
242  };
243
244  /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
245  /// X
246  struct VISIBILITY_HIDDEN DarwinAsmPrinter : public PPCAsmPrinter {
247
248    DwarfWriter DW;
249
250    DarwinAsmPrinter(std::ostream &O, PPCTargetMachine &TM,
251                     const TargetAsmInfo *T)
252      : PPCAsmPrinter(O, TM, T), DW(O, this, T) {
253      bool isPPC64 = Subtarget.isPPC64();
254    }
255
256    virtual const char *getPassName() const {
257      return "Darwin PPC Assembly Printer";
258    }
259
260    bool runOnMachineFunction(MachineFunction &F);
261    bool doInitialization(Module &M);
262    bool doFinalization(Module &M);
263
264    void getAnalysisUsage(AnalysisUsage &AU) const {
265      AU.setPreservesAll();
266      AU.addRequired<MachineDebugInfo>();
267      PPCAsmPrinter::getAnalysisUsage(AU);
268    }
269
270  };
271} // end of anonymous namespace
272
273// Include the auto-generated portion of the assembly writer
274#include "PPCGenAsmWriter.inc"
275
276void PPCAsmPrinter::printOp(const MachineOperand &MO) {
277  switch (MO.getType()) {
278  case MachineOperand::MO_Immediate:
279    std::cerr << "printOp() does not handle immediate values\n";
280    abort();
281    return;
282
283  case MachineOperand::MO_MachineBasicBlock:
284    printBasicBlockLabel(MO.getMachineBasicBlock());
285    return;
286  case MachineOperand::MO_JumpTableIndex:
287    O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
288      << '_' << MO.getJumpTableIndex();
289    // FIXME: PIC relocation model
290    return;
291  case MachineOperand::MO_ConstantPoolIndex:
292    O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
293      << '_' << MO.getConstantPoolIndex();
294    return;
295  case MachineOperand::MO_ExternalSymbol:
296    // Computing the address of an external symbol, not calling it.
297    if (TM.getRelocationModel() != Reloc::Static) {
298      std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
299      GVStubs.insert(Name);
300      O << "L" << Name << "$non_lazy_ptr";
301      return;
302    }
303    O << TAI->getGlobalPrefix() << MO.getSymbolName();
304    return;
305  case MachineOperand::MO_GlobalAddress: {
306    // Computing the address of a global symbol, not calling it.
307    GlobalValue *GV = MO.getGlobal();
308    std::string Name = Mang->getValueName(GV);
309    int offset = MO.getOffset();
310
311    // External or weakly linked global variables need non-lazily-resolved stubs
312    if (TM.getRelocationModel() != Reloc::Static) {
313      if (((GV->isExternal() || GV->hasWeakLinkage() ||
314            GV->hasLinkOnceLinkage()))) {
315        GVStubs.insert(Name);
316        O << "L" << Name << "$non_lazy_ptr";
317        return;
318      }
319    }
320
321    O << Name;
322    return;
323  }
324
325  default:
326    O << "<unknown operand type: " << MO.getType() << ">";
327    return;
328  }
329}
330
331/// PrintAsmOperand - Print out an operand for an inline asm expression.
332///
333bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
334                                    unsigned AsmVariant,
335                                    const char *ExtraCode) {
336  // Does this asm operand have a single letter operand modifier?
337  if (ExtraCode && ExtraCode[0]) {
338    if (ExtraCode[1] != 0) return true; // Unknown modifier.
339
340    switch (ExtraCode[0]) {
341    default: return true;  // Unknown modifier.
342    case 'L': // Write second word of DImode reference.
343      // Verify that this operand has two consecutive registers.
344      if (!MI->getOperand(OpNo).isRegister() ||
345          OpNo+1 == MI->getNumOperands() ||
346          !MI->getOperand(OpNo+1).isRegister())
347        return true;
348      ++OpNo;   // Return the high-part.
349      break;
350    }
351  }
352
353  printOperand(MI, OpNo);
354  return false;
355}
356
357bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
358                                          unsigned AsmVariant,
359                                          const char *ExtraCode) {
360  if (ExtraCode && ExtraCode[0])
361    return true; // Unknown modifier.
362  printMemRegReg(MI, OpNo);
363  return false;
364}
365
366/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
367/// the current output stream.
368///
369void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
370  ++EmittedInsts;
371
372  // Check for slwi/srwi mnemonics.
373  if (MI->getOpcode() == PPC::RLWINM) {
374    bool FoundMnemonic = false;
375    unsigned char SH = MI->getOperand(2).getImmedValue();
376    unsigned char MB = MI->getOperand(3).getImmedValue();
377    unsigned char ME = MI->getOperand(4).getImmedValue();
378    if (SH <= 31 && MB == 0 && ME == (31-SH)) {
379      O << "slwi "; FoundMnemonic = true;
380    }
381    if (SH <= 31 && MB == (32-SH) && ME == 31) {
382      O << "srwi "; FoundMnemonic = true;
383      SH = 32-SH;
384    }
385    if (FoundMnemonic) {
386      printOperand(MI, 0);
387      O << ", ";
388      printOperand(MI, 1);
389      O << ", " << (unsigned int)SH << "\n";
390      return;
391    }
392  } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
393    if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
394      O << "mr ";
395      printOperand(MI, 0);
396      O << ", ";
397      printOperand(MI, 1);
398      O << "\n";
399      return;
400    }
401  }
402
403  if (printInstruction(MI))
404    return; // Printer was automatically generated
405
406  assert(0 && "Unhandled instruction in asm writer!");
407  abort();
408  return;
409}
410
411/// runOnMachineFunction - This uses the printMachineInstruction()
412/// method to print assembly for each instruction.
413///
414bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
415  DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
416
417  SetupMachineFunction(MF);
418  O << "\n\n";
419
420  // Print out constants referenced by the function
421  EmitConstantPool(MF.getConstantPool());
422
423  // Print out labels for the function.
424  const Function *F = MF.getFunction();
425  switch (F->getLinkage()) {
426  default: assert(0 && "Unknown linkage type!");
427  case Function::InternalLinkage:  // Symbols default to internal.
428    SwitchToTextSection("\t.text", F);
429    break;
430  case Function::ExternalLinkage:
431    SwitchToTextSection("\t.text", F);
432    O << "\t.globl\t" << CurrentFnName << "\n";
433    break;
434  case Function::WeakLinkage:
435  case Function::LinkOnceLinkage:
436    SwitchToTextSection(
437                ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
438    O << "\t.globl\t" << CurrentFnName << "\n";
439    O << "\t.weak_definition\t" << CurrentFnName << "\n";
440    break;
441  }
442  EmitAlignment(4, F);
443  O << CurrentFnName << ":\n";
444
445  // Emit pre-function debug information.
446  DW.BeginFunction(&MF);
447
448  // Print out code for the function.
449  for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
450       I != E; ++I) {
451    // Print a label for the basic block.
452    if (I != MF.begin()) {
453      printBasicBlockLabel(I, true);
454      O << '\n';
455    }
456    for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
457         II != E; ++II) {
458      // Print the assembly for the instruction.
459      O << "\t";
460      printMachineInstruction(II);
461    }
462  }
463
464  // Emit post-function debug information.
465  DW.EndFunction();
466
467  // Print out jump tables referenced by the function.
468  EmitJumpTableInfo(MF.getJumpTableInfo());
469
470  // We didn't modify anything.
471  return false;
472}
473
474
475bool DarwinAsmPrinter::doInitialization(Module &M) {
476  if (Subtarget.isGigaProcessor())
477    O << "\t.machine ppc970\n";
478  AsmPrinter::doInitialization(M);
479
480  // Darwin wants symbols to be quoted if they have complex names.
481  Mang->setUseQuotes(true);
482
483  // Emit initial debug information.
484  DW.BeginModule(&M);
485  return false;
486}
487
488bool DarwinAsmPrinter::doFinalization(Module &M) {
489  const TargetData *TD = TM.getTargetData();
490
491  // Print out module-level global variables here.
492  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
493       I != E; ++I) {
494    if (!I->hasInitializer()) continue;   // External global require no code
495
496    // Check to see if this is a special global used by LLVM, if so, emit it.
497    if (EmitSpecialLLVMGlobal(I))
498      continue;
499
500    std::string name = Mang->getValueName(I);
501    Constant *C = I->getInitializer();
502    unsigned Size = TD->getTypeSize(C->getType());
503    unsigned Align = getPreferredAlignmentLog(I);
504
505    if (C->isNullValue() && /* FIXME: Verify correct */
506        (I->hasInternalLinkage() || I->hasWeakLinkage() ||
507         I->hasLinkOnceLinkage() ||
508         (I->hasExternalLinkage() && !I->hasSection()))) {
509      if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
510      if (I->hasExternalLinkage()) {
511        O << "\t.globl " << name << '\n';
512        O << "\t.zerofill __DATA, __common, " << name << ", "
513          << Size << ", " << Align;
514      } else if (I->hasInternalLinkage()) {
515        SwitchToDataSection("\t.data", I);
516        O << TAI->getLCOMMDirective() << name << "," << Size << "," << Align;
517      } else {
518        SwitchToDataSection("\t.data", I);
519        O << ".comm " << name << "," << Size;
520      }
521      O << "\t\t; '" << I->getName() << "'\n";
522    } else {
523      switch (I->getLinkage()) {
524      case GlobalValue::LinkOnceLinkage:
525      case GlobalValue::WeakLinkage:
526        O << "\t.globl " << name << '\n'
527          << "\t.weak_definition " << name << '\n';
528        SwitchToDataSection(".section __DATA,__datacoal_nt,coalesced", I);
529        break;
530      case GlobalValue::AppendingLinkage:
531        // FIXME: appending linkage variables should go into a section of
532        // their name or something.  For now, just emit them as external.
533      case GlobalValue::ExternalLinkage:
534        // If external or appending, declare as a global symbol
535        O << "\t.globl " << name << "\n";
536        // FALL THROUGH
537      case GlobalValue::InternalLinkage:
538        SwitchToDataSection("\t.data", I);
539        break;
540      default:
541        std::cerr << "Unknown linkage type!";
542        abort();
543      }
544
545      EmitAlignment(Align, I);
546      O << name << ":\t\t\t\t; '" << I->getName() << "'\n";
547      EmitGlobalConstant(C);
548      O << '\n';
549    }
550  }
551
552  bool isPPC64 = TD->getPointerSizeInBits() == 64;
553
554  // Output stubs for dynamically-linked functions
555  if (TM.getRelocationModel() == Reloc::PIC_) {
556    for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
557         i != e; ++i) {
558      SwitchToTextSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
559                          "pure_instructions,32", 0);
560      EmitAlignment(4);
561      O << "L" << *i << "$stub:\n";
562      O << "\t.indirect_symbol " << *i << "\n";
563      O << "\tmflr r0\n";
564      O << "\tbcl 20,31,L0$" << *i << "\n";
565      O << "L0$" << *i << ":\n";
566      O << "\tmflr r11\n";
567      O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
568      O << "\tmtlr r0\n";
569      if (isPPC64)
570        O << "\tldu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
571      else
572        O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
573      O << "\tmtctr r12\n";
574      O << "\tbctr\n";
575      SwitchToDataSection(".lazy_symbol_pointer", 0);
576      O << "L" << *i << "$lazy_ptr:\n";
577      O << "\t.indirect_symbol " << *i << "\n";
578      if (isPPC64)
579        O << "\t.quad dyld_stub_binding_helper\n";
580      else
581        O << "\t.long dyld_stub_binding_helper\n";
582    }
583  } else {
584    for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
585         i != e; ++i) {
586      SwitchToTextSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
587                          "pure_instructions,16", 0);
588      EmitAlignment(4);
589      O << "L" << *i << "$stub:\n";
590      O << "\t.indirect_symbol " << *i << "\n";
591      O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
592      if (isPPC64)
593        O << "\tldu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
594      else
595        O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
596      O << "\tmtctr r12\n";
597      O << "\tbctr\n";
598      SwitchToDataSection(".lazy_symbol_pointer", 0);
599      O << "L" << *i << "$lazy_ptr:\n";
600      O << "\t.indirect_symbol " << *i << "\n";
601      if (isPPC64)
602        O << "\t.quad dyld_stub_binding_helper\n";
603      else
604        O << "\t.long dyld_stub_binding_helper\n";
605    }
606  }
607
608  O << "\n";
609
610  // Output stubs for external and common global variables.
611  if (GVStubs.begin() != GVStubs.end()) {
612    SwitchToDataSection(".non_lazy_symbol_pointer", 0);
613    for (std::set<std::string>::iterator I = GVStubs.begin(),
614         E = GVStubs.end(); I != E; ++I) {
615      O << "L" << *I << "$non_lazy_ptr:\n";
616      O << "\t.indirect_symbol " << *I << "\n";
617      if (isPPC64)
618        O << "\t.quad\t0\n";
619      else
620        O << "\t.long\t0\n";
621
622    }
623  }
624
625  // Emit initial debug information.
626  DW.EndModule();
627
628  // Funny Darwin hack: This flag tells the linker that no global symbols
629  // contain code that falls through to other global symbols (e.g. the obvious
630  // implementation of multiple entry points).  If this doesn't occur, the
631  // linker can safely perform dead code stripping.  Since LLVM never generates
632  // code that does this, it is always safe to set.
633  O << "\t.subsections_via_symbols\n";
634
635  AsmPrinter::doFinalization(M);
636  return false; // success
637}
638
639
640
641/// createDarwinCodePrinterPass - Returns a pass that prints the PPC assembly
642/// code for a MachineFunction to the given output stream, in a format that the
643/// Darwin assembler can deal with.
644///
645FunctionPass *llvm::createPPCAsmPrinterPass(std::ostream &o,
646                                            PPCTargetMachine &tm) {
647  return new DarwinAsmPrinter(o, tm, tm.getTargetAsmInfo());
648}
649
650