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