blob: 590a51b7b4d1355b6a50c5e1dadbc739a2bbde3a [file] [log] [blame]
Nick Lewyckyf7a3c502010-09-07 18:14:24 +00001//===-- 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
Che-Liang Chioudf659632010-11-08 03:06:08 +000015#define DEBUG_TYPE "ptx-asm-printer"
16
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000017#include "PTX.h"
Justin Holewinskid8e4ed22011-09-28 14:32:04 +000018#include "PTXAsmPrinter.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000019#include "PTXMachineFunctionInfo.h"
Justin Holewinski27f08fc2011-09-23 14:18:22 +000020#include "PTXParamManager.h"
Justin Holewinski297984d2011-09-22 16:45:40 +000021#include "PTXRegisterInfo.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000022#include "PTXTargetMachine.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000023#include "llvm/DerivedTypes.h"
24#include "llvm/Module.h"
Eric Christopher50880d02010-09-18 18:52:28 +000025#include "llvm/ADT/SmallString.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000026#include "llvm/ADT/StringExtras.h"
27#include "llvm/ADT/Twine.h"
Justin Holewinski47997292011-06-24 19:19:18 +000028#include "llvm/Analysis/DebugInfo.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000029#include "llvm/CodeGen/AsmPrinter.h"
Justin Holewinskidf1c8d82011-06-20 15:56:20 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000031#include "llvm/CodeGen/MachineInstr.h"
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Justin Holewinski47997292011-06-24 19:19:18 +000033#include "llvm/MC/MCContext.h"
Justin Holewinskid8e4ed22011-09-28 14:32:04 +000034#include "llvm/MC/MCExpr.h"
35#include "llvm/MC/MCInst.h"
Eric Christopher50880d02010-09-18 18:52:28 +000036#include "llvm/MC/MCStreamer.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000037#include "llvm/MC/MCSymbol.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000038#include "llvm/Target/Mangler.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000039#include "llvm/Target/TargetLoweringObjectFile.h"
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000040#include "llvm/Support/CommandLine.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000041#include "llvm/Support/Debug.h"
42#include "llvm/Support/ErrorHandling.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000043#include "llvm/Support/MathExtras.h"
Justin Holewinski47997292011-06-24 19:19:18 +000044#include "llvm/Support/Path.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000045#include "llvm/Support/TargetRegistry.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000046#include "llvm/Support/raw_ostream.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000047
48using namespace llvm;
49
Che-Liang Chioudf659632010-11-08 03:06:08 +000050static const char PARAM_PREFIX[] = "__param_";
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +000051static const char RETURN_PREFIX[] = "__ret_";
Che-Liang Chioudf659632010-11-08 03:06:08 +000052
Justin Holewinski5422a0f2011-09-22 16:45:46 +000053static const char *getRegisterTypeName(unsigned RegNo,
54 const MachineRegisterInfo& MRI) {
55 const TargetRegisterClass *TRC = MRI.getRegClass(RegNo);
56
57#define TEST_REGCLS(cls, clsstr) \
58 if (PTX::cls ## RegisterClass == TRC) return # clsstr;
59
Justin Holewinski1b91bcd2011-06-16 17:49:58 +000060 TEST_REGCLS(RegPred, pred);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +000061 TEST_REGCLS(RegI16, b16);
62 TEST_REGCLS(RegI32, b32);
63 TEST_REGCLS(RegI64, b64);
64 TEST_REGCLS(RegF32, b32);
65 TEST_REGCLS(RegF64, b64);
Che-Liang Chioudf659632010-11-08 03:06:08 +000066#undef TEST_REGCLS
67
68 llvm_unreachable("Not in any register class!");
69 return NULL;
70}
71
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000072static const char *getStateSpaceName(unsigned addressSpace) {
Che-Liang Chioud34f19f2010-12-30 10:41:27 +000073 switch (addressSpace) {
74 default: llvm_unreachable("Unknown state space");
Justin Holewinskif51b7e52011-09-30 14:36:36 +000075 case PTXStateSpace::Global: return "global";
76 case PTXStateSpace::Constant: return "const";
77 case PTXStateSpace::Local: return "local";
78 case PTXStateSpace::Parameter: return "param";
79 case PTXStateSpace::Shared: return "shared";
Che-Liang Chioud34f19f2010-12-30 10:41:27 +000080 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000081 return NULL;
82}
83
Chris Lattnerdb125cf2011-07-18 04:54:35 +000084static const char *getTypeName(Type* type) {
Che-Liang Chiouf7172022011-02-28 06:34:09 +000085 while (true) {
86 switch (type->getTypeID()) {
87 default: llvm_unreachable("Unknown type");
88 case Type::FloatTyID: return ".f32";
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000089 case Type::DoubleTyID: return ".f64";
90 case Type::IntegerTyID:
91 switch (type->getPrimitiveSizeInBits()) {
92 default: llvm_unreachable("Unknown integer bit-width");
93 case 16: return ".u16";
94 case 32: return ".u32";
95 case 64: return ".u64";
96 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +000097 case Type::ArrayTyID:
98 case Type::PointerTyID:
Chris Lattnerdb125cf2011-07-18 04:54:35 +000099 type = dyn_cast<SequentialType>(type)->getElementType();
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000100 break;
101 }
102 }
103 return NULL;
104}
105
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000106bool PTXAsmPrinter::doFinalization(Module &M) {
107 // XXX Temproarily remove global variables so that doFinalization() will not
108 // emit them again (global variables are emitted at beginning).
109
110 Module::GlobalListType &global_list = M.getGlobalList();
111 int i, n = global_list.size();
112 GlobalVariable **gv_array = new GlobalVariable* [n];
113
114 // first, back-up GlobalVariable in gv_array
115 i = 0;
116 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
117 I != E; ++I)
118 gv_array[i++] = &*I;
119
120 // second, empty global_list
121 while (!global_list.empty())
122 global_list.remove(global_list.begin());
123
124 // call doFinalization
125 bool ret = AsmPrinter::doFinalization(M);
126
127 // now we restore global variables
128 for (i = 0; i < n; i ++)
129 global_list.insert(global_list.end(), gv_array[i]);
130
131 delete[] gv_array;
132 return ret;
133}
134
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000135void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
136{
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000137 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
138
Justin Holewinskied0e4c82011-09-28 14:32:06 +0000139 // Emit the PTX .version and .target attributes
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000140 OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString()));
141 OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() +
Justin Holewinski12785e82011-03-03 13:34:29 +0000142 (ST.supportsDouble() ? ""
143 : ", map_f64_to_f32")));
Justin Holewinskia9c85f92011-06-22 00:43:56 +0000144 // .address_size directive is optional, but it must immediately follow
145 // the .target directive if present within a module
146 if (ST.supportsPTX23()) {
147 std::string addrSize = ST.is64Bit() ? "64" : "32";
148 OutStreamer.EmitRawText(Twine("\t.address_size " + addrSize));
149 }
150
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000151 OutStreamer.AddBlankLine();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000152
Justin Holewinski47997292011-06-24 19:19:18 +0000153 // Define any .file directives
154 DebugInfoFinder DbgFinder;
155 DbgFinder.processModule(M);
156
157 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
158 E = DbgFinder.compile_unit_end(); I != E; ++I) {
159 DICompileUnit DIUnit(*I);
160 StringRef FN = DIUnit.getFilename();
161 StringRef Dir = DIUnit.getDirectory();
162 GetOrCreateSourceID(FN, Dir);
163 }
164
165 OutStreamer.AddBlankLine();
166
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000167 // declare global variables
168 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
169 i != e; ++i)
170 EmitVariableDeclaration(i);
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000171}
172
Che-Liang Chioudf659632010-11-08 03:06:08 +0000173void PTXAsmPrinter::EmitFunctionBodyStart() {
174 OutStreamer.EmitRawText(Twine("{"));
175
176 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
Justin Holewinskie953a642011-09-23 14:31:12 +0000177 const PTXParamManager &PM = MFI->getParamManager();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000178
Justin Holewinski297984d2011-09-22 16:45:40 +0000179 // Print register definitions
180 std::string regDefs;
181 unsigned numRegs;
Che-Liang Chioudf659632010-11-08 03:06:08 +0000182
Justin Holewinski297984d2011-09-22 16:45:40 +0000183 // pred
184 numRegs = MFI->getNumRegistersForClass(PTX::RegPredRegisterClass);
185 if(numRegs > 0) {
186 regDefs += "\t.reg .pred %p<";
187 regDefs += utostr(numRegs);
188 regDefs += ">;\n";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000189 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000190
Justin Holewinski297984d2011-09-22 16:45:40 +0000191 // i16
192 numRegs = MFI->getNumRegistersForClass(PTX::RegI16RegisterClass);
193 if(numRegs > 0) {
194 regDefs += "\t.reg .b16 %rh<";
195 regDefs += utostr(numRegs);
196 regDefs += ">;\n";
197 }
198
199 // i32
200 numRegs = MFI->getNumRegistersForClass(PTX::RegI32RegisterClass);
201 if(numRegs > 0) {
202 regDefs += "\t.reg .b32 %r<";
203 regDefs += utostr(numRegs);
204 regDefs += ">;\n";
205 }
206
207 // i64
208 numRegs = MFI->getNumRegistersForClass(PTX::RegI64RegisterClass);
209 if(numRegs > 0) {
210 regDefs += "\t.reg .b64 %rd<";
211 regDefs += utostr(numRegs);
212 regDefs += ">;\n";
213 }
214
215 // f32
216 numRegs = MFI->getNumRegistersForClass(PTX::RegF32RegisterClass);
217 if(numRegs > 0) {
218 regDefs += "\t.reg .f32 %f<";
219 regDefs += utostr(numRegs);
220 regDefs += ">;\n";
221 }
222
223 // f64
224 numRegs = MFI->getNumRegistersForClass(PTX::RegF64RegisterClass);
225 if(numRegs > 0) {
226 regDefs += "\t.reg .f64 %fd<";
227 regDefs += utostr(numRegs);
228 regDefs += ">;\n";
229 }
230
Justin Holewinskie953a642011-09-23 14:31:12 +0000231 // Local params
232 for (PTXParamManager::param_iterator i = PM.local_begin(), e = PM.local_end();
233 i != e; ++i) {
234 regDefs += "\t.param .b";
235 regDefs += utostr(PM.getParamSize(*i));
236 regDefs += " ";
237 regDefs += PM.getParamName(*i);
238 regDefs += ";\n";
239 }
240
Justin Holewinski297984d2011-09-22 16:45:40 +0000241 OutStreamer.EmitRawText(Twine(regDefs));
242
243
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000244 const MachineFrameInfo* FrameInfo = MF->getFrameInfo();
Justin Holewinski08d03162011-06-22 16:07:03 +0000245 DEBUG(dbgs() << "Have " << FrameInfo->getNumObjects()
246 << " frame object(s)\n");
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000247 for (unsigned i = 0, e = FrameInfo->getNumObjects(); i != e; ++i) {
248 DEBUG(dbgs() << "Size of object: " << FrameInfo->getObjectSize(i) << "\n");
Justin Holewinski08d03162011-06-22 16:07:03 +0000249 if (FrameInfo->getObjectSize(i) > 0) {
Justin Holewinski58788502011-09-26 16:20:34 +0000250 std::string def = "\t.local .align ";
251 def += utostr(FrameInfo->getObjectAlignment(i));
Justin Holewinski63602ed2011-09-26 18:57:22 +0000252 def += " .b8";
Justin Holewinskic1d8fbd2011-09-26 16:20:28 +0000253 def += " __local";
Justin Holewinski08d03162011-06-22 16:07:03 +0000254 def += utostr(i);
Justin Holewinski63602ed2011-09-26 18:57:22 +0000255 def += "[";
256 def += utostr(FrameInfo->getObjectSize(i)); // Convert to bits
257 def += "]";
Justin Holewinski08d03162011-06-22 16:07:03 +0000258 def += ";";
259 OutStreamer.EmitRawText(Twine(def));
260 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000261 }
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000262
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000263 //unsigned Index = 1;
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000264 // Print parameter passing params
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000265 //for (PTXMachineFunctionInfo::param_iterator
266 // i = MFI->paramBegin(), e = MFI->paramEnd(); i != e; ++i) {
267 // std::string def = "\t.param .b";
268 // def += utostr(*i);
269 // def += " __ret_";
270 // def += utostr(Index);
271 // Index++;
272 // def += ";";
273 // OutStreamer.EmitRawText(Twine(def));
274 //}
Che-Liang Chioudf659632010-11-08 03:06:08 +0000275}
276
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000277void PTXAsmPrinter::EmitFunctionBodyEnd() {
278 OutStreamer.EmitRawText(Twine("}"));
279}
280
Eric Christopher50880d02010-09-18 18:52:28 +0000281void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000282 MCInst TmpInst;
283 LowerPTXMachineInstrToMCInst(MI, TmpInst, *this);
284 OutStreamer.EmitInstruction(TmpInst);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000285}
286
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000287void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
288 // Check to see if this is a special global used by LLVM, if so, emit it.
289 if (EmitSpecialLLVMGlobal(gv))
290 return;
291
292 MCSymbol *gvsym = Mang->getSymbol(gv);
293
294 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
295
296 std::string decl;
297
298 // check if it is defined in some other translation unit
299 if (gv->isDeclaration())
300 decl += ".extern ";
301
302 // state space: e.g., .global
303 decl += ".";
304 decl += getStateSpaceName(gv->getType()->getAddressSpace());
305 decl += " ";
306
307 // alignment (optional)
308 unsigned alignment = gv->getAlignment();
309 if (alignment != 0) {
310 decl += ".align ";
Justin Holewinskicfab2be2011-09-28 18:24:58 +0000311 decl += utostr(gv->getAlignment());
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000312 decl += " ";
313 }
314
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000315
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000316 if (PointerType::classof(gv->getType())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000317 PointerType* pointerTy = dyn_cast<PointerType>(gv->getType());
318 Type* elementTy = pointerTy->getElementType();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000319
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000320 decl += ".b8 ";
321 decl += gvsym->getName();
322 decl += "[";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000323
Justin Holewinski9583a862011-04-28 00:19:50 +0000324 if (elementTy->isArrayTy())
325 {
326 assert(elementTy->isArrayTy() && "Only pointers to arrays are supported");
327
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000328 ArrayType* arrayTy = dyn_cast<ArrayType>(elementTy);
Justin Holewinski9583a862011-04-28 00:19:50 +0000329 elementTy = arrayTy->getElementType();
330
331 unsigned numElements = arrayTy->getNumElements();
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000332
Justin Holewinski9583a862011-04-28 00:19:50 +0000333 while (elementTy->isArrayTy()) {
334
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000335 arrayTy = dyn_cast<ArrayType>(elementTy);
Justin Holewinski9583a862011-04-28 00:19:50 +0000336 elementTy = arrayTy->getElementType();
337
338 numElements *= arrayTy->getNumElements();
339 }
340
341 // FIXME: isPrimitiveType() == false for i16?
342 assert(elementTy->isSingleValueType() &&
343 "Non-primitive types are not handled");
344
345 // Compute the size of the array, in bytes.
346 uint64_t arraySize = (elementTy->getPrimitiveSizeInBits() >> 3)
347 * numElements;
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000348
Justin Holewinski9583a862011-04-28 00:19:50 +0000349 decl += utostr(arraySize);
350 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000351
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000352 decl += "]";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000353
Justin Holewinski9583a862011-04-28 00:19:50 +0000354 // handle string constants (assume ConstantArray means string)
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000355
Justin Holewinski9583a862011-04-28 00:19:50 +0000356 if (gv->hasInitializer())
357 {
Justin Holewinski297984d2011-09-22 16:45:40 +0000358 const Constant *C = gv->getInitializer();
Justin Holewinski9583a862011-04-28 00:19:50 +0000359 if (const ConstantArray *CA = dyn_cast<ConstantArray>(C))
360 {
361 decl += " = {";
362
363 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
364 {
365 if (i > 0) decl += ",";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000366
367 decl += "0x" +
368 utohexstr(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());
Justin Holewinski9583a862011-04-28 00:19:50 +0000369 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000370
Justin Holewinski9583a862011-04-28 00:19:50 +0000371 decl += "}";
372 }
373 }
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000374 }
375 else {
376 // Note: this is currently the fall-through case and most likely generates
377 // incorrect code.
378 decl += getTypeName(gv->getType());
379 decl += " ";
380
381 decl += gvsym->getName();
382
383 if (ArrayType::classof(gv->getType()) ||
384 PointerType::classof(gv->getType()))
385 decl += "[]";
386 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000387
388 decl += ";";
389
390 OutStreamer.EmitRawText(Twine(decl));
391
392 OutStreamer.AddBlankLine();
393}
394
Justin Holewinskied0e4c82011-09-28 14:32:06 +0000395void PTXAsmPrinter::EmitFunctionEntryLabel() {
Che-Liang Chioudf659632010-11-08 03:06:08 +0000396 // The function label could have already been emitted if two symbols end up
397 // conflicting due to asm renaming. Detect this and emit an error.
398 if (!CurrentFnSym->isUndefined()) {
399 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
400 "' label emitted multiple times to assembly file");
401 return;
402 }
403
404 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000405 const PTXParamManager &PM = MFI->getParamManager();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000406 const bool isKernel = MFI->isKernel();
Justin Holewinski67a91842011-06-23 18:10:03 +0000407 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000408 const MachineRegisterInfo& MRI = MF->getRegInfo();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000409
410 std::string decl = isKernel ? ".entry" : ".func";
411
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000412 if (!isKernel) {
413 decl += " (";
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000414 if (ST.useParamSpaceForDeviceArgs()) {
415 for (PTXParamManager::param_iterator i = PM.ret_begin(), e = PM.ret_end(),
416 b = i; i != e; ++i) {
417 if (i != b) {
418 decl += ", ";
419 }
420
421 decl += ".param .b";
422 decl += utostr(PM.getParamSize(*i));
423 decl += " ";
424 decl += PM.getParamName(*i);
425 }
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000426 } else {
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000427 for (PTXMachineFunctionInfo::reg_iterator
428 i = MFI->retreg_begin(), e = MFI->retreg_end(), b = i;
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000429 i != e; ++i) {
430 if (i != b) {
431 decl += ", ";
432 }
433 decl += ".reg .";
434 decl += getRegisterTypeName(*i, MRI);
435 decl += " ";
436 decl += MFI->getRegisterName(*i);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000437 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000438 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000439 decl += ")";
440 }
441
442 // Print function name
443 decl += " ";
444 decl += CurrentFnSym->getName().str();
445
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000446 decl += " (";
447
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000448 // Print parameters
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000449 if (isKernel || ST.useParamSpaceForDeviceArgs()) {
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000450 for (PTXParamManager::param_iterator i = PM.arg_begin(), e = PM.arg_end(),
451 b = i; i != e; ++i) {
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000452 if (i != b) {
453 decl += ", ";
454 }
455
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000456 decl += ".param .b";
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000457 decl += utostr(PM.getParamSize(*i));
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000458 decl += " ";
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000459 decl += PM.getParamName(*i);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000460 }
461 } else {
462 for (PTXMachineFunctionInfo::reg_iterator
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000463 i = MFI->argreg_begin(), e = MFI->argreg_end(), b = i;
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000464 i != e; ++i) {
465 if (i != b) {
466 decl += ", ";
467 }
468
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000469 decl += ".reg .";
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000470 decl += getRegisterTypeName(*i, MRI);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000471 decl += " ";
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000472 decl += MFI->getRegisterName(*i);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000473 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000474 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000475 decl += ")";
476
Che-Liang Chioudf659632010-11-08 03:06:08 +0000477 OutStreamer.EmitRawText(Twine(decl));
478}
479
Justin Holewinski47997292011-06-24 19:19:18 +0000480unsigned PTXAsmPrinter::GetOrCreateSourceID(StringRef FileName,
481 StringRef DirName) {
482 // If FE did not provide a file name, then assume stdin.
483 if (FileName.empty())
484 return GetOrCreateSourceID("<stdin>", StringRef());
485
486 // MCStream expects full path name as filename.
487 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
488 SmallString<128> FullPathName = DirName;
489 sys::path::append(FullPathName, FileName);
490 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
491 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
492 }
493
494 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
495 if (Entry.getValue())
496 return Entry.getValue();
497
498 unsigned SrcId = SourceIdMap.size();
499 Entry.setValue(SrcId);
500
501 // Print out a .file directive to specify files for .loc directives.
502 OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
503
504 return SrcId;
505}
506
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000507MCOperand PTXAsmPrinter::GetSymbolRef(const MachineOperand &MO,
508 const MCSymbol *Symbol) {
509 const MCExpr *Expr;
510 Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None, OutContext);
511 return MCOperand::CreateExpr(Expr);
512}
513
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000514MCOperand PTXAsmPrinter::lowerOperand(const MachineOperand &MO) {
515 MCOperand MCOp;
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000516 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
517 const MCExpr *Expr;
518 const char *RegSymbolName;
519 switch (MO.getType()) {
520 default:
521 llvm_unreachable("Unknown operand type");
522 case MachineOperand::MO_Register:
523 // We create register operands as symbols, since the PTXInstPrinter class
524 // has no way to map virtual registers back to a name without some ugly
525 // hacks.
526 // FIXME: Figure out a better way to handle virtual register naming.
527 RegSymbolName = MFI->getRegisterName(MO.getReg());
528 Expr = MCSymbolRefExpr::Create(RegSymbolName, MCSymbolRefExpr::VK_None,
529 OutContext);
530 MCOp = MCOperand::CreateExpr(Expr);
531 break;
532 case MachineOperand::MO_Immediate:
533 MCOp = MCOperand::CreateImm(MO.getImm());
534 break;
535 case MachineOperand::MO_MachineBasicBlock:
536 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
537 MO.getMBB()->getSymbol(), OutContext));
538 break;
539 case MachineOperand::MO_GlobalAddress:
540 MCOp = GetSymbolRef(MO, Mang->getSymbol(MO.getGlobal()));
541 break;
542 case MachineOperand::MO_ExternalSymbol:
543 MCOp = GetSymbolRef(MO, GetExternalSymbolSymbol(MO.getSymbolName()));
544 break;
545 case MachineOperand::MO_FPImmediate:
546 APFloat Val = MO.getFPImm()->getValueAPF();
547 bool ignored;
548 Val.convert(APFloat::IEEEdouble, APFloat::rmTowardZero, &ignored);
549 MCOp = MCOperand::CreateFPImm(Val.convertToDouble());
550 break;
551 }
552
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000553 return MCOp;
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000554}
Eric Christopher50880d02010-09-18 18:52:28 +0000555
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000556// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000557extern "C" void LLVMInitializePTXAsmPrinter() {
Justin Holewinskie1fee482011-04-20 15:37:17 +0000558 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target);
559 RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target);
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000560}
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000561