PTX: Fix case where printed alignment could be 0
[oota-llvm.git] / lib / Target / PTX / PTXAsmPrinter.cpp
1 //===-- PTXAsmPrinter.cpp - PTX LLVM assembly writer ----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 PTX assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "ptx-asm-printer"
16
17 #include "PTX.h"
18 #include "PTXMachineFunctionInfo.h"
19 #include "PTXParamManager.h"
20 #include "PTXRegisterInfo.h"
21 #include "PTXTargetMachine.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Module.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/ADT/Twine.h"
27 #include "llvm/Analysis/DebugInfo.h"
28 #include "llvm/CodeGen/AsmPrinter.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/MC/MCContext.h"
33 #include "llvm/MC/MCStreamer.h"
34 #include "llvm/MC/MCSymbol.h"
35 #include "llvm/Target/Mangler.h"
36 #include "llvm/Target/TargetLoweringObjectFile.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/MathExtras.h"
41 #include "llvm/Support/Path.h"
42 #include "llvm/Support/TargetRegistry.h"
43 #include "llvm/Support/raw_ostream.h"
44
45 using namespace llvm;
46
47 namespace {
48 class PTXAsmPrinter : public AsmPrinter {
49 public:
50   explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
51     : AsmPrinter(TM, Streamer) {}
52
53   const char *getPassName() const { return "PTX Assembly Printer"; }
54
55   bool doFinalization(Module &M);
56
57   virtual void EmitStartOfAsmFile(Module &M);
58
59   virtual bool runOnMachineFunction(MachineFunction &MF);
60
61   virtual void EmitFunctionBodyStart();
62   virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); }
63
64   virtual void EmitInstruction(const MachineInstr *MI);
65
66   void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
67   void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
68                        const char *Modifier = 0);
69   void printReturnOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
70                           const char *Modifier = 0);
71   void printPredicateOperand(const MachineInstr *MI, raw_ostream &O);
72
73   void printCall(const MachineInstr *MI, raw_ostream &O);
74
75   unsigned GetOrCreateSourceID(StringRef FileName,
76                                StringRef DirName);
77
78   // autogen'd.
79   void printInstruction(const MachineInstr *MI, raw_ostream &OS);
80   static const char *getRegisterName(unsigned RegNo);
81
82 private:
83   void EmitVariableDeclaration(const GlobalVariable *gv);
84   void EmitFunctionDeclaration();
85
86   StringMap<unsigned> SourceIdMap;
87 }; // class PTXAsmPrinter
88 } // namespace
89
90 static const char PARAM_PREFIX[] = "__param_";
91 static const char RETURN_PREFIX[] = "__ret_";
92
93 static const char *getRegisterTypeName(unsigned RegNo,
94                                        const MachineRegisterInfo& MRI) {
95   const TargetRegisterClass *TRC = MRI.getRegClass(RegNo);
96
97 #define TEST_REGCLS(cls, clsstr) \
98   if (PTX::cls ## RegisterClass == TRC) return # clsstr;
99
100   TEST_REGCLS(RegPred, pred);
101   TEST_REGCLS(RegI16, b16);
102   TEST_REGCLS(RegI32, b32);
103   TEST_REGCLS(RegI64, b64);
104   TEST_REGCLS(RegF32, b32);
105   TEST_REGCLS(RegF64, b64);
106 #undef TEST_REGCLS
107
108   llvm_unreachable("Not in any register class!");
109   return NULL;
110 }
111
112 static const char *getStateSpaceName(unsigned addressSpace) {
113   switch (addressSpace) {
114   default: llvm_unreachable("Unknown state space");
115   case PTX::GLOBAL:    return "global";
116   case PTX::CONSTANT:  return "const";
117   case PTX::LOCAL:     return "local";
118   case PTX::PARAMETER: return "param";
119   case PTX::SHARED:    return "shared";
120   }
121   return NULL;
122 }
123
124 static const char *getTypeName(Type* type) {
125   while (true) {
126     switch (type->getTypeID()) {
127       default: llvm_unreachable("Unknown type");
128       case Type::FloatTyID: return ".f32";
129       case Type::DoubleTyID: return ".f64";
130       case Type::IntegerTyID:
131         switch (type->getPrimitiveSizeInBits()) {
132           default: llvm_unreachable("Unknown integer bit-width");
133           case 16: return ".u16";
134           case 32: return ".u32";
135           case 64: return ".u64";
136         }
137       case Type::ArrayTyID:
138       case Type::PointerTyID:
139         type = dyn_cast<SequentialType>(type)->getElementType();
140         break;
141     }
142   }
143   return NULL;
144 }
145
146 bool PTXAsmPrinter::doFinalization(Module &M) {
147   // XXX Temproarily remove global variables so that doFinalization() will not
148   // emit them again (global variables are emitted at beginning).
149
150   Module::GlobalListType &global_list = M.getGlobalList();
151   int i, n = global_list.size();
152   GlobalVariable **gv_array = new GlobalVariable* [n];
153
154   // first, back-up GlobalVariable in gv_array
155   i = 0;
156   for (Module::global_iterator I = global_list.begin(), E = global_list.end();
157        I != E; ++I)
158     gv_array[i++] = &*I;
159
160   // second, empty global_list
161   while (!global_list.empty())
162     global_list.remove(global_list.begin());
163
164   // call doFinalization
165   bool ret = AsmPrinter::doFinalization(M);
166
167   // now we restore global variables
168   for (i = 0; i < n; i ++)
169     global_list.insert(global_list.end(), gv_array[i]);
170
171   delete[] gv_array;
172   return ret;
173 }
174
175 void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
176 {
177   const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
178
179   OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString()));
180   OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() +
181                                 (ST.supportsDouble() ? ""
182                                                      : ", map_f64_to_f32")));
183   // .address_size directive is optional, but it must immediately follow
184   // the .target directive if present within a module
185   if (ST.supportsPTX23()) {
186     std::string addrSize = ST.is64Bit() ? "64" : "32";
187     OutStreamer.EmitRawText(Twine("\t.address_size " + addrSize));
188   }
189
190   OutStreamer.AddBlankLine();
191
192   // Define any .file directives
193   DebugInfoFinder DbgFinder;
194   DbgFinder.processModule(M);
195
196   for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
197        E = DbgFinder.compile_unit_end(); I != E; ++I) {
198     DICompileUnit DIUnit(*I);
199     StringRef FN = DIUnit.getFilename();
200     StringRef Dir = DIUnit.getDirectory();
201     GetOrCreateSourceID(FN, Dir);
202   }
203
204   OutStreamer.AddBlankLine();
205
206   // declare global variables
207   for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
208        i != e; ++i)
209     EmitVariableDeclaration(i);
210 }
211
212 bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
213   SetupMachineFunction(MF);
214   EmitFunctionDeclaration();
215   EmitFunctionBody();
216   return false;
217 }
218
219 void PTXAsmPrinter::EmitFunctionBodyStart() {
220   OutStreamer.EmitRawText(Twine("{"));
221
222   const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
223   const PTXParamManager &PM = MFI->getParamManager();
224
225   // Print register definitions
226   std::string regDefs;
227   unsigned numRegs;
228
229   // pred
230   numRegs = MFI->getNumRegistersForClass(PTX::RegPredRegisterClass);
231   if(numRegs > 0) {
232     regDefs += "\t.reg .pred %p<";
233     regDefs += utostr(numRegs);
234     regDefs += ">;\n";
235   }
236
237   // i16
238   numRegs = MFI->getNumRegistersForClass(PTX::RegI16RegisterClass);
239   if(numRegs > 0) {
240     regDefs += "\t.reg .b16 %rh<";
241     regDefs += utostr(numRegs);
242     regDefs += ">;\n";
243   }
244
245   // i32
246   numRegs = MFI->getNumRegistersForClass(PTX::RegI32RegisterClass);
247   if(numRegs > 0) {
248     regDefs += "\t.reg .b32 %r<";
249     regDefs += utostr(numRegs);
250     regDefs += ">;\n";
251   }
252
253   // i64
254   numRegs = MFI->getNumRegistersForClass(PTX::RegI64RegisterClass);
255   if(numRegs > 0) {
256     regDefs += "\t.reg .b64 %rd<";
257     regDefs += utostr(numRegs);
258     regDefs += ">;\n";
259   }
260
261   // f32
262   numRegs = MFI->getNumRegistersForClass(PTX::RegF32RegisterClass);
263   if(numRegs > 0) {
264     regDefs += "\t.reg .f32 %f<";
265     regDefs += utostr(numRegs);
266     regDefs += ">;\n";
267   }
268
269   // f64
270   numRegs = MFI->getNumRegistersForClass(PTX::RegF64RegisterClass);
271   if(numRegs > 0) {
272     regDefs += "\t.reg .f64 %fd<";
273     regDefs += utostr(numRegs);
274     regDefs += ">;\n";
275   }
276
277   // Local params
278   for (PTXParamManager::param_iterator i = PM.local_begin(), e = PM.local_end();
279        i != e; ++i) {
280     regDefs += "\t.param .b";
281     regDefs += utostr(PM.getParamSize(*i));
282     regDefs += " ";
283     regDefs += PM.getParamName(*i);
284     regDefs += ";\n";
285   }
286
287   OutStreamer.EmitRawText(Twine(regDefs));
288
289
290   const MachineFrameInfo* FrameInfo = MF->getFrameInfo();
291   DEBUG(dbgs() << "Have " << FrameInfo->getNumObjects()
292                << " frame object(s)\n");
293   for (unsigned i = 0, e = FrameInfo->getNumObjects(); i != e; ++i) {
294     DEBUG(dbgs() << "Size of object: " << FrameInfo->getObjectSize(i) << "\n");
295     if (FrameInfo->getObjectSize(i) > 0) {
296       std::string def = "\t.local .align ";
297       def += utostr(FrameInfo->getObjectAlignment(i));
298       def += " .b8";
299       def += " __local";
300       def += utostr(i);
301       def += "[";
302       def += utostr(FrameInfo->getObjectSize(i)); // Convert to bits
303       def += "]";
304       def += ";";
305       OutStreamer.EmitRawText(Twine(def));
306     }
307   }
308
309   //unsigned Index = 1;
310   // Print parameter passing params
311   //for (PTXMachineFunctionInfo::param_iterator
312   //     i = MFI->paramBegin(), e = MFI->paramEnd(); i != e; ++i) {
313   //  std::string def = "\t.param .b";
314   //  def += utostr(*i);
315   //  def += " __ret_";
316   //  def += utostr(Index);
317   //  Index++;
318   //  def += ";";
319   //  OutStreamer.EmitRawText(Twine(def));
320   //}
321 }
322
323 void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
324   std::string str;
325   str.reserve(64);
326
327   raw_string_ostream OS(str);
328
329   DebugLoc DL = MI->getDebugLoc();
330   if (!DL.isUnknown()) {
331
332     const MDNode *S = DL.getScope(MF->getFunction()->getContext());
333
334     // This is taken from DwarfDebug.cpp, which is conveniently not a public
335     // LLVM class.
336     StringRef Fn;
337     StringRef Dir;
338     unsigned Src = 1;
339     if (S) {
340       DIDescriptor Scope(S);
341       if (Scope.isCompileUnit()) {
342         DICompileUnit CU(S);
343         Fn = CU.getFilename();
344         Dir = CU.getDirectory();
345       } else if (Scope.isFile()) {
346         DIFile F(S);
347         Fn = F.getFilename();
348         Dir = F.getDirectory();
349       } else if (Scope.isSubprogram()) {
350         DISubprogram SP(S);
351         Fn = SP.getFilename();
352         Dir = SP.getDirectory();
353       } else if (Scope.isLexicalBlock()) {
354         DILexicalBlock DB(S);
355         Fn = DB.getFilename();
356         Dir = DB.getDirectory();
357       } else
358         assert(0 && "Unexpected scope info");
359
360       Src = GetOrCreateSourceID(Fn, Dir);
361     }
362     OutStreamer.EmitDwarfLocDirective(Src, DL.getLine(), DL.getCol(),
363                                      0, 0, 0, Fn);
364
365     const MCDwarfLoc& MDL = OutContext.getCurrentDwarfLoc();
366
367     OS << "\t.loc ";
368     OS << utostr(MDL.getFileNum());
369     OS << " ";
370     OS << utostr(MDL.getLine());
371     OS << " ";
372     OS << utostr(MDL.getColumn());
373     OS << "\n";
374   }
375
376
377   // Emit predicate
378   printPredicateOperand(MI, OS);
379
380   // Write instruction to str
381   if (MI->getOpcode() == PTX::CALL) {
382     printCall(MI, OS);
383   } else {
384     printInstruction(MI, OS);
385   }
386   OS << ';';
387   OS.flush();
388
389   StringRef strref = StringRef(str);
390   OutStreamer.EmitRawText(strref);
391 }
392
393 void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
394                                  raw_ostream &OS) {
395   const MachineOperand &MO = MI->getOperand(opNum);
396   const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
397
398   switch (MO.getType()) {
399     default:
400       llvm_unreachable("<unknown operand type>");
401       break;
402     case MachineOperand::MO_GlobalAddress:
403       OS << *Mang->getSymbol(MO.getGlobal());
404       break;
405     case MachineOperand::MO_Immediate:
406       OS << (long) MO.getImm();
407       break;
408     case MachineOperand::MO_MachineBasicBlock:
409       OS << *MO.getMBB()->getSymbol();
410       break;
411     case MachineOperand::MO_Register:
412       OS << MFI->getRegisterName(MO.getReg());
413       break;
414     case MachineOperand::MO_ExternalSymbol:
415       OS << MO.getSymbolName();
416       break;
417     case MachineOperand::MO_FPImmediate:
418       APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt();
419       bool  isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID;
420       // Emit 0F for 32-bit floats and 0D for 64-bit doubles.
421       if (isFloat) {
422         OS << "0F";
423       }
424       else {
425         OS << "0D";
426       }
427       // Emit the encoded floating-point value.
428       if (constFP.getZExtValue() > 0) {
429         OS << constFP.toString(16, false);
430       }
431       else {
432         OS << "00000000";
433         // If We have a double-precision zero, pad to 8-bytes.
434         if (!isFloat) {
435           OS << "00000000";
436         }
437       }
438       break;
439   }
440 }
441
442 void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
443                                     raw_ostream &OS, const char *Modifier) {
444   printOperand(MI, opNum, OS);
445
446   if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
447     return; // don't print "+0"
448
449   OS << "+";
450   printOperand(MI, opNum+1, OS);
451 }
452
453 void PTXAsmPrinter::printReturnOperand(const MachineInstr *MI, int opNum,
454                                        raw_ostream &OS, const char *Modifier) {
455   //OS << RETURN_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
456   OS << "__ret";
457 }
458
459 void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
460   // Check to see if this is a special global used by LLVM, if so, emit it.
461   if (EmitSpecialLLVMGlobal(gv))
462     return;
463
464   MCSymbol *gvsym = Mang->getSymbol(gv);
465
466   assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
467
468   std::string decl;
469
470   // check if it is defined in some other translation unit
471   if (gv->isDeclaration())
472     decl += ".extern ";
473
474   // state space: e.g., .global
475   decl += ".";
476   decl += getStateSpaceName(gv->getType()->getAddressSpace());
477   decl += " ";
478
479   // alignment (optional)
480   unsigned alignment = gv->getAlignment();
481   if (alignment != 0) {
482     decl += ".align ";
483     decl += utostr(std::max(1U, Log2_32(gv->getAlignment())));
484     decl += " ";
485   }
486
487
488   if (PointerType::classof(gv->getType())) {
489     PointerType* pointerTy = dyn_cast<PointerType>(gv->getType());
490     Type* elementTy = pointerTy->getElementType();
491
492     decl += ".b8 ";
493     decl += gvsym->getName();
494     decl += "[";
495
496     if (elementTy->isArrayTy())
497     {
498       assert(elementTy->isArrayTy() && "Only pointers to arrays are supported");
499
500       ArrayType* arrayTy = dyn_cast<ArrayType>(elementTy);
501       elementTy = arrayTy->getElementType();
502
503       unsigned numElements = arrayTy->getNumElements();
504
505       while (elementTy->isArrayTy()) {
506
507         arrayTy = dyn_cast<ArrayType>(elementTy);
508         elementTy = arrayTy->getElementType();
509
510         numElements *= arrayTy->getNumElements();
511       }
512
513       // FIXME: isPrimitiveType() == false for i16?
514       assert(elementTy->isSingleValueType() &&
515               "Non-primitive types are not handled");
516
517       // Compute the size of the array, in bytes.
518       uint64_t arraySize = (elementTy->getPrimitiveSizeInBits() >> 3)
519                         * numElements;
520
521       decl += utostr(arraySize);
522     }
523
524     decl += "]";
525
526     // handle string constants (assume ConstantArray means string)
527
528     if (gv->hasInitializer())
529     {
530       const Constant *C = gv->getInitializer();
531       if (const ConstantArray *CA = dyn_cast<ConstantArray>(C))
532       {
533         decl += " = {";
534
535         for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
536         {
537           if (i > 0)   decl += ",";
538
539           decl += "0x" +
540                 utohexstr(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());
541         }
542
543         decl += "}";
544       }
545     }
546   }
547   else {
548     // Note: this is currently the fall-through case and most likely generates
549     //       incorrect code.
550     decl += getTypeName(gv->getType());
551     decl += " ";
552
553     decl += gvsym->getName();
554
555     if (ArrayType::classof(gv->getType()) ||
556         PointerType::classof(gv->getType()))
557       decl += "[]";
558   }
559
560   decl += ";";
561
562   OutStreamer.EmitRawText(Twine(decl));
563
564   OutStreamer.AddBlankLine();
565 }
566
567 void PTXAsmPrinter::EmitFunctionDeclaration() {
568   // The function label could have already been emitted if two symbols end up
569   // conflicting due to asm renaming.  Detect this and emit an error.
570   if (!CurrentFnSym->isUndefined()) {
571     report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
572                        "' label emitted multiple times to assembly file");
573     return;
574   }
575
576   const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
577   const PTXParamManager &PM = MFI->getParamManager();
578   const bool isKernel = MFI->isKernel();
579   const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
580   const MachineRegisterInfo& MRI = MF->getRegInfo();
581
582   std::string decl = isKernel ? ".entry" : ".func";
583
584   unsigned cnt = 0;
585
586   if (!isKernel) {
587     decl += " (";
588     if (ST.useParamSpaceForDeviceArgs()) {
589       for (PTXParamManager::param_iterator i = PM.ret_begin(), e = PM.ret_end(),
590            b = i; i != e; ++i) {
591         if (i != b) {
592           decl += ", ";
593         }
594
595         decl += ".param .b";
596         decl += utostr(PM.getParamSize(*i));
597         decl += " ";
598         decl += PM.getParamName(*i);
599       }
600     } else {
601       for (PTXMachineFunctionInfo::reg_iterator
602            i = MFI->retreg_begin(), e = MFI->retreg_end(), b = i;
603            i != e; ++i) {
604         if (i != b) {
605           decl += ", ";
606         }
607         decl += ".reg .";
608         decl += getRegisterTypeName(*i, MRI);
609         decl += " ";
610         decl += MFI->getRegisterName(*i);
611       }
612     }
613     decl += ")";
614   }
615
616   // Print function name
617   decl += " ";
618   decl += CurrentFnSym->getName().str();
619
620   decl += " (";
621
622   cnt = 0;
623
624   // Print parameters
625   if (isKernel || ST.useParamSpaceForDeviceArgs()) {
626     for (PTXParamManager::param_iterator i = PM.arg_begin(), e = PM.arg_end(),
627          b = i; i != e; ++i) {
628       if (i != b) {
629         decl += ", ";
630       }
631
632       decl += ".param .b";
633       decl += utostr(PM.getParamSize(*i));
634       decl += " ";
635       decl += PM.getParamName(*i);
636     }
637   } else {
638     for (PTXMachineFunctionInfo::reg_iterator
639          i = MFI->argreg_begin(), e = MFI->argreg_end(), b = i;
640          i != e; ++i) {
641       if (i != b) {
642         decl += ", ";
643       }
644
645       decl += ".reg .";
646       decl += getRegisterTypeName(*i, MRI);
647       decl += " ";
648       decl += MFI->getRegisterName(*i);
649     }
650   }
651   decl += ")";
652
653   OutStreamer.EmitRawText(Twine(decl));
654 }
655
656 void PTXAsmPrinter::
657 printPredicateOperand(const MachineInstr *MI, raw_ostream &O) {
658   int i = MI->findFirstPredOperandIdx();
659   if (i == -1)
660     llvm_unreachable("missing predicate operand");
661
662   unsigned reg = MI->getOperand(i).getReg();
663   int predOp = MI->getOperand(i+1).getImm();
664   const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
665
666   DEBUG(dbgs() << "predicate: (" << reg << ", " << predOp << ")\n");
667
668   if (reg != PTX::NoRegister) {
669     O << '@';
670     if (predOp == PTX::PRED_NEGATE)
671       O << '!';
672     O << MFI->getRegisterName(reg);
673   }
674 }
675
676 void PTXAsmPrinter::
677 printCall(const MachineInstr *MI, raw_ostream &O) {
678   O << "\tcall.uni\t";
679   // The first two operands are the predicate slot
680   unsigned Index = 2;
681   while (!MI->getOperand(Index).isGlobal()) {
682     if (Index == 2) {
683       O << "(";
684     } else {
685       O << ", ";
686     }
687     printOperand(MI, Index, O);
688     Index++;
689   }
690
691   if (Index != 2) {
692     O << "), ";
693   }
694
695   assert(MI->getOperand(Index).isGlobal() &&
696          "A GlobalAddress must follow the return arguments");
697
698   const GlobalValue *Address = MI->getOperand(Index).getGlobal();
699   O << Address->getName() << ", (";
700   Index++;
701
702   while (Index < MI->getNumOperands()) {
703     printOperand(MI, Index, O);
704     if (Index < MI->getNumOperands()-1) {
705       O << ", ";
706     }
707     Index++;
708   }
709
710   O << ")";
711 }
712
713 unsigned PTXAsmPrinter::GetOrCreateSourceID(StringRef FileName,
714                                             StringRef DirName) {
715   // If FE did not provide a file name, then assume stdin.
716   if (FileName.empty())
717     return GetOrCreateSourceID("<stdin>", StringRef());
718
719   // MCStream expects full path name as filename.
720   if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
721     SmallString<128> FullPathName = DirName;
722     sys::path::append(FullPathName, FileName);
723     // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
724     return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
725   }
726
727   StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
728   if (Entry.getValue())
729     return Entry.getValue();
730
731   unsigned SrcId = SourceIdMap.size();
732   Entry.setValue(SrcId);
733
734   // Print out a .file directive to specify files for .loc directives.
735   OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
736
737   return SrcId;
738 }
739
740 #include "PTXGenAsmWriter.inc"
741
742 // Force static initialization.
743 extern "C" void LLVMInitializePTXAsmPrinter() {
744   RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target);
745   RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target);
746 }