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