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