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