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