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