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 | 6b8990d | 2011-09-26 16:20:25 +0000 | [diff] [blame] | 300 | std::string def = "\t.local .b"; |
Justin Holewinski | 08d0316 | 2011-06-22 16:07:03 +0000 | [diff] [blame] | 301 | def += utostr(FrameInfo->getObjectSize(i)*8); // Convert to bits |
Justin Holewinski | c1d8fbd | 2011-09-26 16:20:28 +0000 | [diff] [blame^] | 302 | def += " __local"; |
Justin Holewinski | 08d0316 | 2011-06-22 16:07:03 +0000 | [diff] [blame] | 303 | def += utostr(i); |
| 304 | def += ";"; |
| 305 | OutStreamer.EmitRawText(Twine(def)); |
| 306 | } |
Justin Holewinski | df1c8d8 | 2011-06-20 15:56:20 +0000 | [diff] [blame] | 307 | } |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 308 | |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 309 | //unsigned Index = 1; |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 310 | // Print parameter passing params |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 311 | //for (PTXMachineFunctionInfo::param_iterator |
| 312 | // i = MFI->paramBegin(), e = MFI->paramEnd(); i != e; ++i) { |
| 313 | // std::string def = "\t.param .b"; |
| 314 | // def += utostr(*i); |
| 315 | // def += " __ret_"; |
| 316 | // def += utostr(Index); |
| 317 | // Index++; |
| 318 | // def += ";"; |
| 319 | // OutStreamer.EmitRawText(Twine(def)); |
| 320 | //} |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 323 | void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) { |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 324 | std::string str; |
| 325 | str.reserve(64); |
| 326 | |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 327 | raw_string_ostream OS(str); |
Che-Liang Chiou | c2ec0f9 | 2011-03-13 17:26:00 +0000 | [diff] [blame] | 328 | |
Justin Holewinski | 4799729 | 2011-06-24 19:19:18 +0000 | [diff] [blame] | 329 | DebugLoc DL = MI->getDebugLoc(); |
| 330 | if (!DL.isUnknown()) { |
| 331 | |
| 332 | const MDNode *S = DL.getScope(MF->getFunction()->getContext()); |
| 333 | |
| 334 | // This is taken from DwarfDebug.cpp, which is conveniently not a public |
| 335 | // LLVM class. |
| 336 | StringRef Fn; |
| 337 | StringRef Dir; |
| 338 | unsigned Src = 1; |
| 339 | if (S) { |
| 340 | DIDescriptor Scope(S); |
| 341 | if (Scope.isCompileUnit()) { |
| 342 | DICompileUnit CU(S); |
| 343 | Fn = CU.getFilename(); |
| 344 | Dir = CU.getDirectory(); |
| 345 | } else if (Scope.isFile()) { |
| 346 | DIFile F(S); |
| 347 | Fn = F.getFilename(); |
| 348 | Dir = F.getDirectory(); |
| 349 | } else if (Scope.isSubprogram()) { |
| 350 | DISubprogram SP(S); |
| 351 | Fn = SP.getFilename(); |
| 352 | Dir = SP.getDirectory(); |
| 353 | } else if (Scope.isLexicalBlock()) { |
| 354 | DILexicalBlock DB(S); |
| 355 | Fn = DB.getFilename(); |
| 356 | Dir = DB.getDirectory(); |
| 357 | } else |
| 358 | assert(0 && "Unexpected scope info"); |
| 359 | |
| 360 | Src = GetOrCreateSourceID(Fn, Dir); |
| 361 | } |
| 362 | OutStreamer.EmitDwarfLocDirective(Src, DL.getLine(), DL.getCol(), |
| 363 | 0, 0, 0, Fn); |
| 364 | |
| 365 | const MCDwarfLoc& MDL = OutContext.getCurrentDwarfLoc(); |
| 366 | |
| 367 | OS << "\t.loc "; |
| 368 | OS << utostr(MDL.getFileNum()); |
| 369 | OS << " "; |
| 370 | OS << utostr(MDL.getLine()); |
| 371 | OS << " "; |
| 372 | OS << utostr(MDL.getColumn()); |
| 373 | OS << "\n"; |
| 374 | } |
| 375 | |
| 376 | |
Che-Liang Chiou | c2ec0f9 | 2011-03-13 17:26:00 +0000 | [diff] [blame] | 377 | // Emit predicate |
| 378 | printPredicateOperand(MI, OS); |
| 379 | |
| 380 | // Write instruction to str |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 381 | if (MI->getOpcode() == PTX::CALL) { |
| 382 | printCall(MI, OS); |
| 383 | } else { |
| 384 | printInstruction(MI, OS); |
| 385 | } |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 386 | OS << ';'; |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 387 | OS.flush(); |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame] | 388 | |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 389 | StringRef strref = StringRef(str); |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 390 | OutStreamer.EmitRawText(strref); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum, |
| 394 | raw_ostream &OS) { |
| 395 | const MachineOperand &MO = MI->getOperand(opNum); |
Justin Holewinski | 297984d | 2011-09-22 16:45:40 +0000 | [diff] [blame] | 396 | const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>(); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 397 | |
| 398 | switch (MO.getType()) { |
| 399 | default: |
| 400 | llvm_unreachable("<unknown operand type>"); |
| 401 | break; |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 402 | case MachineOperand::MO_GlobalAddress: |
| 403 | OS << *Mang->getSymbol(MO.getGlobal()); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 404 | break; |
| 405 | case MachineOperand::MO_Immediate: |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 406 | OS << (long) MO.getImm(); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 407 | break; |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 408 | case MachineOperand::MO_MachineBasicBlock: |
| 409 | OS << *MO.getMBB()->getSymbol(); |
| 410 | break; |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 411 | case MachineOperand::MO_Register: |
Justin Holewinski | 297984d | 2011-09-22 16:45:40 +0000 | [diff] [blame] | 412 | OS << MFI->getRegisterName(MO.getReg()); |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 413 | break; |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 414 | case MachineOperand::MO_FPImmediate: |
| 415 | APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt(); |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 416 | bool isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID; |
| 417 | // Emit 0F for 32-bit floats and 0D for 64-bit doubles. |
| 418 | if (isFloat) { |
| 419 | OS << "0F"; |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 420 | } |
| 421 | else { |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 422 | OS << "0D"; |
| 423 | } |
| 424 | // Emit the encoded floating-point value. |
| 425 | if (constFP.getZExtValue() > 0) { |
| 426 | OS << constFP.toString(16, false); |
| 427 | } |
| 428 | else { |
| 429 | OS << "00000000"; |
| 430 | // If We have a double-precision zero, pad to 8-bytes. |
| 431 | if (!isFloat) { |
| 432 | OS << "00000000"; |
| 433 | } |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 434 | } |
| 435 | break; |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 436 | } |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 439 | void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum, |
| 440 | raw_ostream &OS, const char *Modifier) { |
| 441 | printOperand(MI, opNum, OS); |
| 442 | |
| 443 | if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0) |
| 444 | return; // don't print "+0" |
| 445 | |
| 446 | OS << "+"; |
| 447 | printOperand(MI, opNum+1, OS); |
| 448 | } |
| 449 | |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 450 | void PTXAsmPrinter::printParamOperand(const MachineInstr *MI, int opNum, |
| 451 | raw_ostream &OS, const char *Modifier) { |
Justin Holewinski | 27f08fc | 2011-09-23 14:18:22 +0000 | [diff] [blame] | 452 | const PTXMachineFunctionInfo *MFI = MI->getParent()->getParent()-> |
| 453 | getInfo<PTXMachineFunctionInfo>(); |
| 454 | OS << MFI->getParamManager().getParamName(MI->getOperand(opNum).getImm()); |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame] | 457 | void PTXAsmPrinter::printReturnOperand(const MachineInstr *MI, int opNum, |
| 458 | raw_ostream &OS, const char *Modifier) { |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 459 | //OS << RETURN_PREFIX << (int) MI->getOperand(opNum).getImm() + 1; |
| 460 | OS << "__ret"; |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Justin Holewinski | c1d8fbd | 2011-09-26 16:20:28 +0000 | [diff] [blame^] | 463 | void PTXAsmPrinter::printLocalOperand(const MachineInstr *MI, int opNum, |
| 464 | raw_ostream &OS, const char *Modifier) { |
| 465 | OS << "__local" << MI->getOperand(opNum).getImm(); |
| 466 | } |
| 467 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 468 | void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) { |
| 469 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 470 | if (EmitSpecialLLVMGlobal(gv)) |
| 471 | return; |
| 472 | |
| 473 | MCSymbol *gvsym = Mang->getSymbol(gv); |
| 474 | |
| 475 | assert(gvsym->isUndefined() && "Cannot define a symbol twice!"); |
| 476 | |
| 477 | std::string decl; |
| 478 | |
| 479 | // check if it is defined in some other translation unit |
| 480 | if (gv->isDeclaration()) |
| 481 | decl += ".extern "; |
| 482 | |
| 483 | // state space: e.g., .global |
| 484 | decl += "."; |
| 485 | decl += getStateSpaceName(gv->getType()->getAddressSpace()); |
| 486 | decl += " "; |
| 487 | |
| 488 | // alignment (optional) |
| 489 | unsigned alignment = gv->getAlignment(); |
| 490 | if (alignment != 0) { |
| 491 | decl += ".align "; |
| 492 | decl += utostr(Log2_32(gv->getAlignment())); |
| 493 | decl += " "; |
| 494 | } |
| 495 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 496 | |
Justin Holewinski | ae3ce17 | 2011-03-14 15:40:11 +0000 | [diff] [blame] | 497 | if (PointerType::classof(gv->getType())) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 498 | PointerType* pointerTy = dyn_cast<PointerType>(gv->getType()); |
| 499 | Type* elementTy = pointerTy->getElementType(); |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 500 | |
Justin Holewinski | ae3ce17 | 2011-03-14 15:40:11 +0000 | [diff] [blame] | 501 | decl += ".b8 "; |
| 502 | decl += gvsym->getName(); |
| 503 | decl += "["; |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 504 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 505 | if (elementTy->isArrayTy()) |
| 506 | { |
| 507 | assert(elementTy->isArrayTy() && "Only pointers to arrays are supported"); |
| 508 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 509 | ArrayType* arrayTy = dyn_cast<ArrayType>(elementTy); |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 510 | elementTy = arrayTy->getElementType(); |
| 511 | |
| 512 | unsigned numElements = arrayTy->getNumElements(); |
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 | while (elementTy->isArrayTy()) { |
| 515 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 516 | arrayTy = dyn_cast<ArrayType>(elementTy); |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 517 | elementTy = arrayTy->getElementType(); |
| 518 | |
| 519 | numElements *= arrayTy->getNumElements(); |
| 520 | } |
| 521 | |
| 522 | // FIXME: isPrimitiveType() == false for i16? |
| 523 | assert(elementTy->isSingleValueType() && |
| 524 | "Non-primitive types are not handled"); |
| 525 | |
| 526 | // Compute the size of the array, in bytes. |
| 527 | uint64_t arraySize = (elementTy->getPrimitiveSizeInBits() >> 3) |
| 528 | * numElements; |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 529 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 530 | decl += utostr(arraySize); |
| 531 | } |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 532 | |
Justin Holewinski | ae3ce17 | 2011-03-14 15:40:11 +0000 | [diff] [blame] | 533 | decl += "]"; |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 534 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 535 | // handle string constants (assume ConstantArray means string) |
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 | if (gv->hasInitializer()) |
| 538 | { |
Justin Holewinski | 297984d | 2011-09-22 16:45:40 +0000 | [diff] [blame] | 539 | const Constant *C = gv->getInitializer(); |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 540 | if (const ConstantArray *CA = dyn_cast<ConstantArray>(C)) |
| 541 | { |
| 542 | decl += " = {"; |
| 543 | |
| 544 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) |
| 545 | { |
| 546 | if (i > 0) decl += ","; |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 547 | |
| 548 | decl += "0x" + |
| 549 | utohexstr(cast<ConstantInt>(CA->getOperand(i))->getZExtValue()); |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 550 | } |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 551 | |
Justin Holewinski | 9583a86 | 2011-04-28 00:19:50 +0000 | [diff] [blame] | 552 | decl += "}"; |
| 553 | } |
| 554 | } |
Justin Holewinski | ae3ce17 | 2011-03-14 15:40:11 +0000 | [diff] [blame] | 555 | } |
| 556 | else { |
| 557 | // Note: this is currently the fall-through case and most likely generates |
| 558 | // incorrect code. |
| 559 | decl += getTypeName(gv->getType()); |
| 560 | decl += " "; |
| 561 | |
| 562 | decl += gvsym->getName(); |
| 563 | |
| 564 | if (ArrayType::classof(gv->getType()) || |
| 565 | PointerType::classof(gv->getType())) |
| 566 | decl += "[]"; |
| 567 | } |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 568 | |
| 569 | decl += ";"; |
| 570 | |
| 571 | OutStreamer.EmitRawText(Twine(decl)); |
| 572 | |
| 573 | OutStreamer.AddBlankLine(); |
| 574 | } |
| 575 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 576 | void PTXAsmPrinter::EmitFunctionDeclaration() { |
| 577 | // The function label could have already been emitted if two symbols end up |
| 578 | // conflicting due to asm renaming. Detect this and emit an error. |
| 579 | if (!CurrentFnSym->isUndefined()) { |
| 580 | report_fatal_error("'" + Twine(CurrentFnSym->getName()) + |
| 581 | "' label emitted multiple times to assembly file"); |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>(); |
Justin Holewinski | 27f08fc | 2011-09-23 14:18:22 +0000 | [diff] [blame] | 586 | const PTXParamManager &PM = MFI->getParamManager(); |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 587 | const bool isKernel = MFI->isKernel(); |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 588 | const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>(); |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 589 | const MachineRegisterInfo& MRI = MF->getRegInfo(); |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 590 | |
| 591 | std::string decl = isKernel ? ".entry" : ".func"; |
| 592 | |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame] | 593 | unsigned cnt = 0; |
| 594 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 595 | if (!isKernel) { |
| 596 | decl += " ("; |
Justin Holewinski | 27f08fc | 2011-09-23 14:18:22 +0000 | [diff] [blame] | 597 | if (ST.useParamSpaceForDeviceArgs()) { |
| 598 | for (PTXParamManager::param_iterator i = PM.ret_begin(), e = PM.ret_end(), |
| 599 | b = i; i != e; ++i) { |
| 600 | if (i != b) { |
| 601 | decl += ", "; |
| 602 | } |
| 603 | |
| 604 | decl += ".param .b"; |
| 605 | decl += utostr(PM.getParamSize(*i)); |
| 606 | decl += " "; |
| 607 | decl += PM.getParamName(*i); |
| 608 | } |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 609 | } else { |
Justin Holewinski | dc0baf9 | 2011-09-23 17:15:53 +0000 | [diff] [blame] | 610 | for (PTXMachineFunctionInfo::reg_iterator |
| 611 | i = MFI->retreg_begin(), e = MFI->retreg_end(), b = i; |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 612 | i != e; ++i) { |
| 613 | if (i != b) { |
| 614 | decl += ", "; |
| 615 | } |
| 616 | decl += ".reg ."; |
| 617 | decl += getRegisterTypeName(*i, MRI); |
| 618 | decl += " "; |
| 619 | decl += MFI->getRegisterName(*i); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 620 | } |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 621 | } |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 622 | decl += ")"; |
| 623 | } |
| 624 | |
| 625 | // Print function name |
| 626 | decl += " "; |
| 627 | decl += CurrentFnSym->getName().str(); |
| 628 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 629 | decl += " ("; |
| 630 | |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame] | 631 | cnt = 0; |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 632 | |
| 633 | // Print parameters |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 634 | if (isKernel || ST.useParamSpaceForDeviceArgs()) { |
Justin Holewinski | 27f08fc | 2011-09-23 14:18:22 +0000 | [diff] [blame] | 635 | for (PTXParamManager::param_iterator i = PM.arg_begin(), e = PM.arg_end(), |
| 636 | b = i; i != e; ++i) { |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 637 | if (i != b) { |
| 638 | decl += ", "; |
| 639 | } |
| 640 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 641 | decl += ".param .b"; |
Justin Holewinski | 27f08fc | 2011-09-23 14:18:22 +0000 | [diff] [blame] | 642 | decl += utostr(PM.getParamSize(*i)); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 643 | decl += " "; |
Justin Holewinski | 27f08fc | 2011-09-23 14:18:22 +0000 | [diff] [blame] | 644 | decl += PM.getParamName(*i); |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 645 | } |
| 646 | } else { |
| 647 | for (PTXMachineFunctionInfo::reg_iterator |
Justin Holewinski | dc0baf9 | 2011-09-23 17:15:53 +0000 | [diff] [blame] | 648 | i = MFI->argreg_begin(), e = MFI->argreg_end(), b = i; |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 649 | i != e; ++i) { |
| 650 | if (i != b) { |
| 651 | decl += ", "; |
| 652 | } |
| 653 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 654 | decl += ".reg ."; |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 655 | decl += getRegisterTypeName(*i, MRI); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 656 | decl += " "; |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 657 | decl += MFI->getRegisterName(*i); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 658 | } |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 659 | } |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 660 | decl += ")"; |
| 661 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 662 | OutStreamer.EmitRawText(Twine(decl)); |
| 663 | } |
| 664 | |
Che-Liang Chiou | c2ec0f9 | 2011-03-13 17:26:00 +0000 | [diff] [blame] | 665 | void PTXAsmPrinter:: |
| 666 | printPredicateOperand(const MachineInstr *MI, raw_ostream &O) { |
| 667 | int i = MI->findFirstPredOperandIdx(); |
| 668 | if (i == -1) |
| 669 | llvm_unreachable("missing predicate operand"); |
| 670 | |
| 671 | unsigned reg = MI->getOperand(i).getReg(); |
| 672 | int predOp = MI->getOperand(i+1).getImm(); |
Justin Holewinski | 297984d | 2011-09-22 16:45:40 +0000 | [diff] [blame] | 673 | const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>(); |
Che-Liang Chiou | c2ec0f9 | 2011-03-13 17:26:00 +0000 | [diff] [blame] | 674 | |
| 675 | DEBUG(dbgs() << "predicate: (" << reg << ", " << predOp << ")\n"); |
| 676 | |
Che-Liang Chiou | f78847e | 2011-03-14 11:26:01 +0000 | [diff] [blame] | 677 | if (reg != PTX::NoRegister) { |
Che-Liang Chiou | c2ec0f9 | 2011-03-13 17:26:00 +0000 | [diff] [blame] | 678 | O << '@'; |
| 679 | if (predOp == PTX::PRED_NEGATE) |
| 680 | O << '!'; |
Justin Holewinski | 297984d | 2011-09-22 16:45:40 +0000 | [diff] [blame] | 681 | O << MFI->getRegisterName(reg); |
Che-Liang Chiou | c2ec0f9 | 2011-03-13 17:26:00 +0000 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 685 | void PTXAsmPrinter:: |
| 686 | printCall(const MachineInstr *MI, raw_ostream &O) { |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 687 | O << "\tcall.uni\t"; |
Justin Holewinski | 75d8095 | 2011-09-23 16:48:41 +0000 | [diff] [blame] | 688 | // The first two operands are the predicate slot |
| 689 | unsigned Index = 2; |
| 690 | while (!MI->getOperand(Index).isGlobal()) { |
| 691 | if (Index == 2) { |
| 692 | O << "("; |
| 693 | } else { |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 694 | O << ", "; |
| 695 | } |
Justin Holewinski | 75d8095 | 2011-09-23 16:48:41 +0000 | [diff] [blame] | 696 | printParamOperand(MI, Index, O); |
| 697 | Index++; |
| 698 | } |
| 699 | |
| 700 | if (Index != 2) { |
| 701 | O << "), "; |
| 702 | } |
| 703 | |
| 704 | assert(MI->getOperand(Index).isGlobal() && |
| 705 | "A GlobalAddress must follow the return arguments"); |
| 706 | |
| 707 | const GlobalValue *Address = MI->getOperand(Index).getGlobal(); |
| 708 | O << Address->getName() << ", ("; |
| 709 | Index++; |
| 710 | |
| 711 | while (Index < MI->getNumOperands()) { |
| 712 | printParamOperand(MI, Index, O); |
| 713 | if (Index < MI->getNumOperands()-1) { |
| 714 | O << ", "; |
| 715 | } |
| 716 | Index++; |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | O << ")"; |
| 720 | } |
| 721 | |
Justin Holewinski | 4799729 | 2011-06-24 19:19:18 +0000 | [diff] [blame] | 722 | unsigned PTXAsmPrinter::GetOrCreateSourceID(StringRef FileName, |
| 723 | StringRef DirName) { |
| 724 | // If FE did not provide a file name, then assume stdin. |
| 725 | if (FileName.empty()) |
| 726 | return GetOrCreateSourceID("<stdin>", StringRef()); |
| 727 | |
| 728 | // MCStream expects full path name as filename. |
| 729 | if (!DirName.empty() && !sys::path::is_absolute(FileName)) { |
| 730 | SmallString<128> FullPathName = DirName; |
| 731 | sys::path::append(FullPathName, FileName); |
| 732 | // Here FullPathName will be copied into StringMap by GetOrCreateSourceID. |
| 733 | return GetOrCreateSourceID(StringRef(FullPathName), StringRef()); |
| 734 | } |
| 735 | |
| 736 | StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName); |
| 737 | if (Entry.getValue()) |
| 738 | return Entry.getValue(); |
| 739 | |
| 740 | unsigned SrcId = SourceIdMap.size(); |
| 741 | Entry.setValue(SrcId); |
| 742 | |
| 743 | // Print out a .file directive to specify files for .loc directives. |
| 744 | OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey()); |
| 745 | |
| 746 | return SrcId; |
| 747 | } |
| 748 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 749 | #include "PTXGenAsmWriter.inc" |
| 750 | |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 751 | // Force static initialization. |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 752 | extern "C" void LLVMInitializePTXAsmPrinter() { |
Justin Holewinski | e1fee48 | 2011-04-20 15:37:17 +0000 | [diff] [blame] | 753 | RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target); |
| 754 | RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target); |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 755 | } |