blob: e329d5dcc7d006de9d818216cf08be42ed44408e [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"
Justin Holewinski68226a42011-10-09 15:42:02 +000023#include "llvm/Argument.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000024#include "llvm/DerivedTypes.h"
Justin Holewinski68226a42011-10-09 15:42:02 +000025#include "llvm/Function.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000026#include "llvm/Module.h"
Eric Christopher50880d02010-09-18 18:52:28 +000027#include "llvm/ADT/SmallString.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000028#include "llvm/ADT/StringExtras.h"
29#include "llvm/ADT/Twine.h"
Justin Holewinski47997292011-06-24 19:19:18 +000030#include "llvm/Analysis/DebugInfo.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000031#include "llvm/CodeGen/AsmPrinter.h"
Justin Holewinskidf1c8d82011-06-20 15:56:20 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000033#include "llvm/CodeGen/MachineInstr.h"
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Justin Holewinski47997292011-06-24 19:19:18 +000035#include "llvm/MC/MCContext.h"
Justin Holewinskid8e4ed22011-09-28 14:32:04 +000036#include "llvm/MC/MCExpr.h"
37#include "llvm/MC/MCInst.h"
Eric Christopher50880d02010-09-18 18:52:28 +000038#include "llvm/MC/MCStreamer.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000039#include "llvm/MC/MCSymbol.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000040#include "llvm/Target/Mangler.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000041#include "llvm/Target/TargetLoweringObjectFile.h"
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000042#include "llvm/Support/CommandLine.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000043#include "llvm/Support/Debug.h"
44#include "llvm/Support/ErrorHandling.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000045#include "llvm/Support/MathExtras.h"
Justin Holewinski47997292011-06-24 19:19:18 +000046#include "llvm/Support/Path.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000047#include "llvm/Support/TargetRegistry.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000048#include "llvm/Support/raw_ostream.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000049
50using namespace llvm;
51
Che-Liang Chioudf659632010-11-08 03:06:08 +000052static const char PARAM_PREFIX[] = "__param_";
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +000053static const char RETURN_PREFIX[] = "__ret_";
Che-Liang Chioudf659632010-11-08 03:06:08 +000054
Justin Holewinski5422a0f2011-09-22 16:45:46 +000055static const char *getRegisterTypeName(unsigned RegNo,
56 const MachineRegisterInfo& MRI) {
57 const TargetRegisterClass *TRC = MRI.getRegClass(RegNo);
58
59#define TEST_REGCLS(cls, clsstr) \
60 if (PTX::cls ## RegisterClass == TRC) return # clsstr;
61
Justin Holewinski1b91bcd2011-06-16 17:49:58 +000062 TEST_REGCLS(RegPred, pred);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +000063 TEST_REGCLS(RegI16, b16);
64 TEST_REGCLS(RegI32, b32);
65 TEST_REGCLS(RegI64, b64);
66 TEST_REGCLS(RegF32, b32);
67 TEST_REGCLS(RegF64, b64);
Che-Liang Chioudf659632010-11-08 03:06:08 +000068#undef TEST_REGCLS
69
70 llvm_unreachable("Not in any register class!");
71 return NULL;
72}
73
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000074static const char *getStateSpaceName(unsigned addressSpace) {
Che-Liang Chioud34f19f2010-12-30 10:41:27 +000075 switch (addressSpace) {
76 default: llvm_unreachable("Unknown state space");
Justin Holewinskif51b7e52011-09-30 14:36:36 +000077 case PTXStateSpace::Global: return "global";
78 case PTXStateSpace::Constant: return "const";
79 case PTXStateSpace::Local: return "local";
80 case PTXStateSpace::Parameter: return "param";
81 case PTXStateSpace::Shared: return "shared";
Che-Liang Chioud34f19f2010-12-30 10:41:27 +000082 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000083 return NULL;
84}
85
Chris Lattnerdb125cf2011-07-18 04:54:35 +000086static const char *getTypeName(Type* type) {
Che-Liang Chiouf7172022011-02-28 06:34:09 +000087 while (true) {
88 switch (type->getTypeID()) {
89 default: llvm_unreachable("Unknown type");
90 case Type::FloatTyID: return ".f32";
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000091 case Type::DoubleTyID: return ".f64";
92 case Type::IntegerTyID:
93 switch (type->getPrimitiveSizeInBits()) {
94 default: llvm_unreachable("Unknown integer bit-width");
95 case 16: return ".u16";
96 case 32: return ".u32";
97 case 64: return ".u64";
98 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +000099 case Type::ArrayTyID:
100 case Type::PointerTyID:
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000101 type = dyn_cast<SequentialType>(type)->getElementType();
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000102 break;
103 }
104 }
105 return NULL;
106}
107
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000108bool PTXAsmPrinter::doFinalization(Module &M) {
109 // XXX Temproarily remove global variables so that doFinalization() will not
110 // emit them again (global variables are emitted at beginning).
111
112 Module::GlobalListType &global_list = M.getGlobalList();
113 int i, n = global_list.size();
114 GlobalVariable **gv_array = new GlobalVariable* [n];
115
116 // first, back-up GlobalVariable in gv_array
117 i = 0;
118 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
119 I != E; ++I)
120 gv_array[i++] = &*I;
121
122 // second, empty global_list
123 while (!global_list.empty())
124 global_list.remove(global_list.begin());
125
126 // call doFinalization
127 bool ret = AsmPrinter::doFinalization(M);
128
129 // now we restore global variables
130 for (i = 0; i < n; i ++)
131 global_list.insert(global_list.end(), gv_array[i]);
132
133 delete[] gv_array;
134 return ret;
135}
136
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000137void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
138{
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000139 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
140
Justin Holewinskied0e4c82011-09-28 14:32:06 +0000141 // Emit the PTX .version and .target attributes
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000142 OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString()));
143 OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() +
Justin Holewinski12785e82011-03-03 13:34:29 +0000144 (ST.supportsDouble() ? ""
145 : ", map_f64_to_f32")));
Justin Holewinskia9c85f92011-06-22 00:43:56 +0000146 // .address_size directive is optional, but it must immediately follow
147 // the .target directive if present within a module
148 if (ST.supportsPTX23()) {
149 std::string addrSize = ST.is64Bit() ? "64" : "32";
150 OutStreamer.EmitRawText(Twine("\t.address_size " + addrSize));
151 }
152
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000153 OutStreamer.AddBlankLine();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000154
Justin Holewinski47997292011-06-24 19:19:18 +0000155 // Define any .file directives
156 DebugInfoFinder DbgFinder;
157 DbgFinder.processModule(M);
158
159 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
160 E = DbgFinder.compile_unit_end(); I != E; ++I) {
161 DICompileUnit DIUnit(*I);
162 StringRef FN = DIUnit.getFilename();
163 StringRef Dir = DIUnit.getDirectory();
164 GetOrCreateSourceID(FN, Dir);
165 }
166
167 OutStreamer.AddBlankLine();
168
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000169 // declare global variables
170 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
171 i != e; ++i)
172 EmitVariableDeclaration(i);
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000173}
174
Che-Liang Chioudf659632010-11-08 03:06:08 +0000175void PTXAsmPrinter::EmitFunctionBodyStart() {
176 OutStreamer.EmitRawText(Twine("{"));
177
178 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
Justin Holewinskie953a642011-09-23 14:31:12 +0000179 const PTXParamManager &PM = MFI->getParamManager();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000180
Justin Holewinski297984d2011-09-22 16:45:40 +0000181 // Print register definitions
182 std::string regDefs;
183 unsigned numRegs;
Che-Liang Chioudf659632010-11-08 03:06:08 +0000184
Justin Holewinski297984d2011-09-22 16:45:40 +0000185 // pred
186 numRegs = MFI->getNumRegistersForClass(PTX::RegPredRegisterClass);
187 if(numRegs > 0) {
188 regDefs += "\t.reg .pred %p<";
189 regDefs += utostr(numRegs);
190 regDefs += ">;\n";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000191 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000192
Justin Holewinski297984d2011-09-22 16:45:40 +0000193 // i16
194 numRegs = MFI->getNumRegistersForClass(PTX::RegI16RegisterClass);
195 if(numRegs > 0) {
196 regDefs += "\t.reg .b16 %rh<";
197 regDefs += utostr(numRegs);
198 regDefs += ">;\n";
199 }
200
201 // i32
202 numRegs = MFI->getNumRegistersForClass(PTX::RegI32RegisterClass);
203 if(numRegs > 0) {
204 regDefs += "\t.reg .b32 %r<";
205 regDefs += utostr(numRegs);
206 regDefs += ">;\n";
207 }
208
209 // i64
210 numRegs = MFI->getNumRegistersForClass(PTX::RegI64RegisterClass);
211 if(numRegs > 0) {
212 regDefs += "\t.reg .b64 %rd<";
213 regDefs += utostr(numRegs);
214 regDefs += ">;\n";
215 }
216
217 // f32
218 numRegs = MFI->getNumRegistersForClass(PTX::RegF32RegisterClass);
219 if(numRegs > 0) {
220 regDefs += "\t.reg .f32 %f<";
221 regDefs += utostr(numRegs);
222 regDefs += ">;\n";
223 }
224
225 // f64
226 numRegs = MFI->getNumRegistersForClass(PTX::RegF64RegisterClass);
227 if(numRegs > 0) {
228 regDefs += "\t.reg .f64 %fd<";
229 regDefs += utostr(numRegs);
230 regDefs += ">;\n";
231 }
232
Justin Holewinskie953a642011-09-23 14:31:12 +0000233 // Local params
234 for (PTXParamManager::param_iterator i = PM.local_begin(), e = PM.local_end();
235 i != e; ++i) {
236 regDefs += "\t.param .b";
237 regDefs += utostr(PM.getParamSize(*i));
238 regDefs += " ";
239 regDefs += PM.getParamName(*i);
240 regDefs += ";\n";
241 }
242
Justin Holewinski297984d2011-09-22 16:45:40 +0000243 OutStreamer.EmitRawText(Twine(regDefs));
244
245
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000246 const MachineFrameInfo* FrameInfo = MF->getFrameInfo();
Justin Holewinski08d03162011-06-22 16:07:03 +0000247 DEBUG(dbgs() << "Have " << FrameInfo->getNumObjects()
248 << " frame object(s)\n");
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000249 for (unsigned i = 0, e = FrameInfo->getNumObjects(); i != e; ++i) {
250 DEBUG(dbgs() << "Size of object: " << FrameInfo->getObjectSize(i) << "\n");
Justin Holewinski08d03162011-06-22 16:07:03 +0000251 if (FrameInfo->getObjectSize(i) > 0) {
Justin Holewinski58788502011-09-26 16:20:34 +0000252 std::string def = "\t.local .align ";
253 def += utostr(FrameInfo->getObjectAlignment(i));
Justin Holewinski63602ed2011-09-26 18:57:22 +0000254 def += " .b8";
Justin Holewinskic1d8fbd2011-09-26 16:20:28 +0000255 def += " __local";
Justin Holewinski08d03162011-06-22 16:07:03 +0000256 def += utostr(i);
Justin Holewinski63602ed2011-09-26 18:57:22 +0000257 def += "[";
258 def += utostr(FrameInfo->getObjectSize(i)); // Convert to bits
259 def += "]";
Justin Holewinski08d03162011-06-22 16:07:03 +0000260 def += ";";
261 OutStreamer.EmitRawText(Twine(def));
262 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000263 }
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000264
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000265 //unsigned Index = 1;
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000266 // Print parameter passing params
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000267 //for (PTXMachineFunctionInfo::param_iterator
268 // i = MFI->paramBegin(), e = MFI->paramEnd(); i != e; ++i) {
269 // std::string def = "\t.param .b";
270 // def += utostr(*i);
271 // def += " __ret_";
272 // def += utostr(Index);
273 // Index++;
274 // def += ";";
275 // OutStreamer.EmitRawText(Twine(def));
276 //}
Che-Liang Chioudf659632010-11-08 03:06:08 +0000277}
278
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000279void PTXAsmPrinter::EmitFunctionBodyEnd() {
280 OutStreamer.EmitRawText(Twine("}"));
281}
282
Eric Christopher50880d02010-09-18 18:52:28 +0000283void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000284 MCInst TmpInst;
285 LowerPTXMachineInstrToMCInst(MI, TmpInst, *this);
286 OutStreamer.EmitInstruction(TmpInst);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000287}
288
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000289void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
290 // Check to see if this is a special global used by LLVM, if so, emit it.
291 if (EmitSpecialLLVMGlobal(gv))
292 return;
293
294 MCSymbol *gvsym = Mang->getSymbol(gv);
295
296 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
297
298 std::string decl;
299
300 // check if it is defined in some other translation unit
301 if (gv->isDeclaration())
302 decl += ".extern ";
303
304 // state space: e.g., .global
305 decl += ".";
306 decl += getStateSpaceName(gv->getType()->getAddressSpace());
307 decl += " ";
308
309 // alignment (optional)
310 unsigned alignment = gv->getAlignment();
311 if (alignment != 0) {
312 decl += ".align ";
Justin Holewinskicfab2be2011-09-28 18:24:58 +0000313 decl += utostr(gv->getAlignment());
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000314 decl += " ";
315 }
316
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000317
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000318 if (PointerType::classof(gv->getType())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000319 PointerType* pointerTy = dyn_cast<PointerType>(gv->getType());
320 Type* elementTy = pointerTy->getElementType();
Dan Baileye1f38f22011-11-03 19:24:46 +0000321
Justin Holewinski9583a862011-04-28 00:19:50 +0000322 if (elementTy->isArrayTy())
323 {
324 assert(elementTy->isArrayTy() && "Only pointers to arrays are supported");
325
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000326 ArrayType* arrayTy = dyn_cast<ArrayType>(elementTy);
Justin Holewinski9583a862011-04-28 00:19:50 +0000327 elementTy = arrayTy->getElementType();
328
329 unsigned numElements = arrayTy->getNumElements();
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000330
Justin Holewinski9583a862011-04-28 00:19:50 +0000331 while (elementTy->isArrayTy()) {
332
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000333 arrayTy = dyn_cast<ArrayType>(elementTy);
Justin Holewinski9583a862011-04-28 00:19:50 +0000334 elementTy = arrayTy->getElementType();
335
336 numElements *= arrayTy->getNumElements();
337 }
338
339 // FIXME: isPrimitiveType() == false for i16?
340 assert(elementTy->isSingleValueType() &&
341 "Non-primitive types are not handled");
Dan Baileye1f38f22011-11-03 19:24:46 +0000342
343 // Find the size of the element in bits
344 unsigned elementSize = elementTy->getPrimitiveSizeInBits();
Justin Holewinski9583a862011-04-28 00:19:50 +0000345
Dan Baileye1f38f22011-11-03 19:24:46 +0000346 decl += ".b";
347 decl += utostr(elementSize);
348 decl += " ";
349 decl += gvsym->getName();
350 decl += "[";
351 decl += utostr(numElements);
352 decl += "]";
Justin Holewinski9583a862011-04-28 00:19:50 +0000353 }
Dan Baileye1f38f22011-11-03 19:24:46 +0000354 else
355 {
356 decl += ".b8 ";
357 decl += gvsym->getName();
358 decl += "[]";
359 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000360
Justin Holewinski9583a862011-04-28 00:19:50 +0000361 // handle string constants (assume ConstantArray means string)
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000362
Justin Holewinski9583a862011-04-28 00:19:50 +0000363 if (gv->hasInitializer())
364 {
Justin Holewinski297984d2011-09-22 16:45:40 +0000365 const Constant *C = gv->getInitializer();
Justin Holewinski9583a862011-04-28 00:19:50 +0000366 if (const ConstantArray *CA = dyn_cast<ConstantArray>(C))
367 {
368 decl += " = {";
369
370 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
371 {
372 if (i > 0) decl += ",";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000373
374 decl += "0x" +
375 utohexstr(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());
Justin Holewinski9583a862011-04-28 00:19:50 +0000376 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000377
Justin Holewinski9583a862011-04-28 00:19:50 +0000378 decl += "}";
379 }
380 }
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000381 }
382 else {
383 // Note: this is currently the fall-through case and most likely generates
384 // incorrect code.
385 decl += getTypeName(gv->getType());
386 decl += " ";
387
388 decl += gvsym->getName();
389
390 if (ArrayType::classof(gv->getType()) ||
391 PointerType::classof(gv->getType()))
392 decl += "[]";
393 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000394
395 decl += ";";
396
397 OutStreamer.EmitRawText(Twine(decl));
398
399 OutStreamer.AddBlankLine();
400}
401
Justin Holewinskied0e4c82011-09-28 14:32:06 +0000402void PTXAsmPrinter::EmitFunctionEntryLabel() {
Che-Liang Chioudf659632010-11-08 03:06:08 +0000403 // The function label could have already been emitted if two symbols end up
404 // conflicting due to asm renaming. Detect this and emit an error.
405 if (!CurrentFnSym->isUndefined()) {
406 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
407 "' label emitted multiple times to assembly file");
408 return;
409 }
410
411 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000412 const PTXParamManager &PM = MFI->getParamManager();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000413 const bool isKernel = MFI->isKernel();
Justin Holewinski67a91842011-06-23 18:10:03 +0000414 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000415 const MachineRegisterInfo& MRI = MF->getRegInfo();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000416
417 std::string decl = isKernel ? ".entry" : ".func";
418
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000419 if (!isKernel) {
420 decl += " (";
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000421 if (ST.useParamSpaceForDeviceArgs()) {
422 for (PTXParamManager::param_iterator i = PM.ret_begin(), e = PM.ret_end(),
423 b = i; i != e; ++i) {
424 if (i != b) {
425 decl += ", ";
426 }
427
428 decl += ".param .b";
429 decl += utostr(PM.getParamSize(*i));
430 decl += " ";
431 decl += PM.getParamName(*i);
432 }
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000433 } else {
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000434 for (PTXMachineFunctionInfo::reg_iterator
435 i = MFI->retreg_begin(), e = MFI->retreg_end(), b = i;
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000436 i != e; ++i) {
437 if (i != b) {
438 decl += ", ";
439 }
440 decl += ".reg .";
441 decl += getRegisterTypeName(*i, MRI);
442 decl += " ";
443 decl += MFI->getRegisterName(*i);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000444 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000445 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000446 decl += ")";
447 }
448
449 // Print function name
450 decl += " ";
451 decl += CurrentFnSym->getName().str();
452
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000453 decl += " (";
454
Justin Holewinski68226a42011-10-09 15:42:02 +0000455 const Function *F = MF->getFunction();
456
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000457 // Print parameters
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000458 if (isKernel || ST.useParamSpaceForDeviceArgs()) {
Justin Holewinski68226a42011-10-09 15:42:02 +0000459 /*for (PTXParamManager::param_iterator i = PM.arg_begin(), e = PM.arg_end(),
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000460 b = i; i != e; ++i) {
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000461 if (i != b) {
462 decl += ", ";
463 }
464
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000465 decl += ".param .b";
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000466 decl += utostr(PM.getParamSize(*i));
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000467 decl += " ";
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000468 decl += PM.getParamName(*i);
Justin Holewinski68226a42011-10-09 15:42:02 +0000469 }*/
470 int Counter = 1;
471 for (Function::const_arg_iterator i = F->arg_begin(), e = F->arg_end(),
472 b = i; i != e; ++i) {
473 if (i != b)
474 decl += ", ";
475 const Type *ArgType = (*i).getType();
476 decl += ".param .b";
477 if (ArgType->isPointerTy()) {
478 if (ST.is64Bit())
479 decl += "64";
480 else
481 decl += "32";
482 } else {
483 decl += utostr(ArgType->getPrimitiveSizeInBits());
484 }
485 if (ArgType->isPointerTy() && ST.emitPtrAttribute()) {
486 const PointerType *PtrType = dyn_cast<const PointerType>(ArgType);
487 decl += " .ptr";
488 switch (PtrType->getAddressSpace()) {
489 default:
490 llvm_unreachable("Unknown address space in argument");
491 case PTXStateSpace::Global:
492 decl += " .global";
493 break;
494 case PTXStateSpace::Shared:
495 decl += " .shared";
496 break;
497 }
498 }
499 decl += " __param_";
500 decl += utostr(Counter++);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000501 }
502 } else {
503 for (PTXMachineFunctionInfo::reg_iterator
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000504 i = MFI->argreg_begin(), e = MFI->argreg_end(), b = i;
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000505 i != e; ++i) {
506 if (i != b) {
507 decl += ", ";
508 }
509
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000510 decl += ".reg .";
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000511 decl += getRegisterTypeName(*i, MRI);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000512 decl += " ";
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000513 decl += MFI->getRegisterName(*i);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000514 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000515 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000516 decl += ")";
517
Che-Liang Chioudf659632010-11-08 03:06:08 +0000518 OutStreamer.EmitRawText(Twine(decl));
519}
520
Justin Holewinski47997292011-06-24 19:19:18 +0000521unsigned PTXAsmPrinter::GetOrCreateSourceID(StringRef FileName,
522 StringRef DirName) {
523 // If FE did not provide a file name, then assume stdin.
524 if (FileName.empty())
525 return GetOrCreateSourceID("<stdin>", StringRef());
526
527 // MCStream expects full path name as filename.
528 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
529 SmallString<128> FullPathName = DirName;
530 sys::path::append(FullPathName, FileName);
531 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
532 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
533 }
534
535 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
536 if (Entry.getValue())
537 return Entry.getValue();
538
539 unsigned SrcId = SourceIdMap.size();
540 Entry.setValue(SrcId);
541
542 // Print out a .file directive to specify files for .loc directives.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000543 OutStreamer.EmitDwarfFileDirective(SrcId, "", Entry.getKey());
Justin Holewinski47997292011-06-24 19:19:18 +0000544
545 return SrcId;
546}
547
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000548MCOperand PTXAsmPrinter::GetSymbolRef(const MachineOperand &MO,
549 const MCSymbol *Symbol) {
550 const MCExpr *Expr;
551 Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None, OutContext);
552 return MCOperand::CreateExpr(Expr);
553}
554
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000555MCOperand PTXAsmPrinter::lowerOperand(const MachineOperand &MO) {
556 MCOperand MCOp;
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000557 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
558 const MCExpr *Expr;
559 const char *RegSymbolName;
560 switch (MO.getType()) {
561 default:
562 llvm_unreachable("Unknown operand type");
563 case MachineOperand::MO_Register:
564 // We create register operands as symbols, since the PTXInstPrinter class
565 // has no way to map virtual registers back to a name without some ugly
566 // hacks.
567 // FIXME: Figure out a better way to handle virtual register naming.
568 RegSymbolName = MFI->getRegisterName(MO.getReg());
569 Expr = MCSymbolRefExpr::Create(RegSymbolName, MCSymbolRefExpr::VK_None,
570 OutContext);
571 MCOp = MCOperand::CreateExpr(Expr);
572 break;
573 case MachineOperand::MO_Immediate:
574 MCOp = MCOperand::CreateImm(MO.getImm());
575 break;
576 case MachineOperand::MO_MachineBasicBlock:
577 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
578 MO.getMBB()->getSymbol(), OutContext));
579 break;
580 case MachineOperand::MO_GlobalAddress:
581 MCOp = GetSymbolRef(MO, Mang->getSymbol(MO.getGlobal()));
582 break;
583 case MachineOperand::MO_ExternalSymbol:
584 MCOp = GetSymbolRef(MO, GetExternalSymbolSymbol(MO.getSymbolName()));
585 break;
586 case MachineOperand::MO_FPImmediate:
587 APFloat Val = MO.getFPImm()->getValueAPF();
588 bool ignored;
589 Val.convert(APFloat::IEEEdouble, APFloat::rmTowardZero, &ignored);
590 MCOp = MCOperand::CreateFPImm(Val.convertToDouble());
591 break;
592 }
593
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000594 return MCOp;
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000595}
Eric Christopher50880d02010-09-18 18:52:28 +0000596
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000597// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000598extern "C" void LLVMInitializePTXAsmPrinter() {
Justin Holewinskie1fee482011-04-20 15:37:17 +0000599 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target);
600 RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target);
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000601}