1bc192cf37bc3ce9ba8b322e255b46c2533484ae
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9AsmPrinter.cpp
1 //===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==//
2 //
3 // This file implements all of the stuff neccesary to output a .s file from
4 // LLVM.  The code in this file assumes that the specified module has already
5 // been compiled into the internal data structures of the Module.
6 //
7 // This code largely consists of two LLVM Pass's: a MethodPass and a Pass.  The
8 // MethodPass is pipelined together with all of the rest of the code generation
9 // stages, and the Pass runs at the end to emit code for global variables and
10 // such.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "SparcInternals.h"
15 #include "llvm/Analysis/SlotCalculator.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/CodeGen/MachineCodeForMethod.h"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/ConstantVals.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/BasicBlock.h"
22 #include "llvm/Method.h"
23 #include "llvm/Module.h"
24 #include "Support/StringExtras.h"
25 #include "Support/HashExtras.h"
26 #include <ostream>
27 using std::string;
28
29 namespace {
30
31 //===----------------------------------------------------------------------===//
32 //   Code Shared By the two printer passes, as a mixin
33 //===----------------------------------------------------------------------===//
34
35 class AsmPrinter {
36   typedef std::hash_map<const Value*, int> ValIdMap;
37   typedef ValIdMap::      iterator ValIdMapIterator;
38   typedef ValIdMap::const_iterator ValIdMapConstIterator;
39   
40   SlotCalculator *Table;   // map anonymous values to unique integer IDs
41   ValIdMap valToIdMap;     // used for values not handled by SlotCalculator 
42 public:
43   std::ostream &toAsm;
44   const TargetMachine &Target;
45
46   enum Sections {
47     Unknown,
48     Text,
49     ReadOnlyData,
50     InitRWData,
51     UninitRWData,
52   } CurSection;
53
54   AsmPrinter(std::ostream &os, const TargetMachine &T)
55     : Table(0), toAsm(os), Target(T), CurSection(Unknown) {}
56
57
58   // (start|end)(Module|Method) - Callback methods to be invoked by subclasses
59   void startModule(Module *M) {
60     Table = new SlotCalculator(M, true);
61   }
62   void startMethod(Method *M) {
63     // Make sure the slot table has information about this method...
64     Table->incorporateMethod(M);
65   }
66   void endMethod(Method *M) {
67     Table->purgeMethod();  // Forget all about M.
68   }
69   void endModule() {
70     delete Table; Table = 0;
71     valToIdMap.clear();
72   }
73
74   
75   // enterSection - Use this method to enter a different section of the output
76   // executable.  This is used to only output neccesary section transitions.
77   //
78   void enterSection(enum Sections S) {
79     if (S == CurSection) return;        // Only switch section if neccesary
80     CurSection = S;
81
82     toAsm << "\n\t.section ";
83     switch (S)
84       {
85       default: assert(0 && "Bad section name!");
86       case Text:         toAsm << "\".text\""; break;
87       case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
88       case InitRWData:   toAsm << "\".data\",#alloc,#write"; break;
89       case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break;
90       }
91     toAsm << "\n";
92   }
93
94   static std::string getValidSymbolName(const string &S) {
95     string Result;
96     
97     // Symbol names in Sparc assembly language have these rules:
98     // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
99     // (b) A name beginning in "." is treated as a local name.
100     // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
101     // 
102     if (S[0] == '_' || isdigit(S[0]))
103       Result += "ll";
104     
105     for (unsigned i = 0; i < S.size(); ++i)
106       {
107         char C = S[i];
108         if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
109           Result += C;
110         else
111           {
112             Result += '_';
113             Result += char('0' + ((unsigned char)C >> 4));
114             Result += char('0' + (C & 0xF));
115           }
116       }
117     return Result;
118   }
119
120   // getID - Return a valid identifier for the specified value.  Base it on
121   // the name of the identifier if possible, use a numbered value based on
122   // prefix otherwise.  FPrefix is always prepended to the output identifier.
123   //
124   string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
125     string Result;
126     string FP(FPrefix ? FPrefix : "");  // "Forced prefix"
127     if (V->hasName()) {
128       Result = FP + V->getName();
129     } else {
130       int valId = Table->getValSlot(V);
131       if (valId == -1) {
132         ValIdMapConstIterator I = valToIdMap.find(V);
133         if (I == valToIdMap.end())
134           valId = valToIdMap[V] = valToIdMap.size();
135         else
136           valId = I->second;
137       }
138       Result = FP + string(Prefix) + itostr(valId);
139     }
140     return getValidSymbolName(Result);
141   }
142   
143   // getID Wrappers - Ensure consistent usage...
144   string getID(const Module *M) {
145     return getID(M, "LLVMModule_");
146   }
147   string getID(const Method *M) {
148     return getID(M, "LLVMMethod_");
149   }
150   string getID(const BasicBlock *BB) {
151     return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
152   }
153   string getID(const GlobalVariable *GV) {
154     return getID(GV, "LLVMGlobal_", ".G_");
155   }
156   string getID(const Constant *CV) {
157     return getID(CV, "LLVMConst_", ".C_");
158   }
159 };
160
161
162
163 //===----------------------------------------------------------------------===//
164 //   SparcMethodAsmPrinter Code
165 //===----------------------------------------------------------------------===//
166
167 struct SparcMethodAsmPrinter : public MethodPass, public AsmPrinter {
168   inline SparcMethodAsmPrinter(std::ostream &os, const TargetMachine &t)
169     : AsmPrinter(os, t) {}
170
171   virtual bool doInitialization(Module *M) {
172     startModule(M);
173     return false;
174   }
175
176   virtual bool runOnMethod(Method *M) {
177     startMethod(M);
178     emitMethod(M);
179     endMethod(M);
180     return false;
181   }
182
183   virtual bool doFinalization(Module *M) {
184     endModule();
185     return false;
186   }
187
188   void emitMethod(const Method *M);
189 private :
190   void emitBasicBlock(const BasicBlock *BB);
191   void emitMachineInst(const MachineInstr *MI);
192   
193   unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
194   void printOneOperand(const MachineOperand &Op);
195
196   bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
197   bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
198   
199   unsigned getOperandMask(unsigned Opcode) {
200     switch (Opcode) {
201     case SUBcc:   return 1 << 3;  // Remove CC argument
202     case BA:      return 1 << 0;  // Remove Arg #0, which is always null or xcc
203     default:      return 0;       // By default, don't hack operands...
204     }
205   }
206 };
207
208 inline bool
209 SparcMethodAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
210                                        unsigned int opNum) {
211   switch (MI->getOpCode()) {
212   case JMPLCALL:
213   case JMPLRET: return (opNum == 0);
214   default:      return false;
215   }
216 }
217
218
219 inline bool
220 SparcMethodAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
221                                        unsigned int opNum) {
222   if (Target.getInstrInfo().isLoad(MI->getOpCode()))
223     return (opNum == 0);
224   else if (Target.getInstrInfo().isStore(MI->getOpCode()))
225     return (opNum == 1);
226   else
227     return false;
228 }
229
230
231 #define PrintOp1PlusOp2(Op1, Op2) \
232   printOneOperand(Op1); \
233   toAsm << "+"; \
234   printOneOperand(Op2);
235
236 unsigned int
237 SparcMethodAsmPrinter::printOperands(const MachineInstr *MI,
238                                unsigned int opNum)
239 {
240   const MachineOperand& Op = MI->getOperand(opNum);
241   
242   if (OpIsBranchTargetLabel(MI, opNum))
243     {
244       PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
245       return 2;
246     }
247   else if (OpIsMemoryAddressBase(MI, opNum))
248     {
249       toAsm << "[";
250       PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
251       toAsm << "]";
252       return 2;
253     }
254   else
255     {
256       printOneOperand(Op);
257       return 1;
258     }
259 }
260
261
262 void
263 SparcMethodAsmPrinter::printOneOperand(const MachineOperand &op)
264 {
265   switch (op.getOperandType())
266     {
267     case MachineOperand::MO_VirtualRegister:
268     case MachineOperand::MO_CCRegister:
269     case MachineOperand::MO_MachineRegister:
270       {
271         int RegNum = (int)op.getAllocatedRegNum();
272         
273         // ****this code is temporary till NULL Values are fixed
274         if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
275           toAsm << "<NULL VALUE>";
276         } else {
277           toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
278         }
279         break;
280       }
281     
282     case MachineOperand::MO_PCRelativeDisp:
283       {
284         const Value *Val = op.getVRegValue();
285         if (!Val)
286           toAsm << "\t<*NULL Value*>";
287         else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
288           toAsm << getID(BB);
289         else if (const Method *M = dyn_cast<const Method>(Val))
290           toAsm << getID(M);
291         else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val))
292           toAsm << getID(GV);
293         else if (const Constant *CV = dyn_cast<const Constant>(Val))
294           toAsm << getID(CV);
295         else
296           toAsm << "<unknown value=" << Val << ">";
297         break;
298       }
299     
300     case MachineOperand::MO_SignExtendedImmed:
301     case MachineOperand::MO_UnextendedImmed:
302       toAsm << (long)op.getImmedValue();
303       break;
304     
305     default:
306       toAsm << op;      // use dump field
307       break;
308     }
309 }
310
311
312 void
313 SparcMethodAsmPrinter::emitMachineInst(const MachineInstr *MI)
314 {
315   unsigned Opcode = MI->getOpCode();
316
317   if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
318     return;  // IGNORE PHI NODES
319
320   toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
321
322   unsigned Mask = getOperandMask(Opcode);
323   
324   bool NeedComma = false;
325   unsigned N = 1;
326   for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
327     if (! ((1 << OpNum) & Mask)) {        // Ignore this operand?
328       if (NeedComma) toAsm << ", ";         // Handle comma outputing
329       NeedComma = true;
330       N = printOperands(MI, OpNum);
331     }
332   else
333     N = 1;
334   
335   toAsm << "\n";
336 }
337
338 void
339 SparcMethodAsmPrinter::emitBasicBlock(const BasicBlock *BB)
340 {
341   // Emit a label for the basic block
342   toAsm << getID(BB) << ":\n";
343
344   // Get the vector of machine instructions corresponding to this bb.
345   const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
346   MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
347
348   // Loop over all of the instructions in the basic block...
349   for (; MII != MIE; ++MII)
350     emitMachineInst(*MII);
351   toAsm << "\n";  // Seperate BB's with newlines
352 }
353
354 void
355 SparcMethodAsmPrinter::emitMethod(const Method *M)
356 {
357   string methName = getID(M);
358   toAsm << "!****** Outputing Method: " << methName << " ******\n";
359   enterSection(AsmPrinter::Text);
360   toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
361   //toAsm << "\t.type\t" << methName << ",#function\n";
362   toAsm << "\t.type\t" << methName << ", 2\n";
363   toAsm << methName << ":\n";
364
365   // Output code for all of the basic blocks in the method...
366   for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
367     emitBasicBlock(*I);
368
369   // Output a .size directive so the debugger knows the extents of the function
370   toAsm << ".EndOf_" << methName << ":\n\t.size "
371            << methName << ", .EndOf_"
372            << methName << "-" << methName << "\n";
373
374   // Put some spaces between the methods
375   toAsm << "\n\n";
376 }
377
378 }  // End anonymous namespace
379
380 Pass *UltraSparc::getMethodAsmPrinterPass(PassManager &PM, std::ostream &Out) {
381   return new SparcMethodAsmPrinter(Out, *this);
382 }
383
384
385
386
387
388 //===----------------------------------------------------------------------===//
389 //   SparcMethodAsmPrinter Code
390 //===----------------------------------------------------------------------===//
391
392 namespace {
393
394 class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
395 public:
396   SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
397     : AsmPrinter(os, t) {}
398
399   virtual bool run(Module *M) {
400     startModule(M);
401     emitGlobalsAndConstants(M);
402     endModule();
403     return false;
404   }
405
406   void emitGlobalsAndConstants(const Module *M);
407
408   void printGlobalVariable(const GlobalVariable *GV);
409   void printSingleConstant(   const Constant* CV);
410   void printConstantValueOnly(const Constant* CV);
411   void printConstant(         const Constant* CV, std::string valID = "");
412
413   static void FoldConstants(const Module *M,
414                             std::hash_set<const Constant*> &moduleConstants);
415
416 };
417
418
419 // Can we treat the specified array as a string?  Only if it is an array of
420 // ubytes or non-negative sbytes.
421 //
422 static bool isStringCompatible(ConstantArray *CPA) {
423   const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
424   if (ETy == Type::UByteTy) return true;
425   if (ETy != Type::SByteTy) return false;
426
427   for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
428     if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
429       return false;
430
431   return true;
432 }
433
434 // toOctal - Convert the low order bits of X into an octal letter
435 static inline char toOctal(int X) {
436   return (X&7)+'0';
437 }
438
439 // getAsCString - Return the specified array as a C compatible string, only if
440 // the predicate isStringCompatible is true.
441 //
442 static string getAsCString(ConstantArray *CPA) {
443   if (isStringCompatible(CPA)) {
444     string Result;
445     const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
446     Result = "\"";
447     for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
448       unsigned char C = (ETy == Type::SByteTy) ?
449         (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
450         (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
451
452       if (isprint(C)) {
453         Result += C;
454       } else {
455         switch(C) {
456         case '\a': Result += "\\a"; break;
457         case '\b': Result += "\\b"; break;
458         case '\f': Result += "\\f"; break;
459         case '\n': Result += "\\n"; break;
460         case '\r': Result += "\\r"; break;
461         case '\t': Result += "\\t"; break;
462         case '\v': Result += "\\v"; break;
463         default:
464           Result += '\\';
465           Result += toOctal(C >> 6);
466           Result += toOctal(C >> 3);
467           Result += toOctal(C >> 0);
468           break;
469         }
470       }
471     }
472     Result += "\"";
473
474     return Result;
475   } else {
476     return CPA->getStrValue();
477   }
478 }
479
480 inline bool
481 ArrayTypeIsString(ArrayType* arrayType)
482 {
483   return (arrayType->getElementType() == Type::UByteTy ||
484           arrayType->getElementType() == Type::SByteTy);
485 }
486
487 inline const string
488 TypeToDataDirective(const Type* type)
489 {
490   switch(type->getPrimitiveID())
491     {
492     case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
493       return ".byte";
494     case Type::UShortTyID: case Type::ShortTyID:
495       return ".half";
496     case Type::UIntTyID: case Type::IntTyID:
497       return ".word";
498     case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
499       return ".xword";
500     case Type::FloatTyID:
501       return ".single";
502     case Type::DoubleTyID:
503       return ".double";
504     case Type::ArrayTyID:
505       if (ArrayTypeIsString((ArrayType*) type))
506         return ".ascii";
507       else
508         return "<InvaliDataTypeForPrinting>";
509     default:
510       return "<InvaliDataTypeForPrinting>";
511     }
512 }
513
514 // Get the size of the constant for the given target.
515 // If this is an unsized array, return 0.
516 // 
517 inline unsigned int
518 ConstantToSize(const Constant* CV, const TargetMachine& target)
519 {
520   if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
521     {
522       ArrayType *aty = cast<ArrayType>(CPA->getType());
523       if (ArrayTypeIsString(aty))
524         return 1 + CPA->getNumOperands();
525     }
526   
527   return target.findOptimalStorageSize(CV->getType());
528 }
529
530
531
532 // Align data larger than one L1 cache line on L1 cache line boundaries.
533 // Align all smaller data on the next higher 2^x boundary (4, 8, ...).
534 // 
535 inline unsigned int
536 SizeToAlignment(unsigned int size, const TargetMachine& target)
537 {
538   unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1); 
539   if (size > (unsigned) cacheLineSize / 2)
540     return cacheLineSize;
541   else
542     for (unsigned sz=1; /*no condition*/; sz *= 2)
543       if (sz >= size)
544         return sz;
545 }
546
547 // Get the size of the type and then use SizeToAlignment.
548 // 
549 inline unsigned int
550 TypeToAlignment(const Type* type, const TargetMachine& target)
551 {
552   return SizeToAlignment(target.findOptimalStorageSize(type), target);
553 }
554
555 // Get the size of the constant and then use SizeToAlignment.
556 // Handles strings as a special case;
557 inline unsigned int
558 ConstantToAlignment(const Constant* CV, const TargetMachine& target)
559 {
560   if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
561     if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
562       return SizeToAlignment(1 + CPA->getNumOperands(), target);
563   
564   return TypeToAlignment(CV->getType(), target);
565 }
566
567
568 // Print a single constant value.
569 void
570 SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
571 {
572   assert(CV->getType() != Type::VoidTy &&
573          CV->getType() != Type::TypeTy &&
574          CV->getType() != Type::LabelTy &&
575          "Unexpected type for Constant");
576   
577   assert((! isa<ConstantArray>( CV) && ! isa<ConstantStruct>(CV))
578          && "Collective types should be handled outside this function");
579   
580   toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
581   
582   if (CV->getType()->isPrimitiveType())
583     {
584       if (CV->getType() == Type::FloatTy || CV->getType() == Type::DoubleTy)
585         toAsm << "0r";                  // FP constants must have this prefix
586       toAsm << CV->getStrValue() << "\n";
587     }
588   else if (ConstantPointer* CPP = dyn_cast<ConstantPointer>(CV))
589     {
590       assert(CPP->isNullValue() &&
591              "Cannot yet print non-null pointer constants to assembly");
592       toAsm << "0\n";
593     }
594   else if (isa<ConstantPointerRef>(CV))
595     {
596       assert(0 && "Cannot yet initialize pointer refs in assembly");
597     }
598   else
599     {
600       assert(0 && "Unknown elementary type for constant");
601     }
602 }
603
604 // Print a constant value or values (it may be an aggregate).
605 // Uses printSingleConstant() to print each individual value.
606 void
607 SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
608 {
609   ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
610   
611   if (CPA && isStringCompatible(CPA))
612     { // print the string alone and return
613       toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
614     }
615   else if (CPA)
616     { // Not a string.  Print the values in successive locations
617       const std::vector<Use> &constValues = CPA->getValues();
618       for (unsigned i=1; i < constValues.size(); i++)
619         this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
620     }
621   else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
622     { // Print the fields in successive locations
623       const std::vector<Use>& constValues = CPS->getValues();
624       for (unsigned i=1; i < constValues.size(); i++)
625         this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
626     }
627   else
628     this->printSingleConstant(CV);
629 }
630
631 // Print a constant (which may be an aggregate) prefixed by all the
632 // appropriate directives.  Uses printConstantValueOnly() to print the
633 // value or values.
634 void
635 SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
636 {
637   if (valID.length() == 0)
638     valID = getID(CV);
639   
640   toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
641   
642   // Print .size and .type only if it is not a string.
643   ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
644   if (CPA && isStringCompatible(CPA))
645     { // print it as a string and return
646       toAsm << valID << ":\n";
647       toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
648       return;
649     }
650   
651   toAsm << "\t.type" << "\t" << valID << ",#object\n";
652
653   unsigned int constSize = ConstantToSize(CV, Target);
654   if (constSize)
655     toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
656   
657   toAsm << valID << ":\n";
658   
659   printConstantValueOnly(CV);
660 }
661
662
663 void SparcModuleAsmPrinter::FoldConstants(const Module *M,
664                                           std::hash_set<const Constant*> &MC) {
665   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
666     if (!(*I)->isExternal()) {
667       const std::hash_set<const Constant*> &pool =
668         MachineCodeForMethod::get(*I).getConstantPoolValues();
669       MC.insert(pool.begin(), pool.end());
670     }
671 }
672
673 void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
674 {
675   toAsm << "\t.global\t" << getID(GV) << "\n";
676   
677   if (GV->hasInitializer())
678     printConstant(GV->getInitializer(), getID(GV));
679   else {
680     toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
681                                                 Target) << "\n";
682     toAsm << "\t.type\t" << getID(GV) << ",#object\n";
683     toAsm << "\t.reserve\t" << getID(GV) << ","
684           << Target.findOptimalStorageSize(GV->getType()->getElementType())
685           << "\n";
686   }
687 }
688
689
690 void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module *M) {
691   // First, get the constants there were marked by the code generator for
692   // inclusion in the assembly code data area and fold them all into a
693   // single constant pool since there may be lots of duplicates.  Also,
694   // lets force these constants into the slot table so that we can get
695   // unique names for unnamed constants also.
696   // 
697   std::hash_set<const Constant*> moduleConstants;
698   FoldConstants(M, moduleConstants);
699     
700   // Now, emit the three data sections separately; the cost of I/O should
701   // make up for the cost of extra passes over the globals list!
702   // 
703   // Read-only data section (implies initialized)
704   for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
705     {
706       const GlobalVariable* GV = *GI;
707       if (GV->hasInitializer() && GV->isConstant())
708         {
709           if (GI == M->gbegin())
710             enterSection(AsmPrinter::ReadOnlyData);
711           printGlobalVariable(GV);
712         }
713     }
714   
715   for (std::hash_set<const Constant*>::const_iterator
716          I = moduleConstants.begin(),
717          E = moduleConstants.end();  I != E; ++I)
718     printConstant(*I);
719   
720   // Initialized read-write data section
721   for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
722     {
723       const GlobalVariable* GV = *GI;
724       if (GV->hasInitializer() && ! GV->isConstant())
725         {
726           if (GI == M->gbegin())
727             enterSection(AsmPrinter::InitRWData);
728           printGlobalVariable(GV);
729         }
730     }
731   
732   // Uninitialized read-write data section
733   for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
734     {
735       const GlobalVariable* GV = *GI;
736       if (! GV->hasInitializer())
737         {
738           if (GI == M->gbegin())
739             enterSection(AsmPrinter::UninitRWData);
740           printGlobalVariable(GV);
741         }
742     }
743   
744   toAsm << "\n";
745 }
746
747 }  // End anonymous namespace
748
749 Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
750   return new SparcModuleAsmPrinter(Out, *this);
751 }