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