blob: 35eeadce2d2347a83c26b71964f0005f20d7c89f [file] [log] [blame]
Nick Lewyckyf7a3c502010-09-07 18:14:24 +00001//===-- PTXAsmPrinter.cpp - PTX LLVM assembly writer ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to PTX assembly language.
12//
13//===----------------------------------------------------------------------===//
14
Che-Liang Chioudf659632010-11-08 03:06:08 +000015#define DEBUG_TYPE "ptx-asm-printer"
16
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000017#include "PTX.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000018#include "PTXMachineFunctionInfo.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000019#include "PTXTargetMachine.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000020#include "llvm/DerivedTypes.h"
21#include "llvm/Module.h"
Eric Christopher50880d02010-09-18 18:52:28 +000022#include "llvm/ADT/SmallString.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000023#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/Twine.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000025#include "llvm/CodeGen/AsmPrinter.h"
Eric Christopher50880d02010-09-18 18:52:28 +000026#include "llvm/CodeGen/MachineInstr.h"
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000028#include "llvm/MC/MCStreamer.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000029#include "llvm/MC/MCSymbol.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000030#include "llvm/Target/Mangler.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000031#include "llvm/Target/TargetLoweringObjectFile.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000032#include "llvm/Target/TargetRegistry.h"
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000033#include "llvm/Support/CommandLine.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/ErrorHandling.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000036#include "llvm/Support/MathExtras.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000037#include "llvm/Support/raw_ostream.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000038
39using namespace llvm;
40
41namespace {
Che-Liang Chioudf659632010-11-08 03:06:08 +000042class PTXAsmPrinter : public AsmPrinter {
43public:
44 explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
45 : AsmPrinter(TM, Streamer) {}
Eric Christopher50880d02010-09-18 18:52:28 +000046
Che-Liang Chioudf659632010-11-08 03:06:08 +000047 const char *getPassName() const { return "PTX Assembly Printer"; }
Eric Christopher50880d02010-09-18 18:52:28 +000048
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000049 bool doFinalization(Module &M);
50
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000051 virtual void EmitStartOfAsmFile(Module &M);
52
Che-Liang Chioudf659632010-11-08 03:06:08 +000053 virtual bool runOnMachineFunction(MachineFunction &MF);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000054
Che-Liang Chioudf659632010-11-08 03:06:08 +000055 virtual void EmitFunctionBodyStart();
56 virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); }
57
58 virtual void EmitInstruction(const MachineInstr *MI);
59
60 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000061 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
62 const char *Modifier = 0);
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +000063 void printParamOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
64 const char *Modifier = 0);
Che-Liang Chioudf659632010-11-08 03:06:08 +000065
66 // autogen'd.
67 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
68 static const char *getRegisterName(unsigned RegNo);
69
70private:
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000071 void EmitVariableDeclaration(const GlobalVariable *gv);
Che-Liang Chioudf659632010-11-08 03:06:08 +000072 void EmitFunctionDeclaration();
73}; // class PTXAsmPrinter
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000074} // namespace
75
Che-Liang Chioudf659632010-11-08 03:06:08 +000076static const char PARAM_PREFIX[] = "__param_";
77
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000078static const char *getRegisterTypeName(unsigned RegNo) {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000079#define TEST_REGCLS(cls, clsstr) \
Che-Liang Chioudf659632010-11-08 03:06:08 +000080 if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr;
Che-Liang Chiou3f409f72010-11-17 08:08:49 +000081 TEST_REGCLS(Preds, pred);
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000082 TEST_REGCLS(RRegu16, u16);
83 TEST_REGCLS(RRegu32, u32);
84 TEST_REGCLS(RRegu64, u64);
85 TEST_REGCLS(RRegf32, f32);
86 TEST_REGCLS(RRegf64, f64);
Che-Liang Chioudf659632010-11-08 03:06:08 +000087#undef TEST_REGCLS
88
89 llvm_unreachable("Not in any register class!");
90 return NULL;
91}
92
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000093static const char *getInstructionTypeName(const MachineInstr *MI) {
Che-Liang Chiou3f409f72010-11-17 08:08:49 +000094 for (int i = 0, e = MI->getNumOperands(); i != e; ++i) {
95 const MachineOperand &MO = MI->getOperand(i);
96 if (MO.getType() == MachineOperand::MO_Register)
97 return getRegisterTypeName(MO.getReg());
98 }
99
100 llvm_unreachable("No reg operand found in instruction!");
101 return NULL;
102}
103
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000104static const char *getStateSpaceName(unsigned addressSpace) {
Che-Liang Chioud34f19f2010-12-30 10:41:27 +0000105 switch (addressSpace) {
106 default: llvm_unreachable("Unknown state space");
107 case PTX::GLOBAL: return "global";
108 case PTX::CONSTANT: return "const";
109 case PTX::LOCAL: return "local";
110 case PTX::PARAMETER: return "param";
111 case PTX::SHARED: return "shared";
112 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000113 return NULL;
114}
115
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000116static const char *getTypeName(const Type* type) {
117 while (true) {
118 switch (type->getTypeID()) {
119 default: llvm_unreachable("Unknown type");
120 case Type::FloatTyID: return ".f32";
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000121 case Type::DoubleTyID: return ".f64";
122 case Type::IntegerTyID:
123 switch (type->getPrimitiveSizeInBits()) {
124 default: llvm_unreachable("Unknown integer bit-width");
125 case 16: return ".u16";
126 case 32: return ".u32";
127 case 64: return ".u64";
128 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000129 case Type::ArrayTyID:
130 case Type::PointerTyID:
131 type = dyn_cast<const SequentialType>(type)->getElementType();
132 break;
133 }
134 }
135 return NULL;
136}
137
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000138bool PTXAsmPrinter::doFinalization(Module &M) {
139 // XXX Temproarily remove global variables so that doFinalization() will not
140 // emit them again (global variables are emitted at beginning).
141
142 Module::GlobalListType &global_list = M.getGlobalList();
143 int i, n = global_list.size();
144 GlobalVariable **gv_array = new GlobalVariable* [n];
145
146 // first, back-up GlobalVariable in gv_array
147 i = 0;
148 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
149 I != E; ++I)
150 gv_array[i++] = &*I;
151
152 // second, empty global_list
153 while (!global_list.empty())
154 global_list.remove(global_list.begin());
155
156 // call doFinalization
157 bool ret = AsmPrinter::doFinalization(M);
158
159 // now we restore global variables
160 for (i = 0; i < n; i ++)
161 global_list.insert(global_list.end(), gv_array[i]);
162
163 delete[] gv_array;
164 return ret;
165}
166
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000167void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
168{
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000169 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
170
171 OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString()));
172 OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() +
173 (ST.supportsDouble() ? "" : ", map_f64_to_f32")));
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000174 OutStreamer.AddBlankLine();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000175
176 // declare global variables
177 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
178 i != e; ++i)
179 EmitVariableDeclaration(i);
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000180}
181
Che-Liang Chioudf659632010-11-08 03:06:08 +0000182bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
183 SetupMachineFunction(MF);
184 EmitFunctionDeclaration();
185 EmitFunctionBody();
186 return false;
187}
188
189void PTXAsmPrinter::EmitFunctionBodyStart() {
190 OutStreamer.EmitRawText(Twine("{"));
191
192 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
193
194 // Print local variable definition
195 for (PTXMachineFunctionInfo::reg_iterator
196 i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) {
197 unsigned reg = *i;
198
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000199 std::string def = "\t.reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000200 def += getRegisterTypeName(reg);
201 def += ' ';
202 def += getRegisterName(reg);
203 def += ';';
204 OutStreamer.EmitRawText(Twine(def));
205 }
206}
207
Eric Christopher50880d02010-09-18 18:52:28 +0000208void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000209 std::string str;
210 str.reserve(64);
211
212 // Write instruction to str
213 raw_string_ostream OS(str);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000214 printInstruction(MI, OS);
215 OS << ';';
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000216 OS.flush();
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000217
218 // Replace "%type" if found
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000219 size_t pos;
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000220 if ((pos = str.find("%type")) != std::string::npos)
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000221 str.replace(pos, /*strlen("%type")==*/5, getInstructionTypeName(MI));
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000222
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000223 StringRef strref = StringRef(str);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000224 OutStreamer.EmitRawText(strref);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000225}
226
227void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
228 raw_ostream &OS) {
229 const MachineOperand &MO = MI->getOperand(opNum);
230
231 switch (MO.getType()) {
232 default:
233 llvm_unreachable("<unknown operand type>");
234 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000235 case MachineOperand::MO_GlobalAddress:
236 OS << *Mang->getSymbol(MO.getGlobal());
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000237 break;
238 case MachineOperand::MO_Immediate:
239 OS << (int) MO.getImm();
240 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000241 case MachineOperand::MO_Register:
242 OS << getRegisterName(MO.getReg());
243 break;
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000244 case MachineOperand::MO_FPImmediate:
245 APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt();
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000246 bool isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID;
247 // Emit 0F for 32-bit floats and 0D for 64-bit doubles.
248 if (isFloat) {
249 OS << "0F";
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000250 }
251 else {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000252 OS << "0D";
253 }
254 // Emit the encoded floating-point value.
255 if (constFP.getZExtValue() > 0) {
256 OS << constFP.toString(16, false);
257 }
258 else {
259 OS << "00000000";
260 // If We have a double-precision zero, pad to 8-bytes.
261 if (!isFloat) {
262 OS << "00000000";
263 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000264 }
265 break;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000266 }
Eric Christopher50880d02010-09-18 18:52:28 +0000267}
268
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000269void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
270 raw_ostream &OS, const char *Modifier) {
271 printOperand(MI, opNum, OS);
272
273 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
274 return; // don't print "+0"
275
276 OS << "+";
277 printOperand(MI, opNum+1, OS);
278}
279
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000280void PTXAsmPrinter::printParamOperand(const MachineInstr *MI, int opNum,
281 raw_ostream &OS, const char *Modifier) {
282 OS << PARAM_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
283}
284
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000285void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
286 // Check to see if this is a special global used by LLVM, if so, emit it.
287 if (EmitSpecialLLVMGlobal(gv))
288 return;
289
290 MCSymbol *gvsym = Mang->getSymbol(gv);
291
292 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
293
294 std::string decl;
295
296 // check if it is defined in some other translation unit
297 if (gv->isDeclaration())
298 decl += ".extern ";
299
300 // state space: e.g., .global
301 decl += ".";
302 decl += getStateSpaceName(gv->getType()->getAddressSpace());
303 decl += " ";
304
305 // alignment (optional)
306 unsigned alignment = gv->getAlignment();
307 if (alignment != 0) {
308 decl += ".align ";
309 decl += utostr(Log2_32(gv->getAlignment()));
310 decl += " ";
311 }
312
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000313 decl += getTypeName(gv->getType());
314 decl += " ";
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000315
316 decl += gvsym->getName();
317
318 if (ArrayType::classof(gv->getType()) || PointerType::classof(gv->getType()))
319 decl += "[]";
320
321 decl += ";";
322
323 OutStreamer.EmitRawText(Twine(decl));
324
325 OutStreamer.AddBlankLine();
326}
327
Che-Liang Chioudf659632010-11-08 03:06:08 +0000328void PTXAsmPrinter::EmitFunctionDeclaration() {
329 // The function label could have already been emitted if two symbols end up
330 // conflicting due to asm renaming. Detect this and emit an error.
331 if (!CurrentFnSym->isUndefined()) {
332 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
333 "' label emitted multiple times to assembly file");
334 return;
335 }
336
337 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
338 const bool isKernel = MFI->isKernel();
339 unsigned reg;
340
341 std::string decl = isKernel ? ".entry" : ".func";
342
343 // Print return register
344 reg = MFI->retReg();
345 if (!isKernel && reg != PTX::NoRegister) {
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000346 decl += " (.reg ."; // FIXME: could it return in .param space?
Che-Liang Chioudf659632010-11-08 03:06:08 +0000347 decl += getRegisterTypeName(reg);
348 decl += " ";
349 decl += getRegisterName(reg);
350 decl += ")";
351 }
352
353 // Print function name
354 decl += " ";
355 decl += CurrentFnSym->getName().str();
356
357 // Print parameter list
358 if (!MFI->argRegEmpty()) {
359 decl += " (";
360 if (isKernel) {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000361 unsigned cnt = 0;
362 //for (int i = 0, e = MFI->getNumArg(); i != e; ++i) {
363 for(PTXMachineFunctionInfo::reg_iterator
364 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; i != e; ++i) {
365 reg = *i;
366 assert(reg != PTX::NoRegister && "Not a valid register!");
367 if (i != b)
Che-Liang Chioudf659632010-11-08 03:06:08 +0000368 decl += ", ";
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000369 decl += ".param .u32"; // TODO: Parse type from register map
370 decl += " ";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000371 decl += PARAM_PREFIX;
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000372 decl += utostr(++cnt);
Che-Liang Chioudf659632010-11-08 03:06:08 +0000373 }
374 } else {
375 for (PTXMachineFunctionInfo::reg_iterator
376 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; i != e; ++i) {
377 reg = *i;
378 assert(reg != PTX::NoRegister && "Not a valid register!");
379 if (i != b)
380 decl += ", ";
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000381 decl += ".reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000382 decl += getRegisterTypeName(reg);
383 decl += " ";
384 decl += getRegisterName(reg);
385 }
386 }
387 decl += ")";
388 }
389
390 OutStreamer.EmitRawText(Twine(decl));
391}
392
Eric Christopher50880d02010-09-18 18:52:28 +0000393#include "PTXGenAsmWriter.inc"
394
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000395// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000396extern "C" void LLVMInitializePTXAsmPrinter() {
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000397 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTXTarget);
398}