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