Use generic constant pool emission code in 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/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/CodeGen/ValueTypes.h"
31 #include "llvm/Support/Mangler.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Target/MRegisterInfo.h"
36 #include "llvm/Target/TargetInstrInfo.h"
37 #include "llvm/ADT/Statistic.h"
38 #include "llvm/ADT/StringExtras.h"
39 #include <set>
40 using namespace llvm;
41
42 namespace {
43   Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
44
45   class PPCAsmPrinter : public AsmPrinter {
46 public:
47     std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;
48     
49     PPCAsmPrinter(std::ostream &O, TargetMachine &TM)
50       : AsmPrinter(O, TM) {}
51
52     virtual const char *getPassName() const {
53       return "PowerPC Assembly Printer";
54     }
55
56     PPCTargetMachine &getTM() {
57       return static_cast<PPCTargetMachine&>(TM);
58     }
59
60     unsigned enumRegToMachineReg(unsigned enumReg) {
61       switch (enumReg) {
62       default: assert(0 && "Unhandled register!"); break;
63       case PPC::CR0:  return  0;
64       case PPC::CR1:  return  1;
65       case PPC::CR2:  return  2;
66       case PPC::CR3:  return  3;
67       case PPC::CR4:  return  4;
68       case PPC::CR5:  return  5;
69       case PPC::CR6:  return  6;
70       case PPC::CR7:  return  7;
71       }
72       abort();
73     }
74
75     /// printInstruction - This method is automatically generated by tablegen
76     /// from the instruction set description.  This method returns true if the
77     /// machine instruction was sufficiently described to print it, otherwise it
78     /// returns false.
79     bool printInstruction(const MachineInstr *MI);
80
81     void printMachineInstruction(const MachineInstr *MI);
82     void printOp(const MachineOperand &MO);
83
84     void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
85       const MachineOperand &MO = MI->getOperand(OpNo);
86       if (MO.getType() == MachineOperand::MO_MachineRegister) {
87         assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
88         O << TM.getRegisterInfo()->get(MO.getReg()).Name;
89       } else if (MO.isImmediate()) {
90         O << MO.getImmedValue();
91       } else {
92         printOp(MO);
93       }
94     }
95
96     void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo,
97                             MVT::ValueType VT) {
98       unsigned char value = MI->getOperand(OpNo).getImmedValue();
99       assert(value <= 31 && "Invalid u5imm argument!");
100       O << (unsigned int)value;
101     }
102     void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo,
103                             MVT::ValueType VT) {
104       unsigned char value = MI->getOperand(OpNo).getImmedValue();
105       assert(value <= 63 && "Invalid u6imm argument!");
106       O << (unsigned int)value;
107     }
108     void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo,
109                             MVT::ValueType VT) {
110       O << (short)MI->getOperand(OpNo).getImmedValue();
111     }
112     void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
113                             MVT::ValueType VT) {
114       O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
115     }
116     void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo,
117                               MVT::ValueType VT) {
118       O << (short)MI->getOperand(OpNo).getImmedValue()*4;
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       }
129     }
130     void printCallOperand(const MachineInstr *MI, unsigned OpNo,
131                           MVT::ValueType VT) {
132       const MachineOperand &MO = MI->getOperand(OpNo);
133       if (!PPCGenerateStaticCode) {
134         if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
135           std::string Name(GlobalPrefix); Name += MO.getSymbolName();
136           FnStubs.insert(Name);
137           O << "L" << Name << "$stub";
138           return;
139         } else if (MO.getType() == MachineOperand::MO_GlobalAddress &&
140                    isa<Function>(MO.getGlobal()) && 
141                    cast<Function>(MO.getGlobal())->isExternal()) {
142           // Dynamically-resolved functions need a stub for the function.
143           std::string Name = Mang->getValueName(MO.getGlobal());
144           FnStubs.insert(Name);
145           O << "L" << Name << "$stub";
146           return;
147         }
148       }
149       
150       printOp(MI->getOperand(OpNo));
151     }
152     void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo,
153                              MVT::ValueType VT) {
154      O << (int)MI->getOperand(OpNo).getImmedValue()*4;
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" << getFunctionNumber() << "$pb\"\n";
160       O << "\"L0000" << getFunctionNumber() << "$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" << getFunctionNumber() << "$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" << getFunctionNumber() << "$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 bool runOnMachineFunction(MachineFunction &F) = 0;
196     virtual bool doFinalization(Module &M) = 0;
197   };
198
199   /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
200   /// X
201   ///
202   struct DarwinAsmPrinter : public PPCAsmPrinter {
203
204     DarwinAsmPrinter(std::ostream &O, TargetMachine &TM)
205       : PPCAsmPrinter(O, TM) {
206       CommentString = ";";
207       GlobalPrefix = "_";
208       PrivateGlobalPrefix = "L";     // Marker for constant pool idxs
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       ConstantPoolSection = "\t.const\t";
213     }
214
215     virtual const char *getPassName() const {
216       return "Darwin PPC Assembly Printer";
217     }
218
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       ConstantPoolSection = "\t.const\t";
239     }
240
241     virtual const char *getPassName() const {
242       return "AIX PPC Assembly Printer";
243     }
244
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) {
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 << PrivateGlobalPrefix << "BB" << getFunctionNumber() << "_"
300       << MBBOp->getNumber() << "\t; " << MBBOp->getBasicBlock()->getName();
301     return;
302   }
303
304   case MachineOperand::MO_ConstantPoolIndex:
305     O << PrivateGlobalPrefix << "CPI" << getFunctionNumber()
306       << '_' << MO.getConstantPoolIndex();
307     return;
308
309   case MachineOperand::MO_ExternalSymbol:
310     O << GlobalPrefix << MO.getSymbolName();
311     return;
312
313   case MachineOperand::MO_GlobalAddress: {
314     GlobalValue *GV = MO.getGlobal();
315     std::string Name = Mang->getValueName(GV);
316
317     // External or weakly linked global variables need non-lazily-resolved stubs
318     if (!PPCGenerateStaticCode &&
319         ((GV->isExternal() || GV->hasWeakLinkage() ||
320           GV->hasLinkOnceLinkage()))) {
321       if (GV->hasLinkOnceLinkage())
322         LinkOnceStubs.insert(Name);
323       else
324         GVStubs.insert(Name);
325       O << "L" << Name << "$non_lazy_ptr";
326       return;
327     }
328
329     O << Name;
330     return;
331   }
332
333   default:
334     O << "<unknown operand type: " << MO.getType() << ">";
335     return;
336   }
337 }
338
339 /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
340 /// the current output stream.
341 ///
342 void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
343   ++EmittedInsts;
344
345   // Check for slwi/srwi mnemonics.
346   if (MI->getOpcode() == PPC::RLWINM) {
347     bool FoundMnemonic = false;
348     unsigned char SH = MI->getOperand(2).getImmedValue();
349     unsigned char MB = MI->getOperand(3).getImmedValue();
350     unsigned char ME = MI->getOperand(4).getImmedValue();
351     if (SH <= 31 && MB == 0 && ME == (31-SH)) {
352       O << "slwi "; FoundMnemonic = true;
353     }
354     if (SH <= 31 && MB == (32-SH) && ME == 31) {
355       O << "srwi "; FoundMnemonic = true;
356       SH = 32-SH;
357     }
358     if (FoundMnemonic) {
359       printOperand(MI, 0, MVT::i64);
360       O << ", ";
361       printOperand(MI, 1, MVT::i64);
362       O << ", " << (unsigned int)SH << "\n";
363       return;
364     }
365   }
366
367   if (printInstruction(MI))
368     return; // Printer was automatically generated
369
370   assert(0 && "Unhandled instruction in asm writer!");
371   abort();
372   return;
373 }
374
375
376 /// runOnMachineFunction - This uses the printMachineInstruction()
377 /// method to print assembly for each instruction.
378 ///
379 bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
380   SetupMachineFunction(MF);
381   O << "\n\n";
382
383   // Print out constants referenced by the function
384   EmitConstantPool(MF.getConstantPool());
385
386   // Print out labels for the function.
387   const Function *F = MF.getFunction();
388   SwitchSection(".text", F);
389   EmitAlignment(4, F);
390   if (!F->hasInternalLinkage())
391     O << "\t.globl\t" << CurrentFnName << "\n";
392   O << CurrentFnName << ":\n";
393
394   // Print out code for the function.
395   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
396        I != E; ++I) {
397     // Print a label for the basic block.
398     if (I != MF.begin()) {
399       O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
400         << I->getNumber() << ":\t";
401       if (!I->getBasicBlock()->getName().empty())
402         O << CommentString << " " << I->getBasicBlock()->getName();
403       O << "\n";
404     }
405     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
406          II != E; ++II) {
407       // Print the assembly for the instruction.
408       O << "\t";
409       printMachineInstruction(II);
410     }
411   }
412
413   // We didn't modify anything.
414   return false;
415 }
416
417
418 bool DarwinAsmPrinter::doInitialization(Module &M) {
419   if (TM.getSubtarget<PPCSubtarget>().isGigaProcessor())
420     O << "\t.machine ppc970\n";
421   AsmPrinter::doInitialization(M);
422   
423   // Darwin wants symbols to be quoted if they have complex names.
424   Mang->setUseQuotes(true);
425   return false;
426 }
427
428 bool DarwinAsmPrinter::doFinalization(Module &M) {
429   const TargetData &TD = TM.getTargetData();
430
431   // Print out module-level global variables here.
432   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
433        I != E; ++I)
434     if (I->hasInitializer()) {   // External global require no code
435       O << '\n';
436       std::string name = Mang->getValueName(I);
437       Constant *C = I->getInitializer();
438       unsigned Size = TD.getTypeSize(C->getType());
439       unsigned Align = TD.getTypeAlignmentShift(C->getType());
440
441       if (C->isNullValue() && /* FIXME: Verify correct */
442           (I->hasInternalLinkage() || I->hasWeakLinkage() ||
443            I->hasLinkOnceLinkage())) {
444         SwitchSection(".data", I);
445         if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
446         if (I->hasInternalLinkage())
447           O << ".lcomm " << name << "," << Size << "," << Align;
448         else
449           O << ".comm " << name << "," << Size;
450         O << "\t\t; '" << I->getName() << "'\n";
451       } else {
452         switch (I->getLinkage()) {
453         case GlobalValue::LinkOnceLinkage:
454           SwitchSection("", 0);
455           O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
456             << ".weak_definition " << name << '\n'
457             << ".private_extern " << name << '\n'
458             << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
459           LinkOnceStubs.insert(name);
460           break;
461         case GlobalValue::WeakLinkage:
462           O << ".weak_definition " << name << '\n'
463             << ".private_extern " << name << '\n';
464           break;
465         case GlobalValue::AppendingLinkage:
466           // FIXME: appending linkage variables should go into a section of
467           // their name or something.  For now, just emit them as external.
468         case GlobalValue::ExternalLinkage:
469           // If external or appending, declare as a global symbol
470           O << "\t.globl " << name << "\n";
471           // FALL THROUGH
472         case GlobalValue::InternalLinkage:
473           SwitchSection(".data", I);
474           break;
475         default:
476           std::cerr << "Unknown linkage type!";
477           abort();
478         }
479
480         EmitAlignment(Align, I);
481         O << name << ":\t\t\t\t; '" << I->getName() << "'\n";
482         EmitGlobalConstant(C);
483       }
484     }
485
486   // Output stubs for dynamically-linked functions
487   for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
488        i != e; ++i)
489   {
490     if (PICEnabled) {
491     O << ".data\n";
492     O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
493     EmitAlignment(2);
494     O << "L" << *i << "$stub:\n";
495     O << "\t.indirect_symbol " << *i << "\n";
496     O << "\tmflr r0\n";
497     O << "\tbcl 20,31,L0$" << *i << "\n";
498     O << "L0$" << *i << ":\n";
499     O << "\tmflr r11\n";
500     O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
501     O << "\tmtlr r0\n";
502     O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
503     O << "\tmtctr r12\n";
504     O << "\tbctr\n";
505     O << ".data\n";
506     O << ".lazy_symbol_pointer\n";
507     O << "L" << *i << "$lazy_ptr:\n";
508     O << "\t.indirect_symbol " << *i << "\n";
509     O << "\t.long dyld_stub_binding_helper\n";
510     } else {
511     O << "\t.section __TEXT,__symbol_stub1,symbol_stubs,pure_instructions,16\n";
512     EmitAlignment(4);
513     O << "L" << *i << "$stub:\n";
514     O << "\t.indirect_symbol " << *i << "\n";
515     O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
516     O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
517     O << "\tmtctr r12\n";
518     O << "\tbctr\n";
519     O << "\t.lazy_symbol_pointer\n";
520     O << "L" << *i << "$lazy_ptr:\n";
521     O << "\t.indirect_symbol " << *i << "\n";
522     O << "\t.long dyld_stub_binding_helper\n";
523     }
524   }
525
526   O << "\n";
527
528   // Output stubs for external global variables
529   if (GVStubs.begin() != GVStubs.end())
530     O << ".data\n.non_lazy_symbol_pointer\n";
531   for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
532        i != e; ++i) {
533     O << "L" << *i << "$non_lazy_ptr:\n";
534     O << "\t.indirect_symbol " << *i << "\n";
535     O << "\t.long\t0\n";
536   }
537
538   // Output stubs for link-once variables
539   if (LinkOnceStubs.begin() != LinkOnceStubs.end())
540     O << ".data\n.align 2\n";
541   for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
542          e = LinkOnceStubs.end(); i != e; ++i) {
543     O << "L" << *i << "$non_lazy_ptr:\n"
544       << "\t.long\t" << *i << '\n';
545   }
546
547   // Funny Darwin hack: This flag tells the linker that no global symbols
548   // contain code that falls through to other global symbols (e.g. the obvious
549   // implementation of multiple entry points).  If this doesn't occur, the
550   // linker can safely perform dead code stripping.  Since LLVM never generates
551   // code that does this, it is always safe to set.
552   O << "\t.subsections_via_symbols\n";
553
554   AsmPrinter::doFinalization(M);
555   return false; // success
556 }
557
558 /// runOnMachineFunction - This uses the printMachineInstruction()
559 /// method to print assembly for each instruction.
560 ///
561 bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
562   SetupMachineFunction(MF);
563
564   // Print out constants referenced by the function
565   EmitConstantPool(MF.getConstantPool());
566
567   // Print out header for the function.
568   O << "\t.csect .text[PR]\n"
569     << "\t.align 2\n"
570     << "\t.globl "  << CurrentFnName << '\n'
571     << "\t.globl ." << CurrentFnName << '\n'
572     << "\t.csect "  << CurrentFnName << "[DS],3\n"
573     << CurrentFnName << ":\n"
574     << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n"
575     << "\t.csect .text[PR]\n"
576     << '.' << CurrentFnName << ":\n";
577
578   // Print out code for the function.
579   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
580        I != E; ++I) {
581     // Print a label for the basic block.
582     O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
583       << I->getNumber()
584       << ":\t" << CommentString << I->getBasicBlock()->getName() << '\n';
585     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
586       II != E; ++II) {
587       // Print the assembly for the instruction.
588       O << "\t";
589       printMachineInstruction(II);
590     }
591   }
592
593   O << "LT.." << CurrentFnName << ":\n"
594     << "\t.long 0\n"
595     << "\t.byte 0,0,32,65,128,0,0,0\n"
596     << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n'
597     << "\t.short 3\n"
598     << "\t.byte \"" << CurrentFnName << "\"\n"
599     << "\t.align 2\n";
600
601   // We didn't modify anything.
602   return false;
603 }
604
605 bool AIXAsmPrinter::doInitialization(Module &M) {
606   SwitchSection("", 0);
607   const TargetData &TD = TM.getTargetData();
608
609   O << "\t.machine \"ppc64\"\n"
610     << "\t.toc\n"
611     << "\t.csect .text[PR]\n";
612
613   // Print out module-level global variables
614   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
615        I != E; ++I) {
616     if (!I->hasInitializer())
617       continue;
618
619     std::string Name = I->getName();
620     Constant *C = I->getInitializer();
621     // N.B.: We are defaulting to writable strings
622     if (I->hasExternalLinkage()) {
623       O << "\t.globl " << Name << '\n'
624         << "\t.csect .data[RW],3\n";
625     } else {
626       O << "\t.csect _global.rw_c[RW],3\n";
627     }
628     O << Name << ":\n";
629     EmitGlobalConstant(C);
630   }
631
632   // Output labels for globals
633   if (M.global_begin() != M.global_end()) O << "\t.toc\n";
634   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
635        I != E; ++I) {
636     const GlobalVariable *GV = I;
637     // Do not output labels for unused variables
638     if (GV->isExternal() && GV->use_begin() == GV->use_end())
639       continue;
640
641     IncrementFunctionNumber();
642     std::string Name = GV->getName();
643     std::string Label = "LC.." + utostr(getFunctionNumber());
644     GVToLabelMap[GV] = Label;
645     O << Label << ":\n"
646       << "\t.tc " << Name << "[TC]," << Name;
647     if (GV->isExternal()) O << "[RW]";
648     O << '\n';
649    }
650
651   AsmPrinter::doInitialization(M);
652   return false; // success
653 }
654
655 bool AIXAsmPrinter::doFinalization(Module &M) {
656   const TargetData &TD = TM.getTargetData();
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() || I->hasExternalLinkage())
661       continue;
662
663     std::string Name = I->getName();
664     if (I->hasInternalLinkage()) {
665       O << "\t.lcomm " << Name << ",16,_global.bss_c";
666     } else {
667       O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
668         << "," << Log2_32((unsigned)TD.getTypeAlignment(I->getType()));
669     }
670     O << "\t\t" << CommentString << " ";
671     WriteAsOperand(O, I, false, true, &M);
672     O << "\n";
673   }
674
675   O << "_section_.text:\n"
676     << "\t.csect .data[RW],3\n"
677     << "\t.llong _section_.text\n";
678   AsmPrinter::doFinalization(M);
679   return false; // success
680 }