blob: 175b3c4750506a53fc9a676209cae622206b111d [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"
Eric Christopher50880d02010-09-18 18:52:28 +000020#include "llvm/Support/raw_ostream.h"
21#include "llvm/ADT/SmallString.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000022#include "llvm/ADT/StringExtras.h"
23#include "llvm/ADT/Twine.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000024#include "llvm/CodeGen/AsmPrinter.h"
Eric Christopher50880d02010-09-18 18:52:28 +000025#include "llvm/CodeGen/MachineInstr.h"
26#include "llvm/MC/MCStreamer.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000027#include "llvm/MC/MCSymbol.h"
28#include "llvm/Target/TargetLoweringObjectFile.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000029#include "llvm/Target/TargetRegistry.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/raw_ostream.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000033
34using namespace llvm;
35
36namespace {
Che-Liang Chioudf659632010-11-08 03:06:08 +000037class PTXAsmPrinter : public AsmPrinter {
38public:
39 explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
40 : AsmPrinter(TM, Streamer) {}
Eric Christopher50880d02010-09-18 18:52:28 +000041
Che-Liang Chioudf659632010-11-08 03:06:08 +000042 const char *getPassName() const { return "PTX Assembly Printer"; }
Eric Christopher50880d02010-09-18 18:52:28 +000043
Che-Liang Chioudf659632010-11-08 03:06:08 +000044 virtual bool runOnMachineFunction(MachineFunction &MF);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000045
Che-Liang Chioudf659632010-11-08 03:06:08 +000046 virtual void EmitFunctionBodyStart();
47 virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); }
48
49 virtual void EmitInstruction(const MachineInstr *MI);
50
51 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
52
53 // autogen'd.
54 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
55 static const char *getRegisterName(unsigned RegNo);
56
57private:
58 void EmitFunctionDeclaration();
59}; // class PTXAsmPrinter
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000060} // namespace
61
Che-Liang Chioudf659632010-11-08 03:06:08 +000062static const char PARAM_PREFIX[] = "__param_";
63
64static const char *getRegisterTypeName(unsigned RegNo){
65#define TEST_REGCLS(cls, clsstr) \
66 if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr;
Che-Liang Chiou3f409f72010-11-17 08:08:49 +000067 TEST_REGCLS(RRegs32, s32);
68 TEST_REGCLS(Preds, pred);
Che-Liang Chioudf659632010-11-08 03:06:08 +000069#undef TEST_REGCLS
70
71 llvm_unreachable("Not in any register class!");
72 return NULL;
73}
74
Che-Liang Chiou3f409f72010-11-17 08:08:49 +000075static const char *getInstructionTypeName(const MachineInstr *MI)
76{
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());
81 }
82
83 llvm_unreachable("No reg operand found in instruction!");
84 return NULL;
85}
86
Che-Liang Chioudf659632010-11-08 03:06:08 +000087bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
88 SetupMachineFunction(MF);
89 EmitFunctionDeclaration();
90 EmitFunctionBody();
91 return false;
92}
93
94void PTXAsmPrinter::EmitFunctionBodyStart() {
95 OutStreamer.EmitRawText(Twine("{"));
96
97 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
98
99 // Print local variable definition
100 for (PTXMachineFunctionInfo::reg_iterator
101 i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) {
102 unsigned reg = *i;
103
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000104 std::string def = "\t.reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000105 def += getRegisterTypeName(reg);
106 def += ' ';
107 def += getRegisterName(reg);
108 def += ';';
109 OutStreamer.EmitRawText(Twine(def));
110 }
111}
112
Eric Christopher50880d02010-09-18 18:52:28 +0000113void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000114 SmallString<128> sstr;
115 raw_svector_ostream OS(sstr);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000116 printInstruction(MI, OS);
117 OS << ';';
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000118
119 // Replace "%type" if found
120 StringRef strref = OS.str();
121 size_t pos;
122 if ((pos = strref.find("%type")) == StringRef::npos) {
123 OutStreamer.EmitRawText(strref);
124 return;
125 }
126 std::string str = strref;
127 str.replace(pos, /*strlen("%type")==*/5, getInstructionTypeName(MI));
128 OutStreamer.EmitRawText(StringRef(str));
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000129}
130
131void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
132 raw_ostream &OS) {
133 const MachineOperand &MO = MI->getOperand(opNum);
134
135 switch (MO.getType()) {
136 default:
137 llvm_unreachable("<unknown operand type>");
138 break;
139 case MachineOperand::MO_Register:
140 OS << getRegisterName(MO.getReg());
141 break;
142 case MachineOperand::MO_Immediate:
143 OS << (int) MO.getImm();
144 break;
145 }
Eric Christopher50880d02010-09-18 18:52:28 +0000146}
147
Che-Liang Chioudf659632010-11-08 03:06:08 +0000148void 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");
154 return;
155 }
156
157 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
158 const bool isKernel = MFI->isKernel();
159 unsigned reg;
160
161 std::string decl = isKernel ? ".entry" : ".func";
162
163 // Print return register
164 reg = MFI->retReg();
165 if (!isKernel && reg != PTX::NoRegister) {
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000166 decl += " (.reg ."; // FIXME: could it return in .param space?
Che-Liang Chioudf659632010-11-08 03:06:08 +0000167 decl += getRegisterTypeName(reg);
168 decl += " ";
169 decl += getRegisterName(reg);
170 decl += ")";
171 }
172
173 // Print function name
174 decl += " ";
175 decl += CurrentFnSym->getName().str();
176
177 // Print parameter list
178 if (!MFI->argRegEmpty()) {
179 decl += " (";
180 if (isKernel) {
181 for (int i = 0, e = MFI->getNumArg(); i != e; ++i) {
182 if (i != 0)
183 decl += ", ";
184 decl += ".param .s32 "; // TODO: param's type
185 decl += PARAM_PREFIX;
186 decl += utostr(i + 1);
187 }
188 } else {
189 for (PTXMachineFunctionInfo::reg_iterator
190 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; i != e; ++i) {
191 reg = *i;
192 assert(reg != PTX::NoRegister && "Not a valid register!");
193 if (i != b)
194 decl += ", ";
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000195 decl += ".reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000196 decl += getRegisterTypeName(reg);
197 decl += " ";
198 decl += getRegisterName(reg);
199 }
200 }
201 decl += ")";
202 }
203
204 OutStreamer.EmitRawText(Twine(decl));
205}
206
Eric Christopher50880d02010-09-18 18:52:28 +0000207#include "PTXGenAsmWriter.inc"
208
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000209// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000210extern "C" void LLVMInitializePTXAsmPrinter() {
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000211 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTXTarget);
212}