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