blob: f936d4bb3c4e66bc226214524c87285f69c80795 [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
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000094static const char *getRegisterTypeName(unsigned RegNo) {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000095#define TEST_REGCLS(cls, clsstr) \
Che-Liang Chioudf659632010-11-08 03:06:08 +000096 if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr;
Justin Holewinski1b91bcd2011-06-16 17:49:58 +000097 TEST_REGCLS(RegPred, pred);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +000098 TEST_REGCLS(RegI16, b16);
99 TEST_REGCLS(RegI32, b32);
100 TEST_REGCLS(RegI64, b64);
101 TEST_REGCLS(RegF32, b32);
102 TEST_REGCLS(RegF64, b64);
Che-Liang Chioudf659632010-11-08 03:06:08 +0000103#undef TEST_REGCLS
104
105 llvm_unreachable("Not in any register class!");
106 return NULL;
107}
108
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000109static const char *getStateSpaceName(unsigned addressSpace) {
Che-Liang Chioud34f19f2010-12-30 10:41:27 +0000110 switch (addressSpace) {
111 default: llvm_unreachable("Unknown state space");
112 case PTX::GLOBAL: return "global";
113 case PTX::CONSTANT: return "const";
114 case PTX::LOCAL: return "local";
115 case PTX::PARAMETER: return "param";
116 case PTX::SHARED: return "shared";
117 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000118 return NULL;
119}
120
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000121static const char *getTypeName(Type* type) {
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000122 while (true) {
123 switch (type->getTypeID()) {
124 default: llvm_unreachable("Unknown type");
125 case Type::FloatTyID: return ".f32";
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000126 case Type::DoubleTyID: return ".f64";
127 case Type::IntegerTyID:
128 switch (type->getPrimitiveSizeInBits()) {
129 default: llvm_unreachable("Unknown integer bit-width");
130 case 16: return ".u16";
131 case 32: return ".u32";
132 case 64: return ".u64";
133 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000134 case Type::ArrayTyID:
135 case Type::PointerTyID:
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000136 type = dyn_cast<SequentialType>(type)->getElementType();
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000137 break;
138 }
139 }
140 return NULL;
141}
142
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000143bool PTXAsmPrinter::doFinalization(Module &M) {
144 // XXX Temproarily remove global variables so that doFinalization() will not
145 // emit them again (global variables are emitted at beginning).
146
147 Module::GlobalListType &global_list = M.getGlobalList();
148 int i, n = global_list.size();
149 GlobalVariable **gv_array = new GlobalVariable* [n];
150
151 // first, back-up GlobalVariable in gv_array
152 i = 0;
153 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
154 I != E; ++I)
155 gv_array[i++] = &*I;
156
157 // second, empty global_list
158 while (!global_list.empty())
159 global_list.remove(global_list.begin());
160
161 // call doFinalization
162 bool ret = AsmPrinter::doFinalization(M);
163
164 // now we restore global variables
165 for (i = 0; i < n; i ++)
166 global_list.insert(global_list.end(), gv_array[i]);
167
168 delete[] gv_array;
169 return ret;
170}
171
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000172void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
173{
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000174 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
175
176 OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString()));
177 OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() +
Justin Holewinski12785e82011-03-03 13:34:29 +0000178 (ST.supportsDouble() ? ""
179 : ", map_f64_to_f32")));
Justin Holewinskia9c85f92011-06-22 00:43:56 +0000180 // .address_size directive is optional, but it must immediately follow
181 // the .target directive if present within a module
182 if (ST.supportsPTX23()) {
183 std::string addrSize = ST.is64Bit() ? "64" : "32";
184 OutStreamer.EmitRawText(Twine("\t.address_size " + addrSize));
185 }
186
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000187 OutStreamer.AddBlankLine();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000188
Justin Holewinski47997292011-06-24 19:19:18 +0000189 // Define any .file directives
190 DebugInfoFinder DbgFinder;
191 DbgFinder.processModule(M);
192
193 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
194 E = DbgFinder.compile_unit_end(); I != E; ++I) {
195 DICompileUnit DIUnit(*I);
196 StringRef FN = DIUnit.getFilename();
197 StringRef Dir = DIUnit.getDirectory();
198 GetOrCreateSourceID(FN, Dir);
199 }
200
201 OutStreamer.AddBlankLine();
202
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000203 // declare global variables
204 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
205 i != e; ++i)
206 EmitVariableDeclaration(i);
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000207}
208
Che-Liang Chioudf659632010-11-08 03:06:08 +0000209bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
210 SetupMachineFunction(MF);
211 EmitFunctionDeclaration();
212 EmitFunctionBody();
213 return false;
214}
215
216void PTXAsmPrinter::EmitFunctionBodyStart() {
217 OutStreamer.EmitRawText(Twine("{"));
218
219 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
220
Justin Holewinski297984d2011-09-22 16:45:40 +0000221 // Print register definitions
222 std::string regDefs;
223 unsigned numRegs;
Che-Liang Chioudf659632010-11-08 03:06:08 +0000224
Justin Holewinski297984d2011-09-22 16:45:40 +0000225 // pred
226 numRegs = MFI->getNumRegistersForClass(PTX::RegPredRegisterClass);
227 if(numRegs > 0) {
228 regDefs += "\t.reg .pred %p<";
229 regDefs += utostr(numRegs);
230 regDefs += ">;\n";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000231 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000232
Justin Holewinski297984d2011-09-22 16:45:40 +0000233 // i16
234 numRegs = MFI->getNumRegistersForClass(PTX::RegI16RegisterClass);
235 if(numRegs > 0) {
236 regDefs += "\t.reg .b16 %rh<";
237 regDefs += utostr(numRegs);
238 regDefs += ">;\n";
239 }
240
241 // i32
242 numRegs = MFI->getNumRegistersForClass(PTX::RegI32RegisterClass);
243 if(numRegs > 0) {
244 regDefs += "\t.reg .b32 %r<";
245 regDefs += utostr(numRegs);
246 regDefs += ">;\n";
247 }
248
249 // i64
250 numRegs = MFI->getNumRegistersForClass(PTX::RegI64RegisterClass);
251 if(numRegs > 0) {
252 regDefs += "\t.reg .b64 %rd<";
253 regDefs += utostr(numRegs);
254 regDefs += ">;\n";
255 }
256
257 // f32
258 numRegs = MFI->getNumRegistersForClass(PTX::RegF32RegisterClass);
259 if(numRegs > 0) {
260 regDefs += "\t.reg .f32 %f<";
261 regDefs += utostr(numRegs);
262 regDefs += ">;\n";
263 }
264
265 // f64
266 numRegs = MFI->getNumRegistersForClass(PTX::RegF64RegisterClass);
267 if(numRegs > 0) {
268 regDefs += "\t.reg .f64 %fd<";
269 regDefs += utostr(numRegs);
270 regDefs += ">;\n";
271 }
272
273 OutStreamer.EmitRawText(Twine(regDefs));
274
275
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000276 const MachineFrameInfo* FrameInfo = MF->getFrameInfo();
Justin Holewinski08d03162011-06-22 16:07:03 +0000277 DEBUG(dbgs() << "Have " << FrameInfo->getNumObjects()
278 << " frame object(s)\n");
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000279 for (unsigned i = 0, e = FrameInfo->getNumObjects(); i != e; ++i) {
280 DEBUG(dbgs() << "Size of object: " << FrameInfo->getObjectSize(i) << "\n");
Justin Holewinski08d03162011-06-22 16:07:03 +0000281 if (FrameInfo->getObjectSize(i) > 0) {
282 std::string def = "\t.reg .b";
283 def += utostr(FrameInfo->getObjectSize(i)*8); // Convert to bits
284 def += " s";
285 def += utostr(i);
286 def += ";";
287 OutStreamer.EmitRawText(Twine(def));
288 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000289 }
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000290
291 unsigned Index = 1;
292 // Print parameter passing params
293 for (PTXMachineFunctionInfo::param_iterator
294 i = MFI->paramBegin(), e = MFI->paramEnd(); i != e; ++i) {
295 std::string def = "\t.param .b";
296 def += utostr(*i);
297 def += " __ret_";
298 def += utostr(Index);
299 Index++;
300 def += ";";
301 OutStreamer.EmitRawText(Twine(def));
302 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000303}
304
Eric Christopher50880d02010-09-18 18:52:28 +0000305void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000306 std::string str;
307 str.reserve(64);
308
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000309 raw_string_ostream OS(str);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000310
Justin Holewinski47997292011-06-24 19:19:18 +0000311 DebugLoc DL = MI->getDebugLoc();
312 if (!DL.isUnknown()) {
313
314 const MDNode *S = DL.getScope(MF->getFunction()->getContext());
315
316 // This is taken from DwarfDebug.cpp, which is conveniently not a public
317 // LLVM class.
318 StringRef Fn;
319 StringRef Dir;
320 unsigned Src = 1;
321 if (S) {
322 DIDescriptor Scope(S);
323 if (Scope.isCompileUnit()) {
324 DICompileUnit CU(S);
325 Fn = CU.getFilename();
326 Dir = CU.getDirectory();
327 } else if (Scope.isFile()) {
328 DIFile F(S);
329 Fn = F.getFilename();
330 Dir = F.getDirectory();
331 } else if (Scope.isSubprogram()) {
332 DISubprogram SP(S);
333 Fn = SP.getFilename();
334 Dir = SP.getDirectory();
335 } else if (Scope.isLexicalBlock()) {
336 DILexicalBlock DB(S);
337 Fn = DB.getFilename();
338 Dir = DB.getDirectory();
339 } else
340 assert(0 && "Unexpected scope info");
341
342 Src = GetOrCreateSourceID(Fn, Dir);
343 }
344 OutStreamer.EmitDwarfLocDirective(Src, DL.getLine(), DL.getCol(),
345 0, 0, 0, Fn);
346
347 const MCDwarfLoc& MDL = OutContext.getCurrentDwarfLoc();
348
349 OS << "\t.loc ";
350 OS << utostr(MDL.getFileNum());
351 OS << " ";
352 OS << utostr(MDL.getLine());
353 OS << " ";
354 OS << utostr(MDL.getColumn());
355 OS << "\n";
356 }
357
358
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000359 // Emit predicate
360 printPredicateOperand(MI, OS);
361
362 // Write instruction to str
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000363 if (MI->getOpcode() == PTX::CALL) {
364 printCall(MI, OS);
365 } else {
366 printInstruction(MI, OS);
367 }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000368 OS << ';';
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000369 OS.flush();
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000370
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000371 StringRef strref = StringRef(str);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000372 OutStreamer.EmitRawText(strref);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000373}
374
375void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
376 raw_ostream &OS) {
377 const MachineOperand &MO = MI->getOperand(opNum);
Justin Holewinski297984d2011-09-22 16:45:40 +0000378 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000379
380 switch (MO.getType()) {
381 default:
382 llvm_unreachable("<unknown operand type>");
383 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000384 case MachineOperand::MO_GlobalAddress:
385 OS << *Mang->getSymbol(MO.getGlobal());
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000386 break;
387 case MachineOperand::MO_Immediate:
Justin Holewinski9583a862011-04-28 00:19:50 +0000388 OS << (long) MO.getImm();
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000389 break;
Che-Liang Chiou88d33672011-03-18 11:08:52 +0000390 case MachineOperand::MO_MachineBasicBlock:
391 OS << *MO.getMBB()->getSymbol();
392 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000393 case MachineOperand::MO_Register:
Justin Holewinski297984d2011-09-22 16:45:40 +0000394 OS << MFI->getRegisterName(MO.getReg());
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000395 break;
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000396 case MachineOperand::MO_FPImmediate:
397 APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt();
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000398 bool isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID;
399 // Emit 0F for 32-bit floats and 0D for 64-bit doubles.
400 if (isFloat) {
401 OS << "0F";
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000402 }
403 else {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000404 OS << "0D";
405 }
406 // Emit the encoded floating-point value.
407 if (constFP.getZExtValue() > 0) {
408 OS << constFP.toString(16, false);
409 }
410 else {
411 OS << "00000000";
412 // If We have a double-precision zero, pad to 8-bytes.
413 if (!isFloat) {
414 OS << "00000000";
415 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000416 }
417 break;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000418 }
Eric Christopher50880d02010-09-18 18:52:28 +0000419}
420
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000421void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
422 raw_ostream &OS, const char *Modifier) {
423 printOperand(MI, opNum, OS);
424
425 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
426 return; // don't print "+0"
427
428 OS << "+";
429 printOperand(MI, opNum+1, OS);
430}
431
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000432void PTXAsmPrinter::printParamOperand(const MachineInstr *MI, int opNum,
433 raw_ostream &OS, const char *Modifier) {
434 OS << PARAM_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
435}
436
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000437void PTXAsmPrinter::printReturnOperand(const MachineInstr *MI, int opNum,
438 raw_ostream &OS, const char *Modifier) {
439 OS << RETURN_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
440}
441
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000442void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
443 // Check to see if this is a special global used by LLVM, if so, emit it.
444 if (EmitSpecialLLVMGlobal(gv))
445 return;
446
447 MCSymbol *gvsym = Mang->getSymbol(gv);
448
449 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
450
451 std::string decl;
452
453 // check if it is defined in some other translation unit
454 if (gv->isDeclaration())
455 decl += ".extern ";
456
457 // state space: e.g., .global
458 decl += ".";
459 decl += getStateSpaceName(gv->getType()->getAddressSpace());
460 decl += " ";
461
462 // alignment (optional)
463 unsigned alignment = gv->getAlignment();
464 if (alignment != 0) {
465 decl += ".align ";
466 decl += utostr(Log2_32(gv->getAlignment()));
467 decl += " ";
468 }
469
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000470
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000471 if (PointerType::classof(gv->getType())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000472 PointerType* pointerTy = dyn_cast<PointerType>(gv->getType());
473 Type* elementTy = pointerTy->getElementType();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000474
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000475 decl += ".b8 ";
476 decl += gvsym->getName();
477 decl += "[";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000478
Justin Holewinski9583a862011-04-28 00:19:50 +0000479 if (elementTy->isArrayTy())
480 {
481 assert(elementTy->isArrayTy() && "Only pointers to arrays are supported");
482
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000483 ArrayType* arrayTy = dyn_cast<ArrayType>(elementTy);
Justin Holewinski9583a862011-04-28 00:19:50 +0000484 elementTy = arrayTy->getElementType();
485
486 unsigned numElements = arrayTy->getNumElements();
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000487
Justin Holewinski9583a862011-04-28 00:19:50 +0000488 while (elementTy->isArrayTy()) {
489
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000490 arrayTy = dyn_cast<ArrayType>(elementTy);
Justin Holewinski9583a862011-04-28 00:19:50 +0000491 elementTy = arrayTy->getElementType();
492
493 numElements *= arrayTy->getNumElements();
494 }
495
496 // FIXME: isPrimitiveType() == false for i16?
497 assert(elementTy->isSingleValueType() &&
498 "Non-primitive types are not handled");
499
500 // Compute the size of the array, in bytes.
501 uint64_t arraySize = (elementTy->getPrimitiveSizeInBits() >> 3)
502 * numElements;
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000503
Justin Holewinski9583a862011-04-28 00:19:50 +0000504 decl += utostr(arraySize);
505 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000506
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000507 decl += "]";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000508
Justin Holewinski9583a862011-04-28 00:19:50 +0000509 // handle string constants (assume ConstantArray means string)
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000510
Justin Holewinski9583a862011-04-28 00:19:50 +0000511 if (gv->hasInitializer())
512 {
Justin Holewinski297984d2011-09-22 16:45:40 +0000513 const Constant *C = gv->getInitializer();
Justin Holewinski9583a862011-04-28 00:19:50 +0000514 if (const ConstantArray *CA = dyn_cast<ConstantArray>(C))
515 {
516 decl += " = {";
517
518 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
519 {
520 if (i > 0) decl += ",";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000521
522 decl += "0x" +
523 utohexstr(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());
Justin Holewinski9583a862011-04-28 00:19:50 +0000524 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000525
Justin Holewinski9583a862011-04-28 00:19:50 +0000526 decl += "}";
527 }
528 }
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000529 }
530 else {
531 // Note: this is currently the fall-through case and most likely generates
532 // incorrect code.
533 decl += getTypeName(gv->getType());
534 decl += " ";
535
536 decl += gvsym->getName();
537
538 if (ArrayType::classof(gv->getType()) ||
539 PointerType::classof(gv->getType()))
540 decl += "[]";
541 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000542
543 decl += ";";
544
545 OutStreamer.EmitRawText(Twine(decl));
546
547 OutStreamer.AddBlankLine();
548}
549
Che-Liang Chioudf659632010-11-08 03:06:08 +0000550void PTXAsmPrinter::EmitFunctionDeclaration() {
551 // The function label could have already been emitted if two symbols end up
552 // conflicting due to asm renaming. Detect this and emit an error.
553 if (!CurrentFnSym->isUndefined()) {
554 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
555 "' label emitted multiple times to assembly file");
556 return;
557 }
558
559 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
560 const bool isKernel = MFI->isKernel();
Justin Holewinski67a91842011-06-23 18:10:03 +0000561 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000562
563 std::string decl = isKernel ? ".entry" : ".func";
564
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000565 unsigned cnt = 0;
566
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000567 if (!isKernel) {
568 decl += " (";
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000569 for (PTXMachineFunctionInfo::ret_iterator
570 i = MFI->retRegBegin(), e = MFI->retRegEnd(), b = i;
571 i != e; ++i) {
572 if (i != b) {
573 decl += ", ";
574 }
Justin Holewinskid8149c12011-06-23 18:10:13 +0000575 decl += ".reg .";
576 decl += getRegisterTypeName(*i);
577 decl += " ";
578 decl += getRegisterName(*i);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000579 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000580 decl += ")";
581 }
582
583 // Print function name
584 decl += " ";
585 decl += CurrentFnSym->getName().str();
586
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000587 decl += " (";
588
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000589 cnt = 0;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000590
591 // Print parameters
592 for (PTXMachineFunctionInfo::reg_iterator
593 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i;
594 i != e; ++i) {
595 if (i != b) {
596 decl += ", ";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000597 }
Justin Holewinski35f4fb32011-06-24 16:27:49 +0000598 if (isKernel || ST.useParamSpaceForDeviceArgs()) {
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000599 decl += ".param .b";
600 decl += utostr(*i);
601 decl += " ";
602 decl += PARAM_PREFIX;
603 decl += utostr(++cnt);
604 } else {
605 decl += ".reg .";
606 decl += getRegisterTypeName(*i);
607 decl += " ";
608 decl += getRegisterName(*i);
609 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000610 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000611 decl += ")";
612
Che-Liang Chioudf659632010-11-08 03:06:08 +0000613 OutStreamer.EmitRawText(Twine(decl));
614}
615
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000616void PTXAsmPrinter::
617printPredicateOperand(const MachineInstr *MI, raw_ostream &O) {
618 int i = MI->findFirstPredOperandIdx();
619 if (i == -1)
620 llvm_unreachable("missing predicate operand");
621
622 unsigned reg = MI->getOperand(i).getReg();
623 int predOp = MI->getOperand(i+1).getImm();
Justin Holewinski297984d2011-09-22 16:45:40 +0000624 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000625
626 DEBUG(dbgs() << "predicate: (" << reg << ", " << predOp << ")\n");
627
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000628 if (reg != PTX::NoRegister) {
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000629 O << '@';
630 if (predOp == PTX::PRED_NEGATE)
631 O << '!';
Justin Holewinski297984d2011-09-22 16:45:40 +0000632 O << MFI->getRegisterName(reg);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000633 }
634}
635
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000636void PTXAsmPrinter::
637printCall(const MachineInstr *MI, raw_ostream &O) {
638
639 O << "\tcall.uni\t";
640
641 const GlobalValue *Address = MI->getOperand(2).getGlobal();
642 O << Address->getName() << ", (";
643
644 // (0,1) : predicate register/flag
645 // (2) : callee
646 for (unsigned i = 3; i < MI->getNumOperands(); ++i) {
647 //const MachineOperand& MO = MI->getOperand(i);
648
649 printReturnOperand(MI, i, O);
650 if (i < MI->getNumOperands()-1) {
651 O << ", ";
652 }
653 }
654
655 O << ")";
656}
657
Justin Holewinski47997292011-06-24 19:19:18 +0000658unsigned PTXAsmPrinter::GetOrCreateSourceID(StringRef FileName,
659 StringRef DirName) {
660 // If FE did not provide a file name, then assume stdin.
661 if (FileName.empty())
662 return GetOrCreateSourceID("<stdin>", StringRef());
663
664 // MCStream expects full path name as filename.
665 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
666 SmallString<128> FullPathName = DirName;
667 sys::path::append(FullPathName, FileName);
668 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
669 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
670 }
671
672 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
673 if (Entry.getValue())
674 return Entry.getValue();
675
676 unsigned SrcId = SourceIdMap.size();
677 Entry.setValue(SrcId);
678
679 // Print out a .file directive to specify files for .loc directives.
680 OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
681
682 return SrcId;
683}
684
Eric Christopher50880d02010-09-18 18:52:28 +0000685#include "PTXGenAsmWriter.inc"
686
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000687// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000688extern "C" void LLVMInitializePTXAsmPrinter() {
Justin Holewinskie1fee482011-04-20 15:37:17 +0000689 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target);
690 RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target);
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000691}