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