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