PPCAsmPrinter.cpp revision f5395cee6a24699a016b2e379cf4804b09ce5030
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
185    virtual bool runOnMachineFunction(MachineFunction &F) = 0;
186    virtual bool doFinalization(Module &M) = 0;
187  };
188
189  /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
190  /// X
191  ///
192  struct DarwinAsmPrinter : public PPCAsmPrinter {
193
194    DarwinAsmPrinter(std::ostream &O, TargetMachine &TM)
195      : PPCAsmPrinter(O, TM) {
196      CommentString = ";";
197      GlobalPrefix = "_";
198      PrivateGlobalPrefix = "L";     // Marker for constant pool idxs
199      ZeroDirective = "\t.space\t";  // ".space N" emits N zeros.
200      Data64bitsDirective = 0;       // we can't emit a 64-bit unit
201      AlignmentIsInBytes = false;    // Alignment is by power of 2.
202      ConstantPoolSection = "\t.const\t";
203      LCOMMDirective = "\t.lcomm\t";
204      StaticCtorsSection = ".mod_init_func";
205      StaticDtorsSection = ".mod_term_func";
206    }
207
208    virtual const char *getPassName() const {
209      return "Darwin PPC Assembly Printer";
210    }
211
212    bool runOnMachineFunction(MachineFunction &F);
213    bool doInitialization(Module &M);
214    bool doFinalization(Module &M);
215  };
216
217  /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX
218  ///
219  struct AIXAsmPrinter : public PPCAsmPrinter {
220    /// Map for labels corresponding to global variables
221    ///
222    std::map<const GlobalVariable*,std::string> GVToLabelMap;
223
224    AIXAsmPrinter(std::ostream &O, TargetMachine &TM)
225      : PPCAsmPrinter(O, TM) {
226      CommentString = "#";
227      GlobalPrefix = ".";
228      ZeroDirective = "\t.space\t";  // ".space N" emits N zeros.
229      Data64bitsDirective = 0;       // we can't emit a 64-bit unit
230      AlignmentIsInBytes = false;    // Alignment is by power of 2.
231      ConstantPoolSection = "\t.const\t";
232    }
233
234    virtual const char *getPassName() const {
235      return "AIX PPC Assembly Printer";
236    }
237
238    bool runOnMachineFunction(MachineFunction &F);
239    bool doInitialization(Module &M);
240    bool doFinalization(Module &M);
241  };
242} // end of anonymous namespace
243
244/// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly
245/// code for a MachineFunction to the given output stream, in a format that the
246/// Darwin assembler can deal with.
247///
248FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o, TargetMachine &tm) {
249  return new DarwinAsmPrinter(o, tm);
250}
251
252/// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code
253/// for a MachineFunction to the given output stream, in a format that the
254/// AIX 5L assembler can deal with.
255///
256FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, TargetMachine &tm) {
257  return new AIXAsmPrinter(o, tm);
258}
259
260// Include the auto-generated portion of the assembly writer
261#include "PPCGenAsmWriter.inc"
262
263void PPCAsmPrinter::printOp(const MachineOperand &MO) {
264  const MRegisterInfo &RI = *TM.getRegisterInfo();
265  int new_symbol;
266
267  switch (MO.getType()) {
268  case MachineOperand::MO_VirtualRegister:
269    if (Value *V = MO.getVRegValueOrNull()) {
270      O << "<" << V->getName() << ">";
271      return;
272    }
273    // FALLTHROUGH
274  case MachineOperand::MO_MachineRegister:
275  case MachineOperand::MO_CCRegister:
276    O << RI.get(MO.getReg()).Name;
277    return;
278
279  case MachineOperand::MO_SignExtendedImmed:
280  case MachineOperand::MO_UnextendedImmed:
281    std::cerr << "printOp() does not handle immediate values\n";
282    abort();
283    return;
284
285  case MachineOperand::MO_PCRelativeDisp:
286    std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
287    abort();
288    return;
289
290  case MachineOperand::MO_MachineBasicBlock: {
291    MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
292    O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << "_"
293      << MBBOp->getNumber() << "\t; " << MBBOp->getBasicBlock()->getName();
294    return;
295  }
296
297  case MachineOperand::MO_ConstantPoolIndex:
298    O << PrivateGlobalPrefix << "CPI" << getFunctionNumber()
299      << '_' << MO.getConstantPoolIndex();
300    return;
301  case MachineOperand::MO_ExternalSymbol:
302    // Computing the address of an external symbol, not calling it.
303    if (!PPCGenerateStaticCode) {
304      std::string Name(GlobalPrefix); Name += MO.getSymbolName();
305      GVStubs.insert(Name);
306      O << "L" << Name << "$non_lazy_ptr";
307      return;
308    }
309    O << GlobalPrefix << MO.getSymbolName();
310    return;
311  case MachineOperand::MO_GlobalAddress: {
312    // Computing the address of a global symbol, not calling it.
313    GlobalValue *GV = MO.getGlobal();
314    std::string Name = Mang->getValueName(GV);
315
316    // External or weakly linked global variables need non-lazily-resolved stubs
317    if (!PPCGenerateStaticCode) {
318      if (((GV->isExternal() || GV->hasWeakLinkage() ||
319            GV->hasLinkOnceLinkage()))) {
320        GVStubs.insert(Name);
321        O << "L" << Name << "$non_lazy_ptr";
322        return;
323      }
324    }
325
326    O << Name;
327    return;
328  }
329
330  default:
331    O << "<unknown operand type: " << MO.getType() << ">";
332    return;
333  }
334}
335
336/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
337/// the current output stream.
338///
339void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
340  ++EmittedInsts;
341
342  // Check for slwi/srwi mnemonics.
343  if (MI->getOpcode() == PPC::RLWINM) {
344    bool FoundMnemonic = false;
345    unsigned char SH = MI->getOperand(2).getImmedValue();
346    unsigned char MB = MI->getOperand(3).getImmedValue();
347    unsigned char ME = MI->getOperand(4).getImmedValue();
348    if (SH <= 31 && MB == 0 && ME == (31-SH)) {
349      O << "slwi "; FoundMnemonic = true;
350    }
351    if (SH <= 31 && MB == (32-SH) && ME == 31) {
352      O << "srwi "; FoundMnemonic = true;
353      SH = 32-SH;
354    }
355    if (FoundMnemonic) {
356      printOperand(MI, 0);
357      O << ", ";
358      printOperand(MI, 1);
359      O << ", " << (unsigned int)SH << "\n";
360      return;
361    }
362  }
363
364  if (printInstruction(MI))
365    return; // Printer was automatically generated
366
367  assert(0 && "Unhandled instruction in asm writer!");
368  abort();
369  return;
370}
371
372
373/// runOnMachineFunction - This uses the printMachineInstruction()
374/// method to print assembly for each instruction.
375///
376bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
377  SetupMachineFunction(MF);
378  O << "\n\n";
379
380  // Print out dwarf file info
381  MachineDebugInfo &DebugInfo = MF.getDebugInfo();
382  std::vector<std::string> Sources = DebugInfo.getSourceFiles();
383  for (unsigned i = 0, N = Sources.size(); i < N; i++) {
384    O << "\t; .file\t" << (i + 1) << "," << "\"" << Sources[i]  << "\"" << "\n";
385  }
386
387  // Print out constants referenced by the function
388  EmitConstantPool(MF.getConstantPool());
389
390  // Print out labels for the function.
391  const Function *F = MF.getFunction();
392  switch (F->getLinkage()) {
393  default: assert(0 && "Unknown linkage type!");
394  case Function::InternalLinkage:  // Symbols default to internal.
395    SwitchSection(".text", F);
396    EmitAlignment(4, F);
397    break;
398  case Function::ExternalLinkage:
399    SwitchSection(".text", F);
400    EmitAlignment(4, F);
401    O << "\t.globl\t" << CurrentFnName << "\n";
402    break;
403  case Function::WeakLinkage:
404  case Function::LinkOnceLinkage:
405    SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
406                  F);
407    O << "\t.globl\t" << CurrentFnName << "\n";
408    O << "\t.weak_definition\t" << CurrentFnName << "\n";
409    break;
410  }
411  O << CurrentFnName << ":\n";
412
413  // Print out code for the function.
414  for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
415       I != E; ++I) {
416    // Print a label for the basic block.
417    if (I != MF.begin()) {
418      O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
419        << I->getNumber() << ":\t";
420      if (!I->getBasicBlock()->getName().empty())
421        O << CommentString << " " << I->getBasicBlock()->getName();
422      O << "\n";
423    }
424    for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
425         II != E; ++II) {
426      // Print the assembly for the instruction.
427      O << "\t";
428      printMachineInstruction(II);
429    }
430  }
431
432  // We didn't modify anything.
433  return false;
434}
435
436
437bool DarwinAsmPrinter::doInitialization(Module &M) {
438  if (TM.getSubtarget<PPCSubtarget>().isGigaProcessor())
439    O << "\t.machine ppc970\n";
440  AsmPrinter::doInitialization(M);
441
442  // Darwin wants symbols to be quoted if they have complex names.
443  Mang->setUseQuotes(true);
444  return false;
445}
446
447bool DarwinAsmPrinter::doFinalization(Module &M) {
448  const TargetData &TD = TM.getTargetData();
449
450  // Print out module-level global variables here.
451  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
452       I != E; ++I) {
453    if (!I->hasInitializer()) continue;   // External global require no code
454
455    // Check to see if this is a special global used by LLVM, if so, emit it.
456    if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I))
457      continue;
458
459    O << '\n';
460    std::string name = Mang->getValueName(I);
461    Constant *C = I->getInitializer();
462    unsigned Size = TD.getTypeSize(C->getType());
463    unsigned Align = TD.getTypeAlignmentShift(C->getType());
464
465    if (C->isNullValue() && /* FIXME: Verify correct */
466        (I->hasInternalLinkage() || I->hasWeakLinkage() ||
467         I->hasLinkOnceLinkage())) {
468      SwitchSection(".data", I);
469      if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
470      if (I->hasInternalLinkage())
471        O << LCOMMDirective << name << "," << Size << "," << Align;
472      else
473        O << ".comm " << name << "," << Size;
474      O << "\t\t; '" << I->getName() << "'\n";
475    } else {
476      switch (I->getLinkage()) {
477      case GlobalValue::LinkOnceLinkage:
478      case GlobalValue::WeakLinkage:
479        O << ".globl " << name << '\n'
480          << ".weak_definition " << name << '\n'
481          << ".private_extern " << name << '\n';
482        SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
483        break;
484      case GlobalValue::AppendingLinkage:
485        // FIXME: appending linkage variables should go into a section of
486        // their name or something.  For now, just emit them as external.
487      case GlobalValue::ExternalLinkage:
488        // If external or appending, declare as a global symbol
489        O << "\t.globl " << name << "\n";
490        // FALL THROUGH
491      case GlobalValue::InternalLinkage:
492        SwitchSection(".data", I);
493        break;
494      default:
495        std::cerr << "Unknown linkage type!";
496        abort();
497      }
498
499      EmitAlignment(Align, I);
500      O << name << ":\t\t\t\t; '" << I->getName() << "'\n";
501      EmitGlobalConstant(C);
502    }
503  }
504
505  // Output stubs for dynamically-linked functions
506  if (PICEnabled) {
507    for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
508         i != e; ++i) {
509      SwitchSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
510                    "pure_instructions,32", 0);
511      EmitAlignment(2);
512      O << "L" << *i << "$stub:\n";
513      O << "\t.indirect_symbol " << *i << "\n";
514      O << "\tmflr r0\n";
515      O << "\tbcl 20,31,L0$" << *i << "\n";
516      O << "L0$" << *i << ":\n";
517      O << "\tmflr r11\n";
518      O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
519      O << "\tmtlr r0\n";
520      O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
521      O << "\tmtctr r12\n";
522      O << "\tbctr\n";
523      SwitchSection(".lazy_symbol_pointer", 0);
524      O << "L" << *i << "$lazy_ptr:\n";
525      O << "\t.indirect_symbol " << *i << "\n";
526      O << "\t.long dyld_stub_binding_helper\n";
527    }
528  } else {
529    for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
530         i != e; ++i) {
531      SwitchSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
532                    "pure_instructions,16", 0);
533      EmitAlignment(4);
534      O << "L" << *i << "$stub:\n";
535      O << "\t.indirect_symbol " << *i << "\n";
536      O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
537      O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
538      O << "\tmtctr r12\n";
539      O << "\tbctr\n";
540      SwitchSection(".lazy_symbol_pointer", 0);
541      O << "L" << *i << "$lazy_ptr:\n";
542      O << "\t.indirect_symbol " << *i << "\n";
543      O << "\t.long dyld_stub_binding_helper\n";
544    }
545  }
546
547  O << "\n";
548
549  // Output stubs for external and common global variables.
550  if (GVStubs.begin() != GVStubs.end()) {
551    SwitchSection(".non_lazy_symbol_pointer", 0);
552    for (std::set<std::string>::iterator I = GVStubs.begin(),
553         E = GVStubs.end(); I != E; ++I) {
554      O << "L" << *I << "$non_lazy_ptr:\n";
555      O << "\t.indirect_symbol " << *I << "\n";
556      O << "\t.long\t0\n";
557    }
558  }
559
560  // Funny Darwin hack: This flag tells the linker that no global symbols
561  // contain code that falls through to other global symbols (e.g. the obvious
562  // implementation of multiple entry points).  If this doesn't occur, the
563  // linker can safely perform dead code stripping.  Since LLVM never generates
564  // code that does this, it is always safe to set.
565  O << "\t.subsections_via_symbols\n";
566
567  AsmPrinter::doFinalization(M);
568  return false; // success
569}
570
571/// runOnMachineFunction - This uses the printMachineInstruction()
572/// method to print assembly for each instruction.
573///
574bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
575  SetupMachineFunction(MF);
576
577  // Print out constants referenced by the function
578  EmitConstantPool(MF.getConstantPool());
579
580  // Print out header for the function.
581  O << "\t.csect .text[PR]\n"
582    << "\t.align 2\n"
583    << "\t.globl "  << CurrentFnName << '\n'
584    << "\t.globl ." << CurrentFnName << '\n'
585    << "\t.csect "  << CurrentFnName << "[DS],3\n"
586    << CurrentFnName << ":\n"
587    << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n"
588    << "\t.csect .text[PR]\n"
589    << '.' << CurrentFnName << ":\n";
590
591  // Print out code for the function.
592  for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
593       I != E; ++I) {
594    // Print a label for the basic block.
595    O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
596      << I->getNumber()
597      << ":\t" << CommentString << I->getBasicBlock()->getName() << '\n';
598    for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
599      II != E; ++II) {
600      // Print the assembly for the instruction.
601      O << "\t";
602      printMachineInstruction(II);
603    }
604  }
605
606  O << "LT.." << CurrentFnName << ":\n"
607    << "\t.long 0\n"
608    << "\t.byte 0,0,32,65,128,0,0,0\n"
609    << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n'
610    << "\t.short 3\n"
611    << "\t.byte \"" << CurrentFnName << "\"\n"
612    << "\t.align 2\n";
613
614  // We didn't modify anything.
615  return false;
616}
617
618bool AIXAsmPrinter::doInitialization(Module &M) {
619  SwitchSection("", 0);
620  const TargetData &TD = TM.getTargetData();
621
622  O << "\t.machine \"ppc64\"\n"
623    << "\t.toc\n"
624    << "\t.csect .text[PR]\n";
625
626  // Print out module-level global variables
627  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
628       I != E; ++I) {
629    if (!I->hasInitializer())
630      continue;
631
632    std::string Name = I->getName();
633    Constant *C = I->getInitializer();
634    // N.B.: We are defaulting to writable strings
635    if (I->hasExternalLinkage()) {
636      O << "\t.globl " << Name << '\n'
637        << "\t.csect .data[RW],3\n";
638    } else {
639      O << "\t.csect _global.rw_c[RW],3\n";
640    }
641    O << Name << ":\n";
642    EmitGlobalConstant(C);
643  }
644
645  // Output labels for globals
646  if (M.global_begin() != M.global_end()) O << "\t.toc\n";
647  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
648       I != E; ++I) {
649    const GlobalVariable *GV = I;
650    // Do not output labels for unused variables
651    if (GV->isExternal() && GV->use_begin() == GV->use_end())
652      continue;
653
654    IncrementFunctionNumber();
655    std::string Name = GV->getName();
656    std::string Label = "LC.." + utostr(getFunctionNumber());
657    GVToLabelMap[GV] = Label;
658    O << Label << ":\n"
659      << "\t.tc " << Name << "[TC]," << Name;
660    if (GV->isExternal()) O << "[RW]";
661    O << '\n';
662   }
663
664  AsmPrinter::doInitialization(M);
665  return false; // success
666}
667
668bool AIXAsmPrinter::doFinalization(Module &M) {
669  const TargetData &TD = TM.getTargetData();
670  // Print out module-level global variables
671  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
672       I != E; ++I) {
673    if (I->hasInitializer() || I->hasExternalLinkage())
674      continue;
675
676    std::string Name = I->getName();
677    if (I->hasInternalLinkage()) {
678      O << "\t.lcomm " << Name << ",16,_global.bss_c";
679    } else {
680      O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
681        << "," << Log2_32((unsigned)TD.getTypeAlignment(I->getType()));
682    }
683    O << "\t\t" << CommentString << " ";
684    WriteAsOperand(O, I, false, true, &M);
685    O << "\n";
686  }
687
688  O << "_section_.text:\n"
689    << "\t.csect .data[RW],3\n"
690    << "\t.llong _section_.text\n";
691  AsmPrinter::doFinalization(M);
692  return false; // success
693}
694