blob: 719567b99b3036c6f4986b6c0a9c244198fbba1c [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"
Justin Holewinskid8e4ed22011-09-28 14:32:04 +000018#include "PTXAsmPrinter.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000019#include "PTXMachineFunctionInfo.h"
Justin Holewinski27f08fc2011-09-23 14:18:22 +000020#include "PTXParamManager.h"
Justin Holewinski297984d2011-09-22 16:45:40 +000021#include "PTXRegisterInfo.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000022#include "PTXTargetMachine.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000023#include "llvm/DerivedTypes.h"
24#include "llvm/Module.h"
Eric Christopher50880d02010-09-18 18:52:28 +000025#include "llvm/ADT/SmallString.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000026#include "llvm/ADT/StringExtras.h"
27#include "llvm/ADT/Twine.h"
Justin Holewinski47997292011-06-24 19:19:18 +000028#include "llvm/Analysis/DebugInfo.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000029#include "llvm/CodeGen/AsmPrinter.h"
Justin Holewinskidf1c8d82011-06-20 15:56:20 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000031#include "llvm/CodeGen/MachineInstr.h"
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Justin Holewinski47997292011-06-24 19:19:18 +000033#include "llvm/MC/MCContext.h"
Justin Holewinskid8e4ed22011-09-28 14:32:04 +000034#include "llvm/MC/MCExpr.h"
35#include "llvm/MC/MCInst.h"
Eric Christopher50880d02010-09-18 18:52:28 +000036#include "llvm/MC/MCStreamer.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000037#include "llvm/MC/MCSymbol.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000038#include "llvm/Target/Mangler.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000039#include "llvm/Target/TargetLoweringObjectFile.h"
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000040#include "llvm/Support/CommandLine.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000041#include "llvm/Support/Debug.h"
42#include "llvm/Support/ErrorHandling.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000043#include "llvm/Support/MathExtras.h"
Justin Holewinski47997292011-06-24 19:19:18 +000044#include "llvm/Support/Path.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000045#include "llvm/Support/TargetRegistry.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000046#include "llvm/Support/raw_ostream.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000047
48using namespace llvm;
49
Che-Liang Chioudf659632010-11-08 03:06:08 +000050static const char PARAM_PREFIX[] = "__param_";
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +000051static const char RETURN_PREFIX[] = "__ret_";
Che-Liang Chioudf659632010-11-08 03:06:08 +000052
Justin Holewinski5422a0f2011-09-22 16:45:46 +000053static const char *getRegisterTypeName(unsigned RegNo,
54 const MachineRegisterInfo& MRI) {
55 const TargetRegisterClass *TRC = MRI.getRegClass(RegNo);
56
57#define TEST_REGCLS(cls, clsstr) \
58 if (PTX::cls ## RegisterClass == TRC) return # clsstr;
59
Justin Holewinski1b91bcd2011-06-16 17:49:58 +000060 TEST_REGCLS(RegPred, pred);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +000061 TEST_REGCLS(RegI16, b16);
62 TEST_REGCLS(RegI32, b32);
63 TEST_REGCLS(RegI64, b64);
64 TEST_REGCLS(RegF32, b32);
65 TEST_REGCLS(RegF64, b64);
Che-Liang Chioudf659632010-11-08 03:06:08 +000066#undef TEST_REGCLS
67
68 llvm_unreachable("Not in any register class!");
69 return NULL;
70}
71
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000072static const char *getStateSpaceName(unsigned addressSpace) {
Che-Liang Chioud34f19f2010-12-30 10:41:27 +000073 switch (addressSpace) {
74 default: llvm_unreachable("Unknown state space");
75 case PTX::GLOBAL: return "global";
76 case PTX::CONSTANT: return "const";
77 case PTX::LOCAL: return "local";
78 case PTX::PARAMETER: return "param";
79 case PTX::SHARED: return "shared";
80 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000081 return NULL;
82}
83
Chris Lattnerdb125cf2011-07-18 04:54:35 +000084static const char *getTypeName(Type* type) {
Che-Liang Chiouf7172022011-02-28 06:34:09 +000085 while (true) {
86 switch (type->getTypeID()) {
87 default: llvm_unreachable("Unknown type");
88 case Type::FloatTyID: return ".f32";
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000089 case Type::DoubleTyID: return ".f64";
90 case Type::IntegerTyID:
91 switch (type->getPrimitiveSizeInBits()) {
92 default: llvm_unreachable("Unknown integer bit-width");
93 case 16: return ".u16";
94 case 32: return ".u32";
95 case 64: return ".u64";
96 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +000097 case Type::ArrayTyID:
98 case Type::PointerTyID:
Chris Lattnerdb125cf2011-07-18 04:54:35 +000099 type = dyn_cast<SequentialType>(type)->getElementType();
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000100 break;
101 }
102 }
103 return NULL;
104}
105
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000106bool PTXAsmPrinter::doFinalization(Module &M) {
107 // XXX Temproarily remove global variables so that doFinalization() will not
108 // emit them again (global variables are emitted at beginning).
109
110 Module::GlobalListType &global_list = M.getGlobalList();
111 int i, n = global_list.size();
112 GlobalVariable **gv_array = new GlobalVariable* [n];
113
114 // first, back-up GlobalVariable in gv_array
115 i = 0;
116 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
117 I != E; ++I)
118 gv_array[i++] = &*I;
119
120 // second, empty global_list
121 while (!global_list.empty())
122 global_list.remove(global_list.begin());
123
124 // call doFinalization
125 bool ret = AsmPrinter::doFinalization(M);
126
127 // now we restore global variables
128 for (i = 0; i < n; i ++)
129 global_list.insert(global_list.end(), gv_array[i]);
130
131 delete[] gv_array;
132 return ret;
133}
134
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000135void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
136{
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000137 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
138
139 OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString()));
140 OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() +
Justin Holewinski12785e82011-03-03 13:34:29 +0000141 (ST.supportsDouble() ? ""
142 : ", map_f64_to_f32")));
Justin Holewinskia9c85f92011-06-22 00:43:56 +0000143 // .address_size directive is optional, but it must immediately follow
144 // the .target directive if present within a module
145 if (ST.supportsPTX23()) {
146 std::string addrSize = ST.is64Bit() ? "64" : "32";
147 OutStreamer.EmitRawText(Twine("\t.address_size " + addrSize));
148 }
149
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000150 OutStreamer.AddBlankLine();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000151
Justin Holewinski47997292011-06-24 19:19:18 +0000152 // Define any .file directives
153 DebugInfoFinder DbgFinder;
154 DbgFinder.processModule(M);
155
156 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
157 E = DbgFinder.compile_unit_end(); I != E; ++I) {
158 DICompileUnit DIUnit(*I);
159 StringRef FN = DIUnit.getFilename();
160 StringRef Dir = DIUnit.getDirectory();
161 GetOrCreateSourceID(FN, Dir);
162 }
163
164 OutStreamer.AddBlankLine();
165
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000166 // declare global variables
167 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
168 i != e; ++i)
169 EmitVariableDeclaration(i);
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000170}
171
Che-Liang Chioudf659632010-11-08 03:06:08 +0000172bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
173 SetupMachineFunction(MF);
174 EmitFunctionDeclaration();
175 EmitFunctionBody();
176 return false;
177}
178
179void PTXAsmPrinter::EmitFunctionBodyStart() {
180 OutStreamer.EmitRawText(Twine("{"));
181
182 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
Justin Holewinskie953a642011-09-23 14:31:12 +0000183 const PTXParamManager &PM = MFI->getParamManager();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000184
Justin Holewinski297984d2011-09-22 16:45:40 +0000185 // Print register definitions
186 std::string regDefs;
187 unsigned numRegs;
Che-Liang Chioudf659632010-11-08 03:06:08 +0000188
Justin Holewinski297984d2011-09-22 16:45:40 +0000189 // pred
190 numRegs = MFI->getNumRegistersForClass(PTX::RegPredRegisterClass);
191 if(numRegs > 0) {
192 regDefs += "\t.reg .pred %p<";
193 regDefs += utostr(numRegs);
194 regDefs += ">;\n";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000195 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000196
Justin Holewinski297984d2011-09-22 16:45:40 +0000197 // i16
198 numRegs = MFI->getNumRegistersForClass(PTX::RegI16RegisterClass);
199 if(numRegs > 0) {
200 regDefs += "\t.reg .b16 %rh<";
201 regDefs += utostr(numRegs);
202 regDefs += ">;\n";
203 }
204
205 // i32
206 numRegs = MFI->getNumRegistersForClass(PTX::RegI32RegisterClass);
207 if(numRegs > 0) {
208 regDefs += "\t.reg .b32 %r<";
209 regDefs += utostr(numRegs);
210 regDefs += ">;\n";
211 }
212
213 // i64
214 numRegs = MFI->getNumRegistersForClass(PTX::RegI64RegisterClass);
215 if(numRegs > 0) {
216 regDefs += "\t.reg .b64 %rd<";
217 regDefs += utostr(numRegs);
218 regDefs += ">;\n";
219 }
220
221 // f32
222 numRegs = MFI->getNumRegistersForClass(PTX::RegF32RegisterClass);
223 if(numRegs > 0) {
224 regDefs += "\t.reg .f32 %f<";
225 regDefs += utostr(numRegs);
226 regDefs += ">;\n";
227 }
228
229 // f64
230 numRegs = MFI->getNumRegistersForClass(PTX::RegF64RegisterClass);
231 if(numRegs > 0) {
232 regDefs += "\t.reg .f64 %fd<";
233 regDefs += utostr(numRegs);
234 regDefs += ">;\n";
235 }
236
Justin Holewinskie953a642011-09-23 14:31:12 +0000237 // Local params
238 for (PTXParamManager::param_iterator i = PM.local_begin(), e = PM.local_end();
239 i != e; ++i) {
240 regDefs += "\t.param .b";
241 regDefs += utostr(PM.getParamSize(*i));
242 regDefs += " ";
243 regDefs += PM.getParamName(*i);
244 regDefs += ";\n";
245 }
246
Justin Holewinski297984d2011-09-22 16:45:40 +0000247 OutStreamer.EmitRawText(Twine(regDefs));
248
249
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000250 const MachineFrameInfo* FrameInfo = MF->getFrameInfo();
Justin Holewinski08d03162011-06-22 16:07:03 +0000251 DEBUG(dbgs() << "Have " << FrameInfo->getNumObjects()
252 << " frame object(s)\n");
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000253 for (unsigned i = 0, e = FrameInfo->getNumObjects(); i != e; ++i) {
254 DEBUG(dbgs() << "Size of object: " << FrameInfo->getObjectSize(i) << "\n");
Justin Holewinski08d03162011-06-22 16:07:03 +0000255 if (FrameInfo->getObjectSize(i) > 0) {
Justin Holewinski58788502011-09-26 16:20:34 +0000256 std::string def = "\t.local .align ";
257 def += utostr(FrameInfo->getObjectAlignment(i));
Justin Holewinski63602ed2011-09-26 18:57:22 +0000258 def += " .b8";
Justin Holewinskic1d8fbd2011-09-26 16:20:28 +0000259 def += " __local";
Justin Holewinski08d03162011-06-22 16:07:03 +0000260 def += utostr(i);
Justin Holewinski63602ed2011-09-26 18:57:22 +0000261 def += "[";
262 def += utostr(FrameInfo->getObjectSize(i)); // Convert to bits
263 def += "]";
Justin Holewinski08d03162011-06-22 16:07:03 +0000264 def += ";";
265 OutStreamer.EmitRawText(Twine(def));
266 }
Justin Holewinskidf1c8d82011-06-20 15:56:20 +0000267 }
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000268
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000269 //unsigned Index = 1;
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000270 // Print parameter passing params
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000271 //for (PTXMachineFunctionInfo::param_iterator
272 // i = MFI->paramBegin(), e = MFI->paramEnd(); i != e; ++i) {
273 // std::string def = "\t.param .b";
274 // def += utostr(*i);
275 // def += " __ret_";
276 // def += utostr(Index);
277 // Index++;
278 // def += ";";
279 // OutStreamer.EmitRawText(Twine(def));
280 //}
Che-Liang Chioudf659632010-11-08 03:06:08 +0000281}
282
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000283void PTXAsmPrinter::EmitFunctionBodyEnd() {
284 OutStreamer.EmitRawText(Twine("}"));
285}
286
Eric Christopher50880d02010-09-18 18:52:28 +0000287void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000288#if 0
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000289 std::string str;
290 str.reserve(64);
291
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000292 raw_string_ostream OS(str);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000293
Justin Holewinski47997292011-06-24 19:19:18 +0000294 DebugLoc DL = MI->getDebugLoc();
295 if (!DL.isUnknown()) {
296
297 const MDNode *S = DL.getScope(MF->getFunction()->getContext());
298
299 // This is taken from DwarfDebug.cpp, which is conveniently not a public
300 // LLVM class.
301 StringRef Fn;
302 StringRef Dir;
303 unsigned Src = 1;
304 if (S) {
305 DIDescriptor Scope(S);
306 if (Scope.isCompileUnit()) {
307 DICompileUnit CU(S);
308 Fn = CU.getFilename();
309 Dir = CU.getDirectory();
310 } else if (Scope.isFile()) {
311 DIFile F(S);
312 Fn = F.getFilename();
313 Dir = F.getDirectory();
314 } else if (Scope.isSubprogram()) {
315 DISubprogram SP(S);
316 Fn = SP.getFilename();
317 Dir = SP.getDirectory();
318 } else if (Scope.isLexicalBlock()) {
319 DILexicalBlock DB(S);
320 Fn = DB.getFilename();
321 Dir = DB.getDirectory();
322 } else
323 assert(0 && "Unexpected scope info");
324
325 Src = GetOrCreateSourceID(Fn, Dir);
326 }
327 OutStreamer.EmitDwarfLocDirective(Src, DL.getLine(), DL.getCol(),
328 0, 0, 0, Fn);
329
330 const MCDwarfLoc& MDL = OutContext.getCurrentDwarfLoc();
331
332 OS << "\t.loc ";
333 OS << utostr(MDL.getFileNum());
334 OS << " ";
335 OS << utostr(MDL.getLine());
336 OS << " ";
337 OS << utostr(MDL.getColumn());
338 OS << "\n";
339 }
340
341
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000342 // Emit predicate
343 printPredicateOperand(MI, OS);
344
345 // Write instruction to str
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000346 if (MI->getOpcode() == PTX::CALL) {
347 printCall(MI, OS);
348 } else {
349 printInstruction(MI, OS);
350 }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000351 OS << ';';
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000352 OS.flush();
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000353
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000354 StringRef strref = StringRef(str);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000355 OutStreamer.EmitRawText(strref);
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000356#endif
357
358 MCInst TmpInst;
359 LowerPTXMachineInstrToMCInst(MI, TmpInst, *this);
360 OutStreamer.EmitInstruction(TmpInst);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000361}
362
363void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
364 raw_ostream &OS) {
365 const MachineOperand &MO = MI->getOperand(opNum);
Justin Holewinski297984d2011-09-22 16:45:40 +0000366 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000367
368 switch (MO.getType()) {
369 default:
370 llvm_unreachable("<unknown operand type>");
371 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000372 case MachineOperand::MO_GlobalAddress:
373 OS << *Mang->getSymbol(MO.getGlobal());
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000374 break;
375 case MachineOperand::MO_Immediate:
Justin Holewinski9583a862011-04-28 00:19:50 +0000376 OS << (long) MO.getImm();
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000377 break;
Che-Liang Chiou88d33672011-03-18 11:08:52 +0000378 case MachineOperand::MO_MachineBasicBlock:
379 OS << *MO.getMBB()->getSymbol();
380 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000381 case MachineOperand::MO_Register:
Justin Holewinski297984d2011-09-22 16:45:40 +0000382 OS << MFI->getRegisterName(MO.getReg());
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000383 break;
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000384 case MachineOperand::MO_ExternalSymbol:
385 OS << MO.getSymbolName();
386 break;
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000387 case MachineOperand::MO_FPImmediate:
388 APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt();
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000389 bool isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID;
390 // Emit 0F for 32-bit floats and 0D for 64-bit doubles.
391 if (isFloat) {
392 OS << "0F";
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000393 }
394 else {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000395 OS << "0D";
396 }
397 // Emit the encoded floating-point value.
398 if (constFP.getZExtValue() > 0) {
399 OS << constFP.toString(16, false);
400 }
401 else {
402 OS << "00000000";
403 // If We have a double-precision zero, pad to 8-bytes.
404 if (!isFloat) {
405 OS << "00000000";
406 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000407 }
408 break;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000409 }
Eric Christopher50880d02010-09-18 18:52:28 +0000410}
411
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000412void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
413 raw_ostream &OS, const char *Modifier) {
414 printOperand(MI, opNum, OS);
415
416 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
417 return; // don't print "+0"
418
419 OS << "+";
420 printOperand(MI, opNum+1, OS);
421}
422
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000423void PTXAsmPrinter::printReturnOperand(const MachineInstr *MI, int opNum,
424 raw_ostream &OS, const char *Modifier) {
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000425 //OS << RETURN_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
426 OS << "__ret";
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000427}
428
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000429void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
430 // Check to see if this is a special global used by LLVM, if so, emit it.
431 if (EmitSpecialLLVMGlobal(gv))
432 return;
433
434 MCSymbol *gvsym = Mang->getSymbol(gv);
435
436 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
437
438 std::string decl;
439
440 // check if it is defined in some other translation unit
441 if (gv->isDeclaration())
442 decl += ".extern ";
443
444 // state space: e.g., .global
445 decl += ".";
446 decl += getStateSpaceName(gv->getType()->getAddressSpace());
447 decl += " ";
448
449 // alignment (optional)
450 unsigned alignment = gv->getAlignment();
451 if (alignment != 0) {
452 decl += ".align ";
Justin Holewinski332850d2011-09-27 19:25:49 +0000453 decl += utostr(std::max(1U, Log2_32(gv->getAlignment())));
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000454 decl += " ";
455 }
456
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000457
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000458 if (PointerType::classof(gv->getType())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000459 PointerType* pointerTy = dyn_cast<PointerType>(gv->getType());
460 Type* elementTy = pointerTy->getElementType();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000461
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000462 decl += ".b8 ";
463 decl += gvsym->getName();
464 decl += "[";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000465
Justin Holewinski9583a862011-04-28 00:19:50 +0000466 if (elementTy->isArrayTy())
467 {
468 assert(elementTy->isArrayTy() && "Only pointers to arrays are supported");
469
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000470 ArrayType* arrayTy = dyn_cast<ArrayType>(elementTy);
Justin Holewinski9583a862011-04-28 00:19:50 +0000471 elementTy = arrayTy->getElementType();
472
473 unsigned numElements = arrayTy->getNumElements();
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000474
Justin Holewinski9583a862011-04-28 00:19:50 +0000475 while (elementTy->isArrayTy()) {
476
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000477 arrayTy = dyn_cast<ArrayType>(elementTy);
Justin Holewinski9583a862011-04-28 00:19:50 +0000478 elementTy = arrayTy->getElementType();
479
480 numElements *= arrayTy->getNumElements();
481 }
482
483 // FIXME: isPrimitiveType() == false for i16?
484 assert(elementTy->isSingleValueType() &&
485 "Non-primitive types are not handled");
486
487 // Compute the size of the array, in bytes.
488 uint64_t arraySize = (elementTy->getPrimitiveSizeInBits() >> 3)
489 * numElements;
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000490
Justin Holewinski9583a862011-04-28 00:19:50 +0000491 decl += utostr(arraySize);
492 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000493
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000494 decl += "]";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000495
Justin Holewinski9583a862011-04-28 00:19:50 +0000496 // handle string constants (assume ConstantArray means string)
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000497
Justin Holewinski9583a862011-04-28 00:19:50 +0000498 if (gv->hasInitializer())
499 {
Justin Holewinski297984d2011-09-22 16:45:40 +0000500 const Constant *C = gv->getInitializer();
Justin Holewinski9583a862011-04-28 00:19:50 +0000501 if (const ConstantArray *CA = dyn_cast<ConstantArray>(C))
502 {
503 decl += " = {";
504
505 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
506 {
507 if (i > 0) decl += ",";
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000508
509 decl += "0x" +
510 utohexstr(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());
Justin Holewinski9583a862011-04-28 00:19:50 +0000511 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000512
Justin Holewinski9583a862011-04-28 00:19:50 +0000513 decl += "}";
514 }
515 }
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000516 }
517 else {
518 // Note: this is currently the fall-through case and most likely generates
519 // incorrect code.
520 decl += getTypeName(gv->getType());
521 decl += " ";
522
523 decl += gvsym->getName();
524
525 if (ArrayType::classof(gv->getType()) ||
526 PointerType::classof(gv->getType()))
527 decl += "[]";
528 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000529
530 decl += ";";
531
532 OutStreamer.EmitRawText(Twine(decl));
533
534 OutStreamer.AddBlankLine();
535}
536
Che-Liang Chioudf659632010-11-08 03:06:08 +0000537void PTXAsmPrinter::EmitFunctionDeclaration() {
538 // The function label could have already been emitted if two symbols end up
539 // conflicting due to asm renaming. Detect this and emit an error.
540 if (!CurrentFnSym->isUndefined()) {
541 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
542 "' label emitted multiple times to assembly file");
543 return;
544 }
545
546 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000547 const PTXParamManager &PM = MFI->getParamManager();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000548 const bool isKernel = MFI->isKernel();
Justin Holewinski67a91842011-06-23 18:10:03 +0000549 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000550 const MachineRegisterInfo& MRI = MF->getRegInfo();
Che-Liang Chioudf659632010-11-08 03:06:08 +0000551
552 std::string decl = isKernel ? ".entry" : ".func";
553
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000554 unsigned cnt = 0;
555
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000556 if (!isKernel) {
557 decl += " (";
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000558 if (ST.useParamSpaceForDeviceArgs()) {
559 for (PTXParamManager::param_iterator i = PM.ret_begin(), e = PM.ret_end(),
560 b = i; i != e; ++i) {
561 if (i != b) {
562 decl += ", ";
563 }
564
565 decl += ".param .b";
566 decl += utostr(PM.getParamSize(*i));
567 decl += " ";
568 decl += PM.getParamName(*i);
569 }
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000570 } else {
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000571 for (PTXMachineFunctionInfo::reg_iterator
572 i = MFI->retreg_begin(), e = MFI->retreg_end(), b = i;
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000573 i != e; ++i) {
574 if (i != b) {
575 decl += ", ";
576 }
577 decl += ".reg .";
578 decl += getRegisterTypeName(*i, MRI);
579 decl += " ";
580 decl += MFI->getRegisterName(*i);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000581 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000582 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000583 decl += ")";
584 }
585
586 // Print function name
587 decl += " ";
588 decl += CurrentFnSym->getName().str();
589
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000590 decl += " (";
591
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000592 cnt = 0;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000593
594 // Print parameters
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000595 if (isKernel || ST.useParamSpaceForDeviceArgs()) {
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000596 for (PTXParamManager::param_iterator i = PM.arg_begin(), e = PM.arg_end(),
597 b = i; i != e; ++i) {
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000598 if (i != b) {
599 decl += ", ";
600 }
601
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000602 decl += ".param .b";
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000603 decl += utostr(PM.getParamSize(*i));
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000604 decl += " ";
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000605 decl += PM.getParamName(*i);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000606 }
607 } else {
608 for (PTXMachineFunctionInfo::reg_iterator
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000609 i = MFI->argreg_begin(), e = MFI->argreg_end(), b = i;
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000610 i != e; ++i) {
611 if (i != b) {
612 decl += ", ";
613 }
614
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000615 decl += ".reg .";
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000616 decl += getRegisterTypeName(*i, MRI);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000617 decl += " ";
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000618 decl += MFI->getRegisterName(*i);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000619 }
Che-Liang Chioudf659632010-11-08 03:06:08 +0000620 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000621 decl += ")";
622
Che-Liang Chioudf659632010-11-08 03:06:08 +0000623 OutStreamer.EmitRawText(Twine(decl));
624}
625
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000626void PTXAsmPrinter::
627printPredicateOperand(const MachineInstr *MI, raw_ostream &O) {
628 int i = MI->findFirstPredOperandIdx();
629 if (i == -1)
630 llvm_unreachable("missing predicate operand");
631
632 unsigned reg = MI->getOperand(i).getReg();
633 int predOp = MI->getOperand(i+1).getImm();
Justin Holewinski297984d2011-09-22 16:45:40 +0000634 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000635
636 DEBUG(dbgs() << "predicate: (" << reg << ", " << predOp << ")\n");
637
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000638 if (reg != PTX::NoRegister) {
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000639 O << '@';
640 if (predOp == PTX::PRED_NEGATE)
641 O << '!';
Justin Holewinski297984d2011-09-22 16:45:40 +0000642 O << MFI->getRegisterName(reg);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000643 }
644}
645
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000646void PTXAsmPrinter::
647printCall(const MachineInstr *MI, raw_ostream &O) {
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000648 O << "\tcall.uni\t";
Justin Holewinski75d80952011-09-23 16:48:41 +0000649 // The first two operands are the predicate slot
650 unsigned Index = 2;
651 while (!MI->getOperand(Index).isGlobal()) {
652 if (Index == 2) {
653 O << "(";
654 } else {
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000655 O << ", ";
656 }
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000657 printOperand(MI, Index, O);
Justin Holewinski75d80952011-09-23 16:48:41 +0000658 Index++;
659 }
660
661 if (Index != 2) {
662 O << "), ";
663 }
664
665 assert(MI->getOperand(Index).isGlobal() &&
666 "A GlobalAddress must follow the return arguments");
667
668 const GlobalValue *Address = MI->getOperand(Index).getGlobal();
669 O << Address->getName() << ", (";
670 Index++;
671
672 while (Index < MI->getNumOperands()) {
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000673 printOperand(MI, Index, O);
Justin Holewinski75d80952011-09-23 16:48:41 +0000674 if (Index < MI->getNumOperands()-1) {
675 O << ", ";
676 }
677 Index++;
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000678 }
679
680 O << ")";
681}
682
Justin Holewinski47997292011-06-24 19:19:18 +0000683unsigned PTXAsmPrinter::GetOrCreateSourceID(StringRef FileName,
684 StringRef DirName) {
685 // If FE did not provide a file name, then assume stdin.
686 if (FileName.empty())
687 return GetOrCreateSourceID("<stdin>", StringRef());
688
689 // MCStream expects full path name as filename.
690 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
691 SmallString<128> FullPathName = DirName;
692 sys::path::append(FullPathName, FileName);
693 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
694 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
695 }
696
697 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
698 if (Entry.getValue())
699 return Entry.getValue();
700
701 unsigned SrcId = SourceIdMap.size();
702 Entry.setValue(SrcId);
703
704 // Print out a .file directive to specify files for .loc directives.
705 OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
706
707 return SrcId;
708}
709
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000710MCOperand PTXAsmPrinter::GetSymbolRef(const MachineOperand &MO,
711 const MCSymbol *Symbol) {
712 const MCExpr *Expr;
713 Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None, OutContext);
714 return MCOperand::CreateExpr(Expr);
715}
716
717bool PTXAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
718 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
719 const MCExpr *Expr;
720 const char *RegSymbolName;
721 switch (MO.getType()) {
722 default:
723 llvm_unreachable("Unknown operand type");
724 case MachineOperand::MO_Register:
725 // We create register operands as symbols, since the PTXInstPrinter class
726 // has no way to map virtual registers back to a name without some ugly
727 // hacks.
728 // FIXME: Figure out a better way to handle virtual register naming.
729 RegSymbolName = MFI->getRegisterName(MO.getReg());
730 Expr = MCSymbolRefExpr::Create(RegSymbolName, MCSymbolRefExpr::VK_None,
731 OutContext);
732 MCOp = MCOperand::CreateExpr(Expr);
733 break;
734 case MachineOperand::MO_Immediate:
735 MCOp = MCOperand::CreateImm(MO.getImm());
736 break;
737 case MachineOperand::MO_MachineBasicBlock:
738 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
739 MO.getMBB()->getSymbol(), OutContext));
740 break;
741 case MachineOperand::MO_GlobalAddress:
742 MCOp = GetSymbolRef(MO, Mang->getSymbol(MO.getGlobal()));
743 break;
744 case MachineOperand::MO_ExternalSymbol:
745 MCOp = GetSymbolRef(MO, GetExternalSymbolSymbol(MO.getSymbolName()));
746 break;
747 case MachineOperand::MO_FPImmediate:
748 APFloat Val = MO.getFPImm()->getValueAPF();
749 bool ignored;
750 Val.convert(APFloat::IEEEdouble, APFloat::rmTowardZero, &ignored);
751 MCOp = MCOperand::CreateFPImm(Val.convertToDouble());
752 break;
753 }
754
755 return true;
756}
Eric Christopher50880d02010-09-18 18:52:28 +0000757
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000758// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000759extern "C" void LLVMInitializePTXAsmPrinter() {
Justin Holewinskie1fee482011-04-20 15:37:17 +0000760 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target);
761 RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target);
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000762}
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000763