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