Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 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 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "ptx-asm-printer" |
| 16 | |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 17 | #include "PTX.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 18 | #include "PTXMachineFunctionInfo.h" |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 19 | #include "PTXTargetMachine.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | #include "llvm/ADT/SmallString.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringExtras.h" |
| 23 | #include "llvm/ADT/Twine.h" |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/AsmPrinter.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineInstr.h" |
| 26 | #include "llvm/MC/MCStreamer.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCSymbol.h" |
| 28 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetRegistry.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/ErrorHandling.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace llvm; |
| 35 | |
| 36 | namespace { |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 37 | class PTXAsmPrinter : public AsmPrinter { |
| 38 | public: |
| 39 | explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) |
| 40 | : AsmPrinter(TM, Streamer) {} |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 41 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 42 | const char *getPassName() const { return "PTX Assembly Printer"; } |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 43 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 44 | virtual bool runOnMachineFunction(MachineFunction &MF); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 45 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 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 |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 60 | } // namespace |
| 61 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 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; |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame^] | 67 | TEST_REGCLS(RRegs32, s32); |
| 68 | TEST_REGCLS(Preds, pred); |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 69 | #undef TEST_REGCLS |
| 70 | |
| 71 | llvm_unreachable("Not in any register class!"); |
| 72 | return NULL; |
| 73 | } |
| 74 | |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame^] | 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 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 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 | |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame^] | 104 | std::string def = "\t.reg ."; |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 105 | def += getRegisterTypeName(reg); |
| 106 | def += ' '; |
| 107 | def += getRegisterName(reg); |
| 108 | def += ';'; |
| 109 | OutStreamer.EmitRawText(Twine(def)); |
| 110 | } |
| 111 | } |
| 112 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 113 | void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) { |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame^] | 114 | SmallString<128> sstr; |
| 115 | raw_svector_ostream OS(sstr); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 116 | printInstruction(MI, OS); |
| 117 | OS << ';'; |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame^] | 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)); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 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 | } |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 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) { |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame^] | 166 | decl += " (.reg ."; // FIXME: could it return in .param space? |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 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 += ", "; |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame^] | 195 | decl += ".reg ."; |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 196 | decl += getRegisterTypeName(reg); |
| 197 | decl += " "; |
| 198 | decl += getRegisterName(reg); |
| 199 | } |
| 200 | } |
| 201 | decl += ")"; |
| 202 | } |
| 203 | |
| 204 | OutStreamer.EmitRawText(Twine(decl)); |
| 205 | } |
| 206 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 207 | #include "PTXGenAsmWriter.inc" |
| 208 | |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 209 | // Force static initialization. |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 210 | extern "C" void LLVMInitializePTXAsmPrinter() { |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 211 | RegisterAsmPrinter<PTXAsmPrinter> X(ThePTXTarget); |
| 212 | } |