blob: 2848d5460eee0a0e8206b244cb4262e779b1aa53 [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"
Justin Holewinski47997292011-06-24 19:19:18 +000025#include "llvm/Analysis/DebugInfo.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000026#include "llvm/CodeGen/AsmPrinter.h"
Justin Holewinskidf1c8d82011-06-20 15:56:20 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000028#include "llvm/CodeGen/MachineInstr.h"
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Justin Holewinski47997292011-06-24 19:19:18 +000030#include "llvm/MC/MCContext.h"
Eric Christopher50880d02010-09-18 18:52:28 +000031#include "llvm/MC/MCStreamer.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000032#include "llvm/MC/MCSymbol.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000033#include "llvm/Target/Mangler.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000034#include "llvm/Target/TargetLoweringObjectFile.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000035#include "llvm/Target/TargetRegistry.h"
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000036#include "llvm/Support/CommandLine.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/ErrorHandling.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000039#include "llvm/Support/MathExtras.h"
Justin Holewinski47997292011-06-24 19:19:18 +000040#include "llvm/Support/Path.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000041#include "llvm/Support/raw_ostream.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000042
43using namespace llvm;
44
45namespace {
Che-Liang Chioudf659632010-11-08 03:06:08 +000046class PTXAsmPrinter : public AsmPrinter {
47public:
48 explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
49 : AsmPrinter(TM, Streamer) {}
Eric Christopher50880d02010-09-18 18:52:28 +000050
Che-Liang Chioudf659632010-11-08 03:06:08 +000051 const char *getPassName() const { return "PTX Assembly Printer"; }
Eric Christopher50880d02010-09-18 18:52:28 +000052
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000053 bool doFinalization(Module &M);
54
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000055 virtual void EmitStartOfAsmFile(Module &M);
56
Che-Liang Chioudf659632010-11-08 03:06:08 +000057 virtual bool runOnMachineFunction(MachineFunction &MF);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000058
Che-Liang Chioudf659632010-11-08 03:06:08 +000059 virtual void EmitFunctionBodyStart();
60 virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); }
61
62 virtual void EmitInstruction(const MachineInstr *MI);
63
64 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000065 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
66 const char *Modifier = 0);
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +000067 void printParamOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
68 const char *Modifier = 0);
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +000069 void printReturnOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
70 const char *Modifier = 0);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +000071 void printPredicateOperand(const MachineInstr *MI, raw_ostream &O);
Che-Liang Chioudf659632010-11-08 03:06:08 +000072
Justin Holewinski47997292011-06-24 19:19:18 +000073 unsigned GetOrCreateSourceID(StringRef FileName,
74 StringRef DirName);
75
Che-Liang Chioudf659632010-11-08 03:06:08 +000076 // autogen'd.
77 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
78 static const char *getRegisterName(unsigned RegNo);
79
80private:
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000081 void EmitVariableDeclaration(const GlobalVariable *gv);
Che-Liang Chioudf659632010-11-08 03:06:08 +000082 void EmitFunctionDeclaration();
Justin Holewinski47997292011-06-24 19:19:18 +000083
84 StringMap<unsigned> SourceIdMap;
Che-Liang Chioudf659632010-11-08 03:06:08 +000085}; // class PTXAsmPrinter
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000086} // namespace
87
Che-Liang Chioudf659632010-11-08 03:06:08 +000088static const char PARAM_PREFIX[] = "__param_";
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +000089static const char RETURN_PREFIX[] = "__ret_";
Che-Liang Chioudf659632010-11-08 03:06:08 +000090
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000091static const char *getRegisterTypeName(unsigned RegNo) {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000092#define TEST_REGCLS(cls, clsstr) \
Che-Liang Chioudf659632010-11-08 03:06:08 +000093 if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr;
Justin Holewinski1b91bcd2011-06-16 17:49:58 +000094 TEST_REGCLS(RegPred, pred);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +000095 TEST_REGCLS(RegI16, b16);
96 TEST_REGCLS(RegI32, b32);
97 TEST_REGCLS(RegI64, b64);
98 TEST_REGCLS(RegF32, b32);
99 TEST_REGCLS(RegF64, b64);
Che-Liang Chioudf659632010-11-08 03:06:08 +0000100#undef TEST_REGCLS
101
102 llvm_unreachable("Not in any register class!");
103 return NULL;
104}
105
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000106static const char *getStateSpaceName(unsigned addressSpace) {
Che-Liang Chioud34f19f2010-12-30 10:41:27 +0000107 switch (addressSpace) {
108 default: llvm_unreachable("Unknown state space");
109 case PTX::GLOBAL: return "global";
110 case PTX::CONSTANT: return "const";
111 case PTX::LOCAL: return "local";
112 case PTX::PARAMETER: return "param";
113 case PTX::SHARED: return "shared";
114 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000115 return NULL;
116}
117
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000118static const char *getTypeName(const Type* type) {
119 while (true) {
120 switch (type->getTypeID()) {
121 default: llvm_unreachable("Unknown type");
122 case Type::FloatTyID: return ".f32";
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000123 case Type::DoubleTyID: return ".f64";
124 case Type::IntegerTyID:
125 switch (type->getPrimitiveSizeInBits()) {
126 default: llvm_unreachable("Unknown integer bit-width");
127 case 16: return ".u16";
128 case 32: return ".u32";
129 case 64: return ".u64";
130 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000131 case Type::ArrayTyID:
132 case Type::PointerTyID:
133 type = dyn_cast<const SequentialType>(type)->getElementType();
134 break;
135 }
136 }
137 return NULL;
138}
139
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000140bool PTXAsmPrinter::doFinalization(Module &M) {
141 // XXX Temproarily remove global variables so that doFinalization() will not
142 // emit them again (global variables are emitted at beginning).
143
144 Module::GlobalListType &global_list = M.getGlobalList();
145 int i, n = global_list.size();
146 GlobalVariable **gv_array = new GlobalVariable* [n];
147
148 // first, back-up GlobalVariable in gv_array
149 i = 0;
150 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
151 I != E; ++I)
152 gv_array[i++] = &*I;
153
154 // second, empty global_list
155 while (!global_list.empty())
156 global_list.remove(global_list.begin());
157
158 // call doFinalization
159 bool ret = AsmPrinter::doFinalization(M);
160
161 // now we restore global variables
162 for (i = 0; i < n; i ++)
163 global_list.insert(global_list.end(), gv_array[i]);
164
165 delete[] gv_array;
166 return ret;
167}
168
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000169void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
170{
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000171 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
172
173 OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString()));
174 OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() +
Justin Holewinski12785e82011-03-03 13:34:29 +0000175 (ST.supportsDouble() ? ""
176 : ", map_f64_to_f32")));
Justin Holewinskia9c85f92011-06-22 00:43:56 +0000177 // .address_size directive is optional, but it must immediately follow
178 // the .target directive if present within a module
179 if (ST.supportsPTX23()) {
180 std::string addrSize = ST.is64Bit() ? "64" : "32";
181 OutStreamer.EmitRawText(Twine("\t.address_size " + addrSize));
182 }
183
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000184 OutStreamer.AddBlankLine();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000185
Justin Holewinski47997292011-06-24 19:19:18 +0000186 // Define any .file directives
187 DebugInfoFinder DbgFinder;
188 DbgFinder.processModule(M);
189
190 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
191 E = DbgFinder.compile_unit_end(); I != E; ++I) {
192 DICompileUnit DIUnit(*I);
193 StringRef FN = DIUnit.getFilename();
194 StringRef Dir = DIUnit.getDirectory();
195 GetOrCreateSourceID(FN, Dir);
196 }
197
198 OutStreamer.AddBlankLine();
199
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000200 // declare global variables
201 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
202 i != e; ++i)
203 EmitVariableDeclaration(i);
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000204}
205
Che-Liang Chioudf659632010-11-08 03:06:08 +0000206bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
207 SetupMachineFunction(MF);
208 EmitFunctionDeclaration();
209 EmitFunctionBody();
210 return false;
211}
212
213void PTXAsmPrinter::EmitFunctionBodyStart() {
214 OutStreamer.EmitRawText(Twine("{"));
215
216 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
217
218 // Print local variable definition
219 for (PTXMachineFunctionInfo::reg_iterator
220 i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) {
221 unsigned reg = *i;
222
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000223 std::string def = "\t.reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000224 def += getRegisterTypeName(reg);
225 def += ' ';
226 def += getRegisterName(reg);
227 def += ';';
228 OutStreamer.EmitRawText(Twine(def));
229 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000230
231 const MachineFrameInfo* FrameInfo = MF->getFrameInfo();
Justin Holewinski08d03162011-06-22 16:07:03 +0000232 DEBUG(dbgs() << "Have " << FrameInfo->getNumObjects()
233 << " frame object(s)\n");
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000234 for (unsigned i = 0, e = FrameInfo->getNumObjects(); i != e; ++i) {
235 DEBUG(dbgs() << "Size of object: " << FrameInfo->getObjectSize(i) << "\n");
Justin Holewinski08d03162011-06-22 16:07:03 +0000236 if (FrameInfo->getObjectSize(i) > 0) {
237 std::string def = "\t.reg .b";
238 def += utostr(FrameInfo->getObjectSize(i)*8); // Convert to bits
239 def += " s";
240 def += utostr(i);
241 def += ";";
242 OutStreamer.EmitRawText(Twine(def));
243 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000244 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000245}
246
Eric Christopher50880d02010-09-18 18:52:28 +0000247void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000248 std::string str;
249 str.reserve(64);
250
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000251 raw_string_ostream OS(str);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000252
Justin Holewinski47997292011-06-24 19:19:18 +0000253 DebugLoc DL = MI->getDebugLoc();
254 if (!DL.isUnknown()) {
255
256 const MDNode *S = DL.getScope(MF->getFunction()->getContext());
257
258 // This is taken from DwarfDebug.cpp, which is conveniently not a public
259 // LLVM class.
260 StringRef Fn;
261 StringRef Dir;
262 unsigned Src = 1;
263 if (S) {
264 DIDescriptor Scope(S);
265 if (Scope.isCompileUnit()) {
266 DICompileUnit CU(S);
267 Fn = CU.getFilename();
268 Dir = CU.getDirectory();
269 } else if (Scope.isFile()) {
270 DIFile F(S);
271 Fn = F.getFilename();
272 Dir = F.getDirectory();
273 } else if (Scope.isSubprogram()) {
274 DISubprogram SP(S);
275 Fn = SP.getFilename();
276 Dir = SP.getDirectory();
277 } else if (Scope.isLexicalBlock()) {
278 DILexicalBlock DB(S);
279 Fn = DB.getFilename();
280 Dir = DB.getDirectory();
281 } else
282 assert(0 && "Unexpected scope info");
283
284 Src = GetOrCreateSourceID(Fn, Dir);
285 }
286 OutStreamer.EmitDwarfLocDirective(Src, DL.getLine(), DL.getCol(),
287 0, 0, 0, Fn);
288
289 const MCDwarfLoc& MDL = OutContext.getCurrentDwarfLoc();
290
291 OS << "\t.loc ";
292 OS << utostr(MDL.getFileNum());
293 OS << " ";
294 OS << utostr(MDL.getLine());
295 OS << " ";
296 OS << utostr(MDL.getColumn());
297 OS << "\n";
298 }
299
300
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000301 // Emit predicate
302 printPredicateOperand(MI, OS);
303
304 // Write instruction to str
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000305 printInstruction(MI, OS);
306 OS << ';';
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000307 OS.flush();
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000308
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000309 StringRef strref = StringRef(str);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000310 OutStreamer.EmitRawText(strref);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000311}
312
313void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
314 raw_ostream &OS) {
315 const MachineOperand &MO = MI->getOperand(opNum);
316
317 switch (MO.getType()) {
318 default:
319 llvm_unreachable("<unknown operand type>");
320 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000321 case MachineOperand::MO_GlobalAddress:
322 OS << *Mang->getSymbol(MO.getGlobal());
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000323 break;
324 case MachineOperand::MO_Immediate:
Justin Holewinski9583a862011-04-28 00:19:50 +0000325 OS << (long) MO.getImm();
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000326 break;
Che-Liang Chiou88d33672011-03-18 11:08:52 +0000327 case MachineOperand::MO_MachineBasicBlock:
328 OS << *MO.getMBB()->getSymbol();
329 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000330 case MachineOperand::MO_Register:
331 OS << getRegisterName(MO.getReg());
332 break;
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000333 case MachineOperand::MO_FPImmediate:
334 APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt();
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000335 bool isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID;
336 // Emit 0F for 32-bit floats and 0D for 64-bit doubles.
337 if (isFloat) {
338 OS << "0F";
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000339 }
340 else {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000341 OS << "0D";
342 }
343 // Emit the encoded floating-point value.
344 if (constFP.getZExtValue() > 0) {
345 OS << constFP.toString(16, false);
346 }
347 else {
348 OS << "00000000";
349 // If We have a double-precision zero, pad to 8-bytes.
350 if (!isFloat) {
351 OS << "00000000";
352 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000353 }
354 break;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000355 }
Eric Christopher50880d02010-09-18 18:52:28 +0000356}
357
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000358void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
359 raw_ostream &OS, const char *Modifier) {
360 printOperand(MI, opNum, OS);
361
362 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
363 return; // don't print "+0"
364
365 OS << "+";
366 printOperand(MI, opNum+1, OS);
367}
368
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000369void PTXAsmPrinter::printParamOperand(const MachineInstr *MI, int opNum,
370 raw_ostream &OS, const char *Modifier) {
371 OS << PARAM_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
372}
373
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000374void PTXAsmPrinter::printReturnOperand(const MachineInstr *MI, int opNum,
375 raw_ostream &OS, const char *Modifier) {
376 OS << RETURN_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
377}
378
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000379void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
380 // Check to see if this is a special global used by LLVM, if so, emit it.
381 if (EmitSpecialLLVMGlobal(gv))
382 return;
383
384 MCSymbol *gvsym = Mang->getSymbol(gv);
385
386 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
387
388 std::string decl;
389
390 // check if it is defined in some other translation unit
391 if (gv->isDeclaration())
392 decl += ".extern ";
393
394 // state space: e.g., .global
395 decl += ".";
396 decl += getStateSpaceName(gv->getType()->getAddressSpace());
397 decl += " ";
398
399 // alignment (optional)
400 unsigned alignment = gv->getAlignment();
401 if (alignment != 0) {
402 decl += ".align ";
403 decl += utostr(Log2_32(gv->getAlignment()));
404 decl += " ";
405 }
406
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000407
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000408 if (PointerType::classof(gv->getType())) {
409 const PointerType* pointerTy = dyn_cast<const PointerType>(gv->getType());
410 const Type* elementTy = pointerTy->getElementType();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000411
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000412 decl += ".b8 ";
413 decl += gvsym->getName();
414 decl += "[";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000415
Justin Holewinski9583a862011-04-28 00:19:50 +0000416 if (elementTy->isArrayTy())
417 {
418 assert(elementTy->isArrayTy() && "Only pointers to arrays are supported");
419
420 const ArrayType* arrayTy = dyn_cast<const ArrayType>(elementTy);
421 elementTy = arrayTy->getElementType();
422
423 unsigned numElements = arrayTy->getNumElements();
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000424
Justin Holewinski9583a862011-04-28 00:19:50 +0000425 while (elementTy->isArrayTy()) {
426
427 arrayTy = dyn_cast<const ArrayType>(elementTy);
428 elementTy = arrayTy->getElementType();
429
430 numElements *= arrayTy->getNumElements();
431 }
432
433 // FIXME: isPrimitiveType() == false for i16?
434 assert(elementTy->isSingleValueType() &&
435 "Non-primitive types are not handled");
436
437 // Compute the size of the array, in bytes.
438 uint64_t arraySize = (elementTy->getPrimitiveSizeInBits() >> 3)
439 * numElements;
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000440
Justin Holewinski9583a862011-04-28 00:19:50 +0000441 decl += utostr(arraySize);
442 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000443
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000444 decl += "]";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000445
Justin Holewinski9583a862011-04-28 00:19:50 +0000446 // handle string constants (assume ConstantArray means string)
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000447
Justin Holewinski9583a862011-04-28 00:19:50 +0000448 if (gv->hasInitializer())
449 {
Jay Foad7d715df2011-06-19 18:37:11 +0000450 const Constant *C = gv->getInitializer();
Justin Holewinski9583a862011-04-28 00:19:50 +0000451 if (const ConstantArray *CA = dyn_cast<ConstantArray>(C))
452 {
453 decl += " = {";
454
455 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
456 {
457 if (i > 0) decl += ",";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000458
459 decl += "0x" +
460 utohexstr(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());
Justin Holewinski9583a862011-04-28 00:19:50 +0000461 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000462
Justin Holewinski9583a862011-04-28 00:19:50 +0000463 decl += "}";
464 }
465 }
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000466 }
467 else {
468 // Note: this is currently the fall-through case and most likely generates
469 // incorrect code.
470 decl += getTypeName(gv->getType());
471 decl += " ";
472
473 decl += gvsym->getName();
474
475 if (ArrayType::classof(gv->getType()) ||
476 PointerType::classof(gv->getType()))
477 decl += "[]";
478 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000479
480 decl += ";";
481
482 OutStreamer.EmitRawText(Twine(decl));
483
484 OutStreamer.AddBlankLine();
485}
486
Che-Liang Chioudf659632010-11-08 03:06:08 +0000487void PTXAsmPrinter::EmitFunctionDeclaration() {
488 // The function label could have already been emitted if two symbols end up
489 // conflicting due to asm renaming. Detect this and emit an error.
490 if (!CurrentFnSym->isUndefined()) {
491 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
492 "' label emitted multiple times to assembly file");
493 return;
494 }
495
496 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
497 const bool isKernel = MFI->isKernel();
Justin Holewinski67a91842011-06-23 18:10:03 +0000498 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000499
500 std::string decl = isKernel ? ".entry" : ".func";
501
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000502 unsigned cnt = 0;
503
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000504 if (!isKernel) {
505 decl += " (";
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000506 for (PTXMachineFunctionInfo::ret_iterator
507 i = MFI->retRegBegin(), e = MFI->retRegEnd(), b = i;
508 i != e; ++i) {
509 if (i != b) {
510 decl += ", ";
511 }
Justin Holewinskid8149c12011-06-23 18:10:13 +0000512 decl += ".reg .";
513 decl += getRegisterTypeName(*i);
514 decl += " ";
515 decl += getRegisterName(*i);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000516 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000517 decl += ")";
518 }
519
520 // Print function name
521 decl += " ";
522 decl += CurrentFnSym->getName().str();
523
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000524 decl += " (";
525
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000526 cnt = 0;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000527
528 // Print parameters
529 for (PTXMachineFunctionInfo::reg_iterator
530 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i;
531 i != e; ++i) {
532 if (i != b) {
533 decl += ", ";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000534 }
Justin Holewinski35f4fb32011-06-24 16:27:49 +0000535 if (isKernel || ST.useParamSpaceForDeviceArgs()) {
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000536 decl += ".param .b";
537 decl += utostr(*i);
538 decl += " ";
539 decl += PARAM_PREFIX;
540 decl += utostr(++cnt);
541 } else {
542 decl += ".reg .";
543 decl += getRegisterTypeName(*i);
544 decl += " ";
545 decl += getRegisterName(*i);
546 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000547 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000548 decl += ")";
549
Che-Liang Chioudf659632010-11-08 03:06:08 +0000550 OutStreamer.EmitRawText(Twine(decl));
551}
552
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000553void PTXAsmPrinter::
554printPredicateOperand(const MachineInstr *MI, raw_ostream &O) {
555 int i = MI->findFirstPredOperandIdx();
556 if (i == -1)
557 llvm_unreachable("missing predicate operand");
558
559 unsigned reg = MI->getOperand(i).getReg();
560 int predOp = MI->getOperand(i+1).getImm();
561
562 DEBUG(dbgs() << "predicate: (" << reg << ", " << predOp << ")\n");
563
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000564 if (reg != PTX::NoRegister) {
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000565 O << '@';
566 if (predOp == PTX::PRED_NEGATE)
567 O << '!';
568 O << getRegisterName(reg);
569 }
570}
571
Justin Holewinski47997292011-06-24 19:19:18 +0000572unsigned PTXAsmPrinter::GetOrCreateSourceID(StringRef FileName,
573 StringRef DirName) {
574 // If FE did not provide a file name, then assume stdin.
575 if (FileName.empty())
576 return GetOrCreateSourceID("<stdin>", StringRef());
577
578 // MCStream expects full path name as filename.
579 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
580 SmallString<128> FullPathName = DirName;
581 sys::path::append(FullPathName, FileName);
582 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
583 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
584 }
585
586 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
587 if (Entry.getValue())
588 return Entry.getValue();
589
590 unsigned SrcId = SourceIdMap.size();
591 Entry.setValue(SrcId);
592
593 // Print out a .file directive to specify files for .loc directives.
594 OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
595
596 return SrcId;
597}
598
Eric Christopher50880d02010-09-18 18:52:28 +0000599#include "PTXGenAsmWriter.inc"
600
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000601// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000602extern "C" void LLVMInitializePTXAsmPrinter() {
Justin Holewinskie1fee482011-04-20 15:37:17 +0000603 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target);
604 RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target);
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000605}