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; |
| 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 | bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
| 76 | SetupMachineFunction(MF); |
| 77 | EmitFunctionDeclaration(); |
| 78 | EmitFunctionBody(); |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | void PTXAsmPrinter::EmitFunctionBodyStart() { |
| 83 | OutStreamer.EmitRawText(Twine("{")); |
| 84 | |
| 85 | const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>(); |
| 86 | |
| 87 | // Print local variable definition |
| 88 | for (PTXMachineFunctionInfo::reg_iterator |
| 89 | i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) { |
| 90 | unsigned reg = *i; |
| 91 | |
| 92 | std::string def = "\t.reg "; |
| 93 | def += getRegisterTypeName(reg); |
| 94 | def += ' '; |
| 95 | def += getRegisterName(reg); |
| 96 | def += ';'; |
| 97 | OutStreamer.EmitRawText(Twine(def)); |
| 98 | } |
| 99 | } |
| 100 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 101 | void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) { |
| 102 | SmallString<128> str; |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 103 | raw_svector_ostream OS(str); |
| 104 | printInstruction(MI, OS); |
| 105 | OS << ';'; |
| 106 | OutStreamer.EmitRawText(OS.str()); |
| 107 | } |
| 108 | |
| 109 | void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum, |
| 110 | raw_ostream &OS) { |
| 111 | const MachineOperand &MO = MI->getOperand(opNum); |
| 112 | |
| 113 | switch (MO.getType()) { |
| 114 | default: |
| 115 | llvm_unreachable("<unknown operand type>"); |
| 116 | break; |
| 117 | case MachineOperand::MO_Register: |
| 118 | OS << getRegisterName(MO.getReg()); |
| 119 | break; |
| 120 | case MachineOperand::MO_Immediate: |
| 121 | OS << (int) MO.getImm(); |
| 122 | break; |
| 123 | } |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame^] | 126 | void PTXAsmPrinter::EmitFunctionDeclaration() { |
| 127 | // The function label could have already been emitted if two symbols end up |
| 128 | // conflicting due to asm renaming. Detect this and emit an error. |
| 129 | if (!CurrentFnSym->isUndefined()) { |
| 130 | report_fatal_error("'" + Twine(CurrentFnSym->getName()) + |
| 131 | "' label emitted multiple times to assembly file"); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>(); |
| 136 | const bool isKernel = MFI->isKernel(); |
| 137 | unsigned reg; |
| 138 | |
| 139 | std::string decl = isKernel ? ".entry" : ".func"; |
| 140 | |
| 141 | // Print return register |
| 142 | reg = MFI->retReg(); |
| 143 | if (!isKernel && reg != PTX::NoRegister) { |
| 144 | decl += " (.reg "; // FIXME: could it return in .param space? |
| 145 | decl += getRegisterTypeName(reg); |
| 146 | decl += " "; |
| 147 | decl += getRegisterName(reg); |
| 148 | decl += ")"; |
| 149 | } |
| 150 | |
| 151 | // Print function name |
| 152 | decl += " "; |
| 153 | decl += CurrentFnSym->getName().str(); |
| 154 | |
| 155 | // Print parameter list |
| 156 | if (!MFI->argRegEmpty()) { |
| 157 | decl += " ("; |
| 158 | if (isKernel) { |
| 159 | for (int i = 0, e = MFI->getNumArg(); i != e; ++i) { |
| 160 | if (i != 0) |
| 161 | decl += ", "; |
| 162 | decl += ".param .s32 "; // TODO: param's type |
| 163 | decl += PARAM_PREFIX; |
| 164 | decl += utostr(i + 1); |
| 165 | } |
| 166 | } else { |
| 167 | for (PTXMachineFunctionInfo::reg_iterator |
| 168 | i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; i != e; ++i) { |
| 169 | reg = *i; |
| 170 | assert(reg != PTX::NoRegister && "Not a valid register!"); |
| 171 | if (i != b) |
| 172 | decl += ", "; |
| 173 | decl += ".reg "; |
| 174 | decl += getRegisterTypeName(reg); |
| 175 | decl += " "; |
| 176 | decl += getRegisterName(reg); |
| 177 | } |
| 178 | } |
| 179 | decl += ")"; |
| 180 | } |
| 181 | |
| 182 | OutStreamer.EmitRawText(Twine(decl)); |
| 183 | } |
| 184 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 185 | #include "PTXGenAsmWriter.inc" |
| 186 | |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 187 | // Force static initialization. |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 188 | extern "C" void LLVMInitializePTXAsmPrinter() { |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 189 | RegisterAsmPrinter<PTXAsmPrinter> X(ThePTXTarget); |
| 190 | } |