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