blob: 87b390383bf0bf70f59ea339f71e78c573e44112 [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"
Che-Liang Chioudf659632010-11-08 03:06:08 +000018#include "PTXMachineFunctionInfo.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000019#include "PTXTargetMachine.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000020#include "llvm/DerivedTypes.h"
21#include "llvm/Module.h"
Eric Christopher50880d02010-09-18 18:52:28 +000022#include "llvm/ADT/SmallString.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000023#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/Twine.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000025#include "llvm/CodeGen/AsmPrinter.h"
Justin Holewinskidf1c8d82011-06-20 15:56:20 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000027#include "llvm/CodeGen/MachineInstr.h"
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000029#include "llvm/MC/MCStreamer.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000030#include "llvm/MC/MCSymbol.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000031#include "llvm/Target/Mangler.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000033#include "llvm/Target/TargetRegistry.h"
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000034#include "llvm/Support/CommandLine.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000035#include "llvm/Support/Debug.h"
36#include "llvm/Support/ErrorHandling.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000037#include "llvm/Support/MathExtras.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000038#include "llvm/Support/raw_ostream.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000039
40using namespace llvm;
41
42namespace {
Che-Liang Chioudf659632010-11-08 03:06:08 +000043class PTXAsmPrinter : public AsmPrinter {
44public:
45 explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
46 : AsmPrinter(TM, Streamer) {}
Eric Christopher50880d02010-09-18 18:52:28 +000047
Che-Liang Chioudf659632010-11-08 03:06:08 +000048 const char *getPassName() const { return "PTX Assembly Printer"; }
Eric Christopher50880d02010-09-18 18:52:28 +000049
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000050 bool doFinalization(Module &M);
51
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000052 virtual void EmitStartOfAsmFile(Module &M);
53
Che-Liang Chioudf659632010-11-08 03:06:08 +000054 virtual bool runOnMachineFunction(MachineFunction &MF);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000055
Che-Liang Chioudf659632010-11-08 03:06:08 +000056 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);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000062 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
63 const char *Modifier = 0);
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +000064 void printParamOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
65 const char *Modifier = 0);
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +000066 void printReturnOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
67 const char *Modifier = 0);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +000068 void printPredicateOperand(const MachineInstr *MI, raw_ostream &O);
Che-Liang Chioudf659632010-11-08 03:06:08 +000069
70 // autogen'd.
71 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
72 static const char *getRegisterName(unsigned RegNo);
73
74private:
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000075 void EmitVariableDeclaration(const GlobalVariable *gv);
Che-Liang Chioudf659632010-11-08 03:06:08 +000076 void EmitFunctionDeclaration();
77}; // class PTXAsmPrinter
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000078} // namespace
79
Che-Liang Chioudf659632010-11-08 03:06:08 +000080static const char PARAM_PREFIX[] = "__param_";
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +000081static const char RETURN_PREFIX[] = "__ret_";
Che-Liang Chioudf659632010-11-08 03:06:08 +000082
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000083static const char *getRegisterTypeName(unsigned RegNo) {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000084#define TEST_REGCLS(cls, clsstr) \
Che-Liang Chioudf659632010-11-08 03:06:08 +000085 if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr;
Justin Holewinski1b91bcd2011-06-16 17:49:58 +000086 TEST_REGCLS(RegPred, pred);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +000087 TEST_REGCLS(RegI16, b16);
88 TEST_REGCLS(RegI32, b32);
89 TEST_REGCLS(RegI64, b64);
90 TEST_REGCLS(RegF32, b32);
91 TEST_REGCLS(RegF64, b64);
Che-Liang Chioudf659632010-11-08 03:06:08 +000092#undef TEST_REGCLS
93
94 llvm_unreachable("Not in any register class!");
95 return NULL;
96}
97
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000098static const char *getStateSpaceName(unsigned addressSpace) {
Che-Liang Chioud34f19f2010-12-30 10:41:27 +000099 switch (addressSpace) {
100 default: llvm_unreachable("Unknown state space");
101 case PTX::GLOBAL: return "global";
102 case PTX::CONSTANT: return "const";
103 case PTX::LOCAL: return "local";
104 case PTX::PARAMETER: return "param";
105 case PTX::SHARED: return "shared";
106 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000107 return NULL;
108}
109
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000110static const char *getTypeName(const Type* type) {
111 while (true) {
112 switch (type->getTypeID()) {
113 default: llvm_unreachable("Unknown type");
114 case Type::FloatTyID: return ".f32";
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000115 case Type::DoubleTyID: return ".f64";
116 case Type::IntegerTyID:
117 switch (type->getPrimitiveSizeInBits()) {
118 default: llvm_unreachable("Unknown integer bit-width");
119 case 16: return ".u16";
120 case 32: return ".u32";
121 case 64: return ".u64";
122 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000123 case Type::ArrayTyID:
124 case Type::PointerTyID:
125 type = dyn_cast<const SequentialType>(type)->getElementType();
126 break;
127 }
128 }
129 return NULL;
130}
131
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000132bool PTXAsmPrinter::doFinalization(Module &M) {
133 // XXX Temproarily remove global variables so that doFinalization() will not
134 // emit them again (global variables are emitted at beginning).
135
136 Module::GlobalListType &global_list = M.getGlobalList();
137 int i, n = global_list.size();
138 GlobalVariable **gv_array = new GlobalVariable* [n];
139
140 // first, back-up GlobalVariable in gv_array
141 i = 0;
142 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
143 I != E; ++I)
144 gv_array[i++] = &*I;
145
146 // second, empty global_list
147 while (!global_list.empty())
148 global_list.remove(global_list.begin());
149
150 // call doFinalization
151 bool ret = AsmPrinter::doFinalization(M);
152
153 // now we restore global variables
154 for (i = 0; i < n; i ++)
155 global_list.insert(global_list.end(), gv_array[i]);
156
157 delete[] gv_array;
158 return ret;
159}
160
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000161void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
162{
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000163 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
164
165 OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString()));
166 OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() +
Justin Holewinski12785e82011-03-03 13:34:29 +0000167 (ST.supportsDouble() ? ""
168 : ", map_f64_to_f32")));
Justin Holewinskia9c85f92011-06-22 00:43:56 +0000169 // .address_size directive is optional, but it must immediately follow
170 // the .target directive if present within a module
171 if (ST.supportsPTX23()) {
172 std::string addrSize = ST.is64Bit() ? "64" : "32";
173 OutStreamer.EmitRawText(Twine("\t.address_size " + addrSize));
174 }
175
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000176 OutStreamer.AddBlankLine();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000177
178 // declare global variables
179 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
180 i != e; ++i)
181 EmitVariableDeclaration(i);
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000182}
183
Che-Liang Chioudf659632010-11-08 03:06:08 +0000184bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
185 SetupMachineFunction(MF);
186 EmitFunctionDeclaration();
187 EmitFunctionBody();
188 return false;
189}
190
191void PTXAsmPrinter::EmitFunctionBodyStart() {
192 OutStreamer.EmitRawText(Twine("{"));
193
194 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
195
196 // Print local variable definition
197 for (PTXMachineFunctionInfo::reg_iterator
198 i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) {
199 unsigned reg = *i;
200
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000201 std::string def = "\t.reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000202 def += getRegisterTypeName(reg);
203 def += ' ';
204 def += getRegisterName(reg);
205 def += ';';
206 OutStreamer.EmitRawText(Twine(def));
207 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000208
209 const MachineFrameInfo* FrameInfo = MF->getFrameInfo();
Justin Holewinski08d03162011-06-22 16:07:03 +0000210 DEBUG(dbgs() << "Have " << FrameInfo->getNumObjects()
211 << " frame object(s)\n");
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000212 for (unsigned i = 0, e = FrameInfo->getNumObjects(); i != e; ++i) {
213 DEBUG(dbgs() << "Size of object: " << FrameInfo->getObjectSize(i) << "\n");
Justin Holewinski08d03162011-06-22 16:07:03 +0000214 if (FrameInfo->getObjectSize(i) > 0) {
215 std::string def = "\t.reg .b";
216 def += utostr(FrameInfo->getObjectSize(i)*8); // Convert to bits
217 def += " s";
218 def += utostr(i);
219 def += ";";
220 OutStreamer.EmitRawText(Twine(def));
221 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000222 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000223}
224
Eric Christopher50880d02010-09-18 18:52:28 +0000225void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000226 std::string str;
227 str.reserve(64);
228
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000229 raw_string_ostream OS(str);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000230
231 // Emit predicate
232 printPredicateOperand(MI, OS);
233
234 // Write instruction to str
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000235 printInstruction(MI, OS);
236 OS << ';';
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000237 OS.flush();
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000238
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000239 StringRef strref = StringRef(str);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000240 OutStreamer.EmitRawText(strref);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000241}
242
243void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
244 raw_ostream &OS) {
245 const MachineOperand &MO = MI->getOperand(opNum);
246
247 switch (MO.getType()) {
248 default:
249 llvm_unreachable("<unknown operand type>");
250 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000251 case MachineOperand::MO_GlobalAddress:
252 OS << *Mang->getSymbol(MO.getGlobal());
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000253 break;
254 case MachineOperand::MO_Immediate:
Justin Holewinski9583a862011-04-28 00:19:50 +0000255 OS << (long) MO.getImm();
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000256 break;
Che-Liang Chiou88d33672011-03-18 11:08:52 +0000257 case MachineOperand::MO_MachineBasicBlock:
258 OS << *MO.getMBB()->getSymbol();
259 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000260 case MachineOperand::MO_Register:
261 OS << getRegisterName(MO.getReg());
262 break;
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000263 case MachineOperand::MO_FPImmediate:
264 APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt();
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000265 bool isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID;
266 // Emit 0F for 32-bit floats and 0D for 64-bit doubles.
267 if (isFloat) {
268 OS << "0F";
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000269 }
270 else {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000271 OS << "0D";
272 }
273 // Emit the encoded floating-point value.
274 if (constFP.getZExtValue() > 0) {
275 OS << constFP.toString(16, false);
276 }
277 else {
278 OS << "00000000";
279 // If We have a double-precision zero, pad to 8-bytes.
280 if (!isFloat) {
281 OS << "00000000";
282 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000283 }
284 break;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000285 }
Eric Christopher50880d02010-09-18 18:52:28 +0000286}
287
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000288void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
289 raw_ostream &OS, const char *Modifier) {
290 printOperand(MI, opNum, OS);
291
292 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
293 return; // don't print "+0"
294
295 OS << "+";
296 printOperand(MI, opNum+1, OS);
297}
298
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000299void PTXAsmPrinter::printParamOperand(const MachineInstr *MI, int opNum,
300 raw_ostream &OS, const char *Modifier) {
301 OS << PARAM_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
302}
303
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000304void PTXAsmPrinter::printReturnOperand(const MachineInstr *MI, int opNum,
305 raw_ostream &OS, const char *Modifier) {
306 OS << RETURN_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
307}
308
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000309void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
310 // Check to see if this is a special global used by LLVM, if so, emit it.
311 if (EmitSpecialLLVMGlobal(gv))
312 return;
313
314 MCSymbol *gvsym = Mang->getSymbol(gv);
315
316 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
317
318 std::string decl;
319
320 // check if it is defined in some other translation unit
321 if (gv->isDeclaration())
322 decl += ".extern ";
323
324 // state space: e.g., .global
325 decl += ".";
326 decl += getStateSpaceName(gv->getType()->getAddressSpace());
327 decl += " ";
328
329 // alignment (optional)
330 unsigned alignment = gv->getAlignment();
331 if (alignment != 0) {
332 decl += ".align ";
333 decl += utostr(Log2_32(gv->getAlignment()));
334 decl += " ";
335 }
336
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000337
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000338 if (PointerType::classof(gv->getType())) {
339 const PointerType* pointerTy = dyn_cast<const PointerType>(gv->getType());
340 const Type* elementTy = pointerTy->getElementType();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000341
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000342 decl += ".b8 ";
343 decl += gvsym->getName();
344 decl += "[";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000345
Justin Holewinski9583a862011-04-28 00:19:50 +0000346 if (elementTy->isArrayTy())
347 {
348 assert(elementTy->isArrayTy() && "Only pointers to arrays are supported");
349
350 const ArrayType* arrayTy = dyn_cast<const ArrayType>(elementTy);
351 elementTy = arrayTy->getElementType();
352
353 unsigned numElements = arrayTy->getNumElements();
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000354
Justin Holewinski9583a862011-04-28 00:19:50 +0000355 while (elementTy->isArrayTy()) {
356
357 arrayTy = dyn_cast<const ArrayType>(elementTy);
358 elementTy = arrayTy->getElementType();
359
360 numElements *= arrayTy->getNumElements();
361 }
362
363 // FIXME: isPrimitiveType() == false for i16?
364 assert(elementTy->isSingleValueType() &&
365 "Non-primitive types are not handled");
366
367 // Compute the size of the array, in bytes.
368 uint64_t arraySize = (elementTy->getPrimitiveSizeInBits() >> 3)
369 * numElements;
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000370
Justin Holewinski9583a862011-04-28 00:19:50 +0000371 decl += utostr(arraySize);
372 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000373
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000374 decl += "]";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000375
Justin Holewinski9583a862011-04-28 00:19:50 +0000376 // handle string constants (assume ConstantArray means string)
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000377
Justin Holewinski9583a862011-04-28 00:19:50 +0000378 if (gv->hasInitializer())
379 {
Jay Foad7d715df2011-06-19 18:37:11 +0000380 const Constant *C = gv->getInitializer();
Justin Holewinski9583a862011-04-28 00:19:50 +0000381 if (const ConstantArray *CA = dyn_cast<ConstantArray>(C))
382 {
383 decl += " = {";
384
385 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
386 {
387 if (i > 0) decl += ",";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000388
389 decl += "0x" +
390 utohexstr(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());
Justin Holewinski9583a862011-04-28 00:19:50 +0000391 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000392
Justin Holewinski9583a862011-04-28 00:19:50 +0000393 decl += "}";
394 }
395 }
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000396 }
397 else {
398 // Note: this is currently the fall-through case and most likely generates
399 // incorrect code.
400 decl += getTypeName(gv->getType());
401 decl += " ";
402
403 decl += gvsym->getName();
404
405 if (ArrayType::classof(gv->getType()) ||
406 PointerType::classof(gv->getType()))
407 decl += "[]";
408 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000409
410 decl += ";";
411
412 OutStreamer.EmitRawText(Twine(decl));
413
414 OutStreamer.AddBlankLine();
415}
416
Che-Liang Chioudf659632010-11-08 03:06:08 +0000417void PTXAsmPrinter::EmitFunctionDeclaration() {
418 // The function label could have already been emitted if two symbols end up
419 // conflicting due to asm renaming. Detect this and emit an error.
420 if (!CurrentFnSym->isUndefined()) {
421 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
422 "' label emitted multiple times to assembly file");
423 return;
424 }
425
426 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
427 const bool isKernel = MFI->isKernel();
Justin Holewinski67a91842011-06-23 18:10:03 +0000428 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000429
430 std::string decl = isKernel ? ".entry" : ".func";
431
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000432 unsigned cnt = 0;
433
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000434 if (!isKernel) {
435 decl += " (";
436
437 for (PTXMachineFunctionInfo::ret_iterator
438 i = MFI->retRegBegin(), e = MFI->retRegEnd(), b = i;
439 i != e; ++i) {
440 if (i != b) {
441 decl += ", ";
442 }
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000443 if (ST.getShaderModel() >= PTXSubtarget::PTX_SM_2_0) {
444 decl += ".param .b";
445 decl += utostr(*i);
446 decl += " ";
447 decl += RETURN_PREFIX;
448 decl += utostr(++cnt);
449 } else {
450 decl += ".reg .";
451 decl += getRegisterTypeName(*i);
452 decl += " ";
453 decl += getRegisterName(*i);
454 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000455 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000456 decl += ")";
457 }
458
459 // Print function name
460 decl += " ";
461 decl += CurrentFnSym->getName().str();
462
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000463 decl += " (";
464
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000465 cnt = 0;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000466
467 // Print parameters
468 for (PTXMachineFunctionInfo::reg_iterator
469 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i;
470 i != e; ++i) {
471 if (i != b) {
472 decl += ", ";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000473 }
Justin Holewinski67a91842011-06-23 18:10:03 +0000474 if (isKernel || ST.getShaderModel() >= PTXSubtarget::PTX_SM_2_0) {
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000475 decl += ".param .b";
476 decl += utostr(*i);
477 decl += " ";
478 decl += PARAM_PREFIX;
479 decl += utostr(++cnt);
480 } else {
481 decl += ".reg .";
482 decl += getRegisterTypeName(*i);
483 decl += " ";
484 decl += getRegisterName(*i);
485 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000486 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000487 decl += ")";
488
489 // // Print parameter list
490 // if (!MFI->argRegEmpty()) {
491 // decl += " (";
492 // if (isKernel) {
493 // unsigned cnt = 0;
494 // for(PTXMachineFunctionInfo::reg_iterator
495 // i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i;
496 // i != e; ++i) {
497 // reg = *i;
498 // assert(reg != PTX::NoRegister && "Not a valid register!");
499 // if (i != b)
500 // decl += ", ";
501 // decl += ".param .";
502 // decl += getRegisterTypeName(reg);
503 // decl += " ";
504 // decl += PARAM_PREFIX;
505 // decl += utostr(++cnt);
506 // }
507 // } else {
508 // for (PTXMachineFunctionInfo::reg_iterator
509 // i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i;
510 // i != e; ++i) {
511 // reg = *i;
512 // assert(reg != PTX::NoRegister && "Not a valid register!");
513 // if (i != b)
514 // decl += ", ";
515 // decl += ".reg .";
516 // decl += getRegisterTypeName(reg);
517 // decl += " ";
518 // decl += getRegisterName(reg);
519 // }
520 // }
521 // decl += ")";
522 // }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000523
524 OutStreamer.EmitRawText(Twine(decl));
525}
526
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000527void PTXAsmPrinter::
528printPredicateOperand(const MachineInstr *MI, raw_ostream &O) {
529 int i = MI->findFirstPredOperandIdx();
530 if (i == -1)
531 llvm_unreachable("missing predicate operand");
532
533 unsigned reg = MI->getOperand(i).getReg();
534 int predOp = MI->getOperand(i+1).getImm();
535
536 DEBUG(dbgs() << "predicate: (" << reg << ", " << predOp << ")\n");
537
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000538 if (reg != PTX::NoRegister) {
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000539 O << '@';
540 if (predOp == PTX::PRED_NEGATE)
541 O << '!';
542 O << getRegisterName(reg);
543 }
544}
545
Eric Christopher50880d02010-09-18 18:52:28 +0000546#include "PTXGenAsmWriter.inc"
547
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000548// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000549extern "C" void LLVMInitializePTXAsmPrinter() {
Justin Holewinskie1fee482011-04-20 15:37:17 +0000550 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target);
551 RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target);
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000552}