blob: bdf238b1b049dd1a0bf3e93462573395ed4697b2 [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/Twine.h"
Justin Holewinski47997292011-06-24 19:19:18 +000029#include "llvm/Analysis/DebugInfo.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000030#include "llvm/CodeGen/AsmPrinter.h"
Justin Holewinskidf1c8d82011-06-20 15:56:20 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000032#include "llvm/CodeGen/MachineInstr.h"
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Justin Holewinski47997292011-06-24 19:19:18 +000034#include "llvm/MC/MCContext.h"
Justin Holewinskid8e4ed22011-09-28 14:32:04 +000035#include "llvm/MC/MCExpr.h"
36#include "llvm/MC/MCInst.h"
Eric Christopher50880d02010-09-18 18:52:28 +000037#include "llvm/MC/MCStreamer.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000038#include "llvm/MC/MCSymbol.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000039#include "llvm/Target/Mangler.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000040#include "llvm/Target/TargetLoweringObjectFile.h"
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000041#include "llvm/Support/CommandLine.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000042#include "llvm/Support/Debug.h"
43#include "llvm/Support/ErrorHandling.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000044#include "llvm/Support/MathExtras.h"
Justin Holewinski47997292011-06-24 19:19:18 +000045#include "llvm/Support/Path.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000046#include "llvm/Support/TargetRegistry.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000047#include "llvm/Support/raw_ostream.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000048
49using namespace llvm;
50
Che-Liang Chioudf659632010-11-08 03:06:08 +000051static const char PARAM_PREFIX[] = "__param_";
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +000052static const char RETURN_PREFIX[] = "__ret_";
Che-Liang Chioudf659632010-11-08 03:06:08 +000053
Justin Holewinski5422a0f2011-09-22 16:45:46 +000054static const char *getRegisterTypeName(unsigned RegNo,
55 const MachineRegisterInfo& MRI) {
56 const TargetRegisterClass *TRC = MRI.getRegClass(RegNo);
57
58#define TEST_REGCLS(cls, clsstr) \
59 if (PTX::cls ## RegisterClass == TRC) return # clsstr;
60
Justin Holewinski1b91bcd2011-06-16 17:49:58 +000061 TEST_REGCLS(RegPred, pred);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +000062 TEST_REGCLS(RegI16, b16);
63 TEST_REGCLS(RegI32, b32);
64 TEST_REGCLS(RegI64, b64);
65 TEST_REGCLS(RegF32, b32);
66 TEST_REGCLS(RegF64, b64);
Che-Liang Chioudf659632010-11-08 03:06:08 +000067#undef TEST_REGCLS
68
69 llvm_unreachable("Not in any register class!");
70 return NULL;
71}
72
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000073static const char *getStateSpaceName(unsigned addressSpace) {
Che-Liang Chioud34f19f2010-12-30 10:41:27 +000074 switch (addressSpace) {
75 default: llvm_unreachable("Unknown state space");
Justin Holewinskif51b7e52011-09-30 14:36:36 +000076 case PTXStateSpace::Global: return "global";
77 case PTXStateSpace::Constant: return "const";
78 case PTXStateSpace::Local: return "local";
79 case PTXStateSpace::Parameter: return "param";
80 case PTXStateSpace::Shared: return "shared";
Che-Liang Chioud34f19f2010-12-30 10:41:27 +000081 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000082 return NULL;
83}
84
Chris Lattnerdb125cf2011-07-18 04:54:35 +000085static const char *getTypeName(Type* type) {
Che-Liang Chiouf7172022011-02-28 06:34:09 +000086 while (true) {
87 switch (type->getTypeID()) {
88 default: llvm_unreachable("Unknown type");
89 case Type::FloatTyID: return ".f32";
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000090 case Type::DoubleTyID: return ".f64";
91 case Type::IntegerTyID:
92 switch (type->getPrimitiveSizeInBits()) {
93 default: llvm_unreachable("Unknown integer bit-width");
94 case 16: return ".u16";
95 case 32: return ".u32";
96 case 64: return ".u64";
97 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +000098 case Type::ArrayTyID:
99 case Type::PointerTyID:
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000100 type = dyn_cast<SequentialType>(type)->getElementType();
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000101 break;
102 }
103 }
104 return NULL;
105}
106
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000107bool PTXAsmPrinter::doFinalization(Module &M) {
108 // XXX Temproarily remove global variables so that doFinalization() will not
109 // emit them again (global variables are emitted at beginning).
110
111 Module::GlobalListType &global_list = M.getGlobalList();
112 int i, n = global_list.size();
113 GlobalVariable **gv_array = new GlobalVariable* [n];
114
115 // first, back-up GlobalVariable in gv_array
116 i = 0;
117 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
118 I != E; ++I)
119 gv_array[i++] = &*I;
120
121 // second, empty global_list
122 while (!global_list.empty())
123 global_list.remove(global_list.begin());
124
125 // call doFinalization
126 bool ret = AsmPrinter::doFinalization(M);
127
128 // now we restore global variables
129 for (i = 0; i < n; i ++)
130 global_list.insert(global_list.end(), gv_array[i]);
131
132 delete[] gv_array;
133 return ret;
134}
135
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000136void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
137{
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000138 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
139
Justin Holewinskied0e4c82011-09-28 14:32:06 +0000140 // Emit the PTX .version and .target attributes
Benjamin Kramer055a6472011-11-07 21:00:43 +0000141 OutStreamer.EmitRawText(Twine("\t.version ") + ST.getPTXVersionString());
142 OutStreamer.EmitRawText(Twine("\t.target ") + ST.getTargetString() +
Justin Holewinski12785e82011-03-03 13:34:29 +0000143 (ST.supportsDouble() ? ""
Benjamin Kramer055a6472011-11-07 21:00:43 +0000144 : ", map_f64_to_f32"));
Justin Holewinskia9c85f92011-06-22 00:43:56 +0000145 // .address_size directive is optional, but it must immediately follow
146 // the .target directive if present within a module
147 if (ST.supportsPTX23()) {
Benjamin Kramer055a6472011-11-07 21:00:43 +0000148 const char *addrSize = ST.is64Bit() ? "64" : "32";
149 OutStreamer.EmitRawText(Twine("\t.address_size ") + addrSize);
Justin Holewinskia9c85f92011-06-22 00:43:56 +0000150 }
151
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000152 OutStreamer.AddBlankLine();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000153
Justin Holewinski47997292011-06-24 19:19:18 +0000154 // Define any .file directives
155 DebugInfoFinder DbgFinder;
156 DbgFinder.processModule(M);
157
158 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
159 E = DbgFinder.compile_unit_end(); I != E; ++I) {
160 DICompileUnit DIUnit(*I);
161 StringRef FN = DIUnit.getFilename();
162 StringRef Dir = DIUnit.getDirectory();
163 GetOrCreateSourceID(FN, Dir);
164 }
165
166 OutStreamer.AddBlankLine();
167
Dan Bailey96e64582011-11-11 14:45:12 +0000168 // declare external functions
169 for (Module::const_iterator i = M.begin(), e = M.end();
170 i != e; ++i)
171 EmitFunctionDeclaration(i);
172
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000173 // declare global variables
174 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
175 i != e; ++i)
176 EmitVariableDeclaration(i);
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000177}
178
Che-Liang Chioudf659632010-11-08 03:06:08 +0000179void PTXAsmPrinter::EmitFunctionBodyStart() {
180 OutStreamer.EmitRawText(Twine("{"));
181
182 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
Justin Holewinskie953a642011-09-23 14:31:12 +0000183 const PTXParamManager &PM = MFI->getParamManager();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000184
Justin Holewinski297984d2011-09-22 16:45:40 +0000185 // Print register definitions
Benjamin Kramer055a6472011-11-07 21:00:43 +0000186 SmallString<128> regDefs;
187 raw_svector_ostream os(regDefs);
Justin Holewinski297984d2011-09-22 16:45:40 +0000188 unsigned numRegs;
Che-Liang Chioudf659632010-11-08 03:06:08 +0000189
Justin Holewinski297984d2011-09-22 16:45:40 +0000190 // pred
191 numRegs = MFI->getNumRegistersForClass(PTX::RegPredRegisterClass);
Benjamin Kramer055a6472011-11-07 21:00:43 +0000192 if(numRegs > 0)
193 os << "\t.reg .pred %p<" << numRegs << ">;\n";
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000194
Justin Holewinski297984d2011-09-22 16:45:40 +0000195 // i16
196 numRegs = MFI->getNumRegistersForClass(PTX::RegI16RegisterClass);
Benjamin Kramer055a6472011-11-07 21:00:43 +0000197 if(numRegs > 0)
198 os << "\t.reg .b16 %rh<" << numRegs << ">;\n";
Justin Holewinski297984d2011-09-22 16:45:40 +0000199
200 // i32
201 numRegs = MFI->getNumRegistersForClass(PTX::RegI32RegisterClass);
Benjamin Kramer055a6472011-11-07 21:00:43 +0000202 if(numRegs > 0)
203 os << "\t.reg .b32 %r<" << numRegs << ">;\n";
Justin Holewinski297984d2011-09-22 16:45:40 +0000204
205 // i64
206 numRegs = MFI->getNumRegistersForClass(PTX::RegI64RegisterClass);
Benjamin Kramer055a6472011-11-07 21:00:43 +0000207 if(numRegs > 0)
208 os << "\t.reg .b64 %rd<" << numRegs << ">;\n";
Justin Holewinski297984d2011-09-22 16:45:40 +0000209
210 // f32
211 numRegs = MFI->getNumRegistersForClass(PTX::RegF32RegisterClass);
Benjamin Kramer055a6472011-11-07 21:00:43 +0000212 if(numRegs > 0)
213 os << "\t.reg .f32 %f<" << numRegs << ">;\n";
Justin Holewinski297984d2011-09-22 16:45:40 +0000214
215 // f64
216 numRegs = MFI->getNumRegistersForClass(PTX::RegF64RegisterClass);
Benjamin Kramer055a6472011-11-07 21:00:43 +0000217 if(numRegs > 0)
218 os << "\t.reg .f64 %fd<" << numRegs << ">;\n";
Justin Holewinski297984d2011-09-22 16:45:40 +0000219
Justin Holewinskie953a642011-09-23 14:31:12 +0000220 // Local params
221 for (PTXParamManager::param_iterator i = PM.local_begin(), e = PM.local_end();
Benjamin Kramer055a6472011-11-07 21:00:43 +0000222 i != e; ++i)
223 os << "\t.param .b" << PM.getParamSize(*i) << ' ' << PM.getParamName(*i)
224 << ";\n";
Justin Holewinskie953a642011-09-23 14:31:12 +0000225
Benjamin Kramer055a6472011-11-07 21:00:43 +0000226 OutStreamer.EmitRawText(os.str());
Justin Holewinski297984d2011-09-22 16:45:40 +0000227
228
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000229 const MachineFrameInfo* FrameInfo = MF->getFrameInfo();
Justin Holewinski08d03162011-06-22 16:07:03 +0000230 DEBUG(dbgs() << "Have " << FrameInfo->getNumObjects()
231 << " frame object(s)\n");
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000232 for (unsigned i = 0, e = FrameInfo->getNumObjects(); i != e; ++i) {
233 DEBUG(dbgs() << "Size of object: " << FrameInfo->getObjectSize(i) << "\n");
Justin Holewinski08d03162011-06-22 16:07:03 +0000234 if (FrameInfo->getObjectSize(i) > 0) {
Benjamin Kramer055a6472011-11-07 21:00:43 +0000235 OutStreamer.EmitRawText("\t.local .align " +
236 Twine(FrameInfo->getObjectAlignment(i)) +
237 " .b8 __local" +
238 Twine(i) +
239 "[" +
240 Twine(FrameInfo->getObjectSize(i)) +
241 "];");
Justin Holewinski08d03162011-06-22 16:07:03 +0000242 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000243 }
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000244
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000245 //unsigned Index = 1;
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000246 // Print parameter passing params
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000247 //for (PTXMachineFunctionInfo::param_iterator
248 // i = MFI->paramBegin(), e = MFI->paramEnd(); i != e; ++i) {
249 // std::string def = "\t.param .b";
250 // def += utostr(*i);
251 // def += " __ret_";
252 // def += utostr(Index);
253 // Index++;
254 // def += ";";
255 // OutStreamer.EmitRawText(Twine(def));
256 //}
Che-Liang Chioudf659632010-11-08 03:06:08 +0000257}
258
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000259void PTXAsmPrinter::EmitFunctionBodyEnd() {
260 OutStreamer.EmitRawText(Twine("}"));
261}
262
Eric Christopher50880d02010-09-18 18:52:28 +0000263void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000264 MCInst TmpInst;
265 LowerPTXMachineInstrToMCInst(MI, TmpInst, *this);
266 OutStreamer.EmitInstruction(TmpInst);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000267}
268
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000269void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
270 // Check to see if this is a special global used by LLVM, if so, emit it.
271 if (EmitSpecialLLVMGlobal(gv))
272 return;
273
274 MCSymbol *gvsym = Mang->getSymbol(gv);
275
276 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
277
Benjamin Kramer055a6472011-11-07 21:00:43 +0000278 SmallString<128> decl;
279 raw_svector_ostream os(decl);
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000280
281 // check if it is defined in some other translation unit
282 if (gv->isDeclaration())
Benjamin Kramer055a6472011-11-07 21:00:43 +0000283 os << ".extern ";
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000284
285 // state space: e.g., .global
Benjamin Kramer055a6472011-11-07 21:00:43 +0000286 os << '.' << getStateSpaceName(gv->getType()->getAddressSpace()) << ' ';
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000287
288 // alignment (optional)
289 unsigned alignment = gv->getAlignment();
Benjamin Kramer055a6472011-11-07 21:00:43 +0000290 if (alignment != 0)
291 os << ".align " << gv->getAlignment() << ' ';
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000292
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000293
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000294 if (PointerType::classof(gv->getType())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000295 PointerType* pointerTy = dyn_cast<PointerType>(gv->getType());
296 Type* elementTy = pointerTy->getElementType();
Benjamin Kramer055a6472011-11-07 21:00:43 +0000297
298 if (elementTy->isArrayTy()) {
Justin Holewinski9583a862011-04-28 00:19:50 +0000299 assert(elementTy->isArrayTy() && "Only pointers to arrays are supported");
300
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000301 ArrayType* arrayTy = dyn_cast<ArrayType>(elementTy);
Justin Holewinski9583a862011-04-28 00:19:50 +0000302 elementTy = arrayTy->getElementType();
303
304 unsigned numElements = arrayTy->getNumElements();
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000305
Justin Holewinski9583a862011-04-28 00:19:50 +0000306 while (elementTy->isArrayTy()) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000307 arrayTy = dyn_cast<ArrayType>(elementTy);
Justin Holewinski9583a862011-04-28 00:19:50 +0000308 elementTy = arrayTy->getElementType();
309
310 numElements *= arrayTy->getNumElements();
311 }
312
313 // FIXME: isPrimitiveType() == false for i16?
314 assert(elementTy->isSingleValueType() &&
Benjamin Kramer055a6472011-11-07 21:00:43 +0000315 "Non-primitive types are not handled");
316
Dan Baileye1f38f22011-11-03 19:24:46 +0000317 // Find the size of the element in bits
318 unsigned elementSize = elementTy->getPrimitiveSizeInBits();
Justin Holewinski9583a862011-04-28 00:19:50 +0000319
Benjamin Kramer055a6472011-11-07 21:00:43 +0000320 os << ".b" << elementSize << ' ' << gvsym->getName()
321 << '[' << numElements << ']';
322 } else {
323 os << ".b8" << gvsym->getName() << "[]";
Dan Baileye1f38f22011-11-03 19:24:46 +0000324 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000325
Justin Holewinski9583a862011-04-28 00:19:50 +0000326 // handle string constants (assume ConstantArray means string)
Benjamin Kramer055a6472011-11-07 21:00:43 +0000327 if (gv->hasInitializer()) {
Justin Holewinski297984d2011-09-22 16:45:40 +0000328 const Constant *C = gv->getInitializer();
Benjamin Kramer055a6472011-11-07 21:00:43 +0000329 if (const ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
330 os << " = {";
Justin Holewinski9583a862011-04-28 00:19:50 +0000331
Benjamin Kramer055a6472011-11-07 21:00:43 +0000332 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
333 if (i > 0)
334 os << ',';
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000335
Benjamin Kramer055a6472011-11-07 21:00:43 +0000336 os << "0x";
337 os.write_hex(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());
Justin Holewinski9583a862011-04-28 00:19:50 +0000338 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000339
Benjamin Kramer055a6472011-11-07 21:00:43 +0000340 os << '}';
Justin Holewinski9583a862011-04-28 00:19:50 +0000341 }
342 }
Benjamin Kramer055a6472011-11-07 21:00:43 +0000343 } else {
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000344 // Note: this is currently the fall-through case and most likely generates
345 // incorrect code.
Benjamin Kramer055a6472011-11-07 21:00:43 +0000346 os << getTypeName(gv->getType()) << ' ' << gvsym->getName();
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000347
Benjamin Kramer055a6472011-11-07 21:00:43 +0000348 if (isa<ArrayType>(gv->getType()) || isa<PointerType>(gv->getType()))
349 os << "[]";
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000350 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000351
Benjamin Kramer055a6472011-11-07 21:00:43 +0000352 os << ';';
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000353
Benjamin Kramer055a6472011-11-07 21:00:43 +0000354 OutStreamer.EmitRawText(os.str());
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000355 OutStreamer.AddBlankLine();
356}
357
Justin Holewinskied0e4c82011-09-28 14:32:06 +0000358void PTXAsmPrinter::EmitFunctionEntryLabel() {
Che-Liang Chioudf659632010-11-08 03:06:08 +0000359 // The function label could have already been emitted if two symbols end up
360 // conflicting due to asm renaming. Detect this and emit an error.
361 if (!CurrentFnSym->isUndefined()) {
362 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
363 "' label emitted multiple times to assembly file");
364 return;
365 }
366
367 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000368 const PTXParamManager &PM = MFI->getParamManager();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000369 const bool isKernel = MFI->isKernel();
Justin Holewinski67a91842011-06-23 18:10:03 +0000370 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000371 const MachineRegisterInfo& MRI = MF->getRegInfo();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000372
Benjamin Kramer055a6472011-11-07 21:00:43 +0000373 SmallString<128> decl;
374 raw_svector_ostream os(decl);
375 os << (isKernel ? ".entry" : ".func");
Che-Liang Chioudf659632010-11-08 03:06:08 +0000376
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000377 if (!isKernel) {
Benjamin Kramer055a6472011-11-07 21:00:43 +0000378 os << " (";
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000379 if (ST.useParamSpaceForDeviceArgs()) {
380 for (PTXParamManager::param_iterator i = PM.ret_begin(), e = PM.ret_end(),
381 b = i; i != e; ++i) {
Benjamin Kramer055a6472011-11-07 21:00:43 +0000382 if (i != b)
383 os << ", ";
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000384
Benjamin Kramer055a6472011-11-07 21:00:43 +0000385 os << ".param .b" << PM.getParamSize(*i) << ' ' << PM.getParamName(*i);
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000386 }
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000387 } else {
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000388 for (PTXMachineFunctionInfo::reg_iterator
389 i = MFI->retreg_begin(), e = MFI->retreg_end(), b = i;
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000390 i != e; ++i) {
Benjamin Kramer055a6472011-11-07 21:00:43 +0000391 if (i != b)
392 os << ", ";
393
394 os << ".reg ." << getRegisterTypeName(*i, MRI) << ' '
395 << MFI->getRegisterName(*i);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000396 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000397 }
Benjamin Kramer055a6472011-11-07 21:00:43 +0000398 os << ')';
Che-Liang Chioudf659632010-11-08 03:06:08 +0000399 }
400
401 // Print function name
Benjamin Kramer055a6472011-11-07 21:00:43 +0000402 os << ' ' << CurrentFnSym->getName() << " (";
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000403
Justin Holewinski68226a42011-10-09 15:42:02 +0000404 const Function *F = MF->getFunction();
405
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000406 // Print parameters
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000407 if (isKernel || ST.useParamSpaceForDeviceArgs()) {
Justin Holewinski68226a42011-10-09 15:42:02 +0000408 /*for (PTXParamManager::param_iterator i = PM.arg_begin(), e = PM.arg_end(),
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000409 b = i; i != e; ++i) {
Benjamin Kramer055a6472011-11-07 21:00:43 +0000410 if (i != b)
411 os << ", ";
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000412
Benjamin Kramer055a6472011-11-07 21:00:43 +0000413 os << ".param .b" << PM.getParamSize(*i) << ' ' << PM.getParamName(*i);
Justin Holewinski68226a42011-10-09 15:42:02 +0000414 }*/
415 int Counter = 1;
416 for (Function::const_arg_iterator i = F->arg_begin(), e = F->arg_end(),
417 b = i; i != e; ++i) {
418 if (i != b)
Benjamin Kramer055a6472011-11-07 21:00:43 +0000419 os << ", ";
Justin Holewinski68226a42011-10-09 15:42:02 +0000420 const Type *ArgType = (*i).getType();
Benjamin Kramer055a6472011-11-07 21:00:43 +0000421 os << ".param .b";
Justin Holewinski68226a42011-10-09 15:42:02 +0000422 if (ArgType->isPointerTy()) {
423 if (ST.is64Bit())
Benjamin Kramer055a6472011-11-07 21:00:43 +0000424 os << "64";
Justin Holewinski68226a42011-10-09 15:42:02 +0000425 else
Benjamin Kramer055a6472011-11-07 21:00:43 +0000426 os << "32";
Justin Holewinski68226a42011-10-09 15:42:02 +0000427 } else {
Benjamin Kramer055a6472011-11-07 21:00:43 +0000428 os << ArgType->getPrimitiveSizeInBits();
Justin Holewinski68226a42011-10-09 15:42:02 +0000429 }
430 if (ArgType->isPointerTy() && ST.emitPtrAttribute()) {
431 const PointerType *PtrType = dyn_cast<const PointerType>(ArgType);
Benjamin Kramer055a6472011-11-07 21:00:43 +0000432 os << " .ptr";
Justin Holewinski68226a42011-10-09 15:42:02 +0000433 switch (PtrType->getAddressSpace()) {
434 default:
435 llvm_unreachable("Unknown address space in argument");
436 case PTXStateSpace::Global:
Benjamin Kramer055a6472011-11-07 21:00:43 +0000437 os << " .global";
Justin Holewinski68226a42011-10-09 15:42:02 +0000438 break;
439 case PTXStateSpace::Shared:
Benjamin Kramer055a6472011-11-07 21:00:43 +0000440 os << " .shared";
Justin Holewinski68226a42011-10-09 15:42:02 +0000441 break;
442 }
443 }
Benjamin Kramer055a6472011-11-07 21:00:43 +0000444 os << " __param_" << Counter++;
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000445 }
446 } else {
447 for (PTXMachineFunctionInfo::reg_iterator
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000448 i = MFI->argreg_begin(), e = MFI->argreg_end(), b = i;
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000449 i != e; ++i) {
Benjamin Kramer055a6472011-11-07 21:00:43 +0000450 if (i != b)
451 os << ", ";
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000452
Benjamin Kramer055a6472011-11-07 21:00:43 +0000453 os << ".reg ." << getRegisterTypeName(*i, MRI) << ' '
454 << MFI->getRegisterName(*i);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000455 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000456 }
Benjamin Kramer055a6472011-11-07 21:00:43 +0000457 os << ')';
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000458
Benjamin Kramer055a6472011-11-07 21:00:43 +0000459 OutStreamer.EmitRawText(os.str());
Che-Liang Chioudf659632010-11-08 03:06:08 +0000460}
461
Dan Bailey96e64582011-11-11 14:45:12 +0000462void PTXAsmPrinter::EmitFunctionDeclaration(const Function* func)
463{
464 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
465
466 std::string decl = "";
467
468 // hard-coded emission of extern vprintf function
469
470 if (func->getName() == "printf" || func->getName() == "puts") {
471 decl += ".extern .func (.param .b32 __param_1) vprintf (.param .b";
472 if (ST.is64Bit())
473 decl += "64";
474 else
475 decl += "32";
476 decl += " __param_2, .param .b";
477 if (ST.is64Bit())
478 decl += "64";
479 else
480 decl += "32";
481 decl += " __param_3)\n";
482 }
483
484 OutStreamer.EmitRawText(Twine(decl));
485}
486
Justin Holewinski47997292011-06-24 19:19:18 +0000487unsigned PTXAsmPrinter::GetOrCreateSourceID(StringRef FileName,
488 StringRef DirName) {
489 // If FE did not provide a file name, then assume stdin.
490 if (FileName.empty())
491 return GetOrCreateSourceID("<stdin>", StringRef());
492
493 // MCStream expects full path name as filename.
494 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
495 SmallString<128> FullPathName = DirName;
496 sys::path::append(FullPathName, FileName);
497 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
498 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
499 }
500
501 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
502 if (Entry.getValue())
503 return Entry.getValue();
504
505 unsigned SrcId = SourceIdMap.size();
506 Entry.setValue(SrcId);
507
508 // Print out a .file directive to specify files for .loc directives.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000509 OutStreamer.EmitDwarfFileDirective(SrcId, "", Entry.getKey());
Justin Holewinski47997292011-06-24 19:19:18 +0000510
511 return SrcId;
512}
513
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000514MCOperand PTXAsmPrinter::GetSymbolRef(const MachineOperand &MO,
515 const MCSymbol *Symbol) {
516 const MCExpr *Expr;
517 Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None, OutContext);
518 return MCOperand::CreateExpr(Expr);
519}
520
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000521MCOperand PTXAsmPrinter::lowerOperand(const MachineOperand &MO) {
522 MCOperand MCOp;
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000523 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
524 const MCExpr *Expr;
525 const char *RegSymbolName;
526 switch (MO.getType()) {
527 default:
528 llvm_unreachable("Unknown operand type");
529 case MachineOperand::MO_Register:
530 // We create register operands as symbols, since the PTXInstPrinter class
531 // has no way to map virtual registers back to a name without some ugly
532 // hacks.
533 // FIXME: Figure out a better way to handle virtual register naming.
534 RegSymbolName = MFI->getRegisterName(MO.getReg());
535 Expr = MCSymbolRefExpr::Create(RegSymbolName, MCSymbolRefExpr::VK_None,
536 OutContext);
537 MCOp = MCOperand::CreateExpr(Expr);
538 break;
539 case MachineOperand::MO_Immediate:
540 MCOp = MCOperand::CreateImm(MO.getImm());
541 break;
542 case MachineOperand::MO_MachineBasicBlock:
543 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
544 MO.getMBB()->getSymbol(), OutContext));
545 break;
546 case MachineOperand::MO_GlobalAddress:
547 MCOp = GetSymbolRef(MO, Mang->getSymbol(MO.getGlobal()));
548 break;
549 case MachineOperand::MO_ExternalSymbol:
550 MCOp = GetSymbolRef(MO, GetExternalSymbolSymbol(MO.getSymbolName()));
551 break;
552 case MachineOperand::MO_FPImmediate:
553 APFloat Val = MO.getFPImm()->getValueAPF();
554 bool ignored;
555 Val.convert(APFloat::IEEEdouble, APFloat::rmTowardZero, &ignored);
556 MCOp = MCOperand::CreateFPImm(Val.convertToDouble());
557 break;
558 }
559
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000560 return MCOp;
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000561}
Eric Christopher50880d02010-09-18 18:52:28 +0000562
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000563// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000564extern "C" void LLVMInitializePTXAsmPrinter() {
Justin Holewinskie1fee482011-04-20 15:37:17 +0000565 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target);
566 RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target);
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000567}