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