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