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