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