1 //===-- PTXAsmPrinter.cpp - PTX LLVM assembly writer ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to PTX assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "ptx-asm-printer"
18 #include "PTXMachineFunctionInfo.h"
19 #include "PTXTargetMachine.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Module.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/Twine.h"
25 #include "llvm/CodeGen/AsmPrinter.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/Target/Mangler.h"
32 #include "llvm/Target/TargetLoweringObjectFile.h"
33 #include "llvm/Target/TargetRegistry.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/Support/raw_ostream.h"
43 class PTXAsmPrinter : public AsmPrinter {
45 explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
46 : AsmPrinter(TM, Streamer) {}
48 const char *getPassName() const { return "PTX Assembly Printer"; }
50 bool doFinalization(Module &M);
52 virtual void EmitStartOfAsmFile(Module &M);
54 virtual bool runOnMachineFunction(MachineFunction &MF);
56 virtual void EmitFunctionBodyStart();
57 virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); }
59 virtual void EmitInstruction(const MachineInstr *MI);
61 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
62 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
63 const char *Modifier = 0);
64 void printParamOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
65 const char *Modifier = 0);
66 void printPredicateOperand(const MachineInstr *MI, raw_ostream &O);
69 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
70 static const char *getRegisterName(unsigned RegNo);
73 void EmitVariableDeclaration(const GlobalVariable *gv);
74 void EmitFunctionDeclaration();
75 }; // class PTXAsmPrinter
78 static const char PARAM_PREFIX[] = "__param_";
80 static const char *getRegisterTypeName(unsigned RegNo) {
81 #define TEST_REGCLS(cls, clsstr) \
82 if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr;
83 TEST_REGCLS(RegPred, pred);
84 TEST_REGCLS(RegI16, b16);
85 TEST_REGCLS(RegI32, b32);
86 TEST_REGCLS(RegI64, b64);
87 TEST_REGCLS(RegF32, b32);
88 TEST_REGCLS(RegF64, b64);
91 llvm_unreachable("Not in any register class!");
95 static const char *getStateSpaceName(unsigned addressSpace) {
96 switch (addressSpace) {
97 default: llvm_unreachable("Unknown state space");
98 case PTX::GLOBAL: return "global";
99 case PTX::CONSTANT: return "const";
100 case PTX::LOCAL: return "local";
101 case PTX::PARAMETER: return "param";
102 case PTX::SHARED: return "shared";
107 static const char *getTypeName(const Type* type) {
109 switch (type->getTypeID()) {
110 default: llvm_unreachable("Unknown type");
111 case Type::FloatTyID: return ".f32";
112 case Type::DoubleTyID: return ".f64";
113 case Type::IntegerTyID:
114 switch (type->getPrimitiveSizeInBits()) {
115 default: llvm_unreachable("Unknown integer bit-width");
116 case 16: return ".u16";
117 case 32: return ".u32";
118 case 64: return ".u64";
120 case Type::ArrayTyID:
121 case Type::PointerTyID:
122 type = dyn_cast<const SequentialType>(type)->getElementType();
129 bool PTXAsmPrinter::doFinalization(Module &M) {
130 // XXX Temproarily remove global variables so that doFinalization() will not
131 // emit them again (global variables are emitted at beginning).
133 Module::GlobalListType &global_list = M.getGlobalList();
134 int i, n = global_list.size();
135 GlobalVariable **gv_array = new GlobalVariable* [n];
137 // first, back-up GlobalVariable in gv_array
139 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
143 // second, empty global_list
144 while (!global_list.empty())
145 global_list.remove(global_list.begin());
147 // call doFinalization
148 bool ret = AsmPrinter::doFinalization(M);
150 // now we restore global variables
151 for (i = 0; i < n; i ++)
152 global_list.insert(global_list.end(), gv_array[i]);
158 void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
160 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
162 OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString()));
163 OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() +
164 (ST.supportsDouble() ? ""
165 : ", map_f64_to_f32")));
166 // .address_size directive is optional, but it must immediately follow
167 // the .target directive if present within a module
168 if (ST.supportsPTX23()) {
169 std::string addrSize = ST.is64Bit() ? "64" : "32";
170 OutStreamer.EmitRawText(Twine("\t.address_size " + addrSize));
173 OutStreamer.AddBlankLine();
175 // declare global variables
176 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
178 EmitVariableDeclaration(i);
181 bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
182 SetupMachineFunction(MF);
183 EmitFunctionDeclaration();
188 void PTXAsmPrinter::EmitFunctionBodyStart() {
189 OutStreamer.EmitRawText(Twine("{"));
191 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
193 // Print local variable definition
194 for (PTXMachineFunctionInfo::reg_iterator
195 i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) {
198 std::string def = "\t.reg .";
199 def += getRegisterTypeName(reg);
201 def += getRegisterName(reg);
203 OutStreamer.EmitRawText(Twine(def));
206 const MachineFrameInfo* FrameInfo = MF->getFrameInfo();
207 DEBUG(dbgs() << "Have " << FrameInfo->getNumObjects() << " frame object(s)\n");
208 for (unsigned i = 0, e = FrameInfo->getNumObjects(); i != e; ++i) {
209 DEBUG(dbgs() << "Size of object: " << FrameInfo->getObjectSize(i) << "\n");
210 std::string def = "\t.reg .b";
211 def += utostr(FrameInfo->getObjectSize(i)*8); // Convert to bits
215 OutStreamer.EmitRawText(Twine(def));
219 void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
223 raw_string_ostream OS(str);
226 printPredicateOperand(MI, OS);
228 // Write instruction to str
229 printInstruction(MI, OS);
233 StringRef strref = StringRef(str);
234 OutStreamer.EmitRawText(strref);
237 void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
239 const MachineOperand &MO = MI->getOperand(opNum);
241 switch (MO.getType()) {
243 llvm_unreachable("<unknown operand type>");
245 case MachineOperand::MO_GlobalAddress:
246 OS << *Mang->getSymbol(MO.getGlobal());
248 case MachineOperand::MO_Immediate:
249 OS << (long) MO.getImm();
251 case MachineOperand::MO_MachineBasicBlock:
252 OS << *MO.getMBB()->getSymbol();
254 case MachineOperand::MO_Register:
255 OS << getRegisterName(MO.getReg());
257 case MachineOperand::MO_FPImmediate:
258 APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt();
259 bool isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID;
260 // Emit 0F for 32-bit floats and 0D for 64-bit doubles.
267 // Emit the encoded floating-point value.
268 if (constFP.getZExtValue() > 0) {
269 OS << constFP.toString(16, false);
273 // If We have a double-precision zero, pad to 8-bytes.
282 void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
283 raw_ostream &OS, const char *Modifier) {
284 printOperand(MI, opNum, OS);
286 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
287 return; // don't print "+0"
290 printOperand(MI, opNum+1, OS);
293 void PTXAsmPrinter::printParamOperand(const MachineInstr *MI, int opNum,
294 raw_ostream &OS, const char *Modifier) {
295 OS << PARAM_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
298 void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
299 // Check to see if this is a special global used by LLVM, if so, emit it.
300 if (EmitSpecialLLVMGlobal(gv))
303 MCSymbol *gvsym = Mang->getSymbol(gv);
305 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
309 // check if it is defined in some other translation unit
310 if (gv->isDeclaration())
313 // state space: e.g., .global
315 decl += getStateSpaceName(gv->getType()->getAddressSpace());
318 // alignment (optional)
319 unsigned alignment = gv->getAlignment();
320 if (alignment != 0) {
322 decl += utostr(Log2_32(gv->getAlignment()));
327 if (PointerType::classof(gv->getType())) {
328 const PointerType* pointerTy = dyn_cast<const PointerType>(gv->getType());
329 const Type* elementTy = pointerTy->getElementType();
332 decl += gvsym->getName();
335 if (elementTy->isArrayTy())
337 assert(elementTy->isArrayTy() && "Only pointers to arrays are supported");
339 const ArrayType* arrayTy = dyn_cast<const ArrayType>(elementTy);
340 elementTy = arrayTy->getElementType();
342 unsigned numElements = arrayTy->getNumElements();
344 while (elementTy->isArrayTy()) {
346 arrayTy = dyn_cast<const ArrayType>(elementTy);
347 elementTy = arrayTy->getElementType();
349 numElements *= arrayTy->getNumElements();
352 // FIXME: isPrimitiveType() == false for i16?
353 assert(elementTy->isSingleValueType() &&
354 "Non-primitive types are not handled");
356 // Compute the size of the array, in bytes.
357 uint64_t arraySize = (elementTy->getPrimitiveSizeInBits() >> 3)
360 decl += utostr(arraySize);
365 // handle string constants (assume ConstantArray means string)
367 if (gv->hasInitializer())
369 const Constant *C = gv->getInitializer();
370 if (const ConstantArray *CA = dyn_cast<ConstantArray>(C))
374 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
376 if (i > 0) decl += ",";
379 utohexstr(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());
387 // Note: this is currently the fall-through case and most likely generates
389 decl += getTypeName(gv->getType());
392 decl += gvsym->getName();
394 if (ArrayType::classof(gv->getType()) ||
395 PointerType::classof(gv->getType()))
401 OutStreamer.EmitRawText(Twine(decl));
403 OutStreamer.AddBlankLine();
406 void PTXAsmPrinter::EmitFunctionDeclaration() {
407 // The function label could have already been emitted if two symbols end up
408 // conflicting due to asm renaming. Detect this and emit an error.
409 if (!CurrentFnSym->isUndefined()) {
410 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
411 "' label emitted multiple times to assembly file");
415 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
416 const bool isKernel = MFI->isKernel();
418 std::string decl = isKernel ? ".entry" : ".func";
423 for (PTXMachineFunctionInfo::ret_iterator
424 i = MFI->retRegBegin(), e = MFI->retRegEnd(), b = i;
430 decl += getRegisterTypeName(*i);
432 decl += getRegisterName(*i);
437 // Print function name
439 decl += CurrentFnSym->getName().str();
446 for (PTXMachineFunctionInfo::reg_iterator
447 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i;
456 decl += PARAM_PREFIX;
457 decl += utostr(++cnt);
460 decl += getRegisterTypeName(*i);
462 decl += getRegisterName(*i);
467 // // Print parameter list
468 // if (!MFI->argRegEmpty()) {
472 // for(PTXMachineFunctionInfo::reg_iterator
473 // i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i;
476 // assert(reg != PTX::NoRegister && "Not a valid register!");
479 // decl += ".param .";
480 // decl += getRegisterTypeName(reg);
482 // decl += PARAM_PREFIX;
483 // decl += utostr(++cnt);
486 // for (PTXMachineFunctionInfo::reg_iterator
487 // i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i;
490 // assert(reg != PTX::NoRegister && "Not a valid register!");
494 // decl += getRegisterTypeName(reg);
496 // decl += getRegisterName(reg);
502 OutStreamer.EmitRawText(Twine(decl));
506 printPredicateOperand(const MachineInstr *MI, raw_ostream &O) {
507 int i = MI->findFirstPredOperandIdx();
509 llvm_unreachable("missing predicate operand");
511 unsigned reg = MI->getOperand(i).getReg();
512 int predOp = MI->getOperand(i+1).getImm();
514 DEBUG(dbgs() << "predicate: (" << reg << ", " << predOp << ")\n");
516 if (reg != PTX::NoRegister) {
518 if (predOp == PTX::PRED_NEGATE)
520 O << getRegisterName(reg);
524 #include "PTXGenAsmWriter.inc"
526 // Force static initialization.
527 extern "C" void LLVMInitializePTXAsmPrinter() {
528 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target);
529 RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target);