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" |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 20 | #include "llvm/DerivedTypes.h" |
| 21 | #include "llvm/Module.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
| 24 | #include "llvm/ADT/Twine.h" |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/AsmPrinter.h" |
Justin Holewinski | df1c8d8 | 2011-06-20 15:56:20 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineInstr.h" |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCStreamer.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCSymbol.h" |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 31 | #include "llvm/Target/Mangler.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetRegistry.h" |
Che-Liang Chiou | 21d8b9b | 2010-11-30 10:14:14 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
| 36 | #include "llvm/Support/ErrorHandling.h" |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MathExtras.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace llvm; |
| 41 | |
| 42 | namespace { |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 43 | class PTXAsmPrinter : public AsmPrinter { |
| 44 | public: |
| 45 | explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) |
| 46 | : AsmPrinter(TM, Streamer) {} |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 47 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 48 | const char *getPassName() const { return "PTX Assembly Printer"; } |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 49 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 50 | bool doFinalization(Module &M); |
| 51 | |
Che-Liang Chiou | 21d8b9b | 2010-11-30 10:14:14 +0000 | [diff] [blame] | 52 | virtual void EmitStartOfAsmFile(Module &M); |
| 53 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 54 | virtual bool runOnMachineFunction(MachineFunction &MF); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 55 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 56 | virtual void EmitFunctionBodyStart(); |
| 57 | virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); } |
| 58 | |
| 59 | virtual void EmitInstruction(const MachineInstr *MI); |
| 60 | |
| 61 | void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS); |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 62 | void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS, |
| 63 | const char *Modifier = 0); |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 64 | void printParamOperand(const MachineInstr *MI, int opNum, raw_ostream &OS, |
| 65 | const char *Modifier = 0); |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame^] | 66 | void printReturnOperand(const MachineInstr *MI, int opNum, raw_ostream &OS, |
| 67 | const char *Modifier = 0); |
Che-Liang Chiou | c2ec0f9 | 2011-03-13 17:26:00 +0000 | [diff] [blame] | 68 | void printPredicateOperand(const MachineInstr *MI, raw_ostream &O); |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 69 | |
| 70 | // autogen'd. |
| 71 | void printInstruction(const MachineInstr *MI, raw_ostream &OS); |
| 72 | static const char *getRegisterName(unsigned RegNo); |
| 73 | |
| 74 | private: |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 75 | void EmitVariableDeclaration(const GlobalVariable *gv); |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 76 | void EmitFunctionDeclaration(); |
| 77 | }; // class PTXAsmPrinter |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 78 | } // namespace |
| 79 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 80 | static const char PARAM_PREFIX[] = "__param_"; |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame^] | 81 | static const char RETURN_PREFIX[] = "__ret_"; |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 82 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 83 | static const char *getRegisterTypeName(unsigned RegNo) { |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 84 | #define TEST_REGCLS(cls, clsstr) \ |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 85 | if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr; |
Justin Holewinski | 1b91bcd | 2011-06-16 17:49:58 +0000 | [diff] [blame] | 86 | TEST_REGCLS(RegPred, pred); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 87 | TEST_REGCLS(RegI16, b16); |
| 88 | TEST_REGCLS(RegI32, b32); |
| 89 | TEST_REGCLS(RegI64, b64); |
| 90 | TEST_REGCLS(RegF32, b32); |
| 91 | TEST_REGCLS(RegF64, b64); |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 92 | #undef TEST_REGCLS |
| 93 | |
| 94 | llvm_unreachable("Not in any register class!"); |
| 95 | return NULL; |
| 96 | } |
| 97 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 98 | static const char *getStateSpaceName(unsigned addressSpace) { |
Che-Liang Chiou | d34f19f | 2010-12-30 10:41:27 +0000 | [diff] [blame] | 99 | switch (addressSpace) { |
| 100 | default: llvm_unreachable("Unknown state space"); |
| 101 | case PTX::GLOBAL: return "global"; |
| 102 | case PTX::CONSTANT: return "const"; |
| 103 | case PTX::LOCAL: return "local"; |
| 104 | case PTX::PARAMETER: return "param"; |
| 105 | case PTX::SHARED: return "shared"; |
| 106 | } |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 107 | return NULL; |
| 108 | } |
| 109 | |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 110 | static const char *getTypeName(const Type* type) { |
| 111 | while (true) { |
| 112 | switch (type->getTypeID()) { |
| 113 | default: llvm_unreachable("Unknown type"); |
| 114 | case Type::FloatTyID: return ".f32"; |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 115 | case Type::DoubleTyID: return ".f64"; |
| 116 | case Type::IntegerTyID: |
| 117 | switch (type->getPrimitiveSizeInBits()) { |
| 118 | default: llvm_unreachable("Unknown integer bit-width"); |
| 119 | case 16: return ".u16"; |
| 120 | case 32: return ".u32"; |
| 121 | case 64: return ".u64"; |
| 122 | } |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 123 | case Type::ArrayTyID: |
| 124 | case Type::PointerTyID: |
| 125 | type = dyn_cast<const SequentialType>(type)->getElementType(); |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | return NULL; |
| 130 | } |
| 131 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 132 | bool PTXAsmPrinter::doFinalization(Module &M) { |
| 133 | // XXX Temproarily remove global variables so that doFinalization() will not |
| 134 | // emit them again (global variables are emitted at beginning). |
| 135 | |
| 136 | Module::GlobalListType &global_list = M.getGlobalList(); |
| 137 | int i, n = global_list.size(); |
| 138 | GlobalVariable **gv_array = new GlobalVariable* [n]; |
| 139 | |
| 140 | // first, back-up GlobalVariable in gv_array |
| 141 | i = 0; |
| 142 | for (Module::global_iterator I = global_list.begin(), E = global_list.end(); |
| 143 | I != E; ++I) |
| 144 | gv_array[i++] = &*I; |
| 145 | |
| 146 | // second, empty global_list |
| 147 | while (!global_list.empty()) |
| 148 | global_list.remove(global_list.begin()); |
| 149 | |
| 150 | // call doFinalization |
| 151 | bool ret = AsmPrinter::doFinalization(M); |
| 152 | |
| 153 | // now we restore global variables |
| 154 | for (i = 0; i < n; i ++) |
| 155 | global_list.insert(global_list.end(), gv_array[i]); |
| 156 | |
| 157 | delete[] gv_array; |
| 158 | return ret; |
| 159 | } |
| 160 | |
Che-Liang Chiou | 21d8b9b | 2010-11-30 10:14:14 +0000 | [diff] [blame] | 161 | void PTXAsmPrinter::EmitStartOfAsmFile(Module &M) |
| 162 | { |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 163 | const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>(); |
| 164 | |
| 165 | OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString())); |
| 166 | OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() + |
Justin Holewinski | 12785e8 | 2011-03-03 13:34:29 +0000 | [diff] [blame] | 167 | (ST.supportsDouble() ? "" |
| 168 | : ", map_f64_to_f32"))); |
Justin Holewinski | a9c85f9 | 2011-06-22 00:43:56 +0000 | [diff] [blame] | 169 | // .address_size directive is optional, but it must immediately follow |
| 170 | // the .target directive if present within a module |
| 171 | if (ST.supportsPTX23()) { |
| 172 | std::string addrSize = ST.is64Bit() ? "64" : "32"; |
| 173 | OutStreamer.EmitRawText(Twine("\t.address_size " + addrSize)); |
| 174 | } |
| 175 | |
Che-Liang Chiou | 21d8b9b | 2010-11-30 10:14:14 +0000 | [diff] [blame] | 176 | OutStreamer.AddBlankLine(); |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 177 | |
| 178 | // declare global variables |
| 179 | for (Module::const_global_iterator i = M.global_begin(), e = M.global_end(); |
| 180 | i != e; ++i) |
| 181 | EmitVariableDeclaration(i); |
Che-Liang Chiou | 21d8b9b | 2010-11-30 10:14:14 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 184 | bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
| 185 | SetupMachineFunction(MF); |
| 186 | EmitFunctionDeclaration(); |
| 187 | EmitFunctionBody(); |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | void PTXAsmPrinter::EmitFunctionBodyStart() { |
| 192 | OutStreamer.EmitRawText(Twine("{")); |
| 193 | |
| 194 | const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>(); |
| 195 | |
| 196 | // Print local variable definition |
| 197 | for (PTXMachineFunctionInfo::reg_iterator |
| 198 | i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) { |
| 199 | unsigned reg = *i; |
| 200 | |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame] | 201 | std::string def = "\t.reg ."; |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 202 | def += getRegisterTypeName(reg); |
| 203 | def += ' '; |
| 204 | def += getRegisterName(reg); |
| 205 | def += ';'; |
| 206 | OutStreamer.EmitRawText(Twine(def)); |
| 207 | } |
Justin Holewinski | df1c8d8 | 2011-06-20 15:56:20 +0000 | [diff] [blame] | 208 | |
| 209 | const MachineFrameInfo* FrameInfo = MF->getFrameInfo(); |
Justin Holewinski | 08d0316 | 2011-06-22 16:07:03 +0000 | [diff] [blame] | 210 | DEBUG(dbgs() << "Have " << FrameInfo->getNumObjects() |
| 211 | << " frame object(s)\n"); |
Justin Holewinski | df1c8d8 | 2011-06-20 15:56:20 +0000 | [diff] [blame] | 212 | for (unsigned i = 0, e = FrameInfo->getNumObjects(); i != e; ++i) { |
| 213 | DEBUG(dbgs() << "Size of object: " << FrameInfo->getObjectSize(i) << "\n"); |
Justin Holewinski | 08d0316 | 2011-06-22 16:07:03 +0000 | [diff] [blame] | 214 | if (FrameInfo->getObjectSize(i) > 0) { |
| 215 | std::string def = "\t.reg .b"; |
| 216 | def += utostr(FrameInfo->getObjectSize(i)*8); // Convert to bits |
| 217 | def += " s"; |
| 218 | def += utostr(i); |
| 219 | def += ";"; |
| 220 | OutStreamer.EmitRawText(Twine(def)); |
| 221 | } |
Justin Holewinski | df1c8d8 | 2011-06-20 15:56:20 +0000 | [diff] [blame] | 222 | } |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 225 | void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) { |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 226 | std::string str; |
| 227 | str.reserve(64); |
| 228 | |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 229 | raw_string_ostream OS(str); |
Che-Liang Chiou | c2ec0f9 | 2011-03-13 17:26:00 +0000 | [diff] [blame] | 230 | |
| 231 | // Emit predicate |
| 232 | printPredicateOperand(MI, OS); |
| 233 | |
| 234 | // Write instruction to str |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 235 | printInstruction(MI, OS); |
| 236 | OS << ';'; |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 237 | OS.flush(); |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame] | 238 | |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 239 | StringRef strref = StringRef(str); |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 240 | OutStreamer.EmitRawText(strref); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum, |
| 244 | raw_ostream &OS) { |
| 245 | const MachineOperand &MO = MI->getOperand(opNum); |
| 246 | |
| 247 | switch (MO.getType()) { |
| 248 | default: |
| 249 | llvm_unreachable("<unknown operand type>"); |
| 250 | break; |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 251 | case MachineOperand::MO_GlobalAddress: |
| 252 | OS << *Mang->getSymbol(MO.getGlobal()); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 253 | break; |
| 254 | case MachineOperand::MO_Immediate: |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 255 | OS << (long) MO.getImm(); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 256 | break; |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 257 | case MachineOperand::MO_MachineBasicBlock: |
| 258 | OS << *MO.getMBB()->getSymbol(); |
| 259 | break; |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 260 | case MachineOperand::MO_Register: |
| 261 | OS << getRegisterName(MO.getReg()); |
| 262 | break; |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 263 | case MachineOperand::MO_FPImmediate: |
| 264 | APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt(); |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 265 | bool isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID; |
| 266 | // Emit 0F for 32-bit floats and 0D for 64-bit doubles. |
| 267 | if (isFloat) { |
| 268 | OS << "0F"; |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 269 | } |
| 270 | else { |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 271 | OS << "0D"; |
| 272 | } |
| 273 | // Emit the encoded floating-point value. |
| 274 | if (constFP.getZExtValue() > 0) { |
| 275 | OS << constFP.toString(16, false); |
| 276 | } |
| 277 | else { |
| 278 | OS << "00000000"; |
| 279 | // If We have a double-precision zero, pad to 8-bytes. |
| 280 | if (!isFloat) { |
| 281 | OS << "00000000"; |
| 282 | } |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 283 | } |
| 284 | break; |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 285 | } |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 288 | void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum, |
| 289 | raw_ostream &OS, const char *Modifier) { |
| 290 | printOperand(MI, opNum, OS); |
| 291 | |
| 292 | if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0) |
| 293 | return; // don't print "+0" |
| 294 | |
| 295 | OS << "+"; |
| 296 | printOperand(MI, opNum+1, OS); |
| 297 | } |
| 298 | |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 299 | void PTXAsmPrinter::printParamOperand(const MachineInstr *MI, int opNum, |
| 300 | raw_ostream &OS, const char *Modifier) { |
| 301 | OS << PARAM_PREFIX << (int) MI->getOperand(opNum).getImm() + 1; |
| 302 | } |
| 303 | |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame^] | 304 | void PTXAsmPrinter::printReturnOperand(const MachineInstr *MI, int opNum, |
| 305 | raw_ostream &OS, const char *Modifier) { |
| 306 | OS << RETURN_PREFIX << (int) MI->getOperand(opNum).getImm() + 1; |
| 307 | } |
| 308 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 309 | void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) { |
| 310 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 311 | if (EmitSpecialLLVMGlobal(gv)) |
| 312 | return; |
| 313 | |
| 314 | MCSymbol *gvsym = Mang->getSymbol(gv); |
| 315 | |
| 316 | assert(gvsym->isUndefined() && "Cannot define a symbol twice!"); |
| 317 | |
| 318 | std::string decl; |
| 319 | |
| 320 | // check if it is defined in some other translation unit |
| 321 | if (gv->isDeclaration()) |
| 322 | decl += ".extern "; |
| 323 | |
| 324 | // state space: e.g., .global |
| 325 | decl += "."; |
| 326 | decl += getStateSpaceName(gv->getType()->getAddressSpace()); |
| 327 | decl += " "; |
| 328 | |
| 329 | // alignment (optional) |
| 330 | unsigned alignment = gv->getAlignment(); |
| 331 | if (alignment != 0) { |
| 332 | decl += ".align "; |
| 333 | decl += utostr(Log2_32(gv->getAlignment())); |
| 334 | decl += " "; |
| 335 | } |
| 336 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 337 | |
Justin Holewinski | ae3ce17 | 2011-03-14 15:40:11 +0000 | [diff] [blame] | 338 | if (PointerType::classof(gv->getType())) { |
| 339 | const PointerType* pointerTy = dyn_cast<const PointerType>(gv->getType()); |
| 340 | const Type* elementTy = pointerTy->getElementType(); |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 341 | |
Justin Holewinski | ae3ce17 | 2011-03-14 15:40:11 +0000 | [diff] [blame] | 342 | decl += ".b8 "; |
| 343 | decl += gvsym->getName(); |
| 344 | decl += "["; |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 345 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 346 | if (elementTy->isArrayTy()) |
| 347 | { |
| 348 | assert(elementTy->isArrayTy() && "Only pointers to arrays are supported"); |
| 349 | |
| 350 | const ArrayType* arrayTy = dyn_cast<const ArrayType>(elementTy); |
| 351 | elementTy = arrayTy->getElementType(); |
| 352 | |
| 353 | unsigned numElements = arrayTy->getNumElements(); |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 354 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 355 | while (elementTy->isArrayTy()) { |
| 356 | |
| 357 | arrayTy = dyn_cast<const ArrayType>(elementTy); |
| 358 | elementTy = arrayTy->getElementType(); |
| 359 | |
| 360 | numElements *= arrayTy->getNumElements(); |
| 361 | } |
| 362 | |
| 363 | // FIXME: isPrimitiveType() == false for i16? |
| 364 | assert(elementTy->isSingleValueType() && |
| 365 | "Non-primitive types are not handled"); |
| 366 | |
| 367 | // Compute the size of the array, in bytes. |
| 368 | uint64_t arraySize = (elementTy->getPrimitiveSizeInBits() >> 3) |
| 369 | * numElements; |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 370 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 371 | decl += utostr(arraySize); |
| 372 | } |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 373 | |
Justin Holewinski | ae3ce17 | 2011-03-14 15:40:11 +0000 | [diff] [blame] | 374 | decl += "]"; |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 375 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 376 | // handle string constants (assume ConstantArray means string) |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 377 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 378 | if (gv->hasInitializer()) |
| 379 | { |
Jay Foad | 7d715df | 2011-06-19 18:37:11 +0000 | [diff] [blame] | 380 | const Constant *C = gv->getInitializer(); |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 381 | if (const ConstantArray *CA = dyn_cast<ConstantArray>(C)) |
| 382 | { |
| 383 | decl += " = {"; |
| 384 | |
| 385 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) |
| 386 | { |
| 387 | if (i > 0) decl += ","; |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 388 | |
| 389 | decl += "0x" + |
| 390 | utohexstr(cast<ConstantInt>(CA->getOperand(i))->getZExtValue()); |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 391 | } |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 392 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 393 | decl += "}"; |
| 394 | } |
| 395 | } |
Justin Holewinski | ae3ce17 | 2011-03-14 15:40:11 +0000 | [diff] [blame] | 396 | } |
| 397 | else { |
| 398 | // Note: this is currently the fall-through case and most likely generates |
| 399 | // incorrect code. |
| 400 | decl += getTypeName(gv->getType()); |
| 401 | decl += " "; |
| 402 | |
| 403 | decl += gvsym->getName(); |
| 404 | |
| 405 | if (ArrayType::classof(gv->getType()) || |
| 406 | PointerType::classof(gv->getType())) |
| 407 | decl += "[]"; |
| 408 | } |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 409 | |
| 410 | decl += ";"; |
| 411 | |
| 412 | OutStreamer.EmitRawText(Twine(decl)); |
| 413 | |
| 414 | OutStreamer.AddBlankLine(); |
| 415 | } |
| 416 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 417 | void PTXAsmPrinter::EmitFunctionDeclaration() { |
| 418 | // The function label could have already been emitted if two symbols end up |
| 419 | // conflicting due to asm renaming. Detect this and emit an error. |
| 420 | if (!CurrentFnSym->isUndefined()) { |
| 421 | report_fatal_error("'" + Twine(CurrentFnSym->getName()) + |
| 422 | "' label emitted multiple times to assembly file"); |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>(); |
| 427 | const bool isKernel = MFI->isKernel(); |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 428 | const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>(); |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 429 | |
| 430 | std::string decl = isKernel ? ".entry" : ".func"; |
| 431 | |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame^] | 432 | unsigned cnt = 0; |
| 433 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 434 | if (!isKernel) { |
| 435 | decl += " ("; |
| 436 | |
| 437 | for (PTXMachineFunctionInfo::ret_iterator |
| 438 | i = MFI->retRegBegin(), e = MFI->retRegEnd(), b = i; |
| 439 | i != e; ++i) { |
| 440 | if (i != b) { |
| 441 | decl += ", "; |
| 442 | } |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame^] | 443 | if (ST.getShaderModel() >= PTXSubtarget::PTX_SM_2_0) { |
| 444 | decl += ".param .b"; |
| 445 | decl += utostr(*i); |
| 446 | decl += " "; |
| 447 | decl += RETURN_PREFIX; |
| 448 | decl += utostr(++cnt); |
| 449 | } else { |
| 450 | decl += ".reg ."; |
| 451 | decl += getRegisterTypeName(*i); |
| 452 | decl += " "; |
| 453 | decl += getRegisterName(*i); |
| 454 | } |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 455 | } |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 456 | decl += ")"; |
| 457 | } |
| 458 | |
| 459 | // Print function name |
| 460 | decl += " "; |
| 461 | decl += CurrentFnSym->getName().str(); |
| 462 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 463 | decl += " ("; |
| 464 | |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame^] | 465 | cnt = 0; |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 466 | |
| 467 | // Print parameters |
| 468 | for (PTXMachineFunctionInfo::reg_iterator |
| 469 | i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; |
| 470 | i != e; ++i) { |
| 471 | if (i != b) { |
| 472 | decl += ", "; |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 473 | } |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 474 | if (isKernel || ST.getShaderModel() >= PTXSubtarget::PTX_SM_2_0) { |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 475 | decl += ".param .b"; |
| 476 | decl += utostr(*i); |
| 477 | decl += " "; |
| 478 | decl += PARAM_PREFIX; |
| 479 | decl += utostr(++cnt); |
| 480 | } else { |
| 481 | decl += ".reg ."; |
| 482 | decl += getRegisterTypeName(*i); |
| 483 | decl += " "; |
| 484 | decl += getRegisterName(*i); |
| 485 | } |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 486 | } |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 487 | decl += ")"; |
| 488 | |
| 489 | // // Print parameter list |
| 490 | // if (!MFI->argRegEmpty()) { |
| 491 | // decl += " ("; |
| 492 | // if (isKernel) { |
| 493 | // unsigned cnt = 0; |
| 494 | // for(PTXMachineFunctionInfo::reg_iterator |
| 495 | // i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; |
| 496 | // i != e; ++i) { |
| 497 | // reg = *i; |
| 498 | // assert(reg != PTX::NoRegister && "Not a valid register!"); |
| 499 | // if (i != b) |
| 500 | // decl += ", "; |
| 501 | // decl += ".param ."; |
| 502 | // decl += getRegisterTypeName(reg); |
| 503 | // decl += " "; |
| 504 | // decl += PARAM_PREFIX; |
| 505 | // decl += utostr(++cnt); |
| 506 | // } |
| 507 | // } else { |
| 508 | // for (PTXMachineFunctionInfo::reg_iterator |
| 509 | // i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; |
| 510 | // i != e; ++i) { |
| 511 | // reg = *i; |
| 512 | // assert(reg != PTX::NoRegister && "Not a valid register!"); |
| 513 | // if (i != b) |
| 514 | // decl += ", "; |
| 515 | // decl += ".reg ."; |
| 516 | // decl += getRegisterTypeName(reg); |
| 517 | // decl += " "; |
| 518 | // decl += getRegisterName(reg); |
| 519 | // } |
| 520 | // } |
| 521 | // decl += ")"; |
| 522 | // } |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 523 | |
| 524 | OutStreamer.EmitRawText(Twine(decl)); |
| 525 | } |
| 526 | |
Che-Liang Chiou | c2ec0f9 | 2011-03-13 17:26:00 +0000 | [diff] [blame] | 527 | void PTXAsmPrinter:: |
| 528 | printPredicateOperand(const MachineInstr *MI, raw_ostream &O) { |
| 529 | int i = MI->findFirstPredOperandIdx(); |
| 530 | if (i == -1) |
| 531 | llvm_unreachable("missing predicate operand"); |
| 532 | |
| 533 | unsigned reg = MI->getOperand(i).getReg(); |
| 534 | int predOp = MI->getOperand(i+1).getImm(); |
| 535 | |
| 536 | DEBUG(dbgs() << "predicate: (" << reg << ", " << predOp << ")\n"); |
| 537 | |
Che-Liang Chiou | f78847e | 2011-03-14 11:26:01 +0000 | [diff] [blame] | 538 | if (reg != PTX::NoRegister) { |
Che-Liang Chiou | c2ec0f9 | 2011-03-13 17:26:00 +0000 | [diff] [blame] | 539 | O << '@'; |
| 540 | if (predOp == PTX::PRED_NEGATE) |
| 541 | O << '!'; |
| 542 | O << getRegisterName(reg); |
| 543 | } |
| 544 | } |
| 545 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 546 | #include "PTXGenAsmWriter.inc" |
| 547 | |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 548 | // Force static initialization. |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 549 | extern "C" void LLVMInitializePTXAsmPrinter() { |
Justin Holewinski | e1fee48 | 2011-04-20 15:37:17 +0000 | [diff] [blame] | 550 | RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target); |
| 551 | RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target); |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 552 | } |