blob: b1f7c1e4b2be3d5dec503635e5c56b9eb00faa5a [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);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +000066 void printPredicateOperand(const MachineInstr *MI, raw_ostream &O);
Che-Liang Chioudf659632010-11-08 03:06:08 +000067
68 // autogen'd.
69 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
70 static const char *getRegisterName(unsigned RegNo);
71
72private:
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000073 void EmitVariableDeclaration(const GlobalVariable *gv);
Che-Liang Chioudf659632010-11-08 03:06:08 +000074 void EmitFunctionDeclaration();
75}; // class PTXAsmPrinter
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000076} // namespace
77
Che-Liang Chioudf659632010-11-08 03:06:08 +000078static const char PARAM_PREFIX[] = "__param_";
79
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000080static const char *getRegisterTypeName(unsigned RegNo) {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000081#define TEST_REGCLS(cls, clsstr) \
Che-Liang Chioudf659632010-11-08 03:06:08 +000082 if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr;
Justin Holewinski1b91bcd2011-06-16 17:49:58 +000083 TEST_REGCLS(RegPred, pred);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +000084 TEST_REGCLS(RegI16, b16);
85 TEST_REGCLS(RegI32, b32);
86 TEST_REGCLS(RegI64, b64);
87 TEST_REGCLS(RegF32, b32);
88 TEST_REGCLS(RegF64, b64);
Che-Liang Chioudf659632010-11-08 03:06:08 +000089#undef TEST_REGCLS
90
91 llvm_unreachable("Not in any register class!");
92 return NULL;
93}
94
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000095static const char *getStateSpaceName(unsigned addressSpace) {
Che-Liang Chioud34f19f2010-12-30 10:41:27 +000096 switch (addressSpace) {
97 default: llvm_unreachable("Unknown state space");
98 case PTX::GLOBAL: return "global";
99 case PTX::CONSTANT: return "const";
100 case PTX::LOCAL: return "local";
101 case PTX::PARAMETER: return "param";
102 case PTX::SHARED: return "shared";
103 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000104 return NULL;
105}
106
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000107static const char *getTypeName(const Type* type) {
108 while (true) {
109 switch (type->getTypeID()) {
110 default: llvm_unreachable("Unknown type");
111 case Type::FloatTyID: return ".f32";
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000112 case Type::DoubleTyID: return ".f64";
113 case Type::IntegerTyID:
114 switch (type->getPrimitiveSizeInBits()) {
115 default: llvm_unreachable("Unknown integer bit-width");
116 case 16: return ".u16";
117 case 32: return ".u32";
118 case 64: return ".u64";
119 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000120 case Type::ArrayTyID:
121 case Type::PointerTyID:
122 type = dyn_cast<const SequentialType>(type)->getElementType();
123 break;
124 }
125 }
126 return NULL;
127}
128
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000129bool PTXAsmPrinter::doFinalization(Module &M) {
130 // XXX Temproarily remove global variables so that doFinalization() will not
131 // emit them again (global variables are emitted at beginning).
132
133 Module::GlobalListType &global_list = M.getGlobalList();
134 int i, n = global_list.size();
135 GlobalVariable **gv_array = new GlobalVariable* [n];
136
137 // first, back-up GlobalVariable in gv_array
138 i = 0;
139 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
140 I != E; ++I)
141 gv_array[i++] = &*I;
142
143 // second, empty global_list
144 while (!global_list.empty())
145 global_list.remove(global_list.begin());
146
147 // call doFinalization
148 bool ret = AsmPrinter::doFinalization(M);
149
150 // now we restore global variables
151 for (i = 0; i < n; i ++)
152 global_list.insert(global_list.end(), gv_array[i]);
153
154 delete[] gv_array;
155 return ret;
156}
157
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000158void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
159{
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000160 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
161
162 OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString()));
163 OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() +
Justin Holewinski12785e82011-03-03 13:34:29 +0000164 (ST.supportsDouble() ? ""
165 : ", map_f64_to_f32")));
Justin Holewinskia9c85f92011-06-22 00:43:56 +0000166 // .address_size directive is optional, but it must immediately follow
167 // the .target directive if present within a module
168 if (ST.supportsPTX23()) {
169 std::string addrSize = ST.is64Bit() ? "64" : "32";
170 OutStreamer.EmitRawText(Twine("\t.address_size " + addrSize));
171 }
172
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000173 OutStreamer.AddBlankLine();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000174
175 // declare global variables
176 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
177 i != e; ++i)
178 EmitVariableDeclaration(i);
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000179}
180
Che-Liang Chioudf659632010-11-08 03:06:08 +0000181bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
182 SetupMachineFunction(MF);
183 EmitFunctionDeclaration();
184 EmitFunctionBody();
185 return false;
186}
187
188void PTXAsmPrinter::EmitFunctionBodyStart() {
189 OutStreamer.EmitRawText(Twine("{"));
190
191 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
192
193 // Print local variable definition
194 for (PTXMachineFunctionInfo::reg_iterator
195 i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) {
196 unsigned reg = *i;
197
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000198 std::string def = "\t.reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000199 def += getRegisterTypeName(reg);
200 def += ' ';
201 def += getRegisterName(reg);
202 def += ';';
203 OutStreamer.EmitRawText(Twine(def));
204 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000205
206 const MachineFrameInfo* FrameInfo = MF->getFrameInfo();
Justin Holewinski08d03162011-06-22 16:07:03 +0000207 DEBUG(dbgs() << "Have " << FrameInfo->getNumObjects()
208 << " frame object(s)\n");
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000209 for (unsigned i = 0, e = FrameInfo->getNumObjects(); i != e; ++i) {
210 DEBUG(dbgs() << "Size of object: " << FrameInfo->getObjectSize(i) << "\n");
Justin Holewinski08d03162011-06-22 16:07:03 +0000211 if (FrameInfo->getObjectSize(i) > 0) {
212 std::string def = "\t.reg .b";
213 def += utostr(FrameInfo->getObjectSize(i)*8); // Convert to bits
214 def += " s";
215 def += utostr(i);
216 def += ";";
217 OutStreamer.EmitRawText(Twine(def));
218 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000219 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000220}
221
Eric Christopher50880d02010-09-18 18:52:28 +0000222void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000223 std::string str;
224 str.reserve(64);
225
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000226 raw_string_ostream OS(str);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000227
228 // Emit predicate
229 printPredicateOperand(MI, OS);
230
231 // Write instruction to str
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000232 printInstruction(MI, OS);
233 OS << ';';
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000234 OS.flush();
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000235
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000236 StringRef strref = StringRef(str);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000237 OutStreamer.EmitRawText(strref);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000238}
239
240void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
241 raw_ostream &OS) {
242 const MachineOperand &MO = MI->getOperand(opNum);
243
244 switch (MO.getType()) {
245 default:
246 llvm_unreachable("<unknown operand type>");
247 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000248 case MachineOperand::MO_GlobalAddress:
249 OS << *Mang->getSymbol(MO.getGlobal());
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000250 break;
251 case MachineOperand::MO_Immediate:
Justin Holewinski9583a862011-04-28 00:19:50 +0000252 OS << (long) MO.getImm();
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000253 break;
Che-Liang Chiou88d33672011-03-18 11:08:52 +0000254 case MachineOperand::MO_MachineBasicBlock:
255 OS << *MO.getMBB()->getSymbol();
256 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000257 case MachineOperand::MO_Register:
258 OS << getRegisterName(MO.getReg());
259 break;
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000260 case MachineOperand::MO_FPImmediate:
261 APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt();
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000262 bool isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID;
263 // Emit 0F for 32-bit floats and 0D for 64-bit doubles.
264 if (isFloat) {
265 OS << "0F";
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000266 }
267 else {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000268 OS << "0D";
269 }
270 // Emit the encoded floating-point value.
271 if (constFP.getZExtValue() > 0) {
272 OS << constFP.toString(16, false);
273 }
274 else {
275 OS << "00000000";
276 // If We have a double-precision zero, pad to 8-bytes.
277 if (!isFloat) {
278 OS << "00000000";
279 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000280 }
281 break;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000282 }
Eric Christopher50880d02010-09-18 18:52:28 +0000283}
284
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000285void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
286 raw_ostream &OS, const char *Modifier) {
287 printOperand(MI, opNum, OS);
288
289 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
290 return; // don't print "+0"
291
292 OS << "+";
293 printOperand(MI, opNum+1, OS);
294}
295
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000296void PTXAsmPrinter::printParamOperand(const MachineInstr *MI, int opNum,
297 raw_ostream &OS, const char *Modifier) {
298 OS << PARAM_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
299}
300
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000301void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
302 // Check to see if this is a special global used by LLVM, if so, emit it.
303 if (EmitSpecialLLVMGlobal(gv))
304 return;
305
306 MCSymbol *gvsym = Mang->getSymbol(gv);
307
308 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
309
310 std::string decl;
311
312 // check if it is defined in some other translation unit
313 if (gv->isDeclaration())
314 decl += ".extern ";
315
316 // state space: e.g., .global
317 decl += ".";
318 decl += getStateSpaceName(gv->getType()->getAddressSpace());
319 decl += " ";
320
321 // alignment (optional)
322 unsigned alignment = gv->getAlignment();
323 if (alignment != 0) {
324 decl += ".align ";
325 decl += utostr(Log2_32(gv->getAlignment()));
326 decl += " ";
327 }
328
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000329
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000330 if (PointerType::classof(gv->getType())) {
331 const PointerType* pointerTy = dyn_cast<const PointerType>(gv->getType());
332 const Type* elementTy = pointerTy->getElementType();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000333
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000334 decl += ".b8 ";
335 decl += gvsym->getName();
336 decl += "[";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000337
Justin Holewinski9583a862011-04-28 00:19:50 +0000338 if (elementTy->isArrayTy())
339 {
340 assert(elementTy->isArrayTy() && "Only pointers to arrays are supported");
341
342 const ArrayType* arrayTy = dyn_cast<const ArrayType>(elementTy);
343 elementTy = arrayTy->getElementType();
344
345 unsigned numElements = arrayTy->getNumElements();
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000346
Justin Holewinski9583a862011-04-28 00:19:50 +0000347 while (elementTy->isArrayTy()) {
348
349 arrayTy = dyn_cast<const ArrayType>(elementTy);
350 elementTy = arrayTy->getElementType();
351
352 numElements *= arrayTy->getNumElements();
353 }
354
355 // FIXME: isPrimitiveType() == false for i16?
356 assert(elementTy->isSingleValueType() &&
357 "Non-primitive types are not handled");
358
359 // Compute the size of the array, in bytes.
360 uint64_t arraySize = (elementTy->getPrimitiveSizeInBits() >> 3)
361 * numElements;
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000362
Justin Holewinski9583a862011-04-28 00:19:50 +0000363 decl += utostr(arraySize);
364 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000365
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000366 decl += "]";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000367
Justin Holewinski9583a862011-04-28 00:19:50 +0000368 // handle string constants (assume ConstantArray means string)
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000369
Justin Holewinski9583a862011-04-28 00:19:50 +0000370 if (gv->hasInitializer())
371 {
Jay Foad7d715df2011-06-19 18:37:11 +0000372 const Constant *C = gv->getInitializer();
Justin Holewinski9583a862011-04-28 00:19:50 +0000373 if (const ConstantArray *CA = dyn_cast<ConstantArray>(C))
374 {
375 decl += " = {";
376
377 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
378 {
379 if (i > 0) decl += ",";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000380
381 decl += "0x" +
382 utohexstr(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());
Justin Holewinski9583a862011-04-28 00:19:50 +0000383 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000384
Justin Holewinski9583a862011-04-28 00:19:50 +0000385 decl += "}";
386 }
387 }
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000388 }
389 else {
390 // Note: this is currently the fall-through case and most likely generates
391 // incorrect code.
392 decl += getTypeName(gv->getType());
393 decl += " ";
394
395 decl += gvsym->getName();
396
397 if (ArrayType::classof(gv->getType()) ||
398 PointerType::classof(gv->getType()))
399 decl += "[]";
400 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000401
402 decl += ";";
403
404 OutStreamer.EmitRawText(Twine(decl));
405
406 OutStreamer.AddBlankLine();
407}
408
Che-Liang Chioudf659632010-11-08 03:06:08 +0000409void PTXAsmPrinter::EmitFunctionDeclaration() {
410 // The function label could have already been emitted if two symbols end up
411 // conflicting due to asm renaming. Detect this and emit an error.
412 if (!CurrentFnSym->isUndefined()) {
413 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
414 "' label emitted multiple times to assembly file");
415 return;
416 }
417
418 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
419 const bool isKernel = MFI->isKernel();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000420
421 std::string decl = isKernel ? ".entry" : ".func";
422
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000423 if (!isKernel) {
424 decl += " (";
425
426 for (PTXMachineFunctionInfo::ret_iterator
427 i = MFI->retRegBegin(), e = MFI->retRegEnd(), b = i;
428 i != e; ++i) {
429 if (i != b) {
430 decl += ", ";
431 }
432 decl += ".reg .";
433 decl += getRegisterTypeName(*i);
434 decl += " ";
435 decl += getRegisterName(*i);
436 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000437 decl += ")";
438 }
439
440 // Print function name
441 decl += " ";
442 decl += CurrentFnSym->getName().str();
443
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000444 decl += " (";
445
446 unsigned cnt = 0;
447
448 // Print parameters
449 for (PTXMachineFunctionInfo::reg_iterator
450 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i;
451 i != e; ++i) {
452 if (i != b) {
453 decl += ", ";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000454 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000455 if (isKernel) {
456 decl += ".param .b";
457 decl += utostr(*i);
458 decl += " ";
459 decl += PARAM_PREFIX;
460 decl += utostr(++cnt);
461 } else {
462 decl += ".reg .";
463 decl += getRegisterTypeName(*i);
464 decl += " ";
465 decl += getRegisterName(*i);
466 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000467 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000468 decl += ")";
469
470 // // Print parameter list
471 // if (!MFI->argRegEmpty()) {
472 // decl += " (";
473 // if (isKernel) {
474 // unsigned cnt = 0;
475 // for(PTXMachineFunctionInfo::reg_iterator
476 // i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i;
477 // i != e; ++i) {
478 // reg = *i;
479 // assert(reg != PTX::NoRegister && "Not a valid register!");
480 // if (i != b)
481 // decl += ", ";
482 // decl += ".param .";
483 // decl += getRegisterTypeName(reg);
484 // decl += " ";
485 // decl += PARAM_PREFIX;
486 // decl += utostr(++cnt);
487 // }
488 // } else {
489 // for (PTXMachineFunctionInfo::reg_iterator
490 // i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i;
491 // i != e; ++i) {
492 // reg = *i;
493 // assert(reg != PTX::NoRegister && "Not a valid register!");
494 // if (i != b)
495 // decl += ", ";
496 // decl += ".reg .";
497 // decl += getRegisterTypeName(reg);
498 // decl += " ";
499 // decl += getRegisterName(reg);
500 // }
501 // }
502 // decl += ")";
503 // }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000504
505 OutStreamer.EmitRawText(Twine(decl));
506}
507
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000508void PTXAsmPrinter::
509printPredicateOperand(const MachineInstr *MI, raw_ostream &O) {
510 int i = MI->findFirstPredOperandIdx();
511 if (i == -1)
512 llvm_unreachable("missing predicate operand");
513
514 unsigned reg = MI->getOperand(i).getReg();
515 int predOp = MI->getOperand(i+1).getImm();
516
517 DEBUG(dbgs() << "predicate: (" << reg << ", " << predOp << ")\n");
518
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000519 if (reg != PTX::NoRegister) {
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000520 O << '@';
521 if (predOp == PTX::PRED_NEGATE)
522 O << '!';
523 O << getRegisterName(reg);
524 }
525}
526
Eric Christopher50880d02010-09-18 18:52:28 +0000527#include "PTXGenAsmWriter.inc"
528
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000529// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000530extern "C" void LLVMInitializePTXAsmPrinter() {
Justin Holewinskie1fee482011-04-20 15:37:17 +0000531 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target);
532 RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target);
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000533}