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