PPCAsmPrinter.cpp revision 88276b887c426cf5d998b705a14663bfa38f8efd
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/MachineDebugInfo.h"
29#include "llvm/CodeGen/MachineFunctionPass.h"
30#include "llvm/CodeGen/MachineInstr.h"
31#include "llvm/Support/Mangler.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/Debug.h"
35#include "llvm/Target/MRegisterInfo.h"
36#include "llvm/Target/TargetInstrInfo.h"
37#include "llvm/ADT/Statistic.h"
38#include "llvm/ADT/StringExtras.h"
39#include <set>
40using namespace llvm;
41
42namespace {
43  Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
44
45  class PPCAsmPrinter : public AsmPrinter {
46  public:
47    std::set<std::string> FnStubs, GVStubs;
48
49    PPCAsmPrinter(std::ostream &O, TargetMachine &TM)
50      : AsmPrinter(O, TM) {}
51
52    virtual const char *getPassName() const {
53      return "PowerPC Assembly Printer";
54    }
55
56    PPCTargetMachine &getTM() {
57      return static_cast<PPCTargetMachine&>(TM);
58    }
59
60    unsigned enumRegToMachineReg(unsigned enumReg) {
61      switch (enumReg) {
62      default: assert(0 && "Unhandled register!"); break;
63      case PPC::CR0:  return  0;
64      case PPC::CR1:  return  1;
65      case PPC::CR2:  return  2;
66      case PPC::CR3:  return  3;
67      case PPC::CR4:  return  4;
68      case PPC::CR5:  return  5;
69      case PPC::CR6:  return  6;
70      case PPC::CR7:  return  7;
71      }
72      abort();
73    }
74
75    /// printInstruction - This method is automatically generated by tablegen
76    /// from the instruction set description.  This method returns true if the
77    /// machine instruction was sufficiently described to print it, otherwise it
78    /// returns false.
79    bool printInstruction(const MachineInstr *MI);
80
81    void printMachineInstruction(const MachineInstr *MI);
82    void printOp(const MachineOperand &MO);
83
84    void printOperand(const MachineInstr *MI, unsigned OpNo){
85      const MachineOperand &MO = MI->getOperand(OpNo);
86      if (MO.getType() == MachineOperand::MO_MachineRegister) {
87        assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
88        O << TM.getRegisterInfo()->get(MO.getReg()).Name;
89      } else if (MO.isImmediate()) {
90        O << MO.getImmedValue();
91      } else {
92        printOp(MO);
93      }
94    }
95
96    void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
97      unsigned char value = MI->getOperand(OpNo).getImmedValue();
98      assert(value <= 31 && "Invalid u5imm argument!");
99      O << (unsigned int)value;
100    }
101    void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
102      unsigned char value = MI->getOperand(OpNo).getImmedValue();
103      assert(value <= 63 && "Invalid u6imm argument!");
104      O << (unsigned int)value;
105    }
106    void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
107      O << (short)MI->getOperand(OpNo).getImmedValue();
108    }
109    void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
110      O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
111    }
112    void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
113      O << (short)MI->getOperand(OpNo).getImmedValue()*4;
114    }
115    void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
116      // Branches can take an immediate operand.  This is used by the branch
117      // selection pass to print $+8, an eight byte displacement from the PC.
118      if (MI->getOperand(OpNo).isImmediate()) {
119        O << "$+" << MI->getOperand(OpNo).getImmedValue();
120      } else {
121        printOp(MI->getOperand(OpNo));
122      }
123    }
124    void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
125      const MachineOperand &MO = MI->getOperand(OpNo);
126      if (!PPCGenerateStaticCode) {
127        if (MO.getType() == MachineOperand::MO_GlobalAddress) {
128          GlobalValue *GV = MO.getGlobal();
129          if (((GV->isExternal() || GV->hasWeakLinkage() ||
130                GV->hasLinkOnceLinkage()))) {
131            // Dynamically-resolved functions need a stub for the function.
132            std::string Name = Mang->getValueName(GV);
133            FnStubs.insert(Name);
134            O << "L" << Name << "$stub";
135            return;
136          }
137        }
138        if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
139          std::string Name(GlobalPrefix); Name += MO.getSymbolName();
140          FnStubs.insert(Name);
141          O << "L" << Name << "$stub";
142          return;
143        }
144      }
145
146      printOp(MI->getOperand(OpNo));
147    }
148    void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
149     O << (int)MI->getOperand(OpNo).getImmedValue()*4;
150    }
151    void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
152      O << "\"L" << getFunctionNumber() << "$pb\"\n";
153      O << "\"L" << getFunctionNumber() << "$pb\":";
154    }
155    void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
156      if (MI->getOperand(OpNo).isImmediate()) {
157        printS16ImmOperand(MI, OpNo);
158      } else {
159        O << "ha16(";
160        printOp(MI->getOperand(OpNo));
161        if (PICEnabled)
162          O << "-\"L" << getFunctionNumber() << "$pb\")";
163        else
164          O << ')';
165      }
166    }
167    void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
168      if (MI->getOperand(OpNo).isImmediate()) {
169        printS16ImmOperand(MI, OpNo);
170      } else {
171        O << "lo16(";
172        printOp(MI->getOperand(OpNo));
173        if (PICEnabled)
174          O << "-\"L" << getFunctionNumber() << "$pb\")";
175        else
176          O << ')';
177      }
178    }
179    void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
180      unsigned CCReg = MI->getOperand(OpNo).getReg();
181      unsigned RegNo = enumRegToMachineReg(CCReg);
182      O << (0x80 >> RegNo);
183    }
184    // The new addressing mode printers, currently empty
185    void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
186      printSymbolLo(MI, OpNo);
187      O << '(';
188      printOperand(MI, OpNo+1);
189      O << ')';
190    }
191    void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
192      // When used as the base register, r0 reads constant zero rather than
193      // the value contained in the register.  For this reason, the darwin
194      // assembler requires that we print r0 as 0 (no r) when used as the base.
195      const MachineOperand &MO = MI->getOperand(OpNo);
196      if (MO.getReg() == PPC::R0)
197        O << '0';
198      else
199        O << TM.getRegisterInfo()->get(MO.getReg()).Name;
200      O << ", ";
201      printOperand(MI, OpNo+1);
202    }
203
204    virtual bool runOnMachineFunction(MachineFunction &F) = 0;
205    virtual bool doFinalization(Module &M) = 0;
206  };
207
208  /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
209  /// X
210  ///
211  struct DarwinAsmPrinter : public PPCAsmPrinter {
212
213    DarwinAsmPrinter(std::ostream &O, TargetMachine &TM)
214      : PPCAsmPrinter(O, TM) {
215      CommentString = ";";
216      GlobalPrefix = "_";
217      PrivateGlobalPrefix = "L";     // Marker for constant pool idxs
218      ZeroDirective = "\t.space\t";  // ".space N" emits N zeros.
219      Data64bitsDirective = 0;       // we can't emit a 64-bit unit
220      AlignmentIsInBytes = false;    // Alignment is by power of 2.
221      ConstantPoolSection = "\t.const\t";
222      LCOMMDirective = "\t.lcomm\t";
223      StaticCtorsSection = ".mod_init_func";
224      StaticDtorsSection = ".mod_term_func";
225    }
226
227    virtual const char *getPassName() const {
228      return "Darwin PPC Assembly Printer";
229    }
230
231    bool runOnMachineFunction(MachineFunction &F);
232    bool doInitialization(Module &M);
233    bool doFinalization(Module &M);
234  };
235
236  /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX
237  ///
238  struct AIXAsmPrinter : public PPCAsmPrinter {
239    /// Map for labels corresponding to global variables
240    ///
241    std::map<const GlobalVariable*,std::string> GVToLabelMap;
242
243    AIXAsmPrinter(std::ostream &O, TargetMachine &TM)
244      : PPCAsmPrinter(O, TM) {
245      CommentString = "#";
246      GlobalPrefix = ".";
247      ZeroDirective = "\t.space\t";  // ".space N" emits N zeros.
248      Data64bitsDirective = 0;       // we can't emit a 64-bit unit
249      AlignmentIsInBytes = false;    // Alignment is by power of 2.
250      ConstantPoolSection = "\t.const\t";
251    }
252
253    virtual const char *getPassName() const {
254      return "AIX PPC Assembly Printer";
255    }
256
257    bool runOnMachineFunction(MachineFunction &F);
258    bool doInitialization(Module &M);
259    bool doFinalization(Module &M);
260  };
261} // end of anonymous namespace
262
263/// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly
264/// code for a MachineFunction to the given output stream, in a format that the
265/// Darwin assembler can deal with.
266///
267FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o, TargetMachine &tm) {
268  return new DarwinAsmPrinter(o, tm);
269}
270
271/// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code
272/// for a MachineFunction to the given output stream, in a format that the
273/// AIX 5L assembler can deal with.
274///
275FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, TargetMachine &tm) {
276  return new AIXAsmPrinter(o, tm);
277}
278
279// Include the auto-generated portion of the assembly writer
280#include "PPCGenAsmWriter.inc"
281
282void PPCAsmPrinter::printOp(const MachineOperand &MO) {
283  const MRegisterInfo &RI = *TM.getRegisterInfo();
284  int new_symbol;
285
286  switch (MO.getType()) {
287  case MachineOperand::MO_VirtualRegister:
288    if (Value *V = MO.getVRegValueOrNull()) {
289      O << "<" << V->getName() << ">";
290      return;
291    }
292    // FALLTHROUGH
293  case MachineOperand::MO_MachineRegister:
294  case MachineOperand::MO_CCRegister:
295    O << RI.get(MO.getReg()).Name;
296    return;
297
298  case MachineOperand::MO_SignExtendedImmed:
299  case MachineOperand::MO_UnextendedImmed:
300    std::cerr << "printOp() does not handle immediate values\n";
301    abort();
302    return;
303
304  case MachineOperand::MO_PCRelativeDisp:
305    std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
306    abort();
307    return;
308
309  case MachineOperand::MO_MachineBasicBlock: {
310    MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
311    O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << "_"
312      << MBBOp->getNumber() << "\t; " << MBBOp->getBasicBlock()->getName();
313    return;
314  }
315
316  case MachineOperand::MO_ConstantPoolIndex:
317    O << PrivateGlobalPrefix << "CPI" << getFunctionNumber()
318      << '_' << MO.getConstantPoolIndex();
319    return;
320  case MachineOperand::MO_ExternalSymbol:
321    // Computing the address of an external symbol, not calling it.
322    if (!PPCGenerateStaticCode) {
323      std::string Name(GlobalPrefix); Name += MO.getSymbolName();
324      GVStubs.insert(Name);
325      O << "L" << Name << "$non_lazy_ptr";
326      return;
327    }
328    O << GlobalPrefix << MO.getSymbolName();
329    return;
330  case MachineOperand::MO_GlobalAddress: {
331    // Computing the address of a global symbol, not calling it.
332    GlobalValue *GV = MO.getGlobal();
333    std::string Name = Mang->getValueName(GV);
334
335    // External or weakly linked global variables need non-lazily-resolved stubs
336    if (!PPCGenerateStaticCode) {
337      if (((GV->isExternal() || GV->hasWeakLinkage() ||
338            GV->hasLinkOnceLinkage()))) {
339        GVStubs.insert(Name);
340        O << "L" << Name << "$non_lazy_ptr";
341        return;
342      }
343    }
344
345    O << Name;
346    return;
347  }
348
349  default:
350    O << "<unknown operand type: " << MO.getType() << ">";
351    return;
352  }
353}
354
355/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
356/// the current output stream.
357///
358void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
359  ++EmittedInsts;
360
361  // Check for slwi/srwi mnemonics.
362  if (MI->getOpcode() == PPC::RLWINM) {
363    bool FoundMnemonic = false;
364    unsigned char SH = MI->getOperand(2).getImmedValue();
365    unsigned char MB = MI->getOperand(3).getImmedValue();
366    unsigned char ME = MI->getOperand(4).getImmedValue();
367    if (SH <= 31 && MB == 0 && ME == (31-SH)) {
368      O << "slwi "; FoundMnemonic = true;
369    }
370    if (SH <= 31 && MB == (32-SH) && ME == 31) {
371      O << "srwi "; FoundMnemonic = true;
372      SH = 32-SH;
373    }
374    if (FoundMnemonic) {
375      printOperand(MI, 0);
376      O << ", ";
377      printOperand(MI, 1);
378      O << ", " << (unsigned int)SH << "\n";
379      return;
380    }
381  }
382
383  if (printInstruction(MI))
384    return; // Printer was automatically generated
385
386  assert(0 && "Unhandled instruction in asm writer!");
387  abort();
388  return;
389}
390
391
392/// runOnMachineFunction - This uses the printMachineInstruction()
393/// method to print assembly for each instruction.
394///
395bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
396  SetupMachineFunction(MF);
397  O << "\n\n";
398
399  // Print out dwarf file info
400  MachineDebugInfo &DebugInfo = MF.getDebugInfo();
401  std::vector<std::string> Sources = DebugInfo.getSourceFiles();
402  for (unsigned i = 0, N = Sources.size(); i < N; i++) {
403    O << "\t; .file\t" << (i + 1) << "," << "\"" << Sources[i]  << "\"" << "\n";
404  }
405
406  // Print out constants referenced by the function
407  EmitConstantPool(MF.getConstantPool());
408
409  // Print out labels for the function.
410  const Function *F = MF.getFunction();
411  switch (F->getLinkage()) {
412  default: assert(0 && "Unknown linkage type!");
413  case Function::InternalLinkage:  // Symbols default to internal.
414    SwitchSection(".text", F);
415    EmitAlignment(4, F);
416    break;
417  case Function::ExternalLinkage:
418    SwitchSection(".text", F);
419    EmitAlignment(4, F);
420    O << "\t.globl\t" << CurrentFnName << "\n";
421    break;
422  case Function::WeakLinkage:
423  case Function::LinkOnceLinkage:
424    SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
425                  F);
426    O << "\t.globl\t" << CurrentFnName << "\n";
427    O << "\t.weak_definition\t" << CurrentFnName << "\n";
428    break;
429  }
430  O << CurrentFnName << ":\n";
431
432  // Print out code for the function.
433  for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
434       I != E; ++I) {
435    // Print a label for the basic block.
436    if (I != MF.begin()) {
437      O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
438        << I->getNumber() << ":\t";
439      if (!I->getBasicBlock()->getName().empty())
440        O << CommentString << " " << I->getBasicBlock()->getName();
441      O << "\n";
442    }
443    for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
444         II != E; ++II) {
445      // Print the assembly for the instruction.
446      O << "\t";
447      printMachineInstruction(II);
448    }
449  }
450
451  // We didn't modify anything.
452  return false;
453}
454
455
456bool DarwinAsmPrinter::doInitialization(Module &M) {
457  if (TM.getSubtarget<PPCSubtarget>().isGigaProcessor())
458    O << "\t.machine ppc970\n";
459  AsmPrinter::doInitialization(M);
460
461  // Darwin wants symbols to be quoted if they have complex names.
462  Mang->setUseQuotes(true);
463  return false;
464}
465
466bool DarwinAsmPrinter::doFinalization(Module &M) {
467  const TargetData &TD = TM.getTargetData();
468
469  // Print out module-level global variables here.
470  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
471       I != E; ++I) {
472    if (!I->hasInitializer()) continue;   // External global require no code
473
474    // Check to see if this is a special global used by LLVM, if so, emit it.
475    if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I))
476      continue;
477
478    O << '\n';
479    std::string name = Mang->getValueName(I);
480    Constant *C = I->getInitializer();
481    unsigned Size = TD.getTypeSize(C->getType());
482    unsigned Align = TD.getTypeAlignmentShift(C->getType());
483
484    if (C->isNullValue() && /* FIXME: Verify correct */
485        (I->hasInternalLinkage() || I->hasWeakLinkage() ||
486         I->hasLinkOnceLinkage())) {
487      SwitchSection(".data", I);
488      if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
489      if (I->hasInternalLinkage())
490        O << LCOMMDirective << name << "," << Size << "," << Align;
491      else
492        O << ".comm " << name << "," << Size;
493      O << "\t\t; '" << I->getName() << "'\n";
494    } else {
495      switch (I->getLinkage()) {
496      case GlobalValue::LinkOnceLinkage:
497      case GlobalValue::WeakLinkage:
498        O << ".globl " << name << '\n'
499          << ".weak_definition " << name << '\n'
500          << ".private_extern " << name << '\n';
501        SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
502        break;
503      case GlobalValue::AppendingLinkage:
504        // FIXME: appending linkage variables should go into a section of
505        // their name or something.  For now, just emit them as external.
506      case GlobalValue::ExternalLinkage:
507        // If external or appending, declare as a global symbol
508        O << "\t.globl " << name << "\n";
509        // FALL THROUGH
510      case GlobalValue::InternalLinkage:
511        SwitchSection(".data", I);
512        break;
513      default:
514        std::cerr << "Unknown linkage type!";
515        abort();
516      }
517
518      EmitAlignment(Align, I);
519      O << name << ":\t\t\t\t; '" << I->getName() << "'\n";
520      EmitGlobalConstant(C);
521    }
522  }
523
524  // Output stubs for dynamically-linked functions
525  if (PICEnabled) {
526    for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
527         i != e; ++i) {
528      SwitchSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
529                    "pure_instructions,32", 0);
530      EmitAlignment(2);
531      O << "L" << *i << "$stub:\n";
532      O << "\t.indirect_symbol " << *i << "\n";
533      O << "\tmflr r0\n";
534      O << "\tbcl 20,31,L0$" << *i << "\n";
535      O << "L0$" << *i << ":\n";
536      O << "\tmflr r11\n";
537      O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
538      O << "\tmtlr r0\n";
539      O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
540      O << "\tmtctr r12\n";
541      O << "\tbctr\n";
542      SwitchSection(".lazy_symbol_pointer", 0);
543      O << "L" << *i << "$lazy_ptr:\n";
544      O << "\t.indirect_symbol " << *i << "\n";
545      O << "\t.long dyld_stub_binding_helper\n";
546    }
547  } else {
548    for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
549         i != e; ++i) {
550      SwitchSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
551                    "pure_instructions,16", 0);
552      EmitAlignment(4);
553      O << "L" << *i << "$stub:\n";
554      O << "\t.indirect_symbol " << *i << "\n";
555      O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
556      O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
557      O << "\tmtctr r12\n";
558      O << "\tbctr\n";
559      SwitchSection(".lazy_symbol_pointer", 0);
560      O << "L" << *i << "$lazy_ptr:\n";
561      O << "\t.indirect_symbol " << *i << "\n";
562      O << "\t.long dyld_stub_binding_helper\n";
563    }
564  }
565
566  O << "\n";
567
568  // Output stubs for external and common global variables.
569  if (GVStubs.begin() != GVStubs.end()) {
570    SwitchSection(".non_lazy_symbol_pointer", 0);
571    for (std::set<std::string>::iterator I = GVStubs.begin(),
572         E = GVStubs.end(); I != E; ++I) {
573      O << "L" << *I << "$non_lazy_ptr:\n";
574      O << "\t.indirect_symbol " << *I << "\n";
575      O << "\t.long\t0\n";
576    }
577  }
578
579  // Funny Darwin hack: This flag tells the linker that no global symbols
580  // contain code that falls through to other global symbols (e.g. the obvious
581  // implementation of multiple entry points).  If this doesn't occur, the
582  // linker can safely perform dead code stripping.  Since LLVM never generates
583  // code that does this, it is always safe to set.
584  O << "\t.subsections_via_symbols\n";
585
586  AsmPrinter::doFinalization(M);
587  return false; // success
588}
589
590/// runOnMachineFunction - This uses the printMachineInstruction()
591/// method to print assembly for each instruction.
592///
593bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
594  SetupMachineFunction(MF);
595
596  // Print out constants referenced by the function
597  EmitConstantPool(MF.getConstantPool());
598
599  // Print out header for the function.
600  O << "\t.csect .text[PR]\n"
601    << "\t.align 2\n"
602    << "\t.globl "  << CurrentFnName << '\n'
603    << "\t.globl ." << CurrentFnName << '\n'
604    << "\t.csect "  << CurrentFnName << "[DS],3\n"
605    << CurrentFnName << ":\n"
606    << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n"
607    << "\t.csect .text[PR]\n"
608    << '.' << CurrentFnName << ":\n";
609
610  // Print out code for the function.
611  for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
612       I != E; ++I) {
613    // Print a label for the basic block.
614    O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
615      << I->getNumber()
616      << ":\t" << CommentString << I->getBasicBlock()->getName() << '\n';
617    for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
618      II != E; ++II) {
619      // Print the assembly for the instruction.
620      O << "\t";
621      printMachineInstruction(II);
622    }
623  }
624
625  O << "LT.." << CurrentFnName << ":\n"
626    << "\t.long 0\n"
627    << "\t.byte 0,0,32,65,128,0,0,0\n"
628    << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n'
629    << "\t.short 3\n"
630    << "\t.byte \"" << CurrentFnName << "\"\n"
631    << "\t.align 2\n";
632
633  // We didn't modify anything.
634  return false;
635}
636
637bool AIXAsmPrinter::doInitialization(Module &M) {
638  SwitchSection("", 0);
639  const TargetData &TD = TM.getTargetData();
640
641  O << "\t.machine \"ppc64\"\n"
642    << "\t.toc\n"
643    << "\t.csect .text[PR]\n";
644
645  // Print out module-level global variables
646  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
647       I != E; ++I) {
648    if (!I->hasInitializer())
649      continue;
650
651    std::string Name = I->getName();
652    Constant *C = I->getInitializer();
653    // N.B.: We are defaulting to writable strings
654    if (I->hasExternalLinkage()) {
655      O << "\t.globl " << Name << '\n'
656        << "\t.csect .data[RW],3\n";
657    } else {
658      O << "\t.csect _global.rw_c[RW],3\n";
659    }
660    O << Name << ":\n";
661    EmitGlobalConstant(C);
662  }
663
664  // Output labels for globals
665  if (M.global_begin() != M.global_end()) O << "\t.toc\n";
666  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
667       I != E; ++I) {
668    const GlobalVariable *GV = I;
669    // Do not output labels for unused variables
670    if (GV->isExternal() && GV->use_begin() == GV->use_end())
671      continue;
672
673    IncrementFunctionNumber();
674    std::string Name = GV->getName();
675    std::string Label = "LC.." + utostr(getFunctionNumber());
676    GVToLabelMap[GV] = Label;
677    O << Label << ":\n"
678      << "\t.tc " << Name << "[TC]," << Name;
679    if (GV->isExternal()) O << "[RW]";
680    O << '\n';
681   }
682
683  AsmPrinter::doInitialization(M);
684  return false; // success
685}
686
687bool AIXAsmPrinter::doFinalization(Module &M) {
688  const TargetData &TD = TM.getTargetData();
689  // Print out module-level global variables
690  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
691       I != E; ++I) {
692    if (I->hasInitializer() || I->hasExternalLinkage())
693      continue;
694
695    std::string Name = I->getName();
696    if (I->hasInternalLinkage()) {
697      O << "\t.lcomm " << Name << ",16,_global.bss_c";
698    } else {
699      O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
700        << "," << Log2_32((unsigned)TD.getTypeAlignment(I->getType()));
701    }
702    O << "\t\t" << CommentString << " ";
703    WriteAsOperand(O, I, false, true, &M);
704    O << "\n";
705  }
706
707  O << "_section_.text:\n"
708    << "\t.csect .data[RW],3\n"
709    << "\t.llong _section_.text\n";
710  AsmPrinter::doFinalization(M);
711  return false; // success
712}
713