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/Support/raw_ostream.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/CodeGen/AsmPrinter.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/Target/TargetLoweringObjectFile.h"
29 #include "llvm/Target/TargetRegistry.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/raw_ostream.h"
37 static cl::opt<std::string>
38 OptPTXVersion("ptx-version", cl::desc("Set PTX version"),
41 static cl::opt<std::string>
42 OptPTXTarget("ptx-target", cl::desc("Set GPU target (comma-separated list)"),
46 class PTXAsmPrinter : public AsmPrinter {
48 explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
49 : AsmPrinter(TM, Streamer) {}
51 const char *getPassName() const { return "PTX Assembly Printer"; }
53 virtual void EmitStartOfAsmFile(Module &M);
55 virtual bool runOnMachineFunction(MachineFunction &MF);
57 virtual void EmitFunctionBodyStart();
58 virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); }
60 virtual void EmitInstruction(const MachineInstr *MI);
62 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
63 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
64 const char *Modifier = 0);
67 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
68 static const char *getRegisterName(unsigned RegNo);
71 void EmitFunctionDeclaration();
72 }; // class PTXAsmPrinter
75 static const char PARAM_PREFIX[] = "__param_";
77 static const char *getRegisterTypeName(unsigned RegNo) {
78 #define TEST_REGCLS(cls, clsstr) \
79 if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr;
80 TEST_REGCLS(RRegs32, s32);
81 TEST_REGCLS(Preds, pred);
84 llvm_unreachable("Not in any register class!");
88 static const char *getInstructionTypeName(const MachineInstr *MI) {
89 for (int i = 0, e = MI->getNumOperands(); i != e; ++i) {
90 const MachineOperand &MO = MI->getOperand(i);
91 if (MO.getType() == MachineOperand::MO_Register)
92 return getRegisterTypeName(MO.getReg());
95 llvm_unreachable("No reg operand found in instruction!");
99 void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
101 OutStreamer.EmitRawText(Twine("\t.version " + OptPTXVersion));
102 OutStreamer.EmitRawText(Twine("\t.target " + OptPTXTarget));
103 OutStreamer.AddBlankLine();
106 bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
107 SetupMachineFunction(MF);
108 EmitFunctionDeclaration();
113 void PTXAsmPrinter::EmitFunctionBodyStart() {
114 OutStreamer.EmitRawText(Twine("{"));
116 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
118 // Print local variable definition
119 for (PTXMachineFunctionInfo::reg_iterator
120 i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) {
123 std::string def = "\t.reg .";
124 def += getRegisterTypeName(reg);
126 def += getRegisterName(reg);
128 OutStreamer.EmitRawText(Twine(def));
132 void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
136 // Write instruction to str
137 raw_string_ostream OS(str);
138 printInstruction(MI, OS);
142 // Replace "%type" if found
144 if ((pos = str.find("%type")) != std::string::npos)
145 str.replace(pos, /*strlen("%type")==*/5, getInstructionTypeName(MI));
147 StringRef strref = StringRef(str);
148 OutStreamer.EmitRawText(strref);
151 void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
153 const MachineOperand &MO = MI->getOperand(opNum);
155 switch (MO.getType()) {
157 llvm_unreachable("<unknown operand type>");
159 case MachineOperand::MO_Register:
160 OS << getRegisterName(MO.getReg());
162 case MachineOperand::MO_Immediate:
163 OS << (int) MO.getImm();
168 void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
169 raw_ostream &OS, const char *Modifier) {
170 printOperand(MI, opNum, OS);
172 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
173 return; // don't print "+0"
176 printOperand(MI, opNum+1, OS);
179 void PTXAsmPrinter::EmitFunctionDeclaration() {
180 // The function label could have already been emitted if two symbols end up
181 // conflicting due to asm renaming. Detect this and emit an error.
182 if (!CurrentFnSym->isUndefined()) {
183 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
184 "' label emitted multiple times to assembly file");
188 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
189 const bool isKernel = MFI->isKernel();
192 std::string decl = isKernel ? ".entry" : ".func";
194 // Print return register
196 if (!isKernel && reg != PTX::NoRegister) {
197 decl += " (.reg ."; // FIXME: could it return in .param space?
198 decl += getRegisterTypeName(reg);
200 decl += getRegisterName(reg);
204 // Print function name
206 decl += CurrentFnSym->getName().str();
208 // Print parameter list
209 if (!MFI->argRegEmpty()) {
212 for (int i = 0, e = MFI->getNumArg(); i != e; ++i) {
215 decl += ".param .s32 "; // TODO: param's type
216 decl += PARAM_PREFIX;
217 decl += utostr(i + 1);
220 for (PTXMachineFunctionInfo::reg_iterator
221 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; i != e; ++i) {
223 assert(reg != PTX::NoRegister && "Not a valid register!");
227 decl += getRegisterTypeName(reg);
229 decl += getRegisterName(reg);
235 OutStreamer.EmitRawText(Twine(decl));
238 #include "PTXGenAsmWriter.inc"
240 // Force static initialization.
241 extern "C" void LLVMInitializePTXAsmPrinter() {
242 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTXTarget);