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/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/raw_ostream.h"
37 class PTXAsmPrinter : public AsmPrinter {
39 explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
40 : AsmPrinter(TM, Streamer) {}
42 const char *getPassName() const { return "PTX Assembly Printer"; }
44 virtual bool runOnMachineFunction(MachineFunction &MF);
46 virtual void EmitFunctionBodyStart();
47 virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); }
49 virtual void EmitInstruction(const MachineInstr *MI);
51 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
54 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
55 static const char *getRegisterName(unsigned RegNo);
58 void EmitFunctionDeclaration();
59 }; // class PTXAsmPrinter
62 static const char PARAM_PREFIX[] = "__param_";
64 static const char *getRegisterTypeName(unsigned RegNo){
65 #define TEST_REGCLS(cls, clsstr) \
66 if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr;
67 TEST_REGCLS(RRegs32, s32);
68 TEST_REGCLS(Preds, pred);
71 llvm_unreachable("Not in any register class!");
75 static const char *getInstructionTypeName(const MachineInstr *MI)
77 for (int i = 0, e = MI->getNumOperands(); i != e; ++i) {
78 const MachineOperand &MO = MI->getOperand(i);
79 if (MO.getType() == MachineOperand::MO_Register)
80 return getRegisterTypeName(MO.getReg());
83 llvm_unreachable("No reg operand found in instruction!");
87 bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
88 SetupMachineFunction(MF);
89 EmitFunctionDeclaration();
94 void PTXAsmPrinter::EmitFunctionBodyStart() {
95 OutStreamer.EmitRawText(Twine("{"));
97 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
99 // Print local variable definition
100 for (PTXMachineFunctionInfo::reg_iterator
101 i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) {
104 std::string def = "\t.reg .";
105 def += getRegisterTypeName(reg);
107 def += getRegisterName(reg);
109 OutStreamer.EmitRawText(Twine(def));
113 void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
114 SmallString<128> sstr;
115 raw_svector_ostream OS(sstr);
116 printInstruction(MI, OS);
119 // Replace "%type" if found
120 StringRef strref = OS.str();
122 if ((pos = strref.find("%type")) == StringRef::npos) {
123 OutStreamer.EmitRawText(strref);
126 std::string str = strref;
127 str.replace(pos, /*strlen("%type")==*/5, getInstructionTypeName(MI));
128 OutStreamer.EmitRawText(StringRef(str));
131 void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
133 const MachineOperand &MO = MI->getOperand(opNum);
135 switch (MO.getType()) {
137 llvm_unreachable("<unknown operand type>");
139 case MachineOperand::MO_Register:
140 OS << getRegisterName(MO.getReg());
142 case MachineOperand::MO_Immediate:
143 OS << (int) MO.getImm();
148 void PTXAsmPrinter::EmitFunctionDeclaration() {
149 // The function label could have already been emitted if two symbols end up
150 // conflicting due to asm renaming. Detect this and emit an error.
151 if (!CurrentFnSym->isUndefined()) {
152 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
153 "' label emitted multiple times to assembly file");
157 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
158 const bool isKernel = MFI->isKernel();
161 std::string decl = isKernel ? ".entry" : ".func";
163 // Print return register
165 if (!isKernel && reg != PTX::NoRegister) {
166 decl += " (.reg ."; // FIXME: could it return in .param space?
167 decl += getRegisterTypeName(reg);
169 decl += getRegisterName(reg);
173 // Print function name
175 decl += CurrentFnSym->getName().str();
177 // Print parameter list
178 if (!MFI->argRegEmpty()) {
181 for (int i = 0, e = MFI->getNumArg(); i != e; ++i) {
184 decl += ".param .s32 "; // TODO: param's type
185 decl += PARAM_PREFIX;
186 decl += utostr(i + 1);
189 for (PTXMachineFunctionInfo::reg_iterator
190 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; i != e; ++i) {
192 assert(reg != PTX::NoRegister && "Not a valid register!");
196 decl += getRegisterTypeName(reg);
198 decl += getRegisterName(reg);
204 OutStreamer.EmitRawText(Twine(decl));
207 #include "PTXGenAsmWriter.inc"
209 // Force static initialization.
210 extern "C" void LLVMInitializePTXAsmPrinter() {
211 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTXTarget);