Add simple arithmetics and %type directive for PTX
[oota-llvm.git] / lib / Target / PTX / PTXAsmPrinter.cpp
1 //===-- 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
15 #define DEBUG_TYPE "ptx-asm-printer"
16
17 #include "PTX.h"
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"
33
34 using namespace llvm;
35
36 namespace {
37 class PTXAsmPrinter : public AsmPrinter {
38 public:
39   explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
40     : AsmPrinter(TM, Streamer) {}
41
42   const char *getPassName() const { return "PTX Assembly Printer"; }
43
44   virtual bool runOnMachineFunction(MachineFunction &MF);
45
46   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
57 private:
58   void EmitFunctionDeclaration();
59 }; // class PTXAsmPrinter
60 } // namespace
61
62 static const char PARAM_PREFIX[] = "__param_";
63
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);
69 #undef TEST_REGCLS
70
71   llvm_unreachable("Not in any register class!");
72   return NULL;
73 }
74
75 static 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
87 bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
88   SetupMachineFunction(MF);
89   EmitFunctionDeclaration();
90   EmitFunctionBody();
91   return false;
92 }
93
94 void 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
104     std::string def = "\t.reg .";
105     def += getRegisterTypeName(reg);
106     def += ' ';
107     def += getRegisterName(reg);
108     def += ';';
109     OutStreamer.EmitRawText(Twine(def));
110   }
111 }
112
113 void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
114   SmallString<128> sstr;
115   raw_svector_ostream OS(sstr);
116   printInstruction(MI, OS);
117   OS << ';';
118
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));
129 }
130
131 void 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   }
146 }
147
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");
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) {
166     decl += " (.reg ."; // FIXME: could it return in .param space?
167     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 += ", ";
195         decl += ".reg .";
196         decl += getRegisterTypeName(reg);
197         decl += " ";
198         decl += getRegisterName(reg);
199       }
200     }
201     decl += ")";
202   }
203
204   OutStreamer.EmitRawText(Twine(decl));
205 }
206
207 #include "PTXGenAsmWriter.inc"
208
209 // Force static initialization.
210 extern "C" void LLVMInitializePTXAsmPrinter() {
211   RegisterAsmPrinter<PTXAsmPrinter> X(ThePTXTarget);
212 }