blob: 6337ee99705b620f0609cc59943103de02edca65 [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"
Justin Holewinski297984d2011-09-22 16:45:40 +000019#include "PTXRegisterInfo.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000020#include "PTXTargetMachine.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000021#include "llvm/DerivedTypes.h"
22#include "llvm/Module.h"
Eric Christopher50880d02010-09-18 18:52:28 +000023#include "llvm/ADT/SmallString.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000024#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/Twine.h"
Justin Holewinski47997292011-06-24 19:19:18 +000026#include "llvm/Analysis/DebugInfo.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000027#include "llvm/CodeGen/AsmPrinter.h"
Justin Holewinskidf1c8d82011-06-20 15:56:20 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000029#include "llvm/CodeGen/MachineInstr.h"
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Justin Holewinski47997292011-06-24 19:19:18 +000031#include "llvm/MC/MCContext.h"
Eric Christopher50880d02010-09-18 18:52:28 +000032#include "llvm/MC/MCStreamer.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000033#include "llvm/MC/MCSymbol.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000034#include "llvm/Target/Mangler.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000035#include "llvm/Target/TargetLoweringObjectFile.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"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000041#include "llvm/Support/TargetRegistry.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000042#include "llvm/Support/raw_ostream.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000043
44using namespace llvm;
45
46namespace {
Che-Liang Chioudf659632010-11-08 03:06:08 +000047class PTXAsmPrinter : public AsmPrinter {
48public:
49 explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
50 : AsmPrinter(TM, Streamer) {}
Eric Christopher50880d02010-09-18 18:52:28 +000051
Che-Liang Chioudf659632010-11-08 03:06:08 +000052 const char *getPassName() const { return "PTX Assembly Printer"; }
Eric Christopher50880d02010-09-18 18:52:28 +000053
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000054 bool doFinalization(Module &M);
55
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000056 virtual void EmitStartOfAsmFile(Module &M);
57
Che-Liang Chioudf659632010-11-08 03:06:08 +000058 virtual bool runOnMachineFunction(MachineFunction &MF);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000059
Che-Liang Chioudf659632010-11-08 03:06:08 +000060 virtual void EmitFunctionBodyStart();
61 virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); }
62
63 virtual void EmitInstruction(const MachineInstr *MI);
64
65 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000066 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
67 const char *Modifier = 0);
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +000068 void printParamOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
69 const char *Modifier = 0);
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +000070 void printReturnOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
Justin Holewinski297984d2011-09-22 16:45:40 +000071 const char *Modifier = 0);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +000072 void printPredicateOperand(const MachineInstr *MI, raw_ostream &O);
Che-Liang Chioudf659632010-11-08 03:06:08 +000073
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +000074 void printCall(const MachineInstr *MI, raw_ostream &O);
75
Justin Holewinski47997292011-06-24 19:19:18 +000076 unsigned GetOrCreateSourceID(StringRef FileName,
77 StringRef DirName);
78
Che-Liang Chioudf659632010-11-08 03:06:08 +000079 // autogen'd.
80 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
81 static const char *getRegisterName(unsigned RegNo);
82
83private:
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000084 void EmitVariableDeclaration(const GlobalVariable *gv);
Che-Liang Chioudf659632010-11-08 03:06:08 +000085 void EmitFunctionDeclaration();
Justin Holewinski47997292011-06-24 19:19:18 +000086
87 StringMap<unsigned> SourceIdMap;
Che-Liang Chioudf659632010-11-08 03:06:08 +000088}; // class PTXAsmPrinter
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000089} // namespace
90
Che-Liang Chioudf659632010-11-08 03:06:08 +000091static const char PARAM_PREFIX[] = "__param_";
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +000092static const char RETURN_PREFIX[] = "__ret_";
Che-Liang Chioudf659632010-11-08 03:06:08 +000093
Justin Holewinski5422a0f2011-09-22 16:45:46 +000094static const char *getRegisterTypeName(unsigned RegNo,
95 const MachineRegisterInfo& MRI) {
96 const TargetRegisterClass *TRC = MRI.getRegClass(RegNo);
97
98#define TEST_REGCLS(cls, clsstr) \
99 if (PTX::cls ## RegisterClass == TRC) return # clsstr;
100
Justin Holewinski1b91bcd2011-06-16 17:49:58 +0000101 TEST_REGCLS(RegPred, pred);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000102 TEST_REGCLS(RegI16, b16);
103 TEST_REGCLS(RegI32, b32);
104 TEST_REGCLS(RegI64, b64);
105 TEST_REGCLS(RegF32, b32);
106 TEST_REGCLS(RegF64, b64);
Che-Liang Chioudf659632010-11-08 03:06:08 +0000107#undef TEST_REGCLS
108
109 llvm_unreachable("Not in any register class!");
110 return NULL;
111}
112
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000113static const char *getStateSpaceName(unsigned addressSpace) {
Che-Liang Chioud34f19f2010-12-30 10:41:27 +0000114 switch (addressSpace) {
115 default: llvm_unreachable("Unknown state space");
116 case PTX::GLOBAL: return "global";
117 case PTX::CONSTANT: return "const";
118 case PTX::LOCAL: return "local";
119 case PTX::PARAMETER: return "param";
120 case PTX::SHARED: return "shared";
121 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000122 return NULL;
123}
124
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000125static const char *getTypeName(Type* type) {
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000126 while (true) {
127 switch (type->getTypeID()) {
128 default: llvm_unreachable("Unknown type");
129 case Type::FloatTyID: return ".f32";
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000130 case Type::DoubleTyID: return ".f64";
131 case Type::IntegerTyID:
132 switch (type->getPrimitiveSizeInBits()) {
133 default: llvm_unreachable("Unknown integer bit-width");
134 case 16: return ".u16";
135 case 32: return ".u32";
136 case 64: return ".u64";
137 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000138 case Type::ArrayTyID:
139 case Type::PointerTyID:
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000140 type = dyn_cast<SequentialType>(type)->getElementType();
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000141 break;
142 }
143 }
144 return NULL;
145}
146
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000147bool PTXAsmPrinter::doFinalization(Module &M) {
148 // XXX Temproarily remove global variables so that doFinalization() will not
149 // emit them again (global variables are emitted at beginning).
150
151 Module::GlobalListType &global_list = M.getGlobalList();
152 int i, n = global_list.size();
153 GlobalVariable **gv_array = new GlobalVariable* [n];
154
155 // first, back-up GlobalVariable in gv_array
156 i = 0;
157 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
158 I != E; ++I)
159 gv_array[i++] = &*I;
160
161 // second, empty global_list
162 while (!global_list.empty())
163 global_list.remove(global_list.begin());
164
165 // call doFinalization
166 bool ret = AsmPrinter::doFinalization(M);
167
168 // now we restore global variables
169 for (i = 0; i < n; i ++)
170 global_list.insert(global_list.end(), gv_array[i]);
171
172 delete[] gv_array;
173 return ret;
174}
175
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000176void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
177{
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000178 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
179
180 OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString()));
181 OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() +
Justin Holewinski12785e82011-03-03 13:34:29 +0000182 (ST.supportsDouble() ? ""
183 : ", map_f64_to_f32")));
Justin Holewinskia9c85f92011-06-22 00:43:56 +0000184 // .address_size directive is optional, but it must immediately follow
185 // the .target directive if present within a module
186 if (ST.supportsPTX23()) {
187 std::string addrSize = ST.is64Bit() ? "64" : "32";
188 OutStreamer.EmitRawText(Twine("\t.address_size " + addrSize));
189 }
190
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000191 OutStreamer.AddBlankLine();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000192
Justin Holewinski47997292011-06-24 19:19:18 +0000193 // Define any .file directives
194 DebugInfoFinder DbgFinder;
195 DbgFinder.processModule(M);
196
197 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
198 E = DbgFinder.compile_unit_end(); I != E; ++I) {
199 DICompileUnit DIUnit(*I);
200 StringRef FN = DIUnit.getFilename();
201 StringRef Dir = DIUnit.getDirectory();
202 GetOrCreateSourceID(FN, Dir);
203 }
204
205 OutStreamer.AddBlankLine();
206
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000207 // declare global variables
208 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
209 i != e; ++i)
210 EmitVariableDeclaration(i);
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000211}
212
Che-Liang Chioudf659632010-11-08 03:06:08 +0000213bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
214 SetupMachineFunction(MF);
215 EmitFunctionDeclaration();
216 EmitFunctionBody();
217 return false;
218}
219
220void PTXAsmPrinter::EmitFunctionBodyStart() {
221 OutStreamer.EmitRawText(Twine("{"));
222
223 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
224
Justin Holewinski297984d2011-09-22 16:45:40 +0000225 // Print register definitions
226 std::string regDefs;
227 unsigned numRegs;
Che-Liang Chioudf659632010-11-08 03:06:08 +0000228
Justin Holewinski297984d2011-09-22 16:45:40 +0000229 // pred
230 numRegs = MFI->getNumRegistersForClass(PTX::RegPredRegisterClass);
231 if(numRegs > 0) {
232 regDefs += "\t.reg .pred %p<";
233 regDefs += utostr(numRegs);
234 regDefs += ">;\n";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000235 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000236
Justin Holewinski297984d2011-09-22 16:45:40 +0000237 // i16
238 numRegs = MFI->getNumRegistersForClass(PTX::RegI16RegisterClass);
239 if(numRegs > 0) {
240 regDefs += "\t.reg .b16 %rh<";
241 regDefs += utostr(numRegs);
242 regDefs += ">;\n";
243 }
244
245 // i32
246 numRegs = MFI->getNumRegistersForClass(PTX::RegI32RegisterClass);
247 if(numRegs > 0) {
248 regDefs += "\t.reg .b32 %r<";
249 regDefs += utostr(numRegs);
250 regDefs += ">;\n";
251 }
252
253 // i64
254 numRegs = MFI->getNumRegistersForClass(PTX::RegI64RegisterClass);
255 if(numRegs > 0) {
256 regDefs += "\t.reg .b64 %rd<";
257 regDefs += utostr(numRegs);
258 regDefs += ">;\n";
259 }
260
261 // f32
262 numRegs = MFI->getNumRegistersForClass(PTX::RegF32RegisterClass);
263 if(numRegs > 0) {
264 regDefs += "\t.reg .f32 %f<";
265 regDefs += utostr(numRegs);
266 regDefs += ">;\n";
267 }
268
269 // f64
270 numRegs = MFI->getNumRegistersForClass(PTX::RegF64RegisterClass);
271 if(numRegs > 0) {
272 regDefs += "\t.reg .f64 %fd<";
273 regDefs += utostr(numRegs);
274 regDefs += ">;\n";
275 }
276
277 OutStreamer.EmitRawText(Twine(regDefs));
278
279
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000280 const MachineFrameInfo* FrameInfo = MF->getFrameInfo();
Justin Holewinski08d03162011-06-22 16:07:03 +0000281 DEBUG(dbgs() << "Have " << FrameInfo->getNumObjects()
282 << " frame object(s)\n");
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000283 for (unsigned i = 0, e = FrameInfo->getNumObjects(); i != e; ++i) {
284 DEBUG(dbgs() << "Size of object: " << FrameInfo->getObjectSize(i) << "\n");
Justin Holewinski08d03162011-06-22 16:07:03 +0000285 if (FrameInfo->getObjectSize(i) > 0) {
286 std::string def = "\t.reg .b";
287 def += utostr(FrameInfo->getObjectSize(i)*8); // Convert to bits
288 def += " s";
289 def += utostr(i);
290 def += ";";
291 OutStreamer.EmitRawText(Twine(def));
292 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000293 }
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000294
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000295 //unsigned Index = 1;
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000296 // Print parameter passing params
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000297 //for (PTXMachineFunctionInfo::param_iterator
298 // i = MFI->paramBegin(), e = MFI->paramEnd(); i != e; ++i) {
299 // std::string def = "\t.param .b";
300 // def += utostr(*i);
301 // def += " __ret_";
302 // def += utostr(Index);
303 // Index++;
304 // def += ";";
305 // OutStreamer.EmitRawText(Twine(def));
306 //}
Che-Liang Chioudf659632010-11-08 03:06:08 +0000307}
308
Eric Christopher50880d02010-09-18 18:52:28 +0000309void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000310 std::string str;
311 str.reserve(64);
312
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000313 raw_string_ostream OS(str);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000314
Justin Holewinski47997292011-06-24 19:19:18 +0000315 DebugLoc DL = MI->getDebugLoc();
316 if (!DL.isUnknown()) {
317
318 const MDNode *S = DL.getScope(MF->getFunction()->getContext());
319
320 // This is taken from DwarfDebug.cpp, which is conveniently not a public
321 // LLVM class.
322 StringRef Fn;
323 StringRef Dir;
324 unsigned Src = 1;
325 if (S) {
326 DIDescriptor Scope(S);
327 if (Scope.isCompileUnit()) {
328 DICompileUnit CU(S);
329 Fn = CU.getFilename();
330 Dir = CU.getDirectory();
331 } else if (Scope.isFile()) {
332 DIFile F(S);
333 Fn = F.getFilename();
334 Dir = F.getDirectory();
335 } else if (Scope.isSubprogram()) {
336 DISubprogram SP(S);
337 Fn = SP.getFilename();
338 Dir = SP.getDirectory();
339 } else if (Scope.isLexicalBlock()) {
340 DILexicalBlock DB(S);
341 Fn = DB.getFilename();
342 Dir = DB.getDirectory();
343 } else
344 assert(0 && "Unexpected scope info");
345
346 Src = GetOrCreateSourceID(Fn, Dir);
347 }
348 OutStreamer.EmitDwarfLocDirective(Src, DL.getLine(), DL.getCol(),
349 0, 0, 0, Fn);
350
351 const MCDwarfLoc& MDL = OutContext.getCurrentDwarfLoc();
352
353 OS << "\t.loc ";
354 OS << utostr(MDL.getFileNum());
355 OS << " ";
356 OS << utostr(MDL.getLine());
357 OS << " ";
358 OS << utostr(MDL.getColumn());
359 OS << "\n";
360 }
361
362
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000363 // Emit predicate
364 printPredicateOperand(MI, OS);
365
366 // Write instruction to str
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000367 if (MI->getOpcode() == PTX::CALL) {
368 printCall(MI, OS);
369 } else {
370 printInstruction(MI, OS);
371 }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000372 OS << ';';
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000373 OS.flush();
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000374
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000375 StringRef strref = StringRef(str);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000376 OutStreamer.EmitRawText(strref);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000377}
378
379void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
380 raw_ostream &OS) {
381 const MachineOperand &MO = MI->getOperand(opNum);
Justin Holewinski297984d2011-09-22 16:45:40 +0000382 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000383
384 switch (MO.getType()) {
385 default:
386 llvm_unreachable("<unknown operand type>");
387 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000388 case MachineOperand::MO_GlobalAddress:
389 OS << *Mang->getSymbol(MO.getGlobal());
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000390 break;
391 case MachineOperand::MO_Immediate:
Justin Holewinski9583a862011-04-28 00:19:50 +0000392 OS << (long) MO.getImm();
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000393 break;
Che-Liang Chiou88d33672011-03-18 11:08:52 +0000394 case MachineOperand::MO_MachineBasicBlock:
395 OS << *MO.getMBB()->getSymbol();
396 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000397 case MachineOperand::MO_Register:
Justin Holewinski297984d2011-09-22 16:45:40 +0000398 OS << MFI->getRegisterName(MO.getReg());
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000399 break;
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000400 case MachineOperand::MO_FPImmediate:
401 APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt();
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000402 bool isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID;
403 // Emit 0F for 32-bit floats and 0D for 64-bit doubles.
404 if (isFloat) {
405 OS << "0F";
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000406 }
407 else {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000408 OS << "0D";
409 }
410 // Emit the encoded floating-point value.
411 if (constFP.getZExtValue() > 0) {
412 OS << constFP.toString(16, false);
413 }
414 else {
415 OS << "00000000";
416 // If We have a double-precision zero, pad to 8-bytes.
417 if (!isFloat) {
418 OS << "00000000";
419 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000420 }
421 break;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000422 }
Eric Christopher50880d02010-09-18 18:52:28 +0000423}
424
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000425void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
426 raw_ostream &OS, const char *Modifier) {
427 printOperand(MI, opNum, OS);
428
429 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
430 return; // don't print "+0"
431
432 OS << "+";
433 printOperand(MI, opNum+1, OS);
434}
435
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000436void PTXAsmPrinter::printParamOperand(const MachineInstr *MI, int opNum,
437 raw_ostream &OS, const char *Modifier) {
438 OS << PARAM_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
439}
440
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000441void PTXAsmPrinter::printReturnOperand(const MachineInstr *MI, int opNum,
442 raw_ostream &OS, const char *Modifier) {
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000443 //OS << RETURN_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
444 OS << "__ret";
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000445}
446
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000447void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
448 // Check to see if this is a special global used by LLVM, if so, emit it.
449 if (EmitSpecialLLVMGlobal(gv))
450 return;
451
452 MCSymbol *gvsym = Mang->getSymbol(gv);
453
454 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
455
456 std::string decl;
457
458 // check if it is defined in some other translation unit
459 if (gv->isDeclaration())
460 decl += ".extern ";
461
462 // state space: e.g., .global
463 decl += ".";
464 decl += getStateSpaceName(gv->getType()->getAddressSpace());
465 decl += " ";
466
467 // alignment (optional)
468 unsigned alignment = gv->getAlignment();
469 if (alignment != 0) {
470 decl += ".align ";
471 decl += utostr(Log2_32(gv->getAlignment()));
472 decl += " ";
473 }
474
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000475
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000476 if (PointerType::classof(gv->getType())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000477 PointerType* pointerTy = dyn_cast<PointerType>(gv->getType());
478 Type* elementTy = pointerTy->getElementType();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000479
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000480 decl += ".b8 ";
481 decl += gvsym->getName();
482 decl += "[";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000483
Justin Holewinski9583a862011-04-28 00:19:50 +0000484 if (elementTy->isArrayTy())
485 {
486 assert(elementTy->isArrayTy() && "Only pointers to arrays are supported");
487
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000488 ArrayType* arrayTy = dyn_cast<ArrayType>(elementTy);
Justin Holewinski9583a862011-04-28 00:19:50 +0000489 elementTy = arrayTy->getElementType();
490
491 unsigned numElements = arrayTy->getNumElements();
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000492
Justin Holewinski9583a862011-04-28 00:19:50 +0000493 while (elementTy->isArrayTy()) {
494
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000495 arrayTy = dyn_cast<ArrayType>(elementTy);
Justin Holewinski9583a862011-04-28 00:19:50 +0000496 elementTy = arrayTy->getElementType();
497
498 numElements *= arrayTy->getNumElements();
499 }
500
501 // FIXME: isPrimitiveType() == false for i16?
502 assert(elementTy->isSingleValueType() &&
503 "Non-primitive types are not handled");
504
505 // Compute the size of the array, in bytes.
506 uint64_t arraySize = (elementTy->getPrimitiveSizeInBits() >> 3)
507 * numElements;
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000508
Justin Holewinski9583a862011-04-28 00:19:50 +0000509 decl += utostr(arraySize);
510 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000511
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000512 decl += "]";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000513
Justin Holewinski9583a862011-04-28 00:19:50 +0000514 // handle string constants (assume ConstantArray means string)
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000515
Justin Holewinski9583a862011-04-28 00:19:50 +0000516 if (gv->hasInitializer())
517 {
Justin Holewinski297984d2011-09-22 16:45:40 +0000518 const Constant *C = gv->getInitializer();
Justin Holewinski9583a862011-04-28 00:19:50 +0000519 if (const ConstantArray *CA = dyn_cast<ConstantArray>(C))
520 {
521 decl += " = {";
522
523 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
524 {
525 if (i > 0) decl += ",";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000526
527 decl += "0x" +
528 utohexstr(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());
Justin Holewinski9583a862011-04-28 00:19:50 +0000529 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000530
Justin Holewinski9583a862011-04-28 00:19:50 +0000531 decl += "}";
532 }
533 }
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000534 }
535 else {
536 // Note: this is currently the fall-through case and most likely generates
537 // incorrect code.
538 decl += getTypeName(gv->getType());
539 decl += " ";
540
541 decl += gvsym->getName();
542
543 if (ArrayType::classof(gv->getType()) ||
544 PointerType::classof(gv->getType()))
545 decl += "[]";
546 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000547
548 decl += ";";
549
550 OutStreamer.EmitRawText(Twine(decl));
551
552 OutStreamer.AddBlankLine();
553}
554
Che-Liang Chioudf659632010-11-08 03:06:08 +0000555void PTXAsmPrinter::EmitFunctionDeclaration() {
556 // The function label could have already been emitted if two symbols end up
557 // conflicting due to asm renaming. Detect this and emit an error.
558 if (!CurrentFnSym->isUndefined()) {
559 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
560 "' label emitted multiple times to assembly file");
561 return;
562 }
563
564 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
565 const bool isKernel = MFI->isKernel();
Justin Holewinski67a91842011-06-23 18:10:03 +0000566 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000567 const MachineRegisterInfo& MRI = MF->getRegInfo();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000568
569 std::string decl = isKernel ? ".entry" : ".func";
570
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000571 unsigned cnt = 0;
572
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000573 if (!isKernel) {
574 decl += " (";
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000575 if (ST.useParamSpaceForDeviceArgs() && MFI->getRetParamSize() != 0) {
576 decl += ".param .b";
577 decl += utostr(MFI->getRetParamSize());
578 decl += " __ret";
579 } else {
580 for (PTXMachineFunctionInfo::ret_iterator
581 i = MFI->retRegBegin(), e = MFI->retRegEnd(), b = i;
582 i != e; ++i) {
583 if (i != b) {
584 decl += ", ";
585 }
586 decl += ".reg .";
587 decl += getRegisterTypeName(*i, MRI);
588 decl += " ";
589 decl += MFI->getRegisterName(*i);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000590 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000591 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000592 decl += ")";
593 }
594
595 // Print function name
596 decl += " ";
597 decl += CurrentFnSym->getName().str();
598
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000599 decl += " (";
600
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000601 cnt = 0;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000602
603 // Print parameters
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000604 if (isKernel || ST.useParamSpaceForDeviceArgs()) {
605 for (PTXMachineFunctionInfo::argparam_iterator
606 i = MFI->argParamBegin(), e = MFI->argParamEnd(), b = i;
607 i != e; ++i) {
608 if (i != b) {
609 decl += ", ";
610 }
611
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000612 decl += ".param .b";
613 decl += utostr(*i);
614 decl += " ";
615 decl += PARAM_PREFIX;
616 decl += utostr(++cnt);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000617 }
618 } else {
619 for (PTXMachineFunctionInfo::reg_iterator
620 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i;
621 i != e; ++i) {
622 if (i != b) {
623 decl += ", ";
624 }
625
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000626 decl += ".reg .";
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000627 decl += getRegisterTypeName(*i, MRI);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000628 decl += " ";
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000629 decl += MFI->getRegisterName(*i);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000630 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000631 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000632 decl += ")";
633
Che-Liang Chioudf659632010-11-08 03:06:08 +0000634 OutStreamer.EmitRawText(Twine(decl));
635}
636
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000637void PTXAsmPrinter::
638printPredicateOperand(const MachineInstr *MI, raw_ostream &O) {
639 int i = MI->findFirstPredOperandIdx();
640 if (i == -1)
641 llvm_unreachable("missing predicate operand");
642
643 unsigned reg = MI->getOperand(i).getReg();
644 int predOp = MI->getOperand(i+1).getImm();
Justin Holewinski297984d2011-09-22 16:45:40 +0000645 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000646
647 DEBUG(dbgs() << "predicate: (" << reg << ", " << predOp << ")\n");
648
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000649 if (reg != PTX::NoRegister) {
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000650 O << '@';
651 if (predOp == PTX::PRED_NEGATE)
652 O << '!';
Justin Holewinski297984d2011-09-22 16:45:40 +0000653 O << MFI->getRegisterName(reg);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000654 }
655}
656
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000657void PTXAsmPrinter::
658printCall(const MachineInstr *MI, raw_ostream &O) {
659
660 O << "\tcall.uni\t";
661
662 const GlobalValue *Address = MI->getOperand(2).getGlobal();
663 O << Address->getName() << ", (";
664
665 // (0,1) : predicate register/flag
666 // (2) : callee
667 for (unsigned i = 3; i < MI->getNumOperands(); ++i) {
668 //const MachineOperand& MO = MI->getOperand(i);
669
670 printReturnOperand(MI, i, O);
671 if (i < MI->getNumOperands()-1) {
672 O << ", ";
673 }
674 }
675
676 O << ")";
677}
678
Justin Holewinski47997292011-06-24 19:19:18 +0000679unsigned PTXAsmPrinter::GetOrCreateSourceID(StringRef FileName,
680 StringRef DirName) {
681 // If FE did not provide a file name, then assume stdin.
682 if (FileName.empty())
683 return GetOrCreateSourceID("<stdin>", StringRef());
684
685 // MCStream expects full path name as filename.
686 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
687 SmallString<128> FullPathName = DirName;
688 sys::path::append(FullPathName, FileName);
689 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
690 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
691 }
692
693 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
694 if (Entry.getValue())
695 return Entry.getValue();
696
697 unsigned SrcId = SourceIdMap.size();
698 Entry.setValue(SrcId);
699
700 // Print out a .file directive to specify files for .loc directives.
701 OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
702
703 return SrcId;
704}
705
Eric Christopher50880d02010-09-18 18:52:28 +0000706#include "PTXGenAsmWriter.inc"
707
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000708// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000709extern "C" void LLVMInitializePTXAsmPrinter() {
Justin Holewinskie1fee482011-04-20 15:37:17 +0000710 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target);
711 RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target);
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000712}