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